%CUT   Cuts/crops an image symmetrically around the center
%
% SYNOPSIS:
%  out = cut(in,newsize)
%
% PARAMETERS:
%  in:       input image
%  newsize:  new image size
%
% EXAMPLE:
%  a = readim;
%  cut(a,[300 100])    % symmetric cut
%  cropim(a,[300 100]) % cut from upper left corner
%
% SEE ALSO:
%  cropim, extend

% (C) Copyright 2004-2007      Department of Molecular Biology
%     All rights reserved      Max-Planck-Institute for Biophysical Chemistry
%                              Am Fassberg 11, 37077 G"ottingen
%                              Germany
%
% Bernd Rieger, Sep 2004.
% 9 April 2007, Rewrote to use SUBSREF instead of EVAL. (CL)
% 21 July 2010, Keeping image origin in the correct place. (CL)

function out = cut(varargin)
d = struct('menu','Manipulation',...
   'display','Cut (cropping)',...
   'inparams',struct('name', {'in','bor'},...
         'description',{'Input image','New image size'},...
         'type',       {'image','array'},...
         'dim_check',  {0,1},...
         'range_check',{[],'N+'},...
         'required',   {1,1},...
         'default',    {'a','256'}...
        ),...
   'outparams',struct('name',{'out'},...
                      'description',{'Output image'},...
                      'type',{'image'}...
                      )...
       );
if nargin == 1
   s = varargin{1};
   if ischar(s) & strcmp(s,'DIP_GetParamList')
      out = d;
      return
   end
end
try
   [in,bor] = getparams(d,varargin{:});
catch
   if ~isempty(paramerror)
      error(paramerror)
   else
      error(firsterr)
   end
end
if ~isscalar(in)
   error('Only implemented for scalar (grey-value) images.');
end
sz = imsize(in);
N  = length(sz);

tmp = bor >= sz;
bor(tmp) = sz(tmp);  %if newsize > oldsize do nothing
if all(tmp)
   out = in;
   return;
end

start = (sz-bor)./2;            % start coord
for ii=find(mod(start,1))
   if mod(sz(ii),2) % odd size input
      start(ii) = floor(start(ii));
   else             % even size input
      start(ii) = ceil(start(ii));
   end
end
stop  = start+bor-1;            % end coord

s = substruct('()',{});
for ii = 1:N
   s.subs{ii} = start(ii):stop(ii);
end
out = subsref(in,s);

%%% TEST CODE:
% x = xx(4);
% y = extend(x,5);
% z = extend(x,6);
% if any(cut(y,4) - x), disp('Error!!!'), end
% if any(cut(z,4) - x), disp('Error!!!'), end
% 
% x = xx(3);
% y = extend(x,5);
% z = extend(x,6);
% if any(cut(y,3) - x), disp('Error!!!'), end
% if any(cut(z,3) - x), disp('Error!!!'), end
