Index: /trunk/psLib/src/types/psPixels.c
===================================================================
--- /trunk/psLib/src/types/psPixels.c	(revision 16421)
+++ /trunk/psLib/src/types/psPixels.c	(revision 16422)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.36 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-05-22 19:46:46 $
+ *  @version $Revision: 1.37 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-02-13 21:46:06 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -182,46 +182,26 @@
     float y1 = region.y1;
 
-    if ( (x0 < 0 || x1 < 0) || (y0 < 0 || y1 < 0) ) {
+    if (x0 < 0 || x1 < 0 || y0 < 0 || y1 < 0 || x1 < x0 || y1 < y0) {
         psError(PS_ERR_BAD_PARAMETER_VALUE, true,
                 _("Specified psRegion, [%d:%d,%d:%d], does not specify a valid region."),
-                (int)y0,(int)y1,(int)x0,(int)x1);
+                (int)x0,(int)x1,(int)y0,(int)y1);
         psFree(out);
         return NULL;
     }
 
-    // determine the output image size
-    int numRows = y1-y0;
-    int numCols = x1-x0;
-    numRows += 1;
-    numCols += 1;
-    if (numRows < 1 || numCols < 1) {
-        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
-                _("Specified psRegion, [%d:%d,%d:%d], does not specify a valid region."),
-                (int)y0,(int)y1,(int)x0,(int)x1);
-        psFree(out);
-        return NULL;
-    }
-
-    //  allocate the output image
+    int numRows = y1 - y0 + 1, numCols = x1 - x0 + 1; // Size of image
+
     out = psImageRecycle(out, numCols, numRows, PS_TYPE_MASK);
-    if (out == NULL) {
-        psError(PS_ERR_UNKNOWN, false,
-                _("Failed to create image of size %dx%d."),
-                numCols, numRows);
+    if (!out) {
+        psError(PS_ERR_UNKNOWN, false, _("Failed to create image of size %dx%d."), numCols, numRows);
         return NULL;
     }
     P_PSIMAGE_SET_COL0(out, (int)x0);
     P_PSIMAGE_SET_ROW0(out, (int)y0);
-
-    // initialize image to all zeros
-    int columnByteSize = sizeof(psMaskType)*numCols;
-    for (int row = 0; row < numRows; row++) {
-        memset(out->data.U8[row],0,columnByteSize);
-    }
-
-    // determine the length of the pixel vector
-    long length = pixels->n;
+    psImageInit(out, 0);
+
 
     // cycle through the vector of pixels and insert pixels into image
+    long length = pixels->n;            // Length of pixel array
     psMaskType** outData = out->data.PS_TYPE_MASK_DATA;
     for (int p = 0; p < length; p++) {
@@ -244,45 +224,22 @@
     PS_ASSERT_IMAGE_TYPE(mask, PS_TYPE_MASK, NULL);
 
-    int numRows = mask->numRows;
-    int numCols = mask->numCols;
-
-    // assumption: number of masked pixels is relatively small compared to
-    // total pixels, so it is best to just start with a guess and resize if
-    // necessary
-    long minPixels = numRows*numCols/100; // initial guess, 1% of pixels masked
-    if (minPixels < 32) { // enforce a minimum size
-        minPixels = 32;
-    }
-
-    // check out's validity (allocated/minimum size)
-    if (out == NULL || out->data == NULL || out->nalloc < minPixels) {
-        out = psPixelsRealloc(out,minPixels);
-    }
-
-    // start with a blank list of pixels
+    int numRows = mask->numRows, numCols = mask->numCols; // Size of image
+
+    // assumption: number of masked pixels is relatively small compared to total pixels, so it is best to just
+    // start with a guess and resize if necessary
+    long minPixels = PS_MAX(numRows * numCols / 100, 32); // initial guess: 1% of pixels masked;
+    if (!out || !out->data || out->nalloc < minPixels) {
+        out = psPixelsRealloc(out, minPixels);
+    }
     out->n = 0;
 
     // find the mask pixels in the image
-    long numPixels = 0;
-    psPixelCoord* data = out->data;
-    int nalloc = out->nalloc;
-    for (int row=0; row<numRows; row++) {
-        psMaskType* maskRow = mask->data.PS_TYPE_MASK_DATA[row];
-        for (int col=0; col<numCols; col++) {
-            if ( (maskRow[col] & maskVal) != 0 ) {
-                // check the vector sizes, and expand if necessary
-                if (nalloc <= numPixels) {
-                    out = psPixelsRealloc(out, 2*nalloc);
-                    nalloc = out->nalloc;
-                    data = out->data;
-                }
-
-                data[numPixels].x = col;
-                data[numPixels].y = row;
-                numPixels++;
+    for (int y = 0; y < numRows; y++) {
+        for (int x = 0; x < numCols; x++) {
+            if (mask->data.PS_TYPE_MASK_DATA[y][x] & maskVal) {
+                psPixelsAdd(out, out->nalloc, x, y);
             }
         }
     }
-    out->n = numPixels;
 
     return out;
