Index: trunk/psLib/src/imageops/psImagePixelExtract.c
===================================================================
--- trunk/psLib/src/imageops/psImagePixelExtract.c	(revision 6631)
+++ trunk/psLib/src/imageops/psImagePixelExtract.c	(revision 6750)
@@ -8,6 +8,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.13 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-03-17 20:38:19 $
+ *  @version $Revision: 1.14 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-04-01 02:43:57 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -57,15 +57,37 @@
         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 (input->col0 < 0) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "psImage input is invalid.  col0 cannot be negative.\n");
+        psFree(out);
+        return NULL;
+    }
+    if (input->row0 < 0) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "psImage input is invalid.  row0 cannot be negative.\n");
+        psFree(out);
+        return NULL;
+    }
+    if (input->numCols < 1) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "psImage input is invalid.  numCols must be greater than 0.\n");
+        psFree(out);
+        return NULL;
+    }
+    if (input->numRows < 1) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "psImage input is invalid.  numRows must be greater than 0.\n");
+        psFree(out);
+        return 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");
+        psFree(out);
         return NULL;
     } 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");
+        psFree(out);
         return NULL;
     } else if ( row < 0 ) {
@@ -74,4 +96,5 @@
             psError(PS_ERR_BAD_PARAMETER_VALUE, true,
                     "Specified row number is out of range for specified image.\n");
+            psFree(out);
             return NULL;
         }
@@ -164,15 +187,37 @@
         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 (input->col0 < 0) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "psImage input is invalid.  col0 cannot be negative.\n");
+        psFree(out);
+        return NULL;
+    }
+    if (input->row0 < 0) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "psImage input is invalid.  row0 cannot be negative.\n");
+        psFree(out);
+        return NULL;
+    }
+    if (input->numCols < 1) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "psImage input is invalid.  numCols must be greater than 0.\n");
+        psFree(out);
+        return NULL;
+    }
+    if (input->numRows < 1) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "psImage input is invalid.  numRows must be greater than 0.\n");
+        psFree(out);
+        return 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");
+        psFree(out);
         return NULL;
     } 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");
+        psFree(out);
         return NULL;
     } else if ( column < 0 ) {
@@ -181,4 +226,5 @@
             psError(PS_ERR_BAD_PARAMETER_VALUE, true,
                     "Specified column number is out of range for specified image.\n");
+            psFree(out);
             return NULL;
         }
@@ -263,17 +309,37 @@
         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 (input->col0 < 0) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "psImage input is invalid.  col0 cannot be negative.\n");
+        psFree(out);
+        return NULL;
+    }
+    if (input->row0 < 0) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "psImage input is invalid.  row0 cannot be negative.\n");
+        psFree(out);
+        return NULL;
+    }
+    if (input->numCols < 1) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "psImage input is invalid.  numCols must be greater than 0.\n");
+        psFree(out);
+        return NULL;
+    }
+    if (input->numRows < 1) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "psImage input is invalid.  numRows must be greater than 0.\n");
+        psFree(out);
+        return NULL;
+    }
     /*
         if (col1 < 1) {
             col1 += input->numCols;
         }
-     
+
         if (row1 < 1) {
             row1 += input->numRows;
         }
-     
+
         if (    col0 < 0 ||
                 row0 < 0 ||
@@ -296,5 +362,5 @@
         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);
+                region.x0, input->col0, input->col0+input->numCols-1);
         return NULL;
     }
@@ -305,5 +371,5 @@
         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);
+                region.y0, input->row0, input->row0+input->numRows-1);
         return NULL;
     }
@@ -315,5 +381,5 @@
             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);
+                    region.x1, col1+input->col0, input->col0, input->col0+input->numCols-1);
             return NULL;
         }
@@ -323,5 +389,5 @@
         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);
+                region.x1, col1, input->col0, input->col0+input->numCols-1);
         return NULL;
     }
@@ -332,5 +398,5 @@
             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);
+                    region.y1, row1+input->row0, input->row0, input->row0+input->numRows-1);
             return NULL;
         }
@@ -340,5 +406,5 @@
         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);
+                region.y1, row1, input->row0, input->row0+input->numRows-1);
         return NULL;
     }
@@ -583,8 +649,28 @@
         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 (input->col0 < 0) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "psImage input is invalid.  col0 cannot be negative.\n");
+        psFree(out);
+        return NULL;
+    }
+    if (input->row0 < 0) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "psImage input is invalid.  row0 cannot be negative.\n");
+        psFree(out);
+        return NULL;
+    }
+    if (input->numCols < 1) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "psImage input is invalid.  numCols must be greater than 0.\n");
+        psFree(out);
+        return NULL;
+    }
+    if (input->numRows < 1) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "psImage input is invalid.  numRows must be greater than 0.\n");
+        psFree(out);
+        return NULL;
+    }
     psS32 numCols = input->numCols;
     psS32 numRows = input->numRows;
@@ -598,20 +684,115 @@
     }
 
-
-    float startCol = region.x0;
-    float startRow = region.y0;
-    float endCol = region.x1;
-    float endRow = region.y1;
-    if (startCol < 0 || startCol >= numCols ||
-            startRow < 0 || startRow >= numRows ||
-            endCol < 0 || endCol >= numCols ||
-            endRow < 0 || endRow >= numRows) {
-        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
-                PS_ERRORTEXT_psImage_LINE_NOT_IN_IMAGE,
-                startCol,startRow,endCol,endRow,
-                numCols-1,numRows-1);
-        psFree(out);
-        return NULL;
-    }
+    float col0 = region.x0;
+    float row0 = region.y0;
+    float col1 = region.x1;
+    float row1 = region.y1;
+
+    //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);
+        psFree(out);
+        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);
+        psFree(out);
+        return NULL;
+    }
+    /*
+        //Make sure x1 of region is valid.  If negative, index from tail (if valid).
+        if (col1 < 0) {
+            col1 += input->numCols;
+            if (col1 < 0) {
+                psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                        "Specified psRegion parameter, x1=%f=%f, is out of range [%d,%d].\n",
+                        region.x1, col1+input->col0, input->col0, input->col0+input->numCols);
+                psFree(out);
+                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=%f, is out of range [%d,%d].\n",
+                    region.x1, col1, input->col0, input->col0+input->numCols);
+            psFree(out);
+            return NULL;
+        }
+        //Make sure y1 of region is valid.  If negative, index from tail (if valid).
+        if (row1 < 0) {
+            row1 += input->numRows;
+            if (row1 < 0) {
+                psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                        "Specified psRegion parameter, y1=%f=%f, is out of range [%d,%d].\n",
+                        region.y1, row1+input->row0, input->row0, input->row0+input->numRows);
+                psFree(out);
+                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=%f, is out of range [%d,%d].\n",
+                    region.y1, row1, input->row0, input->row0+input->numRows);
+            psFree(out);
+            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=%f is greater than x1=%f=%f.\n",
+                        region.x0, col0, region.x1, col1);
+            } else {
+                psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                        "Invalid psRegion specified.  y0=%f=%f is greater than y1=%f=%f.\n",
+                        region.y0, row0, region.y1, row1);
+            }
+            psFree(out);
+            return NULL;
+        } else if (col0 == col1 && row0 == row1) {
+            psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                    "Invalid psRegion specified.  Region contains only 1 pixel.\n");
+            psFree(out);
+            return NULL;
+        }
+    */
+    if (col1 < 0 || row1 < 0 || col0 < 0 || row0 < 0 || col0 >= numCols || col1 >= numCols ||
+            row0 >= numRows || row1 >= numRows) {
+        psFree(out);
+        return NULL;
+    }
+    float startCol = col0;
+    float startRow = row0;
+    float endCol = col1;
+    float endRow = row1;
+    /*
+        float startCol = region.x0;
+        float startRow = region.y0;
+        float endCol = region.x1;
+        float endRow = region.y1;
+        if (startCol < 0 || startCol >= numCols ||
+                startRow < 0 || startRow >= numRows ||
+                endCol < 0 || endCol >= numCols ||
+                endRow < 0 || endRow >= numRows) {
+            psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                    PS_ERRORTEXT_psImage_LINE_NOT_IN_IMAGE,
+                    startCol,startRow,endCol,endRow,
+                    numCols-1,numRows-1);
+            psFree(out);
+            return NULL;
+        }
+    */
 
     if (mode < PS_INTERPOLATE_FLAT ) {
