octave-maintainers
[Top][All Lists]
Advanced

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

image/imagesc colorbar


From: David Bateman
Subject: image/imagesc colorbar
Date: Tue, 20 Nov 2007 15:41:19 +0100
User-agent: Thunderbird 1.5.0.7 (X11/20060921)

The imagesc and image functions are incorrectly assuming that the data
to plot should be scaled to the colormap. In fact the data should be
left untouched and the colormap adjusted accordingly. This corrects the
issue of the issue colorbar for images always plotting a range [0:70]
which is incorrect. With the attached patch and the caxis patch I sent
this then allows the correct colormaps to be displayed and adjusted for
images.

As for the colorbar function itself, the colorbar is a axes object that
is a child of the figure in Matlab in the same manner as a subplot. The
colorbar itself is an Nx1 image that is a child of this axes. It would
be easy enough to implement the split of the original plot into a
subplot with a colorbar, but then there are two difficulties.

* If the colormap or the clims of the original plot is changed this
should be reflected in the new colorbar, which unfortunately is in a
different set of axes..

* If the position of the colorbar is moved, its new position must be
calculated wrt the original plot, and the position of the original plot
must also be changed..

Matlab handles this issue by the use of listener functions in the
colorbar and plot axes, to pass information on the changes in one to the
other set of axes. Adding listeners was vetoed by John due the the
extent changes needed to implement it before 3.0..

I therefore see no way to implement correctly a colorbar function prior
to the release of 3.0. Also any implementation of the colorbar as an
image in a separate set of axis and not with the gnuplot colorbox
function will mean that it can not be used with gnuplot 4.0..

D.

-- 
David Bateman                                address@hidden
Motorola Labs - Paris                        +33 1 69 35 48 04 (Ph) 
Parc Les Algorithmes, Commune de St Aubin    +33 6 72 01 06 33 (Mob) 
91193 Gif-Sur-Yvette FRANCE                  +33 1 69 35 77 01 (Fax) 

The information contained in this communication has been classified as: 

[x] General Business Information 
[ ] Motorola Internal Use Only 
[ ] Motorola Confidential Proprietary

*** ./scripts/image/imagesc.m.orig16    2007-11-20 12:02:40.603079390 +0100
--- ./scripts/image/imagesc.m   2007-11-20 12:21:53.388845988 +0100
***************
*** 21,112 ****
  ## @deftypefn {Function File} {} imagesc (@var{A})
  ## @deftypefnx {Function File} {} imagesc (@var{x}, @var{y}, @var{A})
  ## @deftypefnx {Function File} {} imagesc (@dots{}, @var{limits})
! ## @deftypefnx {Function File} { @var{B} = } imagesc (@dots{})
  ## Display a scaled version of the matrix @var{A} as a color image.  The
! ## matrix is scaled so that its entries are indices into the current
! ## colormap.  The scaled matrix is returned.  If @var{limits} = 
address@hidden, @var{hi}] are
! ## given, then that range maps into the full range of the colormap rather 
! ## than the minimum and maximum values of @var{A}.
  ##
  ## The axis values corresponding to the matrix elements are specified in
  ## @var{x} and @var{y}, either as pairs giving the minimum and maximum
  ## values for the respective axes, or as values for each row and column
  ## of the matrix @var{A}.
! ## @seealso{image, imshow}
  ## @end deftypefn
  
  ## Author: Tony Richardson <address@hidden>
  ## Created: July 1994
  ## Adapted-By: jwe
  
! function ret = imagesc (x, y, A, limits, DEPRECATEDZOOM)
  
    ## Deprecated zoom.  Remove this hunk of code if old zoom argument
    ## is outmoded.
!   if ((nargin == 2 && isscalar (y))
!       || (nargin == 3 && (isscalar (y) || isscalar (A)))
!       || (nargin == 4 && isscalar (limits))
!       || nargin == 5)
      warning ("image: zoom argument ignored -- use GUI features");
    endif
!   if (nargin == 5)
      if (isscalar (limits))
        limits = DEPRECATEDZOOM;
      endif
!     nargin = 4;
    endif
!   if (nargin == 4 && isscalar (limits))
!     nargin = 3;
    endif
!   if (nargin == 3 && (isscalar (y) || isscalar (A)))
      if (isscalar (y))
        y = A;
      endif
!     nargin = 2;
    endif
!   if (nargin == 2 && isscalar (y))
!     nargin = 1;
    endif
  
!   if (nargin < 1 || nargin > 4)
      print_usage ();
!   elseif (nargin == 1)
      A = x;
      x = y = limits = [];
!   elseif (nargin == 2)
      A = x;
      limits = y;
      x = y = [];
!   elseif (nargin == 3 && !isscalar (x) && !isscalar (y) && !isscalar (A))
      limits = [];
    endif
  
    ## use given limits or guess them from the matrix
    if (length (limits) == 2 && limits(2) >= limits(1))
!      minval = limits(1);
!      maxval = limits(2);
!      A(A < minval) = minval;
!      A(A > maxval) = maxval;
!   elseif (length (limits) == 0)
!      maxval = max (A(:));
!      minval = min (A(:));
!   else
       error ("expected data limits to be [lo, hi]");
    endif
  
-   ## scale the limits to the range of the colormap
-   if (maxval == minval)
-     B = ones (size (A));
-   else
-     ## Rescale values to between 1 and length (colormap) inclusive.
-     B = round ((A - minval) / (maxval - minval) * (rows (colormap) - 1)) + 1;
-   endif
- 
-   ## display or return the image
-   if (nargout == 0)
-     image (x, y, B);
-   else
-     ret = B;
-   endif
- 
  endfunction
--- 21,120 ----
  ## @deftypefn {Function File} {} imagesc (@var{A})
  ## @deftypefnx {Function File} {} imagesc (@var{x}, @var{y}, @var{A})
  ## @deftypefnx {Function File} {} imagesc (@dots{}, @var{limits})
! ## @deftypefnx {Function File} {} imagesc (@var{h}, @dots{})
! ## @deftypefnx {Function File} { @var{h} = } imagesc (@dots{})
  ## Display a scaled version of the matrix @var{A} as a color image.  The
! ## colormap is scaled so that the entries of the matrix occupy the entire
! ## colormap.  If @var{limits} = address@hidden, @var{hi}] are given, then that
! ## range is set to the 'clim' of the current axes.
  ##
  ## The axis values corresponding to the matrix elements are specified in
  ## @var{x} and @var{y}, either as pairs giving the minimum and maximum
  ## values for the respective axes, or as values for each row and column
  ## of the matrix @var{A}.
! ##
! ## @seealso{image, imshow, clim, caxis}
  ## @end deftypefn
  
  ## Author: Tony Richardson <address@hidden>
  ## Created: July 1994
  ## Adapted-By: jwe
  
! function retval = imagesc (varargin)
! 
!   if (nargin < 1)
!     print_usage ();
!   elseif (isscalar (varargin{1}) && ishandle (varargin{1}))
!     h = varargin {1};
!     if (! strcmp (get (h, "type"), "axes"))
!       error ("imagesc: expecting first argument to be an axes object");
!     endif
!     oldh = gca ();
!     unwind_protect
!       axes (h);
!       tmp = __imagesc__ (h, varargin{2:end});
!     unwind_protect_cleanup
!       axes (oldh);
!     end_unwind_protect
!   else
!     tmp = __imagesc__ (gca (), varargin{:});
!   endif
! 
!   if (nargout > 0)
!     retval = tmp;
!   endif
! 
! endfunction
! 
! function ret = __imagesc__ (ax, x, y, A, limits, DEPRECATEDZOOM)
  
    ## Deprecated zoom.  Remove this hunk of code if old zoom argument
    ## is outmoded.
!   if ((nargin == 3 && isscalar (y))
!       || (nargin == 4 && (isscalar (y) || isscalar (A)))
!       || (nargin == 5 && isscalar (limits))
!       || nargin == 6)
      warning ("image: zoom argument ignored -- use GUI features");
    endif
!   if (nargin == 6)
      if (isscalar (limits))
        limits = DEPRECATEDZOOM;
      endif
!     nargin = 5;
    endif
!   if (nargin == 5 && isscalar (limits))
!     nargin = 4;
    endif
!   if (nargin == 4 && (isscalar (y) || isscalar (A)))
      if (isscalar (y))
        y = A;
      endif
!     nargin = 3;
    endif
!   if (nargin == 3 && isscalar (y))
!     nargin = 2;
    endif
  
!   if (nargin < 2 || nargin > 5)
      print_usage ();
!   elseif (nargin == 2)
      A = x;
      x = y = limits = [];
!   elseif (nargin == 3)
      A = x;
      limits = y;
      x = y = [];
!   elseif (nargin == 4 && !isscalar (x) && !isscalar (y) && !isscalar (A))
      limits = [];
    endif
  
+   ret = image (ax, x, y, A);
+ 
    ## use given limits or guess them from the matrix
    if (length (limits) == 2 && limits(2) >= limits(1))
!      set (ax, "clim", limits);
!   elseif (!isempty (limits))
       error ("expected data limits to be [lo, hi]");
    endif
  
  endfunction
*** ./scripts/image/image.m.orig16      2007-11-20 12:03:19.170102991 +0100
--- ./scripts/image/image.m     2007-11-20 12:24:41.988278422 +0100
***************
*** 21,28 ****
  ## @deftypefn {Function File} {} image (@var{img})
  ## @deftypefnx {Function File} {} image (@var{x}, @var{y}, @var{img})
  ## 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.
  ##
  ## It first tries to use @code{gnuplot}, then @code{display} from 
  ## @code{ImageMagick}, then @code{xv}, and then @code{xloadimage}.
--- 21,28 ----
  ## @deftypefn {Function File} {} image (@var{img})
  ## @deftypefnx {Function File} {} image (@var{x}, @var{y}, @var{img})
  ## Display a matrix as a color image.  The elements of @var{x} are indices
! ## into the current colormap, and the colormap will be scaled so that the
! ## extremes of @var{x} are mapped to the extremes of teh colormap.
  ##
  ## It first tries to use @code{gnuplot}, then @code{display} from 
  ## @code{ImageMagick}, then @code{xv}, and then @code{xloadimage}.
***************
*** 39,73 ****
  ## Created: July 1994
  ## Adapted-By: jwe
  
! function h = image (x, y, img)
  
    ## Deprecated zoom.  Remove this hunk of code if old zoom argument
    ## is outmoded.
!   if ((nargin == 2 && isscalar (y)) || nargin == 4)
      warning ("image: zoom argument ignored -- use GUI features");
    endif
!   if (nargin == 4)
!     nargin = 3;
    endif
!   if (nargin == 2 && isscalar (y))
!     nargin = 1;
    endif
  
!   if (nargin == 0)
      ## Load Bobbie Jo Richardson (Born 3/16/94)
      img = loadimage ("default.img");
      x = y = [];
!   elseif (nargin == 1)
      img = x;
      x = y = [];
!   elseif (nargin == 2 || nargin > 3)
      print_usage ();
    endif
  
!   tmp = __img__ (x, y, img);
! 
!   if (nargout > 0)
!     h = tmp;
!   endif
  
  endfunction
--- 39,95 ----
  ## Created: July 1994
  ## Adapted-By: jwe
  
! function retval = image (varargin)
! 
!   if (nargin < 2)
!     print_usage ();
!   elseif (isscalar (varargin{1}) && ishandle (varargin{1}))
!     h = varargin {1};
!     if (! strcmp (get (h, "type"), "axes"))
!       error ("image: expecting first argument to be an axes object");
!     endif
!     oldh = gca ();
!     unwind_protect
!       axes (h);
!       tmp = __image__ (h, varargin{2:end});
!     unwind_protect_cleanup
!       axes (oldh);
!     end_unwind_protect
!   else
!     tmp = __image__ (gca (), varargin{:});
!   endif
! 
!   if (nargout > 0)
!     retval = tmp;
!   endif
! 
! endfunction
! 
! function h = __image__ (ax, x, y, img)
  
    ## Deprecated zoom.  Remove this hunk of code if old zoom argument
    ## is outmoded.
!   if ((nargin == 3 && isscalar (y)) || nargin == 5)
      warning ("image: zoom argument ignored -- use GUI features");
    endif
!   if (nargin == 5)
!     nargin = 4;
    endif
!   if (nargin == 3 && isscalar (y))
!     nargin = 2;
    endif
  
!   if (nargin == 1)
      ## Load Bobbie Jo Richardson (Born 3/16/94)
      img = loadimage ("default.img");
      x = y = [];
!   elseif (nargin == 2)
      img = x;
      x = y = [];
!   elseif (nargin == 3 || nargin > 4)
      print_usage ();
    endif
  
!   h = __img__ (x, y, img);
  
  endfunction
*** ./scripts/plot/__go_draw_axes__.m.orig16    2007-11-20 11:36:45.987914298 
+0100
--- ./scripts/plot/__go_draw_axes__.m   2007-11-20 12:34:21.250892108 +0100
***************
*** 280,285 ****
--- 280,289 ----
              imagetype = "rgbimage";
            else
              data{data_idx} = img_data(:);
+             if (cautoscale)
+               [cmin, cmax, cminp] = get_data_limits (cmin, cmax, cminp, 
+                                                      data{data_idx});
+             endif
              format = "1";
              imagetype = "image";
  
2007-11-20  David Bateman  <address@hidden>

        * plot/imagesc.m, plot/image.m: Scale colormap to image and not
        the reverse. Allow an axes handle to be passed and/or returned.
        * plot/__go_draw_axse__.m: Autoscale colormap for images.

reply via email to

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