]> Creatis software - CreaPhase.git/blob - octave_packages/m/image/rgb2ind.m
update packages
[CreaPhase.git] / octave_packages / m / image / rgb2ind.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{x}, @var{map}] =} rgb2ind (@var{rgb})
21 ## @deftypefnx {Function File} {[@var{x}, @var{map}] =} rgb2ind (@var{R}, @var{G}, @var{B})
22 ## Convert an RGB image to an Octave indexed image.
23 ## @seealso{ind2rgb, rgb2ntsc}
24 ## @end deftypefn
25
26 ## Bugs: The color map may have duplicate entries.
27
28 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
29 ## Created: July 1994
30 ## Adapted-By: jwe
31
32 function [x, map] = rgb2ind (R, G, B)
33
34   if (nargin != 1 && nargin != 3)
35     print_usage ();
36   endif
37
38   if (nargin == 1)
39     rgb = R;
40     if (length (size (rgb)) == 3 && size (rgb, 3) == 3)
41       R = rgb(:,:,1);
42       G = rgb(:,:,2);
43       B = rgb(:,:,3);
44     else
45       error ("rgb2ind: argument is not an RGB image");
46     endif
47   endif
48
49   if (! size_equal (R, G) || ! size_equal (R, B))
50     error ("rgb2ind: arguments must all have the same size");
51   endif
52
53   [hi, wi] = size (R);
54
55   x = zeros (hi, wi);
56
57   map = zeros (hi*wi, 3);
58
59   map(:,1) = R(:);
60   map(:,2) = G(:);
61   map(:,3) = B(:);
62
63   x(:) = 1:(hi*wi);
64
65 endfunction