Index: /trunk/psModules/src/pmFlatField.c
===================================================================
--- /trunk/psModules/src/pmFlatField.c	(revision 1812)
+++ /trunk/psModules/src/pmFlatField.c	(revision 1813)
@@ -15,6 +15,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-09-14 18:08:14 $
+ *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-09-15 01:04:29 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -33,37 +33,40 @@
 #define CHECK_NULL(STRUCT)                                                                                   \
 if (STRUCT == NULL) {                                                                                        \
-    psAbort(__func__, " : Line %d - Null not allowed for %s", __LINE__, #STRUCT);                            \
+    psError(__func__, " : Line %d - Null not allowed for %s", __LINE__, #STRUCT);                            \
+    return false;                                                                                            \
 }                                                                                                            \
 
-psReadout *pmFlatField(psReadout *in, psReadout *inMask, const psReadout *flat, const psReadout *flatMask)
+bool pmFlatField(psReadout *in, psReadout *inMask, const psReadout *flat)
 {
     int i = 0;
     int j = 0;
+    int totOffCol = 0;
+    int totOffRow = 0;
     psElemType inType;
     psElemType flatType;
+    psElemType maskType;
     psImage *inImage = NULL;
     psImage *flatImage = NULL;
-    psImage *inMaskImage = NULL;
-    psImage *flatMaskImage = NULL;
+    psImage *maskImage = NULL;
 
     CHECK_NULL(in);
     CHECK_NULL(flat);
     CHECK_NULL(inMask);
-    CHECK_NULL(flatMask);
 
     inImage = in->image;
     flatImage = flat->image;
-    inMaskImage = inMask->image;
-    flatMaskImage = flatMask->image;
+    maskImage = inMask->image;
 
     CHECK_NULL(inImage);
     CHECK_NULL(flatImage);
-    CHECK_NULL(inMaskImage);
-    CHECK_NULL(flatMaskImage);
+    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);
-        return NULL;
+        return false;
     }
 
@@ -71,21 +74,55 @@
         psError(__func__, " : Line %d - Input image exceeds flat image in number of columns. (%d vs %d).",
                 __LINE__, inImage->numCols, flatImage->numCols);
-        return NULL;
+        return false;
     }
 
-    if(inImage->row0 > flatImage->numRows-1) {
-        psError(__func__, " : Line %d - Input image row offset exceeds number of flat image rows. (%d vs %d).",
-                __LINE__, inImage->row0, flatImage->numRows);
-        return NULL;
+    if(totOffRow > flatImage->numRows-1) {
+        psError(__func__, " : Line %d - Initial row offset exceeds that of flat image. (%d vs %d).",
+                __LINE__, totOffRow, flatImage->numRows);
+        return false;
     }
 
-    if(inImage->col0 > flatImage->numCols-1) {
-        psError(__func__, " : Line %d - Input image column offset exceeds number of flat image columns. (%d vs %d).",
-                __LINE__, inImage->col0, flatImage->numCols);
-        return NULL;
+    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;
     }
 
-    inType = in->image->type.type;
-    flatType = flat->image->type.type;
+    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;
+    }
+
+    inType = inImage->type.type;
+    flatType = flatImage->type.type;
+    maskType = maskImage->type.type;
+
+    if(PS_IS_PSELEMTYPE_COMPLEX(inType)) {
+        psError(__func__, " : Line %d - Complex types not allowed for input image", __LINE__);
+        return false;
+    }
+
+    if(PS_IS_PSELEMTYPE_COMPLEX(flatType)) {
+        psError(__func__, " : Line %d - Complex types not allowedfor flat image", __LINE__);
+        return false;
+    }
+
+    if(PS_IS_PSELEMTYPE_COMPLEX(maskType)) {
+        psError(__func__, " : Line %d - Complex types not allowed for mask image", __LINE__);
+        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).",
@@ -96,25 +133,22 @@
     // Macro for all PS types
     #define PM_FLAT_DIVISION(TYPE)                                                                           \
-case PS_TYPE_##TYPE:                                                                                         \
-    for(j = inImage->row0; j < inImage->numRows; j++) {                                                      \
-        for(i = inImage->col0; i < inImage->numCols; i++) {                                                  \
-            if(creal(flatImage->data.TYPE[j][i]) <= 0.0) {                                                   \
+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 */           \
-                inMaskImage->data.F64[j][i] = PM_MASK_FLAT;                                                  \
-            } else if(flatMaskImage->data.TYPE[j][i]){                                                       \
-                /* Pixels masked in the flat shall be masked with same values in the output */               \
-                inMaskImage->data.TYPE[j][i] = flatMaskImage->data.TYPE[j][i];                               \
+                maskImage->data.F64[j][i] = PM_MASK_FLAT;                                                    \
             }                                                                                                \
-            if(creal(flatImage->data.TYPE[j][i]) < 0.0) {                                                    \
-                /* Negative pixels from the flat image may be set to zero */                                 \
-                inImage->data.TYPE[j][i] = 0.0;                                                              \
-            } else if(fabs(flatImage->data.TYPE[j][i]) < FLT_EPSILON) {                                      \
-                psError(__func__, " : Line %d - Divide by zero. Row: %d Col: %d",__LINE__, j, i);            \
-            } else {                                                                                         \
+        }                                                                                                    \
+    }                                                                                                        \
+    for(j = totOffRow; j < inImage->numRows; j++) {                                                          \
+        for(i = totOffCol; i < inImage->numCols; i++) {                                                      \
+            if(maskImage->data.F64[j][i] == 0) {                                                             \
                 /* Module shall divide the input image by the flat-fielded image */                          \
                 inImage->data.TYPE[j][i] = inImage->data.TYPE[j][i]/flatImage->data.TYPE[j][i];              \
             }                                                                                                \
-        }                                                                                                     \
-    }                                                                                                         \
+        }                                                                                                    \
+    }                                                                                                        \
     break;
 
@@ -130,6 +164,4 @@
         PM_FLAT_DIVISION(F32);
         PM_FLAT_DIVISION(F64);
-        PM_FLAT_DIVISION(C32);
-        PM_FLAT_DIVISION(C64);
     default:
         psError(__func__, "Unsupported image datatype (%d)", inType);
