%makes a set of jpegs, each of which consists of:
%the original image
%the eye positions located by program
%the au-recognition as bargraphs of previous 100 frames

function M=make_demo(EyePosn,Yout,imgdir,r,firstframe,lastframe)

%Create an avi of the output.  There are three main commands:
%avifile (executed here) and addframe and close (see end of this file). 
movieName = [imgdir,'out.avi'];
%movieName = ['temp_out.avi'];
mov = avifile(movieName)
fprintf('Creating %s\n', movieName);
fprintf('Make sure nothing is occluding the figure window.\n');

%YY is scaled Yout for viewing
%We should really get logoPath from an argument
logoPath = 'Source/Logos/';
logo1 = imread( [logoPath, 'logo1.jpg'] );
logo2 = imread( [logoPath, 'logo2.jpg'] );
  
  t0=-1.5;t1=max(Yout(:))-t0;
  T=(size(r,1)-2);EyePosn=round(EyePosn);

%% Added to deal with large image directories - just do a range. 
if(nargin > 4) 
     frst=firstframe;
else frst = 1;
end
if(nargin > 5)
     lst = lastframe;
else lst = T;
end

  %step through the frames
 for i=frst:lst
  %read in image
   origimg = imread([imgdir,'/',r(i+2).name]);
  %white dots at eye positions
  if (EyePosn(i,:) > 0)
     origimg(EyePosn(i,2)+[-2:2],EyePosn(i,1)+[-2:2],1:3)=255;
     origimg(EyePosn(i,4)+[-2:2],EyePosn(i,3)+[-2:2],1:3)=255;
     %Make red face box
     cx = mean([EyePosn(i,1),EyePosn(i,3)]);
     cy = mean([EyePosn(i,2),EyePosn(i,4)]);
     d = sqrt((EyePosn(i,1)-EyePosn(i,3))^2 + (EyePosn(i,2)-EyePosn(i,4))^2);
     sz=2.2*d;
     topx =  round([cx-sz/2;cx+sz/2]); topy= round([cy-sz*.3;cy-sz*.3]);
     botx =  round([cx-sz/2;cx+sz/2]); boty= round([cy+sz*.7;cy+sz*.7]);
     leftx=  round([cx-sz/2;cx-sz/2]); lefty= round([cy-sz*.3;cy+sz*.7]);
     rightx= round([cx+sz/2;cx+sz/2]); righty= round([cy-sz*.3;cy+sz*.7]);
     faceboxX = [topx,botx,leftx,rightx];faceboxY = [topy,boty,lefty,righty];
     %line(faceboxX,faceboxY);
  end

  %making the bar graph
  %bar8=zeros(480,100,3); 
  %y=Yout((max(1,i-99):i),:)/t1-t0/t1;
  %y(find(y<0))=0;y=round(60*y);%outputs < t0=-1.5 don't show
  %for j=1:size(y,1)
  %  for au=1:size(y,2)
  %    bar8((au*60-y(j,au)+1):(au*60),j,mod(au-1,3)+1)=255;
  %    %(red (AU1,5) green(AU2) blue(AU4))
  %  end
  %  for au=5:size(y,2)
  %    bar8((au*60-y(j,au)+1):(au*60),j,mod(au,3)+1)=255;
  %    %(cyan AU(10,20) pink (AU12) yellow AU(14))
  %  end
  %end
  % % origimg(:,541:640,:)=bar8;%paste bars over right edge of image

  YY=Yout+1;YY(:,7)=YY(:,7)+0.25; %Show output above -1 (above -1.25 for AU 14)

  %display image and add to movie
     subplot('position',[0,0.3,0.5,0.5])
     imshow(origimg(:,50:end-50,:));
     if (EyePosn(i,:) > 0)
        for f=1:4
           line(faceboxX(:,f)-50,faceboxY(:,f));
        end
     end
     subplot('position',[0.15,0.8,0.2,0.2]); imshow(logo1);
     subplot('position',[0.1,0.05,0.3,0.2]); imshow(logo2);
     %Designate sliding window of L frames for output
     L=100; t1 = max((i-L),1); 

     %Make the bar graphs
     subplot('position',[0.5,0.77,0.23,0.23])
     bar(YY(t1:i,1),'r');shading flat;axis([1,L,0,2.5]);axis off;
text(L/2.5,-.25,'AU 1');
     subplot('position',[0.77,0.77,0.23,0.23])
     bar(YY(t1:i,2),'g');shading flat;axis([1,L,0,2.5]);axis off;
text(L/2.5,-.25,'AU 2');
     subplot('position',[0.5,0.53,0.23,0.23])
     bar(YY(t1:i,3),'b');shading flat;axis([1,L,0,2.5]);axis off;
text(L/2.5,-.25,'AU 4');
     subplot('position',[0.77,0.53,0.23,0.23])
     bar(YY(t1:i,4),'r');shading flat;axis([1,L,0,2.5]);axis off;
text(L/2.5,-.25,'AU 5');
     subplot('position',[0.5,0.29,0.23,0.23])
     bar(YY(t1:i,5),'g');shading flat;axis([1,L,0,2.5]);axis off;
text(L/2.5,-.25,'AU 10');
     subplot('position',[0.77,0.29,0.23,0.23])
     bar(YY(t1:i,6),'b');shading flat;axis([1,L,0,2.5]);axis off;
text(L/2.5,-.25,'AU 12');
     subplot('position',[0.5,0.05,0.23,0.23]);
     bar(YY(t1:i,7),'r');shading flat;axis([1,L,0,2.5]);axis off;
text(L/2.5,-.25,'AU 14');
     subplot('position',[0.77,0.05,0.23,0.23]);
     bar(YY(t1:i,8),'g');shading flat;axis([1,L,0,2.5]);axis off;
text(L/2.5,-.25,'AU 20');
h=figure(1);
drawnow
  F = getframe(1);
  mov = addframe(mov,F);
  M(i) = F; %This is just to see the movie in matlab
end
mov = close(mov);

%clf reset
%tmp = frame2im(M(1));imshow(tmp);% Expands the window
%movie(M,2) %Play movie in matlab
