Index: /trunk/psLib/src/imageops/psImageUnbin.c
===================================================================
--- /trunk/psLib/src/imageops/psImageUnbin.c	(revision 14924)
+++ /trunk/psLib/src/imageops/psImageUnbin.c	(revision 14925)
@@ -7,6 +7,6 @@
  *  @author Eugene Magnier, IfA
  *
- *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-03-27 02:43:22 $
+ *  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-09-20 23:55:50 $
  *
  *  Copyright 2007 Institute for Astronomy, University of Hawaii
@@ -205,4 +205,7 @@
  * N.b. This code only works for the central part of the image; the edge
  * cases should be added
+
+ * XXXX this is bilinear interpolation, but written sub-optimally
+ * XXXX this function should be taking float input coordinates!!!
  */
 double psImageUnbinPixel(const int ix, const int iy, // desired Unbinned point (parent coords)
@@ -249,2 +252,92 @@
     return Vxs + (Vxe - Vxs)*(ix - xs)/DX; // value at [iy][ix]
 }
+
+double psImageUnbinPixel_V2(const double xFine, const double yFine, // desired Unbinned point (parent coords)
+			    const psImage *in, // binned image
+			    const psImageBinning *binning)   //!< Overhang
+{
+    PS_ASSERT_IMAGE_NON_NULL(in, NAN);
+    assert (in->type.type == PS_TYPE_F32);
+
+    const float xRuff = psImageBinningGetRuffX (binning, xFine);
+    const float yRuff = psImageBinningGetRuffY (binning, yFine);
+
+    const double value = psImageInterpolatePixelBilinear (xRuff, yRuff, in);
+
+    return value;
+}
+
+// fast & simple API to interpolate to a subpixel position using bilinear interpolation
+// x,y in parent image coordinates (pixel centers at 0.5, 0.5)
+double psImageInterpolatePixelBilinear (const double xIn, const double yIn, const psImage *in) {
+
+    PS_ASSERT_PTR_NON_NULL(in, PS_ERR_BAD_PARAMETER_VALUE);
+    assert (in->type.type == PS_TYPE_F32);
+
+    const double x = xIn - in->col0;
+    const double y = yIn - in->row0;
+
+    // allow extrapolation to edge of valid pixels, but not beyond
+    if ((x < 0) || (x >= in->numCols) || (y < 0) || (y >= in->numRows)) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Point (%f,%f) lies outside binned image", x, y);
+        return NAN;
+    }
+
+    // limiting cases: Nx == 1 and/or Ny == 1
+
+    // if we have a single pixel, there is no spatial information
+    if ((in->numCols == 1) && (in->numRows == 1)) {
+	const double value = in->data.F32[0][0];
+	return value;
+    }
+
+    // handle edge cases with extrapolation
+
+    const int ix = x - 0.5; // index of reference pixel
+    const int iy = y - 0.5; // index of reference pixel
+
+    // do numCols,Rows first so we are never < 0
+    const int Xs = PS_MAX (PS_MIN (ix, in->numCols - 2), 0);
+    const int Ys = PS_MAX (PS_MIN (iy, in->numRows - 2), 0);
+
+    const int Xe = Xs + 1;
+    const int Ye = Ys + 1;
+
+    // dx,dy range from 0.0 to 1.0 for interpolated pixels, and -0.5 to 1.5 for extrapolation
+    const double dx = x - 0.5 - Xs;
+    const double dy = y - 0.5 - Ys;
+
+    const double rx = 1.0 - dx;
+    const double ry = 1.0 - dy;
+
+    // if Nx == 1, we have no x-dir spatial information
+    if (in->numCols == 1) {
+	double V0 = in->data.F32[Ys][Xs];
+	double V1 = in->data.F32[Ye][Xs];
+
+	const double value = V0*ry + V1*dy;
+	return value;
+    }	
+
+    // if Ny == 1, we have no y-dir spatial information
+    if (in->numRows == 1) {
+	double V0 = in->data.F32[Ys][Xs];
+	double V1 = in->data.F32[Ys][Xe];
+
+	const double value = V0*rx + V1*dx;
+	return value;
+    }	
+
+    // Vxy 
+    double V00 = in->data.F32[Ys][Xs];
+    double V10 = in->data.F32[Ys][Xe];
+    double V01 = in->data.F32[Ye][Xs];
+    double V11 = in->data.F32[Ye][Xe];
+
+    // bilinear interpolation
+    const double value = V00*rx*ry + V10*dx*ry + V01*rx*dy + V11*dx*dy;
+
+    return value;
+}
+
+    
Index: /trunk/psLib/src/imageops/psImageUnbin.h
===================================================================
--- /trunk/psLib/src/imageops/psImageUnbin.h	(revision 14924)
+++ /trunk/psLib/src/imageops/psImageUnbin.h	(revision 14925)
@@ -6,6 +6,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  $Revision: 1.4 $ $Name: not supported by cvs2svn $
- *  $Date: 2007-03-27 02:43:22 $
+ *  $Revision: 1.5 $ $Name: not supported by cvs2svn $
+ *  $Date: 2007-09-20 23:55:52 $
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
  */
@@ -28,4 +28,11 @@
                         );
 
+double psImageUnbinPixel_V2(const double xFine, const double yFine, // desired Unbinned point (parent coords)
+			    const psImage *in, // binned image
+			    const psImageBinning *binning   //!< Overhang
+    );
+
+double psImageInterpolatePixelBilinear (const double xIn, const double yIn, const psImage *in);
+
 /// @}
 #endif
