## Copyright (C) 2012 P. Cloetens
## 
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

## cut
## function imcut = cut(im, nn, mn[, cnn, cmn])
##      cuts a sub-matrix out of a larger matrix
##      cuts in the center of the original matrix, except if new center is specified
##      NO CHECKING of validity indices sub-matrix!
##
##      arguments:
##      argument 1: original matrix im
##      argument 2: number of rows in result
##      argument 3: number of columns in result
##      argument 4: center row around which to cut ( default: center )
##      argument 5: center column around which to cut ( default: center )
##
##      examples:
##          im_roi = cut(im, 1024, 1024)                -> cut center 1024x1024 pixels
##          im_roi = cut(im, 1024, 1024, 600.5, 700.5)  -> cut 1024x1024 pixels around pixels (600-601, 700-701)

## Author: P. Cloetens <cloetens@esrf.eu>
## 
## 2012-10-14 P. Cloetens <cloetens@esrf.eu>
## * Initial revision

function imcut = cut(im, nn, mn, cnn, cmn)
    if !exist('cnn', 'var')
        cnn = [];
    endif
    if !exist('cmn', 'var')
        cmn = [];
    endif
    
	[n,m] = size(im);
	if isempty(cnn)
		cnn = (n+1)/2;
	endif
	if isempty(cmn)
		cmn = (m+1)/2;
	endif
	
	rb = round(0.5+cnn-nn/2);
	re = nn+rb-1;
	cb = round(0.5+cmn-mn/2);
	ce = mn+cb-1;
	imcut = im(rb:re, cb:ce);
endfunction
