function []=tifcounter()
% Adds a page counter to multi-page tif images.
%
% When I scan long documents that don't have "Page x of y" printed on them
% already, I use this script to add it after the fact. Just make sure that
% all the pages made it through the scanner first!
% Set color of text, black or white
black = true;
% Pick the file, pick the target dir, make output file name
[file2get path2get] = uigetfile('*.tif',' Select .tif Image');
path2put = uigetdir(path2get,' Select Destination Directory');
[pathstr,file2put] = fileparts([path2get file2get]);
file2put = ['\',file2put,'_c.tif'];
% Get image info and get vars
a = imfinfo([path2get file2get]);
pages = size(a,2);
w = a(1).Width;
h = a(1).Height;
Xres = a(1).XResolution;
Yres = a(1).YResolution;
b = ones(h,w);
boxwidth = 180;
scrsz = get(0,'ScreenSize');
% process loop, one page at a time (read, label, write)
for i = 1:pages
workin = imread([path2get file2get],i);
t = ['Page ',num2str(i),' / ',num2str(pages)];
h1 = figure('Position',[(scrsz(3)/2) (scrsz(4)/2) boxwidth 40]);
subplot('Position',[0 0 1 1]);
axis off;
text(.5,.5,t,'units','normalized','FontSize',14,'Color','k'...
,'HorizontalAlignment','center','VerticalAlignment','middle'...
,'FontWeight','bold','Fontname','FixedWidth');
rectangle('LineWidth',4,'EdgeColor','k');
f = getframe; im = frame2im(f); bw = im2bw(im); mask = logical(bw);
mask2 = imresize(mask, (Xres/boxwidth),'bicubic');
my = size(mask2,2);
% mx = size(mask2,1);
% mask3 = putBinA(b,mask2,(h-mx-20),(w-my-120)); % LR corner position
mask3 = putBinA(b,mask2,(20),(w-my-120)); % UR corner position
if black
workout = workin.*mask3;
else
workout = workin + ~mask3;
end
if a(1).BitDepth == 1
workout = im2bw(workout);
end
imwrite(workout,[path2put file2put],'tif','Writemode','append',...
'Resolution',[Xres Yres]);
close(h1);
end