2847
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
2313
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
1024
|
19 |
3381
|
20 ## -*- texinfo -*- |
4836
|
21 ## @deftypefn {Function File} {} imshow (@var{i}) |
|
22 ## @deftypefnx {Function File} {} imshow (@var{x}, @var{map}) |
3373
|
23 ## @deftypefnx {Function File} {} imshow (@var{i}, @var{n}) |
|
24 ## @deftypefnx {Function File} {} imshow (@var{r}, @var{g}, @var{b}) |
4836
|
25 ## Display an image. |
3426
|
26 ## |
5318
|
27 ## @code{imshow (@var{x})} displays an image @var{x}. |
|
28 ## The numerical class of the image determines its bit-depth: 1 for |
|
29 ## @code{logical}, 8 for @code{uint8} and @code{logical}, and 16 for |
|
30 ## @code{double} or @code{uint16}. If @var{x} has dimensions MxNx3, the |
|
31 ## three matrices represent the red, green and blue components of the |
|
32 ## image. |
3426
|
33 ## |
3373
|
34 ## @code{imshow (@var{x}, @var{map})} displays an indexed image using the |
|
35 ## specified colormap. |
3426
|
36 ## |
5318
|
37 ## @code{imshow (@var{i}, @var{n})} displays a gray scale intensity image of |
4836
|
38 ## N levels. |
3426
|
39 ## |
3373
|
40 ## @code{imshow (@var{r}, @var{g}, @var{b})} displays an RGB image. |
4836
|
41 ## |
5318
|
42 ## The character string @code{"truesize"} can always be used as an |
|
43 ## optional final argument to prevent automatic zooming of the image. |
3373
|
44 ## @end deftypefn |
3457
|
45 ## @seealso{image, imagesc, colormap, gray2ind, and rgb2ind} |
904
|
46 |
3202
|
47 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
2312
|
48 ## Created: July 1994 |
|
49 ## Adapted-By: jwe |
559
|
50 |
4836
|
51 function imshow (varargin) |
|
52 |
|
53 usage_str = "imshow (x) or imshow (x, map) or imshow (i, N) or imshow (r, g, b)"; |
559
|
54 |
4836
|
55 if (nargin == 0 || nargin > 4) |
|
56 usage (usage_str); |
|
57 endif |
|
58 |
|
59 ## Count nr of matrix arguments. |
|
60 mvars = 0; |
|
61 while (mvars < nargin && ismatrix (varargin{mvars+1})) |
|
62 mvars++; |
|
63 endwhile |
|
64 |
|
65 if (mvars < 1 || mvars > 3) |
|
66 usage (usage_str); |
|
67 endif |
5318
|
68 |
|
69 ## Determine image depth |
|
70 imclass = class (varargin{1}); |
|
71 s = __im_numeric_limits__ (imclass); |
|
72 if (!isfield (s, "max")) |
|
73 error ("imshow: cannot handle image class '%s'", imclass); |
|
74 endif |
|
75 |
|
76 ## Maximum bit-depth is 16 |
|
77 if (s.max > 65535) |
|
78 s.max = 65535; |
|
79 endif |
|
80 |
|
81 imdepth = log (s.max+1) / log (2); |
|
82 if (imdepth - floor (imdepth) != 0) |
|
83 error ("imshow: cannot determine image colour depth"); |
|
84 endif |
4836
|
85 |
5318
|
86 ## Remove complex parts of arguments |
|
87 realwarning = false; |
|
88 for i = 1:mvars |
|
89 if (iscomplex (varargin{i})) |
|
90 if (!realwarning) |
|
91 warning ("imshow: displaying real part of complex image"); |
|
92 realwarning = true; |
|
93 endif |
|
94 varargin{i} = real (varargin{i}); |
|
95 endif |
|
96 endfor |
|
97 |
|
98 ## Pack r,g,b image into ND-matrix if necessary |
|
99 if (mvars == 3) |
|
100 I = []; |
|
101 try |
|
102 I = cat (3, varargin{1:3}); |
|
103 catch |
|
104 error ("imshow: r, g and b matrix dimensions must agree"); |
|
105 end_try_catch |
|
106 else |
4836
|
107 I = varargin{1}; |
5318
|
108 endif |
|
109 I = double (I); |
|
110 |
|
111 ## Is the image specified as MxNx3 colour? |
|
112 iscolour = false; |
|
113 if (size (I,3) == 3) |
|
114 iscolour = true; |
|
115 endif |
|
116 |
|
117 ## Is the image indexed? |
|
118 isindexed = false; |
|
119 if (mvars == 2) |
|
120 isindexed = true; |
|
121 if (iscolour) |
|
122 error ("imshow: cannot provide colour image and colourmap"); |
559
|
123 endif |
|
124 endif |
5318
|
125 |
|
126 ## Scale images of class "double" appropriately |
|
127 if (!isindexed) |
|
128 if (strcmp (imclass, "double") == 1) |
|
129 if (max (I(:)) <= 1) |
|
130 ## image in [0-1]; scale to [0 - 2^imdepth] |
|
131 I = I * 2^imdepth; |
|
132 else |
|
133 ## image outside [0-1]; this is unexpected: scale to [0 - 2^imdepth] |
|
134 I = I / max (I(:)) * 2^imdepth; |
|
135 endif |
4911
|
136 endif |
5318
|
137 endif |
|
138 |
|
139 ## Generate colour map |
|
140 if (isindexed) |
4836
|
141 M = varargin{2}; |
|
142 if (isscalar (M)) |
|
143 M = gray (M); |
|
144 endif |
5318
|
145 elseif (iscolour) |
|
146 I = I / 2^imdepth; |
|
147 [I, M] = rgb2ind (I(:,:,1), I(:,:,2), I(:,:,3)); |
|
148 else |
|
149 I = I+1; ## index into colourmap |
|
150 M = gray (2^imdepth); |
4836
|
151 endif |
|
152 |
|
153 ## Check for "truesize". |
|
154 zoom = []; |
|
155 for i = mvars+1:nargin |
|
156 if (isstr (varargin{i}) && strcmp (varargin{i}, "truesize")) |
|
157 zoom = 1; |
|
158 endif |
|
159 endfor |
|
160 |
|
161 colormap (M); |
|
162 image (I, zoom); |
559
|
163 |
|
164 endfunction |
4836
|
165 |
5318
|
166 function s = __im_numeric_limits__ (cname) |
|
167 s = struct (); |
|
168 switch (cname) |
|
169 case ("double") |
|
170 s.max = realmax; |
|
171 case ("char") |
|
172 s.max = 255; |
|
173 case ("logical") |
|
174 s.max = 1; |
|
175 otherwise |
|
176 try |
|
177 s.max = double (intmax (cname)); |
|
178 catch |
|
179 end_try_catch |
|
180 endswitch |
|
181 endfunction |
|
182 |
4836
|
183 %!error imshow () # no arguments |
|
184 %!error imshow (1, 2, 3, 4, 5) # too many arguments |
|
185 %!error imshow ([1,2], [2,3], [3,4], [4,5]) # too many matrix arguments |
|
186 %!error imshow ("image.png") # filename not accepted as argument |
|
187 |
|
188 %!demo |
|
189 %! imshow (loadimage ("default.img")); |
|
190 |
|
191 %!demo |
|
192 %! I = loadimage ("default.img"); |
|
193 %! imshow (I, "truesize") |
|
194 |
|
195 %!demo |
|
196 %! [I, M] = loadimage ("default.img"); |
5318
|
197 %! imshow (I, M); |
|
198 |
|
199 %!demo |
|
200 %! [I, M] = loadimage ("default.img"); |
|
201 %! imshow (I, I*0.5, I*0.8); |
|
202 |
|
203 %!demo |
|
204 %! [I, M] = loadimage ("default.img"); |
|
205 %! X = []; |
|
206 %! X = cat (3, X, I*0.8); |
|
207 %! X = cat (3, X, I*0.8); |
|
208 %! X = cat (3, X, I); |
|
209 %! imshow (X); |
|
210 |