function [] = roam(ifig)
%ROAM Dynamically display zoomed subimage in a second window.
%	ROAM creates a subimage window and displays a zoomed
%	subimage centered about the cursor location in the
%	image displayed in the current figure.  Moving the mouse
%	in the image figure continuously updates the data displayed
%	in the zoomed subimage window.
%
%       The row/column coordinates and pixel value at the cursor
%       location are displayed in the upper left corner of the 
%       zoomed subimage window.
%
%	Press 'Enter' in the command window to exit ROAM.
%
%	ROAM(H) uses the image specified by the figure handle H
%       
%       See also PROFILES.
%
%	             ... and remember, ROAM wasn't built in a day.
%

% EE/PH437  Intro to Image Processing (S97)
% Ed Doering

% Updated 3/8/2000 for compatibility with version 5.3


global PFIG NUMROWS NUMPIX ROW PIX XROW XPIX RLINE CLINE;
global IMDATA IAXES AXES PV SLINE STEXT;
global WSIZE;

% Get handle to image figure (default to current figure if no args)
if nargin<1
	ifig=gcf;
else
	ifig=ifig;
end


%----- INITIALIZATION -----------------------------------------------------

% Window size is 2*WSIZE + 1
WSIZE=8;

% Set pointer type in image figure to crosshair (save existing type
% to be restored at end of function)
figure(ifig)
curse = get(ifig,'pointer');
set(ifig,'pointer','crosshair');

% Get handle to image axes
IAXES=gca;

% Get image data from axes
[IMDATA,imageok]=getimage(IAXES);
if ~imageok
	error('The current figure must contain an image to use ROAM.');
end

% Get colormap from image
%cmap=colormap;
cmap=get(ifig,'Colormap');

% Save state of image figure call back functions
btndown = get(ifig,'WindowButtonDownFcn');
btnup = get(ifig,'WindowButtonUpFcn');
btnmotion = get(ifig,'WindowButtonMotionFcn');

% Window button motion callback routine
wbm_cb=['global IAXES NUMROWS NUMPIX ROW PIX;',...
	'global IMDATA AXES PV WSIZE SLINE STEXT,',...
	'PV=get(IAXES,''CurrentPoint'');',...
	'ROW=min(round(PV(4)),NUMROWS);',...
	'ROW=max(ROW,1);',...
	'PIX=min(round(PV(1)),NUMPIX);',...
	'PIX=max(PIX,1);',...
	'set(AXES,''XLim'',[PIX-WSIZE PIX+WSIZE]);',...
	'set(AXES,''YLim'',[ROW-WSIZE ROW+WSIZE]);',...
	'set(SLINE,''Xdata'',[PIX-0.5 PIX+0.5 PIX+0.5 PIX-0.5 PIX-0.5]);',...
	'set(SLINE,''Ydata'',[ROW+0.5 ROW+0.5 ROW-0.5 ROW-0.5 ROW+0.5]);',...
	'set(STEXT,''Position'',[PIX-WSIZE ROW-WSIZE-1]);',...
	'set(STEXT,''String'',[''('' num2str(double(ROW)) '','' num2str(double(PIX)) '') = '' num2str(double(IMDATA(ROW,PIX)))]);'...
	];

set(ifig,'WindowButtonMotionFcn',wbm_cb)


% Create the plotting figure
PFIG=figure;

% Windows version only:
set(PFIG,'Position',[640-200,480-200,200,200])

% Create the subimage display window
[NUMROWS,NUMPIX]=size(IMDATA);
%IMG=image(IMDATA);
IMG=imshow(IMDATA,'notruesize');
AXES=gca;
set(AXES, 'XLim',[round(NUMPIX/2)-WSIZE-0.5 round(NUMPIX/2)+WSIZE+0.5],...
	  'YLim',[round(NUMROWS/2)-WSIZE-0.5 round(NUMROWS/2)+WSIZE+0.5],...
	  'DrawMode','fast');

% Use the same colormap as the image
colormap(cmap)
%set(PFIG,'Colormap',cmap)

% Create indicator box around center pixel of subimage
p=round(NUMPIX/2);
r=round(NUMROWS/2);
SLINE=line([p-0.5 p+0.5 p+0.5 p-0.5 p-0.5],[r+0.5 r+0.5 r-0.5 r-0.5 r+0.5]);
%set(SLINE,'Color',[0 1 0]);


axis('square'); axis('off')

% Create text object for coordinate/gray value display
STEXT=text(p-WSIZE,r-WSIZE-2,' ');
set(STEXT,'EraseMode','normal')


%waitforbuttonpress;
pause
fprintf('\n');
get(ifig,'SelectionType');


%----- CLEAN UP -----------------------------------------------------------

% Restore pointer type in image figure
figure(ifig)
set(ifig,'pointer',curse);

% Restore state of image figure call back functions
set(ifig,'WindowButtonDownFcn',btndown,'WindowButtonUpFcn',btnup, ...
	'WindowButtonMotionFcn',btnmotion)

% Remove the plotting figure
delete(PFIG)

% Remove variables
clear global CAXES 
clear global CLINE 
clear global IAXES 
clear global IMDATA 
clear global NUMPIX 
clear global NUMROWS 
clear global PFIG 
clear global PIX 
clear global PLOTROW 
clear global RAXES 
clear global RLINE
clear global ROW 
clear global PV
