Index: /trunk/psModules/src/imcombine/pmReadoutCombine.c
===================================================================
--- /trunk/psModules/src/imcombine/pmReadoutCombine.c	(revision 7801)
+++ /trunk/psModules/src/imcombine/pmReadoutCombine.c	(revision 7802)
@@ -5,6 +5,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.18 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-06-29 00:30:36 $
+ *  @version $Revision: 1.19 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-07-04 00:19:11 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -122,16 +122,28 @@
     // Step through each readout in the input image list to determine how big of an output image is needed to
     // combine these input images.
-    long maxInputCols = 0;              // The largest input column value
-    long maxInputRows = 0;              // The largest input row value
-    long minInputCols = LONG_MAX;       // The smallest input column value
-    long minInputRows = LONG_MAX;       // The smallest input row value
+    int maxInputCols = 0;               // The largest input column value
+    int maxInputRows = 0;               // The largest input row value
+    int minInputCols = INT_MAX;         // The smallest input column value
+    int minInputRows = INT_MAX;         // The smallest input row value
+    int xSize = 0, ySize = 0;           // The size of the output image
 
     bool haveWeights = false;           // Do we have weight images?
     bool valid = false;                 // Do we have a single valid input?
-    for (int i = 0; i < inputs->n; i++) {
+    for (long i = 0; i < inputs->n; i++) {
         pmReadout *readout = inputs->data[i]; // Readout of interest
+        pmCell *cell = readout->parent; // The parent cell
+
         if (!readout || !readout->image) {
             psError(PS_ERR_UNEXPECTED_NULL, true, "Input readout %d is NULL or has NULL image.\n", i);
             return false;
+        }
+
+        bool mdok = true;       // Status of MD lookup
+        psRegion *trimsec = psMetadataLookupPtr(&mdok, cell->concepts, "CELL.TRIMSEC"); // Trim section
+        if (!mdok || !trimsec || psRegionIsNaN(*trimsec)) {
+            psLogMsg(__func__, PS_LOG_WARN, "CELL.TRIMSEC is not set for readout %ld --- ignored.\n", i);
+        } else {
+            xSize = PS_MAX(xSize, trimsec->x1 - trimsec->x0);
+            ySize = PS_MAX(ySize, trimsec->y1 - trimsec->y0);
         }
 
@@ -150,5 +162,5 @@
         valid = true;
 
-        // Size of output image
+        // Range of pixels on output image
         minInputRows = PS_MIN(minInputRows, readout->row0);
         maxInputRows = PS_MAX(maxInputRows, readout->row0 + readout->image->numRows);
@@ -182,42 +194,52 @@
     }
 
-    // If there's existing images, we need to extend them
-    long minOutputRows = (output->image || output->mask || output->weight) ?
-                         PS_MIN(minInputRows, output->row0) : minInputRows;  // Smallest output row value
-    long minOutputCols = (output->image || output->mask || output->weight) ?
-                         PS_MIN(minInputCols, output->col0) : minInputCols; // Smallest output column value
-    psTrace(__func__, 7, "Output minimum: %d,%d\n", minOutputCols, minOutputRows);
-
     // Update the origin
-    *(psS32 *) &(output->col0) = minOutputCols;
-    *(psS32 *) &(output->row0) = minOutputRows;
-
-    // Generate the new output image by extending the current one, or making a whole new one
-    psImage *newImage = psImageAlloc(maxInputCols - minOutputCols, maxInputRows - minOutputRows, PS_TYPE_F32);
-    psImageInit(newImage, 0.0);
     if (output->image) {
+        *(psS32 *) &(output->col0) = PS_MIN(minInputCols, output->col0);
+        *(psS32 *) &(output->row0) = PS_MIN(minInputRows, output->row0);
+    } else {
+        *(psS32 *) &(output->col0) = minInputCols;
+        *(psS32 *) &(output->row0) = minInputRows;
+    }
+    psTrace(__func__, 7, "Output minimum: %d,%d\n", output->col0, output->row0);
+
+    // Allocate the output products
+
+    if (!output->image) {
+        output->image = psImageAlloc(xSize, ySize, PS_TYPE_F32);
+    }
+    if (output->image->numCols < xSize || output->image->numRows < ySize) {
+        // Generate the new output image by extending the current one, or making a whole new one
+        psImage *newImage = psImageAlloc(xSize, ySize, PS_TYPE_F32);
+        psImageInit(newImage, 0.0);
         psImageOverlaySection(newImage, output->image, output->col0, output->row0, "=");
         psFree(output->image);
-    }
-    output->image = newImage;
-
-    psImage *newMask = psImageAlloc(maxInputCols - minOutputCols, maxInputRows - minOutputRows, PS_TYPE_U8);
-    psImageInit(newMask, 0);
-    if (output->mask) {
+        output->image = newImage;
+    }
+
+    if (!output->mask) {
+        output->mask = psImageAlloc(xSize, ySize, PS_TYPE_U8);
+    }
+    if (output->mask->numCols < xSize || output->mask->numRows < ySize) {
+        psImage *newMask = psImageAlloc(xSize, ySize, PS_TYPE_U8);
+        psImageInit(newMask, 0);
         psImageOverlaySection(newMask, output->mask, output->col0, output->row0, "=");
         psFree(output->mask);
-    }
-    output->mask = newMask;
+        output->mask = newMask;
+    }
 
     psStatsOptions combineStdev = 0; // Statistics option for weights
     if (haveWeights) {
-        psImage *newWeight = psImageAlloc(maxInputCols - minOutputCols, maxInputRows - minOutputRows,
-                                          PS_TYPE_F32);
-        psImageInit(newWeight, 0.0);
-        if (output->weight) {
+
+        if (!output->weight) {
+            output->weight = psImageAlloc(xSize, ySize, PS_TYPE_F32);
+        }
+        if (output->weight->numCols < xSize || output->weight->numRows < ySize) {
+            psImage *newWeight = psImageAlloc(xSize, ySize, PS_TYPE_F32);
+            psImageInit(newWeight, 0.0);
             psImageOverlaySection(newWeight, output->weight, output->col0, output->row0, "=");
             psFree(output->weight);
-        }
-        output->weight = newWeight;
+            output->weight = newWeight;
+        }
 
         // Get the correct statistics option for weights
