Mercurial > octave-nkf
comparison scripts/image/private/ind2x.m @ 18578:875f4510d6dc
Accept indexed images with color values below the first colormap entry (bug #41851).
Change made for Matlab compatibility.
* ind2gray.m: Change %!tests to accept an indexed image with colors below the
first entry in the colormap.
* ind2rgb.m: Change %!tests to accept an indexed image with colors below the
first entry in the colormap.
* ind2x.m: Change input validation to accept all floating point images with integer
indices. Issue a warning if any of the indices are below the first entry in the
colormap. Map all color indices below the first entry to the first color in the
colormap.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 17 Mar 2014 09:10:32 -0700 |
parents | d63878346099 |
children | 4197fc428c7d |
comparison
equal
deleted
inserted
replaced
18577:3a1a4d587c4e | 18578:875f4510d6dc |
---|---|
20 ## private function for the ind2XXX functions which have a lot of code in common | 20 ## private function for the ind2XXX functions which have a lot of code in common |
21 | 21 |
22 function [x, map] = ind2x (caller, x, map) | 22 function [x, map] = ind2x (caller, x, map) |
23 | 23 |
24 ## Check if X is an indexed image. | 24 ## Check if X is an indexed image. |
25 ## an indexed image is defined has having only 2D, and that's how Matlab | 25 ## An indexed image is defined has having only 2D, and that's how Matlab |
26 ## behaves. But we want to support ND images, so we will allow up to 4D | 26 ## behaves. But we want to support ND images, so we will allow up to 4D |
27 ## and check that the 3rd is a singleton | 27 ## and check that the 3rd dimension is a singleton. |
28 if (all (ndims (x) != [2 4]) || size (x, 3) != 1 || issparse (x) || | 28 if (all (ndims (x) != [2 4]) || size (x, 3) != 1 |
29 (isfloat (x) && ! isindex (x)) || | 29 || iscomplex (x) || issparse (x) |
30 ! any (strcmp (class (x), {"uint8", "uint16", "single", "double"}))) | 30 || (isfloat (x) && x != fix (x)) |
31 || ! any (strcmp (class (x), {"uint8", "uint16", "single", "double"}))) | |
31 error ("%s: X must be an indexed image", caller); | 32 error ("%s: X must be an indexed image", caller); |
32 endif | 33 endif |
33 | 34 |
34 ## Check if map is a valid colormap. | 35 ## Check if map is a valid colormap. |
35 if (! iscolormap (map)) | 36 if (! iscolormap (map)) |
36 error ("%s: MAP must be a valid colormap", caller); | 37 error ("%s: MAP must be a valid colormap", caller); |
37 endif | 38 endif |
38 | 39 |
39 ## Do we have enough colors in the color map? | 40 ## Any color indices below the lower bound of the color map are modified |
40 ## there's an offset of 1 when the indexed image is an integer class so we fix | 41 ## to point to the first color in the map (see bug #41851). |
41 ## it now and convert it to float only if really necessary and even then only | 42 if (isfloat (x)) |
42 ## to single precision since that is enough for both uint8 and uint16. | 43 invalid_idx = x < 1; |
44 if (any (invalid_idx(:))) | |
45 warning (["Octave:" caller ":invalid-idx-img"], | |
46 [caller ": indexed image contains colors outside of colormap"]); | |
47 x(invalid_idx) = 1; | |
48 endif | |
49 endif | |
50 | |
51 ## Switch to using 1-based indexing. | |
52 ## It is possible that an integer storage class may not have enough room | |
53 ## to make the switch, in which case we convert the data to single. | |
43 maxidx = max (x(:)); | 54 maxidx = max (x(:)); |
44 if (isinteger (x)) | 55 if (isinteger (x)) |
45 if (maxidx == intmax (class (x))) | 56 if (maxidx == intmax (class (x))) |
46 x = single (x); | 57 x = single (x); |
47 endif | 58 endif |
48 x += 1; | 59 x += 1; |
49 maxidx += 1; | 60 maxidx += 1; |
50 endif | 61 endif |
51 | 62 |
63 ## When there are more colors in the image, than there are in the map, | |
64 ## pad the colormap with the last color in the map for Matlab compatibility. | |
52 num_colors = rows (map); | 65 num_colors = rows (map); |
53 if (num_colors < maxidx) | 66 if (num_colors < maxidx) |
54 ## Pad with the last color in the map for Matlab compatibility | 67 warning (["Octave:" caller ":invalid-idx-img"], |
68 [caller ": indexed image contains colors outside of colormap"]); | |
55 pad = repmat (map(end,:), maxidx - num_colors, 1); | 69 pad = repmat (map(end,:), maxidx - num_colors, 1); |
56 map(end+1:maxidx, :) = pad; | 70 map(end+1:maxidx, :) = pad; |
57 endif | 71 endif |
58 | 72 |
59 endfunction | 73 endfunction |