Index: trunk/psLib/src/imageops/psImageStats.c
===================================================================
--- trunk/psLib/src/imageops/psImageStats.c	(revision 831)
+++ trunk/psLib/src/imageops/psImageStats.c	(revision 889)
@@ -19,4 +19,9 @@
 
 /// This routine must determine the various statistics for the image.
+/*****************************************************************************
+    GUS: verify that image/mask have the
+ correct types
+ sizes
+ *****************************************************************************/
 psStats *psImageStats(psStats *stats,
                       psImage *in,
@@ -26,15 +31,39 @@
     psVector *junkData=NULL;
     psVector *junkMask=NULL;
+    int ptr = 0;
+    int i = 0;
+    int j = 0;
 
     junkData = psVectorAlloc(in->numRows * in->numCols, in->type.type);
-    junkMask = psVectorAlloc(mask->numRows * mask->numCols, mask->type.type);
-
-    // GUS: figure out mask types
-    junkData->data.F32 = (float *) in->data.F32;
-    junkMask->data.F32 = (float *) mask->data.F32;
-    stats = psVectorStats(stats, junkData, junkMask, maskVal);
+    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];
+        }
+    }
+
+    if (mask != NULL) {
+        // GUS: verify that mask data is PS_TYPE_U8.
+        // GUS: figure out mask types
+
+        junkMask = psVectorAlloc(mask->numRows * mask->numCols,
+                                 mask->type.type);
+        junkMask->n = junkMask->nalloc;
+        // GUS: 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);
+        psVectorFree(junkMask);
+    } else {
+        stats = psVectorStats(stats, junkData, NULL, 0);
+    }
 
     psVectorFree(junkData);
-    psVectorFree(junkMask);
 
     return(stats);
@@ -44,4 +73,8 @@
     NOTE: We assume that the psHistogram structure out has already been
     allocated and initialized.
+ 
+    GUS: verify that image/mask have the
+ correct types
+ sizes
  *****************************************************************************/
 psHistogram *psImageHistogram(psHistogram *out,
@@ -52,16 +85,45 @@
     psVector *junkData=NULL;
     psVector *junkMask=NULL;
+    int ptr = 0;
+    int i = 0;
+    int j = 0;
 
     junkData = psVectorAlloc(in->numRows * in->numCols, in->type.type);
-    junkMask = psVectorAlloc(mask->numRows * mask->numCols, mask->type.type);
-
-    // GUS: figure out mask types
-    junkData->data.F32 = (float *) in->data.F32;
-    junkMask->data.F32 = (float *) mask->data.F32;
-
-    out = psHistogramVector(out, junkData, junkMask, maskVal);
-
+    junkData->n = junkData->nalloc;
+
+    // GUS: 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?
+
+    // GUS: 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];
+        }
+    }
+
+    if (mask != NULL) {
+        // GUS: verify that mask data is PS_TYPE_U8.
+        // GUS: figure out mask types
+
+        junkMask = psVectorAlloc(mask->numRows * mask->numCols,
+                                 mask->type.type);
+        junkMask->n = junkMask->nalloc;
+
+        // GUS: 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);
+        psVectorFree(junkMask);
+    } else {
+        out = psHistogramVector(out, junkData, NULL, 0);
+    }
     psVectorFree(junkData);
-    psVectorFree(junkMask);
 
     return(out);
@@ -91,6 +153,8 @@
     int maxChebyPoly = 0;
 
+
     // Create the sums[][] data structure.  This will hold the LHS of equation
     // 29 in the ADD: sums[k][l] = SUM { image(x,y) * Tk(x) * Tl(y) }
+
     sums = (float **) psAlloc(coeffs->nX * sizeof(float *));
     for (i=0;i<coeffs->nX;i++) {
@@ -105,5 +169,5 @@
     // Determine how many Chebyshev polynomials are needed, then create them.
     maxChebyPoly = coeffs->nX;
-    if (coeffs->nX > coeffs->nY) {
+    if (coeffs->nY > coeffs->nX) {
         maxChebyPoly = coeffs->nY;
     }
@@ -133,4 +197,5 @@
                                  psEvalPolynomial1D((float) x, chebPolys[i]) *
                                  psEvalPolynomial1D((float) y, chebPolys[j]);
+
                 }
             }
@@ -160,9 +225,15 @@
     }
 
-
     // Free the Chebyshev polynomials that were created in this routine.
     for (i=0;i<maxChebyPoly;i++) {
-        psFree(chebPolys[i]);
-    }
+        psPolynomial1DFree(chebPolys[i]);
+    }
+    psFree(chebPolys);
+
+    // Free some data
+    for (i=0;i<coeffs->nX;i++) {
+        psFree(sums[i]);
+    }
+    psFree(sums);
 
     return(coeffs);
@@ -199,5 +270,5 @@
     // Determine how many Chebyshev polynomials are needed, then create them.
     maxChebyPoly = coeffs->nX;
-    if (coeffs->nX > coeffs->nY) {
+    if (coeffs->nY > coeffs->nX) {
         maxChebyPoly = coeffs->nY;
     }
@@ -219,5 +290,4 @@
         }
     }
-
 
     for (x=0;x<input->numRows;x++) {
@@ -227,7 +297,6 @@
                 for (j=0;j<coeffs->nY;j++) {
                     polySum+= psEvalPolynomial1D((float)x, chebPolys[i]) *
-                              psEvalPolynomial1D((float)y, chebPolys[y]) *
+                              psEvalPolynomial1D((float)y, chebPolys[j]) *
                               coeffs->coeff[i][j];
-
                 }
             }
@@ -236,9 +305,15 @@
     }
 
-
     // Free the Chebyshev polynomials that were created in this routine.
     for (i=0;i<maxChebyPoly;i++) {
-        psFree(chebPolys[i]);
-    }
+        psPolynomial1DFree(chebPolys[i]);
+    }
+    psFree(chebPolys);
+
+    // Free some data
+    for (i=0;i<coeffs->nX;i++) {
+        psFree(sums[i]);
+    }
+    psFree(sums);
 
     return(0);
