Index: /trunk/psLib/src/imageops/psImageBackground.c
===================================================================
--- /trunk/psLib/src/imageops/psImageBackground.c	(revision 17370)
+++ /trunk/psLib/src/imageops/psImageBackground.c	(revision 17371)
@@ -42,5 +42,12 @@
     const int Nsubset = (stats->nSubsample == 0) ? Npixels : PS_MIN(stats->nSubsample, Npixels); // Number of pixels in subset
 
-    psVector *values = psVectorAllocEmpty(Nsubset, PS_TYPE_F32); // Vector containing subsample
+    psVector *values;                   // Vector containing subsample
+    if (sample) {
+        *sample = psVectorRecycle(*sample, Nsubset, PS_TYPE_F32);
+        values = psMemIncrRefCounter(*sample);
+        values->n = 0;
+    } else {
+        values = psVectorAllocEmpty(Nsubset, PS_TYPE_F32);
+    }
 
     // Minimum and maximum values
@@ -67,7 +74,7 @@
     }
     if (n < 0.01*Nsubset) {
-	psLogMsg ("psLib", PS_LOG_INFO, "too few data points");
-	psFree (values);
-	return false;
+        psLogMsg ("psLib", PS_LOG_INFO, "too few data points");
+        psFree (values);
+        return false;
     }
 
@@ -121,9 +128,6 @@
     }
 
-    if (sample != NULL) {
-	*sample = values;
-    } else {
-	psFree(values);
-    }
+    psFree(values);
+
     return true;
 }
