function [idnum, x, y] = GetImageScales(index_file, casefilename)

% [idnum, x, y] = GetImageScales(index_file, casefilename)
%
% Processes images in index_file, displaying each one to user and prompting for the user
% to click on the center of the rock of interest.  Coordinates of this point for each image
% are stored in casefile.  
% Also returns matrices of pixel observations for points on the rocks and points off them.

% Liam Pedersen, NASA ARC, 1999

%initialize variables etc.

% casefile header labels
label1 = 'IDnum  sample_id  scale_x scale_y';
CaseFileLabels = [label1];

%*****************************************************************
%*****************************************************************

% open CASE file for output.
casefile = OpenCaseFile(casefilename, CaseFileLabels);

% Open INDEX file and process each line
Err = 0; i = 1;
fid = OpenDataFile(index_file, 3);
L = readline(fid);

while (L > 0)
   S = ParseDataFileLineII(L);
   
   % write first fields of S
   fprintf(casefile,'%d\t', S.idnum);
   fprintf(casefile,'%s\t', S.sample_id);
   
   %****************************
   % Process IMAGE (if possible)
   
   if (S.imagefile(1) ~= '*')
      [dx,dy] = GetImageScale(S.imagefile);
      R = input('object size (in) = ');
      if (isempty(R)) R = 1; end
      r = 25*R;  % convert to mm
      d = sqrt(dx*dx + dy*dy);
      scale = [r/d r/d];
            
      fprintf(casefile,'%f\t',scale);
   else
      for i = 1:2
         fprintf(casefile,'*  ');
      end
   end
         
   drawnow
   fprintf(casefile,'\n');
   L = readline(fid);
end

% close I/O files
fclose(fid);
fclose(casefile);

%****************************************************************************
%****************************************************************************

function zoomcenter(im)
% zoom to point in image. \
% R click to mag x 2
% L click to mag x 0.5
% ENTER to stop.

% display image
clf;
image(im);
hold on
drawnow;

[YY, XX, CC] = size(im)
mag = 1;
dx = XX/2;
dy = YY/2;

[x y button] = ginput(1);
while (button ~= double(' '))
   plot(x,y,'r*');
   if (button == 1)
      mag = mag*2;
      minx = x - dx/mag;
      maxx = x + dx/mag;
      miny = y - dy/mag;
      maxy = y + dy/mag;
      axis([minx maxx miny maxy]);
   end
   if (button == 3)
      mag = mag/2;
      minx = x - dx/mag;
      maxx = x + dx/mag;
      miny = y - dy/mag;
      maxy = y + dy/mag;
      axis([minx maxx miny maxy]);
   end
   drawnow;
   [x y button] = ginput(1);
end



%****************************************************************************
%****************************************************************************
% Extract IMAGE PIXEL feature vectors examples

function [dx, dy] = GetImageScale(imagefile)

% [dx, dy] = GetImageScale(imagefile)

% read image
im = ReadImage(imagefile);
zoomcenter(im);
disp('click on endpoints of scale\n');
R = 'blah';
while (~isempty(R))
   [x y] = ginput(2);
   hold on;
   plot(x,y,'g');
   R = input('hit ENTER to continue, any other key to redo.');
end
dx = diff(x);
dy = diff(y);