Index: trunk/ppImage/src/ppImageRebinReadout.c
===================================================================
--- trunk/ppImage/src/ppImageRebinReadout.c	(revision 20419)
+++ trunk/ppImage/src/ppImageRebinReadout.c	(revision 20438)
@@ -10,5 +10,5 @@
     pmReadout *inReadout, *outReadout;
 
-    pmFPAfile *outFile = psMetadataLookupPtr (NULL, config->files, outName);
+    pmFPAfile *outFile = psMetadataLookupPtr(NULL, config->files, outName);
     if (outFile == NULL) return false;
 
@@ -33,26 +33,9 @@
 
             // run the rebin code
-            ppImageRebinReadout (outReadout, inReadout, outFile, options);
+            if (!pmReadoutRebin(outReadout, inReadout, options->maskVal, outFile->xBin, outFile->yBin)) {
+                psError(PS_ERR_UNKNOWN, false, "Unable to rebin readout.");
+                return false;
+            }
         }
-
-        // Get the original values for the binning
-        bool mdok = true;               // Status of MD lookup
-        int xBin = psMetadataLookupS32(&mdok, cell->concepts, "CELL.XBIN"); // The binning in x
-        if (!mdok || xBin <= 0) {
-            psLogMsg(__func__, PS_LOG_WARN, "CELL.XBIN is not set --- assuming 1.\n");
-            xBin = 1;
-        }
-        int yBin = psMetadataLookupS32(&mdok, cell->concepts, "CELL.YBIN"); // The binning in y
-        if (!mdok || yBin <= 0) {
-            psLogMsg(__func__, PS_LOG_WARN, "CELL.YBIN is not set --- assuming 1.\n");
-            yBin = 1;
-        }
-
-        // Update the concepts with the new values for the binning
-        pmCell *outCell = pmFPAviewThisCell(view, outFile->fpa); // The output cell
-        psMetadataItem *binItem = psMetadataLookup(outCell->concepts, "CELL.XBIN");
-        binItem->data.S32 = xBin * outFile->xBin;
-        binItem = psMetadataLookup(outCell->concepts, "CELL.YBIN");
-        binItem->data.S32 = yBin * outFile->yBin;
     }
 
@@ -60,55 +43,2 @@
 }
 
-// XXX this should be made consistent with psImageBinning
-bool ppImageRebinReadout (pmReadout *output, pmReadout *input, pmFPAfile *outFile, ppImageOptions *options)
-{
-    PS_ASSERT_PTR_NON_NULL(output, false);
-    PS_ASSERT_IMAGE_NON_NULL(output->image, false);
-    PS_ASSERT_PTR_NON_NULL(input, false);
-    PS_ASSERT_IMAGE_NON_NULL(input->image, false);
-    PS_ASSERT_PTR_NON_NULL(outFile, false);
-
-    // the binning process must not change the size of the output image...
-    // psStats *stats = psStatsAlloc (PS_STATS_SAMPLE_MEAN);
-    // psImageRebin (output->image, input->image, NULL, 0, scale, stats);
-    // psFree (stats);
-
-    int dX = outFile->xBin;
-    int dY = outFile->yBin;
-
-    int nX = input->image->numCols;
-    int nY = input->image->numRows;
-
-    // we should *either* skip masked pixels or replace masked pixels with the specified value 
-    
-
-    // do the rebinning by hand, mean only for test
-    for (int yOut = 0; yOut < output->image->numRows; yOut++) {
-        for (int xOut = 0; xOut < output->image->numCols; xOut++) {
-	    float maskedValue = 0.0;
-            float value = 0;
-            int nPix = 0;
-            for (int yIn = yOut * dY; (yIn < yOut * dY + dY) && (yIn < nY); yIn ++) {
-                for (int xIn = xOut * dX; (xIn < xOut * dX + dX) && (xIn < nX); xIn ++) {
-		    if (input->mask && input->mask->data.U8[yIn][xIn]) {
-		      maskedValue = input->image->data.F32[yIn][xIn];
-		      continue;
-		    }
-                    value += input->image->data.F32[yIn][xIn];
-                    nPix ++;
-                }
-            }
-	    if (nPix > 0) {
-	      output->image->data.F32[yOut][xOut] = value / nPix;
-	    } else {
-	      if (options->replaceMasked) {
-		output->image->data.F32[yOut][xOut] = maskedValue;
-	      } else {
-		output->image->data.F32[yOut][xOut] = 0.0;
-	      }
-	    }
-        }
-    }
-
-    return true;
-}
