Index: /branches/rel10_ifa/psLib/src/math/psRegion.c
===================================================================
--- /branches/rel10_ifa/psLib/src/math/psRegion.c	(revision 6730)
+++ /branches/rel10_ifa/psLib/src/math/psRegion.c	(revision 6730)
@@ -0,0 +1,85 @@
+#include <stdio.h>
+#include <math.h>
+#include "psMemory.h"
+#include "psError.h"
+#include "psErrorText.h"
+#include "psRegion.h"
+
+psRegion *psRegionAlloc(float x0,
+                        float x1,
+                        float y0,
+                        float y1)
+{
+    psRegion *region = psAlloc(sizeof(psRegion)); // New region, to be returned
+    // No complex structures, so no special deallocator
+    *region = psRegionSet(x0, x1, y0, y1);
+    return region;
+}
+
+psRegion psRegionSet(float x0,
+                     float x1,
+                     float y0,
+                     float y1)
+{
+    psRegion out;
+
+    out.x0 = x0;
+    out.y0 = y0;
+    out.x1 = x1;
+    out.y1 = y1;
+
+    return out;
+}
+
+psRegion psRegionFromString(const char* region)
+{
+    psS32 col0;
+    psS32 col1;
+    psS32 row0;
+    psS32 row1;
+
+    // section should be of the form '[col0:col1,row0:row1]'
+    if (region == NULL) {
+        return psRegionSet(0,0,0,0);
+    }
+
+    if (sscanf(region,"[%d:%d,%d:%d]",&col0,&col1,&row0,&row1) < 4) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true,
+                PS_ERRORTEXT_psImage_SUBSECTION_INVALID,
+                region);
+        return psRegionSet(NAN,NAN,NAN,NAN);
+    }
+
+    if (col0 > col1 || row0 > row1) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                PS_ERRORTEXT_psImage_SUBSET_RANGE_MALFORMED,
+                col0,col1,row0,row1);
+        return psRegionSet(NAN,NAN,NAN,NAN);
+    }
+
+    return psRegionSet(col0-1,col1,row0-1,row1);
+}
+
+psString psRegionToString(const psRegion region)
+{
+    char tmpText[256]; // big enough to store any region as text
+
+    snprintf(tmpText,256,"[%g:%g,%g:%g]",
+             region.x0+1, region.x1,
+             region.y0+1, region.y1);
+
+    return psStringCopy(tmpText);
+}
+
+
+// define a square region centered on the given coordinate
+psRegion psRegionForSquare(double x,
+                           double y,
+                           double radius)
+{
+    psRegion region;
+    region = psRegionSet (x - radius, x + radius + 1,
+                          y - radius, y + radius + 1);
+    return (region);
+}
+
Index: /branches/rel10_ifa/psLib/src/math/psRegion.h
===================================================================
--- /branches/rel10_ifa/psLib/src/math/psRegion.h	(revision 6730)
+++ /branches/rel10_ifa/psLib/src/math/psRegion.h	(revision 6730)
@@ -0,0 +1,73 @@
+#ifndef PS_REGION_H
+#define PS_REGION_H
+
+#include "psString.h"
+
+/** Basic image region structure.
+ *
+ * Struct for specifying a rectangular area in an image.
+ *
+ */
+typedef struct
+{
+    float x0;                         ///< the first column of the region.
+    float x1;                         ///< the last column of the region.
+    float y0;                         ///< the first row of the region.
+    float y1;                         ///< the last row of the region.
+}
+psRegion;
+
+/** Create a pointer to a psRegion, with associated psMemBlock.
+ *
+ * @return psRegion* : a new psRegion.
+ */
+psRegion *psRegionAlloc(float x0,       ///< the first column of the region.
+                        float x1,       ///< the last column of the region + 1.
+                        float y0,       ///< the first row of the region.
+                        float y1        ///< the last row of the region + 1.
+                       );
+
+/** Create a psRegion with the specified attributes.
+ *
+ *  @return psRegion : a cooresponding psRegion.
+ */
+psRegion psRegionSet(
+    float x0,                          ///< the first column of the region.
+    float x1,                          ///< the last column of the region + 1.
+    float y0,                          ///< the first row of the region.
+    float y1                           ///< the last row of the region + 1.
+);
+
+/** Create a psRegion with the attribute values given as a string.
+ *
+ *  Create a psRegion with the attribute values given as a string.  The format
+ *  shall be of the standard IRAF form '[x0:x1,y0:y1]'
+ *
+ *  @return psRegion:  A new psRegion struct, or NULL is not successful.
+ */
+psRegion psRegionFromString(
+    const char* region                 ///< image rectangular region in the form '[x0:x1,y0:y1]'
+);
+
+/** Create a string of the standard IRAF form '[x0:x1,y0:y1]' from a psRegion.
+ *
+ *  @return psString:  A new string representing the psRegion as text, or NULL
+ *                  is not successful.
+ */
+psString psRegionToString(
+    const psRegion region              ///< the psRegion to convert to a string
+);
+
+/** Defines a region corresponding to the square with center at coordinate x,y
+ *  and with coderadius.  The width of the square is 2radius + 1.
+ *
+ *  @return psRegion:       the newly defined psRegion.
+ */
+psRegion psRegionForSquare(
+    double x,                           ///< x coordinate at square-center
+    double y,                           ///< y coordinate at square-center
+    double radius                       ///< radius of square
+);
+
+
+#endif
Index: /branches/rel10_ifa/psLib/src/math/psRegionForImage.c
===================================================================
--- /branches/rel10_ifa/psLib/src/math/psRegionForImage.c	(revision 6730)
+++ /branches/rel10_ifa/psLib/src/math/psRegionForImage.c	(revision 6730)
@@ -0,0 +1,47 @@
+#include <stdio.h>
+#include "psRegion.h"
+#include "psImage.h"
+#include "psRegionForImage.h"
+
+// set actual region based on image parameters:
+// - 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)
+{
+
+    if (image == NULL) {
+        return in;
+    }
+
+    // convert non-positive upper-limits
+    // XXX note that the upper limit in these cases is defined relative to the subimage
+    // also note that truncation limits to the valid subimage pixels
+    in.x1 = (in.x1 <= 0) ? (image->col0 + image->numCols + in.x1) : in.x1;
+    in.y1 = (in.y1 <= 0) ? (image->row0 + image->numRows + in.y1) : in.y1;
+
+    // force the upper-limits to be on the image
+    in.x1 = PS_MIN(image->col0 + image->numCols, in.x1);
+    in.y1 = PS_MIN(image->row0 + image->numRows, in.y1);
+
+    // force the lower-limits to be on the image
+    in.x0 = PS_MAX(image->col0, in.x0);
+    in.y0 = PS_MAX(image->row0, in.y0);
+    in.x0 = PS_MIN(image->col0 + image->numCols, in.x0);
+    in.y0 = PS_MIN(image->row0 + 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);
+}
+
Index: /branches/rel10_ifa/psLib/src/math/psRegionForImage.h
===================================================================
--- /branches/rel10_ifa/psLib/src/math/psRegionForImage.h	(revision 6730)
+++ /branches/rel10_ifa/psLib/src/math/psRegionForImage.h	(revision 6730)
@@ -0,0 +1,26 @@
+#ifndef PS_REGION_FOR_IMAGE_H
+#define PS_REGION_FOR_IMAGE_H
+
+
+/** Sets an actual region based on image parameters.
+ *
+ *  An image region defined with negative upper limits may be rationalized for the bounds of a
+ *  specific image with psRegionForImage.  The output of this function is a region with negative
+ *  upper limits replaced by their corrected value appropriate to the given image.  In addition,
+ *  the lower and upper limits are foced to lie within the bounds of the image.  If the lower-
+ *  limit coordinates are lewss than the lower bound of the image, they are limited to the lower
+ *  bound of the image.  Conversely, if the upper-limit coordinates are greater than the upper
+ *  bound of the image, they are truncated to define only valid pixels.  If the lower-limit
+ *  coordinates are greater than the upper bounds of the image, or the upper-limit coordinates
+ *  are less than the lower bounds of the image, the coordinates should saturate on those limits.
+ *
+ *  @return psRegion:       A region with negative upper limits replaced by the corrected
+ */
+psRegion psRegionForImage(
+    psImage *image,                    ///< the image for which the region is to be set
+    psRegion in                        ///< the image region limits
+);
+
+
+
+#endif
