comparison scripts/image/hsv2rgb.m @ 20404:131ce8cfaa80

Relax input in functions that convert between colorspaces (bug #45456) * scripts/image/hsv2rgb.m, scripts/image/ntsc2rgb.m, scripts/image/rgb2hsv.m, scripts/image/rgb2ntsc.m: remove all input check and leave it up to new private functions handled by all. This adds support for ND images, drops check for values in the [0 1] range, allows integer images, and allows sparse matrices. Also adjust tests and add extra ones. * scripts/image/private/colorspace_conversion_input_check.m, scripts/image/private/colorspace_conversion_revert.m: all of this functions handle input check in the same way. In the same way, they need to prepare the output in the same way based on what preparation was done during input check (transforming image into colormap). So we create two new private functions to avoid repeated code. All code was adapted from hsv2rgb.
author Carnë Draug <carandraug@octave.org>
date Sun, 19 Jul 2015 17:41:21 +0100
parents 84ca63c8a038
children
comparison
equal deleted inserted replaced
20403:2f9119bb3fe5 20404:131ce8cfaa80
61 61
62 if (nargin != 1) 62 if (nargin != 1)
63 print_usage (); 63 print_usage ();
64 endif 64 endif
65 65
66 cls = class (hsv); 66 [hsv, cls, sz, is_im, is_nd, is_int] ...
67 ## If we have an image convert it into a color map. 67 = colorspace_conversion_input_check ("hsv2rgb", "HSV", hsv);
68 if (! iscolormap (hsv))
69 if (! any (strcmp (cls, {"uint8", "uint16", "single", "double"})))
70 error ("hsv2rgb: HSV of invalid data type '%s'", cls);
71 elseif (size (hsv, 3) != 3)
72 error ("hsv2rgb: HSV must be a colormap or HSV image");
73 elseif (! isreal (hsv) || ! isnumeric (hsv))
74 error ("hsv2rgb: HSV must be numeric and real");
75 endif
76 is_image = true;
77
78 ## Allow for ND images, i.e., multiple images on the 4th dimension.
79 sz = size (hsv);
80 nd = ndims (hsv);
81 if (nd == 3)
82 is_ndimage = false;
83 elseif (nd == 4)
84 is_ndimage = true;
85 hsv = permute (hsv, [1 2 4 3]);
86 elseif (nd > 4)
87 error ("hsv2rgb: invalid HSV with more than 4 dimensions");
88 endif
89 hsv = reshape (hsv, [numel(hsv)/3 3]);
90 else
91 is_image = false;
92 is_ndimage = false;
93 endif
94
95 ## Convert to floating point (remember to leave class single alone)
96 if (isinteger (hsv))
97 hsv = double (hsv) / double (intmin (cls));
98 is_uint = true;
99 else
100 is_uint = false;
101 endif
102 68
103 h = hsv(:,1); 69 h = hsv(:,1);
104 s = hsv(:,2); 70 s = hsv(:,2);
105 v = hsv(:,3); 71 v = hsv(:,3);
106 72
124 ## add s*v*hue-function to rgb map 90 ## add s*v*hue-function to rgb map
125 rgb += f .* (6 * (hue < 1/6) .* hue 91 rgb += f .* (6 * (hue < 1/6) .* hue
126 + (hue >= 1/6 & hue < 1/2) 92 + (hue >= 1/6 & hue < 1/2)
127 + (hue >= 1/2 & hue < 2/3) .* (4 - 6 * hue)); 93 + (hue >= 1/2 & hue < 2/3) .* (4 - 6 * hue));
128 94
129 if (is_image) 95 rgb = colorspace_conversion_revert (rgb, cls, sz, is_im, is_nd, is_int);
130 if (is_ndimage)
131 rgb = reshape (rgb, [sz(1:2) sz(4) sz(3)]);
132 rgb = permute (rgb, [1 2 4 3]);
133 else
134 rgb = reshape (rgb, sz);
135 endif
136 endif
137
138 if (is_uint)
139 rgb *= intmax (cls);
140 endif
141 96
142 endfunction 97 endfunction
143 98
144 ## Test pure colors 99 ## Test pure colors
145 %!assert (hsv2rgb ([0 0 1]), [1 1 1]) 100 %!assert (hsv2rgb ([0 0 1]), [1 1 1])
177 ## Test input validation 132 ## Test input validation
178 %!error hsv2rgb () 133 %!error hsv2rgb ()
179 %!error hsv2rgb (1,2) 134 %!error hsv2rgb (1,2)
180 %!error <invalid data type> hsv2rgb ({1}) 135 %!error <invalid data type> hsv2rgb ({1})
181 %!error <HSV must be a colormap or HSV image> hsv2rgb (ones (2,2)) 136 %!error <HSV must be a colormap or HSV image> hsv2rgb (ones (2,2))
137
138 ## Test ND input
139 %!test
140 %! hsv = rand (16, 16, 3, 5);
141 %! rgb = zeros (size (hsv));
142 %! for i = 1:5
143 %! rgb(:,:,:,i) = hsv2rgb (hsv(:,:,:,i));
144 %! endfor
145 %! assert (hsv2rgb (hsv), rgb)