Index: /branches/czw_branch/20120906/ppSkycell/src/ppSkycellLoop.c
===================================================================
--- /branches/czw_branch/20120906/ppSkycell/src/ppSkycellLoop.c	(revision 34624)
+++ /branches/czw_branch/20120906/ppSkycell/src/ppSkycellLoop.c	(revision 34625)
@@ -249,5 +249,5 @@
             psFree(view);
 
-	    //	    data->maskVal = 1;
+	    //	    data->maskVal = 0xffff;
             pmReadout *bin1RO = pmReadoutAlloc(NULL), *bin2RO = pmReadoutAlloc(NULL); // Binned readouts
             if (!pmReadoutRebin(bin1RO, inRO, data->maskVal, data->bin1, data->bin1)) {
Index: /branches/czw_branch/20120906/psModules/src/camera/pmFPABin.c
===================================================================
--- /branches/czw_branch/20120906/psModules/src/camera/pmFPABin.c	(revision 34624)
+++ /branches/czw_branch/20120906/psModules/src/camera/pmFPABin.c	(revision 34625)
@@ -43,4 +43,9 @@
     }
 
+    int Nbits = (int) (ceil(log(maskVal)/log(2)) + 1);
+    int *bitcounter = malloc(sizeof(int) * Nbits);
+    int pxlcount;
+    fprintf(stderr,">> %d %d %x\n",Nbits,maskVal,maskVal);
+
     int xLast = numColsIn - 1, yLast = numRowsIn - 1; // Last index
     int yStart = psImageBinningGetFineY(binning, 0); // Starting input y for binning
@@ -55,6 +60,24 @@
             float sum = 0.0;            // Sum of pixels
             int numPix = 0;             // Number of pixels
+
+	    for (int j = 0; j < Nbits; j++) { // Reset bit counter
+	      bitcounter[j] = 0;
+	    }
+	    pxlcount = 0;
+	    
             for (int y = yStart; y < yStop; y++) {
                 for (int x = xStart; x < xStop; x++) {
+		  if (inMask && (inMask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] != 0)) {
+		      for (int j = 0; j < Nbits; j++) {
+			psImageMaskType M = (psImageMaskType) pow(2,j);
+/* 			fprintf(stderr,"%x %d %x %x\n",inMask->data.PS_TYPE_IMAGE_MASK_DATA[y][x], */
+/* 				j,M,(inMask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & M)); */
+				
+			if (inMask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & M) {
+			  bitcounter[j]++;
+			}
+		      }
+		    }
+		  
                     if (inMask && (inMask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & maskVal)) {
                         continue;
@@ -65,7 +88,23 @@
                     sum += inImage->data.F32[y][x];
                     numPix++;
+
+
+/* 		    if ((yStart < 4561)&&(yStop > 4561)&& */
+/* 			(xStart < 3491)&&(xStop > 3491)) { */
+/* 		      //		      fprintf(stderr,"%d %d %g %x %x \n",x,y,inImage->data.F32[y][x],inMask->data.PS_TYPE_IMAGE_MASK_DATA[y][x],maskVal); */
+/* 		      for (int j = 0; j < Nbits; j++) { */
+/* 			fprintf(stderr,"%d %x %d\t",j,(int) pow(2,j), bitcounter[j]); */
+/* 		      } */
+/* 		      fprintf(stderr,"\n"); */
+/* 		    } */
                 }
             }
-
+/* 	    fprintf(stderr,"%d %d\t",xOut,yOut); */
+/* 	    for (int j = 0; j < Nbits; j++) { */
+/* 	      fprintf(stderr,"%d %x %d\t",j,(int) pow(2,j), bitcounter[j]); */
+/* 	    } */
+/* 	    fprintf(stderr,"\n"); */
+	    
+	    
 	    // Values to set
             float imageValue;
@@ -79,5 +118,11 @@
             }
             outImage->data.F32[yOut][xOut] = imageValue;
-            outMask->data.PS_TYPE_IMAGE_MASK_DATA[yOut][xOut] = maskValue;
+	    //            outMask->data.PS_TYPE_IMAGE_MASK_DATA[yOut][xOut] = maskValue;
+	    outMask->data.PS_TYPE_IMAGE_MASK_DATA[yOut][xOut] = 0;
+	    for (int j = 0; j < Nbits; j++) {
+	      if (bitcounter[j] > 0.5 * pxlcount) {
+		outMask->data.PS_TYPE_IMAGE_MASK_DATA[yOut][xOut] |= (int) pow(2,j);
+	      }
+	    }
             xStart = xStop;
         }
