Index: /branches/eam_branch_20081214/psphot/src/psphotSourceSize.c
===================================================================
--- /branches/eam_branch_20081214/psphot/src/psphotSourceSize.c	(revision 20990)
+++ /branches/eam_branch_20081214/psphot/src/psphotSourceSize.c	(revision 20991)
@@ -3,5 +3,5 @@
 
 // XXX This should be a recipe variable
-# define SN_LIMIT 5.0
+# define SN_LIMIT 3.0
 
 static float psphotModelContour(const psImage *image, const psImage *weight, const psImage *mask,
@@ -330,7 +330,9 @@
     int xe = pixels->numCols;
     for (int ix = xo; ix >= 0; ix--) {
+	if (mask->data.U8[yo][ix] & maskVal) {
+	    continue;
+	}
 	float SN = pixels->data.F32[yo][ix] / sqrt(weight->data.F32[yo][ix]);
 	if (SN < SN_LIMIT) {
-	    xs = ix + 1;
 	    break;
 	}
@@ -338,9 +340,12 @@
 	psVectorAppend (xf, (float) ix);
 	psVectorAppend (yf, (float) yo);
+	xs = ix;
     }
     for (int ix = xo + 1; ix < pixels->numCols; ix++) {
+	if (mask->data.U8[yo][ix] & maskVal) {
+	    continue;
+	}
 	float SN = pixels->data.F32[yo][ix] / sqrt(weight->data.F32[yo][ix]);
 	if (SN < SN_LIMIT) {
-	    xe = ix;
 	    break;
 	} 
@@ -348,6 +353,8 @@
 	psVectorAppend (xf, (float) ix);
 	psVectorAppend (yf, (float) yo);
-    }
-    psphotVisualShowCRMask (xf, yf);
+	xe = ix + 1;
+    }
+    // psphotVisualShowCRMask (xf, yf);
+    // fprintf (stderr, "first line: %d : %d - %d\n", yo, xs, xe);
 
     int startXs = xs;
@@ -369,4 +376,7 @@
 	// first, try all of the pixels in the valid range (xs < x < xe guaranteed to be real pixels)
 	for (int ix = xs; ix < xe; ix++) {
+	    if (mask->data.U8[iy][ix] & maskVal) {
+		continue;
+	    }
 	    float SN = pixels->data.F32[iy][ix] / sqrt(weight->data.F32[iy][ix]);
 	    if (SN < SN_LIMIT) {
@@ -383,7 +393,9 @@
 	// next, try the pixels to the left of the valid range for the previous row
 	for (int ix = xs - 1; ix >= 0; ix--) {
+	    if (mask->data.U8[iy][ix] & maskVal) {
+		continue;
+	    }
 	    float SN = pixels->data.F32[iy][ix] / sqrt(weight->data.F32[iy][ix]);
 	    if (SN < SN_LIMIT) {
-		newXs = ix + 1;
 		break;
 	    }
@@ -392,11 +404,14 @@
 	    psVectorAppend (xf, (float) ix);
 	    psVectorAppend (yf, (float) iy);
+	    newXs = ix;
 	}
 
 	// finally, try the pixels to the right of the valid range for the previous row
 	for (int ix = xe; ix < pixels->numCols; ix++) {
+	    if (mask->data.U8[iy][ix] & maskVal) {
+		continue;
+	    }
 	    float SN = pixels->data.F32[iy][ix] / sqrt(weight->data.F32[iy][ix]);
 	    if (SN < SN_LIMIT) {
-		newXe = ix;
 		break;
 	    }
@@ -405,10 +420,12 @@
 	    psVectorAppend (xf, (float) ix);
 	    psVectorAppend (yf, (float) iy);
-	}
+	    newXe = ix + 1;
+	}
+	// fprintf (stderr, "down line: %d : %d - %d\n", iy, newXs, newXe);
 
 	xs = newXs;
 	xe = newXe;
     }
-    psphotVisualShowCRMask (xf, yf);
+    // psphotVisualShowCRMask (xf, yf);
 
     xs = startXs;
@@ -430,4 +447,7 @@
 	// first, try all of the pixels in the valid range (xs < x < xe guaranteed to be real pixels)
 	for (int ix = xs; ix < xe; ix++) {
+	    if (mask->data.U8[iy][ix] & maskVal) {
+		continue;
+	    }
 	    float SN = pixels->data.F32[iy][ix] / sqrt(weight->data.F32[iy][ix]);
 	    if (SN < SN_LIMIT) {
@@ -444,7 +464,9 @@
 	// next, try the pixels to the left of the valid range for the previous row
 	for (int ix = xs - 1; ix >= 0; ix--) {
+	    if (mask->data.U8[iy][ix] & maskVal) {
+		continue;
+	    }
 	    float SN = pixels->data.F32[iy][ix] / sqrt(weight->data.F32[iy][ix]);
 	    if (SN < SN_LIMIT) {
-		newXs = ix + 1;
 		break;
 	    }
@@ -453,11 +475,14 @@
 	    psVectorAppend (xf, (float) ix);
 	    psVectorAppend (yf, (float) iy);
+	    newXs = ix;
 	}
 
 	// finally, try the pixels to the right of the valid range for the previous row
 	for (int ix = xe; ix < pixels->numCols; ix++) {
+	    if (mask->data.U8[iy][ix] & maskVal) {
+		continue;
+	    }
 	    float SN = pixels->data.F32[iy][ix] / sqrt(weight->data.F32[iy][ix]);
 	    if (SN < SN_LIMIT) {
-		newXe = ix;
 		break;
 	    }
@@ -466,5 +491,7 @@
 	    psVectorAppend (xf, (float) ix);
 	    psVectorAppend (yf, (float) iy);
-	}
+	    newXe = ix + 1;
+	}
+	// fprintf (stderr, "up line: %d : %d - %d\n", iy, newXs, newXe);
 
 	xs = newXs;
@@ -472,5 +499,5 @@
     }
 
-    psphotVisualShowCRMask (xf, yf);
+    // psphotVisualShowCRMask (xf, yf);
     psFree (xf);
     psFree (yf);
