Index: trunk/psLib/src/imageops/psImageGeomManip.c
===================================================================
--- trunk/psLib/src/imageops/psImageGeomManip.c	(revision 23988)
+++ trunk/psLib/src/imageops/psImageGeomManip.c	(revision 23989)
@@ -110,38 +110,43 @@
     out = psImageRecycle(out, outCols, outRows, in->type.type);
 
-    #define PS_IMAGE_REBIN_CASE(TYPE) \
-case PS_TYPE_##TYPE: { \
-        ps##TYPE *outRowData; \
-        ps##TYPE *vecData = vec->data.TYPE; \
-        psImageMaskType *inRowMask = NULL; \
-        for (psS32 row = 0; row < outRows; row++) { \
-            outRowData = out->data.TYPE[row]; \
-            psS32 inCurrentRow = row * scale; \
-            psS32 inNextRow = (row + 1) * scale; \
-            for (psS32 col = 0; col < outCols; col++) { \
-                psS32 inCurrentCol = col * scale; \
-                psS32 inNextCol = (col + 1) * scale; \
-                psS32 n = 0; \
+#define PS_IMAGE_REBIN_CASE(TYPE)					\
+    case PS_TYPE_##TYPE: {						\
+        ps##TYPE *outRowData;						\
+        ps##TYPE *vecData = vec->data.TYPE;				\
+        psImageMaskType *inRowMask = NULL;				\
+        for (psS32 row = 0; row < outRows; row++) {			\
+            outRowData = out->data.TYPE[row];				\
+            psS32 inCurrentRow = row * scale;				\
+            psS32 inNextRow = (row + 1) * scale;			\
+            for (psS32 col = 0; col < outCols; col++) {			\
+                psS32 inCurrentCol = col * scale;			\
+                psS32 inNextCol = (col + 1) * scale;			\
+                psS32 n = 0;						\
                 for (psS32 inRow = inCurrentRow; inRow < inNextRow && inRow < inRows; inRow++) { \
-                    ps##TYPE* inRowData = in->data.TYPE[inRow]; \
-                    if (mask != NULL) { \
+                    ps##TYPE* inRowData = in->data.TYPE[inRow];		\
+                    if (mask != NULL) {					\
                         inRowMask = mask->data.PS_TYPE_IMAGE_MASK_DATA[inRow]; \
-                    } \
+                    }							\
                     for (psS32 inCol = inCurrentCol; inCol < inNextCol && inCol < inCols; inCol++) { \
-                        if (maskData != NULL) { \
+                        if (maskData != NULL) {				\
                             maskData[n] = (inRowMask[inCol] & maskVal); \
-                        } \
-                        vecData[n++] = inRowData[inCol]; \
-                    } \
-                } \
-                vec->n = n; \
-                if (maskVec) { \
-                    maskVec->n = n; \
-                } \
-                 psVectorStats(myStats, vec, NULL, maskVec, 0xff); /* the mask vector has only 0 or 1 */ \
-                outRowData[col] = (ps##TYPE)psStatsGetValue(myStats, statistic); \
-            } \
-        } \
-    } \
+                        }						\
+                        vecData[n++] = inRowData[inCol];		\
+                    }							\
+                }							\
+                vec->n = n;						\
+                if (maskVec) {						\
+                    maskVec->n = n;					\
+                }							\
+		if (!psVectorStats(myStats, vec, NULL, maskVec, 0xff)) { /* the mask vector has only 0 or 1 */ \
+		    psError(PS_ERR_UNKNOWN, false, "failure to measure stats"); \
+		    psFree(out);					\
+		    out = NULL;						\
+		    goto escape;					\
+		}							\
+		outRowData[col] = (ps##TYPE)psStatsGetValue(myStats, statistic); \
+	    }								\
+	}								\
+    }									\
     break;
 
@@ -161,7 +166,5 @@
             char* typeStr;
             PS_TYPE_NAME(typeStr,in->type.type);
-            psError(PS_ERR_BAD_PARAMETER_TYPE, true,
-                    _("Specified psImage type, %s, is not supported."),
-                    typeStr);
+            psError(PS_ERR_BAD_PARAMETER_TYPE, true, _("Specified psImage type, %s, is not supported."), typeStr);
             psFree(out);
             out = NULL;
@@ -169,4 +172,5 @@
     }
 
+escape:
     psFree(vec);
     psFree(maskVec);
Index: trunk/psLib/src/imageops/psImageMap.c
===================================================================
--- trunk/psLib/src/imageops/psImageMap.c	(revision 23988)
+++ trunk/psLib/src/imageops/psImageMap.c	(revision 23989)
@@ -211,18 +211,30 @@
             // XXX need to supply a mask and skip the masked pixels when calculating the centroid
             // this will not in general be properly weighted...
-            if (psVectorStats (map->stats, fCell, dfCell, NULL, 0)) {
+            if (!psVectorStats (map->stats, fCell, dfCell, NULL, 0)) {
+		psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+		return false;
+	    }
+
+	    // XXX ensure only one option is selected, or save both position and width
+	    map->map->data.F32[iy][ix] = psStatsGetValue (map->stats, map->stats->options);
+
+	    if (isnan(map->map->data.F32[iy][ix])) {
+                mask->data.PS_TYPE_IMAGE_MASK_DATA[iy][ix] = 1;
+	    } else {
                 mask->data.PS_TYPE_IMAGE_MASK_DATA[iy][ix] = 0;
-                // XXX ensure only one option is selected, or save both position and width
-                map->map->data.F32[iy][ix] = psStatsGetValue (map->stats, map->stats->options);
 
                 // calculate the mean position and save:
                 psStatsInit (meanStat);
-                psVectorStats (meanStat, xCell, NULL, NULL, 0);
+                if (!psVectorStats (meanStat, xCell, NULL, NULL, 0)) {
+		    psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+		    return false;
+		}
                 xCoord->data.F32[iy][ix] = psStatsGetValue (meanStat, meanStat->options);
                 psStatsInit (meanStat);
-                psVectorStats (meanStat, yCell, NULL, NULL, 0);
+                if (!psVectorStats (meanStat, yCell, NULL, NULL, 0)) {
+		    psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+		    return false;
+		}
                 yCoord->data.F32[iy][ix] = psStatsGetValue (meanStat, meanStat->options);
-            } else {
-                mask->data.PS_TYPE_IMAGE_MASK_DATA[iy][ix] = 1;
             }
 
Index: trunk/psLib/src/imageops/psImageMapFit.c
===================================================================
--- trunk/psLib/src/imageops/psImageMapFit.c	(revision 23988)
+++ trunk/psLib/src/imageops/psImageMapFit.c	(revision 23989)
@@ -73,5 +73,8 @@
 
         // XXX does ROBUST_MEDIAN work with weight?
-        psVectorStats(map->stats, f, NULL, mask, maskValue);
+        if (!psVectorStats(map->stats, f, NULL, mask, maskValue)) {
+	    psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	    return false;
+	}
 
         map->map->data.F32[0][0]   = psStatsGetValue(map->stats, mean);
Index: trunk/psLib/src/imageops/psImagePixelExtract.c
===================================================================
--- trunk/psLib/src/imageops/psImagePixelExtract.c	(revision 23988)
+++ trunk/psLib/src/imageops/psImagePixelExtract.c	(revision 23989)
@@ -419,35 +419,40 @@
         }
 
-#define PSIMAGE_CUT_VERTICAL(TYPE) \
-    case PS_TYPE_##TYPE: { \
-            psVectorMaskType* maskVecData = NULL; \
-            for (psS32 c = col0; c < col1; c++) { \
-                ps##TYPE *imgData = input->data.TYPE[row0] + c; \
-                ps##TYPE *imgVecData = imgVec->data.TYPE; \
-                if (maskVec != NULL) { \
+#define PSIMAGE_CUT_VERTICAL(TYPE)					\
+	case PS_TYPE_##TYPE: {						\
+            psVectorMaskType* maskVecData = NULL;			\
+            for (psS32 c = col0; c < col1; c++) {			\
+                ps##TYPE *imgData = input->data.TYPE[row0] + c;		\
+                ps##TYPE *imgVecData = imgVec->data.TYPE;		\
+                if (maskVec != NULL) {					\
                     maskVecData = maskVec->data.PS_TYPE_VECTOR_MASK_DATA; \
                     maskData = &mask->data.PS_TYPE_IMAGE_MASK_DATA[row0][c]; /* XXX double check this... */ \
                     /** old entry: maskData = (psMaskType* )(mask->data.PS_TYPE_IMAGE_MASK_DATA[row0]) + c; */ \
-                } \
-                for (psS32 r = row0; r < row1; r++) { \
-                   *imgVecData = *imgData; \
-                    imgVecData ++; \
-                    imgData += inCols; \
-                    if (maskVecData != NULL) { \
-                        *maskVecData = (*maskData & maskVal); \
-                        maskVecData ++; \
-                        maskData += inCols; \
-                    } \
-                } \
-                psVectorStats(myStats, imgVec, NULL, maskVec, 0xff); \
-                *outData = psStatsGetValue(myStats, statistic); \
-                if (outPosition != NULL) { \
-                    outPosition->x = c; \
-                    outPosition->y = row0; \
-                    outPosition += delta; \
-                } \
-                outData += delta; \
-            } \
-            break; \
+                }							\
+                for (psS32 r = row0; r < row1; r++) {			\
+		    *imgVecData = *imgData;				\
+                    imgVecData ++;					\
+                    imgData += inCols;					\
+                    if (maskVecData != NULL) {				\
+                        *maskVecData = (*maskData & maskVal);		\
+                        maskVecData ++;					\
+                        maskData += inCols;				\
+                    }							\
+                }							\
+                if (!psVectorStats(myStats, imgVec, NULL, maskVec, 0xff)) { \
+		    psError(PS_ERR_UNKNOWN, false, "failure to measure stats"); \
+		    psFree(out);					\
+		    out = NULL;						\
+		    break;						\
+		}							\
+                *outData = psStatsGetValue(myStats, statistic);		\
+                if (outPosition != NULL) {				\
+                    outPosition->x = c;					\
+                    outPosition->y = row0;				\
+                    outPosition += delta;				\
+                }							\
+                outData += delta;					\
+            }								\
+            break;							\
         }
 
@@ -466,7 +471,5 @@
                 char* typeStr;
                 PS_TYPE_NAME(typeStr,type);
-                psError(PS_ERR_BAD_PARAMETER_TYPE, true,
-                        _("Specified psImage type, %s, is not supported."),
-                        typeStr);
+                psError(PS_ERR_BAD_PARAMETER_TYPE, true, _("Specified psImage type, %s, is not supported."), typeStr);
                 psFree(out);
                 out = NULL;
@@ -534,5 +537,10 @@
 	    }
 
-            psVectorStats(myStats, imgVec, NULL, maskVec, 0xff);
+            if (!psVectorStats(myStats, imgVec, NULL, maskVec, 0xff)) {
+		psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+		psFree (out);
+		out = NULL;
+		break;
+	    }
             *outData = psStatsGetValue(myStats, statistic);
             if (outPosition != NULL) {
@@ -933,5 +941,10 @@
 
     for (psS32 r = 0; r < numOut; r++) {
-        psVectorStats(myStats, buffer[r], NULL, bufferMask[r], 0xff);
+        if (!psVectorStats(myStats, buffer[r], NULL, bufferMask[r], 0xff)){
+	    psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	    psFree(out);
+	    out = NULL;
+	    break;
+	}
         outData[r] = psStatsGetValue(myStats, statistic);
     }
Index: trunk/psLib/src/imageops/psImageStats.c
===================================================================
--- trunk/psLib/src/imageops/psImageStats.c	(revision 23988)
+++ trunk/psLib/src/imageops/psImageStats.c	(revision 23989)
@@ -104,5 +104,9 @@
     }
 
-    psVectorStats(stats, junkData, NULL, junkMask, 0xff);
+    if (!psVectorStats(stats, junkData, NULL, junkMask, 0xff)) {
+	psFree(junkMask);
+	psFree(junkData);
+	return false;
+    }
 
     psFree(junkMask);
