Index: /trunk/psphot/src/psphotMakeResiduals.c
===================================================================
--- /trunk/psphot/src/psphotMakeResiduals.c	(revision 18582)
+++ /trunk/psphot/src/psphotMakeResiduals.c	(revision 18583)
@@ -69,9 +69,8 @@
     // - set output pixel, weight, and mask
 
+    // XXX need to set these correctly!!
     const int badMask = 1;              // mask bits
     const int poorMask = 2;             //       from psImageInterpolate
     const int clippedMask = 4;          // mask bit set for clipped values
-
-    bool offImage = false;              // pixel is off the image
 
     // determine the maximum image size from the input sources
@@ -150,4 +149,6 @@
         for (int ox = 0; ox < resid->Ro->numCols; ox++) {
 
+	    int nGoodPixel = 0;              // pixel is off the image
+    
             // build the vector of data values for this output pixel
             for (int i = 0; i < input->n; i++) {
@@ -160,9 +161,11 @@
 
                 mflux = 0;
-                offImage = false;
+                bool offImage = false;
                 if (psImageInterpolate (&flux, &dflux, &mflux, ix, iy, interp) == PS_INTERPOLATE_STATUS_OFF) {
                     // This pixel is off the image
                     offImage = true;
-                }
+		    fmasks->data.U8[i] = 1;
+		    // fprintf (stderr, "off image: %f %f : %f %f\n", ix, iy, flux, dflux);
+                } 
                 fluxes->data.F32[i] = flux;
                 dfluxes->data.F32[i] = dflux;
@@ -171,5 +174,17 @@
 		    fmasks->data.U8[i] = 1;
 		}
-                // fprintf (stderr, "%f %f : %f %f (%d)\n", ix, iy, flux, dflux, fmasks->data.U8[i]);
+		if (fmasks->data.U8[i] == 0) {
+		    nGoodPixel ++;
+		}            
+	    }
+
+	    // skip pixels which are off the image...
+	    bool validPixel = (SPATIAL_ORDER == 0) ? (nGoodPixel > 1) : (nGoodPixel > 3);
+            if (!validPixel) {
+                resid->Ro->data.F32[oy][ox] = 0.0;
+                resid->Rx->data.F32[oy][ox] = 0.0;
+		resid->Ry->data.F32[oy][ox] = 0.0;
+		resid->mask->data.U8[oy][ox] = 1;
+		continue;
             }
 
@@ -191,5 +206,5 @@
             }
 
-            if (offImage || SPATIAL_ORDER == 0) {
+            if (SPATIAL_ORDER == 0) {
                 // measure the desired statistic on the unclipped pixels
                 *fluxStats = *fluxStatsDef;
