Mercurial > octave
annotate scripts/image/private/colorspace_conversion_input_check.m @ 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 | b4ceb06009e0 |
children | 67e6343cd29a |
rev | line source |
---|---|
20369
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
1 ## Copyright (C) 2015 Carnë Draug <carandraug+dev@gmail.com> |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
2 ## |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
3 ## This file is part of Octave. |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
4 ## |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
5 ## Octave is free software; you can redistribute it and/or modify it |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
6 ## under the terms of the GNU General Public License as published by |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
8 ## your option) any later version. |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
9 ## |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
10 ## Octave is distributed in the hope that it will be useful, but |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
13 ## General Public License for more details. |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
14 ## |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
15 ## You should have received a copy of the GNU General Public License |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
16 ## along with Octave; see the file COPYING. If not, see |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
17 ## <http://www.gnu.org/licenses/>. |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
18 |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
19 ## Private function the functions that convert between color spaces, i.e., |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
20 ## rgb2ntsc, rgb2hsv, hsv2rgb, and ntsc2rgb. All of these functions need to |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
21 ## handle input in the same way. The returned flags are meant to be handled |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
22 ## by the complementary private function colorspace_conversion_revert() |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
23 |
20663
a5ed88c9eb14
Only return floating point class on color conversion functions (patch #8709)
Carnë Draug <carandraug@octave.org>
parents:
20654
diff
changeset
|
24 function [in_arg, cls, sz, is_im, is_nd] ... |
20369
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
25 = colorspace_conversion_input_check (func, arg_name, in_arg) |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
26 |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
27 cls = class (in_arg); |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
28 sz = size (in_arg); |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
29 |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
30 ## If we have an image convert it into a color map. |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
31 if (! iscolormap (in_arg)) |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
32 if (! any (strcmp (cls, {"uint8", "uint16", "single", "double"}))) |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
33 error ("%s: %s of invalid data type '%s'", func, arg_name, cls); |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
34 elseif (size (in_arg, 3) != 3) |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
35 error ("%s: %s must be a colormap or %s image", func, arg_name, arg_name); |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
36 elseif (! isreal (in_arg) || ! isnumeric (in_arg)) |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
37 error ("%s: %s must be numeric and real", func, arg_name); |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
38 endif |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
39 is_im = true; |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
40 |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
41 ## For floating point values, R, G and B should be in the [0 1] range, |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
42 ## otherwise they don't make any sense. We accept those values |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
43 ## anyways because we must return something for Matlab compatibility. |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
44 ## User case is when a function returns an RGB image just slightly outside |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
45 ## the range due to floating point rounding errors. |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
46 |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
47 ## Allow for ND images, i.e., multiple images on the 4th dimension. |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
48 nd = ndims (in_arg); |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
49 if (nd == 3) |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
50 is_nd = false; |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
51 elseif (nd == 4) |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
52 is_nd = true; |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
53 in_arg = permute (in_arg, [1 2 4 3]); |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
54 elseif (nd > 4) |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
55 error ("%s: invalid %s with more than 4 dimensions", func, arg_name); |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
56 endif |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
57 in_arg = reshape (in_arg, [numel(in_arg)/3 3]); |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
58 else |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
59 is_im = false; |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
60 is_nd = false; |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
61 endif |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
62 |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
63 ## Convert to floating point (remember to leave class single alone) |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
64 if (isinteger (in_arg)) |
20654
b4ceb06009e0
Fix regression on color conversion by 84ca63c8a038 (patch #8709)
Hartmut Gimpel <hg_code@gmx.de>
parents:
20369
diff
changeset
|
65 in_arg = double (in_arg) / double (intmax (cls)); |
20369
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
66 endif |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
67 |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
68 endfunction |