Index: trunk/psLib/src/imageops/psImageUnbin.c
===================================================================
--- trunk/psLib/src/imageops/psImageUnbin.c	(revision 24497)
+++ trunk/psLib/src/imageops/psImageUnbin.c	(revision 36631)
@@ -39,11 +39,11 @@
     int DX = binning->nXbin;
     int DY = binning->nYbin;
-    int dx = binning->nXskip;
-    int dy = binning->nYskip;
+    // int dx = binning->nXskip;
+    // int dy = binning->nYskip;
 
     PS_ASSERT_INT_POSITIVE(DX, NULL);
     PS_ASSERT_INT_POSITIVE(DY, NULL);
-    PS_ASSERT_INT_LESS_THAN_OR_EQUAL(dx, DX, NULL);
-    PS_ASSERT_INT_LESS_THAN_OR_EQUAL(dy, DY, NULL);
+    // PS_ASSERT_INT_LESS_THAN_OR_EQUAL(dx, DX, NULL);
+    // PS_ASSERT_INT_LESS_THAN_OR_EQUAL(dy, DY, NULL);
 
     long nx = in->numCols;
@@ -57,4 +57,7 @@
     psF32 **vOut = out->data.F32;
 
+    int col0 = out->col0;
+    int row0 = out->row0;
+
     // loop over all input pixels excluding the last
     for (int Iy = 0; Iy < ny-1; Iy ++) {
@@ -70,8 +73,11 @@
             // corresponding to (Ix,Iy), (Ix+1,Iy+1)
             // XXX should this be "+ dx" and + dy?
-            int Xs = PS_MAX (0, PS_MIN (Nx, psImageBinningGetFineX(binning, Ix + 0.5)));
-            int Ys = PS_MAX (0, PS_MIN (Ny, psImageBinningGetFineY(binning, Iy + 0.5)));
+	    // Ix,Iy are in input pixels coords; result of GetFine is in the 
+	    // data space of the output, so we need to subtract col0,row0 to get raw pixels
+            int Xs = PS_MAX (0, PS_MIN (Nx, psImageBinningGetFineX(binning, Ix + 0.5) - col0));
+            int Ys = PS_MAX (0, PS_MIN (Ny, psImageBinningGetFineY(binning, Iy + 0.5) - row0));
             int Xe = PS_MAX (0, PS_MIN (Nx, Xs + DX));
             int Ye = PS_MAX (0, PS_MIN (Ny, Ys + DY));
+	    // fprintf (stderr, "%f, %f, %f, %f : %d, %d : %d - %d, %d - %d\n", V00, V01, V10, V11, Ix, Iy, Xs, Xe, Ys, Ye);
 
             for (int iy = Ys; (iy < Ye) && (iy < Ny); iy++) {
@@ -113,9 +119,9 @@
 
     // side pixels
-    int Xs = PS_MAX (0, PS_MIN (Nx, psImageBinningGetFineX(binning, 0  + 0.5)));
-    int Xe = PS_MAX (0, PS_MIN (Nx, psImageBinningGetFineX(binning, nx - 0.5)));
+    int Xs = PS_MAX (0, PS_MIN (Nx, psImageBinningGetFineX(binning, 0  + 0.5) - col0));
+    int Xe = PS_MAX (0, PS_MIN (Nx, psImageBinningGetFineX(binning, nx - 0.5) - col0));
     for (int Iy = 0; Iy < ny - 1; Iy++) {
 
-        int Ys = PS_MAX (0, PS_MIN (Ny, psImageBinningGetFineY(binning, Iy + 0.5)));
+        int Ys = PS_MAX (0, PS_MIN (Ny, psImageBinningGetFineY(binning, Iy + 0.5) - row0));
         int Ye = PS_MAX (0, PS_MIN (Ny, Ys + DY));
 
@@ -148,9 +154,9 @@
 
     // top and bottom pixels
-    int Ys = PS_MAX (0, PS_MIN (Ny, psImageBinningGetFineY(binning, 0  + 0.5)));
-    int Ye = PS_MAX (0, PS_MIN (Ny, psImageBinningGetFineY(binning, ny - 0.5)));
+    int Ys = PS_MAX (0, PS_MIN (Ny, psImageBinningGetFineY(binning, 0  + 0.5) - row0));
+    int Ye = PS_MAX (0, PS_MIN (Ny, psImageBinningGetFineY(binning, ny - 0.5) - row0));
     for (int Ix = 0; Ix < nx - 1; Ix++) {
 
-        int Xs = PS_MAX (0, PS_MIN (Nx, psImageBinningGetFineX(binning, Ix + 0.5)));
+        int Xs = PS_MAX (0, PS_MIN (Nx, psImageBinningGetFineX(binning, Ix + 0.5) - col0));
         int Xe = PS_MAX (0, PS_MIN (Nx, Xs + DX));
 
@@ -187,8 +193,8 @@
         float V;
         // center of last pixel
-        int Xs = PS_MAX (0, PS_MIN (Nx, psImageBinningGetFineX(binning, 0  + 0.5)));
-        int Xe = PS_MAX (0, PS_MIN (Nx, psImageBinningGetFineX(binning, nx - 0.5)));
-        int Ys = PS_MAX (0, PS_MIN (Ny, psImageBinningGetFineY(binning, 0  + 0.5)));
-        int Ye = PS_MAX (0, PS_MIN (Ny, psImageBinningGetFineY(binning, ny - 0.5)));
+        int Xs = PS_MAX (0, PS_MIN (Nx, psImageBinningGetFineX(binning, 0  + 0.5) - col0));
+        int Xe = PS_MAX (0, PS_MIN (Nx, psImageBinningGetFineX(binning, nx - 0.5) - col0));
+        int Ys = PS_MAX (0, PS_MIN (Ny, psImageBinningGetFineY(binning, 0  + 0.5) - row0));
+        int Ye = PS_MAX (0, PS_MIN (Ny, psImageBinningGetFineY(binning, ny - 0.5) - row0));
 
         // 0,0
