]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/imresize.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / imresize.m
1 ## Copyright (C) 2005 Søren Hauberg
2 ## 
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
7 ## 
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ## GNU General Public License for more details.
12 ## 
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} @var{B}= imresize (@var{A}, @var{m})
18 ## Scales the image @var{A} by a factor @var{m} using bicubic neighbour
19 ## interpolation. If @var{m} is less than 1 the image size will be reduced,
20 ## and if @var{m} is greater than 1 the image will be enlarged.
21 ##
22 ## @deftypefnx {Function File} @var{B}= imresize (@var{A}, @var{m}, @var{interp})
23 ## Same as above except @var{interp} interpolation is performed instead of
24 ## using nearest neighbour. @var{interp} can be any interpolation method supported by interp2.
25 ## By default, bicubic interpolation is used.
26 ##
27 ## @deftypefnx {Function File} @var{B}= imresize (@var{A}, [@var{mrow} @var{mcol}])
28 ## Scales the image @var{A} to be of size @var{mrow}x@var{mcol}.
29 ##
30 ## @deftypefnx {Function File} @var{B}= imresize (@var{A}, [@var{mrow} @var{mcol}], @var{interp})
31 ## Same as above except @var{interp} interpolation is performed. @var{interp} can
32 ## be any interpolation method supported by interp2.
33 ## @seealso{imremap, imrotate, interp2}
34 ## @end deftypefn
35
36 function ret = imresize(im, m, interp = "bicubic")
37   if (nargin < 2)
38     error ("imresize: not enough input arguments");
39   endif
40   
41   [row, col, num_planes, tmp] = size (im);
42   if (tmp != 1 || (num_planes != 1 && num_planes != 3))
43     error ("imresize: the first argument has must be an image");
44   endif
45
46   ## Handle the argument that describes the size of the result
47   if (length (m) == 1)
48     new_row = round (row*m);
49     new_col = round (col*m);
50   elseif (length (m) == 2)
51     new_row = m (1);
52     new_col = m (2);
53     m = min (new_row/row, new_col/col);
54   else
55     error ("imresize: second argument mest be a scalar or a 2-vector");
56   end
57
58   ## Handle the method argument
59   if (!any (strcmpi (interp, {"nearest", "linear", "bilinear", "cubic", "bicubic", "pchip"})))
60     error ("imresize: unsupported interpolation method");
61   endif
62
63   
64   ## Perform the actual resizing
65   [XI, YI] = meshgrid (linspace (1,col, new_col), linspace (1, row, new_row) );
66   ret = imremap (im, XI, YI, interp);
67 endfunction