Mercurial > octave-antonio
annotate scripts/image/cmpermute.m @ 17170:d6499c14021c
doc: Periodic grammarcheck of documentation.
* doc/interpreter/basics.txi, doc/interpreter/expr.txi,
doc/interpreter/install.txi, doc/interpreter/java.txi,
doc/interpreter/plot.txi, doc/interpreter/sparse.txi,
doc/interpreter/vectorize.txi, libinterp/corefcn/cellfun.cc,
libinterp/corefcn/debug.cc, libinterp/corefcn/file-io.cc,
libinterp/corefcn/load-save.cc, libinterp/corefcn/matrix_type.cc,
libinterp/corefcn/pr-output.cc, libinterp/parse-tree/oct-parse.in.yy,
liboctave/numeric/LSODE-opts.in, scripts/image/imformats.m,
scripts/image/imread.m, scripts/image/imwrite.m,
scripts/miscellaneous/error_ids.m, scripts/miscellaneous/fileattrib.m,
scripts/miscellaneous/mex.m, scripts/miscellaneous/mkoctfile.m,
scripts/miscellaneous/warning_ids.m, scripts/prefs/prefdir.m,
scripts/prefs/preferences.m, scripts/signal/fftfilt.m,
scripts/strings/isstrprop.m, scripts/testfun/assert.m:
Periodic grammarcheck of documentation.
author | Rik <rik@octave.org> |
---|---|
date | Sun, 04 Aug 2013 13:08:39 -0700 |
parents | d1285ebe60ca |
children | bc924baa2c4e |
rev | line source |
---|---|
14896 | 1 ## Copyright (C) 2004 Josep Mones i Teixidor |
2 ## Copyright (C) 2012 Rik Wehbring | |
3 ## | |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
8 ## the Free Software Foundation; either version 3 of the License, or (at | |
9 ## your option) any later version. | |
10 ## | |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
17 ## along with Octave; see the file COPYING. If not, see | |
18 ## <http://www.gnu.org/licenses/>. | |
19 | |
20 ## -*- texinfo -*- | |
21 ## @deftypefn {Function File} {[@var{Y}, @var{newmap}] =} cmpermute (@var{X}, @var{map}) | |
22 ## @deftypefnx {Function File} {[@var{Y}, @var{newmap}] =} cmpermute (@var{X}, @var{map}, @var{index}) | |
23 ## Reorder colors in a colormap. | |
24 ## | |
25 ## When called with only two arguments, @code{cmpermute} randomly rearranges | |
26 ## the colormap @var{map} and returns a new colormap @var{newmap}. It also | |
27 ## returns the indexed image @var{Y} which is the equivalent of the original | |
15718
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
28 ## input image @var{X} when displayed using @var{newmap}. |
14896 | 29 ## |
30 ## When called with an optional third argument the order of colors in the | |
31 ## new colormap is defined by @var{index}. | |
32 ## | |
33 ## @strong{Caution:} @code{index} should not have repeated elements or the | |
34 ## function will fail. | |
35 ## | |
36 ## @end deftypefn | |
37 | |
38 ## Author: Josep Mones i Teixidor <jmones@puntbarra.com> | |
39 | |
40 function [Y, newmap] = cmpermute (X, map, index) | |
41 | |
42 if (nargin < 2 || nargin > 3) | |
43 print_usage (); | |
44 endif | |
45 | |
15718
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
46 cls = class (X); |
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
47 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
|
48 error ("cmpermute: invalid data type '%s'", cls); |
14896 | 49 endif |
50 | |
51 if (! isreal (X) || issparse (X) | |
52 || (isfloat (X) && (any (X(:) < 1 || any (X(:) != fix (X(:))))))) | |
53 error ("cmpermute: X must be an indexed image"); | |
54 endif | |
55 | |
15515
4beb3a4bd440
rgbplot.m, cmpermute.m, cmunique.m, ind2rgb.m, imwrite.m: use core iscolormap function
Carnë Draug <carandraug+dev@gmail.com>
parents:
14896
diff
changeset
|
56 if (! iscolormap (map)) |
14896 | 57 error ("cmpermute: MAP must be a valid colormap"); |
58 endif | |
59 | |
60 if (nargin < 3) | |
61 index = randperm (rows (map)); | |
62 elseif (! isvector (index) || length (index) != rows (map)) | |
63 error ("cmpermute: invalid parameter INDEX"); | |
64 endif | |
65 | |
66 ## new colormap | |
67 newmap = map(index,:); | |
68 | |
69 ## build reverse index | |
70 rindex = zeros (size (index)); | |
71 rindex(index) = 1:length (index); | |
72 | |
15718
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
73 ## preserve class of input image in output |
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
74 if (strcmp (cls, "double")) |
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
75 Y = rindex(X); |
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
76 elseif (strcmp (cls, "single")) |
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
77 rindex = single (rindex); |
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
78 Y = rindex(X); |
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
79 else |
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
80 ## adapt indices |
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
81 rindex = feval (cls, rindex - 1); |
14896 | 82 ## 0-based indices |
15718
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
83 Y = rindex(single (X) + 1); |
14896 | 84 endif |
85 | |
86 endfunction | |
87 | |
88 | |
89 %!demo | |
90 %! [Y, newmap] = cmpermute ([1:4], hot (4), 4:-1:1) | |
91 %! ## colormap will be arranged in reverse order (so will image) | |
92 | |
93 %!shared X, map | |
94 %! X = uint8 (magic (16)); | |
95 %! [X, map] = cmunique (X); | |
96 | |
97 %!test # random permutation, 0-based index | |
98 %! [Y, newmap] = cmpermute (X, map); | |
99 %! ## test we didn't lose colors | |
100 %! assert (sort (map), sortrows (newmap)); | |
101 %! ## test if images are equal | |
102 %! assert (map(double (X)+1), newmap(double (Y)+1)); | |
103 | |
104 %!test # reverse map, 0-based index | |
105 %! [Y, newmap] = cmpermute (X, map, rows (map):-1:1); | |
106 %! ## we expect a reversed colormap | |
107 %! assert (flipud (newmap), map); | |
108 %! ## we expect reversed indices in image | |
109 %! assert (X, max (Y(:)) - Y); | |
110 | |
111 %!shared X,map | |
112 %! X = uint16 (magic (20)); | |
113 %! [X, map] = cmunique (X); | |
114 | |
115 %!test # random permutation, 1-based index | |
116 %! [Y, newmap] = cmpermute (X, map); | |
117 %! ## test we didn't lose colors | |
118 %! assert (sort (map), sortrows (newmap)); | |
119 %! ## test if images are equal | |
120 %! assert (map(X), newmap(Y)); | |
121 | |
122 %!test # reverse map, 1-based index | |
123 %! [Y, newmap] = cmpermute (X, map, rows (map):-1:1); | |
124 %! ## we expect a reversed colormap | |
125 %! assert (newmap (rows (newmap):-1:1,:), map); | |
126 %! ## we expect reversed indices in image | |
127 %! assert (X, max (Y(:)) + 1 - Y); | |
128 | |
129 ## Test input validation | |
130 %!error cmpermute () | |
131 %!error cmpermute (1,2,3,4) | |
15718
d1285ebe60ca
cmpermute.m: Expand to accept indexed image formats uint16 and single.
Rik <rik@octave.org>
parents:
15515
diff
changeset
|
132 %!error <invalid data type 'uint32'> cmpermute (uint32 (magic (16)), jet (256)) |
14896 | 133 %!error <X must be an indexed image> cmpermute (1+i, jet (256)) |
134 %!error <X must be an indexed image> cmpermute (sparse (1), jet (256)) | |
135 %!error <X must be an indexed image> cmpermute (0, jet (256)) | |
136 %!error <X must be an indexed image> cmpermute (1.5, jet (256)) | |
137 %!error <MAP must be a valid colormap> cmpermute (1, "a") | |
138 %!error <MAP must be a valid colormap> cmpermute (1, i) | |
139 %!error <MAP must be a valid colormap> cmpermute (1, ones (3,3,3)) | |
140 %!error <MAP must be a valid colormap> cmpermute (1, ones (3,2)) | |
141 %!error <MAP must be a valid colormap> cmpermute (1, [-1 1 1]) | |
142 %!error <MAP must be a valid colormap> cmpermute (1, [2 1 1]) | |
143 %!error <invalid parameter INDEX> cmpermute (1, [0 1 0;1 0 1], ones (3)) | |
144 %!error <invalid parameter INDEX> cmpermute (1, [0 1 0;1 0 1], 1:3) | |
145 |