Mercurial > octave
annotate scripts/image/private/colorspace_conversion_input_check.m @ 26376:00f796120a6d stable
maint: Update copyright dates in all source files.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 02 Jan 2019 16:32:43 -0500 |
parents | 6652d3823428 |
children | b442ec6dda5c |
rev | line source |
---|---|
26376
00f796120a6d
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
1 ## Copyright (C) 2016-2019 Carnë Draug <carandraug+dev@gmail.com> |
20369
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 ## |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
5 ## Octave is free software: you can redistribute it and/or modify it |
20369
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 |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
7 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
8 ## (at your option) any later version. |
20369
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 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## GNU General Public License for more details. |
20369
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 |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
17 ## <https://www.gnu.org/licenses/>. |
20369
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 |
20676
1672bb8882dd
Remove unused arguments in private functions for colorspace arguments.
Carnë Draug <carandraug@octave.org>
parents:
20665
diff
changeset
|
24 function [in_arg, 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)) |
20665
67e6343cd29a
Add support for colorspace conversion of signed integers images (patch #8709)
Carnë Draug <carandraug@octave.org>
parents:
20663
diff
changeset
|
32 if (! any (strcmp (cls, {"uint8", "int8", "int16", "uint16", ... |
67e6343cd29a
Add support for colorspace conversion of signed integers images (patch #8709)
Carnë Draug <carandraug@octave.org>
parents:
20663
diff
changeset
|
33 "single", "double"}))) |
20369
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
34 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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 endif |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
40 is_im = true; |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
41 |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
42 ## For floating point values, R, G and B should be in the [0 1] range, |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
20676
diff
changeset
|
43 ## otherwise they don't make any sense. We accept those values |
20369
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
44 ## 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
|
45 ## 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
|
46 ## 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
|
47 |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
48 ## 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
|
49 nd = ndims (in_arg); |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
50 if (nd == 3) |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
51 is_nd = false; |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
52 elseif (nd == 4) |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
53 is_nd = true; |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
54 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
|
55 elseif (nd > 4) |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
56 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
|
57 endif |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
58 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
|
59 else |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
60 is_im = false; |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
61 is_nd = false; |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
62 endif |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
63 |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
64 ## 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
|
65 if (isinteger (in_arg)) |
20665
67e6343cd29a
Add support for colorspace conversion of signed integers images (patch #8709)
Carnë Draug <carandraug@octave.org>
parents:
20663
diff
changeset
|
66 int_max = double (intmax (cls)); |
67e6343cd29a
Add support for colorspace conversion of signed integers images (patch #8709)
Carnë Draug <carandraug@octave.org>
parents:
20663
diff
changeset
|
67 int_min = double (intmin (cls)); |
67e6343cd29a
Add support for colorspace conversion of signed integers images (patch #8709)
Carnë Draug <carandraug@octave.org>
parents:
20663
diff
changeset
|
68 if (int_min < 0) |
67e6343cd29a
Add support for colorspace conversion of signed integers images (patch #8709)
Carnë Draug <carandraug@octave.org>
parents:
20663
diff
changeset
|
69 in_arg = (double (in_arg) - int_min) / (int_max - int_min); |
67e6343cd29a
Add support for colorspace conversion of signed integers images (patch #8709)
Carnë Draug <carandraug@octave.org>
parents:
20663
diff
changeset
|
70 else |
67e6343cd29a
Add support for colorspace conversion of signed integers images (patch #8709)
Carnë Draug <carandraug@octave.org>
parents:
20663
diff
changeset
|
71 in_arg = double (in_arg) / int_max; |
67e6343cd29a
Add support for colorspace conversion of signed integers images (patch #8709)
Carnë Draug <carandraug@octave.org>
parents:
20663
diff
changeset
|
72 endif |
20369
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
73 endif |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
74 |
131ce8cfaa80
Relax input in functions that convert between colorspaces (bug #45456)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
75 endfunction |