Mercurial > jwe > octave
annotate scripts/image/im2double.m @ 21122:dba88797f69f
doc: Periodic grammarcheck of documentation.
author | Rik <rik@octave.org> |
---|---|
date | Wed, 20 Jan 2016 21:14:47 -0800 |
parents | 3ec8332e0e01 |
children | ecce63c99c3f |
rev | line source |
---|---|
20736
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
1 ## Copyright (C) 2007-2015 Søren Hauberg |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
2 ## Copyright (C) 2012-2015 Carnë Draug |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
3 ## |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
4 ## This file is part of Octave. |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
5 ## |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
6 ## Octave is free software; you can redistribute it and/or modify it |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
7 ## under the terms of the GNU General Public License as published by |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
9 ## your option) any later version. |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
10 ## |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
11 ## Octave is distributed in the hope that it will be useful, but |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
14 ## General Public License for more details. |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
15 ## |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
16 ## You should have received a copy of the GNU General Public License |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
17 ## along with Octave; see the file COPYING. If not, see |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
18 ## <http://www.gnu.org/licenses/>. |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
19 |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
20 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20737
diff
changeset
|
21 ## @deftypefn {} {} im2double (@var{img}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20737
diff
changeset
|
22 ## @deftypefnx {} {} im2double (@var{img}, "indexed") |
20736
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
23 ## Convert image to double precision. |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
24 ## |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
25 ## The conversion of @var{img} to double precision, is dependent |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
26 ## on the type of input image. The following input classes are |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
27 ## supported: |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
28 ## |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
29 ## @table @samp |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
30 ## @item uint8, uint16, and int16 |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
31 ## The range of values from the class is scaled to the interval [0 1]. |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
32 ## |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
33 ## @item logical |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
34 ## True and false values are assigned a value of 0 and 1 respectively. |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
35 ## |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
36 ## @item single |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
37 ## Values are cast to double. |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
38 ## |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
39 ## @item double |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
40 ## Returns the same image. |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
41 ## |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
42 ## @end table |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
43 ## |
20737
1a329319975b
im2double: allow floating point indexed images with any value.
Carnë Draug <carandraug@octave.org>
parents:
20736
diff
changeset
|
44 ## If @var{img} is an indexed image, then the second argument should be |
1a329319975b
im2double: allow floating point indexed images with any value.
Carnë Draug <carandraug@octave.org>
parents:
20736
diff
changeset
|
45 ## the string @qcode{"indexed"}. If so, then @var{img} must either be |
21122
dba88797f69f
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
20991
diff
changeset
|
46 ## of floating point class, or unsigned integer class and it will simply |
20737
1a329319975b
im2double: allow floating point indexed images with any value.
Carnë Draug <carandraug@octave.org>
parents:
20736
diff
changeset
|
47 ## be cast to double. If it is an integer class, a +1 offset is applied. |
20736
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
48 ## |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
49 ## @seealso{double} |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
50 ## @end deftypefn |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
51 |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
52 ## Author: Søren Hauberg <soren@hauberg.org> |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
53 ## Author: Carnë Draug <carandraug@octave.org> |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
54 |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
55 function img = im2double (img, im_type) |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
56 ## "normal" (non-indexed) images |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
57 if (nargin () == 1) |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
58 switch (class (img)) |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
59 case "uint8", img = double (img) / 255; |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
60 case "uint16", img = double (img) / 65535; |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
61 case "int16", img = (double (img) + 32768) / 65535; |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
62 case "single", img = double (img); |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
63 case "logical", img = double (img); |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
64 case "double", # do nothing |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
65 otherwise, error ("im2double: IMG is of unsupported class \"%s\"", class (img)); |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
66 endswitch |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
67 |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
68 ## indexed images |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
69 elseif (nargin () == 2) |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
70 if (! strcmpi (im_type, "indexed")) |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
71 error ("im2double: second input argument must be the string \"indexed\""); |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
72 elseif (any (isa (img, {"uint8", "uint16"}))) |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
73 img = double (img) + 1; |
20737
1a329319975b
im2double: allow floating point indexed images with any value.
Carnë Draug <carandraug@octave.org>
parents:
20736
diff
changeset
|
74 elseif (isfloat (img) || isbool (img)) |
20736
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
75 img = double (img); |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
76 else |
20737
1a329319975b
im2double: allow floating point indexed images with any value.
Carnë Draug <carandraug@octave.org>
parents:
20736
diff
changeset
|
77 ## Technically, it could also be of logical class and we do not |
1a329319975b
im2double: allow floating point indexed images with any value.
Carnë Draug <carandraug@octave.org>
parents:
20736
diff
changeset
|
78 ## enforce positive integers for floating for Matlab compatibility. |
1a329319975b
im2double: allow floating point indexed images with any value.
Carnë Draug <carandraug@octave.org>
parents:
20736
diff
changeset
|
79 ## Still, no need to tell that to the user. |
20736
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
80 error (["im2double: if IMG is indexed, then it must be positive " ... |
20737
1a329319975b
im2double: allow floating point indexed images with any value.
Carnë Draug <carandraug@octave.org>
parents:
20736
diff
changeset
|
81 "integer floating points, or unsigned integer class"]); |
20736
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
82 endif |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
83 |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
84 else |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
85 print_usage (); |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
86 endif |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
87 endfunction |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
88 |
20991
3ec8332e0e01
maint: Use two newlines between endfunction and start of BIST tests.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
89 |
20736
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
90 %!assert (im2double ([1 2 3]), [1 2 3]) |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
91 %!assert (im2double (single ([1 2 3])), [1 2 3]) |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
92 %!assert (im2double (uint8 ([0 127 128 255])), [0 127/255 128/255 1]) |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
93 %!assert (im2double (uint16 ([0 127 128 65535])), [0 127/65535 128/65535 1]) |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
94 %!assert (im2double (int16 ([-32768 -32767 -32766 32767])), |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
95 %! [0 1/65535 2/65535 1]) |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
96 |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
97 %!assert (im2double (uint8 ([0 1 255]), "indexed"), [1 2 256]) |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
98 %!assert (im2double (uint16 ([0 1 2557]), "indexed"), [1 2 2558]) |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
99 %!assert (im2double ([3 25], "indexed"), [3 25]) |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
100 %!assert (im2double (single ([3 25]), "indexed"), [3 25]) |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
101 |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
102 ## Test for ND input |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
103 %!function test_im2double_nd (cls, low, high) |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
104 %! in = rand (2, 4, 2, 3, 2); |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
105 %! in *= high - low; |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
106 %! in += low; |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
107 %! in = cast (in, cls); |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
108 %! out = zeros (size (in)); |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
109 %! for n = 1:12 |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
110 %! out(:,:,n) = im2double (in(:,:,n)); |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
111 %! endfor |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
112 %! assert (im2double (in), out) |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
113 %!endfunction |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
114 |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
115 %!test |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
116 %! test_im2double_nd ("double", 0, 1); |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
117 %! test_im2double_nd ("single", 0, 1); |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
118 %! test_im2double_nd ("uint8", 0, 255); |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
119 %! test_im2double_nd ("uint16", 0, 6535); |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
120 %! test_im2double_nd ("int16", -32768, 32767); |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
121 |
20737
1a329319975b
im2double: allow floating point indexed images with any value.
Carnë Draug <carandraug@octave.org>
parents:
20736
diff
changeset
|
122 ## Test lack of input check for Matlab compatibility |
1a329319975b
im2double: allow floating point indexed images with any value.
Carnë Draug <carandraug@octave.org>
parents:
20736
diff
changeset
|
123 %!assert (im2double ([0 1 2], "indexed"), [0 1 2]) |
1a329319975b
im2double: allow floating point indexed images with any value.
Carnë Draug <carandraug@octave.org>
parents:
20736
diff
changeset
|
124 %!assert (im2double ([0 -1 -2], "indexed"), [0 -1 -2]) |
1a329319975b
im2double: allow floating point indexed images with any value.
Carnë Draug <carandraug@octave.org>
parents:
20736
diff
changeset
|
125 %!assert (im2double ([0 -1.5 -2], "indexed"), [0 -1.5 -2]) |
1a329319975b
im2double: allow floating point indexed images with any value.
Carnë Draug <carandraug@octave.org>
parents:
20736
diff
changeset
|
126 %!assert (im2double ([0 -1.5 -2i], "indexed"), [0 -1.5 -2i]) |
1a329319975b
im2double: allow floating point indexed images with any value.
Carnë Draug <carandraug@octave.org>
parents:
20736
diff
changeset
|
127 %!assert (im2double ([false true], "indexed"), [0 1]) |
1a329319975b
im2double: allow floating point indexed images with any value.
Carnë Draug <carandraug@octave.org>
parents:
20736
diff
changeset
|
128 |
20736
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
129 %!error <unsigned integer class> im2double (int16 ([17 8]), "indexed"); |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
130 %!error <unsigned integer class> im2double (int16 ([-7 8]), "indexed"); |
842765b159ed
New function im2double ported from the image package.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
131 %!error <must be the string "indexed"> im2double ([1 2 3], "non-indexed"); |