Mercurial > octave
changeset 20663:a5ed88c9eb14
Only return floating point class on color conversion functions (patch #8709)
* scripts/image/private/colorspace_conversion_revert.m: remove code that
convertes a floating point image back into its previous integer class.
* scripts/image/private/colorspace_conversion_input_check.m: do not return
is_int (no longer needed).
* scripts/image/ntsc2rgb.m, scripts/image/rgb2ntsc.m: do not convert the
image back to the same class as input (for Matlab compatibility).
Also, on purpose Matlab incompatibility, return single if input was of
class single.
* scripts/image/hsv2rgb.m, scripts/image/rgb2hsv.m: do not convert the
the image back to the same class as input (for Matlab compatibility).
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Mon, 26 Oct 2015 02:34:29 +0000 |
parents | 5c3dc2650e4f |
children | a278de349250 |
files | scripts/image/hsv2rgb.m scripts/image/ntsc2rgb.m scripts/image/private/colorspace_conversion_input_check.m scripts/image/private/colorspace_conversion_revert.m scripts/image/rgb2hsv.m scripts/image/rgb2ntsc.m |
diffstat | 6 files changed, 20 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/image/hsv2rgb.m Tue Oct 27 20:31:56 2015 -0700 +++ b/scripts/image/hsv2rgb.m Mon Oct 26 02:34:29 2015 +0000 @@ -63,7 +63,7 @@ print_usage (); endif - [hsv, cls, sz, is_im, is_nd, is_int] ... + [hsv, cls, sz, is_im, is_nd] ... = colorspace_conversion_input_check ("hsv2rgb", "HSV", hsv); h = hsv(:,1); @@ -92,7 +92,7 @@ + (hue >= 1/6 & hue < 1/2) + (hue >= 1/2 & hue < 2/3) .* (4 - 6 * hue)); - rgb = colorspace_conversion_revert (rgb, cls, sz, is_im, is_nd, is_int); + rgb = colorspace_conversion_revert (rgb, cls, sz, is_im, is_nd); endfunction
--- a/scripts/image/ntsc2rgb.m Tue Oct 27 20:31:56 2015 -0700 +++ b/scripts/image/ntsc2rgb.m Mon Oct 26 02:34:29 2015 +0000 @@ -45,7 +45,7 @@ print_usage (); endif - [yiq, cls, sz, is_im, is_nd, is_int] ... + [yiq, cls, sz, is_im, is_nd] ... = colorspace_conversion_input_check ("ntsc2rgb", "YIQ", yiq); ## Conversion matrix constructed from 'inv (rgb2ntsc matrix)'. @@ -57,13 +57,16 @@ 0.95617, -0.27269, -1.10374; 0.62143, -0.64681, 1.70062 ]; rgb = yiq * trans; + ## Note that if the input is of class single, we also return an image + ## of class single. This is Matlab incompatible by design, since + ## Matlab always returning class double, is a Matlab bug (see patch #8709) ## truncating / scaling of double rgb values for Matlab compatibility rgb = max (0, rgb); idx = any (rgb > 1, 2); rgb(idx,:) = rgb(idx,:) ./ max (rgb(idx,:), [], 2); - rgb = colorspace_conversion_revert (rgb, cls, sz, is_im, is_nd, is_int); + rgb = colorspace_conversion_revert (rgb, cls, sz, is_im, is_nd); endfunction %!shared trans
--- a/scripts/image/private/colorspace_conversion_input_check.m Tue Oct 27 20:31:56 2015 -0700 +++ b/scripts/image/private/colorspace_conversion_input_check.m Mon Oct 26 02:34:29 2015 +0000 @@ -21,7 +21,7 @@ ## handle input in the same way. The returned flags are meant to be handled ## by the complementary private function colorspace_conversion_revert() -function [in_arg, cls, sz, is_im, is_nd, is_int] ... +function [in_arg, cls, sz, is_im, is_nd] ... = colorspace_conversion_input_check (func, arg_name, in_arg) cls = class (in_arg); @@ -63,9 +63,6 @@ ## Convert to floating point (remember to leave class single alone) if (isinteger (in_arg)) in_arg = double (in_arg) / double (intmax (cls)); - is_int = true; - else - is_int = false; endif endfunction
--- a/scripts/image/private/colorspace_conversion_revert.m Tue Oct 27 20:31:56 2015 -0700 +++ b/scripts/image/private/colorspace_conversion_revert.m Mon Oct 26 02:34:29 2015 +0000 @@ -22,7 +22,7 @@ ## to come from complementary private function ## colorspace_conversion_input_check() -function rv = colorspace_conversion_revert (rv, cls, sz, is_im, is_nd, is_int) +function rv = colorspace_conversion_revert (rv, cls, sz, is_im, is_nd) if (is_im) if (is_nd) rv = reshape (rv, [sz(1:2) sz(4) sz(3)]); @@ -31,7 +31,4 @@ rv = reshape (rv, sz); endif endif - if (is_int) - rv *= intmax (cls); - endif endfunction
--- a/scripts/image/rgb2hsv.m Tue Oct 27 20:31:56 2015 -0700 +++ b/scripts/image/rgb2hsv.m Mon Oct 26 02:34:29 2015 +0000 @@ -43,7 +43,7 @@ print_usage (); endif - [rgb, cls, sz, is_im, is_nd, is_int] ... + [rgb, cls, sz, is_im, is_nd] ... = colorspace_conversion_input_check ("rgb2hsv", "RGB", rgb); ## get the max and min for each row @@ -78,7 +78,7 @@ s(notgray) = 1 - s(notgray) ./ v(notgray); hsv = [h, s, v]; - hsv = colorspace_conversion_revert (hsv, cls, sz, is_im, is_nd, is_int); + hsv = colorspace_conversion_revert (hsv, cls, sz, is_im, is_nd); endfunction
--- a/scripts/image/rgb2ntsc.m Tue Oct 27 20:31:56 2015 -0700 +++ b/scripts/image/rgb2ntsc.m Mon Oct 26 02:34:29 2015 +0000 @@ -51,7 +51,7 @@ print_usage (); endif - [rgb, cls, sz, is_im, is_nd, is_int] ... + [rgb, cls, sz, is_im, is_nd] ... = colorspace_conversion_input_check ("rgb2ntsc", "RGB", rgb); ## Reference matrix for transformation from http://en.wikipedia.org/wiki/YIQ @@ -61,8 +61,12 @@ 0.587, -0.274, -0.523; 0.114, -0.322, 0.312 ]; yiq = rgb * trans; + ## Note that if the input is of class single, we also return an image + ## of class single. This is Matlab incompatible by design, since + ## Matlab always returning class double, is a Matlab bug (see patch #8709) - yiq = colorspace_conversion_revert (yiq, cls, sz, is_im, is_nd, is_int); + yiq = colorspace_conversion_revert (yiq, cls, sz, is_im, is_nd); + endfunction ## Test pure RED, GREEN, BLUE colors @@ -78,6 +82,9 @@ %! rgb_img = rand (64, 64, 3); %! assert (ntsc2rgb (rgb2ntsc (rgb_img)), rgb_img, 1e-3); +## test tolerance input checking on floats +%! assert (rgb2ntsc ([1.5 1 1]), [1.149 0.298 0.105], 1e-3); + ## Test input validation %!error rgb2ntsc () %!error rgb2ntsc (1,2)