% =======================================================================================
% varargout = viewImage(varargin);
% (c) Jan Modersitzki 2008/05/02, see FAIR and FAIRcopyright.m.
% Main function for image visualization, calls:
%
% visualize:
%  vh = viewImage(T,omega,m);
%  visualizes the interpolated data (based on m) on a domain Omega (based on omega)

%
% administration, parameter setting:
%  for resetting, intitializing, setting, updating, clearing, displaying,
%  see options.m
%
% specific optn:
%  viewImage(T,omega,m,specific{:}), uses specific for this call
% =======================================================================================

function varargout = viewImage(varargin);

% handle options
persistent OPTN 
[method,OPTN,task,stop] = options(OPTN,varargin{:});
if stop,
  varargout{1} = method;
  if nargout > 1, varargout{2} = OPTN;  end;
  return;
end

% update options and split
[method,optn] = options(OPTN,'set',varargin{4:end});
if isempty(method), error('no method specified!'); end;


ih = []; B = [];
% -----------------------------------------------------------------------------
% start to work
Tc     = varargin{1};
omega0 = reshape([0;1]*ones(1,length(size(Tc))),1,[]); 
if nargin<2, omega = omega0;   else omega = varargin{2}; end;
if nargin<3, m     = size(Tc); else m     = varargin{3}; end;

%make optn struct
optn = cell2struct(optn(2:2:end),optn(1:2:end),2);

% shortcut to value of boolean option str
OK = @(str) (isfield(optn,str) && ~isempty(optn.(str)));

% scale image to [0,255]
if OK('scale'), 
  minTc = min(Tc);
  maxTc = max(Tc);
  dG = (maxTc-minTc); dG = dG + 2*(dG == 0);
  Tc = 255/dG*(Tc-minTc);
end;

% invert image
if OK('invert'),
  Tc = max(Tc(:))-Tc; 
  %Tc = 255-max(Tc)+Tc;
end;

% shortcut to value of option str
value = @(str) options(optn,'get',str);

% setup figure
fig = value('fig');
if isempty(fig), 
  fig = gcf;  
elseif fig == 0,
  fig = figure; 
else 
  figure(fig); 
end;

% determine the subfigure
sub = value('sub');
if any(sub ~= 1),
  subplot(sub(1),sub(2),sub(3)); cla;
end;

% set the figure name
figname = value('figname');
if ~isempty(figname),
  set(fig,'numbertitle','off','name',sprintf('[FAIR:%d]: %s',fig,figname));
end;

% splitt options into internal 
internals = {mfilename,'scale','invert','fig','sub','figname'};

% remove internals from list 
for k=1:length(internals),
  if isfield(optn,internals{k}), optn = rmfield(optn,internals{k});  end;
end;

% transfer struct optn back to list
[dummy,optn] = options(optn);

% call the viewer with remaining options
switch nargout,
  case 0, feval(method,Tc,omega,m,optn{:});
  case 1, 
    ih = feval(method,Tc,omega,m,optn{:});
    varargout = {ih}; 
  otherwise, 
    [ih,B]    = feval(method,Tc,omega,m,optn{:});
    varargout = {ih,B}; 
end;
drawnow



%$=======================================================================================
%$  FAIR: Flexible Algorithms for Image Registration
%$  Copyright (c): Jan Modersitzki
%$  1280 Main Street West, Hamilton, ON, Canada, L8S 4K1
%$  Email: modersitzki@mcmaster.ca
%$  URL:   http://www.cas.mcmaster.ca/~modersit/index.shtml
%$=======================================================================================
%$  No part of this code may be reproduced, stored in a retrieval system,
%$  translated, transcribed, transmitted, or distributed in any form
%$  or by any means, means, manual, electric, electronic, electro-magnetic,
%$  mechanical, chemical, optical, photocopying, recording, or otherwise,
%$  without the prior explicit written permission of the authors or their 
%$  designated proxies. In no event shall the above copyright notice be 
%$  removed or altered in any way.
%$ 
%$  This code is provided "as is", without any warranty of any kind, either
%$  expressed or implied, including but not limited to, any implied warranty
%$  of merchantibility or fitness for any purpose. In no event will any party
%$  who distributed the code be liable for damages or for any claim(s) by 
%$  any other party, including but not limited to, any lost profits, lost
%$  monies, lost data or data rendered inaccurate, losses sustained by
%$  third parties, or any other special, incidental or consequential damages
%$  arrising out of the use or inability to use the program, even if the 
%$  possibility of such damages has been advised against. The entire risk
%$  as to the quality, the performace, and the fitness of the program for any 
%$  particular purpose lies with the party using the code.
%$=======================================================================================
%$  Any use of this code constitutes acceptance of the terms of the above
%$                              statements
%$=======================================================================================
