changeset 11076:b748b86cb8c1

fix images with width or height of 1
author Shai Ayal <shaiay@users.sourceforge.net>
date Mon, 04 Oct 2010 21:35:02 +0200
parents 4e31d44a9763
children 5dd5df43d392
files scripts/image/image.m src/ChangeLog src/graphics.cc src/graphics.h.in
diffstat 4 files changed, 85 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/image/image.m
+++ b/scripts/image/image.m
@@ -119,32 +119,14 @@
   xdata = [x(1), x(end)];
   ydata = [y(1), y(end)];
 
-  c = size (img, 2);
-  if (c > 1)
-    xlim = 0.5 * (diff (xdata) * c / (c - 1) * [-1, 1] + sum (xdata));
-  elseif (numel (unique (x)) > 1)
-    xlim = xdata;
-  elseif (x(1) !=  0)
-    xlim = [0, x];
-  else
-    xlim = [0, 1];
-  endif
-
-  r = size (img, 1);
-  if (r > 1)
-    ylim = 0.5 * (diff (ydata) * r / (r - 1) * [-1, 1] + sum (ydata));
-  elseif (numel (unique (y)) > 1)
-    ylim = ydata;
-  elseif (y(1) !=  0)
-    ylim = [0, y];
-  else
-    ylim = [0, 1];
-  endif
-
   ca = gca ();
 
   tmp = __go_image__ (ca, "cdata", img, "xdata", xdata, "ydata", ydata,
-                      "cdatamapping", "direct", varargin {:});
+                    "cdatamapping", "direct", varargin {:});
+
+  px = __image_pixel_size__ (tmp);
+  xlim = xdata + [-px(1), px(1)];
+  ylim = ydata + [-px(2), px(2)];
 
   ## FIXME -- how can we do this and also get the {x,y}limmode
   ## properties to remain "auto"?  I suppose this adjustment should
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,14 @@
+2010-10-04  Shai Ayal  <shaiay@users.sourceforge.net>
+
+	* graphics.h.in (axis::properties::pixel_size): New function.
+	(axis::properties::pixel_xsize,axis::properties::pixel_ysize):
+	New functions, use axis::properties::pixel_size
+	(axis::properties::update_ydata,axis::properties::update_xdata):
+	Use axis::properties::pixel_size functions
+
+	* graphics.cc (__image_pixel_size__): New function uses
+	axis::properties::pixel_size functions
+
 2010-10-01  John W. Eaton  <jwe@octave.org>
 
 	* graphics.h.in (class caseless_str): Move to
--- a/src/graphics.cc
+++ b/src/graphics.cc
@@ -6227,6 +6227,45 @@
   return retval;
 }
 
+DEFUN (__image_pixel_size__, args, ,
+   "-*- texinfo -*-\n\
+@deftypefn  {Built-in Function} {@var{px},@var{py}} __image_pixel_size__ (@var{h})\n\
+Internal function: returns the pixel size of the image in normalized units.\n\
+@end deftypefn")
+{
+  octave_value retval;
+
+  int nargin = args.length ();
+
+  if (nargin == 1)
+    {
+      double h = args(0).double_value ();
+
+      if (! error_state)
+        {
+          graphics_object fobj = gh_manager::get_object (h);
+          if (fobj &&  fobj.isa ("image"))
+            {
+              image::properties& ip =
+                dynamic_cast<image::properties&> (fobj.get_properties ());
+              
+              Matrix dp =  Matrix (1, 2, 0);
+              dp(0, 0) = ip.pixel_xsize ();
+              dp(0, 1) = ip.pixel_ysize ();
+              retval = dp;
+            }
+          else
+            error ("__image_pixel_size__: object is not an image");
+        }
+      else
+        error ("__image_pixel_size__: argument is not a handle");
+    }
+  else
+    print_usage ();
+
+  return retval;
+}
+
 DEFUN (available_backends, , ,
    "-*- texinfo -*-\n\
 @deftypefn {Built-in Function} {} available_backends ()\n\
--- a/src/graphics.h.in
+++ b/src/graphics.h.in
@@ -3435,9 +3435,8 @@
   private:
     void update_xdata (void)
     { 
-      octave_idx_type iw = (get_cdata ().dims ())(1) - 1;
       Matrix limits = xdata.get_limits ();
-      float dp = (limits(1) - limits(0))/(2*iw);
+      float dp = pixel_xsize ();
 
       limits(0) = limits(0) - dp;
       limits(1) = limits(1) + dp;
@@ -3446,9 +3445,8 @@
 
     void update_ydata (void)
     {
-      octave_idx_type ih = (get_cdata().dims ())(0) - 1;
       Matrix limits = ydata.get_limits ();
-      float dp = (limits(1) - limits(0))/(2*ih);
+      float dp = pixel_ysize ();
 
       limits(0) = limits(0) - dp;
       limits(1) = limits(1) + dp;
@@ -3462,6 +3460,34 @@
         else
           clim = cdata.get_limits ();
       }
+
+    float pixel_size (octave_idx_type dim, const Matrix limits)
+    {
+      octave_idx_type l = dim - 1;
+      float dp;
+
+      if (l > 0)
+        dp = (limits(1) - limits(0))/(2*l);
+      else
+        {
+          if (limits(1) == limits(2))
+            dp = 0.5;
+          else
+            dp = (limits(1) - limits(0))/2;
+        }
+      return dp;
+    }
+
+  public:
+    float  pixel_xsize (void)
+    {
+      return pixel_size ((get_cdata ().dims ())(1), xdata.get_limits ());
+    }
+
+    float pixel_ysize (void)
+    {
+      return pixel_size ((get_cdata ().dims ())(0), ydata.get_limits ());
+    }
   };
 
 private: