comparison scripts/image/ind2rgb.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 4db08f52a6ed
children a1b634240352
comparison
equal deleted inserted replaced
15688:4db08f52a6ed 15689:14b7679891dd
38 print_usage (); 38 print_usage ();
39 elseif (nargin == 1) 39 elseif (nargin == 1)
40 map = colormap (); 40 map = colormap ();
41 endif 41 endif
42 42
43 ## Check if X is an indexed image. 43 [x, map] = ind2x ("ind2rgb", x, map);
44 if (ndims (x) != 2 || (isfloat (x) && ! isindex (x)) ||
45 ! ismember (class (x), {"double", "single", "uint8", "uint16"}))
46 error ("ind2rgb: X must be an indexed image");
47 endif
48
49 ## Check the color map.
50 if (! iscolormap (map))
51 error ("ind2rgb: MAP must be a valid colormap");
52 endif
53
54 ## Do we have enough colors in the color map?
55 ## there's an offset of 1 when the indexed image is an integer class so we fix
56 ## it now and convert it to float only if really necessary and even then only
57 ## to single precision since its enough for both uint8 and uint16
58 maxidx = max (x(:));
59 if (isinteger (x))
60 if (maxidx == intmax (class (x)))
61 x = single (x);
62 endif
63 x += 1;
64 maxidx += 1;
65 endif
66
67 rm = rows (map);
68 if (rm < maxidx)
69 ## Pad with the last color in the map.
70 pad = repmat (map(end,:), maxidx-rm, 1);
71 map(end+1:maxidx, :) = pad;
72 endif
73 44
74 ## Compute result 45 ## Compute result
75 [hi, wi] = size (x); 46 [hi, wi] = size (x);
76 R = reshape (map (x(:), 1), hi, wi); 47 R = reshape (map (x(:), 1), hi, wi);
77 G = reshape (map (x(:), 2), hi, wi); 48 G = reshape (map (x(:), 2), hi, wi);