Index: /branches/eam_branches/ipp-20101205/psphot/src/psphotVisual.c
===================================================================
--- /branches/eam_branches/ipp-20101205/psphot/src/psphotVisual.c	(revision 30164)
+++ /branches/eam_branches/ipp-20101205/psphot/src/psphotVisual.c	(revision 30165)
@@ -93,11 +93,4 @@
     strcpy (coords.ctype, "RA---TAN");
 
-    psStats *stats = psStatsAlloc (PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV);
-    psRandom *rng = psRandomAlloc(PS_RANDOM_TAUS);
-    if (!psImageBackground(stats, NULL, inImage, NULL, 0, rng)) {
-        fprintf (stderr, "failed to get background values\n");
-        return false;
-    }
-
     image.Nx = inImage->numCols;
     image.Ny = inImage->numRows;
@@ -125,7 +118,101 @@
     free (image.data2d);
 
+    return true;
+}
+
+bool psphotVisualShowObjectRegions (pmReadout *readout, psMetadata *recipe, psArray *sources) {
+
+    KiiImage image;
+    KapaImageData data;
+    Coords coords;
+
+    bool status = false;
+
+    // user-defined masks to test for good/bad pixels (build from recipe list if not yet set)
+    psImageMaskType maskVal = psMetadataLookupImageMask(&status, recipe, "MASK.PSPHOT"); // Mask value for bad pixels
+    assert (maskVal);
+
+    psImageMaskType markVal = psMetadataLookupImageMask(&status, recipe, "MARK.PSPHOT"); // Mask value for bad pixels
+    assert (maskVal);
+
+    maskVal |= markVal;
+
+    if (!pmVisualTestLevel("psphot.image.objects", 2)) return true;
+
+    int kapa = psphotKapaChannel (1);
+    if (kapa == -1) return false;
+
+    strcpy (coords.ctype, "RA---TAN");
+
+    psImage *inImage = readout->image;
+    psImage *inMask = readout->mask;
+    image.Nx = inImage->numCols;
+    image.Ny = inImage->numRows;
+    
+    psStats *stats = psStatsAlloc (PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV);
+    psRandom *rng = psRandomAlloc(PS_RANDOM_TAUS);
+    if (!psImageBackground(stats, NULL, inImage, inMask, 0xffff, rng)) {
+        fprintf (stderr, "failed to get background values\n");
+        return false;
+    }
+
+    ALLOCATE (image.data2d, float *, image.Ny);
+    for (int iy = 0; iy < image.Ny; iy++) {
+        ALLOCATE (image.data2d[iy], float, image.Nx);
+	for (int ix = 0; ix < image.Nx; ix++) {
+	    image.data2d[iy][ix] = 0;
+	}
+    }
+
+    // loop over sources and set unmasked pixels to 0
+    for (int i = 0; i < sources->n; i++) {
+
+	pmSource *source = sources->data[i];
+	if (source == NULL) continue;
+
+	psImage *mask = source->maskObj;
+	if (mask == NULL) continue;
+
+	for (int iy = 0; iy < mask->numRows; iy++) {
+	    int jy = iy + mask->row0;
+	    if (jy < 0) continue;
+	    if (jy >= inImage->numRows) continue;
+	    for (int ix = 0; ix < mask->numCols; ix++) {
+		int jx = ix + mask->col0;
+		if (jx < 0) continue;
+		if (jx >= inImage->numCols) continue;
+
+		if (mask->data.PS_TYPE_IMAGE_MASK_DATA[iy][ix] & maskVal) continue;
+		image.data2d[jy][jx] = 1;
+	    }
+	}
+    }
+
+    for (int iy = 0; iy < image.Ny; iy++) {
+	for (int ix = 0; ix < image.Nx; ix++) {
+	    image.data2d[iy][ix] = (image.data2d[iy][ix] == 0.0) ? NAN : inImage->data.F32[iy][ix];
+	}
+    }
+
+    strcpy (data.name, "maskObj");
+    strcpy (data.file, "maskObj");
+    // data.zero = 0.0;
+    // data.range = 1.0;
+    data.zero = stats->robustMedian - stats->robustStdev;
+    data.range = 5*stats->robustStdev;
+    data.logflux = 0;
+
+    KiiSetChannel (kapa, 2);
+    KiiNewPicture2D (kapa, &image, &data, &coords);
+
+    for (int iy = 0; iy < image.Ny; iy++) {
+        free (image.data2d[iy]);
+    }
+    free (image.data2d);
+
     psFree (stats);
     psFree (rng);
-
+    
+    pmVisualAskUser(NULL);
     return true;
 }
@@ -2401,5 +2488,6 @@
 }
 
-bool psphotVisualShowResidualImage (pmReadout *readout) {
+// option to redo variance since in some cases we may have displayed a different image in the meanwhile
+bool psphotVisualShowResidualImage (pmReadout *readout, bool reshow) {
 
     if (!pmVisualTestLevel("psphot.image.resid", 2)) return true;
@@ -2408,5 +2496,8 @@
     if (myKapa == -1) return false;
 
-    psphotVisualScaleImage (myKapa, readout->variance, readout->mask, "variance", 1);
+    if (reshow) {
+	psphotVisualShowMask (myKapa, readout->mask, "mask", 2);
+	psphotVisualScaleImage (myKapa, readout->variance, readout->mask, "variance", 1);
+    }
     psphotVisualScaleImage (myKapa, readout->image, readout->mask, "resid", 0);
 
