help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

image.m and gnuplot 4


From: Fredrik Lingvall
Subject: image.m and gnuplot 4
Date: Tue, 08 Mar 2005 11:39:52 +0100
User-agent: Mozilla Thunderbird 1.0 (X11/20050124)

Hi All,

I've have adapted image.m to use the pm3d map mode
available in gnuplot 4. Now it should be possible to
do for example:

image(x,y,A);

and get axis tics given by x and y.

The script tests if gnuplot 4 (or 3.8) is installed and if not
it should fall back to the old methods (using xv or ImageMagick).

Issues:

colormap do not seem to have any effect in the images.

Note:

To get a Matlab-like behaviour do:

gset palette color;
gset palette negative;

Enjoy!

Fredrik



## Copyright (C) 1996, 1997 John W. Eaton
##
## This file is part of Octave.
##
## 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} {} image (@var{x}, @var{zoom})
## @deftypefnx {Function File} {} image (@var{x}, @var{y}, @var{A}, @var{zoom})
## Display a matrix as a color image.  The elements of @var{x} are indices
## into the current colormap and should have values between 1 and the
## length of the colormap.  If @var{zoom} is omitted, the image will be
## scaled to fit within 600x350 (to a max of 4).
##
## It first tries to use @code{display} from @code{ImageMagick} then
## @code{xv} and then @code{xloadimage}.
##
## The axis values corresponding to the matrix elements are specified in
## @var{x} and @var{y}. At present they are ignored.
## @end deftypefn
##
## @seealso{imshow, imagesc, and colormap}

## Author: Tony Richardson <address@hidden>
## Created: July 1994
## Adapted-By: jwe
## Fredrik Lingvall 2005-03-08: Added gnuplot 4 stuff. 

function image (x, y, A, zoom)

if (nargin == 0)
    ## Load Bobbie Jo Richardson (Born 3/16/94)
    A = loadimage ("default.img");
    x = y = [];
    zoom = 2;
  elseif (nargin == 1)
    A = x;
    zoom = [];
    x = y = [];
  elseif (nargin == 2)
    A = x;
    zoom = y;
    x = y = [];
  elseif (nargin == 3)
    zoom = [];
  elseif (nargin > 4)
    usage ("image (matrix, zoom) or image (x, y, matrix, zoom)");
  end

  # Get version of gnuplot.
  [gnuplot_version,s] = system('gnuplot --version');
  
  # Check for gnuplot 4.x
  if (~isempty(findstr(gnuplot_version,'gnuplot 4')) | ...
        ~isempty(findstr(gnuplot_version,'gnuplot 3.8')))
      
    #
    # Use gnuplot 4 (using similar code as in mesh.m)
    #
    
    if (isempty(x) && isempty(y) && ~isempty(A))
      if (ismatrix (A))
        
        gset hidden3d;
        gset data style lines;
        gset surface;
        gset nocontour;
        gset noparametric;
        gset nologscale;
        
        gset pm3d map; # Set 2D (image) mode.
        gset pm3d corners2color c1; # Use only one corner for images.

        #gset palette gray;
        #gset palette color; # Matlab default.
        #gset palette negative; # More Matlab-like.

        gset size ratio -1;
        gset cbrange [0:64]; # Set color range as in Matlab.

        axis([0 columns(A) 0 rows(A)]);
        axis('xy');
        
        # Hack to force gnuplot to show the whole image.
        # See manual on pm3d mode on this issue.
        A = [A A(:,size(A,2))];
        At = A';
        A = [At(:,1) At]';
        A = A';        

        gsplot(A);
      
      else
        error ("image: argument must be a matrix");
      end
    
    elseif (~isempty(x) && ~isempty(y) && ~isempty(A))

      # Hack to force gnuplot to show the whole image (works 
      # for linearly spaced x and y).
      # See gnuplot manual on pm3d mode on this issue.
      A = [A A(:,size(A,2))];
      At = A';
      A = [At At(:,size(At,2))]';
      x = [x(:)' x(length(x))+x(length(x))-x(length(x)-1)]; 
      y = [y(:)' y(length(y))+y(length(y))-y(length(y)-1)]; 

      if (isvector (x) && isvector (y) && ismatrix (A))
        xlen = length (x);
        ylen = length (y);
        
        if (xlen == columns (A) && ylen == rows (A))
          if (rows (y) == 1)
            y = y';
          end

          len = 3 * xlen;
          zz = zeros (ylen, len);
          k = 1;
          for i = 1:3:len
            zz(:,i)   = x(k) * ones (ylen, 1);
            zz(:,i+1) = y;
            zz(:,i+2) = A(:,k);
            k++;
          end

          gset hidden3d;
          gset data style lines;
          gset surface;
          gset noparametric;
          gset nocontour;
          gset nologscale;
          gset pm3d map; # Set 2D (image) mode.
          gset pm3d corners2color c1; # Use only one corner for images.

          #gset palette gray;
          #gset palette color; # Matlab default.
          #gset palette negative; # More Matlab-like.

          gset size ratio -1;
          gset cbrange [0:64]; # Set color range as in Matlab.

          axis([min(x) max(x) min(y) max(y)]);
          axis('xy');
          
          gset parametric;
          gsplot (zz);
          gset noparametric;

        else
          msg = "image: rows (A) must be the same as length (y) and";
          msg = sprintf ("%s\ncolumns (A) must be the same as length (x)", msg);
          error (msg);
        end
      else
        error ("image: x and y must be vectors and A must be a matrix");
      end
    else
      usage ("image (A)");
    end
    
  else 
    
    #
    # Fallback to old the methods.
    #

    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);
      end
    end
    ppm_name = tmpnam ();
    
    saveimage (ppm_name, A, "ppm");
    
    ## Start the viewer.  Try display, xv, then xloadimage.
    
    xv = sprintf ("xv -raw -expand %f \"%s\"", zoom, ppm_name);
    
    xloadimage = sprintf ("xloadimage -zoom %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);
    
    ## 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));
  end
endfunction

reply via email to

[Prev in Thread] Current Thread [Next in Thread]