]> Creatis software - CreaPhase.git/blob - octave_packages/m/image/rgb2ntsc.m
update packages
[CreaPhase.git] / octave_packages / m / image / rgb2ntsc.m
1 ## Copyright (C) 1994-2012 John W. Eaton
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} rgb2ntsc (@var{rgb})
21 ## Transform a colormap or image from RGB to NTSC.
22 ## @seealso{ntsc2rgb}
23 ## @end deftypefn
24
25 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
26 ## Created: July 1994
27 ## Adapted-By: jwe
28
29 function yiq = rgb2ntsc (rgb)
30
31   if (nargin != 1)
32     print_usage ();
33   endif
34
35   ## If we have an image convert it into a color map.
36   if (ismatrix (rgb) && ndims (rgb) == 3)
37     is_image = true;
38     Sz = size (rgb);
39     rgb = [rgb(:,:,1)(:), rgb(:,:,2)(:), rgb(:,:,3)(:)];
40     ## Convert to a double image.
41     if (isinteger (rgb))
42       C = class (rgb);
43       low = double (intmin (C));
44       high = double (intmax (C));
45       rgb = (double (rgb) - low) / (high - low);
46     endif
47   else
48     is_image = false;
49   endif
50
51   if (! ismatrix (rgb) || columns (rgb) != 3)
52     error ("rgb2ntsc: argument must be a matrix of size Nx3 or NxMx3");
53   endif
54
55   ## Convert data
56   trans = [ 0.299,  0.596,  0.211;
57             0.587, -0.274, -0.523;
58             0.114, -0.322,  0.312 ];
59
60   yiq = rgb * trans;
61
62   ## If input was an image, convert it back into one.
63   if (is_image)
64     yiq = reshape (yiq, Sz);
65   endif
66
67 endfunction