1 ## Copyright (C) 2004 Josep Mones i Teixidor
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {[@var{Y}, @var{newmap}] = } cmpermute (@var{X},@var{map})
18 ## @deftypefnx {Function File} {[@var{Y}, @var{newmap}] = } cmpermute (@var{X},@var{map},@var{index})
19 ## Reorders colors in a colormap.
21 ## @code{[Y,newmap]=cmpermute(X,map)} rearranges colormap @var{map}
22 ## randomly returning colormap @var{newmap} and generates indexed image
23 ## @var{Y} so that it mantains correspondence between indices and the
24 ## colormap from original indexed image @var{X} (both image and colormap
25 ## pairs produce the same result).
27 ## @code{[Y,newmap]=cmpermute(X,map,index)} behaves as described above
28 ## but instead of sorting colors randomly, it uses @var{index} to define
29 ## the order of the colors in the new colormap.
31 ## @strong{Note:} @code{index} shouldn't have repeated elements, this
32 ## function won't explicitly check this, but it will fail if it has.
37 ## Author: Josep Mones i Teixidor <jmones@puntbarra.com>
39 function [Y, newmap] = cmpermute(X, map, index)
42 index=randperm(rows(map));
44 if(!isvector(index) || length(index)!=rows(map))
45 error("cmpermute: invalid parameter index.");
48 usage("[Y, newmap] = cmpermute(X, map [, index])");
54 ## build reverse index
55 rindex = zeros(size(index));
56 rindex(index) = 1:length(index);
60 rindex=uint8(rindex-1);
62 Y=rindex(double(X)+1);
70 %! [Y,newmap]=cmpermute([1:4],hot(4),4:-1:1)
71 %! # colormap will be arranged in reverse order (so will image)
75 %! [X,map]=cmunique(X);
77 %!test # random permutation, 0-based index
78 %! [Y,newmap]=cmpermute(X,map);
79 %! # test we didn't lose colors
80 %! assert(sort(map),sortrows(newmap));
81 %! # test if images are equal
82 %! assert(map(double(X)+1),newmap(double(Y)+1));
84 %!test # reverse map, 0-based index
85 %! [Y,newmap]=cmpermute(X,map,rows(map):-1:1);
86 %! # we expect a reversed colormap
87 %! assert(newmap(rows(newmap):-1:1,:),map);
88 %! # we expect reversed indices in image
89 %! assert(X,max(Y(:))-Y);
93 %! [X,map]=cmunique(X);
95 %!test # random permutation, 1-based index
96 %! [Y,newmap]=cmpermute(X,map);
97 %! # test we didn't lose colors
98 %! assert(sort(map),sortrows(newmap));
99 %! # test if images are equal
100 %! assert(map(X),newmap(Y));
102 %!test # reverse map, 1-based index
103 %! [Y,newmap]=cmpermute(X,map,rows(map):-1:1);
104 %! # we expect a reversed colormap
105 %! assert(newmap(rows(newmap):-1:1,:),map);
106 %! # we expect reversed indices in image
107 %! assert(X,max(Y(:))+1-Y);
111 % Revision 1.3 2007/03/23 16:14:36 adb014
112 % Update the FSF address
114 % Revision 1.2 2007/01/04 23:44:22 hauberg
115 % Minor changes in help text
117 % Revision 1.1 2006/08/20 12:59:32 hauberg
118 % Changed the structure to match the package system
120 % Revision 1.4 2004/09/08 15:01:28 pkienzle
121 % Redo tests: reduce # of shared variables; force full range of uint8
123 % Revision 1.3 2004/09/08 14:13:08 jmones
124 % Synchronized with cmunique. uint8 support added. Tests working for 2.1.58
126 % Revision 1.2 2004/08/18 14:57:42 jmones
127 % speed improvement suggested by Paul Kienzle
129 % Revision 1.1 2004/08/17 19:18:42 jmones
130 % cmpermute added: Reorders colors in a colormap