% Opens a directory, finds all the GIF image files, and then displays
% them.

% It then displays an average image, based on the smallest image size
% found.


close all;

% Read in the file names and info about them
files = dir('rotated/*.gif');

fprintf('Info on first file found.\n');
disp(files(1));

rowmin = 1000;  colmin = 1000;
avg_img = zeros(rowmin, colmin);


for (i = 1:length(files))
    figure(1);
    rgb = imread(sprintf('rotated/%s', files(i).name));
    imshow(rgb);
    pause(0.5);  % pause for 0.5 secs
    
    % problem showing the image: we need to map from the colors stored in
    % the file
    [rgb, map] = imread(sprintf('rotated/%s', files(i).name));
    imshow(rgb, map);    % show the filename and size in the title
    title(sprintf('%s - %d x %d', files(i).name, size(rgb)), 'FontSize', 16);

    pause(0.5);  % pause for 0.5 secs
    
    % track the minimum size found
    figure(2);
    rowmin = min(rowmin, size(rgb, 1));
    colmin = min(colmin, size(rgb, 2));
    
    % cut down the current average image to the smallest current size
    avg_img = avg_img(1:rowmin, 1:colmin);
    
    % Create a greyscale version of the image (removing the color map)
    grayscale = ind2rgb(rgb, map);
    avg_img = avg_img + grayscale(1:rowmin, 1:colmin, 1);
    imshow(avg_img/i);
    title('Average of Preceding Images', 'Fontsize', 16);
    
end

% plotting 