Mercurial > octave
annotate scripts/image/cmpermute.m @ 31218:a33a9c87220b stable
imformats.m: Fix "isa" function in return value (bug #62974)
* imformats.m (isa_magick): Fix typo "Format" to "format" for name of field in
struct returned from __magick_ping__.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 30 Aug 2022 15:24:29 -0700 |
parents | 796f54d4ddbf |
children | 597f3ee61a48 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
3 ## Copyright (C) 2004-2022 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
14896 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23084
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
14896 | 11 ## 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:
23084
diff
changeset
|
12 ## 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
|
13 ## (at your option) any later version. |
14896 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
14896 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
21 ## 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:
23084
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
14896 | 25 |
26 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20357
diff
changeset
|
27 ## @deftypefn {} {[@var{Y}, @var{newmap}] =} cmpermute (@var{X}, @var{map}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20357
diff
changeset
|
28 ## @deftypefnx {} {[@var{Y}, @var{newmap}] =} cmpermute (@var{X}, @var{map}, @var{index}) |
14896 | 29 ## Reorder colors in a colormap. |
30 ## | |
31 ## When called with only two arguments, @code{cmpermute} randomly rearranges | |
32 ## the colormap @var{map} and returns a new colormap @var{newmap}. It also | |
33 ## returns the indexed image @var{Y} which is the equivalent of the original | |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
15718
diff
changeset
|
34 ## input image @var{X} when displayed using @var{newmap}. |
14896 | 35 ## |
20158
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
36 ## When called with an optional third argument the order of colors in the new |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
37 ## colormap is defined by @var{index}. |
14896 | 38 ## |
25472
9771111f04f4
doc: Use @var rather than @code to mark inputs to functions in docstrings.
Rik <rik@octave.org>
parents:
25166
diff
changeset
|
39 ## @strong{Caution:} @var{index} should not have repeated elements or the |
14896 | 40 ## function will fail. |
41 ## | |
42 ## @end deftypefn | |
43 | |
44 function [Y, newmap] = cmpermute (X, map, index) | |
45 | |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
46 if (nargin < 2) |
14896 | 47 print_usage (); |
48 endif | |
49 | |
15718
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
50 cls = class (X); |
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
51 if (! any (strcmp (cls, {"uint8", "uint16", "single", "double"}))) |
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
52 error ("cmpermute: invalid data type '%s'", cls); |
14896 | 53 endif |
54 | |
55 if (! isreal (X) || issparse (X) | |
56 || (isfloat (X) && (any (X(:) < 1 || any (X(:) != fix (X(:))))))) | |
57 error ("cmpermute: X must be an indexed image"); | |
58 endif | |
59 | |
20357
a3bf503652b2
iscolormap: relax input check - specially in [0 1] range.
Carnë Draug <carandraug@octave.org>
parents:
20158
diff
changeset
|
60 if (! iscolormap (map) || min (map(:)) < 0 || max (map(:)) > 1) |
14896 | 61 error ("cmpermute: MAP must be a valid colormap"); |
62 endif | |
63 | |
64 if (nargin < 3) | |
65 index = randperm (rows (map)); | |
66 elseif (! isvector (index) || length (index) != rows (map)) | |
67 error ("cmpermute: invalid parameter INDEX"); | |
68 endif | |
69 | |
70 ## new colormap | |
71 newmap = map(index,:); | |
72 | |
73 ## build reverse index | |
74 rindex = zeros (size (index)); | |
75 rindex(index) = 1:length (index); | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
76 |
15718
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
77 ## preserve class of input image in output |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
78 if (strcmp (cls, "double")) |
15718
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
79 Y = rindex(X); |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
80 elseif (strcmp (cls, "single")) |
15718
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
81 rindex = single (rindex); |
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
82 Y = rindex(X); |
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
83 else |
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
84 ## adapt indices |
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
85 rindex = feval (cls, rindex - 1); |
14896 | 86 ## 0-based indices |
15718
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
87 Y = rindex(single (X) + 1); |
14896 | 88 endif |
89 | |
90 endfunction | |
91 | |
92 | |
93 %!demo | |
94 %! [Y, newmap] = cmpermute ([1:4], hot (4), 4:-1:1) | |
95 %! ## colormap will be arranged in reverse order (so will image) | |
96 | |
97 %!shared X, map | |
98 %! X = uint8 (magic (16)); | |
99 %! [X, map] = cmunique (X); | |
100 | |
101 %!test # random permutation, 0-based index | |
102 %! [Y, newmap] = cmpermute (X, map); | |
103 %! ## test we didn't lose colors | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
104 %! assert (sort (map), sortrows (newmap)); |
14896 | 105 %! ## test if images are equal |
106 %! assert (map(double (X)+1), newmap(double (Y)+1)); | |
107 | |
108 %!test # reverse map, 0-based index | |
109 %! [Y, newmap] = cmpermute (X, map, rows (map):-1:1); | |
110 %! ## we expect a reversed colormap | |
111 %! assert (flipud (newmap), map); | |
112 %! ## we expect reversed indices in image | |
113 %! assert (X, max (Y(:)) - Y); | |
114 | |
115 %!shared X,map | |
116 %! X = uint16 (magic (20)); | |
117 %! [X, map] = cmunique (X); | |
118 | |
119 %!test # random permutation, 1-based index | |
120 %! [Y, newmap] = cmpermute (X, map); | |
121 %! ## test we didn't lose colors | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
122 %! assert (sort (map), sortrows (newmap)); |
14896 | 123 %! ## test if images are equal |
124 %! assert (map(X), newmap(Y)); | |
125 | |
126 %!test # reverse map, 1-based index | |
127 %! [Y, newmap] = cmpermute (X, map, rows (map):-1:1); | |
128 %! ## we expect a reversed colormap | |
129 %! assert (newmap (rows (newmap):-1:1,:), map); | |
130 %! ## we expect reversed indices in image | |
131 %! assert (X, max (Y(:)) + 1 - Y); | |
132 | |
133 ## Test input validation | |
28896
90fea9cc9caa
test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
134 %!error <Invalid call> cmpermute () |
90fea9cc9caa
test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
135 %!error <Invalid call> cmpermute (1) |
15718
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
136 %!error <invalid data type 'uint32'> cmpermute (uint32 (magic (16)), jet (256)) |
14896 | 137 %!error <X must be an indexed image> cmpermute (1+i, jet (256)) |
138 %!error <X must be an indexed image> cmpermute (sparse (1), jet (256)) | |
139 %!error <X must be an indexed image> cmpermute (0, jet (256)) | |
140 %!error <X must be an indexed image> cmpermute (1.5, jet (256)) | |
141 %!error <MAP must be a valid colormap> cmpermute (1, "a") | |
142 %!error <MAP must be a valid colormap> cmpermute (1, i) | |
143 %!error <MAP must be a valid colormap> cmpermute (1, ones (3,3,3)) | |
144 %!error <MAP must be a valid colormap> cmpermute (1, ones (3,2)) | |
145 %!error <MAP must be a valid colormap> cmpermute (1, [-1 1 1]) | |
146 %!error <MAP must be a valid colormap> cmpermute (1, [2 1 1]) | |
147 %!error <invalid parameter INDEX> cmpermute (1, [0 1 0;1 0 1], ones (3)) | |
148 %!error <invalid parameter INDEX> cmpermute (1, [0 1 0;1 0 1], 1:3) |