]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/cmpermute.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / cmpermute.m
1 ## Copyright (C) 2004 Josep Mones i Teixidor
2 ##
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.
7 ##
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.
12 ##
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/>.
15
16 ## -*- texinfo -*-
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.
20 ##
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).
26 ##
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.
30 ##
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.
33 ##
34 ## @end deftypefn
35
36
37 ## Author:  Josep Mones i Teixidor <jmones@puntbarra.com>
38
39 function [Y, newmap] = cmpermute(X, map, index)
40   switch(nargin)
41     case(2)
42       index=randperm(rows(map));
43     case(3)
44       if(!isvector(index) || length(index)!=rows(map))
45         error("cmpermute: invalid parameter index.");
46       endif
47     otherwise
48       usage("[Y, newmap] = cmpermute(X, map [, index])");
49   endswitch
50
51   ## new colormap
52   newmap=map(index,:);
53
54   ## build reverse index
55   rindex = zeros(size(index));
56   rindex(index) = 1:length(index);
57  
58   ## readapt indices
59   if(isa(X,"uint8"))
60     rindex=uint8(rindex-1);
61     ## 0-based indices
62     Y=rindex(double(X)+1);
63   else
64     Y=rindex(X);
65   endif
66 endfunction
67
68
69 %!demo
70 %! [Y,newmap]=cmpermute([1:4],hot(4),4:-1:1)
71 %! # colormap will be arranged in reverse order (so will image)
72
73 %!shared X,map
74 %! X=magic(16);
75 %! [X,map]=cmunique(X);
76
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));
83
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);
90
91 %!shared X,map
92 %! X=magic(20);
93 %! [X,map]=cmunique(X);
94
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));
101
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);
108
109 %
110 % $Log$
111 % Revision 1.3  2007/03/23 16:14:36  adb014
112 % Update the FSF address
113 %
114 % Revision 1.2  2007/01/04 23:44:22  hauberg
115 % Minor changes in help text
116 %
117 % Revision 1.1  2006/08/20 12:59:32  hauberg
118 % Changed the structure to match the package system
119 %
120 % Revision 1.4  2004/09/08 15:01:28  pkienzle
121 % Redo tests: reduce # of shared variables; force full range of uint8
122 %
123 % Revision 1.3  2004/09/08 14:13:08  jmones
124 % Synchronized with cmunique. uint8 support added. Tests working for 2.1.58
125 %
126 % Revision 1.2  2004/08/18 14:57:42  jmones
127 % speed improvement suggested by Paul Kienzle
128 %
129 % Revision 1.1  2004/08/17 19:18:42  jmones
130 % cmpermute added: Reorders colors in a colormap
131 %
132 %