Index: trunk/psModules/src/detrend/pmFlatField.c
===================================================================
--- trunk/psModules/src/detrend/pmFlatField.c	(revision 6873)
+++ trunk/psModules/src/detrend/pmFlatField.c	(revision 7278)
@@ -1,8 +1,2 @@
-//////////////////////////////////////////////////////////////////////////////////////////////////////////////
-// XXX WARNING: I have completely replaced this file with an OLD VERSION (that works) instead of the
-// one that was being worked on.
-//////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
-
 /** @file  pmFlatField.c
  *
@@ -24,6 +18,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-04-17 18:10:08 $
+ *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-06-02 00:55:23 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -46,6 +40,15 @@
 bool pmFlatField(pmReadout *in, const pmReadout *flat)
 {
-    int i = 0;
-    int j = 0;
+    PS_ASSERT_PTR_NON_NULL(in, false);
+    PS_ASSERT_PTR_NON_NULL(in->image, false);
+    PS_ASSERT_IMAGE_NON_EMPTY(in->image, false);
+    PS_ASSERT_PTR_NON_NULL(flat, false);
+    PS_ASSERT_PTR_NON_NULL(flat->image, false);
+    PS_ASSERT_IMAGE_NON_EMPTY(flat->image, false);
+    if (in->mask) {
+        PS_ASSERT_IMAGE_TYPE(in->mask, PS_TYPE_U8, false);
+    }
+    PS_ASSERT_IMAGE_TYPE(flat->image, in->image->type.type, false);
+
     int totOffCol = 0;
     int totOffRow = 0;
@@ -147,39 +150,33 @@
 
     // Macro for all PS types
-    #define PM_FLAT_DIVISION(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++) {                                                          \
-        for(i = totOffCol; i < inImage->numCols; i++) {                                                      \
-            if(flatImage->data.TYPE[j][i] <= 0.0) {                                                          \
-                /* Negative or zero flat pixels shall be masked in input image as  PM_MASK_FLAT */           \
-                if (inMask) {                                                                                \
-                    inMask->data.PS_TYPE_MASK_DATA[j][i] |= PM_MASK_FLAT;                                    \
-                }                                                                                            \
-                flatImage->data.TYPE[j][i] = 0.0;                                                            \
-            }                                                                                                \
-        }                                                                                                    \
-    }                                                                                                        \
-    for(j = totOffRow; j < inImage->numRows; j++) {                                                          \
-        for(i = totOffCol; i < inImage->numCols; i++) {                                                      \
-            if(inMask && !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];                                      \
-            }                                                                                                \
-        }                                                                                                    \
-    }                                                                                                        \
+    #define FLAT_DIVISION_CASE(TYPE) \
+case PS_TYPE_##TYPE: \
+    for (long j = totOffRow; j < inImage->numRows; j++) { \
+        for (long i = totOffCol; i < inImage->numCols; i++) { \
+            if (flatImage->data.TYPE[j][i] <= 0.0) { \
+                if (inMask) { \
+                    inMask->data.U8[j][i] |= PM_MASK_FLAT; \
+                } \
+                flatImage->data.TYPE[j][i] = 0.0; \
+            } else { \
+                if (!inMask || !inMask->data.U8[j][i]) { \
+                    inImage->data.TYPE[j][i] /= flatImage->data.TYPE[j][i]; \
+                } \
+            } \
+        } \
+    } \
     break;
 
     switch(inType) {
-        PM_FLAT_DIVISION(U8);
-        PM_FLAT_DIVISION(U16);
-        PM_FLAT_DIVISION(U32);
-        PM_FLAT_DIVISION(U64);
-        PM_FLAT_DIVISION(S8);
-        PM_FLAT_DIVISION(S16);
-        PM_FLAT_DIVISION(S32);
-        PM_FLAT_DIVISION(S64);
-        PM_FLAT_DIVISION(F32);
-        PM_FLAT_DIVISION(F64);
+        FLAT_DIVISION_CASE(U8);
+        FLAT_DIVISION_CASE(U16);
+        FLAT_DIVISION_CASE(U32);
+        FLAT_DIVISION_CASE(U64);
+        FLAT_DIVISION_CASE(S8);
+        FLAT_DIVISION_CASE(S16);
+        FLAT_DIVISION_CASE(S32);
+        FLAT_DIVISION_CASE(S64);
+        FLAT_DIVISION_CASE(F32);
+        FLAT_DIVISION_CASE(F64);
     default:
         psError( PS_ERR_BAD_PARAMETER_TYPE, true,
