Index: trunk/psLib/src/image/psImageStats.c
===================================================================
--- trunk/psLib/src/image/psImageStats.c	(revision 1233)
+++ trunk/psLib/src/image/psImageStats.c	(revision 1234)
@@ -25,49 +25,48 @@
                       int maskVal)
 {
-    psVector *junkData=NULL;
-    psVector *junkMask=NULL;
-    int ptr = 0;
-    int i = 0;
-    int j = 0;
-
-    // NOTE: Verify this action.
-    if ((stats == NULL) ||
-            (in == NULL)) {
-        return(NULL);
-    }
-    // NOTE: Verify this action.
+    psVector* junkData=NULL;
+    psVector* junkMask=NULL;
+
+    if (stats == NULL) {
+        psError(__func__,"The input psStats struct can not be NULL.");
+        return NULL;
+    }
+
+    if (in == NULL) {
+        psError(__func__,"The input image can not be NULL.");
+        return NULL;
+    }
+
     if (stats->options == 0) {
-        return(stats);
-    }
-
-    junkData = psVectorAlloc(in->numRows * in->numCols, in->type.type);
+        psError(__func__,"No statistic option/operation was specified.");
+        return stats;
+    }
+
+    // stuff the image data into a psVector struct.
+    junkData = psAlloc(sizeof(psVector));
+    junkData->type = in->type;
+    junkData->nalloc = in->numRows*in->numCols;
     junkData->n = junkData->nalloc;
-    ptr=0;
-    for (i=0;i<in->numRows;i++) {
-        for (j=0;j<in->numCols;j++) {
-            junkData->data.F32[ptr++] = in->data.F32[i][j];
-        }
-    }
+    junkData->data.V = in->data.V[0];    // since psImage data is contiguous...
 
     if (mask != NULL) {
-        // NOTE: verify that mask data is PS_TYPE_U8.
-        // NOTE: figure out mask types
-
-        junkMask = psVectorAlloc(mask->numRows * mask->numCols,
-                                 mask->type.type);
+        if (mask->type.type != PS_TYPE_MASK) {
+            psError(__func__, "Expected the mask image type not found (type=%x)",
+                    mask->type.type);
+            psFree(junkData);
+            return NULL;
+        }
+
+        // stuff the mask data into a psVector struct.
+        junkMask = psAlloc(sizeof(psVector));
+        junkMask->type = mask->type;
+        junkMask->nalloc = mask->numRows*mask->numCols;
         junkMask->n = junkMask->nalloc;
-        // NOTE: Is there a more efficient way to do this?
-        ptr=0;
-        for (i=0;i<mask->numRows;i++) {
-            for (j=0;j<mask->numCols;j++) {
-                junkMask->data.U8[ptr++] = mask->data.U8[i][j];
-            }
-        }
-
-        stats = psVectorStats(stats, junkData, junkMask, maskVal);
-        psFree(junkMask);
-    } else {
-        stats = psVectorStats(stats, junkData, NULL, 0);
-    }
+        junkMask->data.V = mask->data.V[0];
+    }
+
+    stats = psVectorStats(stats, junkData, junkMask, maskVal);
+
+    psFree(junkMask);
     psFree(junkData);
     return(stats);
@@ -86,7 +85,4 @@
     psVector *junkData=NULL;
     psVector *junkMask=NULL;
-    int ptr = 0;
-    int i = 0;
-    int j = 0;
 
     // NOTE: Verify this action.
@@ -96,40 +92,29 @@
     }
 
-    junkData = psVectorAlloc(in->numRows * in->numCols, in->type.type);
+    junkData = psAlloc(sizeof(psVector));
+    junkData->type = in->type;
+    junkData->nalloc = in->numRows*in->numCols;
     junkData->n = junkData->nalloc;
-
-    // NOTE: Is there a more efficient way to do this?  memcopy() won't work.
-    // Can we trick the junkData structure to use the image buffer, then
-    // untrick it before we deallocate it (so we won't deallocate that buffer
-    // twice?
-
-    // NOTE: Make sure you have the numRows/NumCols in the right place.
-    ptr=0;
-    for (i=0;i<in->numRows;i++) {
-        for (j=0;j<in->numCols;j++) {
-            junkData->data.F32[ptr++] = in->data.F32[i][j];
-        }
-    }
+    junkData->data.V = in->data.V[0];    // since psImage data is contiguous...
 
     if (mask != NULL) {
-        // NOTE: verify that mask data is PS_TYPE_U8.
-        // NOTE: figure out mask types
-
-        junkMask = psVectorAlloc(mask->numRows * mask->numCols,
-                                 mask->type.type);
+        if (mask->type.type != PS_TYPE_MASK) {
+            psError(__func__, "Expected the mask image type not found (type=%x)",
+                    mask->type.type);
+            psFree(junkData);
+            return NULL;
+        }
+
+        // stuff the mask data into a psVector struct.
+        junkMask = psAlloc(sizeof(psVector));
+        junkMask->type = mask->type;
+        junkMask->nalloc = mask->numRows*mask->numCols;
         junkMask->n = junkMask->nalloc;
-
-        // NOTE: Is there a more efficient way to do this?
-        ptr=0;
-        for (i=0;i<mask->numRows;i++) {
-            for (j=0;j<mask->numCols;j++) {
-                junkMask->data.U8[ptr++] = mask->data.U8[i][j];
-            }
-        }
-        out = psHistogramVector(out, junkData, junkMask, maskVal);
-        psFree(junkMask);
-    } else {
-        out = psHistogramVector(out, junkData, NULL, 0);
-    }
+        junkMask->data.V = mask->data.V[0];
+    }
+
+    out = psHistogramVector(out, junkData, junkMask, maskVal);
+
+    psFree(junkMask);
     psFree(junkData);
 
