function [] = profiles(ifig)
%PROFILES Dynamically display image profiles in a second window.
%         PROFILES creates a 2-D plot window and displays a row (or
%         column) profile from the image data in the current figure.
%         Moving the mouse in the image figure continuously updates
%         the data displayed in the 2-D plot.
% 
%         The row/column coordinates and pixel value at the cursor
%         location are displayed in the upper left corner of the 
%         2-D plot window.
%
%         Click on the right mouse button to toggle between row display
%         and column display.  Press 'Enter' in the command window
%         to exit PROFILES.
%
%         PROFILES(H) uses the image specified by the figure handle H.
%       
%         See also ROAM.

%	Ed Doering
%	Rose-Hulman Institute of Technology
%	05 March 1997

%  Updated by Rongguang Liang for MATLAB version 5.0
%  11 June 1997

% Updated 3/8/2000 for compatability with version 5.3
global PFIG NUMROWS NUMPIX PLOTROW ROW PIX XROW XPIX RLINE CLINE;
global IMDATA IAXES RAXES CAXES PV STEXT GMAX;
% Get handle to image figure (default to current figure if no args)
if nargin<1
	ifig=gcf;
else
	ifig=ifig;
end
PLOTROW=1;
%----- INITIALIZATION -----------------------------------------------------
% 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 PROFILES.');
end
% 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 RLINE CLINE PV 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(RLINE,''YData'',IMDATA(ROW,:));'...
   'set(CLINE,''YData'',IMDATA(:,PIX));'...
   'set(STEXT,''String'',[''('' num2str(double(ROW)) '','' num2str(double(PIX)) '') = '' num2str(double(IMDATA(ROW,PIX)))]);'...
   ];
%this line is just before the RLINE line above
%the line return feature doesn't appear to work in Windows
%'fprintf(''row=%4d pix=%4d\r'',ROW,PIX);',...
wbd_cb=['global PFIG IMDATA PLOTROW;',...
	'global RAXES CAXES,',...
	'global RLINE CLINE,',...
	'if PLOTROW,',...
		'PLOTROW=0;',...
		'set(PFIG,''Name'',''Column Profile''),',...
		'set(RAXES,''Visible'',''off''),',...
		'set(RLINE,''Visible'',''off''),',...
		'set(CAXES,''Visible'',''on''),',...
		'set(CLINE,''Visible'',''on''),',...
	'else ',...
		'PLOTROW=1;',...
		'set(PFIG,''Name'',''Row Profile''),',...
		'set(RAXES,''Visible'',''on''),',...
		'set(RLINE,''Visible'',''on''),',...
		'set(CAXES,''Visible'',''off''),',...
		'set(CLINE,''Visible'',''off''),',...
	'end,',...
	];
set(ifig,'WindowButtonMotionFcn',wbm_cb,...
	 'WindowButtonDownFcn',wbd_cb)
% Create the plotting figure
PFIG=figure;
% Windows version only:
set(PFIG,'Position',[20,50,620,150])
% Create the axes
[NUMROWS,NUMPIX]=size(IMDATA);
gmax=max(max(IMDATA));
gmin=min(min(IMDATA));
RAXES=axes(	'XLim',[0 NUMPIX],...
		'YLim',[gmin gmax],...
		'XLimMode','manual',...
		'YLimMode','manual',...
		'DrawMode','fast');
xlabel('Pixel Number')
ylabel('Pixel Intensity')
CAXES=axes(	'XLim',[0 NUMROWS],...
		'YLim',[gmin gmax],...
		'XLimMode','manual',...
		'YLimMode','manual',...
		'DrawMode','fast');
xlabel('Row Number')
ylabel('Pixel Intensity')
XPIX=1:NUMPIX;
XROW=1:NUMROWS;
% Create the plot lines (initialize to zero)
axes(RAXES)
RLINE=line(XPIX,zeros(size(XPIX)));
set(RLINE,'EraseMode','background');
axes(CAXES)
CLINE=line(XROW,zeros(size(XROW)));
set(CLINE,'EraseMode','background');
if PLOTROW
	set(RAXES,'Visible','on');
	set(RLINE,'Visible','on');
	set(CAXES,'Visible','off');
	set(CLINE,'Visible','off');
	set(PFIG,'Name','Row Profile');
else
	set(RAXES,'Visible','off');
	set(RLINE,'Visible','off');
	set(CAXES,'Visible','on');
	set(CLINE,'Visible','on');
	set(PFIG,'Name','Column Profile');
end
pp=double(gmax)-15; 
STEXT=text(10,pp,' ');
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
clear global STEXT
