Index: trunk/pswarp/src/pswarpTransformTile.c
===================================================================
--- trunk/pswarp/src/pswarpTransformTile.c	(revision 19149)
+++ trunk/pswarp/src/pswarpTransformTile.c	(revision 19395)
@@ -1,13 +1,18 @@
-# include "pswarp.h"
+#include "pswarp.h"
 
-void pswarpTransformTileArgsFree (pswarpTransformTileArgs *args) {
-    if (!args) return;
+static void transformTileArgsFree(pswarpTransformTileArgs *args)
+{
+    psFree(args->input);
+    psFree(args->output);
+    psFree(args->grid);
+    psFree(args->interp);
+    psFree(args->region);
     return;
 }
 
-pswarpTransformTileArgs *pswarpTransformTileArgsAlloc() {
-
-    pswarpTransformTileArgs *args = (pswarpTransformTileArgs *)psAlloc(sizeof(pswarpTransformTileArgs));
-    psMemSetDeallocator(args, (psFreeFunc)pswarpTransformTileArgsFree);
+pswarpTransformTileArgs *pswarpTransformTileArgsAlloc()
+{
+    pswarpTransformTileArgs *args = psAlloc(sizeof(pswarpTransformTileArgs));
+    psMemSetDeallocator(args, (psFreeFunc)transformTileArgsFree);
 
     args->input = NULL;
@@ -21,46 +26,38 @@
 
     args->goodPixels = 0;
+    args->xMin = PS_MAX_S32;
+    args->xMax = PS_MIN_S32;
+    args->yMin = PS_MAX_S32;
+    args->yMax = PS_MIN_S32;
 
     return args;
 }
 
-bool pswarpTransformTile (pswarpTransformTileArgs *args) {
+bool pswarpTransformTile(pswarpTransformTileArgs *args)
+{
+    psImage *inImage = args->input->image; // Input image
+    psImage *outImage = args->output->image; // Output image
 
-    // int inCol0 = args->input->image->col0;
-    // int inRow0 = args->input->image->row0;
-    int inNcol = args->input->image->numCols;
-    int inNrow = args->input->image->numRows;
+    int inNumCols = inImage->numCols, inNumRows = inImage->numRows; // Size of input image
+    int outNumCols = outImage->numCols, outNumRows = outImage->numRows; // Size of output image
+    int outCol0 = outImage->col0, outRow0 = outImage->row0; // Offset of output image
 
-    int outCol0 = args->output->image->col0;
-    int outRow0 = args->output->image->row0;
-    int outNcol = args->output->image->numCols;
-    int outNrow = args->output->image->numRows;
+    psPlane minPt, maxPt;               // Minimum and maximum points for this tile
+    pswarpMapGridCoordRange(args->grid, args->gridX, args->gridY, &minPt, &maxPt);
 
-    // get the coordinate range for this grid tile
-    psPlane minPt, maxPt;
-    pswarpMapGridCoordRange (args->grid, args->gridX, args->gridY, &minPt, &maxPt);
-
-    psF32 **outImageData     = (args->output->image)  ? args->output->image->data.F32 : NULL;
+    // Dereference images for convenience
+    psF32 **outImageData     = args->output->image->data.F32;
     psF32 **outVarData       = (args->output->weight) ? args->output->weight->data.F32 : NULL;
     psMaskType **outMaskData = (args->output->mask)   ? args->output->mask->data.PS_TYPE_MASK_DATA : NULL;
     psMaskType **inMaskData  = (args->input->mask)    ? args->input->mask->data.PS_TYPE_MASK_DATA : NULL;
 
-    pswarpMap *map = args->grid->maps[args->gridX][args->gridY];
-
-    psImage *region = args->region;
-
-    double xInRaw, yInRaw;
-
-    // output values for this pixel
-    double imageValue;
-    double varValue;
-    psMaskType maskValue;
+    pswarpMap *map = args->grid->maps[args->gridX][args->gridY]; // Map for this tile
+    psImage *region = args->region;     // Region to transform
 
     // Bounds for iteration
     int xMin = PS_MAX(minPt.x, 0);
-    int xMax = PS_MIN(maxPt.x, outNcol);
+    int xMax = PS_MIN(maxPt.x, outNumCols);
     int yMin = PS_MAX(minPt.y, 0);
-    int yMax = PS_MIN(maxPt.y, outNrow);
-
+    int yMax = PS_MIN(maxPt.y, outNumRows);
 
     // Iterate over the output image pixels (parent frame)
@@ -70,22 +67,20 @@
 
             // Only transform those pixels requested
-            if (region && region->data.U8[y][x]) continue;
+            if (region && region->data.U8[y][x]) {
+                continue;
+            }
 
             // pswarpMapApply converts the output coordinate (x,y) to the input coordinate.
             // both are in the parent frames of the input and output images.
-            pswarpMapApply (&xInRaw, &yInRaw, map, x + 0.5, y + 0.5);
-
-            double xIn = xInRaw - outCol0;      // Position on input image
-            double yIn = yInRaw - outRow0;      // Position on input image
-
-            if (xIn < 0) continue;
-            if (yIn >= inNcol) continue;
-            if (yIn < 0) continue;
-            if (yIn >= inNrow) continue;
-
-            goodPixels++;
+            double xInRaw, yInRaw;      // Input raw pixel coordinates
+            pswarpMapApply(&xInRaw, &yInRaw, map, x + 0.5, y + 0.5);
+            double xIn = xInRaw - outCol0, yIn = yInRaw - outRow0; // Position on input image
+            if (xIn < 0 || xIn >= inNumCols || yIn < 0 || yIn >= inNumRows) {
+                continue;
+            }
 
             // psImagePixelInterpolate determines the value at pixel coordinate (x,y) in child coordinates
-            maskValue = inMaskData ? inMaskData[(int)yIn][(int)xIn] : 0;
+            double imageValue, varValue; // Value of image and variance map
+            psMaskType maskValue = inMaskData ? inMaskData[(int)yIn][(int)xIn] : 0; // Value of mask
             if (!psImageInterpolate(&imageValue, &varValue, &maskValue, xIn, yIn, args->interp)) {
                 psError(PS_ERR_UNKNOWN, false, "Unable to interpolate image.");
@@ -93,8 +88,6 @@
             }
 
-            int xOut = x - outCol0;     // Position on output image
-            int yOut = y - outRow0;     // Position on output image
+            int xOut = x - outCol0, yOut = y - outRow0; // Position on output image
 
-            // not all images need be transformed
             if (outImageData) {
                 outImageData[yOut][xOut] = imageValue;
@@ -106,7 +99,15 @@
                 outMaskData[yOut][xOut] = maskValue;
             }
+
+            goodPixels++;
         }
     }
+
     args->goodPixels = goodPixels;
+    args->xMin = xMin;
+    args->xMax = xMax;
+    args->yMin = yMin;
+    args->yMax = yMax;
+
     return true;
 }
