Index: trunk/ppImage/src/ppImageRebinReadout.c
===================================================================
--- trunk/ppImage/src/ppImageRebinReadout.c	(revision 6849)
+++ trunk/ppImage/src/ppImageRebinReadout.c	(revision 7621)
@@ -1,19 +1,42 @@
 #include "ppImage.h"
 
-bool ppImageRebinReadout (pmConfig *config, pmFPAview *view, char *filename)
+bool ppImageRebinChip (pmConfig *config, pmFPAview *view, char *outName, char *inName) {
+
+    pmCell *cell;
+    pmReadout *inReadout, *outReadout;
+
+    pmFPAfile *inFile = psMetadataLookupPtr (NULL, config->files, inName);
+    if (inFile == NULL) return NULL;
+
+    pmFPAfile *outFile = psMetadataLookupPtr (NULL, config->files, outName);
+    if (outFile == NULL) return NULL;
+
+    // XXX double check that chip != -1?
+
+    pmChip *inChip = pmFPAviewThisChip (view, inFile->fpa);
+    pmChip *outChip = pmFPAviewThisChip (view, outFile->fpa);
+    pmChipCopyStructure (outChip, inChip, outFile->xBin, outFile->yBin);
+
+    while ((cell = pmFPAviewNextCell (view, inFile->fpa, 1)) != NULL) {
+	psLogMsg ("ppImageRebinChip", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
+	if (! cell->process || ! cell->file_exists) { continue; }
+
+	// process each of the readouts
+	while ((inReadout = pmFPAviewNextReadout (view, inFile->fpa, 1)) != NULL) {
+	    if (! inReadout->data_exists) { continue; }
+
+	    outReadout = pmFPAviewThisReadout (view, outFile->fpa);
+
+	    // run the rebin code
+	    ppImageRebinReadout (outReadout, inReadout, outFile);
+	}
+    }
+    return true;
+}
+
+bool ppImageRebinReadout (pmReadout *output, pmReadout *input, pmFPAfile *outFile)
 {
-    bool status;
     psF32 value;
     psS32 nPix;
-
-    // find the currently selected readout
-    pmReadout *input = pmFPAfileThisReadout (config->files, view, "PPIMAGE.INPUT");
-
-    // set up the readouts for dark and bias
-    pmReadout  *output = pmFPAfileThisReadout (config->files, view, filename);
-
-    // we need the file for the binning factors as well
-    pmFPAfile *file = psMetadataLookupPtr (&status, config->files, filename);
-    if (file == NULL) return NULL;
 
     // the binning process must not change the size of the output image...
@@ -22,6 +45,6 @@
     // psFree (stats);
 
-    int dX = file->xBin;
-    int dY = file->yBin;
+    int dX = outFile->xBin;
+    int dY = outFile->yBin;
 
     int nX = input->image->numCols;
