%  CIS601
%  Fall'o4
%  Sumit Basu
%  Lab1 09/13/04
%  This Matlab program reads in a file cameraman.tif,
%  creates a AVI file sumit.avi,
%  reduces the picture and then increases it back
%  flips the picture horizentally and turns it back
%  flips the picture vertically and turns it back

%  Delete existing file
delete 'sumit.avi';

%  Clear all previous variables
clear all;

%  Read in image
I = imread('cameraman.tif');
I1 = zeros(256,256,3);
I1(:,:,1) = I(:,:);
I1(:,:,2) = I(:,:);
I1(:,:,3) = I(:,:);

%  Open AVI file
aviobj = avifile('sumit','fps','1', 'compression', 'Indeo5');
I1 = double(I1)/255;
J1 = I1;
frameObj = im2frame(I1);
aviobj = addframe(aviobj,frameObj);

%  reduces the picture and then increases it back
for i = 1:1:50
    fp = I(1:i:end,1:i:end);
    sz = size(fp,1);
    sz2 = size(fp,2);
    I1 = zeros(256,256,3);
    I1(1:sz,1:sz2,1) = fp(:,:);
    I1(1:sz,1:sz2,2) = fp(:,:);
    I1(1:sz,1:sz2,3) = fp(:,:);
    I1 = double(I1)/255;
    frameObj = im2frame(I1);
    aviobj = addframe(aviobj,frameObj);
    if (i == 2)||(i == 4)||(i == 8)
       for j = 1:20
           aviobj = addframe(aviobj,frameObj);
       end
   end
end
for i = 50:-1:1
    fp = I(1:i:end,1:i:end);
    sz = size(fp,1);
    sz2 = size(fp,2);
    I1 = zeros(256,256,3);
    I1(1:sz,1:sz2,1) = fp(:,:);
    I1(1:sz,1:sz2,2) = fp(:,:);
    I1(1:sz,1:sz2,3) = fp(:,:);
    I1 = double(I1)/255;
    frameObj = im2frame(I1);
    aviobj = addframe(aviobj,frameObj);
    if (i == 2)||(i == 4)||(i == 6)||(i == 8)||(i == 10)
       for j = 1:20
           aviobj = addframe(aviobj,frameObj);
       end
   end
end
for i = 1:50
    aviobj = addframe(aviobj,frameObj);
end
frameObj = im2frame(J1);
for i = 1:25
    aviobj = addframe(aviobj,frameObj);
end

%  flips the picture horizentally and turns it back
for i = 1:1:50
    fp = I(end:-i:1,:);
    sz = size(fp,1);
    I1 = zeros(256,256,3);
    I1(1:sz,:,1) = fp(:,:);
    I1(1:sz,:,2) = fp(:,:);
    I1(1:sz,:,3) = fp(:,:);
    I1 = double(I1)/255;
    frameObj = im2frame(I1);
    aviobj = addframe(aviobj,frameObj);
    if (i == 2)||(i == 4)||(i == 6)||(i == 8)||(i == 10)
       for j = 1:20
           aviobj = addframe(aviobj,frameObj);
       end
   end
end
for i = 1:50
    aviobj = addframe(aviobj,frameObj);
end
for i = 50:-1:1
    fp = I(end:-i:1,:);
    sz = size(fp,1);
    I1 = zeros(256,256,3);
    I1(1:sz,:,1) = fp(:,:);
    I1(1:sz,:,2) = fp(:,:);
    I1(1:sz,:,3) = fp(:,:);
    I1 = double(I1)/255;
    frameObj = im2frame(I1);
    aviobj = addframe(aviobj,frameObj);
    if (i == 2)||(i == 4)||(i == 6)||(i == 8)||(i == 10)
       for j = 1:20
           aviobj = addframe(aviobj,frameObj);
       end
   end
end
for i = 1:50
    aviobj = addframe(aviobj,frameObj);
end
frameObj = im2frame(J1);
for i = 1:25
    aviobj = addframe(aviobj,frameObj);
end

%  flips the picture vertically and turns it back
for i = 1:1:50
    fp = I(:,end:-i:1);
    sz = size(fp,2);
    I1 = zeros(256,256,3);
    I1(:,1:sz,1) = fp(:,:);
    I1(:,1:sz,2) = fp(:,:);
    I1(:,1:sz,3) = fp(:,:);
    I1 = double(I1)/255;
    frameObj = im2frame(I1);
    aviobj = addframe(aviobj,frameObj);
    if (i == 2)||(i == 4)||(i == 6)||(i == 8)||(i == 10)
       for j = 1:20
           aviobj = addframe(aviobj,frameObj);
       end
   end
end
for i = 1:25
    aviobj = addframe(aviobj,frameObj);
end
for i = 50:-1:1
    fp = I(:,end:-i:1);
    sz = size(fp,2);
    I1 = zeros(256,256,3);
    I1(:,1:sz,1) = fp(:,:);
    I1(:,1:sz,2) = fp(:,:);
    I1(:,1:sz,3) = fp(:,:);
    I1 = double(I1)/255;
    frameObj = im2frame(I1);
    aviobj = addframe(aviobj,frameObj);
    if (i == 2)||(i == 4)||(i == 6)||(i == 8)||(i == 10)
       for j = 1:20
           aviobj = addframe(aviobj,frameObj);
       end
   end
end
for i = 1:25
    aviobj = addframe(aviobj,frameObj);
end
frameObj = im2frame(J1);
for i = 1:25
    aviobj = addframe(aviobj,frameObj);
end

%  Close and save AVI file
aviobj = close(aviobj);