Index: /branches/eam_branches/ipp-20100823/psModules/src/imcombine/pmStack.c
===================================================================
--- /branches/eam_branches/ipp-20100823/psModules/src/imcombine/pmStack.c	(revision 29218)
+++ /branches/eam_branches/ipp-20100823/psModules/src/imcombine/pmStack.c	(revision 29219)
@@ -19,5 +19,5 @@
 
 #include <stdio.h>
-#include <stdarg.h>
+#include <string.h> // for memset
 #include <pslib.h>
 
@@ -328,4 +328,8 @@
     *goodMask = 0xffff;
 
+    int nGoodBits[16]; // accumulate the good pixel bits here for fuzzy logic
+    psAssert (sizeof(psImageMaskType) == 2, "psImageMaskType is not the expected size");
+    memset (nGoodBits, 0, 16*sizeof(int));
+
     // Extract the pixel and mask data
     int numGood = 0;                    // Number of good pixels
@@ -371,5 +375,15 @@
         pixelSuspects->data.U8[numGood] = mask->data.PS_TYPE_IMAGE_MASK_DATA[yIn][xIn] & suspectMaskBits ? true : false;
 
-	*goodMask &= mask->data.PS_TYPE_IMAGE_MASK_DATA[yIn][xIn]; // save the mask bits still used
+	// *goodMask &= mask->data.PS_TYPE_IMAGE_MASK_DATA[yIn][xIn]; // save the mask bits still used
+	// check for set bits and increment counter as appropriate
+	{ 
+	    psImageMaskType value = 0x0001;
+	    for (int nbit = 0; nbit < 16; nbit ++) {
+		if (mask->data.PS_TYPE_IMAGE_MASK_DATA[yIn][xIn] & value) {
+		    nGoodBits[nbit] ++;
+		}
+		value <<= 1;
+	    }
+	}
 
         psImage *image = data->readout->image; // Image of interest
@@ -395,4 +409,17 @@
     pixelSuspects->n = numGood;
     *num = numGood;
+
+    // set the mask bits if nGoodBits[i] > f*numGood
+    {
+	# define SUSPECT_FRACTION 0.65
+	*goodMask = 0x0000;
+	psImageMaskType value = 0x0001;
+	for (int nbit = 0; nbit < 16; nbit ++) {
+	    if (nGoodBits[nbit] > SUSPECT_FRACTION*numGood) {
+		*goodMask |= value;
+	    }
+	    value <<= 1;
+	}
+    }
 
 #ifdef TESTING
