Index: /trunk/pswarp/src/pswarpDataLoad.c
===================================================================
--- /trunk/pswarp/src/pswarpDataLoad.c	(revision 10957)
+++ /trunk/pswarp/src/pswarpDataLoad.c	(revision 10958)
@@ -12,4 +12,5 @@
     pmCell *cell;
     pmReadout *readout;
+    pmFPAview *view;
 
     // select the input data sources
@@ -21,13 +22,14 @@
 
     // select the output readout
-    pmFPAview *outView = pmFPAviewAlloc (0);
-    outView->chip = 0;
-    outView->cell = 0;
-    outView->readout = 0;
-    pmReadout  *output = pmFPAfileThisReadout (config->files, outView, "PSWARP.OUTPUT");
+    view = pmFPAviewAlloc (0);
+    view->chip = 0;
+    view->cell = 0;
+    view->readout = 0;
+    pmReadout  *output = pmFPAfileThisReadout (config->files, view, "PSWARP.OUTPUT");
     if (!output) {
 	psError(PSWARP_ERR_CONFIG, true, "Can't find output data!\n");
 	return false;
     }
+    psFree (view);
 
     // de-activate PSWARP.SKYCELL and PSWARP.OUTPUT
@@ -35,5 +37,5 @@
     pmFPAfileActivate (config->files, false, "PSWARP.OUTPUT");
 
-    pmFPAview *view = pmFPAviewAlloc (0);
+    view = pmFPAviewAlloc (0);
 
     // find the FPA phu
@@ -81,5 +83,7 @@
 		if (! readout->data_exists) { continue; }
 
-		pswarpTransformReadout (output, readout, config);
+		// XXX _Opt version uses locally-linear map
+		// pswarpTransformReadout (output, readout, config);
+		pswarpTransformReadout_Opt (output, readout, config);
 
 		pmFPAfileIOChecks (config, view, PM_FPA_AFTER);
Index: /trunk/pswarp/src/pswarpMapGrid.c
===================================================================
--- /trunk/pswarp/src/pswarpMapGrid.c	(revision 10957)
+++ /trunk/pswarp/src/pswarpMapGrid.c	(revision 10958)
@@ -5,14 +5,20 @@
 pswarpMapGrid *pswarpMapGridFromImage (pmReadout *dest, pmReadout *src, int nXpix, int nYpix) {
 
+    int i, ni;
+    int j, nj;
+
     // split the difference of the remainder
     int xMin = 0.5*(src->image->numCols % nXpix);
     int yMin = 0.5*(src->image->numRows % nYpix);
 
-    pswarpMapGrid *grid = pswarpMapGridAlloc (src->image->numCols / nXpix, src->image->numRows / nYpix);
-
-    int ni = 0;
-    int nj = 0;
-    for (int i = xMin; i < src->image->numCols; i += nXpix, ni++) {
-	for (int j = yMin; j < src->image->numRows; j += nYpix, nj++) {
+    int nXpts = src->image->numCols / nXpix;
+    if (src->image->numCols % nXpix == 0) nXpts ++;
+    int nYpts = src->image->numRows / nYpix;
+    if (src->image->numRows % nYpix == 0) nYpts ++;
+
+    pswarpMapGrid *grid = pswarpMapGridAlloc (nXpts, nYpts);
+
+    for (ni = 0, i = xMin; ni < nXpts; i += nXpix, ni++) {
+	for (nj = 0, j = yMin; nj < nYpts; j += nYpix, nj++) {
 	    pswarpMapSetLocalModel (grid->maps[ni][nj], dest, src, i, j);
 	}
@@ -128,9 +134,9 @@
     map->Xx = V10->x - V00->x;
     map->Xy = V01->x - V00->x;
-    map->Xo = V00->x + map->Xx*ix + map->Xy*iy;
+    map->Xo = V00->x - map->Xx*ix - map->Xy*iy;
 
     map->Yx = V10->y - V00->y;
     map->Yy = V01->y - V00->y;
-    map->Yo = V00->y + map->Yx*ix + map->Yy*iy;
+    map->Yo = V00->y - map->Yx*ix - map->Yy*iy;
  
     map->xo = ix;
Index: /trunk/pswarp/src/pswarpMatchRange.c
===================================================================
--- /trunk/pswarp/src/pswarpMatchRange.c	(revision 10957)
+++ /trunk/pswarp/src/pswarpMatchRange.c	(revision 10958)
@@ -8,8 +8,8 @@
     psPlaneTransformApply (FP, fpaSrc->fromTPA, TP); \
     psPlaneTransformApply (srcPix, chipSrc->fromFPA, FP); \
-    *minX = PS_MAX (*minX, srcPix->x); \
-    *minY = PS_MAX (*minY, srcPix->y); \
-    *maxX = PS_MIN (*maxX, srcPix->x); \
-    *maxY = PS_MIN (*maxY, srcPix->y);
+    *minX = PS_MIN (*minX, srcPix->x); \
+    *minY = PS_MIN (*minY, srcPix->y); \
+    *maxX = PS_MAX (*maxX, srcPix->x); \
+    *maxY = PS_MAX (*maxY, srcPix->y);
 
 // we are warping from src to dest.  find the max overlapping pixels in the INPUT (src)
@@ -30,8 +30,8 @@
     pmFPA *fpaDest = chipDest->parent;
 
-    *minX = 0;
-    *minY = 0;
-    *maxX = src->image->numCols;
-    *maxY = src->image->numRows;
+    *minX = src->image->numCols;
+    *minY = src->image->numRows;
+    *maxX = 0;
+    *maxY = 0;
 
     // XXX save these as static for speed?
@@ -75,4 +75,15 @@
     TEST_MINMAX;
 
+    *minX = PS_MAX (*minX, 0);
+    *minY = PS_MAX (*minY, 0);
+    *maxX = PS_MIN (*maxX, src->image->numCols);
+    *maxY = PS_MIN (*maxY, src->image->numRows);
+
+    psFree (srcPix);
+    psFree (destPix);
+    psFree (FP);
+    psFree (TP);
+    psFree (sky);
+
     return true;
 }
Index: /trunk/pswarp/src/pswarpTransformReadout.c
===================================================================
--- /trunk/pswarp/src/pswarpTransformReadout.c	(revision 10957)
+++ /trunk/pswarp/src/pswarpTransformReadout.c	(revision 10958)
@@ -55,4 +55,11 @@
 	}
     }
+
+    psFree (inPix);
+    psFree (outPix);
+    psFree (FP);
+    psFree (TP);
+    psFree (sky);
+
     return true;
 }
Index: /trunk/pswarp/src/pswarpTransformReadout_Opt.c
===================================================================
--- /trunk/pswarp/src/pswarpTransformReadout_Opt.c	(revision 10957)
+++ /trunk/pswarp/src/pswarpTransformReadout_Opt.c	(revision 10958)
@@ -61,4 +61,7 @@
 	}
     }
+
+    psFree (inPix);
+    psFree (grid);
     return true;
 }
Index: /trunk/pswarp/test/simple.image.op
===================================================================
--- /trunk/pswarp/test/simple.image.op	(revision 10957)
+++ /trunk/pswarp/test/simple.image.op	(revision 10958)
@@ -1,17 +1,20 @@
+
+$NX = 2048
+$NY = 2048
 
 macro go
   mkref
   mksrc
-#  exec pswarp -file src.fits out.fits ref.fits
+  exec pswarp -file src.fits out ref.fits
 end
 
 macro mkref
-  mcreate ref 512 512
+  mcreate ref $NX $NY
   keyword ref CTYPE1 -w "RA---TAN"
   keyword ref CTYPE2 -w "DEC--TAN"
   keyword ref CRVAL1 -wf 0.0
   keyword ref CRVAL2 -wf 0.0
-  keyword ref CRPIX1 -wf 256
-  keyword ref CRPIX2 -wf 256
+  keyword ref CRPIX1 -wf {$NX/2}
+  keyword ref CRPIX2 -wf {$NY/2}
 
   keyword ref CDELT1 -wf {1/3600}
@@ -27,7 +30,7 @@
 
 macro mksrc
-  mcreate src 512 512
-  for i 0 512 64
-    for j 0 512 64
+  mcreate src $NX $NY
+  for i 0 $NX 64
+    for j 0 $NY 64
       zap src $i $j 1 1 -v 1
     end
@@ -38,6 +41,6 @@
   keyword src CRVAL1 -wf 0.0
   keyword src CRVAL2 -wf 0.0
-  keyword src CRPIX1 -wf 256
-  keyword src CRPIX2 -wf 256
+  keyword src CRPIX1 -wf {$NX/2}
+  keyword src CRPIX2 -wf {$NY/2}
 
   keyword src CDELT1 -wf {1/3600}
