2847
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
2313
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
2313
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
1024
|
18 |
3381
|
19 ## -*- texinfo -*- |
6247
|
20 ## @deftypefn {Function File} {@var{rgb} =} ind2rgb (@var{x}, @var{map}) |
|
21 ## @deftypefnx {Function File} {[@var{r}, @var{g}, @var{b}] =} ind2rgb (@var{x}, @var{map}) |
2311
|
22 ## Convert an indexed image to red, green, and blue color components. |
6247
|
23 ## If the colormap doesn't contain enough colors, pad it with the |
|
24 ## last color in the map. |
3373
|
25 ## If @var{map} is omitted, the current colormap is used for the conversion. |
5642
|
26 ## @seealso{rgb2ind, image, imshow, ind2gray, gray2ind} |
3373
|
27 ## @end deftypefn |
904
|
28 |
3202
|
29 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
2312
|
30 ## Created: July 1994 |
|
31 ## Adapted-By: jwe |
559
|
32 |
2312
|
33 function [R, G, B] = ind2rgb (X, map) |
1024
|
34 |
6247
|
35 ## Do we have the right number of inputs? |
1024
|
36 if (nargin < 1 || nargin > 2) |
6046
|
37 print_usage (); |
1024
|
38 elseif (nargin == 1) |
|
39 map = colormap (); |
559
|
40 endif |
|
41 |
6247
|
42 ## Check if X is an indexed image. |
|
43 if (ndims (X) != 2 || any (X(:) != round (X(:))) || min (X(:)) < 1) |
|
44 error ("ind2rgb: first input argument must be an indexed image"); |
|
45 endif |
|
46 |
|
47 ## Check the color map. |
|
48 if (ndims (map) != 2 || columns (map) != 3) |
|
49 error ("ind2rgb: second input argument must be a color map"); |
|
50 endif |
1024
|
51 |
6247
|
52 ## Do we have enough colors in the color map? |
|
53 maxidx = max (X(:)); |
|
54 rm = rows (map); |
|
55 if (rm < maxidx) |
|
56 ## Pad with the last color in the map. |
|
57 pad = repmat (map(end,:), maxidx-rm, 1); |
|
58 map(end+1:maxidx, :) = pad; |
|
59 endif |
|
60 |
|
61 ## Compute result |
|
62 [hi, wi] = size (X); |
3486
|
63 R = reshape (map (X(:), 1), hi, wi); |
|
64 G = reshape (map (X(:), 2), hi, wi); |
|
65 B = reshape (map (X(:), 3), hi, wi); |
559
|
66 |
6247
|
67 ## Use 3D array if only one output is requested. |
|
68 if (nargout <= 1) |
|
69 R(:,:,3) = B; |
|
70 R(:,:,2) = G; |
|
71 endif |
559
|
72 endfunction |