Index: trunk/ppSub/src/ppSubReadout.c
===================================================================
--- trunk/ppSub/src/ppSubReadout.c	(revision 13662)
+++ trunk/ppSub/src/ppSubReadout.c	(revision 13738)
@@ -10,15 +10,10 @@
 #include "ppSub.h"
 
-#define MASK_BAD      0x01              // Mask value for bad pixel
-#define MASK_STAMP    0x02              // Mask value for bad stamp (and bad stamp region)
-
 bool ppSubReadout(pmConfig *config, const pmFPAview *view)
 {
     pmReadout *inRO = pmFPAfileThisReadout(config->files, view, "PPSUB.INPUT"); // Input readout
     pmReadout *refRO = pmFPAfileThisReadout(config->files, view, "PPSUB.REF"); // Reference readout
-#if 0
     pmCell *outCell = pmFPAfileThisCell(config->files, view, "PPSUB.OUTPUT"); // Output cell
     pmReadout *outRO = pmReadoutAlloc(outCell); // Output readout
-#endif
 
     psImage *input = inRO->image;       // Input image
@@ -49,15 +44,9 @@
     if (!refRO->mask) {
         refRO->mask = psImageAlloc(numCols, numRows, PS_TYPE_MASK);
-        psImageInit(inRO->mask, 0);
+        psImageInit(refRO->mask, 0);
     }
 
-    // Worried about the masks for bad pixels and bad stamps colliding, so make our own mask
-    psImage *stampMask = psImageAlloc(numCols, numRows, PS_TYPE_MASK); // Mask to use for stamps
-    for (int y = 0; y < numRows; y++) {
-        for (int x = 0; x < numCols; x++) {
-            stampMask->data.PS_TYPE_MASK_DATA[y][x] =
-                (refRO->mask->data.PS_TYPE_MASK_DATA[y][x] & maskBad) ? MASK_BAD : 0;
-        }
-    }
+    // Mask for subtraction
+    psImage *subMask = pmSubtractionMask(inRO->mask, refRO->mask, maskBad, size, footprint);
 
 #if 0
@@ -84,6 +73,5 @@
     int numRejected = -1;               // Number of rejected stamps in each iteration
     for (int i = 0; i < iter && numRejected != 0; i++) {
-        stamps = pmSubtractionFindStamps(stamps, refRO->image, stampMask, MASK_BAD, MASK_STAMP,
-                                         threshold, spacing, size + footprint);
+        stamps = pmSubtractionFindStamps(stamps, refRO->image, subMask, threshold, spacing);
         if (!stamps) {
             psError(PS_ERR_UNKNOWN, false, "Unable to find stamps on reference image.");
@@ -103,5 +91,5 @@
         }
 
-        numRejected = pmSubtractionRejectStamps(stamps, refRO->image, inRO->image, stampMask, MASK_STAMP,
+        numRejected = pmSubtractionRejectStamps(stamps, refRO->image, inRO->image, subMask,
                                                 solution, footprint, rej, kernels);
         if (numRejected < 0) {
@@ -111,5 +99,4 @@
         psLogMsg("ppSub", PS_LOG_INFO, "%d stamps rejected on iteration %d.", numRejected, i);
     }
-    psFree(stampMask);
 
     if (numRejected > 0) {
@@ -122,23 +109,34 @@
 
     psImage *convImage = NULL, *convWeight = NULL, *convMask = NULL; // Convolved images
-    if (!pmSubtractionConvolve(&convImage, &convWeight, &convMask,
-                               refRO->image, refRO->weight, refRO->mask,
-                               MASK_BAD, maskBlank, solution, kernels)) {
+    if (!pmSubtractionConvolve(&convImage, &convWeight, &convMask, refRO->image, refRO->weight, subMask,
+                               maskBlank, solution, kernels)) {
         psError(PS_ERR_UNKNOWN, false, "Unable to convolve reference image.");
         goto ERROR;
     }
+    psFree(subMask);
 
     // Do the subtraction
     if (reverse) {
-        (void)psBinaryOp(inRO->image, convImage, "-", inRO->image);
+        outRO->image = (psImage*)psBinaryOp(NULL, convImage, "-", inRO->image);
     } else {
-        (void)psBinaryOp(inRO->image, inRO->image, "-", convImage);
+        outRO->image = (psImage*)psBinaryOp(NULL, inRO->image, "-", convImage);
     }
-    (void)psBinaryOp(inRO->mask, convMask, "|", inRO->mask);
-    (void)psBinaryOp(inRO->weight, convWeight, "+", inRO->weight);
+    outRO->mask = (psImage*)psBinaryOp(NULL, convMask, "|", inRO->mask);
+    if (convWeight) {
+        outRO->weight = (psImage*)psBinaryOp(NULL, convWeight, "+", inRO->weight);
+    }
 
     psFree(convImage);
     psFree(convMask);
     psFree(convWeight);
+
+    outRO->data_exists = true;
+    outCell->data_exists = true;
+    outCell->parent->data_exists = true;
+
+    if (!pmFPACopyConcepts(outCell->parent->parent, inRO->parent->parent->parent)) {
+        psError(PS_ERR_UNKNOWN, false, "Unable to copy concepts from input to output.");
+        return false;
+    }
 
 #if 0
