# HG changeset patch # User Shai Ayal # Date 1286220902 -7200 # Node ID b748b86cb8c1950e2adb8c9ef7eca5c7b2cc59e7 # Parent 4e31d44a9763a0967b05dc0214d868f5f59feeb0 fix images with width or height of 1 diff -r 4e31d44a9763 -r b748b86cb8c1 scripts/image/image.m --- a/scripts/image/image.m Fri Oct 01 14:18:37 2010 -0400 +++ b/scripts/image/image.m Mon Oct 04 21:35:02 2010 +0200 @@ -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 diff -r 4e31d44a9763 -r b748b86cb8c1 src/ChangeLog --- a/src/ChangeLog Fri Oct 01 14:18:37 2010 -0400 +++ b/src/ChangeLog Mon Oct 04 21:35:02 2010 +0200 @@ -1,3 +1,14 @@ +2010-10-04 Shai Ayal + + * 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 * graphics.h.in (class caseless_str): Move to diff -r 4e31d44a9763 -r b748b86cb8c1 src/graphics.cc --- a/src/graphics.cc Fri Oct 01 14:18:37 2010 -0400 +++ b/src/graphics.cc Mon Oct 04 21:35:02 2010 +0200 @@ -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 (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\ diff -r 4e31d44a9763 -r b748b86cb8c1 src/graphics.h.in --- a/src/graphics.h.in Fri Oct 01 14:18:37 2010 -0400 +++ b/src/graphics.h.in Mon Oct 04 21:35:02 2010 +0200 @@ -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: