annotate scripts/image/hsv2rgb.m @ 7948:af10baa63915 ss-3-1-50

3.1.50 snapshot
author John W. Eaton <jwe@octave.org>
date Fri, 18 Jul 2008 17:42:48 -0400
parents 4fbfce35012a
children cadc73247d65
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
2 ## Kai Habel
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
3 ##
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
5 ##
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
9 ## your option) any later version.
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
10 ##
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
14 ## General Public License for more details.
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
15 ##
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
18 ## <http://www.gnu.org/licenses/>.
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
19
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6046
diff changeset
21 ## @deftypefn {Function File} {@var{rgb_map} =} hsv2rgb (@var{hsv_map})
7375
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
22 ## Transform a colormap or image from the hsv space to the rgb space.
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5307
diff changeset
23 ## @seealso{rgb2hsv}
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
24 ## @end deftypefn
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
25
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
26 ## Author: Kai Habel <kai.habel@gmx.de>
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
27 ## Adapted-by: jwe
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
28
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
29 function rgb_map = hsv2rgb (hsv_map)
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
30
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
31 ## each color value x=(r,g,b) is calculated with
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
32 ## x = (1-sat)*val+sat*val*f_x(hue)
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
33 ## where f_x(hue) is a piecewise defined function for
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
34 ## each color with f_r(hue-2/3) = f_g(hue) = f_b(hue-1/3)
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
35
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
36 if (nargin != 1)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
37 print_usage ();
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
38 endif
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
39
7375
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
40 ## If we have an image convert it into a color map.
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
41 if (ismatrix (hsv_map) && ndims (hsv_map) == 3)
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
42 is_image = true;
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
43 Sz = size (hsv_map);
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
44 hsv_map = [hsv_map(:,:,1)(:), hsv_map(:,:,2)(:), hsv_map(:,:,3)(:)];
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
45 ## Convert to a double image.
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
46 if (isinteger (hsv_map))
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
47 C = class (hsv_map);
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
48 low = double (intmin (C));
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
49 high = double (intmax (C));
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
50 hsv_map = (double (hsv_map) - low) / (high - low);
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
51 endif
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
52 else
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
53 is_image = false;
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
54 endif
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
55
4030
22bd65326ec1 [project @ 2002-08-09 18:58:13 by jwe]
jwe
parents: 3803
diff changeset
56 if (! ismatrix (hsv_map) || columns (hsv_map) != 3)
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
57 error ("hsv2rgb: argument must be a matrix of size nx3");
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
58 endif
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
59
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
60 ## set values <0 to 0 and >1 to 1
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
61 hsv_map = (hsv_map >= 0 & hsv_map <= 1) .* hsv_map \
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
62 + (hsv_map < 0) .* 0 + (hsv_map > 1);
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
63
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
64 ## fill rgb map with v*(1-s)
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
65 rgb_map = kron ([1, 1, 1], hsv_map(:,3) .* (1 - hsv_map(:,2)));
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
66
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
67 ## red(hue-2/3)=green(hue)=blue(hue-1/3)
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
68 ## apply modulo 1 for red and blue
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
69 t = hsv_map(:,1);
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
70 tp = t';
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
71 hue = [(tp - 2/3 - floor (t - 2/3)');
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
72 tp;
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
73 (tp - 1/3 - floor (t - 1/3)')]';
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
74
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
75 ## factor s*v -> f
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
76 f = kron ([1, 1, 1], hsv_map(:,2)) .* kron ([1, 1, 1], hsv_map(:,3));
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
77
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
78 ## add s*v* hue-function to rgb map
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
79 rgb_map = rgb_map + f .* (6 * (hue < 1/6) .* hue
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
80 + (hue >= 1/6 & hue < 1/2)
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
81 + (hue >= 1/2 & hue < 2/3) .* (4 - 6 * hue));
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
82
7375
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
83 ## If input was an image, convert it back into one.
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
84 if (is_image)
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
85 rgb_map = reshape (rgb_map, Sz);
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
86 endif
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
87
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
88 endfunction