function [varargout] = datathief(imfile,filetype)
% DATATHIEF: data from image file. 
% function [x,y,x2,y2,..] = datathief(['imfile'],['filetype']): take data from image file.  
% Version 1: assume plot is already exactly oriented wrt the image axes. 
% If 'imfile' is not provided (string arg.) then it is assumed to already be 
% displayed. If imfile is provided, 'filetype' can also be provided but is 
% not required. See imread. 

points = 1; 
if exist('imfile','var'), 
 if exist('filetype','var'), 
  B = imread(imfile,filetype); 
 else, 
  B = imread(imfile); 
 end 
 fig = figure; colormap gray; imagesc(B); 
 if input('zoom yes/no (0/1)?'), zoom, end; 
end; 

points = input('points yes/no (0/1)?' ); 
hold on; 

for j = 2:2:nargout, % until [x,y] pairs are used up. 
if j==2, click=1; else click = input('redo axes 1/0? '); end; 
if click, 
 disp('Click on all corners LowLeft, LowRigh, UpLeft, UpRight, then enter the X and Y limits.  '); 
 LowLeft  = ginput(1); plot(LowLeft(1),LowLeft(2),'x')
 LowRight = ginput(1); plot(LowRight(1),LowRight(2),'x')
 UpLeft   = ginput(1); plot(UpLeft(1),UpLeft(2),'x')
 UpRight  = ginput(1); plot(UpRight(1),UpRight(2),'x')

 Left  = mean([LowLeft(1) UpLeft(1)]); 
 Right = mean([LowRight(1) LowRight(1)]); 
 Low   = mean([LowLeft(2) LowRight(2)]); 
 Up    = mean([UpLeft(2)  UpRight(2)]); 

 LeftLim  = input('x-min (left of graph?) '); 
 RightLim = input('x-max (right of graph?) '); 
 LowLim   = input('y-min (bottom of graph?) '); 
 UpLim    = input('y-max (top of graph?) '); 
end; 

disp('click on points to be stolen and hit return when done. ') 
[X,Y] = ginput; 
if points, hold on; plot(X,Y,'o-r'); end;
x = (X-Left)*(RightLim-LeftLim)/(Right-Left) + LeftLim; 
y = (Y-Low) *(UpLim-LowLim)    /(Up-Low)     + LowLim; 

varargout(j-1:j) = {x,y}; 
end; 

if  exist('fig','var'), if input('Delete figure (1/0)? '), delete(fig); 
 end; end; 
 