
function dft2img(action,varargin)
%DFT2IMG  2D DFT coefficients and the image relation demo.
%
%

%Author: J. Yang  2/2/99
%Copyright: Professor Jan. Allebach at Purdue ECE 
%$Revision:  1.01 $  $Date:  2/12/99     $

%--------<< Subfunctions: >>-------------------
% 1. InitDEMO
% 2-1. CBMenu_open
% 2-2. CBMenu_image
% 2-3. CBMenu_display
% 2-4. CBMenu_sequence
% 2-5. CBMenu_plottype
% 3-1. Pb_manualrun
% 3-2. Pb_autorun
% 3-3. Pb_close
% 4-1. Action_loadimage
% 4-2. Action_fft
% 4-3. Action_compostep
% 4-3-1. get_offset
% 4-4. Action_auto
%----------------------------------------------

if nargin < 1,
   action='InitDemo' ;
end ;

feval(action,varargin{:}) ;
return ;


%%%%%%% Sub function -- InitDEMO
function InitDemo()
%%
%%
% If dctdemo is already running, bring it to the foreground.
h = findobj(allchild(0), 'tag', '2D DFT Coefficients');
if ~isempty(h)
   figure(h(1))
   return
end

% not running, load the MAT file
%global pbtnval pbtnmode ;
global pbtnval pbtnmode coords;
    pbtnval= 0 ; pbtnmode = 0 ;
    coords = [0 0] ;
% Set up for screen frame and attributes
Demoframe = figure('Color',[0.7 0.7 0.7], ...
   'Colormap',gray(256), ...
	'MenuBar','none', ...
	'Name','2D DFT Coefficients', ...
   'Visible','off', 'Resize','off', ... % initially not drawn
	'NumberTitle','off', ...
   'Units','pixels', ...
	'Position',[20 50 700 800], ...
	'Tag','DFT coeff');

%%%---------< Menu bar >----------------------
% 1.Menubar menu item IMAGE open
%----
Hdl.umnu1 = uimenu('Parent',Demoframe, ...
	'Label','Image', ...
	'Tag','uimenu1');
%% 1.1 submenu OPEN 	

call_routine = 'dft2img(''CbMenu_open'');' ;
Hdl.sumnu1_1 = uimenu('Parent',Hdl.umnu1, ...
	    'Callback', call_routine , ...
	    'Label','Open', ...
	    'Tag','uimenu1_1');
%% 1.2 submenu LIST
Hdl.liststr = {'Grid' 'Checker' 'Rectangle' 'Circle' 'Cross'} ;
call_routine = 'dft2img(''CbMenu_image'');' ;
Hdl.sumnu1_2 = uimenu('Parent',Hdl.umnu1, ...
	    'Label','List', ...
	    'Callback', call_routine , ...
	    'Tag','uimenu1_2');
%----
% 2. Menubar DISPLAY for DFT View angle pair 
%----
Hdl.umnu2 = uimenu('Parent',Demoframe, ...
	'Label','DFT Plot', ...
	'Userdata',1, ...
	'Tag','uimenu2');
%% 2.1 submenu DFT 3D plot
call_routine = 'dft2img(''CbMenu_display'',1);' ;
Hdl.sumnu2_1 = uimenu('Parent',Hdl.umnu2, ...
      'Label','3D plot', ...
      'Callback', call_routine , ...
      'checked','on', ...
      'Tag','uimenu2_1') ;
%% 2.2 submenu CUMULATE
call_routine = 'dft2img(''CbMenu_display'',2);' ;
Hdl.sumnu2_2 = uimenu('Parent',Hdl.umnu2, ...
      'Label','2D plot', ...
      'Callback', call_routine , ...
      'Tag','uimenu2_2') ;
%----          	
% Menubar 3. SEQUENCE for display components
%----
%listr = { 'Hori & Vert pts', ' Diagonal pts', 'Spiral pts'} ;
Hdl.umnu3 = uimenu('Parent',Demoframe, ...
	'Label','Sequence', ...
	'Userdata',3 , ...
	'Tag','uimenu3');
%% 3.1 submenu Hori & Vert pts
call_routine = 'dft2img(''CbMenu_sequence'',1);' ;
Hdl.sumnu3_1 = uimenu('Parent',Hdl.umnu3, ...
      'Label', 'Hori & Vert pts' , ...
      'Callback', call_routine , ...
      'Tag','uimenu3_1') ;
%% 3.2 submenu Diagonal pts
call_routine = 'dft2img(''CbMenu_sequence'',2);' ;
Hdl.sumnu3_2 = uimenu('Parent',Hdl.umnu3, ...
      'Label', 'Diagonal pts' , ...
      'Callback', call_routine , ...
      'Tag','uimenu3_2') ;
%% 3.3 submenu Spiral pts
call_routine = 'dft2img(''CbMenu_sequence'',3);' ;
Hdl.sumnu3_3 = uimenu('Parent',Hdl.umnu3, ...
      'Label', 'Spiral pts' , ...
      'Callback', call_routine , ...
      'checked','on', ...
      'Tag','uimenu3_3') ;
%-----
% 4.Menubar FF stops
%-----
Hdl.umnu4 = uimenu('Parent',Demoframe, ...
	'Label','FF Stops', ...
	'Userdata',3 , ...
	'Tag','uimenu4');
%% 4.1 submenu Mesh
call_routine = 'dft2img(''CbMenu_plottype'',1);' ;
Hdl.sumnu4_1 = uimenu('Parent', Hdl.umnu4, ...
	    'Label', '2 stops', ...
	    'Callback', call_routine, ...
	    'Tag', 'uimenu4_1') ;
%% 4.2 submenu Surface
call_routine = 'dft2img(''CbMenu_plottype'',2 );' ;
Hdl.sumnu4_2 = uimenu('Parent', Hdl.umnu4, ...
	    'Label', '4 stops', ...
	    'Callback', call_routine, ...
	    'Tag', 'uimenu4_2') ;
%% 4.3 submenu gray Tone
call_routine = 'dft2img(''CbMenu_plottype'',3 );' ;
Hdl.sumnu4_3 = uimenu('Parent', Hdl.umnu4, ...
      'Label', '8 stops', ...
	   'Callback', call_routine, ...
      'checked','on', ...
      'Tag', 'uimenu4_3') ;
%%%-------< POP UP Button >----
Phdl.pbtnval = 0 ;
Phdl.automode = 1 ;
Hdl.pbtn1 = uicontrol('Parent',Demoframe, ...
	'Units','pixels', ...
	'BackgroundColor',[0.7 0.7 0.7], ...
	'Position',[235 22 72 24], ...
	'String','Manual run', ...
	'Callback','dft2img(''Pb_manualrun'');' , ...
  'Userdata',Phdl, ...
	'Tag','Pushbutton1');
CBStr1 ='dft2img(''Pb_autorun'');' ;
CBStr2 ='dft2img(''Action_auto'');' ;
Hdl.pbtn2 = uicontrol('Parent',Demoframe, ...
	'Units','pixels', ...
	'BackgroundColor',[0.7 0.7 0.7], ...
	'Position',[358 22 72 24], ...
	'String','Auto run', ...
	'Callback',strcat(CBStr1,CBStr2) , ...
	'Userdata',{CBStr1,CBStr2}, ...
	'Tag','Pushbutton2');
Hdl.pbtn3 = uicontrol('Parent',Demoframe, ...
	'Units','pixels', ...
	'BackgroundColor',[0.7 0.7 0.7], ...
	'Position',[479 22 72 24], ...
	'String','close', ...
	'Callback', 'dft2img(''Pb_close'');', ... %' close( gcbf ); ', ...
	'Tag','Pushbutton3');

%------------< AXES >------------------------
%----
% The Left Axes image
%----
Adl.cumul = [] ;
Adl.coords = zeros(2) ; % [shell, cnt]
Hdl.leftimgAxes = axes('Parent',Demoframe, ...
   'Units','pixels', ...
   'BusyAction','Queue','Interruptible','off',...
   'ydir', 'reverse', ...
   'XLim', [-1 258], ...
   'YLim', [-1 258],...
   'CLim', [0 1], ...
   'Position',[398 150  256 256], ...
   'Box','on', 'layer','top', ...   
   'Drawmode','fast', ...
   'Userdata',Adl, ...
	 'XTick',[],'YTick',[], ...
   'Tag','leftaxes1') ;
Hdl.leftimg = image('Parent', Hdl.leftimgAxes, ...
   'Visible', 'off', ...
   'CData', [], ...
   'BusyAction','Queue','Interruptible','off',...
   'CDataMapping', 'direct', ...
   'Xdata', [1:256],...
   'Ydata', [1:256],...
   'EraseMode', 'normal');   % should be none, HG geck 
title('Space Domain DFT component') 
%----
% place for left up view rectangle for original image
%----
Hdl.luimgAxes = axes('Parent',Demoframe, ...
   'Units','pixels', ...
   'BusyAction','Queue','Interruptible','off',...
   'ydir', 'reverse', ...
   'Box','on','layer','top', ...   
   'XLim', [-1 258], ...
   'YLim', [-1 258],...
   'CLim', [0 1], ...
   'Drawmode','fast', ...
   'Position',[34 470  256 256], ...
   'XTick',[],'YTick',[], ...
   'Userdata',Adl, ...
   'Tag','leftupaxes1') ;
Hdl.luimg = image('Parent',Hdl.luimgAxes, ...
   'Visible', 'off', ...
   'CData', [], ...
   'BusyAction','Queue','Interruptible','off',...
   'CDataMapping', 'direct', ...
   'Xdata', [1:256],...
   'Ydata', [1:256],...
   'EraseMode', 'normal');   % should be none, HG geck 

title('Original Image') ;

% The Right Axes image ( cumulative, or Response )
Hdl.rightimgAxes = axes('Parent', Demoframe, ...
   'Units','pixels', ...
   'BusyAction','Queue','Interruptible','off',...
   'ydir', 'reverse', ...
   'Box','on','layer','top', ...   
   'Drawmode','fast', ...
   'XLim', [-1 258], ...
   'YLim', [-1 258],...
   'CLim', [0 1], ...
   'Position',[34 150 256 256], ...
   'Tag', 'rightaxes1', ...
   'XTick',[],'YTick',[]);
Hdl.rightimg = image('Parent', Hdl.rightimgAxes,...
   'Visible','off', ...
   'CData', [], ...
   'BusyAction','Queue','Interruptible','off',...
   'CDataMapping', 'direct', ...
   'Xdata', [1:256],...
   'Ydata', [1:256],...
   'EraseMode', 'normal');   % should be none, HG geck
title('Cumulative DFT components'' effect') ; 
% share place for right up 3D view rectangle
Hdl.ruimgAxes = axes('Parent',Demoframe, ...
   'Units','pixels', ...
   'BusyAction','Queue','Interruptible','off',...
   'Visible', 'off', ...
   'Box','on','layer','top', ...
   'XGrid','on','YGrid','on','ZGrid','on', ...
   'view',[-37.5 30] ,...
   'ZScale', 'log', ...
   'XLim', [.5 256.5], ...
   'YLim', [.5 256.5],...
   'Position',[398 470 256 256], ...
   'Drawmode','fast', ...
   'Tag', 'rightupaxes1', ...
   'XTickLabel','-128|-64|0|64|128', ...
   'YTickLabel','-128|-64|0|64|128', ...
   'XTick',[1 64 128 192 256], 'YTick',[1 64 128 192 256]);
set(Hdl.ruimgAxes,   'XLabel', text('String','u') , ...
   'YLabel', text('String','v'), ...
   'ZLabel', text('String','Log |DFT(u,v)|') ) ;

Hdl.rusfc = surface('Parent', Hdl.ruimgAxes, ...  
   'Visible','off', ...
   'BackFaceLighting','unlit', ...
   'FaceLighting','flat', ...
   'CDataMapping','direct', ...
   'XData',[1:256], 'YData',[1:256], ...
   'EraseMode','normal', ...
   'EdgeLighting','flat');
title('DFT coefficients in 3D view')

% share place for right up 2D view rectangle
Hdl.rusimgAxes = axes('Parent',Demoframe, ...
   'Units','pixels', ...
   'BusyAction','Queue','Interruptible','off',...
   'Visible', 'off' , ...
   'Box','on','layer','top', ...   
   'Drawmode','fast', ...
   'XLim', [-1 258], ...
   'YLim', [-1 258],...
   'XGrid','on', 'YGrid','on', ...
   'XTickLabel','-128|-64|0|64|128', ...
   'YTickLabel','-128|-64|0|64|128', ...
   'Position',[398 470 256 256], ...
   'Tag', 'srightupaxes1', ...
   'XTick',[1 64 128 192 256], ...
   'YTick',[1 64 128 192 256]);
%%   'ydir', 'reverse', ...
Hdl.rusimg = image('Parent',Hdl.rusimgAxes, ...
   'CData', [], ...
   'BusyAction','Queue','Interruptible','off',...
   'Visible', 'off', ...
   'CDataMapping', 'direct', ...
   'Xdata', [1:256],...
   'Ydata', [1:256],...
   'EraseMode', 'normal');   % should be none, HG geck 
title('DFT coefficients in 2D view') ;
%%-----------< status bar >----------------------
bc = get(Demoframe,'Color') ;
Hdl.status = uicontrol( 'Parent', Demoframe, ...
   'BusyAction','Queue','Interruptible','off', ...
   'Style','text', ...
   'Units','pixels', ...
   'Position',[20 30 200 18], ...
   'Foreground',[0.8 0 0], ...
   'Background',bc, ...
   'Horiz', 'center', ...
   'Tag','status', ...
   'string','Init Demo Frame ...') ;
%===========================================
% set up USERDATA Hdl struct to Demoframe
%===========================================
Hdl.defaultname = 'race.tif' ;
Hdl.img = [] ;
Hdl.fft = [] ;
Hdl.menusel = [1 1 3 3] ;
Hdl.autotime = 1 ;
Hdl.ffstep = 20 ;
set([Hdl.pbtn1 Hdl.pbtn2], 'enable', 'off') ;
set(Demoframe, 'Userdata', Hdl) ;

%==========================================
%   Actual init sub routines
%==========================================
Action_loadimage(Demoframe,[]) ;
Action_fft(Demoframe) ;

%==========================================
%   Initial display of the figure
%==========================================
hdl = get(Demoframe,'Userdata') ; % need updated handle for data coherency
set([hdl.leftimg hdl.rightimg hdl.luimg], 'Visible', 'on') ;
set([hdl.pbtn1 hdl.pbtn2], 'enable', 'on') ;
set([hdl.rusfc hdl.ruimgAxes],'Visible','on') ;
set(Demoframe, 'Userdata', hdl, 'Visible', 'on') ;
drawnow
return ;
%%}} end of initDemo


%%%% ----< Sub function Open menu >-----------

function CbMenu_open(Demofrm)

%{{{

if nargin<1       % when called through menu Callback

   Demofrm = gcbf ;

end

global pbtnval pbtnmode coords;
    pbtnval= 0 ; pbtnmode = 0 ;
    coords = [0 0] ;
[fname,path] = uigetfile('*.tif','Open an image file :') ;

if fname ~= 0
   fname = strcat(path, fname) ;
   Action_loadimage(Demofrm,fname) ;
   Action_fft(Demofrm) ;
end ;

return ;

%}}} end of CbMenu_open


%%%% ----< Sub function ImageList menu >------
function CbMenu_image(Demofrm)
%{{{
%
global pbtnval pbtnmode coords;
    pbtnval= 0 ; pbtnmode = 0 ;
    coords = [0 0] ;
    
if nargin < 1       % when called through menu Callback

   Demofrm = gcbf ;

end

hdl = get(Demofrm,'Userdata') ;

[sel,val] = listdlg('PromptString', 'Select an image file:', ...
                'SelectionMode', 'single' , ...
                'ListString', hdl.liststr) ;
if val == 0 ; 
   return ; 
end ;

if exist('demo1.mat')
  switch( sel )
  case 1
     Grid =[] ; load demo1 Grid ; img = Grid ;
  case 2
     Checker=[]; load demo1 Checker; img = Checker ;
  case 3
     Rectangle=[]; load demo1 Rectangle; img = Rectangle ;
  case 4
     Circle=[]; load demo1 Circle; img=Circle ;
  case 5
     Cross=[]; load demo1 Cross; img = Cross ;
  end ;

  hdl = get(Demofrm,'Userdata') ;
  x = double(img) /255 ;
  hdl.img = img ;
  ud = get(hdl.leftimgAxes,'Userdata') ;
  ud.cumul = zeros(size(x)) ;
  set(hdl.leftimgAxes,'Userdata',ud) ;
  set(hdl.luimg,'CData',cat(3,img,img,img)) ;
  set(Demofrm,'Userdata',hdl) ;
  drawnow
  Action_fft(Demofrm) ;
else
   errmsg = [' "demo1.dat" file is not found.' ] ;
   errordlg(errmsg,'FILE NON-EXISTENT ERROR','modal') ;
end ;

return ;               
%}}} end of CbMenu_image

%%%% ----< Sub function Display menu >--------
function CbMenu_display(id)
%{{{
%
Demofrm = gcbf ;

hdl = get(Demofrm, 'Userdata') ;

if hdl.menusel(2) == id

   return ;

end



set([hdl.sumnu2_1 hdl.sumnu2_2],'checked','off') ;

switch id

case 1 % 3D View
   set(hdl.sumnu2_1, 'checked','on') ;
   % change screen
   set([hdl.rusimgAxes hdl.rusimg],'Visible','off') ;
   set([hdl.ruimgAxes hdl.rusfc],'Visible','on') ;
case 2 % 2D view

   set(hdl.sumnu2_2, 'checked','on') ;
   % change screen
   set([hdl.ruimgAxes hdl.rusfc],'Visible','off') ;
   set([hdl.rusimg hdl.rusimgAxes],'Visible','on') ;
end

hdl.menusel(2) = id ;

set(Demofrm,'Userdata',hdl) ;
drawnow ;

return ;
%}}}

%%%% ----< Sub function Sequence menu >------
function CbMenu_sequence(id)
%{{{
%
Demofrm = gcbf ;

hdl = get(Demofrm, 'Userdata') ;

if hdl.menusel(3) == id

   return ;

end



set([hdl.sumnu3_1 hdl.sumnu3_2 hdl.sumnu3_3],'checked','off') ;

switch id

case 1 % H+V
   set(hdl.sumnu3_1,'checked','on') ;

case 2 % Diag

   set(hdl.sumnu3_2,'checked','on') ;

case 3 % Spiral

   set(hdl.sumnu3_3,'checked','on') ;

end

hdl.menusel(3)= id ;

set(Demofrm,'Userdata',hdl) ;
return ;
%}}} end of CbMenu_sequence

%%%% ----< Sub function FF stops menu : 2, 4, 8 >------
function CbMenu_plottype(id)
%{{{
%

Demofrm = gcbf ;

hdl = get(Demofrm, 'Userdata') ;

if hdl.menusel(4) == id

   return ;

end


set([hdl.sumnu4_1 hdl.sumnu4_2 hdl.sumnu4_3], 'checked', 'off') ;


switch id

case 1 % 2 FF stops
   set(hdl.sumnu4_1, 'checked','on') ;
case 2 % 4 FF stops
   set(hdl.sumnu4_2, 'checked','on') ;
case 3 % 8 FF stops
   set(hdl.sumnu4_3, 'checked','on') ;
end

hdl.menusel(4) = id ;

set(Demofrm,'Userdata',hdl) ;

return ;
% }}}  end of CbMenu_plottype



%%%% ----<Sub function Manual Run pushbutton >---

function Pb_manualrun(Demofrm)

% {{{

   global pbtnval pbtnmode coords;
%
Demofrm = gcbf ;

set(Demofrm,'Pointer','watch') ;
hdl = get(Demofrm,'Userdata') ;

switch pbtnval
case 0   % initial (Manual*/Auto)
 %disp('Manual button') ;  
   set([hdl.umnu1 hdl.umnu2 hdl.umnu3 hdl.umnu4], 'Enable', 'off') ;
   pbtnval = 100 ;
   pbtnmode = 0 ;
   ud = get(hdl.leftimgAxes,'Userdata') ;
   coords = [0,0] ;
   hdl.ffstep = 0 ;
   ud.cumul = zeros(size(hdl.fft)) ;
   set(hdl.leftimgAxes,'Userdata',ud) ;
   set(hdl.pbtn1,'String','Step') ;
   set(hdl.pbtn2,'String','FF') ;
   ub = get(hdl.pbtn2,'Userdata') ;
   set(hdl.pbtn2,'Callback', ub{1} ) ; % change single call back !
   set(hdl.status,'String','Manual running ...') ;
case 100 % when manual (Step*/FF)
 %disp('Step button') ;  
   set(Demofrm,'Userdata',hdl) ;   % copy by value ,  not reference !!
   v = Action_compostep(Demofrm,1) ;
   hdl = get(Demofrm,'Userdata') ;
   if v == 1
      pbtnval = 0 ; pbtnmode = 0 ;
      set(hdl.pbtn1,'String','Manual run') ;
      set(hdl.pbtn2,'String','Auto run') ;
      ud = get(hdl.pbtn2,'Userdata') ;
      set(hdl.pbtn2,'Callback',strcat(ud{1},ud{2}) ) ;
      set([hdl.umnu1 hdl.umnu2 hdl.umnu3 hdl.umnu4], 'Enable', 'on') ;
   set(hdl.status,'String',' ');
   end
case 200 % when auto (FF*/Pause)
 %disp('auto FF') ;  
   pbtnmode = 2 ;
   set(hdl.status,'String','Auto FastForwarding ...');
   set(Demofrm,'Userdata',hdl,'Pointer','arrow') ;
   return ;  % do not draw here
case 210 % when auto (Stop*/Resume)
 %disp('auto stop') ;  
     pbtnmode = 0 ;
      pbtnval = 0 ;
      set(hdl.pbtn1,'String','Manual run') ;
      set(hdl.pbtn2,'String','Auto run') ;
      set([hdl.umnu1 hdl.umnu2 hdl.umnu3 hdl.umnu4], 'Enable', 'on') ;
      ud = get(hdl.pbtn2,'Userdata') ;
      set(hdl.pbtn2,'Callback',strcat(ud{1},ud{2}) ) ;
         % initially set to 'Interruptible','on' ) ;
   set(hdl.status,'String',' ');
end
set(Demofrm,'Userdata',hdl,'Pointer','arrow') ;
drawnow

return ;

% }}} end of Pb_manual run



%%%% ----< Sub function Auto Run pushbutton >----

function Pb_autorun(Demofrm)

% {{{

   global pbtnval pbtnmode coords;
%

Demofrm = gcbf ;

set(Demofrm,'Pointer','watch') ;
hdl = get(Demofrm,'Userdata') ;

switch pbtnval
case 0   % initial (Manual/Auto*)
 %disp('Auto button') ;
   set([hdl.umnu1 hdl.umnu2 hdl.umnu3 hdl.umnu4], 'Enable', 'off') ;
   
   pbtnval = 200 ;
   pbtnmode = 1 ;
   ud = get(hdl.leftimgAxes,'Userdata') ;
   coords = [0,0] ;
   hdl.ffstep = 0 ;
   ud.cumul = zeros(size(hdl.fft)) ;
   set(hdl.leftimgAxes,'Userdata',ud) ;
   set(hdl.pbtn1,'String','FF') ;
   set(hdl.pbtn2,'String','Pause') ;
   ub = get(hdl.pbtn2,'Userdata') ;
   set(hdl.pbtn2,'Callback',ub{1}) ; % dft2img(Pb_autorun) ;
   set(hdl.status,'String','Auto stepping ...');
case 100 % when manual (Step/FF*)
   %% to maintain consistency in Demofrm.'Userdata' field
 %disp('manu FF') ;  
   set(Demofrm,'Userdata',hdl) ;
   hdl = get(Demofrm,'Userdata') ;
   set(hdl.status,'String','Manual FastForward ...') ;
   drawnow
   v = Action_compostep(Demofrm,2) ;
   hdl = get(Demofrm,'Userdata') ;
 
   if v == 1 ;
      pbtnval = 0 ; pbtnmode = 0 ;
      set(hdl.pbtn1,'String','Manual run') ;
      set(hdl.pbtn2,'String','Auto run') ;
   set(hdl.status,'String',' ') ;
      ud = get(hdl.pbtn2,'Userdata') ;
      set(hdl.pbtn2,'Callback',strcat(ud{1},ud{2}) ) ;
      set([hdl.umnu1 hdl.umnu2 hdl.umnu3 hdl.umnu4], 'Enable', 'on') ;
   end ;
case 200 % when auto (FF/Pause*)
 % disp('auto Pause') ;
   set(hdl.status,'String', strcat(get(hdl.status,'String'),'Paused'));
      pbtnval = 210 ;
      set(hdl.pbtn1,'String','Stop') ;
      set(hdl.pbtn2,'String','Resume') ;
case 210 % when auto (Stop/Resume*)
  %disp('auto Resume') ; 
      pbtnval = 200 ; 
      pbtnmode = 1 ;
   set(hdl.status,'String', strtok(get(hdl.status,'String'),'P'));
      set(hdl.pbtn1,'String','FF') ;
      set(hdl.pbtn2,'String','Pause') ;
end

set(Demofrm,'Userdata',hdl,'Pointer','arrow') ;
drawnow

return ;

% }}} end of Pb_autorun


function Pb_close()
% {{{
global pbtnval pbtnmode;
%
if pbtnmode == 0
   close(gcbf) ;
else
   pbtnval = 1000 ;
end 

return ;
% }}} last of Pb_close

%===============================
%  Action Sub routines
        %
%===============================
function Action_loadimage(Demofrm,fname)
% {{{{
%   dframe = Demofrm ;
   set(Demofrm,'Pointer','watch') ;
   hdl = get (Demofrm, 'Userdata') ;
   set(hdl.status,'String',['Loading the ' fname ' image...']) ;
   drawnow
   
   try
   if exist(fname)   % fname ~= [] or ''
      x = imread(fname) ;
   else 
      x = imread(hdl.defaultname,'TIFF') ; 
   end  
   catch
   errordlg(LASTERR,'MATLAB Error Message','modal') ;
   set(hdl.status,'String',' ') ;
   set(Demofrm,'Pointer','arrow',hdl) ;
   drawnow 
   return  ;
   end
   
   [xs ys] = size(x) ;
   if( xs>256 & ys>=256) | (ys>256 & xs>=256)
      xs = ceil( (xs-255)/2 ) ;
      ys = ceil( (ys-255)/2 ) ;
      y = x(xs:xs+255,ys:ys+255) ;
      x = y ;   
   else if( xs<256 | ys<256 )
      errmsg = ['The "' fname '" file has smaller image size than 256 x 256.' ...
            '\n Image Load operation aborted.'] ;
      errordlg(errmsg,'Image size error','modal') ;
      set(hdl.status,'String',' ') ;
      set(Demofrm,'Pointer','arrow','Userdata',hdl) ;
      drawnow
      return ;
      end ;   
   end ;
   
   img = double(x) / 255 ;
   hdl.img = x ;
   ud = get(hdl.leftimgAxes,'Userdata') ;
   ud.cumul = zeros(size(img)) ;
   set(hdl.leftimgAxes,'Userdata',ud) ;
   set(hdl.luimg, 'Cdata', cat(3,img,img,img)) ;
   set(hdl.status,'String',' ') ;
   set(Demofrm,'Pointer','arrow','Userdata',hdl) ;
   drawnow
 
   return ;
% }}}} end of Action_loadimage

function Action_fft(Demofrm)
% {{{{
  %% dframe = Demofrm ;

  set(Demofrm,'Pointer','watch') ;
  hdl = get (Demofrm, 'Userdata') ;
  set(hdl.status,'String','Calculating...') ;
  drawnow
  
  x = hdl.img ;
  F = fft2(x) ;
  F = fftshift(F) ;
  hdl.fft = F ;
  f = log(abs(F) + 1) ;
  
  z = f(1:256, 1:256) ;
  cmap = jet(256) ;cr = cmap(:,1) ;
 zmin = min(z(:)) ; zmax = max(z(:)) ;
 alpha = 254/(zmax - zmin) ;
 zidx = floor((z-zmin)*alpha + 1.5) ;
 
 for i=1:256,;
    znorm(i,:,:) = cmap(zidx(i,:),:)  ;
 end ;
 
 %%  sufhdl = mesh(z) ; %%surfl(z) ; %%mesh(z) ; this is surface handle
 %% 3D view update
 set(hdl.ruimgAxes,'zlim',[zmin-(.05*abs(zmin)) zmax*1.05]) ;  
 set(hdl.rusfc,'CData',znorm, ...
     'ZData',z) ;
 %% 2D view data update
 set(hdl.rusimg,'CData',znorm) ;

 set(hdl.status,'String',' ') ;
 set(Demofrm,'Pointer','arrow','Userdata',hdl) ;
 
 drawnow 
  
 return ;
  
% }}}} end of Action_fft

function  val = Action_compostep(Demofrm,mode)
% {{{{
  global coords ;
%
  hdl = get(Demofrm,'Userdata') ;

  val = 0 ;
  ud = get(hdl.leftimgAxes,'Userdata') ;
  z = zeros(size(hdl.fft)) ;
  l = coords(1) ; k = coords(2) ;
  if mode == 1 ;
    % hdl.fftinfo = ...  % [ x y cnt shell ]
    [i j k l] = get_offset(l, k, hdl.menusel(3)) ;
    if l > 127 ; val = 1 ; break; end ;
    cy = ceil(size(z,1)/2) + 1 ;
    cx = ceil(size(z,2)/2) + 1 ;
    z(cy+j,cx+i) = hdl.fft(cy+j,cx+i) ;
    z(cy-j,cx-i) = hdl.fft(cy-j,cx-i) ;
  else 
    cy = ceil(size(z,1)/2) + 1 ;
    cx = ceil(size(z,2)/2) + 1 ;
    
    j = 128 / (2^hdl.menusel(4)) ;
    i = 1 ;
    while( l >= i )  % l = shell
       hdl.ffstep = hdl.ffstep + 1 ;
       i = j * hdl.ffstep ;
       if( i > 127 ) val = 1 ; break ;end ;
    end ; 
    if( i> 127) i = 127 ;end ;
    k = 0 ; l = i ;
    
    switch hdl.menusel(3)
    case 1
    z(cy,cx-i:cx+i) = hdl.fft(cy,cx-i:cx+i) ; % hori
    z(cy-i:cy+i,cx) = hdl.fft(cy-i:cy+i,cx) ; % vert
    case 2
    for( j = -i:i ) ;
       z(cy+j,cx+j) = hdl.fft(cy+j,cx+j) ; % fall diag
       z(cy-j,cx-j) = hdl.fft(cy-j,cx-j) ; % rise diag
       z(cy+j,cx-j) = hdl.fft(cy+j,cx-j) ; % fall diag
       z(cy+j,cx-j) = hdl.fft(cy+j,cx-j) ; % rise diag
    end ;
    case 3
    z(cy-i:cy+i,cx-i:cx+i) = hdl.fft(cy-i:cy+i,cx-i:cx+i) ; % square
    end ;
 end ;

  coords = [l, k];

  f = real( ifft2( fftshift(z) ) );
  
  
  if(mode==1) % FF
    if i == 0 & j == 0
     set(get(hdl.leftimgAxes,'XLabel'),'String', ...
      sprintf('Component at ( %d, %d )',i,j) ) ;
     g = max(min(f/255,1),0) ;
    else
     set(get(hdl.leftimgAxes,'XLabel'),'String', ...
      sprintf('Components at ( %d, %d ) and ( %d, %d )',i,j,-i,-j) ) ;
     g = max(min(f/255 + 0.5,1.0),0.0) ;
    end ;
    f = ud.cumul + f ; 
    set(hdl.leftimg,'CData',cat(3,g,g,g));
  else % FF
    s = get(hdl.status,'String') ;
    s = sprintf('%s %d', strtok(s,'0123456789'),hdl.ffstep)  ;
    set(hdl.status, 'String', s) ;
  end ;
  ud.cumul = f ;
  f = max(min(f/255,1),0) ;
  set(hdl.rightimg,'CData',cat(3,f,f,f)) ;
  
  set(hdl.leftimgAxes,'Userdata',ud) ; 
  set(Demofrm,'Userdata',hdl) ;

  drawnow 
  return ;
% }}}} end of Action_compostep

function [xof,yof,cnt,shl] = get_offset(shell,lcnt,mode)
% {{{{{
   if shell==0
      xof = 0 ; yof = 0 ; cnt = 0 ; shl = 1 ;
      return ;
   end ;

   cnt = lcnt + 1 ;
   switch mode
   case 1  % H,V
	if cnt == 2 ;
	    cnt = 0 ; shl = shell + 1 ;
	   xof = shell ; yof = 0 ;
   else
      shl = shell ;
	   xof = 0; yof=shell ; shl = shell ;
	end ;
   case 2  % Diag
	if cnt == 2 
	   shl = shell + 1 ;
	   cnt = 0 ; xof = shell ; yof = -shell ;
	else
	   shl = shell; xof = shell ; yof = shell ;
	end ;
   case 3  % Sprial
        switch cnt 
        case 1
           xof = shell; yof = 0 ;
        case 2
           xof = 0; yof = shell ;
        case 3
           xof = shell; yof = shell ;
        case 4
           xof = -shell; yof = shell ;
        otherwise
           m = floor( (cnt-1)/4 ) ;
           switch bitand(cnt,3)
           case 1,
              yof = shell; xof = m ;
           case 2,
              yof = shell; xof = -m ;
           case 3,   
              yof = m; xof = shell ;
           case 0,
              yof = m; xof = -shell ;
           end
        end ;
        shl = shell ;
        if cnt == 4*shell ;
           shl = shell + 1 ;
           cnt = 0 ;
        end ; % start of new shell 
   end ;  % switch
   return ;
% }}}}} end of get_offset

function Action_auto()
% {{{{
  global pbtnval pbtnmode coords;
%
  Demofrm = gcbf ;
  hdl = get(Demofrm,'Userdata') ;
  
  tts = 0.5 ; %% hdl.autotime ;
  val = 0 ;
  while ( pbtnval >= 200 & val == 0 )  %% auto first press
     tic ;
     set(Demofrm,'Userdata',hdl) ;
     val = Action_compostep(Demofrm,pbtnmode) ;
     if( val == 1 ) break ; end ;
     while ( toc < tts ) , end ;
     while ( pbtnval == 210 ) ;
       pause(0.05) ;
     end ;
     if( pbtnval == 1000 ), break ; end ;
     hdl = get(Demofrm,'Userdata') ;
  end ;

  pbtnval = 0 ;
  pbtnmode = 0 ;
  coords = [0 0] ;
  set(hdl.pbtn1,'String','Manual run') ;
  set(hdl.pbtn2,'String','Auto run') ;
  set([hdl.umnu1 hdl.umnu2 hdl.umnu3 hdl.umnu4], 'Enable', 'on') ;
  set(hdl.status,'String',' ');
  ud = get(hdl.pbtn2,'Userdata') ;
  set(hdl.pbtn2,'Callback',strcat(ud{1},ud{2})) ;
  set(Demofrm,'Userdata',hdl,'Pointer','arrow') ;
 %disp('Action Auto end') ; 
  if( pbtnval == 1000 ) close( gcbf ) ; end ;
  return ;
% }}}} end of Action_auto
