%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function 'makeMovie' creates UNCOMPRESSED .avi file which contains motion of small matrix imposed onto big matrix, and 
% the motion is governed by left-top coordinates of the small matrix read from an input file. It can also created UNCOMPRESSED .avi 
% file from given set of .bmp images starting with 'framedump_00001.bmp' with total number of images as the input parameter.

% Programmer : Nilesh Ghubade (nileshg@temple.edu)
% Date : Aug 2002 (modified in Sept 2002)
% Advisor : Dr.Longin Jan Latecki (latecki@temple.edu )
% Project : Video Analysis (extracting key frames from a given movie file)
% Department : Computer and Information Science Department, Temple University, Philadelphia, PA-19122, USA

% Input arguments:-
% posCoordsFileORBMPImagesDiry = name of the file that contains 'row' and 'column' values for the position of left-top corner of the 
%                                small matrix OR path of the directory containing set of .bmp images starting with 
%                                'framedump_00001.bmp'.
% noOfImages = total number of .bmp images that collectively would form the .avi movie. It should be '0' when .txt file is given as
%               input that contains 'row' and 'column' values for the position of left-top corner of the small matrix.

% Output : -
% aviFileName = the name of the .avi file that contains the movie.

% Example : -
% 1) makeMovie('coords.txt',0)
% 2) makeMovie('MrBeans/Images/',10)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [aviFileName] = makeMovie(posCoordsFileORBMPImagesDiry,noOfImages)

% The 'if' condition is true when input is .txt file containing 'row' and 'column' values for the position of left-top corner of 
% the small matrix. The corresponding 'else' condition handles making of movie from given set of .bmp images.

if (findstr(posCoordsFileORBMPImagesDiry,'.txt'))
    positionCoordsMat = load(posCoordsFileORBMPImagesDiry);
    [maxValues,indices] = max(positionCoordsMat);
    maxRowCoord = maxValues(1);
    maxColCoord = maxValues(2);
    smallMatrixRows = 3;
    smallMatrixCols = 3;
    bigMatrixRows = maxRowCoord;
    bigMatrixCols = maxColCoord;
    bigMatrixRows = bigMatrixRows + rem(bigMatrixRows,4);
    bigMatrixCols = bigMatrixCols + rem(bigMatrixCols,4);

    if (bigMatrixRows <= 32) 
        bigMatrixRows = 32;
    end

    if (bigMatrixCols <= 32) 
        bigMatrixCols = 32;
    end

    smallMatrix = ones(smallMatrixRows,smallMatrixCols); % initialize 'smallMatrix' with all 1's.

    framesPerSec = 5; % 'frames per second' parameter for the .avi file.

    aviFileName = strtok(posCoordsFileORBMPImagesDiry,'.')

    aviobj = avifile(aviFileName,'fps',framesPerSec,'COMPRESSION','NONE'); % initialize .avi file object.

        bigMatrix = zeros(bigMatrixRows,bigMatrixCols);
    % for loop for creating each frame of the .avi file.
    for count=1:size(positionCoordsMat,1)

        leftTopRow = positionCoordsMat(count,1);
        leftTopCol = positionCoordsMat(count,2);
        position(1) = leftTopRow;
        position(2) = leftTopCol;
        [bigMatrix] = imposingSmallMatOntoBigMat(smallMatrix,bigMatrix,position); % this function superimposes 'smallMatrix' onto 'bigMatrix'
                                % at given position with respect to the left-top corner of 'smallMatrix'.

    % all 3 color intensities (red, green and blue) of 'bigMatrix' are given same values to form a true color image.                                
        bigMatrixImg(:,:,1) = bigMatrix;
        bigMatrixImg(:,:,2) = bigMatrix;
        bigMatrixImg(:,:,3) = bigMatrix;

        if ((1 <= count) & (count <= 9))
             imageFileNamePrefix = 'framedump_0000';
        else if ((10 <= count) & (count <= 99))
                 imageFileNamePrefix = 'framedump_000';
            else imageFileNamePrefix = 'framedump_00'; 
            end
        end

        fileName = strcat(imageFileNamePrefix,num2str(count),'.bmp');
 

        imwrite(bigMatrixImg,fileName,'bmp');
        imgObj = imread(fileName,'bmp');
        frameObj = im2frame(imgObj);  
        aviobj = addframe(aviobj,frameObj);
        frame(count) = frameObj;
        delete(fileName);
    end
    aviobj = close(aviobj);
    movie(frame,1);
    
    
% This 'else' condition handles making of the movie from given set of .bmp images.    
else
    aviFileName = 'movie.avi';
    framesPerSec = 5; % 'frames per second' parameter for the .avi file.
    aviobj = avifile(aviFileName,'fps',framesPerSec,'COMPRESSION','NONE'); % initialize .avi file object.
    for count = 1:noOfImages
    
        if ((1 <= count) & (count <= 9))
             imageFileNamePrefix = 'framedump_0000';
        else if ((10 <= count) & (count <= 99))
                 imageFileNamePrefix = 'framedump_000';
            else imageFileNamePrefix = 'framedump_00'; 
            end
        end

        fileName = strcat(imageFileNamePrefix,num2str(count),'.bmp');
 

        imgObj = imread(fileName,'bmp');
        frameObj = im2frame(imgObj);  
        aviobj = addframe(aviobj,frameObj);
        frame(count) = frameObj;
    end
    aviobj = close(aviobj);
    movie(frame,1);
    
end