1 ## Copyright (C) 1994-2012 John W. Eaton
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {[@var{img}, @var{map}] =} gray2ind (@var{I}, @var{n})
21 ## Convert a gray scale intensity image to an Octave indexed image.
22 ## The indexed image will consist of @var{n} different intensity values. If not
23 ## given @var{n} will default to 64.
26 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
30 function [X, map] = gray2ind (I, n = 64)
32 if (nargin < 1 || nargin > 2)
36 if (! ismatrix (I) || ndims (I) != 2)
37 error ("gray2ind: first input argument must be a gray scale image");
39 if (! isscalar (n) || n < 0)
40 error ("gray2ind: second input argument must be a positive integer");
42 ints = {"uint8", "uint16", "int8", "int16"};
43 floats = {"double", "single"};
44 if (! ismember (C, {ints{:}, floats{:}}))
45 error ("gray2ind: invalid data type '%s'", C);
47 if (ismember (C, floats) && (min (I(:)) < 0 || max (I(:)) > 1))
48 error ("gray2ind: floating point images may only contain values between 0 and 1");
53 ## If @var{I} is an integer matrix convert it to a double matrix with values in [0, 1]
54 if (ismember (C, ints))
55 low = double (intmin (C));
56 high = double (intmax (C));
57 I = (double (I) - low) / (high - low);
59 X = round (I*(n-1)) + 1;