Index: trunk/psLib/src/imageops/psImageBackground.c
===================================================================
--- trunk/psLib/src/imageops/psImageBackground.c	(revision 10169)
+++ trunk/psLib/src/imageops/psImageBackground.c	(revision 10273)
@@ -26,5 +26,5 @@
     PS_ASSERT_FLOAT_LESS_THAN_OR_EQUAL(fmin, 1.0, NULL);
     PS_ASSERT_FLOAT_LESS_THAN_OR_EQUAL(fmax, 1.0, NULL);
-    PS_ASSERT_INT_POSITIVE(nMax, NULL);
+    PS_ASSERT_INT_NONNEGATIVE(nMax, NULL);
     PS_ASSERT_PTR_NON_NULL(rng, NULL);
 
@@ -33,6 +33,6 @@
     long ny = image->numRows;
 
-    int Nsubset = PS_MIN(nMax, nx*ny);  // Number of pixels in nubset
-    int Npixels = nx*ny;                // Total number of pixels
+    const int Npixels = nx*ny;                // Total number of pixels
+    const int Nsubset = (nMax == 0) ? Npixels : PS_MIN(nMax, Npixels); // Number of pixels in subset
 
     psVector *values = psVectorAllocEmpty(Nsubset, PS_TYPE_F32); // Vector containing subsample
@@ -95,5 +95,12 @@
             stats->clipSigma = 1.0;
         }
-        psVectorStats (stats, values, NULL, NULL, 0);
+        if (psVectorStats (stats, values, NULL, NULL, 0) == NULL) {
+            psError(PS_ERR_UNKNOWN, false, "Unable to measure statistics for image background "
+                    "(%dx%d, (row0,col0) = (%d,%d)",
+                    image->numRows, image->numCols, image->row0, image->col0);
+            psFree(stats);
+            psFree(values);
+            return NULL;
+        }
     }
 
