1 ## Copyright (C) 1994-2012 John W. Eaton
3 ## This file is part of Octave.
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 3 of the License, or (at
8 ## your option) any later version.
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.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} image (@var{img})
21 ## @deftypefnx {Function File} {} image (@var{x}, @var{y}, @var{img})
22 ## @deftypefnx {Function File} {@var{h} =} image (@dots{})
23 ## Display a matrix as a color image. The elements of @var{img} are indices
24 ## into the current colormap, and the colormap will be scaled so that the
25 ## extremes of @var{img} are mapped to the extremes of the colormap.
27 ## The axis values corresponding to the matrix elements are specified in
28 ## @var{x} and @var{y}. If you're not using gnuplot 4.2 or later, these
29 ## variables are ignored.
31 ## Implementation Note: The origin (0, 0) for images is located in the
32 ## upper left. For ordinary plots, the origin is located in the lower
33 ## left. Octave handles this inversion by plotting the data normally,
34 ## and then reversing the direction of the y-axis by setting the
35 ## @code{ydir} property to @code{"reverse"}. This has implications whenever
36 ## an image and an ordinary plot need to be overlaid. The recommended
37 ## solution is to display the image and then plot the reversed ydata
38 ## using, for example, @code{flipud (ydata,1)}.
40 ## The optional return value @var{h} is a graphics handle to the image.
41 ## @seealso{imshow, imagesc, colormap}
44 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
48 function retval = image (varargin)
50 [ax, varargin, nargin] = __plt_get_axis_arg__ ("image", varargin{:});
52 firstnonnumeric = Inf;
54 if (! isnumeric (varargin{i}))
60 if (nargin == 0 || firstnonnumeric == 1)
61 img = imread ("default.img");
63 elseif (nargin == 1 || firstnonnumeric == 2)
66 elseif (nargin == 2 || firstnonnumeric == 3)
78 h = __img__ (x, y, img, varargin {firstnonnumeric:end});
79 set (ax, "layer", "top");
80 unwind_protect_cleanup
90 ## Generic image creation.
92 ## The axis values corresponding to the matrix elements are specified in
93 ## @var{x} and @var{y}. If you're not using gnuplot 4.2 or later, these
94 ## variables are ignored.
96 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
100 function h = __img__ (x, y, img, varargin)
105 error ("__img__: matrix is empty");
109 x = [1, columns(img)];
116 xdata = [x(1), x(end)];
117 ydata = [y(1), y(end)];
121 dx = std (dx) / mean (abs (dx));
122 dy = std (dy) / mean (abs (dy));
124 if (any (dx > tol) || any (dy > tol))
125 warning ("Image does not map to non-linearly spaced coordinates")
130 tmp = __go_image__ (ca, "cdata", img, "xdata", xdata, "ydata", ydata,
131 "cdatamapping", "direct", varargin {:});
133 px = __image_pixel_size__ (tmp);
135 if (xdata(2) < xdata(1))
136 xdata = xdata(2:-1:1);
137 elseif (xdata(2) == xdata(1))
138 xdata = xdata(1) + [0, size(img,2)-1];
140 if (ydata(2) < ydata(1))
141 ydata = ydata(2:-1:1);
142 elseif (ydata(2) == ydata(1))
143 ydata = ydata(1) + [0, size(img,1)-1];
145 xlim = xdata + [-px(1), px(1)];
146 ylim = ydata + [-px(2), px(2)];
148 ## FIXME -- how can we do this and also get the {x,y}limmode
149 ## properties to remain "auto"? I suppose this adjustment should
150 ## happen automatically in axes::update_axis_limits instead of
151 ## explicitly setting the values here. But then what information is
152 ## available to axes::update_axis_limits to determine that the
153 ## adjustment is necessary?
154 set (ca, "xlim", xlim, "ylim", ylim);
156 if (ndims (img) == 3)
161 set (ca, "clim", double ([mn, mx]));
165 set (ca, "view", [0, 90]);
167 if (strcmp (get (ca, "nextplot"), "replace"))
168 # Always reverse y-axis for images, unless hold is on
169 set (ca, "ydir", "reverse");
180 %! img = 1 ./ hilb (11);
184 %! h = image (abs(x), abs(y), img);
185 %! set (h, "cdatamapping", "scaled")
186 %! ylabel ("limits = [4.5, 15.5]")
187 %! title ('image (abs(x), abs(y), img)')
189 %! h = image (-x, y, img);
190 %! set (h, "cdatamapping", "scaled")
191 %! title ('image (-x, y, img)')
193 %! h = image (x, -y, img);
194 %! set (h, "cdatamapping", "scaled")
195 %! title ('image (x, -y, img)')
196 %! ylabel ("limits = [-5.5, 5.5]")
198 %! h = image (-x, -y, img);
199 %! set (h, "cdatamapping", "scaled")
200 %! title ('image (-x, -y, img)')
206 %! imagesc (g, g, sin (h));
208 %! imagesc (g, g+12, cos (h/2));
209 %! axis ([0 10 0 22])
211 %! title ("two consecutive images")
217 %! imagesc (g, g, sin (h));
219 %! plot (g, 11.0 * ones (size (g)))
220 %! imagesc (g, g+12, cos (h/2));
221 %! axis ([0 10 0 22])
223 %! title ("image, line, image")
229 %! plot (g, 10.5 * ones (size (g)))
231 %! imagesc (g, g, sin (h));
232 %! plot (g, 11.0 * ones (size (g)))
233 %! imagesc (g, g+12, cos (h/2));
234 %! plot (g, 11.5 * ones (size (g)))
235 %! axis ([0 10 0 22])
237 %! title ("line, image, line, image, line")