Index: trunk/psModules/src/detrend/pmFlatField.c
===================================================================
--- trunk/psModules/src/detrend/pmFlatField.c	(revision 5435)
+++ trunk/psModules/src/detrend/pmFlatField.c	(revision 5516)
@@ -18,6 +18,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-10-20 23:06:24 $
+ *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-11-15 20:09:03 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -36,4 +36,5 @@
 #include "pmMaskBadPixels.h"
 #include "pmFlatFieldErrors.h"
+#include "pmSubtractBias.h"
 
 
@@ -42,6 +43,20 @@
     const pmReadout *flat)
 {
+    // XXX: Use the proper image and readout asserts.
+    PS_ASSERT_PTR_NON_NULL(in, true);
+    PS_ASSERT_PTR_NON_NULL(in->image, false);
+    PS_ASSERT_PTR_NON_NULL(in->mask, false);
+    PS_ASSERT_PTR_NON_NULL(flat, false);
+    PS_ASSERT_PTR_NON_NULL(flat->image, false);
+    if (in == NULL)
+        printf("XXX: NULL\n");
+
     // XXX: Not sure if this is correct.  Must consult with IfA.
     PS_ASSERT_PTR_NON_NULL(in->mask, false);
+
+    PS_WARN_PTR_NON_NULL(in->parent);
+    if (in->parent != NULL) {
+        PS_WARN_PTR_NON_NULL(in->parent->concepts);
+    }
     int i = 0;
     int j = 0;
@@ -51,37 +66,25 @@
     psElemType flatType;
     psElemType maskType;
-    psImage *inImage = NULL;
     psImage *inMask = NULL;
     psImage *flatImage = NULL;
 
-
-    // Check for nulls
-    if (in == NULL) {
-        return true;       // Readout might have data in it
-    } else if(flat==NULL) {
-        psError( PS_ERR_BAD_PARAMETER_NULL, true,
-                 PS_ERRORTEXT_pmFlatField_NULL_FLAT_READOUT);
-        return false;
-    }
-
-    inImage = in->image;
+    //
+    // Determine trimmed image from metadata.
+    //
+    psImage *trimmedImg = p_psDetermineTrimmedImage(in);
     flatImage = flat->image;
-    if (inImage == NULL) {
-        psError( PS_ERR_BAD_PARAMETER_NULL, true,
-                 PS_ERRORTEXT_pmFlatField_NULL_INPUT_IMAGE);
-        return false;
-    } else if(flatImage == NULL) {
-        psError( PS_ERR_BAD_PARAMETER_NULL, true,
-                 PS_ERRORTEXT_pmFlatField_NULL_FLAT_IMAGE);
-        return false;
-    }
     inMask = in->mask;
 
     // Check input image and its mask are not larger than flat image
 
-    if (inImage->numRows>flatImage->numRows || inImage->numCols>flatImage->numCols) {
+    if (trimmedImg == NULL)
+        printf("XXX: 00\n");
+    if (flatImage == NULL)
+        printf("XXX 01\n");
+
+    if (trimmedImg->numRows>flatImage->numRows || trimmedImg->numCols>flatImage->numCols) {
         psError( PS_ERR_BAD_PARAMETER_SIZE, true,
                  PS_ERRORTEXT_pmFlatField_SIZE_INPUT_IMAGE,
-                 inImage->numRows, inImage->numCols, flatImage->numRows, flatImage->numCols);
+                 trimmedImg->numRows, trimmedImg->numCols, flatImage->numRows, flatImage->numCols);
         return false;
     }
@@ -94,6 +97,6 @@
 
     // Determine total offset based on image offset with chip offset
-    totOffCol = inImage->col0 + in->col0;
-    totOffRow = inImage->row0 + in->row0;
+    totOffCol = trimmedImg->col0 + in->col0;
+    totOffRow = trimmedImg->row0 + in->row0;
 
     // Check that offsets are within image limits
@@ -103,8 +106,8 @@
                  totOffRow, totOffCol, flatImage->numRows, flatImage->numCols);
         return false;
-    } else if(totOffRow>=inImage->numRows || totOffCol>=inImage->numCols) {
+    } else if(totOffRow>=trimmedImg->numRows || totOffCol>=trimmedImg->numCols) {
         psError( PS_ERR_BAD_PARAMETER_SIZE, true,
                  PS_ERRORTEXT_pmFlatField_OFFSET_INPUT_IMAGE,
-                 totOffRow, totOffCol, inImage->numRows, inImage->numCols);
+                 totOffRow, totOffCol, trimmedImg->numRows, trimmedImg->numCols);
         return false;
     } else if(totOffRow>=inMask->numRows || totOffCol>=inMask->numCols) {
@@ -116,5 +119,5 @@
 
     // Check for incorrect types
-    inType = inImage->type.type;
+    inType = trimmedImg->type.type;
     flatType = flatImage->type.type;
     maskType = inMask->type.type;
@@ -145,6 +148,6 @@
 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++) {                                                      \
+    for(j = totOffRow; j < trimmedImg->numRows; j++) {                                                          \
+        for(i = totOffCol; i < trimmedImg->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 */           \
@@ -154,9 +157,9 @@
         }                                                                                                    \
     }                                                                                                        \
-    for(j = totOffRow; j < inImage->numRows; j++) {                                                          \
-        for(i = totOffCol; i < inImage->numCols; i++) {                                                      \
+    for(j = totOffRow; j < trimmedImg->numRows; j++) {                                                          \
+        for(i = totOffCol; i < trimmedImg->numCols; 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];                                      \
+                trimmedImg->data.TYPE[j][i] /= flatImage->data.TYPE[j][i];                                      \
             }                                                                                                \
         }                                                                                                    \
