]> Creatis software - CreaPhase.git/blob - octave_packages/m/image/gray2ind.m
update packages
[CreaPhase.git] / octave_packages / m / image / gray2ind.m
1 ## Copyright (C) 1994-2012 John W. Eaton
2 ##
3 ## This file is part of Octave.
4 ##
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.
9 ##
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.
14 ##
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/>.
18
19 ## -*- texinfo -*-
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.
24 ## @end deftypefn
25
26 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
27 ## Created: July 1994
28 ## Adapted-By: jwe
29
30 function [X, map] = gray2ind (I, n = 64)
31   ## Check input
32   if (nargin < 1 || nargin > 2)
33     print_usage ();
34   endif
35   C = class(I);
36   if (! ismatrix (I) || ndims (I) != 2)
37     error ("gray2ind: first input argument must be a gray scale image");
38   endif
39   if (! isscalar (n) || n < 0)
40     error ("gray2ind: second input argument must be a positive integer");
41   endif
42   ints = {"uint8", "uint16", "int8", "int16"};
43   floats = {"double", "single"};
44   if (! ismember (C, {ints{:}, floats{:}}))
45     error ("gray2ind: invalid data type '%s'", C);
46   endif
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");
49   endif
50
51   ## Convert data
52   map = gray (n);
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);
58   endif
59   X = round (I*(n-1)) + 1;
60
61 endfunction