Index: trunk/pois/src/poisFindStamps.c
===================================================================
--- trunk/pois/src/poisFindStamps.c	(revision 3813)
+++ trunk/pois/src/poisFindStamps.c	(revision 5717)
@@ -7,11 +7,11 @@
 
 psArray *poisFindStamps(psArray *stamps, // Existing list of stamps, or NULL
-			const psImage *image, // Image for which to find stamps
-			const psImage *mask, // Mask image
-			const poisConfig *config // Configuration values
+                        const psImage *image, // Image for which to find stamps
+                        const psImage *mask, // Mask image
+                        const poisConfig *config // Configuration values
     )
 {
-    int nx = config->xImage;		// Size of image in x
-    int ny = config->yImage;		// Size of image in y
+    int nx = config->xImage;            // Size of image in x
+    int ny = config->yImage;            // Size of image in y
 
     assert(image->numCols == nx && image->numRows == ny);
@@ -21,20 +21,18 @@
     assert(!stamps || stamps->n == config->nsx * config->nsy);
 
-    float threshold = config->threshold;// Stamp threshold
-    int footprint = config->footprint;	// Footprint for stamps
+    int footprint = config->footprint;  // Footprint for stamps
     int borderx = footprint + config->xKernel; // Border around image in x
     int bordery = footprint + config->yKernel; // Border around image in y
-    int nsx = config->nsx;		// Number of stamps in x
-    int nsy = config->nsy;		// Number of stamps in y
+    int nsx = config->nsx;              // Number of stamps in x
+    int nsy = config->nsy;              // Number of stamps in y
 
-    psF32 **pixels = image->data.F32;	// The image pixels
-    psU8 **maskpix = mask->data.U8; // The mask pixels
+    psF32 **pixels = image->data.F32;   // The image pixels
 
     if (stamps == NULL) {
-	stamps = psArrayAlloc(nsx * nsy);
-					// Initialise; should be done by psArrayAlloc
-	for (int i = 0; i < nsx * nsy; i++) {
-	    stamps->data[i] = NULL;
-	}
+        stamps = psArrayAlloc(nsx * nsy);
+        // Initialise
+        for (int i = 0; i < nsx * nsy; i++) {
+            stamps->data[i] = poisStampAlloc();
+        }
     }
 
@@ -42,57 +40,42 @@
     int num = 0;
     for (int i = 0; i < nsx; i++) {
-	for (int j = 0; j < nsy; j++) {
-	    poisStamp *stamp = stamps->data[num]; // The stamp
-	    if (stamp == NULL) {
-		stamp = stamps->data[num] = poisStampAlloc();
-	    }
+        for (int j = 0; j < nsy; j++) {
+            poisStamp *stamp = stamps->data[num]; // The stamp
 
-	    // Only find a new stamp if we need to
-	    if (stamp->status == POIS_STAMP_RESET) {
-		// Find maximum non-masked value in the image section, but don't include a footprint around
-		// the edge
-		float max = - INFINITY;		// Negative infinity
-		int bestx = 0, besty = 0;	// Position of maximum
-		
-		for (int y = bordery + j * (ny - 2.0 * bordery) / nsy; 
-		     y < bordery + (j + 1) * (ny - 2.0 * bordery) / nsy; y++) {
-		    for (int x = borderx + i * (nx - 2.0 * borderx) / nsx;
-			 x < borderx + (i + 1) * (nx - 2.0 * borderx) / nsx; x++) {
-			if (pixels[y][x] > max && pixels[y][x] >= threshold) {
-			    // Check the footprint
-			    int ok = 1;
-			    for (int v = y - footprint; v <= y + footprint && ok; v++) {
-				for (int u = x - footprint; u <= x + footprint && ok; u++) {
-				    if (maskpix[v][u] &
-					(POIS_MASK_BAD | POIS_MASK_NEAR_BAD | POIS_MASK_STAMP)) {
-					ok = 0;
-				    }
-				}
-			    }
-			    if (ok) {
-				max = pixels[y][x];
-				bestx = x;
-				besty = y;
-			    }
-			} // Done checking the candidate pixel
-		    }
-		} // Done iterating over this image section
-		
-		// Store the stamp
-		if (max != - INFINITY) {
-		    stamp->x = bestx;
-		    stamp->y = besty;
-		    stamp->status = POIS_STAMP_RECALC;
-		} else {
-		    // No stamp in this section
-		    stamp->status = POIS_STAMP_BAD;
-		}
-	    } // Finding a new stamp
+            // Only find a new stamp if we need to
+            if ((stamp->x == 0 && stamp->y == 0) || stamp->status == POIS_STAMP_RESET) {
+                // Find maximum non-masked value in the image section, but don't include a footprint around
+                // the edge
+                float max = - INFINITY;         // Negative infinity
+                int bestx = 0, besty = 0;       // Position of maximum
 
-	    psTrace("pois.findStamps", 7, "Stamp %d: %d,%d\n", num, stamp->x, stamp->y);
-	    num++;
-	}
+                for (int y = bordery + j * (ny - 2.0 * bordery) / nsy;
+                     y < bordery + (j + 1) * (ny - 2.0 * bordery) / nsy; y++) {
+                    for (int x = borderx + i * (nx - 2.0 * borderx) / nsx;
+                         x < borderx + (i + 1) * (nx - 2.0 * borderx) / nsx; x++) {
+                        if (pixels[y][x] > max && poisCheckStamp(image, mask, x, y, config)) {
+                            max = pixels[y][x];
+                            bestx = x;
+                            besty = y;
+                        }
+                    }
+                } // Done iterating over this image section
+
+                // Store the stamp
+                if (max != - INFINITY) {
+                    stamp->x = bestx;
+                    stamp->y = besty;
+                    stamp->status = POIS_STAMP_RECALC;
+                } else {
+                    // No stamp in this section
+                    stamp->status = POIS_STAMP_BAD;
+                }
+            } // Finding a new stamp
+
+            psTrace("pois.findStamps", 7, "Stamp %d: %d,%d\n", num, stamp->x, stamp->y);
+            num++;
+        }
     } // Done iterating over sections
 
     return stamps;
-}				    
+}
