Mercurial > octave-nkf
annotate scripts/image/cmpermute.m @ 15515:4beb3a4bd440
rgbplot.m, cmpermute.m, cmunique.m, ind2rgb.m, imwrite.m: use core iscolormap function
author | Carnë Draug <carandraug+dev@gmail.com> |
---|---|
date | Fri, 12 Oct 2012 23:11:08 +0200 |
parents | 0ba7be7fed1c |
children | d1285ebe60ca |
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 | |
28 ## input image @var{X} when displayed using @var{newmap}. The input image | |
29 ## @var{X} must be an indexed image of class uint8 or double. | |
30 ## | |
31 ## When called with an optional third argument the order of colors in the | |
32 ## new colormap is defined by @var{index}. | |
33 ## | |
34 ## @strong{Caution:} @code{index} should not have repeated elements or the | |
35 ## function will fail. | |
36 ## | |
37 ## @end deftypefn | |
38 | |
39 ## Author: Josep Mones i Teixidor <jmones@puntbarra.com> | |
40 | |
41 function [Y, newmap] = cmpermute (X, map, index) | |
42 | |
43 if (nargin < 2 || nargin > 3) | |
44 print_usage (); | |
45 endif | |
46 | |
47 ## FIXME: Matlab only accepts 2 types. Expand to uint16 & single?? | |
48 if (! (isa (X, "uint8") || isa (X, "double"))) | |
49 error ("cmpermute: X must be of class uint8 or double"); | |
50 endif | |
51 | |
52 if (! isreal (X) || issparse (X) | |
53 || (isfloat (X) && (any (X(:) < 1 || any (X(:) != fix (X(:))))))) | |
54 error ("cmpermute: X must be an indexed image"); | |
55 endif | |
56 | |
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
|
57 if (! iscolormap (map)) |
14896 | 58 error ("cmpermute: MAP must be a valid colormap"); |
59 endif | |
60 | |
61 if (nargin < 3) | |
62 index = randperm (rows (map)); | |
63 elseif (! isvector (index) || length (index) != rows (map)) | |
64 error ("cmpermute: invalid parameter INDEX"); | |
65 endif | |
66 | |
67 ## new colormap | |
68 newmap = map(index,:); | |
69 | |
70 ## build reverse index | |
71 rindex = zeros (size (index)); | |
72 rindex(index) = 1:length (index); | |
73 | |
74 ## adapt indices | |
75 if (isa (X, "uint8")) | |
76 rindex = uint8 (rindex-1); | |
77 ## 0-based indices | |
78 Y = rindex(double (X) + 1); | |
79 else | |
80 Y = rindex(X); | |
81 endif | |
82 | |
83 endfunction | |
84 | |
85 | |
86 %!demo | |
87 %! [Y, newmap] = cmpermute ([1:4], hot (4), 4:-1:1) | |
88 %! ## colormap will be arranged in reverse order (so will image) | |
89 | |
90 %!shared X, map | |
91 %! X = uint8 (magic (16)); | |
92 %! [X, map] = cmunique (X); | |
93 | |
94 %!test # random permutation, 0-based index | |
95 %! [Y, newmap] = cmpermute (X, map); | |
96 %! ## test we didn't lose colors | |
97 %! assert (sort (map), sortrows (newmap)); | |
98 %! ## test if images are equal | |
99 %! assert (map(double (X)+1), newmap(double (Y)+1)); | |
100 | |
101 %!test # reverse map, 0-based index | |
102 %! [Y, newmap] = cmpermute (X, map, rows (map):-1:1); | |
103 %! ## we expect a reversed colormap | |
104 %! assert (flipud (newmap), map); | |
105 %! ## we expect reversed indices in image | |
106 %! assert (X, max (Y(:)) - Y); | |
107 | |
108 %!shared X,map | |
109 %! X = uint16 (magic (20)); | |
110 %! [X, map] = cmunique (X); | |
111 | |
112 %!test # random permutation, 1-based index | |
113 %! [Y, newmap] = cmpermute (X, map); | |
114 %! ## test we didn't lose colors | |
115 %! assert (sort (map), sortrows (newmap)); | |
116 %! ## test if images are equal | |
117 %! assert (map(X), newmap(Y)); | |
118 | |
119 %!test # reverse map, 1-based index | |
120 %! [Y, newmap] = cmpermute (X, map, rows (map):-1:1); | |
121 %! ## we expect a reversed colormap | |
122 %! assert (newmap (rows (newmap):-1:1,:), map); | |
123 %! ## we expect reversed indices in image | |
124 %! assert (X, max (Y(:)) + 1 - Y); | |
125 | |
126 ## Test input validation | |
127 %!error cmpermute () | |
128 %!error cmpermute (1,2,3,4) | |
129 %!error <X must be of class uint8> cmpermute (uint16 (magic (16)), jet (256)) | |
130 %!error <X must be an indexed image> cmpermute (1+i, jet (256)) | |
131 %!error <X must be an indexed image> cmpermute (sparse (1), jet (256)) | |
132 %!error <X must be an indexed image> cmpermute (0, jet (256)) | |
133 %!error <X must be an indexed image> cmpermute (1.5, jet (256)) | |
134 %!error <MAP must be a valid colormap> cmpermute (1, "a") | |
135 %!error <MAP must be a valid colormap> cmpermute (1, i) | |
136 %!error <MAP must be a valid colormap> cmpermute (1, ones (3,3,3)) | |
137 %!error <MAP must be a valid colormap> cmpermute (1, ones (3,2)) | |
138 %!error <MAP must be a valid colormap> cmpermute (1, [-1 1 1]) | |
139 %!error <MAP must be a valid colormap> cmpermute (1, [2 1 1]) | |
140 %!error <invalid parameter INDEX> cmpermute (1, [0 1 0;1 0 1], ones (3)) | |
141 %!error <invalid parameter INDEX> cmpermute (1, [0 1 0;1 0 1], 1:3) | |
142 |