%This script will manipulate the image we were given. It generates some
%sound, too.

function [anim] = assignment1
srcimage = imread('hw1.jpg');

%Show the image, then delay for a few seconds before the effects.
imshow(srcimage)
anim = getframe();
pause(3)

%Fade each channel one at a time, then fade all three.
%We'll use 10 frames and the left (decreasing) half of a concave up parabola
%to create a curve for the color channel.
dupimage = srcimage;
for channel = 1:4
    for frame = 0:10
        if (channel < 4)
            dupimage(:,:,1) = srcimage(:,:,channel) .* ((frame-5) .^2 ./ 25);
        else
            dupimage(:,:,:) = srcimage(:,:,:) .* ((frame-10) .^2 ./ 100);
        end
    
        %Add some random noise in while we're at it.
        imshow(imnoise(dupimage, 'salt & pepper', abs(frame-5)/500))
        anim = [anim getframe()];
        pause(0.001 / (channel*2))  %Faster each time.
    end
    
    pause(1)
end

anim = [anim genwhitenoise(size(srcimage), 3, 10)];
pause(.2)
imshow(srcimage)
anim = [anim getframe()];
pause(.2)

anim = [anim genwhitenoise(size(srcimage), 2, 10)];
pause(.2)
imshow(srcimage)
anim = [anim getframe()];
pause(.2)

anim = [anim genwhitenoise(size(srcimage), 5, 10)];
pause(.2)
imshow(srcimage)
anim = [anim getframe()];
pause(.2)

imshow(srcimage)
anim = [anim getframe()];
pause(1)

dupimage = srcimage;

anim = [anim genwhitenoise(size(srcimage), 20, 10)];

for power=10:-2:-30
    anim = [anim genwhitenoise(size(srcimage), 3, power)];
end
end

%Accepts a 2-element vector containing height, width dimensions of the
%noise, the number of frames to generate, and the noise intensity (dBW).
%Returns a sequence of frames for use in an animation.
function [anim] = genwhitenoise(dimensions, frames, intensity)
    %Generate 10 frames of white noise.
    anim = [];
    sound(wgn(frames * 1800, 1, intensity-25), 15000);       %Speakers on for full effect!
    for frame = 1:frames
        imshow(wgn(dimensions(1), dimensions(2), intensity))
        anim = [anim getframe()];
        pause(0.01)
    end    
end

%immovie(whitepicture);