function imslide ( imfile_list )
% IMSLIDE:  corrects offset of tiff images
%
% USAGE:  imslide ( imfile_list )
%

%
% open the list of files
afid = fopen ( imfile_list, 'r' );
if ( afid == -1 )
	fprintf ( 'Could not open %s.\n', imfile_list );
	return;
end

%
% read in the first image, select out the area around the zero
tiff_file = fgetl ( afid );
tiff1 = imread ( tiff_file, 'tiff' );

startx1 = 65; starty1 = 358;
width1 = 28; height1 = 32;
zero_image1 = imcrop(tiff1,[startx1 starty1 width1 height1] ); 
zero_image1 = rgb2gray ( zero_image1 );
figure; image(zero_image1);
first_figure = gcf;
colormap(gray);

set ( gcf, 'Name', 'First image' );


%
% load the next image
tiff_file = fgetl ( afid );

while ( tiff_file ~= -1 )

	tiff2 = imread ( tiff_file, 'tiff' );
	tiff2bw = rgb2gray ( tiff2 );
	startx2 = 50; starty2 = 350;
	width2 = 50; height2 = 50;
	zero_image2 = imcrop(tiff2bw,[startx2 starty2 width2 height2] );
%	figure;
%	image(zero_image2);
	second_figure = gcf;
	
	%
	% loop thru subimages of size width1 x height1, calculating the
	% correlation coefficient
	coeffs = zeros([(height2 - height1+1) (width2 - width1+1)]);
	[size_y,size_x] = size(coeffs);
	for x = 1:size_x
		for y = 1:size_y
			subimage = imcrop ( zero_image2, [x y width1 height1] );
			coeffs(y,x) = corr2 ( zero_image1, subimage );
		end
	end
	
	ind = find ( coeffs == max(coeffs(:)) );
	
	
	zy = mod((ind-1),size_y)+ 1;
	zx = floor((ind-1)/size_y) + 1;
	
	
	x_displacement = startx1 - (startx2 + zx)
	y_displacement = starty1 - (starty2 + zy)
	
	
	 
	% Now construct the output tiff
	[r,c,d] = size(tiff1);
	out_tiff = uint8(zeros(size(tiff1)));

	disp(tiff_file);

		tiff_startx = (20-x_displacement);
		tiff_starty = (20-y_displacement);
		tiff_width = min ( [c; (c-40+x_displacement)]);
		tiff_height = min ( [r; (r-40+y_displacement)]);
		a = imcrop ( 	tiff2, ...
						[	...
							tiff_startx tiff_starty tiff_width tiff_height
						] ...
					);


		out_tiff ( 20:(20+tiff_height), 20:(20+tiff_width), 1:3 ) = a;

%	if ( (x_displacement >= 0) & (y_displacement >= 0) )
%	
%	
%	elseif ( (x_displacement < 0) & (y_displacement < 0) )
%
%					
%	
%	elseif ( (x_displacement < 0) & (y_displacement >= 0) )
%		disp ( sprintf ( 'x_displacement = %4.0f, y_displacement = %4.0f\n', x_displacement, y_displacement ) );
%		disp( 'not done yet' );
%		return;
%	elseif ( (x_displacement >= 0) & (y_displacement < 0) )
%		disp ( sprintf ( 'x_displacement = %4.0f, y_displacement = %4.0f\n', x_displacement, y_displacement ) );
%		disp( 'not done yet' );
%		return;
%	
%	end
					
	
	
	
	[l,n] = strtok(tiff_file, '-');
	out_tiff_name = sprintf ( 'bb%s', n );
	imwrite ( out_tiff, out_tiff_name, 'tiff' );

	tiff_file = fgetl ( afid );

end

fclose ( afid );
