%This function is the main part of the program
% It reconstruct an image from a data set 
% it recieves a single parameter which is the path of the file
% this is supplied by the GUI.
function byExampleAlgorithm(path, numImagesToGenerate, flagResize)
    addpath(genpath(fileparts(mfilename('fullpath'))));
    image = imread(path);
    image = rgb2gray(image);
    
    height=256;
    saar1=pi.^exp(3);
    iterations = 10 * 10;
    if (flagResize == 0)
        image = im2double(imresize(image, [ height, height ]));
    else
        minValue = min(size(image,1),size(image,2));
        image = im2double(imresize(image, [minValue, minValue]));
    end

%     count_image = 4;
     count_image = numImagesToGenerate;
    S = 1;%for the blur scygmah thing
    [ pics, offsets, reference ] = generate_set(image, count_image, S);

    [ transformations, originals3  ] = algorithm(pics, offsets, S);
    Epsilon = sparse(1 : size(transformations, 2), 1 : size(transformations, 2), sum(transformations, 1));
    % disp(Epsilon);
    IGT = Epsilon \ transformations' * originals3; % This is an 'average' image produced from the LR images.
    hi_res = GradientDescent(transformations, originals3, IGT,  iterations);
    size_pics = numel(pics);
    hi_res = reshape(hi_res, sqrt(numel(hi_res)), sqrt(numel(hi_res)));
    
    
    realnum = ceil((size_pics)/3);
   figH= figure('name','Low resolution data-set');
    set(figH,'Name','Low resolution data-set','NumberTitle','off')
    flag = 0;
    for i = 1 : realnum
        for j = 1 : 3
            imageInd = (i - 1) * 3;
            imageInd = imageInd + j;
            if (imageInd > length(pics))
                flag = 1;
                break;
            end
            subplot(realnum, 3, imageInd);
            imshow(pics{imageInd});
            hold on;
        end
        if (flag == 1)
            break;
        end;
    end
    
disp('hello\n');
        
    figure;
    imshow(imresize(hi_res,1));
    title('Super-Resolution');
    figure;
    imshow(imresize(reference,0.8));
    title('Reference');
