function crop_jpg2tif
%This script will allow the user to crop a set of jpg images and save them
%to tif files in preparation for the Eyeball script.
%This version is written to be platform independent (will work with both
%"/" and "\" paths.

%                                       Joshua Logan, jlogan@usgs.gov
%                                       6/25/08

%Determine platform, adjust path divider.
if ismac 
    slashdir = '/';
elseif ispc
    slashdir = '\';
end

%Get folder where photos to be cropped reside.
phtdir = uigetdir('','Select directory with input JPG images');

%Get folder where output cropped files will go.
outphtdir = uigetdir('','Save cropped output TIFF images here');

%Get list of all photos using rdir (make sure that files are greater than
%100kb to filter out thumbnail files)
dirlist=dir(fullfile(phtdir,'*.JPG'));
notthumbsidx=find([dirlist.bytes]>100000);
phtlist=dirlist(notthumbsidx);

for i = 1:length(phtlist)
    %find root name, without .jpg
    p2=findstr('.JPG',phtlist(i).name)-1;
    if isempty(p2)
        p2=findstr('.jpg',phtlist(i).name)-1;
    end
    rootname=phtlist(i).name(1:p2);
    rgb=imread([phtdir, slashdir, phtlist(i).name]);
    disp('------------------------------------------------------------------');
    disp(['Displaying image: ' phtlist(i).name]);
    disp([num2str(i) ' of ' num2str(length(phtlist))]);
    disp('Drag box to crop image');
    %turn off warnings
    warning off all
    rgb2=imcrop(rgb);
    %turn on warnings
    warning on all
    disp(['Saving as ' [outphtdir rootname '_crop.tif']  '....']);
    imwrite(rgb2, [outphtdir slashdir rootname '_crop.tif'], 'tif');
end
close

return


