1## Copyright (C) 1994-2017 John W. Eaton <jwe@octave.org> 2## 3## This program is free software: you can redistribute it and/or modify 4## it under the terms of the GNU General Public License as published by 5## the Free Software Foundation, either version 3 of the License, or 6## (at your option) any later version. 7## 8## This program is distributed in the hope that it will be useful, 9## but WITHOUT ANY WARRANTY; without even the implied warranty of 10## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11## GNU General Public License for more details. 12## 13## You should have received a copy of the GNU General Public License 14## along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16## -*- texinfo -*- 17## @deftypefn {} {@var{rgb_map} =} ntsc2rgb (@var{yiq_map}) 18## @deftypefnx {} {@var{rgb_img} =} ntsc2rgb (@var{yiq_img}) 19## Transform a colormap or image from luminance-chrominance (NTSC) space to 20## red-green-blue (RGB) color space. 21## 22## Implementation Note: 23## The conversion matrix is chosen to be the inverse of the matrix used for 24## rgb2ntsc such that 25## 26## @example 27## x == ntsc2rgb (rgb2ntsc (x)) 28## @end example 29## 30## @sc{matlab} uses a slightly different matrix where rounding means the 31## equality above does not hold. 32## @seealso{rgb2ntsc, hsv2rgb, ind2rgb} 33## @end deftypefn 34 35function rgb = ntsc2rgb (yiq) 36 37 if (nargin != 1) 38 print_usage (); 39 endif 40 41 ## Unlike other colorspace conversion functions, we do not accept 42 ## integers as valid input. We check this before 43 ## colorspace_conversion_input_check() which is general and would 44 ## convert integers to double assuming a [0 1] interval range. 45 ## The reason for not supporting integers here is that there's no 46 ## common such conversion. If we were to support a conversion 47 ## the most reasonable definition would be to convert the YIQ 48 ## from their integer range into the ranges: 49 ## Y = [ 0 1.106] 50 ## I = [-0.797 0.587] 51 ## Q = [-0.322 0.426] 52 ## See https://savannah.gnu.org/patch/?8709#comment11 53 if (! isfloat (yiq)) 54 error ("ntsc2rgb: YIQ must be of floating point class"); 55 endif 56 [yiq, cls, sz, is_im, is_nd, is_int] ... 57 = colorspace_conversion_input_check ("ntsc2rgb", "YIQ", yiq, true); 58 59 ## Conversion matrix constructed from 'inv (rgb2ntsc matrix)'. 60 ## See programming notes in rgb2ntsc.m. Note: Matlab matrix for inverse 61 ## is slightly different. We prefer this matrix so that 62 ## x == ntsc2rgb (rgb2ntsc (x)) rather than maintaining strict compatibility 63 ## with Matlab. 64 trans = [ 1.0, 1.0, 1.0; 65 0.95617, -0.27269, -1.10374; 66 0.62143, -0.64681, 1.70062 ]; 67 rgb = yiq * trans; 68 ## Note that if the input is of class single, we also return an image 69 ## of class single. This is Matlab incompatible by design, since 70 ## Matlab always returning class double, is a Matlab bug (see patch #8709) 71 72 ## truncating / scaling of double rgb values for Matlab compatibility 73 rgb = max (0, rgb); 74 idx = any (rgb > 1, 2); 75 rgb(idx,:) = rgb(idx,:) ./ max (rgb(idx,:), [], 2); 76 77 rgb = colorspace_conversion_revert (rgb, cls, sz, is_im, is_nd, is_int, 78 false); 79 80endfunction 81 82 83%!shared trans 84%! trans = [ 1.0, 1.0, 1.0; 85%! 0.95617, -0.27269, -1.10374; 86%! 0.62143, -0.64681, 1.70062 ]; 87 88## Test pure R, G, B colors 89%!assert (ntsc2rgb ([.299 .596 .211]), [1 0 0], 1e-5) 90%!assert (ntsc2rgb ([.587 -.274 -.523]), [0 1 0], 1e-5) 91%!assert (ntsc2rgb ([.114 -.322 .312]), [0 0 1], 1e-5) 92 93%!test 94%! rgb_map = rand (64, 3); 95%! assert (ntsc2rgb (rgb2ntsc (rgb_map)), rgb_map, 1e-3); 96 97%!test 98%! rgb_img = rand (64, 64, 3); 99%! assert (ntsc2rgb (rgb2ntsc (rgb_img)), rgb_img, 1e-3); 100 101## test cropping of rgb output 102%!assert (ntsc2rgb ([1.5 0 0]), [1 1 1]) 103 104## Test scaling of output. After conversion, cut of negative values 105## and scaling of all the others relative to the maximum above 1. 106%!test 107%! ntsc = [0.4229 0.0336 0.7184]; 108%! rgb = ntsc * trans; # [0.9014 -0.0509 1.6075] 109%! rgb(1) /= rgb(3); # scaled based on the maximum 110%! rgb(2) = 0; # cut to 0 111%! rgb(3) = 1; # cut to 1 112%! assert (ntsc2rgb (ntsc), rgb); 113 114## test scaling when conversion has more than one value above 1 115## (check that it does pick the maximum) 116%!test 117%! ntsc = [0.8229 0.3336 0.7184]; 118%! rgb = ntsc * trans; # [1.58831 0.26726 1.67642] 119%! rgb /= rgb(3); 120%! assert (ntsc2rgb (ntsc), rgb); 121 122## check scaling for more than 1 row 123%!test 124%! ntsc = [0.4229 0.0336 0.7184 125%! 0.8229 0.3336 0.7184]; 126%! rgb = ntsc * trans; # [0.9014 -0.0509 1.6075; 1.58831 0.26726 1.67642] 127%! rgb(1,1) /= rgb(1,3); 128%! rgb(1,2) = 0; 129%! rgb(1,3) = 1; 130%! rgb(2,:) /= rgb(2,3); 131%! assert (ntsc2rgb (ntsc), rgb); 132 133## Test input validation 134%!error ntsc2rgb () 135%!error ntsc2rgb (1,2) 136%!error <YIQ must be of floating point class> ntsc2rgb (uint8 (1)) 137%!error <YIQ must be a colormap or YIQ image> ntsc2rgb (ones (2,2)) 138%!error <YIQ must be of floating point class> ntsc2rgb (ones ([10 10 3], "uint8")) 139%!error <YIQ must be of floating point class> ntsc2rgb (ones ([10 10 3], "uint16")) 140%!error <YIQ must be of floating point class> ntsc2rgb (ones ([10 10 3], "int16")) 141 142## Test ND input 143%!test 144%! yiq = rand (16, 16, 3, 5); 145%! rgb = zeros (size (yiq)); 146%! for i = 1:5 147%! rgb(:,:,:,i) = ntsc2rgb (yiq(:,:,:,i)); 148%! endfor 149%! assert (ntsc2rgb (yiq), rgb); 150 151## Test output class and size for input images. 152## Most of the tests only test for colormap input. 153 154%!test 155%! rgb = ntsc2rgb (rand (10, 10, 3)); 156%! assert (class (rgb), "double"); 157%! assert (size (rgb), [10 10 3]); 158 159%!test 160%! rgb = ntsc2rgb (rand (10, 10, 3, "single")); 161%! assert (class (rgb), "single"); 162%! assert (size (rgb), [10 10 3]); 163 164%!test 165%! ntsc = (rand (10, 10, 3) * 3 ) - 0.5; # values outside range [0 1] 166%! rgb = ntsc2rgb (ntsc); 167%! assert (class (rgb), "double"); 168%! assert (size (rgb), [10 10 3]); 169 170%!test 171%! ntsc = (rand (10, 10, 3, "single") * 3 ) - 0.5; # values outside range [0 1] 172%! rgb = ntsc2rgb (ntsc); 173%! assert (class (rgb), "single"); 174%! assert (size (rgb), [10 10 3]); 175 176%!test 177%! ntsc_double = reshape ([.299 .587 .114 0 .596 -.274 -.322 0 .211 -.523 .312 0], 178%! [2 2 3]); 179%! expected = reshape ([1 0 0 0 0 1 0 0 0 0 1 0], [2 2 3]); 180%! 181%! assert (ntsc2rgb (ntsc_double), expected, 1e-5); 182%! assert (ntsc2rgb (single (ntsc_double)), single (expected), 1e-5); 183