Index: trunk/ppImage/src/ppImageRebinReadout.c
===================================================================
--- trunk/ppImage/src/ppImageRebinReadout.c	(revision 18216)
+++ trunk/ppImage/src/ppImageRebinReadout.c	(revision 18724)
@@ -5,5 +5,5 @@
 #include "ppImage.h"
 
-bool ppImageRebinChip (pmConfig *config, pmFPAview *view, char *outName) {
+bool ppImageRebinChip (pmConfig *config, pmFPAview *view, ppImageOptions *options, char *outName) {
 
     pmCell *cell;
@@ -33,5 +33,5 @@
 
             // run the rebin code
-            ppImageRebinReadout (outReadout, inReadout, outFile);
+            ppImageRebinReadout (outReadout, inReadout, outFile, options);
         }
 
@@ -61,5 +61,5 @@
 
 // XXX this should be made consistent with psImageBinning
-bool ppImageRebinReadout (pmReadout *output, pmReadout *input, pmFPAfile *outFile)
+bool ppImageRebinReadout (pmReadout *output, pmReadout *input, pmFPAfile *outFile, ppImageOptions *options)
 {
     PS_ASSERT_PTR_NON_NULL(output, false);
@@ -80,17 +80,32 @@
     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]) continue;
+		    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 ++;
                 }
             }
-            output->image->data.F32[yOut][xOut] = value / 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;
+	      }
+	    }
         }
     }
