Index: /trunk/psModules/src/pmSubtractBias.c
===================================================================
--- /trunk/psModules/src/pmSubtractBias.c	(revision 1870)
+++ /trunk/psModules/src/pmSubtractBias.c	(revision 1871)
@@ -1,5 +1,5 @@
 /** @file  psMinimize.c
  *  \brief basic minimization functions
- *  @ingroup Math
+ *  @ingroup Modules
  *
  *  This file will contain a module which will subtract the detector bias
@@ -8,6 +8,6 @@
  *  @author George Gusciora, MHPCC
  *
- *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-09-23 06:48:30 $
+ *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-09-24 00:23:48 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -36,16 +36,52 @@
 
 
-psReadout *psSubtractFrame(in, bias)
+psReadout *psSubtractFrame(psReadout *in,
+                           const psReadout *bias)
 {
+    int i;
+    int j;
+
     if (bias == NULL) {
         return(in);
     }
-    // XXX: Check sizes
-    // XXX: use offsets
-
-    for (i=0;i<rows;i++)
-        for (j=0;j<cols;j++)
-            in[i][j]-=bias[i][j];
+
+    if (((in->image)->numRows + in->row0 - bias->row0) > (bias->image)->numRows) {
+        // XXX: psErrorMsg() bias image does not have enough rows.
+    }
+    if (((in->image)->numCols + in->row0 - bias->row0) > (bias->image)->numCols) {
+        // XXX: psErrorMsg() bias image does not have enough columns.
+    }
+
+    // XXX: Verify that this is the correct way to use offsets.
+    // XXX: Instead of performing an if-statement to determine if the mask
+    //      is non-zero, we instead always or the bias mask with the image
+    //      mask.  Might want to verify this is faster.
+    for (i=0;i<(in->image)->numRows;i++) {
+        for (j=0;j<(in->image)->numCols;j++) {
+            (in->image)->data.F32[i][j]-=
+                (bias->image)->data.F32[i+in->row0-bias->row0][j+in->col0-bias->col0];
+            ((in->mask)->data.U8[i][j])|=
+                (bias->mask)->data.U8[i+in->row0-bias->row0][j+in->col0-bias->col0];
+        }
+    }
+
+    return(in);
 }
+
+// XXX: Is there already a function which does this?
+psImage *p_psImageSubtractScalar(psImage *image,
+                                 float *scalar)
+{
+    int i;
+    int j;
+
+    for (i=0;i<image->numRows;i++) {
+        for (j=0;j<image->numCols;j++) {
+            image->data.F32[i][j]-= scalar;
+        }
+    }
+    return(image);
+}
+
 
 psReadout *pmSubtractBias(psReadout *in,
@@ -58,18 +94,31 @@
                           const psReadout *bias)
 {
-    if ((overscans == NULL) &&
-            (overScanAxis != PM_OVERSCAN_NONE)) {
+    int i;
+    int j;
+    int nBin;
+    int numBins;
+    static psVector *tmp = NULL;
+    static psVector *tmpRow = NULL;
+    static psVector *tmpCol = NULL;
+    static psVector *tmpMask = NULL;
+
+    if (in == NULL) {
         //XXX: psErrorMsg()
     }
-    if ((overscans != NULL) &&
-            (overScanAxis == PM_OVERSCAN_NONE)) {
+
+    if ((overscans == NULL) && (overScanAxis != PM_OVERSCAN_NONE)) {
+        //XXX: psErrorMsg()
+    }
+    if ((overscans != NULL) && (overScanAxis == PM_OVERSCAN_NONE)) {
         //XXX: psWarningMsg()
-        psSubtractFrame(in, bias);
-    }
+        return(psSubtractFrame(in, bias));
+    }
+
     if (overScanAxis == PM_OVERSCAN_ALL) {
-        psImageStats(in->image..., stat);
-        psSubtractScalarFromImage(...);
-        return()?
+        psImageStats(stat, in->image, in->mask, 0xffffffff);
+        p_psImageSubtractScalar(in->image, stat->value);
+        return(in)
           }
+
           if (((overScanAxis == PM_OVERSCAN_NONE) ||
                   (overScanAxis == PM_OVERSCAN_ALL)) &&
@@ -77,29 +126,80 @@
               //XXX: psWarningMsg()
               //XXX: Then what?
-          }
-          if (overScanAxis == PM_OVERSCAN_ROW) {
-              myVec = reduce the overscan area to a vector
-                  }
-                  if (overScanAxis == PM_OVERSCAN_COL) {
-                      myVec = reduce the overscan area to a vector
-                          }
-                          if (nBin > 0) {
-                              psVector *myBin = psVectorAlloc(nBin, PS_TYPE_F32);
-                          }
-                          if (!((fitSpec == NULL) || (fit ==PM_FIT_NONE))) {
-                              fit the vector to a linear or cubic spline.
-                          }
-
-                          if (overScanAxis == PM_OVERSCAN_ROW) {
-                              for (i=0;i<rows;i++)
-                                  for (j=0;j<cols;j++)
-                                      image[i][j]-= myBin[i];
-                          }
-                          if (overScanAxis == PM_OVERSCAN_COLUMN) {
-                              for (i=0;i<rows;i++)
-                                  for (j=0;j<cols;j++)
-                                      image[i][j]-= myBin[j];
-                          }
-                          return(in);
-}
-
+              return(in)
+                }
+
+                // XXX: Is there a better way to extract a psVector from a psImage without
+                // having to copy every element in that vector?
+
+                // XXX: If we get here, do we know that overScanAxis == PM_OVERSCAN_ROW
+                // or PM_OVERSCAN_COL?
+
+                if ((overScanAxis == PM_OVERSCAN_ROW) || (overScanAxis == PM_OVERSCAN_COL)) {
+                    if (overScanAxis == PM_OVERSCAN_ROW) {
+                        tmp = psVectorAlloc((in->image)->numCols, PS_TYPE_F32);
+                        tmpRow = psVectorAlloc((in->image)->numCols, PS_TYPE_F32);
+                        tmpMask = psVectorAlloc((in->image)->numCols, PS_TYPE_U8);
+                        for (i=0;i<(in->image)->numCols;i++) {
+                            for (j=0;j<(in->image)->numRows;j++) {
+                                tmpRow->data.F32[j] = (in->image)->data.F32[i][j];
+                                tmpMask->data.U8[j] = (in->mask)->data.U8[i][j];
+                            }
+                            stat = psVectorStats(stat, tmpRow, tmpMask, 0xffffffff);
+                            tmp->data.F32[i] = stat->value;
+                        }
+                    }
+                    if (overScanAxis == PM_OVERSCAN_COL) {
+                        tmp = psVectorAlloc((in->image)->numRows, PS_TYPE_F32);
+                        tmpcol = psVectorAlloc((in->image)->numRows, PS_TYPE_F32);
+                        tmpMask = psVectorAlloc((in->image)->nuRows, PS_TYPE_U8);
+                        for (i=0;i<(in->image)->numRows;i++) {
+                            for (j=0;j<(in->image)->numCols;j++) {
+                                tmpRow->data.F32[j] = (in->image)->data.F32[i][j];
+                                tmpMask->data.U8[j] = (in->mask)->data.U8[i][j];
+                            }
+                            stat = psVectorStats(stat, tmpRow, tmpMask, 0xffffffff);
+                            tmp->data.F32[i] = stat->value;
+                        }
+                    }
+
+                    if (nBin > 0) {
+                        numBins = 1+((tmp->n)/nBin);
+                        psVector *myBin = psVectorAlloc(numBins, PS_TYPE_F32);
+
+                        for (i=0;i<numBins;i++) {
+                            // XXX: do this
+                        }
+                        // XXX: resize tmp to myBin, and store myBin elements there.
+                    } else {
+                        nBin = 1;
+                    }
+
+                    if (!((fitSpec == NULL) || (fit ==PM_FIT_NONE))) {
+                        fit the vector to a linear or cubic spline.
+                    }
+
+                    if (overScanAxis == PM_OVERSCAN_ROW) {
+                        for (i=0;i<(in->image)->numCols;i++) {
+                            for (j=0;j<(in->image)->numRows;j++) {
+                                (in->image)->data.F32[i][j]-= tmp->data.F32[j/nBin];
+                            }
+                        }
+                    }
+
+                    if (overScanAxis == PM_OVERSCAN_COLUMN) {
+                        for (i=0;i<(in->image)->numRows;i++) {
+                            for (j=0;j<(in->image)->numCols;j++) {
+                                (in->image)->data.F32[i][j]-= tmp->data.F32[j/nBin];
+                            }
+                        }
+                    }
+                    return(in);
+                }
+
+                if (bias != NULL) {
+                    return(psSubtractFrame(in, bias));
+                }
+
+                // XXX: psErrorMsg(): Shouldn't get here?
+            }
+
