Index: /branches/eam_rel9_b0/psLib/src/mathtypes/psImage.c
===================================================================
--- /branches/eam_rel9_b0/psLib/src/mathtypes/psImage.c	(revision 5733)
+++ /branches/eam_rel9_b0/psLib/src/mathtypes/psImage.c	(revision 5734)
@@ -9,6 +9,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.92 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-12-05 22:14:27 $
+ *  @version $Revision: 1.92.4.1 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-12-07 20:55:18 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -163,43 +163,40 @@
 
 // set actual region based on image parameters:
-// compensate for negative upper limits
-// XXX this is inconsistent: the coordindates should always be in the parent
-//     frame, which means the negative values should subtract from Nx,Ny of
-//     the parent, not the child.  but, we don't carry the dimensions of the
-//     parent in the psImage container.  for now, us the child Nx,Ny
-// force range to be on this subimage
-// XXX EAM : this needs to be changes to use psRegion rather than psRegion*
+// - compensate for negative upper limits
+// - force range to be on this image
+// - saturate on upper and lower limits of image
+// - flip x0,x1 if x0>x1
+// - flip y0,y1 if y0>y1
+// psRegion in refers to coordinates in the
 psRegion psRegionForImage(psImage *image,
                           psRegion in)
 {
 
-    // x0,y0, x1,y1 are in *parent* units
-    //    PS_ASSERT_PTR_NON_NULL(in, NULL);
-    /*
-        if( in == NULL ) {
-            psError(PS_ERR_BAD_PARAMETER_NULL, true, "Unallowable operation.  psRegion in is NULL.");
-            return in;
-        }
-    */    /*    if (out == NULL) {
-        //    out = psRegionAlloc(in->x0, in->x1, in->y0, in->y1);
-            *out = psRegionSet(in->x0, in->x1, in->y0, in->y1);
-            } else {
-            *out = *in;
-            }
-        */    // XXX these are probably wrong (see above)
-    if (in.x1 <= 0) {
-        in.x1 = image->col0 + image->numCols + in.x1;
-    }
-    if (in.y1 <= 0) {
-        in.y1 = image->row0 + image->numRows + in.y1;
-    }
-
-    // force the lower-limits to be on the child
-    in.x0 = PS_MAX(image->col0, in.x0);
-    in.y0 = PS_MAX(image->row0, in.y0);
-
-    // force the upper-limits to be on the child
-    in.x1 = PS_MIN(image->col0 + image->numCols, in.x1);
-    in.y1 = PS_MIN(image->row0 + image->numRows, in.y1);
+    if (image == NULL) {
+        return in;
+    }
+
+    // convert non-positive upper-limits
+    in.x1 = (in.x1 <= 0) ? (image->numCols + in.x1) : in.x1;
+    in.y1 = (in.y1 <= 0) ? (image->numRows + in.y1) : in.y1;
+
+    // force the upper-limits to be on the image
+    in.x1 = PS_MIN(image->numCols, in.x1);
+    in.y1 = PS_MIN(image->numRows, in.y1);
+
+    // force the lower-limits to be on the image
+    in.x0 = PS_MAX(0, in.x0);
+    in.y0 = PS_MAX(0, in.y0);
+    in.x0 = PS_MIN(image->numCols, in.x0);
+    in.y0 = PS_MIN(image->numRows, in.y0);
+
+    // flip start and end if out of order
+    if (in.x0 > in.x1) {
+        PS_SWAP (in.x0, in.x1);
+    }
+    if (in.y0 > in.y1) {
+        PS_SWAP (in.y0, in.y1);
+    }
+
     return (in);
 }
