Index: trunk/psModules/src/pmFlatField.c
===================================================================
--- trunk/psModules/src/pmFlatField.c	(revision 1815)
+++ trunk/psModules/src/pmFlatField.c	(revision 1822)
@@ -1,20 +1,23 @@
-/** @file  pmFlatField.cS
+/** @file  pmFlatField.c
  *
- *  @brief Given an input image and a flat-field image, pmFlatField shall divide the input image by the flat-field image.
+ *  @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.
+ *  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.7 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-09-15 01:08:23 $
+ *  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-09-17 00:49:04 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -29,13 +32,7 @@
 
 #include "pmFlatField.h"
+#include "pmMaskBadPixels.h"
 
-
-#define CHECK_NULL(STRUCT)                                                                                   \
-if (STRUCT == NULL) {                                                                                        \
-    psError(__func__, " : Line %d - Null not allowed for %s", __LINE__, #STRUCT);                            \
-    return false;                                                                                            \
-}                                                                                                            \
-
-bool pmFlatField(psReadout *in, psReadout *inMask, const psReadout *flat)
+bool pmFlatField(psReadout *in, const psReadout *flat)
 {
     int i = 0;
@@ -47,85 +44,98 @@
     psElemType maskType;
     psImage *inImage = NULL;
+    psImage *inMask = NULL;
     psImage *flatImage = NULL;
-    psImage *maskImage = NULL;
 
-    CHECK_NULL(in);
-    CHECK_NULL(flat);
-    CHECK_NULL(inMask);
 
-    inImage = in->image;
-    flatImage = flat->image;
-    maskImage = inMask->image;
-
-    CHECK_NULL(inImage);
-    CHECK_NULL(flatImage);
-    CHECK_NULL(maskImage);
-
-    totOffCol = inImage->col0 + in->col0;
-    totOffRow = inImage->row0 + in->row0;
-
-    if(inImage->numRows > flatImage->numRows) {
-        psError(__func__, " : Line %d - Input image exceeds flat image in number of rows. (%d vs %d).",
-                __LINE__, inImage->numRows, flatImage->numRows);
+    // Check for nulls
+    if (in == NULL) {
+        psError(__func__, " : Line %d - Null not allowed for input readout", __LINE__);
+        return false;
+    } else if(flat==NULL) {
+        psError(__func__, " : Line %d - Null not allowed for flat readout", __LINE__);
         return false;
     }
 
-    if(inImage->numCols > flatImage->numCols) {
-        psError(__func__, " : Line %d - Input image exceeds flat image in number of columns. (%d vs %d).",
+    inImage = in->image;
+    inMask = in->mask;
+    flatImage = flat->image;
+    if (inImage == NULL) {
+        psError(__func__, " : Line %d - Null not allowed for input image", __LINE__);
+        return false;
+    } else if(flatImage) {
+        psError(__func__, " : Line %d - Null not allowed for flat image", __LINE__);
+        return false;
+    } else if(inMask == NULL) {
+        inMask = psImageAlloc(inImage->numCols, inImage->numRows, PS_TYPE_MASK);
+    }
+
+    // Check input image and its mask are not larger than flat image
+    if(inImage->numRows > flatImage->numRows) {
+        psError(__func__, " : Line %d - Input image exceeds mask image in number of rows. (%d vs %d).",
+                __LINE__, inImage->numRows, flatImage->numRows);
+        return false;
+    } else if(inImage->numCols > flatImage->numCols) {
+        psError(__func__, " : Line %d - Input image exceeds mask image in number of columns. (%d vs %d).",
+                __LINE__, inImage->numCols, flatImage->numCols);
+        return false;
+    } else if(inMask->numRows > flatImage->numRows) {
+        psError(__func__, " : Line %d - Input image mask exceeds mask image in number of rows. (%d vs %d).",
+                __LINE__, inImage->numRows, flatImage->numRows);
+        return false;
+    } else if(inMask->numCols > flatImage->numCols) {
+        psError(__func__, " : Line %d - Input image mask exceeds mask image in number of columns. (%d vs %d).",
                 __LINE__, inImage->numCols, flatImage->numCols);
         return false;
     }
 
+    // Determine total offset based on image offset with chip offset
+    totOffCol = inImage->col0 + in->col0;
+    totOffRow = inImage->row0 + in->row0;
+
+    // Check that offsets are within image limits
     if(totOffRow > flatImage->numRows-1) {
-        psError(__func__, " : Line %d - Initial row offset exceeds that of flat image. (%d vs %d).",
+        psError(__func__, " : Line %d - Initial row offset exceeds that of mask image. (%d vs %d).",
                 __LINE__, totOffRow, flatImage->numRows);
+        return false;
+    } else if(totOffCol > flatImage->numCols-1) {
+        psError(__func__, " : Line %d - Initial column offset exceeds that of mask image. (%d vs %d).",
+                __LINE__, totOffCol, flatImage->numCols);
+        return false;
+    } else if(totOffRow > inImage->numRows-1) {
+        psError(__func__, " : Line %d - Initial row offset exceeds that of input image. (%d vs %d).",
+                __LINE__, totOffRow, inImage->numRows);
+        return false;
+    } else if(totOffCol > inImage->numCols-1) {
+        psError(__func__, " : Line %d - Initial column offset exceeds that of input image. (%d vs %d).",
+                __LINE__, totOffCol, inImage->numCols);
+        return false;
+    } else if(totOffRow > inMask->numRows-1) {
+        psError(__func__, " : Line %d - Initial row offset exceeds that of input image mask. (%d vs %d).",
+                __LINE__, totOffRow, inMask->numRows);
+        return false;
+    } else if(totOffCol > inMask->numCols-1) {
+        psError(__func__, " : Line %d - Initial column offset exceeds that of input image mask. (%d vs %d).",
+                __LINE__, totOffCol, inMask->numCols);
         return false;
     }
 
-    if(totOffCol > flatImage->numCols-1) {
-        psError(__func__, " : Line %d - Initial column offset exceeds that of flat image. (%d vs %d).",
-                __LINE__, totOffCol, flatImage->numCols);
-        return false;
-    }
-
-    if(totOffRow > inImage->numRows-1) {
-        psError(__func__, " : Line %d - Initial row offset exceeds that of input image. (%d vs %d).",
-                __LINE__, totOffRow, inImage->numRows);
-        return false;
-    }
-
-    if(totOffCol > inImage->numCols-1) {
-        psError(__func__, " : Line %d - Initial column offset exceeds that of input image. (%d vs %d).",
-                __LINE__, totOffCol, inImage->numCols);
-        return false;
-    }
-
+    // Check for incorrect types
     inType = inImage->type.type;
     flatType = flatImage->type.type;
-    maskType = maskImage->type.type;
-
+    maskType = inMask->type.type;
     if(PS_IS_PSELEMTYPE_COMPLEX(inType)) {
-        psError(__func__, " : Line %d - Complex types not allowed for input image", __LINE__);
+        psError(__func__, " : Line %d - Complex types not allowed for input image. Type: %d", __LINE__,
+                inType);
         return false;
-    }
-
-    if(PS_IS_PSELEMTYPE_COMPLEX(flatType)) {
-        psError(__func__, " : Line %d - Complex types not allowedfor flat image", __LINE__);
+    } else if(PS_IS_PSELEMTYPE_COMPLEX(flatType)) {
+        psError(__func__, " : Line %d - Complex types not allowed for flat image. Type: %d", __LINE__,
+                flatType);
         return false;
-    }
-
-    if(PS_IS_PSELEMTYPE_COMPLEX(maskType)) {
-        psError(__func__, " : Line %d - Complex types not allowed for mask image", __LINE__);
+    } else if(maskType != PS_TYPE_MASK) {
+        psError(__func__, " : Line %d - Mask must be PS_TYPE_MASK type. Type: %d", __LINE__, maskType);
         return false;
-    }
-
-    if(PS_IS_PSELEMTYPE_REAL(maskType)) {
-        psError(__func__, " : Line %d - Floating point types not allowed for mask image", __LINE__);
-        return false;
-    }
-
-    if(inType != flatType) {
-        psError(__func__, " : Line %d - Input and flat image types don't agree. (%d vs %d).",
-                __LINE__, inType, flatType);
+    } else if(inType != flatType) {
+        psError(__func__, " : Line %d - Input and flat image types differ. (%d vs %d)", __LINE__, inType,
+                flatType);
         return false;
     }
@@ -133,5 +143,5 @@
     // Macro for all PS types
     #define PM_FLAT_DIVISION(TYPE)                                                                           \
-case PS_TYPE_##TYPE:                                                                                     \
+case PS_TYPE_##TYPE:                                                                                         \
     /* Per Eugene's request, use two sets of loops: first to fill mask, second to avoid div with bad pix */  \
     for(j = totOffRow; j < inImage->numRows; j++) {                                                          \
@@ -139,5 +149,5 @@
             if(flatImage->data.TYPE[j][i] <= 0.0) {                                                          \
                 /* Negative or zero flat pixels shall be masked in input image as  PM_MASK_FLAT */           \
-                maskImage->data.F64[j][i] = PM_MASK_FLAT;                                                    \
+                inMask->data.PS_TYPE_MASK_DATA[j][i] = PM_MASK_FLAT;                                         \
             }                                                                                                \
         }                                                                                                    \
@@ -145,5 +155,5 @@
     for(j = totOffRow; j < inImage->numRows; j++) {                                                          \
         for(i = totOffCol; i < inImage->numCols; i++) {                                                      \
-            if(!maskImage->data.F64[j][i]) {                                                                 \
+            if(!inMask->data.PS_TYPE_MASK_DATA[j][i]) {                                                      \
                 /* Module shall divide the input image by the flat-fielded image */                          \
                 inImage->data.TYPE[j][i] /= flatImage->data.TYPE[j][i];                                      \
@@ -165,7 +175,7 @@
         PM_FLAT_DIVISION(F64);
     default:
-        psError(__func__, "Unsupported image datatype (%d)", inType);
+        psError(__func__, "Unsupported image datatype. Type: %d", inType);
     }
 
-    return in;
+    return true;
 }
