Index: trunk/dvoTools/src/dvoMakeCorrUnbin.c
===================================================================
--- trunk/dvoTools/src/dvoMakeCorrUnbin.c	(revision 11877)
+++ trunk/dvoTools/src/dvoMakeCorrUnbin.c	(revision 12592)
@@ -26,21 +26,14 @@
     assert (trimsec);
 
-    // dimensions of input image:
-    int nx = inData->image->numCols;
-    int ny = inData->image->numRows;
+    // I have the fine and ruff image sizes, determine the binning factor
+    psImageBinning *binning = psImageBinningAlloc();
+    binning->nXruff = inData->numCols;
+    binning->nYruff = inData->numRows;
+    binning->nXfine = trimsec->x1 - trimsec->x0;
+    binning->nYfine = trimsec->y1 - trimsec->y0;
+    psImageBinningSetBinning (binning, PS_IMAGE_BINNING_CENTER);
+    psImageBinningSetSkip(binning, inData);
 
-    // dimensions of output image:
-    int Nx = trimsec->x1 - trimsec->x0;
-    int Ny = trimsec->y1 - trimsec->y0;
-
-    // choose the binning factor which would yield nx,ny pixels from Nx,Ny
-    int DX = Nx / nx;			// 36 / 3 = 12, 35 / 3 = 11, 34 / 3 = 11
-    if (Nx % nx) DX ++;			// 36, 35, 34 -> DX = 12
-    int xOffset = (Nx % nx) / 2;	// for nx = 3, xOffset = 0 or 1
-    
-    int DY = Ny / ny;
-    if (Ny % ny) DY ++;
-    int yOffset = (Ny % ny) / 2;
-
+    // construct the supporing pmFPA/pmChip/pmCell structures
     pmReadout *outData = pmFPAviewThisReadout (view, outFile->fpa);
     if (outData == NULL) {
@@ -48,14 +41,16 @@
 	pmChip *outChip = pmFPAviewThisChip (view, outFile->fpa);
 
-	pmChipCopyStructure (outChip, inChip, DX, DY);
+	pmChipCopyStructure (outChip, inChip, binning->nXbin, binning->nYbin);
 	outData = pmFPAviewThisReadout (view, outFile->fpa);
 	assert (outData != NULL);
     }
 
-    psImageRecycle (outData->image, Nx, Ny, PS_TYPE_F32);
+    // generate the output (fine-scale) image array
+    psImageRecycle (outData->image, binning->nXfine, binning->nYfine, PS_TYPE_F32);
 
-    // linear interpolation to full-scale
-    if (!psImageUnbin (outData->image, inData->image, DX, DY, xOffset, yOffset)) {
+    // linear interpolation to full fine scale
+    if (!psImageUnbin (outData->image, inData->image, binning)) {
 	psError (PS_ERR_UNKNOWN, true, "failed to unbin image");
+	psFree (binning);
 	return false;
     }
@@ -74,4 +69,5 @@
     // exit (0);
 
+    psFree (binning);
     return true;
 }
