Index: trunk/psModules/src/detrend/pmFlatField.c
===================================================================
--- trunk/psModules/src/detrend/pmFlatField.c	(revision 7858)
+++ trunk/psModules/src/detrend/pmFlatField.c	(revision 9612)
@@ -1,27 +1,2 @@
-/** @file  pmFlatField.c
- *
- *  @brief Given an input image and a flat field image, pmFlatField shall divide the input image by the flat
- *  field image.
- *
- *  The input image, in, and the flat field image, flat, need not be the same size, since the input image may
- *  already have been trimmed (following overscan subtraction), but the function shall use the offsets in the
- *  image (in->x0 and in->y0) to determine the appropriate offsets to obtain the correct pixel on the flat
- *  field. In the event that the flat image is too small (i.e., pixels on the input image refer to pixels
- *  outside the range of the flat image), the function shall generate an error. Pixels which are negative or
- *  zero in the flat shall be masked in the input image with the value PM_MASK_FLAT. Negative pixels in the
- *  flat may be set to zero so that they are treated identically to zeroes. Any pixels masked in the flat
- *  shall be masked with corresponding values in the output. The function shall not normalize the flat; this
- *  responsibility is left to the caller. This function is basically equivalent to a divide (with psImageOp),
- *  but with care for the region that is divided, checking for negative pixels, and copying of the mask from
- *  the flat to the output.
- *
- *  @author Ross Harman, MHPCC
- *
- *  @version $Revision: 1.13 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-07-10 20:54:52 $
- *
- *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
- */
-
 #if HAVE_CONFIG_H
 #include <config.h>
@@ -29,12 +4,9 @@
 
 #include <stdio.h>
-#include <math.h>
-#include <strings.h>
+#include <pslib.h>
 
-#include <pslib.h>
 #include "pmFPA.h"
 #include "pmFlatField.h"
 #include "pmFPAMaskWeight.h"
-#include "pmFlatFieldErrors.h"
 
 
@@ -52,8 +24,13 @@
     }
     PS_ASSERT_IMAGE_TYPE(flat->image, in->image->type.type, false);
+    if (flat->mask) {
+        PS_ASSERT_IMAGE_TYPE(flat->mask, PS_TYPE_MASK, false);
+        PS_ASSERT_IMAGES_SIZE_EQUAL(flat->mask, flat->image, false);
+    }
 
     psImage *inImage   = in->image;     // Input image
     psImage *inMask    = in->mask;      // Mask for input image
     psImage *flatImage = flat->image;   // Flat-field image
+    psImage *flatMask  = flat->mask;    // Mask for flat-field image
 
     // Check input image is not larger than flat image; mask is the same size as the input
@@ -71,6 +48,6 @@
 
     // Determine offset based on image offset with chip offset: input frame to flat frame
-    int yOffset = in->row0 + y0in - flat->col0 - y0flat;
-    int xOffset = in->row0 + x0in - flat->col0 - x0flat;
+    int yOffset = in->row0 + y0in - flat->row0 - y0flat;
+    int xOffset = in->col0 + x0in - flat->col0 - x0flat;
 
     // Check that offsets are within image limits
@@ -88,5 +65,6 @@
     for (int j = 0; j < inImage->numRows; j++) { \
         for (int i = 0; i < inImage->numCols; i++) { \
-            if (flatImage->data.TYPE[j][i] <= 0.0) { \
+            ps##TYPE flatValue = flatImage->data.TYPE[j + yOffset][i + xOffset]; \
+            if (flatValue <= 0.0 || (flatMask && flatMask->data.U8[j + yOffset][i + xOffset])) { \
                 if (inMask) { \
                     inMask->data.U8[j][i] |= PM_MASK_FLAT; \
@@ -94,5 +72,5 @@
                 inImage->data.TYPE[j][i] = SPECIAL; \
             } else { \
-                inImage->data.TYPE[j][i] /= flatImage->data.TYPE[j][i]; \
+                inImage->data.TYPE[j][i] /= flatValue; \
             } \
         } \
@@ -112,5 +90,6 @@
         FLAT_DIVISION_CASE(F64, NAN);
     default:
-        psError(PS_ERR_BAD_PARAMETER_TYPE, true, "Unsupported type for input image: %x\n", inImage->type.type);
+        psError(PS_ERR_BAD_PARAMETER_TYPE, true, "Unsupported type for input image: %x\n",
+                inImage->type.type);
         return false;
     }
