Attached is a modified version of Dan's code for displaying images in
gnuplot (versions >=4.1). I have reorganized the code a bit, but I put
Dan's name in the copyright notice, since what I did was mostly cutting
and pasting. I'm including 3 files: a patch to image.m,
__gnuplot_version__.m, which returns the version of gnuplot, and
__img__.m, the low-level interface with gnuplot. I have opted to put
only the gnuplot specific code in __img__.
Quentin
------------------------------------------------------------------------
Index: scripts/image/image.m
===================================================================
RCS file: /cvs/octave/scripts/image/image.m,v
retrieving revision 1.26
diff -u -r1.26 image.m
--- scripts/image/image.m 10 Oct 2006 16:10:27 -0000 1.26
+++ scripts/image/image.m 19 Oct 2006 19:25:05 -0000
@@ -57,35 +57,40 @@
print_usage ();
endif
- if isempty(zoom)
- ## Find an integer scale factor which sets the image to
- ## approximately the size of the screen.
- zoom = min ([350/rows(A), 600/columns(A), 4]);
- if (zoom >= 1)
- zoom = floor (zoom);
- else
- zoom = 1 / ceil (1/zoom);
+ if __gnuplot_version__ > 4.0
+ __img__ (x, y, A);
+ else
+
+ if isempty(zoom)
+ ## Find an integer scale factor which sets the image to
+ ## approximately the size of the screen.
+ zoom = min ([350/rows(A), 600/columns(A), 4]);
+ if (zoom >= 1)
+ zoom = floor (zoom);
+ else
+ zoom = 1 / ceil (1/zoom);
+ endif
endif
- endif
- ppm_name = tmpnam ();
+ ppm_name = tmpnam ();
- saveimage (ppm_name, A, "ppm");
+ saveimage (ppm_name, A, "ppm");
- ## Start the viewer. Try display, xv, then xloadimage.
+ ## Start the viewer. Try display, xv, then xloadimage.
- xv = sprintf ("xv -raw -expand %f \"%s\"", zoom, ppm_name);
+ xv = sprintf ("xv -raw -expand %f \"%s\"", zoom, ppm_name);
- xloadimage = sprintf ("xloadimage -zoom %f \"%s\"", zoom*100, ppm_name);
+ xloadimage = sprintf ("xloadimage -zoom %f \"%s\"", zoom*100, ppm_name);
- ## ImageMagick:
- im_display = sprintf ("display -geometry %f%% \"%s\"", zoom*100, ppm_name);
+ ## ImageMagick:
+ im_display = sprintf ("display -geometry %f%% \"%s\"", zoom*100, ppm_name);
- rm = sprintf ("rm -f \"%s\"", ppm_name);
+ rm = sprintf ("rm -f \"%s\"", ppm_name);
- ## Need to let the shell clean up the tmp file because we are putting
- ## the viewer in the background.
+ ## Need to let the shell clean up the tmp file because we are putting
+ ## the viewer in the background.
- system (sprintf ("( %s || %s || %s && %s ) > /dev/null 2>&1 &",
- im_display, xv, xloadimage, rm));
+ system (sprintf ("( %s || %s || %s && %s ) > /dev/null 2>&1 &",
+ im_display, xv, xloadimage, rm));
+ endif
endfunction
------------------------------------------------------------------------
## Copyright (C) 2006 Daniel Sebald
##
## Octave is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2, or (at your option)
## any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING. If not, write to the Free
## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
## 02111-1307, USA.
## -*- texinfo -*-
## @deftypefn {Function File} { @var{version} = } __gnuplot_version__
## Returns the version of gnuplot.
## @end deftypefn
function version = __gnuplot_version__
persistent __version__ = 0;
if !__version__
## If the function hasn't been called before, use this code to get
## the version from gnuplot.
[status, output] = system([gnuplot_binary, " --version"]);
words = split(output, " ");
if ( (size (words,1) >= 2) && isdigit (words(2,1)) )
__version__ = sscanf(words(2,:), "%f", 1);
else
__version__ = -1;
endif
endif
version = __version__;
endfunction
------------------------------------------------------------------------
## Copyright (C) 2006 Daniel Sebald
##
## Octave is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2, or (at your option)
## any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING. If not, write to the Free
## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
## 02111-1307, USA.
## -*- texinfo -*-
## @deftypefn {Function File} {} __img__ (@var{x}, @var{y}, @var{A})
## Display an image using @code{gnuplot}, where vectors @var{x} and
## @var{y} define the axes and the matrix @var{A} contains the image
## data.
## @end deftypefn
function __img__ (x, y, A)
if (nargin ~= 3)
print_usage ();
endif
if (isempty(A))
error("Image matrix is empty.");
endif
__gnuplot_raw__ ("set nokey\n");
__current_color_map__ = colormap ();
palette_size = size (__current_color_map__, 1);
__gnuplot_raw__ (sprintf ("set palette positive color model RGB maxcolors
%i\n", palette_size));
if (palette_size <= 128)
## Break up command to avoid buffer overflow.
__gnuplot_raw__ ("set palette file \"-\"\n");
for i = 1:palette_size
__gnuplot_raw__ (sprintf ("%g %g %g %g\n", 1e-3 * round (1e3 *
[(i-1)/(palette_size-1) __current_color_map__(i,:)])));
end
__gnuplot_raw__("e\n");
else
## Let the file be deleted when Octave exits or `purge_tmp_files' is called.
[fid, binary_file_name, msg] = mkstemp([P_tmpdir,"/gpimageXXXXXX"], 1);
fwrite(fid, __current_color_map__', "float32", 0, "ieee-le");
fclose(fid);
__gnuplot_raw__ (sprintf ("set palette file \"%s\" binary record=%d using
1:2:3\n", binary_file_name, palette_size));
endif
## Use the newly added mode of "plot" called "with image".
if (isempty(x))
x = [1 size(A,2)];
y = [1 size(A,1)];
endif
## Force rectangular grid by using only end points of
## first row (column) if x (y) is a matrix or vector.
if ((size(x,2)) > 1)
x = x(1,:)';
endif
if (abs(x(end) - x(1)) < (10*eps))
error ("End points in x dimension must not be equal.");
else
x_dim = size(A,2);
if (x_dim > 1)
dx = (x(end)-x(1))/(x_dim-1);
else
dx = 1;
endif
endif
if ((size(y,1)) > 1)
y = y(:,1)';
endif
if (abs(y(end) - y(1)) < 10*eps)
error ("End points in y dimension must not be equal.");
else
y_dim = size(A,1);
if (y_dim > 1)
dy = (y(end)-y(1))/(y_dim-1);
else
dy = 1;
endif
endif
__gnuplot_raw__ (sprintf ("set xrange [%g:%g]\n", x(1)-dx/2, x(end)+dx/2));
__gnuplot_raw__ (sprintf ("set yrange [%g:%g]\n", y(1)-dy/2, y(end)+dy/2));
__gnuplot_raw__ ("set autoscale fix\n"); # "fix" is helpful for "a" hotkey
__gnuplot_raw__ ("set tics out\n");
A = reshape(A,size(A,1)*size(A,2),1);
## Let the file be deleted when Octave exits or `purge_tmp_files' is called.
[fid, binary_file_name, msg] = mkstemp ([P_tmpdir, "/gpimageXXXXXX"], 1);
## Gnuplot reads binary files very quickly. However, the 'fwrite' below
## is much slower than using the current '__gnuplot_plot__' command.
fwrite(fid, A', "float", 0, "ieee-le");
fclose(fid);
__gnuplot_raw__ (sprintf ("plot \"%s\" binary array=%dx%d scan=yx flipy
origin=(%g,%g) dx=%g dy=%g endian=little using 1 with image\n", binary_file_name, x_dim,
y_dim, min (x(1), x(end)), min(y(1), y(end)), abs (dx), abs (dy)));
## Put back in default data mode.
__gnuplot_raw__ ("set xrange [*:*];\n");
__gnuplot_raw__ ("set yrange [*:*];\n");
endfunction