%DIPPROJECTION   Calculates the max/sum projection along the not visible axis for 3D
%   DIPPROJECTION(H,PROJ)
%
%   H, the figure window handle, can be left out. It will default to
%   the current figure.
%   
%   PROJ: 'max','sum' 
%
%   The projection is displayed in a new window. For 4D images the projection is
%   done along the time axis if a spatial only slice is displayed, otherwise first
%   not visible dimension is used. For color images the operation is performed per channel
% 
%   See also DIPSHOW, DIP_IMAGE/MAX, DIP_IMAGE/SUM

% (C) Copyright 1999-2014               Pattern Recognition Group
%     All rights reserved               Faculty of Applied Physics
%                                       Delft University of Technology
%                                       Lorentzweg 1
%                                       2628 CJ Delft
%                                       The Netherlands
%
% Bernd Rieger, July 2004
% Feb 2005:       Added color images.
% 11 August 2014: Fix for new graphics in MATLAB 8.4 (R2014b).

function menu_out = dipprojection(varargin)

% Avoid being in menu
if nargin == 1 & ischar(varargin{1}) & strcmp(varargin{1},'DIP_GetParamList')
   menu_out = struct('menu','none');
   return
end

if nargin == 2 & ischar(varargin{1})
   if strcmp(varargin{1},'DIP_RangeDlg_Callback')
      rangecallbacks(varargin{2});
      return
   elseif strcmp(varargin{1},'DIP_ColmapDlg_Callback')
      colmapcallbacks(varargin{2});
      return
   end
end

% Defaults...
fig = [];
promax =0;
prosum =0;

% Parse input
N = nargin;
if N==0
   error('At least one argument required.')
end
ii = 1;
while ii<=N
   arg = lower(varargin{ii});
   if ischar(arg)
      switch arg
         case {'max'}
            if promax, error('Too many arguments.'); end
            promax = 1;
         case {'sum'}
            if prosum, error('Too many arguments.'); end
            prosum = 1;
      end
   else
      if ( isnumeric(arg) | ishandle(arg) ) & length(arg)==1 & ii==1 & N>1
         % figure handle parameter?
         try
            fig = getfigh(arg);
         catch
            error('Argument must be a valid figure handle.')
         end
      else
         if ~isnumeric(arg) | ~(isempty(arg) | prod(size(arg))==2)
            error('Arguments should be strings or two-element vectors.')
         end
         if prosum
            error('Too many arguments.')
         end
         prosum = 1;
      end
   end
   ii = ii+1;
end
if isempty(fig)
   fig = get(0,'CurrentFigure');
   if isempty(fig)
      error('No figure window open to do operation on.')
   end
end
if ~strncmp(get(fig,'Tag'),'DIP_Image',9)
   error('DIPPROCETION only works on images displayed using DIPSHOW.')
end


udata = get(fig,'UserData');
is3D =length(udata.imsize)==3;
slc = udata.slicing;
if is3D
   switch slc
      case 'xy'
         proj = 3;
      case 'xz'
         proj = 2;
      case 'yz'
         proj = 1;
      otherwise
         error('Should not happen, unkown slicing.');
   end
else
   switch slc
      case 'xy'
         proj = 4;%sum over time if spatial only slice is shown
      case 'xz'
         proj = 4;
      case 'yz'
         proj = 4;
      case 'xt'
         proj = 2;
      case 'yt'
         proj = 1;
      case 'zt'
         proj = 1;
      otherwise
         error('Should not happen, unkown slicing.');
   end
end

iscol = isfield(udata,'colordata');
imagename = udata.imname;
if prosum
   img = udata.slices;
   if iscol
      %#function sum
      tmp = iterate('sum',img,[],proj);
   else
      tmp = sum(img,[],proj);
   end
   dipshow(tmp,'lin','name',[imagename '_sumproj']); 
elseif promax
   img = udata.slices;
   if iscol
      %#function max
      tmp = iterate('max',img,[],proj);
   else
      tmp = max(img,[],proj);
   end
   dipshow(tmp,'lin','name',[imagename '_maxproj']);    
else
   error('should not happen.');
end

