Index: trunk/psLib/src/image/psImage.c
===================================================================
--- trunk/psLib/src/image/psImage.c	(revision 715)
+++ trunk/psLib/src/image/psImage.c	(revision 719)
@@ -7,6 +7,6 @@
  *  @author Ross Harman, MHPCC
  *   
- *  @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-05-18 18:39:43 $
+ *  @version $Revision: 1.12 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-05-18 23:26:26 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -70,8 +70,43 @@
 }
 
+psImage* psImageRealloc(psImage* old,unsigned int numCols, unsigned int numRows, const psElemType type)
+{
+    int elementSize = PSELEMTYPE_SIZEOF(type); // element size in bytes
+    int rowSize = numCols*elementSize;  // row size in bytes.
+
+    if (old == NULL) {
+        old = psImageAlloc(numCols,numRows,type);
+        return old;
+    }
+
+    if (old->type.dimen != PS_DIMEN_IMAGE) {
+        psError(__func__,"Can not realloc image because image is not an image.");
+        return NULL;
+    }
+
+    /* image already the right size/type? */
+    if (numCols == old->numCols && numRows == old->numRows && type == old->type.type) {
+        return old;
+    }
+
+    // Resize the image buffer
+    old->data.v[0] = psRealloc(old->data.v[0],numCols * numRows * elementSize);
+    old->data.v = (void**) psRealloc(old->data.v,numRows * sizeof(void*));
+
+    // recreate the row pointers
+    for(int i = 1; i < numRows; i++) {
+        old->data.v[i] = (void*)((int8_t*)old->data.v[i-1]+rowSize);
+    }
+
+    *(unsigned int*)&old->numCols = numCols;
+    *(unsigned int*)&old->numRows = numRows;
+    *(psElemType*)&old->type.type = type;
+
+    return old;
+}
+
 psImage *psImageSubset(psImage *out,psImage *image,unsigned int numCols,
                        unsigned int numRows, unsigned int col0, unsigned int row0)
 {
-    psElemType type;
     unsigned int elementSize;           // size of image element in bytes
     unsigned int outputRowSize;         // output row size in bytes
@@ -88,22 +123,28 @@
     }
 
+    if (numCols < 1 || numRows < 1) {
+        psError(__func__,"Can not subset image because number of rows or columns are zero (%dx%d).",
+                numCols, numRows);
+        return NULL;
+    }
+
+    if (col0 >= image->numCols || row0 >= image->numRows) {
+        psError(__func__,"Can not subset image because col0,row0 (%d,%d) is not a valid pixel "
+                "location.", col0,row0);
+        return NULL;
+    }
+
     /* validate subimage size */
     if (col0+numCols >= image->numCols || row0+numRows >= image->numRows) {
         psError(__func__,"Can not subset image outside of image boundaries (size=%dx%d, "
-                "subset=[%d:%d,%d:%d]).",image->numCols,image->numRows,image->col0,
-                image->col0+numCols, image->row0, image->row0+numRows);
-        return NULL;
-    }
-
-    type = image->type.type;
-    elementSize = PSELEMTYPE_SIZEOF(type);
-
-    if (out == NULL) {
-        out = psImageAlloc(numCols,numRows,image->type.type);
-    } else if (out->numCols != numCols || out->numRows != numRows || out->type.type != type) {
-        // sizes/type different, can't reuse the given image buffer
-        psImageFree(out);
-        out = psImageAlloc(numCols,numRows,image->type.type);
-    }
+                "subset=[%d:%d,%d:%d]).",image->numCols,image->numRows,col0,
+                col0+numCols, row0, row0+numRows);
+        return NULL;
+    }
+
+
+    elementSize = PSELEMTYPE_SIZEOF(image->type.type);
+
+    out = psImageRealloc(out,numCols,numRows,image->type.type);
 
     // set the parent information into the child output image
@@ -199,14 +240,7 @@
     }
 
-
-    if (output == NULL) {
-        output = psImageAlloc(numCols,numRows,type);
-    } else if (output->numCols != numCols || output->numRows != numRows ||
-               output->type.type != type) {        // sizes/type different, can't reuse the given image buffer
-        psImageFree(output);
-        output = psImageAlloc(numCols,numRows,type);
-    }
-
-    // cover the trival case of copy of the same type.
+    output = psImageRealloc(output,numCols,numRows,type);
+
+    // cover the trival case of copy of the same datatype.
     if (type == inDatatype) {
         memcpy(output->data.v[0],input->data.v[0],elementSize*numRows*numCols);
