Index: trunk/psLib/src/imageops/psImagePixelExtract.c
===================================================================
--- trunk/psLib/src/imageops/psImagePixelExtract.c	(revision 6484)
+++ trunk/psLib/src/imageops/psImagePixelExtract.c	(revision 6631)
@@ -8,6 +8,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.12 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-02-24 23:43:15 $
+ *  @version $Revision: 1.13 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-03-17 20:38:19 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -51,21 +51,33 @@
                      int row)
 {
-    if (input == NULL) {
-        psError(PS_ERR_BAD_PARAMETER_NULL, true, PS_ERRORTEXT_psImage_IMAGE_NULL);
-        return NULL;
-    }
-    if (row >= input->numRows) {
+    if (input == NULL || input->data.V == NULL) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true,
+                PS_ERRORTEXT_psImage_IMAGE_NULL);
+        psFree(out);
+        return NULL;
+    }
+    PS_ASSERT_INT_NONNEGATIVE(input->col0, NULL);
+    PS_ASSERT_INT_NONNEGATIVE(input->row0, NULL);
+    PS_ASSERT_INT_POSITIVE(input->numCols, NULL);
+    PS_ASSERT_INT_POSITIVE(input->numRows, NULL);
+    if (row >= (input->numRows + input->row0) ) {
         psError(PS_ERR_BAD_PARAMETER_NULL, true,
                 "Specified row number is out of range for specified image.\n");
         return NULL;
-    }
-    if (row < 0) {
+    } else if ( row < input->row0 && row >= 0 ) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "Specified row number is out of range for specified image.\n");
+        return NULL;
+    } else if ( row < 0 ) {
         row += input->numRows;
-        if (row < 0) {
+        if ( row < 0 ) {
             psError(PS_ERR_BAD_PARAMETER_VALUE, true,
                     "Specified row number is out of range for specified image.\n");
             return NULL;
         }
-    }
+    } else {
+        row -= input->row0;
+    }
+
     psVectorRecycle(out, input->numCols, input->type.type);
 
@@ -146,22 +158,32 @@
                      int column)
 {
-    if (input == NULL) {
-        psError(PS_ERR_BAD_PARAMETER_NULL, true, PS_ERRORTEXT_psImage_IMAGE_NULL);
-        return NULL;
-    }
-    if (column >= input->numCols) {
+    if (input == NULL || input->data.V == NULL) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true,
+                PS_ERRORTEXT_psImage_IMAGE_NULL);
+        psFree(out);
+        return NULL;
+    }
+    PS_ASSERT_INT_NONNEGATIVE(input->col0, NULL);
+    PS_ASSERT_INT_NONNEGATIVE(input->row0, NULL);
+    PS_ASSERT_INT_POSITIVE(input->numCols, NULL);
+    PS_ASSERT_INT_POSITIVE(input->numRows, NULL);
+    if (column >= (input->numCols + input->col0) ) {
         psError(PS_ERR_BAD_PARAMETER_NULL, true,
                 "Specified column number is out of range for specified image.\n");
         return NULL;
-    }
-    if (column < 0) {
+    } else if ( column < input->col0 && column >= 0 ) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "Specified column number is out of range for specified image.\n");
+        return NULL;
+    } else if ( column < 0 ) {
         column += input->numCols;
-        if (column < 0) {
+        if ( column < 0 ) {
             psError(PS_ERR_BAD_PARAMETER_VALUE, true,
                     "Specified column number is out of range for specified image.\n");
             return NULL;
         }
-    }
-
+    } else {
+        column -= input->col0;
+    }
 
     psVectorRecycle(out, input->numRows, input->type.type);
@@ -213,5 +235,4 @@
 
 }
-
 
 psVector* psImageSlice(psVector* out,
@@ -242,24 +263,99 @@
         return NULL;
     }
-
-    if (col1 < 1) {
+    PS_ASSERT_INT_NONNEGATIVE(input->col0, NULL);
+    PS_ASSERT_INT_NONNEGATIVE(input->row0, NULL);
+    PS_ASSERT_INT_POSITIVE(input->numCols, NULL);
+    PS_ASSERT_INT_POSITIVE(input->numRows, NULL);
+    /*
+        if (col1 < 1) {
+            col1 += input->numCols;
+        }
+     
+        if (row1 < 1) {
+            row1 += input->numRows;
+        }
+     
+        if (    col0 < 0 ||
+                row0 < 0 ||
+                col1 > input->numCols ||
+                row1 > input->numRows ||
+                col0 >= col1 ||
+                row0 >= row1) {
+            psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                    PS_ERRORTEXT_psImage_SUBSET_RANGE_INVALID,
+                    col0, col1, row0, row1,
+                    input->numCols, input->numRows);
+            psFree(out);
+            return NULL;
+        }
+    */
+    //Make sure x0 of region is inside image.  If so, set col0 to corresponding index number.
+    if (col0 >= input->col0 && col0 < (input->col0 + input->numCols) ) {
+        col0 -= input->col0;
+    } else {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "Specified psRegion parameter, x0=%f, is out of range [%d,%d].\n",
+                region.x0, input->col0, input->col0+input->numCols);
+        return NULL;
+    }
+    //Make sure y0 of region is inside image.  If so, set row0 to corresponding index number.
+    if (row0 >= input->row0 && row0 < (input->row0 + input->numRows) ) {
+        row0 -= input->row0;
+    } else {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "Specified psRegion parameter, y0=%f, is out of range [%d,%d].\n",
+                region.y0, input->row0, input->row0+input->numRows);
+        return NULL;
+    }
+
+    //Make sure x1 of region is valid.  If negative, index from tail (if valid).
+    if (col1 < 0) {
         col1 += input->numCols;
-    }
-
-    if (row1 < 1) {
+        if (col1 < 0) {
+            psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                    "Specified psRegion parameter, x1=%f=%d, is out of range [%d,%d].\n",
+                    region.x1, col1+input->col0, input->col0, input->col0+input->numCols);
+            return NULL;
+        }
+    } else if (col1 >= input->col0 && col1 < (input->col0 + input->numCols) ) {
+        col1 -= input->col0;
+    } else {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "Specified psRegion parameter, x1=%f=%d, is out of range [%d,%d].\n",
+                region.x1, col1, input->col0, input->col0+input->numCols);
+        return NULL;
+    }
+    //Make sure y1 of region is valid.  If negative, index from tail (if valid).
+    if (row1 < 0) {
         row1 += input->numRows;
-    }
-
-    if (    col0 < 0 ||
-            row0 < 0 ||
-            col1 > input->numCols ||
-            row1 > input->numRows ||
-            col0 >= col1 ||
-            row0 >= row1) {
-        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
-                PS_ERRORTEXT_psImage_SUBSET_RANGE_INVALID,
-                col0, col1, row0, row1,
-                input->numCols, input->numRows);
-        psFree(out);
+        if (row1 < 0) {
+            psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                    "Specified psRegion parameter, y1=%f=%d, is out of range [%d,%d].\n",
+                    region.y1, row1+input->row0, input->row0, input->row0+input->numRows);
+            return NULL;
+        }
+    } else if (row1 >= input->row0 && row1 < (input->row0 + input->numRows) ) {
+        row1 -= input->row0;
+    } else {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "Specified psRegion parameter, y1=%f=%d, is out of range [%d,%d].\n",
+                region.y1, row1, input->row0, input->row0+input->numRows);
+        return NULL;
+    }
+    //Now make sure that the region makes sense.
+    if (col0 > col1 || row0 > row1) {
+        if (col0 > col1) {
+            psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                    "Invalid psRegion specified.  x0=%f=%d is greater than x1=%f=%d.\n",
+                    region.x0, col0, region.x1, col1);
+        } else {
+            psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                    "Invalid psRegion specified.  y0=%f=%d is greater than y1=%f=%d.\n",
+                    region.y0, row0, region.y1, row1);
+        }
+        return NULL;
+    } else if (col0 == col1 && row0 == row1) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "Invalid psRegion specified.  Region contains only 1 pixel.\n");
         return NULL;
     }
@@ -487,4 +583,8 @@
         return NULL;
     }
+    //    PS_ASSERT_INT_NONNEGATIVE(input->col0, NULL);
+    //    PS_ASSERT_INT_NONNEGATIVE(input->row0, NULL);
+    //    PS_ASSERT_INT_POSITIVE(input->numCols, NULL);
+    //    PS_ASSERT_INT_POSITIVE(input->numRows, NULL);
     psS32 numCols = input->numCols;
     psS32 numRows = input->numRows;
@@ -497,4 +597,5 @@
         return NULL;
     }
+
 
     float startCol = region.x0;
