% Description:
%
%       optimize7.m attempts to find the optimum values for the size of a
%       block, the width of the Gaussians, and returns the optimized image
%       For scenes with less than 7 exposures, repeat an image used
%       earlier.
%       Note that this optimization doesn't always work so be careful.
%       Also, each blend takes 15+ minutes so be prepared to wait.
%       
% Input:
%
%       image* - image at a given exposure
%
% Output:
%
%       dopt - optimized side of a block
%       sigmaopt - optimized blending width for the Gaussians
%       final - optimized image
%

function [dopt sigmaopt final] = optimize7(image1,image2,image3,image4,image5,image6,image7)

dtemp = 160;
sigmatemp = 160;
delta = 32;

[row col rgb] = size(image1);

% Find dopt

dtemp
    
little1 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
little1(1:row,1:col,:) = image1;
little2 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
little2(1:row,1:col,:) = image2;
little3 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
little3(1:row,1:col,:) = image3;
little4 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
little4(1:row,1:col,:) = image4;
little5 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
little5(1:row,1:col,:) = image5;
little6 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
little6(1:row,1:col,:) = image6;
little7 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
little7(1:row,1:col,:) = image7;
   
[index] = choose7(dtemp,little1,little2,little3,little4,little5,little6,little7);
final = blend7(dtemp,sigmatemp,index,little1,little2,little3,little4,little5,little6,little7);
final = final(1:row,1:col,:);
Ecur = entropyC(final)

dtemp = dtemp-delta
    
little1 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
little1(1:row,1:col,:) = image1;
little2 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
little2(1:row,1:col,:) = image2;
little3 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
little3(1:row,1:col,:) = image3;
little4 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
little4(1:row,1:col,:) = image4;
little5 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
little5(1:row,1:col,:) = image5;
little6 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
little6(1:row,1:col,:) = image6;
little7 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
little7(1:row,1:col,:) = image7;
    
[index] = choose7(dtemp,little1,little2,little3,little4,little5,little6,little7);
final = blend7(dtemp,sigmatemp,index,little1,little2,little3,little4,little5,little6,little7);
final = final(1:row,1:col,:);
Ebot = entropyC(final)

dtemp = dtemp+2*delta
    
little1 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
little1(1:row,1:col,:) = image1;
little2 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
little2(1:row,1:col,:) = image2;
little3 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
little3(1:row,1:col,:) = image3;
little4 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
little4(1:row,1:col,:) = image4;
little5 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
little5(1:row,1:col,:) = image5;
little6 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
little6(1:row,1:col,:) = image6;
little7 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
little7(1:row,1:col,:) = image7;
    
[index] = choose7(dtemp,little1,little2,little3,little4,little5,little6,little7);
final = blend7(dtemp,sigmatemp,index,little1,little2,little3,little4,little5,little6,little7);
final = final(1:row,1:col,:);
Etop = entropyC(final)

dtemp = dtemp-delta;

if (Ecur > Ebot) && (Ecur > Etop)
    dopt = dtemp;
elseif (Ebot > Ecur)
        
        Eprev = Ecur;
        Ecur = Ebot;
        dtemp = dtemp-delta;
        
        while Ecur > Eprev
    
            dtemp = dtemp-delta
    
            little1 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
            little1(1:row,1:col,:) = image1;
            little2 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
            little2(1:row,1:col,:) = image2;
            little3 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
            little3(1:row,1:col,:) = image3;
            little4 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
            little4(1:row,1:col,:) = image4;
            little5 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
            little5(1:row,1:col,:) = image5;
            little6 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
            little6(1:row,1:col,:) = image6;
            little7 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
            little7(1:row,1:col,:) = image7;
 
            [index] = choose7(dtemp,little1,little2,little3,little4,little5,little6,little7);
            final = blend7(dtemp,sigmatemp,index,little1,little2,little3,little4,little5,little6,little7);
            final = final(1:row,1:col,:);
            Eprev = Ecur;
            Ecur = entropyC(final)
    
        end

        dopt = dtemp+delta;

else
    
        Eprev = Ecur;
        Ecur = Etop;
        dtemp = dtemp+delta;
        
        while Ecur > Eprev
    
            dtemp = dtemp+delta
    
            little1 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
            little1(1:row,1:col,:) = image1;
            little2 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
            little2(1:row,1:col,:) = image2;
            little3 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
            little3(1:row,1:col,:) = image3;
            little4 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
            little4(1:row,1:col,:) = image4;
            little5 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
            little5(1:row,1:col,:) = image5;
            little6 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
            little6(1:row,1:col,:) = image6;
            little7 = zeros(row+dtemp-mod(row,dtemp),col+dtemp-mod(col,dtemp),rgb);
            little7(1:row,1:col,:) = image7;
     
            [index] = choose7(dtemp,little1,little2,little3,little4,little5,little6,little7);
            final = blend7(dtemp,sigmatemp,index,little1,little2,little3,little4,little5,little6,little7);
            final = final(1:row,1:col,:);
            Eprev = Ecur;
            Ecur = entropyC(final)
    
        end

        dopt = dtemp-delta;
        
end

% End


% Find sigmaopt

little1 = zeros(row+dopt-mod(row,dopt),col+dopt-mod(col,dopt),rgb);
little1(1:row,1:col,:) = image1;
little2 = zeros(row+dopt-mod(row,dopt),col+dopt-mod(col,dopt),rgb);
little2(1:row,1:col,:) = image2;
little3 = zeros(row+dopt-mod(row,dopt),col+dopt-mod(col,dopt),rgb);
little3(1:row,1:col,:) = image3;
little4 = zeros(row+dopt-mod(row,dopt),col+dopt-mod(col,dopt),rgb);
little4(1:row,1:col,:) = image4;
little5 = zeros(row+dopt-mod(row,dopt),col+dopt-mod(col,dopt),rgb);
little5(1:row,1:col,:) = image5;
little6 = zeros(row+dopt-mod(row,dopt),col+dopt-mod(col,dopt),rgb);
little6(1:row,1:col,:) = image6;
little7 = zeros(row+dopt-mod(row,dopt),col+dopt-mod(col,dopt),rgb);
little7(1:row,1:col,:) = image7; 
[index] = choose7(dopt,little1,little2,little3,little4,little5,little6,little7)

sigmatemp = dopt+delta

final = blend7(dopt,sigmatemp,index,little1,little2,little3,little4,little5,little6,little7);
final = final(1:row,1:col,:);
Eprev = entropyC(final)
 
sigmatemp = sigmatemp+delta

final = blend7(dopt,sigmatemp,index,little1,little2,little3,little4,little5,little6,little7);
final = final(1:row,1:col,:);
Ecur = entropyC(final)
    
while (Ecur > Eprev) && (sigmatemp < 3*dopt)

    sigmatemp = sigmatemp+delta

    final = blend7(dopt,sigmatemp,index,little1,little2,little3,little4,little5,little6,little7);
    final = final(1:row,1:col,:);
    Eprev = Ecur;
    Ecur = entropyC(final)

end

sigmaopt = sigmatemp-delta;

% End


final = blend7(dopt,sigmaopt,index,little1,little2,little3,little4,little5,little6,little7);
final = final(1:row,1:col,:);