
clear all;
I_path          = 'woman.jpg';
%%
% aspectRatio = h/w;
aspectRatio    = 6/5;
wRT            = 1;
wVB            = 0.3;
sThresh        = 1;
minSR          = 100;
scaleUp        = 1;
goldenSection  = true;

%% Read Image
I_rgb = im2double(imread(I_path));

%% Calculate preliminary calculation-heavy features (edges, objects)
[colorMap, colorMask]   = colorSegment(I_rgb);
I_rgb                   = im2double(I_rgb);

I                       = rgb2gray(I_rgb);
I                       = imadjust(I.^2.5 / 0.2^0.5, [], [], 1/2.2);

[segmentGraph, uniqueLabels, segMasks]  = segmentImage(I);      % segment Image



%% these things can be cropped and passed on
Iedge                                   = segEdges(segMasks,minSR/2);   % make edge map
saliencyMap                             = getSalience(I_rgb);   % find saliency



%% Once the image is cropped (along with I, Iedge, saliencyMap, and segmentGraph), these
%  things have to be computed again:
[objectMap,salientRegion]   = objectSalience(saliencyMap,segMasks);
%%
[object,objectS]     = separateObjects(colorMap,segMasks,objectMap,salientRegion,sThresh,minSR);
%%
bestLines            = getLines(I,Iedge,true);



%% Display original Image
imtool(I_rgb);

%% Calculate Scores!!!!
if(goldenSection),
    origRT          = rule_golden(I, object, bestLines);
else
    origRT          = rule_thirds(I, object, bestLines);
end
[origDA,noDiag] = rule_diag(I, bestLines);
origVB          = rule_visbal(I, object);

%% calculate total score!!!
if(noDiag)
    wDA = 0;
else
    wDA = 1;
end

origScore = (wRT*origRT + wDA*origDA + wVB*origVB)/(wRT + wDA + wVB);

%% calculate appropriate output width and height based on aspect ratio
[baseNewH, baseNewW] = aspect2dim(I,aspectRatio);  
%%

% number of rows we can translate down
[h,w] = size(I);
clear scaledCrop;
scaledCrop = struct('startX',1,'startY',1,'width',1,'height',1,'score',0);
numScaled = 1;
%%
for thisSize = 1:0.025:scaleUp,
    
    newHeight = round(baseNewH/thisSize);
    newWidth  = round(baseNewW/thisSize);
    lastH = (h - newHeight) + 1;
    lastW = (w - newWidth) + 1;
    clear finalCrops;
    finalCrops = struct('startX',1,'startY',1,'width',1,'height',1,'score',0);
    
    for curY = 1:lastH,
        for curX = 1:lastW,
            % recropping stuff
            cropSegGraph        = imcrop(segmentGraph,[curX,curY,newWidth,newHeight]);
            cropSaliencyMap     = imcrop(saliencyMap,[curX,curY,newWidth,newHeight]);
            cropColorMap        = imcrop(colorMap,[curX,curY,newWidth,newHeight]);
            cropIedge           = imcrop(Iedge,[curX,curY,newWidth,newHeight]);
            cropI               = imcrop(I,[curX,curY,newWidth,newHeight]);

            % recalculating stuff
            [cropUniqueLabels, cropSegMasks]    = makeSegMasks(cropSegGraph);
            [cropObjectMap, cropSalientRegion]  = objectSalience(cropSaliencyMap,cropSegMasks);
            [cropObjects, cropObjectSalience]   = separateObjects(cropColorMap,cropSegMasks,cropObjectMap,cropSalientRegion,sThresh,minSR);
            cropLines                           = getLines(cropI,cropIedge);
            
            if(goldenSection),
                cropRT              = rule_golden(cropI, cropObjects, cropLines);
            else
                cropRT              = rule_thirds(cropI, cropObjects, cropLines);
            end
            [cropDA,cropNoDiag] = rule_diag(cropI, cropLines);
            cropVB              = rule_visbal(cropI, cropObjects);

            if(cropNoDiag)
                cropWDA = 0;
            else
                cropWDA = 1;
            end

            cropScore = (wRT*cropRT + cropWDA*cropDA + wVB*cropVB)/(wRT + cropWDA + wVB);

            disp([num2str(curX + (curY-1)*lastW),'/',num2str(lastH*lastW)]);
            finalCrops(curX + (curY-1)*lastW).startX    = curX;
            finalCrops(curX + (curY-1)*lastW).startY    = curY;
            finalCrops(curX + (curY-1)*lastW).width     = newWidth;
            finalCrops(curX + (curY-1)*lastW).height    = newHeight;
            finalCrops(curX + (curY-1)*lastW).score     = cropScore;
            finalCrops(curX + (curY-1)*lastW).RT        = cropRT;
            finalCrops(curX + (curY-1)*lastW).DA        = cropDA;
            finalCrops(curX + (curY-1)*lastW).VB        = cropVB;
            
            clear cropSegGraph cropSaliencyMap cropColorMap cropIedge cropI cropUniqueLabels cropSegMasks cropObjectMap cropSalientRegion cropObjects cropObjectSalience cropLines;
        end
    end
    [m, best] = max([finalCrops.score]);
    imtool(imcrop(I_rgb,[finalCrops(best).startX,finalCrops(best).startY,finalCrops(best).width,finalCrops(best).height]));
    scaledCrop(numScaled).startX    = finalCrops(best).startX;
    scaledCrop(numScaled).startY    = finalCrops(best).startY;
    scaledCrop(numScaled).width     = finalCrops(best).width;
    scaledCrop(numScaled).height    = finalCrops(best).height;
    scaledCrop(numScaled).score     = finalCrops(best).score;
    scaledCrop(numScaled).RT        = finalCrops(best).RT;
    scaledCrop(numScaled).DA        = finalCrops(best).DA;
    scaledCrop(numScaled).VB        = finalCrops(best).VB;
    
    numScaled = numScaled + 1;
end

%%

