]> Creatis software - CreaPhase.git/blob - octave_packages/m/image/image.m
update packages
[CreaPhase.git] / octave_packages / m / image / image.m
1 ## Copyright (C) 1994-2012 John W. Eaton
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 3 of the License, or (at
8 ## your option) 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, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
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.
26 ##
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.
30 ##
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)}.
39 ##
40 ## The optional return value @var{h} is a graphics handle to the image.
41 ## @seealso{imshow, imagesc, colormap}
42 ## @end deftypefn
43
44 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
45 ## Created: July 1994
46 ## Adapted-By: jwe
47
48 function retval = image (varargin)
49
50   [ax, varargin, nargin] = __plt_get_axis_arg__ ("image", varargin{:});
51
52   firstnonnumeric = Inf;
53   for i = 1 : nargin
54     if (! isnumeric (varargin{i}))
55       firstnonnumeric = i;
56       break;
57     endif
58   endfor
59
60   if (nargin == 0 || firstnonnumeric == 1)
61     img = imread ("default.img");
62     x = y = [];
63   elseif (nargin == 1 || firstnonnumeric == 2)
64     img = varargin{1};
65     x = y = [];
66   elseif (nargin == 2 || firstnonnumeric == 3)
67     print_usage ();
68   else
69     x = varargin{1};
70     y = varargin{2};
71     img = varargin{3};
72     firstnonnumeric = 4;
73   endif
74
75   oldax = gca ();
76   unwind_protect
77     axes (ax);
78     h = __img__ (x, y, img, varargin {firstnonnumeric:end});
79     set (ax, "layer", "top");
80   unwind_protect_cleanup
81     axes (oldax);
82   end_unwind_protect
83
84   if (nargout > 0)
85     retval = h;
86   endif
87
88 endfunction
89
90 ## Generic image creation.
91 ##
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.
95
96 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
97 ## Created: July 1994
98 ## Adapted-By: jwe
99
100 function h = __img__ (x, y, img, varargin)
101   
102   newplot ();
103
104   if (isempty (img))
105     error ("__img__: matrix is empty");
106   endif
107
108   if (isempty (x))
109     x = [1, columns(img)];
110   endif
111
112   if (isempty (y))
113     y = [1, rows(img)];
114   endif
115
116   xdata = [x(1), x(end)];
117   ydata = [y(1), y(end)];
118
119   dx = diff (x);
120   dy = diff (y);
121   dx = std (dx) / mean (abs (dx));
122   dy = std (dy) / mean (abs (dy));
123   tol = 100*eps;
124   if (any (dx > tol) || any (dy > tol))
125     warning ("Image does not map to non-linearly spaced coordinates")
126   endif
127
128   ca = gca ();
129
130   tmp = __go_image__ (ca, "cdata", img, "xdata", xdata, "ydata", ydata,
131                     "cdatamapping", "direct", varargin {:});
132
133   px = __image_pixel_size__ (tmp);
134
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];
139   endif
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];
144   endif
145   xlim = xdata + [-px(1), px(1)];
146   ylim = ydata + [-px(2), px(2)];
147
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);
155
156   if (ndims (img) == 3)
157     if (isinteger (img))
158       c = class (img);
159       mn = intmin (c);
160       mx = intmax (c);
161       set (ca, "clim", double ([mn, mx]));
162     endif
163   endif
164
165   set (ca, "view", [0, 90]);
166
167   if (strcmp (get (ca, "nextplot"), "replace"))
168     # Always reverse y-axis for images, unless hold is on
169     set (ca, "ydir", "reverse");
170   endif
171
172   if (nargout > 0)
173     h = tmp;
174   endif
175
176 endfunction
177
178 %!demo
179 %! clf
180 %! img = 1 ./ hilb (11);
181 %! x = -5:5;
182 %! y = x;
183 %! subplot (2,2,1)
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)')
188 %! subplot (2,2,2)
189 %! h = image (-x, y, img);
190 %! set (h, "cdatamapping", "scaled")
191 %! title ('image (-x, y, img)')
192 %! subplot (2,2,3)
193 %! h = image (x, -y, img);
194 %! set (h, "cdatamapping", "scaled")
195 %! title ('image (x, -y, img)')
196 %! ylabel ("limits = [-5.5, 5.5]")
197 %! subplot (2,2,4)
198 %! h = image (-x, -y, img);
199 %! set (h, "cdatamapping", "scaled")
200 %! title ('image (-x, -y, img)')
201
202 %!demo
203 %! clf
204 %! g = 0.1:0.1:10;
205 %! h = g'*g;
206 %! imagesc (g, g, sin (h));
207 %! hold on
208 %! imagesc (g, g+12, cos (h/2));
209 %! axis ([0 10 0 22])
210 %! hold off
211 %! title ("two consecutive images")
212
213 %!demo
214 %! clf
215 %! g = 0.1:0.1:10;
216 %! h = g'*g;
217 %! imagesc (g, g, sin (h));
218 %! hold all
219 %! plot (g, 11.0 * ones (size (g)))
220 %! imagesc (g, g+12, cos (h/2));
221 %! axis ([0 10 0 22])
222 %! hold off
223 %! title ("image, line, image")
224
225 %!demo
226 %! clf
227 %! g = 0.1:0.1:10;
228 %! h = g'*g;
229 %! plot (g, 10.5 * ones (size (g)))
230 %! hold all
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])
236 %! hold off
237 %! title ("line, image, line, image, line")
238