Index: /trunk/psLib/src/imageops/psImageStructManip.c
===================================================================
--- /trunk/psLib/src/imageops/psImageStructManip.c	(revision 12813)
+++ /trunk/psLib/src/imageops/psImageStructManip.c	(revision 12814)
@@ -8,6 +8,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.14 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-03-14 00:39:50 $
+ *  @version $Revision: 1.15 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-04-12 18:54:51 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -20,4 +20,5 @@
 
 #include <string.h>
+#include <assert.h>
 
 #include "psMemory.h"
@@ -26,5 +27,8 @@
 
 
-
+// col0,row0 are the starting pixel in the input image coordinate frame
+// col1,row1 are the ending pixel in the input image coordinate frame
+// note that these are relative to the input col0,row0
+// also note that col0,row0 may not be less than input->col0,row0
 static psImage* imageSubset(psImage* out,
                             psImage* image,
@@ -35,5 +39,6 @@
 {
     psU32 elementSize;          // size of image element in bytes
-    psS32 inputColOffset;       // offset in bytes to first subset pixel in input row
+    psS32 inputColOffset;       // offset in **bytes** to first subset pixel in input row
+    psS32 inputRowOffset;       // offset in **rows*** to first input row
 
     if (image == NULL || image->data.V == NULL) {
@@ -44,6 +49,29 @@
 
     if ( col0 < image->col0 || row0 < image->row0 ) {
-        //        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
-        //                _("Specified subset range, [%d:%d,%d:%d], is invalid or outside input psImage's boundaries, [0:%d,0:%d]."));
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                _("Specified subset range, [%d:%d,%d:%d], is invalid or outside input psImage's boundaries, [0:%d,0:%d]."),
+                col0, col1-1, row0, row1-1, image->numCols-1, image->numRows-1);
+        return NULL;
+    }
+
+    if (image->type.dimen != PS_DIMEN_IMAGE) {
+        psError(PS_ERR_BAD_PARAMETER_TYPE, true,
+                _("The input psImage must have a PS_DIMEN_IMAGE dimension type."));
+        return NULL;
+    }
+
+    if (col1 < 1) {
+        col1 = image->col0 + image->numCols + col1;
+    }
+    if (row1 < 1) {
+        row1 = image->row0 + image->numRows + row1;
+    }
+
+    if (col1 <= col0 ||
+        row1 <= row0 ||
+        col0 >= image->col0 + image->numCols ||
+        row0 >= image->row0 + image->numRows ||
+        col1 > image->col0 + image->numCols ||
+        row1 > image->row0 + image->numRows ) {
         psError(PS_ERR_BAD_PARAMETER_VALUE, true,
                 _("Specified subset range, [%d:%d,%d:%d], is invalid or outside input psImage's boundaries, [0:%d,0:%d]."),
@@ -53,32 +81,4 @@
     }
 
-    if (image->type.dimen != PS_DIMEN_IMAGE) {
-        psError(PS_ERR_BAD_PARAMETER_TYPE, true,
-                _("The input psImage must have a PS_DIMEN_IMAGE dimension type."));
-        return NULL;
-    }
-
-    if (col1 < 1) {
-        col1 = image->col0 + image->numCols + col1;
-    }
-    if (row1 < 1) {
-        row1 = image->row0 + image->numRows + row1;
-    }
-
-    if (    col1 <= col0 ||
-            row1 <= row0 ||
-            col0 >= image->col0 + image->numCols ||
-            row0 >= image->row0 + image->numRows ||
-            col1 > image->col0 + image->numCols ||
-            row1 > image->row0 + image->numRows ) {
-        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
-                _("Specified subset range, [%d:%d,%d:%d], is invalid or outside input psImage's boundaries, [0:%d,0:%d]."),
-                col0, col1-1, row0, row1-1,
-                image->numCols-1, image->numRows-1);
-        return NULL;
-    }
-
-
-
     psS32 numRows = row1-row0;
     psS32 numCols = col1-col0;
@@ -86,10 +86,7 @@
     elementSize = PSELEMTYPE_SIZEOF(image->type.type);
 
-    if (image->parent != NULL) { // if this is a child, we need to start working with parent.
-        // XXX EAM : we now treat the region as parent coordinates
-        // col0 += image->col0;
-        // col1 += image->col0;
-        // row0 += image->row0;
-        // row1 += image->row0;
+    // if this is a child, we need to start working with parent pixels
+    // the subset region (col0,row0 - col1,row1) is in the parent frame
+    if (image->parent != NULL) { 
         image = (psImage*)image->parent;
     }
@@ -116,7 +113,7 @@
     P_PSIMAGE_SET_NUMCOLS(out, numCols);
     P_PSIMAGE_SET_NUMROWS(out, numRows);
-
-    out->row0 = row0;
-    out->col0 = col0;
+    P_PSIMAGE_SET_COL0(out, col0);
+    P_PSIMAGE_SET_ROW0(out, row0);
+
     out->parent = image;
     out->children = NULL;
@@ -126,7 +123,10 @@
     psMemSetDeallocator(out,psMemGetDeallocator(image));
 
-    inputColOffset = elementSize * col0;
+    inputRowOffset = (row0 - image->row0);
+    inputColOffset = (col0 - image->col0)*elementSize;
+    assert (inputRowOffset >= 0);
+    assert (inputColOffset >= 0);
     for (psS32 row = 0; row < numRows; row++) {
-        out->data.V[row] = image->data.U8[row0 + row] + inputColOffset;
+        out->data.V[row] = image->data.U8[row + inputRowOffset] + inputColOffset;
     }
 
@@ -183,6 +183,6 @@
 
     output = psImageRecycle(output, numCols, numRows, type);
-    output->col0 = input->col0;
-    output->row0 = input->row0;
+    P_PSIMAGE_SET_COL0(output, input->col0);
+    P_PSIMAGE_SET_ROW0(output, input->row0);
 
     // cover the trival case of copy of the same
