% Description:
%
%       blend7.m blends the blocks from the different exposures.
%       For scenes with less than 7 exposures, repeat an image used
%       earlier.
%
% Input:
%
%       d - side of a block
%       sigma - width of the Gaussian
%       index - index of which image the chosen blocks come from
%       imageRGB* - image in RGB
%
% Output:
%
%       finalRGB - image after blending
%

function [finalRGB] = ... 
    blend7(d,sigma,index,imageRGB1,imageRGB2,imageRGB3,imageRGB4,imageRGB5,imageRGB6,imageRGB7)

fix(clock)

[irow, icol, irgb] = size(imageRGB1);
finalLAB = zeros(irow, icol, irgb);
nr = irow/d;
nc = icol/d;

imageRGB1 = double(imageRGB1);
imageRGB2 = double(imageRGB2);
imageRGB3 = double(imageRGB3);
imageRGB4 = double(imageRGB4);
imageRGB5 = double(imageRGB5);
imageRGB6 = double(imageRGB6);
imageRGB7 = double(imageRGB7);
imageLAB1 = imageRGB2LAB(imageRGB1);
imageLAB2 = imageRGB2LAB(imageRGB2);
imageLAB3 = imageRGB2LAB(imageRGB3);
imageLAB4 = imageRGB2LAB(imageRGB4);
imageLAB5 = imageRGB2LAB(imageRGB5);
imageLAB6 = imageRGB2LAB(imageRGB6);
imageLAB7 = imageRGB2LAB(imageRGB7);

MasterTable = GaussianTable(d,sigma,nr,nc);
table = MasterTable;

for a = 1:1:nr,
    
    % Creates table / Shifts table down
    if a == 1
        LocalTable = MasterTable;
    else
        LocalTable(d*(a-1)+1:d*nr,:) = MasterTable(1:d*nr-d*(a-1),:);
        for row = 1:1:d*(a-1),
            LocalTable(row,:) = MasterTable(d*a+1-row,:);    
        end
    end
    % End
    
    for b = 1:1:nc,
        
        % Shifts table right
        if b == 1
            table = LocalTable;
        else
            table(:,d*(b-1)+1:d*nc) = LocalTable(:,1:d*nc-d*(b-1));
            for col = 1:1:d*(b-1),
                table(:,col) = LocalTable(:,d*b+1-col);    
            end
        end
        % End
        
        for e = 1:1:d,
            for f = 1:1:d,
        
                x = d*(a-1)+e;
                y = d*(b-1)+f;
                
                for j = 1:1:nr,
                    for k = 1:1:nc,
                
                        switch index(j,k)
                            case 1
                            finalLAB(x,y,:) = ... 
                                finalLAB(x,y,:) + ... 
                                table((d*(j-1)+e),(d*(k-1)+f))*imageLAB1(x,y,:);
                            case 2
                            finalLAB(x,y,:) = ... 
                                finalLAB(x,y,:) + ... 
                                table((d*(j-1)+e),(d*(k-1)+f))*imageLAB2(x,y,:);
                            case 3
                            finalLAB(x,y,:) = ... 
                                finalLAB(x,y,:) + ...  
                                table((d*(j-1)+e),(d*(k-1)+f))*imageLAB3(x,y,:);
                            case 4
                            finalLAB(x,y,:) = ... 
                                finalLAB(x,y,:) + ...  
                                table((d*(j-1)+e),(d*(k-1)+f))*imageLAB4(x,y,:);
                            case 5
                            finalLAB(x,y,:) = ... 
                                finalLAB(x,y,:) + ...  
                                table((d*(j-1)+e),(d*(k-1)+f))*imageLAB5(x,y,:);
                            case 6
                            finalLAB(x,y,:) = ... 
                                finalLAB(x,y,:) + ...  
                                table((d*(j-1)+e),(d*(k-1)+f))*imageLAB6(x,y,:);
                            case 7
                            finalLAB(x,y,:) = ... 
                                finalLAB(x,y,:) + ...  
                                table((d*(j-1)+e),(d*(k-1)+f))*imageLAB7(x,y,:);
                            otherwise
                                ;
                        end
                        
                    end
                end
                
                den = 0;
                for m = 1:1:nr,
                    for n = 1:1:nc,
                        den = den + table((d*(m-1)+e),(d*(n-1)+f));
                    end
                end
                finalLAB(x,y,:) = finalLAB(x,y,:)/den;
        
            end
        end
        
    end
end

finalRGB = uint8(imageLAB2RGB(finalLAB));

fix(clock)