comparison scripts/image/private/ind2x.m @ 15689:14b7679891dd

ind2sub ind2gray: merge common code in private function, expanding fix for images when integers, input check, and expansion of colormap to ind2gray.
author Carnë Draug <carandraug+dev@gmail.com>
date Mon, 12 Nov 2012 04:53:42 +0000
parents
children a1b634240352
comparison
equal deleted inserted replaced
15688:4db08f52a6ed 15689:14b7679891dd
1 ## Copyright (C) 1994-2012 John W. Eaton
2 ## Copyright (C) 2012 Carnë Draug
3 ##
4 ## This file is part of Octave.
5 ##
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
10 ##
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
19
20 ## private function for the ind2something functions which have a lot of code
21 ## in common
22
23 function [x, map] = ind2x (name, x, map)
24
25 ## Check if X is an indexed image.
26 if (ndims (x) != 2 || (isfloat (x) && ! isindex (x)) ||
27 ! ismember (class (x), {"double", "single", "uint8", "uint16"}))
28 error ("%s: X must be an indexed image", name);
29 endif
30
31 ## Check the color map.
32 if (! iscolormap (map))
33 error ("%s: MAP must be a valid colormap", name);
34 endif
35
36 ## Do we have enough colors in the color map?
37 ## there's an offset of 1 when the indexed image is an integer class so we fix
38 ## it now and convert it to float only if really necessary and even then only
39 ## to single precision since its enough for both uint8 and uint16
40 maxidx = max (x(:));
41 if (isinteger (x))
42 if (maxidx == intmax (class (x)))
43 x = single (x);
44 endif
45 x += 1;
46 maxidx += 1;
47 endif
48
49 rm = rows (map);
50 if (rm < maxidx)
51 ## Pad with the last color in the map.
52 pad = repmat (map(end,:), maxidx-rm, 1);
53 map(end+1:maxidx, :) = pad;
54 endif
55
56 endfunction