Index: trunk/psLib/src/imageops/psImageStats.c
===================================================================
--- trunk/psLib/src/imageops/psImageStats.c	(revision 5096)
+++ trunk/psLib/src/imageops/psImageStats.c	(revision 5137)
@@ -9,6 +9,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.80 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-09-22 22:49:29 $
+ *  @version $Revision: 1.81 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-09-26 22:35:53 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -638,16 +638,73 @@
 // count number of pixels with given mask value
 long psImageCountPixelMask (psImage *mask,
+                            psRegion region,
                             psMaskType value)
 {
     long Npixels = 0;
-
-    for (long i = 0; i < mask->numRows; i++) {
-        for (long j = 0; j < mask->numCols; j++) {
-            if (mask->data.U8[i][j] & value) {
-                Npixels ++;
-            }
-        }
+    int x0 = 0;
+    int y0 = 0;
+    int x1 = 0;
+    int y1 = 0;
+    psElemType type;
+    if (mask == NULL) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true,
+                PS_ERRORTEXT_psImage_IMAGE_NULL);
+        return -1;
+    }
+    if (region.x1 > mask->numCols || region.y1 > mask->numRows) {
+        psError(PS_ERR_BAD_PARAMETER_SIZE, true,
+                "psRegion input is outside of image boundary\n");
+        return -1;
+    }
+    if (region.x0 <= 0 || region.x1 <= 0 || region.y0 <= 0 || region.y1 <= 0) {
+        region = psRegionForImage(mask, region);
+    }
+    if (region.x0 > region.x1 || region.y0 > region.y1) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "Invalid region.  Lower boundary greater than upper boundary.\n");
+        return -1;
+    }
+    if (region.x0 == region.x1 || region.y0 == region.y1) {
+        psError(PS_ERR_BAD_PARAMETER_SIZE, true,
+                "psRegion input contains 0 pixels\n");
+        return -1;
+    }
+
+    x0 = (int)(roundf(region.x0));
+    x1 = (int)(roundf(region.x1));
+    y0 = (int)(roundf(region.y0));
+    y1 = (int)(roundf(region.y1));
+
+    type = mask->type.type;
+
+    switch (type) {
+    case PS_TYPE_U8:
+        for (long i = x0; i < x1; i++) {
+            for (long j = y0; j < y1; j++) {
+                if (mask->data.U8[i][j] & value) {
+                    Npixels ++;
+                }
+            }
+        }
+        break;
+    case PS_TYPE_S8:
+    case PS_TYPE_S16:
+    case PS_TYPE_S32:
+    case PS_TYPE_S64:
+    case PS_TYPE_U16:
+    case PS_TYPE_U32:
+    case PS_TYPE_U64:
+    case PS_TYPE_F32:
+    case PS_TYPE_F64:
+    case PS_TYPE_C32:
+    case PS_TYPE_C64:
+    default:
+        psError(PS_ERR_BAD_PARAMETER_TYPE, true,
+                PS_ERRORTEXT_psImage_IMAGE_MASK_TYPE, type, PS_TYPE_U8);
+        return -1;
     }
     return (Npixels);
-}
-
+
+
+}
+
