Index: trunk/psLib/src/image/psImageExtraction.c
===================================================================
--- trunk/psLib/src/image/psImageExtraction.c	(revision 1920)
+++ trunk/psLib/src/image/psImageExtraction.c	(revision 1924)
@@ -9,6 +9,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-09-28 23:26:48 $
+ *  @version $Revision: 1.18 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-09-29 01:10:27 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -146,10 +146,10 @@
                            const char* section)
 {
-    int x1;
-    int x2;
-    int y1;
-    int y2;
-
-    // section should be of the form '[x1:x2,y1:y2]'
+    int col0;
+    int col1;
+    int row0;
+    int row1;
+
+    // section should be of the form '[col0:col1,row0:row1]'
     if (section == NULL) {
         psErrorMsg(PS_ERRORNAME_DOMAIN "psImageSubsection",
@@ -159,5 +159,5 @@
     }
 
-    if (sscanf(section,"[%d:%d,%d:%d]",&x1,&x2,&y1,&y2) < 4) {
+    if (sscanf(section,"[%d:%d,%d:%d]",&col0,&col1,&row0,&row1) < 4) {
         psErrorMsg(PS_ERRORNAME_DOMAIN "psImageSubsection",
                    PS_ERR_BAD_PARAMETER_NULL, true,
@@ -166,8 +166,8 @@
         return NULL;
     }
-    return imageSubset(NULL,image,x1,y1,x2,y2);
+    return imageSubset(NULL,image,col0,row0,col1,row1);
 }
 
-psImage* psImageTrim(psImage* image, int x0, int y0, int x1, int y1)
+psImage* psImageTrim(psImage* image, int col0, int row0, int col1, int row1)
 {
     if (image == NULL || image->data.V == NULL) {
@@ -181,18 +181,20 @@
         return imageSubset(image,
                            (psImage*)image->parent,
-                           x0+image->col0,
-                           y0+image->row0,
-                           x1+image->col0,
-                           y1+image->row0);
-    }
-
-    if (x0 < 0 || x1 < 0 || y0 < 0 || y1 < 0 ||
-            x0 >= image->numCols || x1 >= image->numCols ||
-            y0 >= image->numRows || y1 >= image->numRows ||
-            x0 > x1 || y0 > y1 ) {
+                           col0+image->col0,
+                           row0+image->row0,
+                           col1+image->col0,
+                           row1+image->row0);
+    }
+
+    if (    col0 < 0 ||
+            row0 < 0 ||
+            col1 >= image->numCols ||
+            row1 >= image->numRows ||
+            col0 > col1 ||
+            row0 > row1 ) {
         psErrorMsg(PS_ERRORNAME_DOMAIN "psImageTrim",
                    PS_ERR_BAD_PARAMETER_VALUE, true,
                    PS_ERRORTEXT_psImage_SUBSET_RANGE_INVALID,
-                   x0, x1, y0, y1,
+                   col0, col1, row0, row1,
                    image->numCols, image->numRows);
         return NULL;
@@ -202,10 +204,10 @@
 
     unsigned int elementSize = PSELEMTYPE_SIZEOF(image->type.type);
-    unsigned int numCols = x1-x0+1;
-    unsigned int numRows = y1-y0+1;
+    unsigned int numCols = col1-col0+1;
+    unsigned int numRows = row1-row0+1;
     unsigned int rowSize = elementSize*numCols;
-    unsigned int colOffset = elementSize * x0;
+    unsigned int colOffset = elementSize * col0;
     void* imageData = image->rawDataBuffer;
-    for (int row = y0; row < y1; row++) {
+    for (int row = row0; row < row1; row++) {
         memmove(imageData,image->data.U8[row] + colOffset,rowSize);
         imageData = (psU8*)imageData + rowSize;
@@ -399,8 +401,8 @@
                        const psImage* restrict mask,
                        unsigned int maskVal,
-                       unsigned int col,
-                       unsigned int row,
-                       unsigned int numCols,
-                       unsigned int numRows,
+                       int col0,
+                       int row0,
+                       int col1,
+                       int row1,
                        psImageCutDirection direction,
                        const psStats* stats)
@@ -422,9 +424,23 @@
     }
 
-    if (numRows < 1 || numCols < 1) {
+    if (col1 < 0) {
+        col1 += in->numCols;
+    }
+
+    if (row1 < 0) {
+        row1 += in->numRows;
+    }
+
+    if (    col0 < 0 ||
+            row0 < 0 ||
+            col1 >= in->numCols ||
+            row1 >= in->numRows ||
+            col0 >= col1 ||
+            row0 >= row1) {
         psErrorMsg(PS_ERRORNAME_DOMAIN "psImageSlice",
-                   PS_ERR_BAD_PARAMETER_NULL, true,
-                   PS_ERRORTEXT_psImage_SUBSET_ZERO_SIZE,
-                   col,col+numCols,row,row+numRows);
+                   PS_ERR_BAD_PARAMETER_VALUE, true,
+                   PS_ERRORTEXT_psImage_SUBSET_RANGE_INVALID,
+                   col0, col1, row0, row1,
+                   in->numCols, in->numRows);
         psFree(out);
         return NULL;
@@ -437,19 +453,4 @@
     if (direction == PS_CUT_X_NEG || direction == PS_CUT_Y_NEG) {
         delta = -1;
-    }
-    // if numRows/numCols is negative, invert the
-    // problem to give positive
-    // numRows/numCols (and cut in opposite
-    // direction).
-    if (numRows < 0) {
-        numRows = -numRows;
-        row -= (numRows - 1);
-        delta = -delta;
-    }
-
-    if (numCols < 0) {
-        numCols = -numCols;
-        col -= (numCols - 1);
-        delta = -delta;
     }
 
@@ -474,14 +475,4 @@
     }
 
-    if (row >= inRows || col >= inCols || col + numCols > inCols || row + numRows > inRows) {
-        psErrorMsg(PS_ERRORNAME_DOMAIN "psImageSlice",
-                   PS_ERR_BAD_PARAMETER_VALUE, true,
-                   PS_ERRORTEXT_psImage_SUBSET_RANGE_INVALID,
-                   col, row, col+numCols, row+numRows,
-                   inCols, inRows);
-        psFree(out);
-        return NULL;
-    }
-
     if (stats == NULL) {
         psErrorMsg(PS_ERRORNAME_DOMAIN "psImageSlice",
@@ -505,4 +496,7 @@
     myStats = psAlloc(sizeof(psStats));
     *myStats = *stats;
+
+    int numCols = col1-col0;
+    int numRows = row1-row0;
 
     if (direction == PS_CUT_X_POS || direction == PS_CUT_X_NEG) {
@@ -534,12 +528,12 @@
     case PS_TYPE_##TYPE: { \
             psMaskType* maskVecData = NULL; \
-            for (int c=0;c<numCols;c++) { \
-                ps##TYPE *imgData = in->data.TYPE[row] + col + c; \
+            for (int c=col0;c<col1;c++) { \
+                ps##TYPE *imgData = in->data.TYPE[row0] + c; \
                 ps##TYPE *imgVecData = imgVec->data.TYPE; \
                 if (maskVec != NULL) { \
                     maskVecData = maskVec->data.V; \
-                    maskData = (psMaskType* )(mask->data.V[row]) + col + c; \
+                    maskData = (psMaskType* )(mask->data.V[row0]) + c; \
                 } \
-                for (int r=0;r<numRows;r++) { \
+                for (int r=row0;r<row1;r++) { \
                     *(imgVecData++) = *imgData; \
                     imgData += inCols; \
@@ -553,5 +547,5 @@
                 *outData = statVal; \
                 if (outPosition != NULL) { \
-                    *outPosition = col+c; \
+                    *outPosition = c; \
                     outPosition += delta; \
                 } \
@@ -594,6 +588,5 @@
         psU32* outPosition = NULL;
 
-        // fill in psVectors to fake out the
-        // statistics functions.
+        // fill in psVector to fake out the statistics functions.
         imgVec = psAlloc(sizeof(psVector));
         imgVec->type = in->type;
@@ -614,16 +607,16 @@
         outData = out->data.F64;
         if (delta < 0) {
-            outData += numRows - 1;
+            outData += numRows-1;
             if (outPosition != NULL) {
-                outPosition += numRows - 1;
+                outPosition += numRows-1;
             }
         }
 
-        for (int r = 0; r < numRows; r++) {
+        for (int r = row0; r < row1; r++) {
             // point the vector struct to the
             // data to calculate the stats
-            imgVec->data.V = (void *)(in->data.U8[row + r] + col * elementSize);
+            imgVec->data.V = (void *)(in->data.U8[r] + col0 * elementSize);
             if (maskVec != NULL) {
-                maskVec->data.V = (void *)(mask->data.U8[row + r] + col * sizeof(psMaskType));
+                maskVec->data.V = (void *)(mask->data.U8[r] + col0 * sizeof(psMaskType));
             }
             myStats = psVectorStats(myStats, imgVec, maskVec, maskVal);
@@ -631,5 +624,5 @@
             *outData = statVal;
             if (outPosition != NULL) {
-                *outPosition = row + r;
+                *outPosition = r;
                 outPosition += delta;
 
