Index: trunk/psphot/src/LocalSky.c
===================================================================
--- trunk/psphot/src/LocalSky.c	(revision 4215)
+++ trunk/psphot/src/LocalSky.c	(revision 4216)
@@ -1,35 +1,18 @@
 # include "psphot.h"
 
-psSource *pmSourceDefinePixels(const psImageData *imdata,
-			       const psPeak *peak,
-			       psF32 Radius)
+bool pmSourceDefinePixels(psSource *mySource, 
+			  const psImageData *imdata,
+			  psF32 x, 
+			  psF32 y,
+			  psF32 Radius)
 {
-    // XXX EAM : EndRow is *exclusive* of pixel region (ie, last pixel + 1)
-    psS32 SubImageStartRow  = PS_MAX (0, peak->y - Radius);
-    psS32 SubImageEndRow    = PS_MIN (image->numRows, peak->y + Radius + 1);
-    psS32 SubImageStartCol  = PS_MAX (0, peak->x - Radius);
-    psS32 SubImageEndCol    = PS_MIN (image->numCols, peak->x + Radius + 1);
+    // Grab a subimage of the original image of size (2 * outerRadius).
+    srcRegion = psRegionSquare (x, y, Radius);
+    srcRegion = psRegionForImage (srcRegion, imdata->image);
 
-    // Grab a subimage of the original image of size (2 * outerRadius).
-    psImage *subImage = psImageSubset(imdata->image,
-                                      SubImageStartCol,
-                                      SubImageStartRow,
-                                      SubImageEndCol,
-                                      SubImageEndRow);
+    psImage *subImage = psImageSubset(imdata->image, srcRegion);
+    psImage *subImageMask = psImageSubset(imdata->mask, srcRegion);
+    psImage *subImageNoise = psImageSubset(imdata->noise, srcRegion);
 
-    psImage *subImageMask = psImageSubset(imdata->mask,
-					  SubImageStartCol,
-					  SubImageStartRow,
-					  SubImageEndCol,
-					  SubImageEndRow);
-
-    psImage *subImageNoise = psImageSubset(imdata->noise,
-					   SubImageStartCol,
-					   SubImageStartRow,
-					   SubImageEndCol,
-					   SubImageEndRow);
-
-    psSource *mySource = pmSourceAlloc();
-    mySource->peak = peak;
     mySource->pixels = subImage;
     mySource->mask = subImageMask;
@@ -49,25 +32,11 @@
     psImage *peak  = source->peak;
 
-    // Loop through the subimage, mask off pixels in the inner square.
-    // this uses a static mask value of 0x08
-    psS32 StartCol  = PS_MAX (0, peak->x - mask->col0 - Radius);
-    psS32 StartRow  = PS_MAX (0, peak->y - mask->row0 - Radius);
-    psS32 EndCol    = PS_MIN (mask->numCols, peak->x - mask->col0 + Radius + 1);
-    psS32 EndRow    = PS_MIN (mask->numRows, peak->y - mask->row0 + Radius + 1);
-    for (psS32 row = StartRow; row < EndRow; row++) {
-        for (psS32 col = StartCol; col < EndCol; col++) {
-	    mask->data.U8[row][col] |= 0x80;
-        }
-    }
+    srcRegion = psRegionSquare (peak->x, peak->y, Radius);
+    srcRegion = psRegionForImage (srcRegion, imdata->mask);
+    psImageMaskRegion (imdata->mask, region, OR, 0x80);
 
     psStats *myStats = psStatsAlloc(statsOptions);
     myStats = psImageStats(myStats, image, mask, 0xff);
-    
-    // clear the mask on the inner pixels
-    for (psS32 row = StartRow; row < EndRow; row++) {
-        for (psS32 col = StartCol; col < EndCol; col++) {
-	    mask->data.U8[row][col] &= 0x7f;
-        }
-    }
+    psImageMaskRegion (imdata->mask, region, AND, 0x7f);
 
     mySource->moments = pmMomentsAlloc();
@@ -79,34 +48,7 @@
 }
 
-// set mask pixels within the circle
-bool psImageMaskCircle(psImage *mask,
-		       psF32 x, 
-		       psF32 y,
-		       psF32 radius,
-		       int maskValue)
-{
-    // XXX EAM : for the circle to stay on the image
-    // XXX EAM : EndRow is *exclusive* of pixel region (ie, last pixel + 1)
-    psS32 SubImageStartRow  = PS_MAX (0, x + mask->row0 - radiusS32);
-    psS32 SubImageEndRow    = PS_MIN (mask->numRows, SubImageCenterRow + radiusS32 + 1);
-    psS32 SubImageStartCol  = PS_MAX (0, SubImageCenterCol - radiusS32);
-    psS32 SubImageEndCol    = PS_MIN (mask->numCols, SubImageCenterCol + radiusS32 + 1);
-
-    // Loop through the subimage mask, set maskValue bits
-    for (psS32 row = 0 ; row < source->mask->numRows; row++) {
-        for (psS32 col = 0 ; col < source->mask->numCols; col++) {
-
-            if (CheckRadius2((psF32) radiusS32,
-                             (psF32) radiusS32,
-                             radius,
-                             (psF32) col,
-                             (psF32) row)) {
-                source->mask->data.U8[row][col] = 0;
-            } else {
-                source->mask->data.U8[row][col] = 1;
-            }
-        }
-    }
-    return(true);
+// define a square region centered on the given coordinate
+psRegion *psRegionSquare (psF32 x, psF32 y, psF32 radius) {
+    region = psRegionAlloc (x - Radius, y - Radius, x + Radius + 1, y + Radius + 1);
+    return (region);
 }
-
