Index: trunk/psLib/src/imageops/psImageUnbin.c
===================================================================
--- trunk/psLib/src/imageops/psImageUnbin.c	(revision 11795)
+++ trunk/psLib/src/imageops/psImageUnbin.c	(revision 12588)
@@ -1,2 +1,16 @@
+/** @file  psImageUnbin.c
+ *
+ *  @brief Functions to perform unbinning (resampling) of images.
+ *
+ *  @ingroup Image
+ *
+ *  @author Eugene Magnier, IfA
+ *
+ *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-03-27 02:43:22 $
+ *
+ *  Copyright 2007 Institute for Astronomy, University of Hawaii
+ */
+
 #ifdef HAVE_CONFIG_H
 # include "config.h"
@@ -4,8 +18,9 @@
 
 #include <stdio.h>
-
 #include "psError.h"
+#include "psAssert.h"
+#include "psRegion.h"
 #include "psImage.h"
-#include "psAssert.h"
+#include "psImageBinning.h"
 #include "psImageUnbin.h"
 
@@ -15,8 +30,14 @@
 // binned pixel. 
 // XXX check that this is still consistent with psphotImageMedian...
-psImage *psImageUnbin(psImage *out, const psImage *in, int DX, int DY, int dx, int dy)
+psImage *psImageUnbin(psImage *out, const psImage *in, const psImageBinning *binning)
 {
     PS_ASSERT_IMAGE_NON_NULL(in, NULL);
     PS_ASSERT_IMAGE_NON_NULL(out, NULL);
+
+    int DX = binning->nXbin;
+    int DY = binning->nYbin;
+    int dx = binning->nXskip;
+    int dy = binning->nYskip;
+
     PS_ASSERT_INT_POSITIVE(DX, NULL);
     PS_ASSERT_INT_POSITIVE(DY, NULL);
@@ -28,4 +49,6 @@
     long Nx = out->numCols;
     long Ny = out->numRows;
+
+    // XXX validate the binning structure vs the input and output images?
 
     psF32 **vIn = in->data.F32;
@@ -183,11 +206,17 @@
  * cases should be added
  */
-double psImageUnbinPixel(const int ix, const int iy, // desired Unbinned point
+double psImageUnbinPixel(const int ix, const int iy, // desired Unbinned point (parent coords)
                          const psImage *in, // binned image
-                         const int DX, const int DY,  //!< Scaling factors in x and y
-                         const int dx, const int dy)   //!< Overhang
+                         const psImageBinning *binning)   //!< Overhang
 {
+
+    int DX = binning->nXbin;
+    int DY = binning->nYbin;
+    int dx = binning->nXskip;
+    int dy = binning->nYskip;
+
     PS_ASSERT_IMAGE_NON_NULL(in, NAN);
     assert (in->type.type == PS_TYPE_F32);
+
     PS_ASSERT_INT_POSITIVE(DX, NAN);
     PS_ASSERT_INT_POSITIVE(DY, NAN);
@@ -197,6 +226,7 @@
      * Find which binned pixel we're in
      */
-    const int Xs = (ix + dx)/DX; // index of binned pixel
-    const int Ys = (iy + dy)/DY;
+
+    const int Xs = (ix - dx)/DX; // index of binned pixel
+    const int Ys = (iy - dy)/DY;
     if (Xs < 0 || Xs >= in->numCols || Ys < 0 || Ys >= in->numRows) {
         psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Point (%d,%d) lies outside binned image", ix, iy);
