Index: trunk/psLib/src/collections/psPixels.c
===================================================================
--- trunk/psLib/src/collections/psPixels.c	(revision 3761)
+++ trunk/psLib/src/collections/psPixels.c	(revision 3959)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-04-23 00:10:19 $
+ *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-05-18 21:38:19 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -18,4 +18,7 @@
 #include "psPixels.h"
 #include "psMemory.h"
+
+#include "psError.h"
+#include "psCollectionsErrors.h"
 
 typedef int(*qsortCompareFcn)(const void *, const void *);
@@ -66,5 +69,5 @@
     psMemSetDeallocator(out, (psFreeFcn)pixelsFree);
 
-    return NULL;
+    return out;
 }
 
@@ -75,5 +78,10 @@
     }
 
-    pixels->data = psRealloc(pixels->data, sizeof(psPixelCoord)*size);
+    if (size > 0) {
+        pixels->data = psRealloc(pixels->data, sizeof(psPixelCoord)*size);
+    } else {
+        psFree(pixels->data);
+        pixels->data = NULL;
+    }
 
     pixels->nalloc = size;
@@ -88,4 +96,5 @@
 {
     if (in == NULL) {
+        psFree(out);
         return NULL;
     }
@@ -93,5 +102,5 @@
     out = psPixelsRealloc(out, in->n);
 
-    memcpy(in->data,out->data, in->n*sizeof(psPixelCoord));
+    memcpy(out->data,in->data, in->n*sizeof(psPixelCoord));
     out->n = in->n;
 
@@ -99,9 +108,10 @@
 }
 
-psImage *psPixelsToMask(psImage *out, const psPixels *pixels, const psRegion *region, unsigned int maskVal)
+psImage *psPixelsToMask(psImage *out, const psPixels *pixels, const psRegion region, unsigned int maskVal)
 {
     // check that the input pixel vector is valid
     if (pixels == NULL) {
-        // XXX: Error message
+        psError(PS_ERR_BAD_PARAMETER_NULL, true,
+                PS_ERRORTEXT_psPixels_NULL);
         psFree(out);
         return NULL;
@@ -109,19 +119,14 @@
     psPixelCoord* data = pixels->data;
     if (data == NULL) {
-        // XXX: Error message
-        psFree(out);
-        return NULL;
-    }
-
-    // check if the input region is valid
-    if (region == NULL) {
-        // XXX: Error message
-        psFree(out);
-        return NULL;
-    }
-    int x0 = region->x0;
-    int x1 = region->x1;
-    int y0 = region->y0;
-    int y1 = region->y1;
+        psError(PS_ERR_BAD_PARAMETER_SIZE, true,
+                PS_ERRORTEXT_psPixels_DATA_NULL);
+        psFree(out);
+        return NULL;
+    }
+
+    int x0 = region.x0;
+    int x1 = region.x1;
+    int y0 = region.y0;
+    int y1 = region.y1;
 
     // determine the output image size
@@ -129,5 +134,7 @@
     int numCols = y1-y0;
     if (numRows < 1 || numCols < 1) {
-        // XXX: Error message
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                PS_ERRORTEXT_psPixels_REGION_INVALID,
+                x0,x1,y0,y1);
         psFree(out);
         return NULL;
@@ -137,5 +144,7 @@
     out = psImageRecycle(out, numCols, numRows, PS_TYPE_MASK);
     if (out == NULL) {
-        // XXX: Error message
+        psError(PS_ERR_UNKNOWN, false,
+                PS_ERRORTEXT_psPixels_FAILED_IMAGE_CREATE,
+                numCols, numRows);
         return NULL;
     }
@@ -144,5 +153,5 @@
 
     // initialize image to all zeros
-    int columnByteSize = sizeof(PS_TYPE_MASK)*numCols;
+    int columnByteSize = sizeof(psMaskType)*numCols;
     for (int row = 0; row < numRows; row++) {
         memset(out->data.U8[row],0,columnByteSize);
@@ -166,13 +175,18 @@
 }
 
-psPixels *psMaskToPixels(psPixels *out, const psImage *mask, unsigned int maskVal)
+psPixels* psPixelsFromMask(psPixels* out, const psImage* mask, unsigned int maskVal)
 {
     if (mask == NULL) {
-        // XXX: Error message
+        psError(PS_ERR_BAD_PARAMETER_NULL, true,
+                PS_ERRORTEXT_psPixels_MASK_NULL);
         psFree(out);
         return NULL;
     }
     if (mask->type.type != PS_TYPE_MASK) {
-        // XXX: Error message
+        char* typeStr;
+        PS_TYPE_NAME(typeStr,mask->type.type);
+        psError(PS_ERR_BAD_PARAMETER_TYPE, true,
+                PS_ERRORTEXT_psPixels_MASK_TYPE,
+                typeStr);
         psFree(out);
         return NULL;
@@ -204,7 +218,7 @@
         psMaskType* maskRow = mask->data.PS_TYPE_MASK_DATA[row];
         for (int col=0; col<numCols; col++) {
-            if ( (maskRow[col] | maskVal) != 0 ) {
+            if ( (maskRow[col] & maskVal) != 0 ) {
                 // check the vector sizes, and expand if necessary
-                if (nalloc >= numPixels) {
+                if (nalloc <= numPixels) {
                     out = psPixelsRealloc(out, 2*nalloc);
                     nalloc = out->nalloc;
@@ -218,4 +232,5 @@
         }
     }
+    out->n = numPixels;
 
     return out;
@@ -225,7 +240,9 @@
 {
     if (pixels == NULL) {
-        // XXX: Error message
-        return NULL;
-    }
+        psError(PS_ERR_BAD_PARAMETER_NULL, true,
+                PS_ERRORTEXT_psPixels_NULL);
+        return out;
+    }
+
     int pixelsN = pixels->n;
     psPixelCoord* pixelsData = pixels->data;
@@ -243,5 +260,5 @@
 
     // sort the OUT array to help in searching for duplicates later
-    qsort(outData, sizeof(psPixelCoord), outN,
+    qsort(outData, outN, sizeof(psPixelCoord),
           (qsortCompareFcn)comparePixelCoord);
 
@@ -251,5 +268,5 @@
     for (int n = 0; n < pixelsN; n++) {
         pCoord = pixelsData[n];
-        if (bsearch(&pCoord, outData, sizeof(psPixelCoord), outN,
+        if (bsearch(&pCoord, outData, outN, sizeof(psPixelCoord),
                     (qsortCompareFcn)comparePixelCoord) == NULL) {
             // no match in OUT array of this value
@@ -261,2 +278,27 @@
     return out;
 }
+
+bool p_psPixelsPrint (FILE *fd, psPixels* pixels, const char *name)
+{
+
+    fprintf (fd, "psPixels: %s\n", name);
+
+    if (pixels == NULL) {
+        fprintf(fd,"NULL\n\n");
+        return true;
+    }
+
+    int n = pixels->n;
+    psPixelCoord* data = pixels->data;
+
+    if (data == NULL || n == 0) {
+        fprintf(fd,"EMPTY\n\n");
+        return true;
+    }
+
+    for (int i = 0; i < n; i++) {
+        fprintf (fd, "(%d,%d)\n", data[i].x, data[i].y);
+    }
+    fprintf (fd, "\n");
+    return (true);
+}
