Index: trunk/psModules/src/imcombine/pmStackReject.c
===================================================================
--- trunk/psModules/src/imcombine/pmStackReject.c	(revision 16423)
+++ trunk/psModules/src/imcombine/pmStackReject.c	(revision 16476)
@@ -59,5 +59,6 @@
     }
 
-    psImage *mask = psPixelsToMask(NULL, in, psRegionSet(minCols, numCols, minRows, numRows), 0x01); // Mask
+    psImage *mask = psPixelsToMask(NULL, in, psRegionSet(minCols, numCols - 1, minRows, numRows - 1),
+                                   0x01); // Mask
     psImage *image = psImageCopy(NULL, mask, PS_TYPE_F32); // Floating-point version, so we can convolve
     psFree(mask);
@@ -70,12 +71,12 @@
     inRO->row0 = minRows;
     for (int i = 0; i < numRegions; i++) {
-        psRegion *subRegion = subRegions->data[i]; // Region of interest
-        if (valid && (subRegion->x0 > valid->x1 || subRegion->x1 < valid->x0 ||
-                      subRegion->y0 > valid->y1 || subRegion->y1 < valid->y0)) {
+        psRegion *region = subRegions->data[i]; // Region of interest
+        if (valid && (region->x0 > valid->x1 || region->x1 < valid->x0 ||
+                      region->y0 > valid->y1 || region->y1 < valid->y0)) {
             // Region is outside of our sub-image
             continue;
         }
         pmSubtractionKernels *kernel = kernels->data[i]; // Kernel of interest
-        if (!pmSubtractionConvolve(convRO, NULL, inRO, NULL, NULL, 0, subRegion, kernel, true)) {
+        if (!pmSubtractionConvolve(convRO, NULL, inRO, NULL, NULL, 0, region, kernel, true)) {
             psError(PS_ERR_UNKNOWN, false, "Unable to convolve mask image in region %d.", i);
             psFree(convRO);
@@ -88,7 +89,7 @@
 
         // Image of the kernel at the centre of the region
-        float xNorm = (subRegion->x0 + 0.5 * (subRegion->x1 - subRegion->x0) - kernel->numCols/2.0) /
+        float xNorm = (region->x0 + 0.5 * (region->x1 - region->x0) - kernel->numCols/2.0) /
             (float)kernel->numCols;
-        float yNorm = (subRegion->y0 + 0.5 * (subRegion->y1 - subRegion->y0) - kernel->numRows/2.0) /
+        float yNorm = (region->y0 + 0.5 * (region->y1 - region->y0) - kernel->numRows/2.0) /
             (float)kernel->numRows;
         psImage *image = pmSubtractionKernelImage(kernel, xNorm, yNorm, false);
@@ -107,6 +108,16 @@
         psFree(image);
 
-        psImage *subConv = psImageSubset(convRO->image, *subRegion); // Sub-image of convolved image
-        psBinaryOp(subConv, subConv, "*", psScalarAlloc(1.0 / sum, PS_TYPE_F32));
+        // Range for normalisation
+        int yMin = PS_MAX(minRows, region->y0) - inRO->row0;
+        int yMax = PS_MIN(numRows - 1, region->y1) - inRO->row0;
+        int xMin = PS_MAX(minCols, region->x0) - inRO->col0;
+        int xMax = PS_MIN(numCols - 1, region->x1) - inRO->col0;
+        psTrace("psModules.imcombine", 2, "Normalising convolved mask image by %f over %d:%d,%d:%d\n",
+                sum, xMin, xMax, yMin, yMax);
+        for (int y = yMin; y <= yMax; y++) {
+            for (int x = xMin; x <= xMax; x++) {
+                convRO->image->data.F32[y][x] /= sum;
+            }
+        }
     }
     psFree(inRO);
@@ -116,6 +127,6 @@
     // Threshold the convolved image
     psPixels *bad = psPixelsAllocEmpty(PIXEL_LIST_BUFFER); // List of pixels that should be masked
-    for (int y = 0; y < numRows; y++) {
-        for (int x = 0; x < numCols; x++) {
+    for (int y = 0; y < convolved->numRows; y++) {
+        for (int x = 0; x < convolved->numCols; x++) {
             if (convolved->data.F32[y][x] > threshold) {
                 bad = psPixelsAdd(bad, PIXEL_LIST_BUFFER, x, y);
