Index: trunk/psLib/test/image/tst_psImage.c
===================================================================
--- trunk/psLib/test/image/tst_psImage.c	(revision 717)
+++ trunk/psLib/test/image/tst_psImage.c	(revision 720)
@@ -6,6 +6,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-05-18 19:22:34 $
+ *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-05-18 23:28:33 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -19,9 +19,12 @@
 #include <math.h>
 #include <float.h>
+#include <string.h>
 
 static int testImageAlloc(void);
+static int testImageSubset(void);
 
 testDescription tests[] = {
                               {testImageAlloc,"546-testImageAlloc",0},
+                              {testImageSubset,"547-testImageSubset",0},
                               {NULL}
                           };
@@ -31,5 +34,5 @@
     psSetLogLevel(PS_LOG_INFO);
 
-    testImageAlloc();
+    testImageSubset();
 
     if (! runTestSuite(stderr,"psImage",tests)) {
@@ -221,2 +224,223 @@
 }
 
+// #547: psImageSubset shall create child image of a specified size from a parent psImage structure
+int testImageSubset(void)
+{
+    psImage preSubsetStruct;
+    psImage* original;
+    psImage* subset1 = NULL;
+    psImage* subset2 = NULL;
+    psImage* subset3 = NULL;
+    int c = 128;
+    int r = 256;
+
+    original = psImageAlloc(c,r,PS_TYPE_U32);
+    for (int row=0;row<r;row++) {
+        for (int col=0;col<c;col++) {
+            original->data.F32[row][col] = row*1000+col;
+        }
+    }
+
+    psLogMsg(__func__,PS_LOG_INFO,"memcpy(&preSubsetStruct,original,sizeof(psImage));");
+    memcpy(&preSubsetStruct,original,sizeof(psImage));
+
+    psLogMsg(__func__,PS_LOG_INFO,"subset1 = psImageAlloc(c/4,r/4,PS_TYPE_U8);");
+    subset1 = psImageAlloc(c/4,r/4,PS_TYPE_U8);
+
+    psLogMsg(__func__,PS_LOG_INFO,"subset2 = psImageSubset(subset1,original,c/2,r/2,c/4,r/4);");
+    subset2 = psImageSubset(subset1,original,c/2,r/2,c/4,r/4);
+
+    psLogMsg(__func__,PS_LOG_INFO,"subset3 = psImageSubset(NULL,original,c/2,r/2,0,0);");
+    subset3 = psImageSubset(NULL,original,c/2,r/2,0,0);
+
+    psLogMsg(__func__,PS_LOG_INFO,"Verify the returned psImage structure is equal to the input psImage "
+             "structure parameter out, if input parameter out is specified.");
+
+    if (subset1 != subset2 || subset2 == NULL) {
+        psError(__func__,"psImageSubset didn't recycle the psImage given");
+        return 1;
+    }
+
+    psLogMsg(__func__,PS_LOG_INFO,"Verify a new psImage structure is created, if input parameter out is set "
+             "to null.");
+
+    if (subset3 == NULL) {
+        psError(__func__,"psImageSubset output was NULL for subset3.");
+        return 2;
+    }
+
+    psLogMsg(__func__,PS_LOG_INFO,"Verify the returned psImage structure contains expected values in the "
+             "row member, if the input psImage structure image contains known values.");
+
+    for (int row=0;row<r/2;row++) {
+        for (int col=0;col<c/2;col++) {
+            if (subset2->data.U32[row][col] != original->data.U32[row+r/4][col+c/4]) {
+                psError(__func__,"psImageSubset output #1 was wrong at %dx%d (%d vs %d).",
+                        row,col,subset2->data.U32[row][col], original->data.U32[row+r/4][col+c/4]);
+                return 3;
+            }
+            if (subset3->data.U32[row][col] != original->data.U32[row][col]) {
+                psError(__func__,"psImageSubset output #1 was wrong at %dx%d (%d vs %d).",
+                        row,col,subset2->data.U32[row][col], original->data.U32[row][col]);
+                return 4;
+            }
+        }
+    }
+
+    psLogMsg(__func__,PS_LOG_INFO,"Verify the returned psImage structure members nrow and ncol are equal to "
+             "the input parameter nrow and ncol respectively.");
+
+    if (subset3->numCols != c/2 || subset3->numRows != r/2) {
+        psError(__func__,"psImageSubset output size was not proper(%dx%d, should be %dx%d).",
+                subset3->numCols, subset3->numRows, c/2,r/2);
+        return 5;
+    }
+
+    psLogMsg(__func__,PS_LOG_INFO,"Verify the returned psImage structure member type is equal to the input "
+             "psImage structure member type.");
+
+    if (subset2->type.type != PS_TYPE_U32) {
+        psError(__func__,"psImageSubset output type was not proper(%d, should be %d).",
+                subset2->type.type, PS_TYPE_U32);
+        return 6;
+    }
+    if (subset3->type.type != PS_TYPE_U32) {
+        psError(__func__,"psImageSubset output type was not proper(%d, should be %d).",
+                subset3->type.type, PS_TYPE_U32);
+        return 7;
+    }
+
+    psLogMsg(__func__,PS_LOG_INFO,"Verify the returned psImage structure members row0 and col0 are equal to "
+             "the input parameters row0 and col0 respectively.");
+
+    if (subset2->col0 != c/4 || subset2->row0 != r/4) {
+        psError(__func__,"psImageSubset didn't set col0/row0 for subset2 (%d/%d, should be %d/%d).",
+                subset2->col0,subset2->row0,c/4,r/4);
+        return 8;
+    }
+    if (subset3->col0 != 0 || subset3->row0 != 0) {
+        psError(__func__,"psImageSubset didn't set col0/row0 for subset3 (%d/%d, should be %d/%d).",
+                subset3->col0,subset3->row0,0,0);
+        return 9;
+    }
+
+    psLogMsg(__func__,PS_LOG_INFO,"Verify the returned psImage structure member parent is equal to the "
+             "input psImage structure pointer image.");
+
+    if (subset2->parent != original || subset3->parent != original) {
+        psError(__func__,"psImageSubset didn't set parent.");
+        return 10;
+    }
+
+    psLogMsg(__func__,PS_LOG_INFO,"Verify the returned psImage structure member Nchildren is equal to "
+             "zero.");
+
+    if (subset2->nChildren != 0 || subset3->nChildren != 0) {
+        psError(__func__,"psImageSubset didn't set nChildren to zero.");
+        return 11;
+    }
+
+    psLogMsg(__func__,PS_LOG_INFO,"Verify the returned psImage structure member children is null.");
+
+    if (subset2->children != NULL || subset3->children != NULL) {
+        psError(__func__,"psImageSubset didn't set children to NULL.");
+        return 11;
+    }
+
+    psLogMsg(__func__,PS_LOG_INFO,"Verify the input psImage structure image only has the following members "
+             "changed: 1) Nchildren is increased by one. 2) parent contains pointer psImage structure "
+             "out at parent[Nchildren-1].");
+
+    if (original->nChildren != preSubsetStruct.nChildren+2) {
+        psError(__func__,"psImageSubset didn't increment nChildren by one per subset.");
+        return 12;
+    }
+    if (original->children[0] != subset2 || original->children[1] != subset3) {
+        psError(__func__,"psImageSubset didn't properly store the children pointers.");
+        return 13;
+    }
+
+    psLogMsg(__func__,PS_LOG_INFO,"Verify the returned psImage structure pointer is null and program "
+             "execution doesn't stop, if the input parameter image is null. Also verified the input "
+             "psImage structure is not modified.");
+
+    subset1 = psImageSubset(NULL,NULL,c/2,r/2,0,0);
+    if (subset1 != NULL) {
+        psError(__func__,"psImageSubset didn't return NULL when input image was NULL.");
+        return 14;
+    }
+
+    psLogMsg(__func__,PS_LOG_INFO,"Verify the returned psImage structure pointer is null and program "
+             " execution doesn't stop, if the input parameters nrow and/or ncol are zero. Also verify "
+             "input psImage structure is not modified.");
+
+    memcpy(&preSubsetStruct,original,sizeof(psImage));
+    subset1 = psImageSubset(NULL,original,c/2,0,0,0);
+    if (subset1 != NULL) {
+        psError(__func__,"psImageSubset didn't return NULL when numRows=0.");
+        return 15;
+    }
+    subset1 = psImageSubset(NULL,original,0,r/2,0,0);
+    if (subset1 != NULL) {
+        psError(__func__,"psImageSubset didn't return NULL when numCols=0.");
+        return 16;
+    }
+    if (memcmp(original,&preSubsetStruct,sizeof(psImage)) != 0) {
+        psError(__func__,"psImageSubset changed the original struct though it failed to subset.");
+        return 17;
+    }
+
+
+    psLogMsg(__func__,PS_LOG_INFO,"Verify the returned psImage structure pointer is null and program "
+             "execution doesn't stop, if the input parameters row0 and col0 are not within the range of "
+             "values of psImage structure image.");
+
+    subset1 = psImageSubset(NULL,original,c/2,r/2,c,0);
+    if (subset1 != NULL) {
+        psError(__func__,"psImageSubset didn't return NULL when subset origin was outside of "
+                "image (via cols).");
+        return 18;
+    }
+    subset1 = psImageSubset(NULL,original,c/2,r/2,0,r);
+    if (subset1 != NULL) {
+        psError(__func__,"psImageSubset didn't return NULL when subset origin was outside of "
+                "image (via rows).");
+        return 19;
+    }
+    subset1 = psImageSubset(NULL,original,c/2,r/2,-1,0);
+    if (subset1 != NULL) {
+        psError(__func__,"psImageSubset didn't return NULL when subset origin was outside of "
+                "image (col0=-1).");
+        return 20;
+    }
+    subset1 = psImageSubset(NULL,original,c/2,r/2,0,-1);
+    if (subset1 != NULL) {
+        psError(__func__,"psImageSubset didn't return NULL when subset origin was outside of "
+                "image (row0=-1).");
+        return 21;
+    }
+
+    psLogMsg(__func__,PS_LOG_INFO,"Verify the returned psImage structure pointer is null and program "
+             "execution doesn't stop if the input parameters nrow, ncol, row0 and col0 specify a range of "
+             "data not within the input psImage structure image.  Also verify the input psImage structure "
+             "is not modified.");
+
+    subset1 = psImageSubset(NULL,original,c/2,r/2,c/2,0);
+    if (subset1 != NULL) {
+        psError(__func__,"psImageSubset didn't return NULL when subset was outside of image (via rows).");
+        return 22;
+    }
+    subset1 = psImageSubset(NULL,original,c/2,r/2,0,r/2);
+    if (subset1 != NULL) {
+        psError(__func__,"psImageSubset didn't return NULL when subset was outside of image (via cols).");
+        return 23;
+    }
+    subset1 = psImageSubset(NULL,original,c/2,r/2,c/2,r/2);
+    if (subset1 != NULL) {
+        psError(__func__,"psImageSubset didn't return NULL when subset was outside of image (via row+cols).");
+        return 24;
+    }
+
+    return 0;
+}
+
