function [aimg mx mn Nf info amov]=im2mov(wc,sf,ef,box,rot,fn,color)
% Convert wildcard list of image file into a movie
% Usage: [aimg mximg mnimg Nf info]=im2mov(wc,[sf],[ef],[box],[cx cy th],[fn])
%
% aimg is the average of all frames.  
% mximg is the maximum of all frames.  
% mnimg is the minimum of all frames.  
% Nf is the number of frames.
% info is a structure with all information from the first file.
% Use [] for default values.
%
% wc is a wildcard list of files to convert (e.g., '*.jpg')
% sf is the start frame. Default sf=1
% ef is the start frame. Default ef=Nf
% box is an roi. Default box=[1 Nx 1 Ny]
% cx,cy are the center for a rotation of th degrees.
% if fn exist it is a filename to used to create an avi file.
% color 1=color 0=grayscale;

% revision history:
% 12/17/09 Mark D. Shattuck <mds> im2mov.m  
% 02/02/10 mds add visual counter.

lastwarn('');
dr=dir(wc);
[p b e v]=fileparts(wc);
nm=cell2mat({dr(:).name}');
[junk ii]=sort(datenum({dr(:).date})*3600*24+str2num(nm(:,5:8))/1e4); %#ok<ASGLU,ST2NM>
Nf=length(dr);
info=imfinfo([p '/' dr(1).name]);
info.good=-ones(1,Nf);
info.good(sf:ef)=1;
Nx=info.Width;
Ny=info.Height;
if(~exist('sf','var') || isempty(sf))
	sf=1;
end
if(~exist('ef','var')|| isempty(ef))
	ef=Nf;
end
if(~exist('box','var')|| isempty(box))
	box=[1 Nx 1 Ny];
end
if(~exist('rot','var')|| isempty(rot))
	rot=false;
end
if(~exist('color','var')|| isempty(color))
	color=1;
end
if(~exist('fn','var'))
	saveavi=false;
else
	saveavi=true;
	amov=avifile(fn,'compression','none');
	amov.fps=15;
	if(color==0)
		amov.compression='RLE';
		amov.colormap=colormap(gray(256));
		%amov.keyframe=15;
		amov.quality=90;
	end
end


if(rot)
	[yy xx]=ndgrid(1:Ny,1:Nx);
	xx=xx-rot(1);
	yy=yy-rot(2);
	r=(xx+1i*yy)*exp(1i*pi/180*rot(3));
end
colormap(gray(256));
aimg=0;
mx=0;
mn=1e99;
fprintf(1,'Processing...');
Nc=length(sprintf('%d',ef));
fmt=sprintf('[%%0%dd/%%0%dd]',Nc,Nc);
fprintf(1,fmt,0,ef);
wid='';
for nf=sf:ef;
	backspace(1,fmt,nf,ef);
	fprintf(1,fmt,nf,ef);

  im=imread([p '/' dr(ii(nf)).name]);
  wid=lastwarn('');
  while(~isempty(wid)) % skip bad frames
		info.good(nf)=false;
    nf=nf+1;   %#ok<FXSET>
    im=imread([p '/' dr(ii(nf)).name]);
    wid=lastwarn('');
		fprintf(1,['\n' fmt],nf,ef);
  end
	ddim=double(im);
	if(rot)
		if(color)
			im(:,:,1)=uint8(interp2(xx,yy,ddim(:,:,1),real(r),imag(r)));
			im(:,:,2)=uint8(interp2(xx,yy,ddim(:,:,2),real(r),imag(r)));
			im(:,:,3)=uint8(interp2(xx,yy,ddim(:,:,3),real(r),imag(r)));
		else
			ddim=interp2(xx,yy,sum(ddim,3),real(r),imag(r));
		end
	else
		ddim=sum(ddim(:,:,[1 2 3]),3);
	end
	if(color)
		tmp=ddim(box(3):box(4),box(1):box(2));
		aimg=aimg+tmp;
		mx=max(mx,tmp);
		mn=min(mn,tmp);
		image(im(box(3):box(4),box(1):box(2),:));
	else
		tmp=ddim(box(3):box(4),box(1):box(2));
		aimg=aimg+tmp;
		mx=max(mx,tmp);
		mn=min(mn,tmp);
		ibw=uint8(ddim(box(3):box(4),box(1):box(2))/max(flat(ddim(box(3):box(4),box(1):box(2))))*256);
		h=image(ibw);
		set(h,'cdatamapping','direct');
	end
	axis('image');
	drawnow;
	if(saveavi)
		if(color)
			amov=addframe(amov,im(box(3):box(4),box(1):box(2),:));
		else
			amov=addframe(amov,ibw);
		end
	else
		drawnow;
	end
end
fprintf(1,'...Done\n');
if(saveavi)
	amov=close(amov); 
end
aimg=aimg/(ef-sf+1);