% Description:
%
%       choose.m produces a mosaic of the best blocks and returns an index 
%       of which image the chosen blocks come from.
%       This function should be used for only 2 exposures.
%
% Input:
%
%       d - side of a block
%       image* - image at a given exposure
%
% Output:
%
%       final - mosaic of blocks from the 2 different images
%       index - index of which image the chosen blocks come from
%

function [final index] = choose(d,image1,image2)

[row, col, rgb] = size(image1);
index = zeros(row/d, col/d);
final = zeros(row, col, rgb);

for j = 1:1:row/d;
    for k = 1:1:col/d;
        
        E = -1;
        
        Ecur = entropyC(image1((j-1)*d+1:j*d,(k-1)*d+1:k*d,:));
        if Ecur > E
            E = Ecur;
            ntemp = 1;
            final((j-1)*d+1:j*d,(k-1)*d+1:k*d,:) = ... 
                image1((j-1)*d+1:j*d,(k-1)*d+1:k*d,:);
        end
        
        Ecur = entropyC(image2((j-1)*d+1:j*d,(k-1)*d+1:k*d,:));
        if Ecur > E
            E = Ecur;
            ntemp = 2;
            final((j-1)*d+1:j*d,(k-1)*d+1:k*d,:) = ... 
                image2((j-1)*d+1:j*d,(k-1)*d+1:k*d,:);
        end
        
        index(j,k) = ntemp;
        
    end
end

final = uint8(final);