clear;pack

% parameters that can be changed:
n = 150; %how many frames?
hordistmax = 50; %what is the maximum horizontal distortion (in pixels)
imagename = 'hw1.jpg';


%read the original image
orig = imread(imagename);

%get the size of the original image
s = size(orig);

%convert the original image to double
orig = double(orig);

%prepare an array for horizontally distorted image
hordist = orig*0;


%init. movie array
m = [];

for i = 1:n
    c = min(1,max(0,i/n*1.4 - 0.2)); % Coefficient for mixing
    % image vs noise, and for the level of horizontal distortion.
    % It's value is constant (0) in the first part,
    % then it slowly increases to 1,
    % and it's constant (1) again in the last part.

    %create a horizontally distorted image from the original
    for row=1:s(1)
        inds = [0:(s(2)-1)] + (rand*2-1)*hordistmax*(1-c);
        inds = mod(round(inds),s(2))+1;
        hordist(row,1:s(2),:) = orig(row,inds,:);
    end

    % create random noise image
    noise1 = rand(s)*256;
    
    % mix noise and hor. dist. image
    im = noise1*(1-c) + hordist*c;
    
    % convert image to uint8 and store in the movie array
    m(i).cdata = uint8(im-0.4); 
    
    % no colormap is needed, because RGB images are used
    m(i).colormap = []; 
    
    % disp
    disp(['Processed ',int2str(i),' of ',int2str(n),' images.']);
end

disp('structure "m" contains the movie, to display it use: movie(m)');
