clear all

framesPerSec = 5; 
num_frame=45;

aviFileName = 'sunrise.avi';

aviobj = avifile(aviFileName,'fps',framesPerSec,'COMPRESSION','NONE');


x=imread('sailboat.jpg');     % the moving sailboat
x=imresize(x,[66 66]);
x=double(x)/255/4;            % darken the image
y=imread('sailing.jpg');     % the background
BK=imresize(y,[240 480]);
BK=double(BK)/255/4;         % darken the image

moon_radius=10;
moon_y_inc=2/3*size(BK,1)-2*moon_radius;
x0=size(BK,2)-moon_radius;    % the starting point of the sun
y0=3/4*size(BK,1);

boat_x=1;                      % the position of the boat in the graph
boat_y=2/3*size(BK,1);


for count=1:num_frame

   tmp=BK;
   tmp(boat_y+1:boat_y+size(x,1)-2,boat_x+1:boat_x+size(x,2)-2,:)=x(2:size(x,1)-1,2:size(x,2)-1,:);
   IMG=tmp*(1+3/num_frame*count);                 % increase the brightness of the image step by step

   
   for i=-moon_radius:moon_radius     % draw the sun
       for j=-moon_radius:moon_radius
           if i^2+j^2 <=moon_radius^2
           IMG(y0+j,x0+i,1)=1;
           IMG(y0+j,x0+i,2)=0.8/(1+7/num_frame*count);
           IMG(y0+j,x0+i,3)=0;
           end
       end
   end

count 

   imshow(IMG);
   
   new_frame=getframe;

   aviobj = addframe(aviobj,new_frame);
   frame(count) = new_frame;
   
   
x0=size(IMG,2)-1.5*moon_y_inc-moon_radius+1.5*floor(moon_y_inc*cos(count*(pi/2)/num_frame));
y0=3/4*size(IMG,1)-floor(moon_y_inc*sin(count*(pi/2)/num_frame));

boat_x=boat_x+(size(BK,2)-size(x,2))*(2/5)*1/num_frame;


if mod(count,2)==0                      % decrease the size of the sailboat
    x=imresize(x,0.999,'bilinear');
end

end

image(IMG);

aviobj = close(aviobj);
movie(frame,1);