changeset 7074:87151f298e6a

[project @ 2007-10-26 17:57:34 by jwe]
author jwe
date Fri, 26 Oct 2007 17:57:34 +0000
parents 66c6cab344f7
children 1558d3dab722
files scripts/ChangeLog scripts/image/image.m scripts/image/imshow.m
diffstat 3 files changed, 105 insertions(+), 79 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Fri Oct 26 16:09:12 2007 +0000
+++ b/scripts/ChangeLog	Fri Oct 26 17:57:34 2007 +0000
@@ -1,5 +1,8 @@
 2007-10-26  John W. Eaton  <jwe@octave.org>
 
+	* image/imshow.m: Improve compatibility.
+	* image/image.m: Return handle if nargou > 0.
+
 	* pkg/pkg.m: Delete PKG_ADD directive for autoloading packes.
 
 2007-10-25  John W. Eaton  <jwe@octave.org>
--- a/scripts/image/image.m	Fri Oct 26 16:09:12 2007 +0000
+++ b/scripts/image/image.m	Fri Oct 26 17:57:34 2007 +0000
@@ -39,7 +39,7 @@
 ## Created: July 1994
 ## Adapted-By: jwe
 
-function image (x, y, img)
+function h = image (x, y, img)
 
   ## Deprecated zoom.  Remove this hunk of code if old zoom argument
   ## is outmoded.
--- a/scripts/image/imshow.m	Fri Oct 26 16:09:12 2007 +0000
+++ b/scripts/image/imshow.m	Fri Oct 26 17:57:34 2007 +0000
@@ -1,5 +1,5 @@
 ## Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2004, 2005,
-##               2006, 2007 Soren Hauberg
+##               2006, 2007 John W. Eaton
 ## 
 ## This file is part of Octave.
 ##
@@ -21,13 +21,11 @@
 ## @deftypefn {Function File} {} imshow (@var{im})
 ## @deftypefnx {Function File} {} imshow (@var{im}, @var{limits})
 ## @deftypefnx {Function File} {} imshow (@var{im}, @var{map})
-## @deftypefnx {Function File} {} imshow (@var{R}, @var{G}, @var{B}, @dots{})
+## @deftypefnx {Function File} {} imshow (@var{rgb}, @dots{})
 ## @deftypefnx {Function File} {} imshow (@var{filename})
 ## @deftypefnx {Function File} {} imshow (@dots{}, @var{string_param1}, @var{value1}, @dots{})
 ## Display the image @var{im}, where @var{im} can be a 2-dimensional
-## (gray-scale image) or a 3-dimensional (RGB image) matrix. If three matrices
-## of the same size are given as arguments, they will be concatenated into
-## a 3-dimensional (RGB image) matrix.
+## (gray-scale image) or a 3-dimensional (RGB image) matrix.
 ##
 ## If @var{limits} is a 2-element vector @code{[@var{low}, @var{high}]},
 ## the image is shown using a display range between @var{low} and
@@ -50,75 +48,87 @@
 ## @seealso{image, imagesc, colormap, gray2ind, rgb2ind}
 ## @end deftypefn
 
+## Author: Stefan van der Walt  <stefan@sun.ac.za>
 ## Author: Soren Hauberg <hauberg at gmail dot com>
 ## Adapted-By: jwe
 
-function imshow (im, varargin)
+function h = imshow (im, varargin)
 
   if (nargin == 0)
     print_usage ();
   endif
 
+  display_range = [];
+  true_color = false;
+  indexed = false;
+
   ## Get the image.
   if (ischar (im))
-    im = loadimage (im); # It would be better to use imread from octave-forge
-  elseif (! ismatrix (im))
+    ## Eventually, this should be imread.
+    [im, map] = loadimage (im);
+    indexed = true;
+    colormap (map);
+  endif
+
+  if (! (isnumeric (im) && (ndims (im) == 2 || ndims (im) == 3)))
     error ("imshow: first argument must be an image or the filename of an image");
   endif
-  
-  ## Is the function called with 3 matrices (i.e., imshow (R, G, B))?
-  if (nargin >= 3
-      && ndims (im) == 2
-      && ndims (varargin{1}) == 2
-      && ndims (varargin{2}) == 2
-      && size_equal (im, varargin{1})
-      && size_equal (im, varargin{2}))
-    im(:,:,3) = varargin{2};
-    im(:,:,2) = varargin{1};
-    varargin(1:2) = [];
+
+  if (ndims (im) == 2)
+    if (! indexed)
+      colormap (gray ());
+    endif
+  elseif (size (im, 3) == 3)
+    if (ismember (class (im), {"uint8", "uint16", "double", "single"}))
+      true_color = true;
+    else
+      error ("imshow: color image must be uint8, uint16, double, or single");
+    endif
+  else
+    error ("imshow: expecting MxN or MxNx3 matrix for image");
   endif
 
+  narg = 1;
+  while (narg <= numel (varargin))
+    arg = varargin{narg++};
+    if (isnumeric (arg))
+      if (numel (arg) == 2)
+	display_range = arg;
+      elseif (columns (arg) == 3)
+	indexed = true;
+	colormap (arg);
+      elseif (! isempty (arg))
+	error ("imshow: argument number %d is invalid", narg+1);
+      endif
+    elseif (ischar (arg))
+      switch (arg)
+	case "displayrange";
+	  displayrange = varargin{narg++};
+	case {"truesize", "initialmagnification"}
+	  warning ("image: zoom argument ignored -- use GUI features");
+	otherwise
+	  warning ("imshow: unrecognized property %s", arg);
+	  narg++;
+      endswitch
+    else
+      error ("imshow: argument number %d is invalid", narg+1);
+    endif
+  endwhile
+
   ## Set default display range.
-  switch class (im)
-    case {"uint8"}
-      display_range = [0, 255];
-    case {"uint16"}
-      display_range = [0, 65535];
-    case {"double", "single", "logical"}
-      display_range = [0, 1];
-    otherwise
-      error ("imshow: currently only images whos class is uint8, uint16, logical, or double are supported");
-  endswitch
-
-  ## Set other default parameters.
-  isindexed = false;
-  initial_magnification = 100;
-  color_map = colormap ();
-  
-  ## Handle the rest of the arguments.
-  narg = 1;
-  while (narg <= length (varargin))
-    arg = varargin{narg};
-    if (ismatrix (arg) && size (arg, 2) == 3)
-      color_map = arg;
-      isindexed = true;
-    elseif (ismatrix (arg) && numel (arg) == 2)
-      display_range = arg;
-    elseif (isempty (arg))
-      display_range = [min(im(:)), max(im(:))];
-    elseif (ischar (arg) && strcmpi (arg, "displayrange"))
-      narg++;
-      display_range = varargin{narg};
-    elseif (ischar (arg) &&
-	    (strcmpi (arg, "truesize") ||
-             strcmpi (arg, "initialmagnification")))
-      narg++;
-      warning ("image: zoom argument ignored -- use GUI features");
-    else
-      warning ("imshow: input argument number %d is unsupported", narg) 
-    endif
-    narg++;
-  endwhile
+  if (true_color || isempty (display_range))
+    display_range = [min(im(:)), max(im(:))];
+  else
+    t = class (im);
+    switch (t)
+      case {"double", "single", "logical"}
+	display_range = [0, 1];
+      case {"int8", "int16", "int32", "uint8", "uint16", "uint32"}
+	display_range = [intmin(t), intmax(t)];
+      otherwise
+	error ("imshow: invalid data type for image");
+    endswitch
+  endif
 
   ## Check for complex images.
   if (iscomplex (im))
@@ -129,42 +139,46 @@
   nans = isnan (im(:));
   if (any (nans))
     warning ("Octave:imshow-NaN",
-	     "imshow: pixel with NaN or NA values are set to zero");
+	     "imshow: pixels with NaN or NA values are set to minimum pixel value");
     im(nans) = display_range(1);
   endif
 
+  ## This is for compatibility.
+  if (ismember (class (im), {"int8", "int16", "uint32", "int32", "single"}))
+    im = double (im);
+  endif
+
   ## Scale the image to the interval [0, 1] according to display_range.
-  if (! isindexed)
+  if (! indexed || islogical (im))
     low = display_range(1);
     high = display_range(2);
-    im = (double (im) - low)/(high-low);
+    im = (im-low)/(high-low);
     im(im < 0) = 0;
     im(im > 1) = 1;
   endif
 
-  dim = ndims (im);
-  if (dim == 2)
-    im = round ((size (color_map, 1) - 1) * im);
-    image (im);
-    colormap (color_map);
-  elseif (dim == 3 && size (im, 3) == 3)
-    __img__ ([] , [], im);
-    ## FIXME -- needed anymore for a special case?
-    ## Convert to indexed image.
-    ## [im, color_map] = rgb2ind (im);
+  if (true_color)
+    tmp = __img__ ([] , [], im);
   else
-    error ("imshow: input image must be a 2D or 3D matrix");
+    tmp = image (round ((rows (colormap ()) - 1) * im));
   endif
-  
+
+  if (nargout > 0)
+    h = tmp;
+  endif
+
 endfunction
 
 %!error imshow ()                           # no arguments
 %!error imshow ({"cell"})                   # No image or filename given
-%!error imshow (int8(1))                    # Unsupported image class
 %!error imshow (ones(4,4,4))                # Too many dimensions in image
 
 %!demo
-%!  imshow (loadimage ("default.img"));
+%!  imshow ("default.img");
+
+%!demo
+%!  imshow ("default.img");
+%!  colormap ("autumn");
 
 %!demo
 %!  [I, M] = loadimage ("default.img");
@@ -175,5 +189,14 @@
 %!  imshow (cat(3, I, I*0.5, I*0.8));
 
 %!demo
-%!  I = loadimage("default.img");
-%!  imshow(I, I, I);
+%!  imshow (rand (100, 100));
+
+%!demo
+%!  imshow (rand (100, 100, 3));
+
+%!demo
+%!  imshow (100*rand (100, 100, 3));
+
+%!demo
+%!  imshow (rand (100, 100));
+%!  colormap (jet);