%  demomov.m
%  Requires: matlab + 
%            pbmplus (standard on linux machines)
%
%
%  This is a matlab program which will make a complete set of animated
%  pictures, run in a javascript player.
%  it produces an html - file from the text parts listed below
%
%  This file can be edited to produce your own movies
%  The places where you should edit are indicated in the code
%
%  Just to run the demo to produce a test movie, run demomov in matlab
%  It will produce a set of pictures and an html file
%  all with a name stored in variable namestr
%
%  You must have the following files
%                                   demomov.m
%                                   PART1.txt
%                                   PART2.txt
%                                   PART3.txt
%
%   Author of this setup:   Ladislav Kocbach
%   Author of jsplayer:     Martin Holecko
%
%   Please, keep the acknowledgements of the authors 
%   in the generated html files
%
format compact
%%
%%
%%   Example:  10 frames generated;  should be changed
%%
%%
      nfram=10;
      % typestr='gif';    %%  movie is GIF-files
      typestr='jpg';    %%  movie is JPG-files
%%
%%    example:  namestr is ROTDEMO
%%
      namestr='ROTDEMO'      
      nam=namestr;
%%  
%%  LOOP OVER FRAMES, INCLUDING FRAME 1
%%  
for kfram=1:nfram
 if kfram==1
   %%
   %%   Preparing the figure window. This should not be changed
   %%
   figure
    set(gcf,'pos',[ 10 10 320 200] )   %  WINDOW SIZE  is 320 x 200
    set(gcf,'menu','none','numbertitle','off','name','NEWMOVIE') 
    set(gcf,'menu','none');
    set(gcf,'color','white')
    %%
    %%  This makes gray colormap can be changed
    %%
 end
 %%
 %%
 %%    HERE COMES YOUR CODE FOR PRODUCING EACH OF THE FRAMES
 %%
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

                                                %%   REPLACE
    if kfram==1 				%%   REPLACE
       fff=peaks(20);				%%   REPLACE
       surf(fff); grid off;			%%   REPLACE
       shading interp; colormap hot;		%%   REPLACE
    end 					%%   REPLACE
    alf=30-10*kfram;				%%   REPLACE
						%%   REPLACE
      % Change the viewing angle		%%   REPLACE
      view( alf ,30);				%%   REPLACE
						%%   REPLACE
      % Flush pending graphics events		%%   REPLACE
      drawnow;					%%   REPLACE
						%%   REPLACE
  
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
 %%
 %%    END OF THE DRAWING CODE IS HERE
 %%
 %% 
 %%    THE FOLLOWING PART PICKS THE FRAMES AND MAKES GIF IMAGES
 %%
 %%    IN PRINCIPLE, MATLAB CAN DO THIS, BUT THAT NEEDS 
 %%    A WELL TUNED MATLAB (The author never met one)
 %%
 
 % xwd  is  an  X  Window System window dumping utility.
 if typestr=='gif'
    eval( [ '!xwd -name "NEWMOVIE " | xwdtopnm |  ppmtogif > ' ...
    nam num2str(100+kfram) '.gif' ] ) 
 end
 if typestr=='jpg'
    eval( [ '!xwd -name "NEWMOVIE " | xwdtopnm |  cjpeg > ' ...
    nam num2str(100+kfram) '.jpg' ] ) 
 end 
 %%
 %%
 %%
end % over all the FRAMES
%%
%%
%%
%%   The following lines handle the production of the HTML
%%   YOU SHOULD NOT CHANGE MUCH HERE 
%%   UNLES YOU KNOW WHAT YOU ARE DOING
%%   
%%   
% get dimensions of the window
 windim=get(gcf,'pos');

% print size of the window
 windim(3),windim(4)

% delete all previously created temporary files
 eval( [ '! rm -f tmp.txt tmp2.txt ' nam '.html '  ]);

% Put PART1.txt into the output html file
 eval([ '! cat PART1.txt > ' nam '.html ' ]);

% create a new temporary file 
 fid=fopen('tmp.txt','w');

% description of the animation

  line1=[ 'image_name = "'  nam '";' ]; 
  if typestr=='gif'
       line2=[ 'image_type = "gif";']; 
  end
  if typestr=='jpg'
       line2=[ 'image_type = "jpg";']; 
  end    
  line3=[ 'first_image = 101;' ]; 
  line4=[ 'last_image  = ' num2str(100+ nfram) '; ' ];
  line5=[ 'animation_height = ' num2str(windim(4)) '; '];             
  line6=[ 'animation_width = '  num2str(windim(3)) '; '];          


% put the description in the file

  fprintf(fid,'%s%c%c', line1,setstr(10),setstr(13));
  fprintf(fid,'%s%c%c', line2,setstr(10),setstr(13));
  fprintf(fid,'%s%c%c', line3,setstr(10),setstr(13));
  fprintf(fid,'%s%c%c', line4,setstr(10),setstr(13));
  fprintf(fid,'%s%c%c', line5,setstr(10),setstr(13));
  fprintf(fid,'%s%c%c', line6,setstr(10),setstr(13));

% close  the file
  fclose(fid);

% append contents of the temporary file to the output html file
  eval([ '! cat tmp.txt >> ' nam '.html ' ]);
  
% append PART2.TXT to the html file
  eval([ '! cat PART2.txt >> ' nam '.html ' ]);
 
% html code for the picture
  lineL=[ '   document.write(" <P><IMG NAME=\"animation\" SRC=\"' ...
            nam '101.gif\"HEIGHT=",animation_height, " WIDTH=", ' ...
	    'animation_width, "\" >");' ];
 
% put html into a file
  fid=fopen('tmp2.txt','w');
  fprintf(fid,'%s%c%c', lineL,setstr(10),setstr(13)); 
  fclose(fid);

% append tmp2.txt to the output html file
  eval([ '! cat tmp2.txt >> ' nam '.html ' ]);

% append PART3.txt to the output file 
  eval([ '! cat PART3.txt >> ' nam '.html ' ]);  

  
