% HW 1
% Erickson Miranda

X = imread('philadelphia-eagles.jpg');
Y = imread('philadelphia-helmet.jpg');
[X,map] = rgb2ind(X,4,'nodither');
Y = rgb2ind(Y,map); 
X = flipud(X); % Matlab 7 bug, confirmed in MathWorks website that
Y = flipud(Y); % im2frame flips the image when used with movie function
i = 0;
for j=1:50
    rand_map = rand(4,3);
    i=i+1;
    M(i) = im2frame(X,rand_map);
end
[r,c] = size(X);
idx = randperm(r*c);
X2 = X;
for j=1:150:length(idx)
    X2(idx(j:j+149)) = Y(idx(j:j+149));
    i = i+1;
    M(i) = im2frame(X2,map);
end
for j=1:50
    rand_map = rand(4,3);
    i=i+1;
    M(i) = im2frame(Y,rand_map);
end
idx = randperm(r*c);
Y2 = Y;
for j=1:150:length(idx)
    Y2(idx(j:j+149)) = X(idx(j:j+149));
    i = i+1;
    M(i) = im2frame(Y2,map);
end
movie(M);