Index: branches/pap/psLib/src/imageops/psImageGeomManip.c
===================================================================
--- branches/pap/psLib/src/imageops/psImageGeomManip.c	(revision 23948)
+++ branches/pap/psLib/src/imageops/psImageGeomManip.c	(revision 25027)
@@ -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);
