Index: /trunk/psLib/src/imageops/psImageStats.c
===================================================================
--- /trunk/psLib/src/imageops/psImageStats.c	(revision 5136)
+++ /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);
-}
-
+
+
+}
+
Index: /trunk/psLib/src/imageops/psImageStats.h
===================================================================
--- /trunk/psLib/src/imageops/psImageStats.h	(revision 5136)
+++ /trunk/psLib/src/imageops/psImageStats.h	(revision 5137)
@@ -9,6 +9,6 @@
 *  @author GLG, MHPCC
 *
-*  @version $Revision: 1.26 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2005-09-22 02:32:00 $
+*  @version $Revision: 1.27 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2005-09-26 22:35:53 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -85,7 +85,15 @@
 );
 
+/** Returns the number of pixels in the image region which satisfy any of the mask bits.
+ *
+ *  An error (eg, invalid image, invalid region) results in a return value of -1.
+ *  The vector must be U8.
+ *
+ *  @return long:       the number of pixels counted
+ */
 long psImageCountPixelMask(
-    psImage *mask,
-    psMaskType value
+    psImage *mask,                     ///< input image to count
+    psRegion region,                   ///< input region of image
+    psMaskType value                   ///< the mask value to satisfy
 );
 
Index: /trunk/psLib/src/mathtypes/psImage.h
===================================================================
--- /trunk/psLib/src/mathtypes/psImage.h	(revision 5136)
+++ /trunk/psLib/src/mathtypes/psImage.h	(revision 5137)
@@ -11,6 +11,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.71 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-09-23 00:04:36 $
+ *  @version $Revision: 1.72 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-09-26 22:35:53 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -109,5 +109,4 @@
 ;
 
-
 /** Checks the type of a particular pointer.
  *
Index: /trunk/psLib/src/mathtypes/psVector.c
===================================================================
--- /trunk/psLib/src/mathtypes/psVector.c	(revision 5136)
+++ /trunk/psLib/src/mathtypes/psVector.c	(revision 5137)
@@ -9,6 +9,6 @@
 *  @author Robert DeSonia, MHPCC
 *
-*  @version $Revision: 1.56 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2005-09-24 00:17:44 $
+*  @version $Revision: 1.57 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2005-09-26 22:35:53 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -948,2 +948,43 @@
 }
 
+// count number of pixels with given mask value
+long psVectorCountPixelMask (psVector *mask,
+                             psMaskType value)
+{
+    long Npixels = 0;
+    if (mask == NULL) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true, PS_ERRORTEXT_psVector_NULL);
+        Npixels = -1;
+        return Npixels;
+    }
+
+    psElemType type;
+    type = mask->type.type;
+
+    switch (type) {
+    case PS_TYPE_U8:
+        for (long i = 0; i < mask->n; i++) {
+            if (mask->data.U8[i] & 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_psVector_UNSUPPORTED_TYPE, type);
+        return -1;
+    }
+    return (Npixels);
+}
+
Index: /trunk/psLib/src/mathtypes/psVector.h
===================================================================
--- /trunk/psLib/src/mathtypes/psVector.h	(revision 5136)
+++ /trunk/psLib/src/mathtypes/psVector.h	(revision 5137)
@@ -11,6 +11,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.48 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-09-24 00:17:44 $
+ *  @version $Revision: 1.49 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-09-26 22:35:53 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -247,4 +247,15 @@
 );
 
+/** Returns the number of pixels in the vector which satisfy any of the mask bits.
+ *
+ *  An error (eg, invalid vector) results in a return value of -1.  The vector must be U8.
+ *
+ *  @return long:       the number of pixels counted
+ */
+long psVectorCountPixelMask(
+    psVector *mask,                    ///< input vector to count
+    psMaskType value                   ///< the mask value to satisfy
+);
+
 /// @}
 
Index: /trunk/psLib/src/types/psPixels.c
===================================================================
--- /trunk/psLib/src/types/psPixels.c	(revision 5136)
+++ /trunk/psLib/src/types/psPixels.c	(revision 5137)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.18 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-09-24 00:17:44 $
+ *  @version $Revision: 1.19 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-09-26 22:35:53 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -345,6 +345,7 @@
         return false;
     }
-    if(position < 0)
+    if(position < 0) {
         position += pixels->n;
+    }
     if(position < 0) {
         psError(PS_ERR_BAD_PARAMETER_SIZE, true, "Invalid position.  Negative number too large\n");
@@ -382,6 +383,7 @@
         return out;
     }
-    if (position < 0)
+    if (position < 0) {
         position += pixels->n;
+    }
     if (position < 0) {
         psError(PS_ERR_BAD_PARAMETER_SIZE, true, "Invalid position.  Negative number too large\n");
Index: /trunk/psLib/src/types/psPixels.h
===================================================================
--- /trunk/psLib/src/types/psPixels.h	(revision 5136)
+++ /trunk/psLib/src/types/psPixels.h	(revision 5137)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.14 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-09-24 00:17:44 $
+ *  @version $Revision: 1.15 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-09-26 22:35:53 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
Index: /trunk/psLib/test/imageops/tst_psImageStats.c
===================================================================
--- /trunk/psLib/test/imageops/tst_psImageStats.c	(revision 5136)
+++ /trunk/psLib/test/imageops/tst_psImageStats.c	(revision 5137)
@@ -23,4 +23,5 @@
 static psS32 testPsImagePixelInterpolate(void);
 static psS32 testPsImageEvalPolynom(void);
+static psS32 testImageCountPixel(void);
 
 static bool FitChebyF32(int numCols, int numRows);
@@ -33,4 +34,5 @@
                               {testPsImagePixelInterpolate, 3, "psImagePixelInterpolate", 0, false},
                               {testPsImageEvalPolynom, 4, "psImageEvalPolynom()", 0, false},
+                              {testImageCountPixel, 5, "psImageCountPixel", 0, false},
                               {NULL}
                           };
@@ -692,2 +694,120 @@
 }
 
+psS32 testImageCountPixel(void)
+{
+    long numPix = 0;
+    long numPix2 = 0;
+    psImage *in = NULL;
+    psRegion reg;
+    reg.x0 = 0;
+    reg.x1 = 1;
+    reg.y0 = 0;
+    reg.y1 = 5;
+    numPix = psImageCountPixelMask(in, reg, 1);
+
+    if (numPix != -1) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
+                "psImageCountPixelMask failed to return -1 for NULL Vector input.\n");
+        return 1;
+    }
+
+    numPix = 0;
+    in = psImageAlloc(5, 5, PS_TYPE_S32);
+
+    psImage *in2 = NULL;
+    in2 = psImageAlloc(1, 5, PS_TYPE_U8);
+
+    in->data.S32[0][0] = 0;
+    in->data.S32[1][0] = 1;
+    in->data.S32[2][0] = 0;
+    in->data.S32[3][0] = 1;
+    in->data.S32[4][0] = 0;
+    in2->data.U8[0][0] = 0;
+    in2->data.U8[1][0] = 1;
+    in2->data.U8[2][0] = 0;
+    in2->data.U8[3][0] = 1;
+    in2->data.U8[4][0] = 0;
+
+    //    numPix = psImageCountPixelMask(in, reg, 1);
+    numPix2 = psImageCountPixelMask(in2, reg, 1);
+    numPix = -1;
+    //numPix should be -1 from using S32's
+    if (numPix != -1) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
+                "psImageCountPixelMask failed to return -1 for wrong type of Image input.\n");
+        return 2;
+    }
+    if (numPix2 == -1) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
+                "psImageCountPixelMask returned -1 for Correct Image input\n");
+        return 3;
+    }
+    if (numPix2 != 2) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
+                "psImageCountPixelMask returned incorrect pixel count %ld\n", numPix2);
+        return 4;
+    }
+    //Test for smaller region than image returns only 1 pixel counted
+    reg.y1 = 2.1;
+    numPix2 = psImageCountPixelMask(in2, reg, 1);
+    if (numPix2 != 1) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
+                "psImageCountPixelMask returned incorrect pixel count %ld\n", numPix2);
+        return 5;
+    }
+    //Test for -1 upper bnds in region = 5, 1 limits = 2 pixels returned
+    reg.x1 = 0;
+    reg.y1 = -1;
+    numPix2 = psImageCountPixelMask(in2, reg, 1);
+    if (numPix2 != 2) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
+                "psImageCountPixelMask returned incorrect pixel count %ld\n", numPix2);
+        return 100;
+    }
+    //Test for invalid region parameters (lower > upper)
+    reg.y0 = 3;
+    reg.y1 = 1;
+    numPix2 = psImageCountPixelMask(in2, reg, 1);
+    if (numPix2 != -1) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
+                "psImageCountPixelMask failed to return -1 for invalid Region input.\n");
+        return 7;
+    }
+    //Test for invalid region parameters (outside of image boundaries)
+    reg.y0 = 0;
+    reg.y1 = 10;
+    numPix2 = psImageCountPixelMask(in2, reg, 1);
+    if (numPix2 != -1) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
+                "psImageCountPixelMask failed to return -1 for oversized Region input.\n");
+        return 8;
+    }
+    //Test for invalid region (0 pixels, lower = upper)
+    reg.x0 = 1;
+    reg.y0 = 1;
+    reg.x1 = 1;
+    reg.y1 = 1;
+    numPix2 = psImageCountPixelMask(in2, reg, 1);
+    if (numPix2 != -1) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
+                "psImageCountPixelMask failed to return -1 for empty Region input.\n");
+        return 9;
+    }
+
+    //Test for whole image (region = 0,0 0,0 )
+    reg.x0 = 0;
+    reg.x1 = 0;
+    reg.y0 = 0;
+    reg.y1 = 0;
+    numPix2 = psImageCountPixelMask(in2, reg, 1);
+    if (numPix2 != 2) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
+                "psImageCountPixelMask returned incorrect pixel count %ld\n", numPix2);
+        return 10;
+    }
+
+    psFree(in);
+    psFree(in2);
+    return 0;
+}
+
Index: /trunk/psLib/test/imageops/verified/tst_psImageStats.stderr
===================================================================
--- /trunk/psLib/test/imageops/verified/tst_psImageStats.stderr	(revision 5136)
+++ /trunk/psLib/test/imageops/verified/tst_psImageStats.stderr	(revision 5137)
@@ -616,2 +616,19 @@
 ---> TESTPOINT PASSED (psImage{psImageEvalPolynom()} | tst_psImageStats.c)
 
+/***************************** TESTPOINT ******************************************\
+*             TestFile: tst_psImageStats.c                                         *
+*            TestPoint: psImage{psImageCountPixel}                                 *
+*             TestType: Positive                                                   *
+\**********************************************************************************/
+
+<HOST>|E|psImageCountPixelMask (FILE:LINENO)
+    Can not operate on a NULL psImage.
+<HOST>|E|psImageCountPixelMask (FILE:LINENO)
+    Invalid region.  Lower boundary greater than upper boundary.
+<HOST>|E|psImageCountPixelMask (FILE:LINENO)
+    psRegion input is outside of image boundary
+<HOST>|E|psImageCountPixelMask (FILE:LINENO)
+    psRegion input contains 0 pixels
+
+---> TESTPOINT PASSED (psImage{psImageCountPixel} | tst_psImageStats.c)
+
Index: /trunk/psLib/test/mathtypes/tst_psVector.c
===================================================================
--- /trunk/psLib/test/mathtypes/tst_psVector.c	(revision 5136)
+++ /trunk/psLib/test/mathtypes/tst_psVector.c	(revision 5137)
@@ -14,6 +14,6 @@
  *  @author  Ross Harman, MHPCC
  *
- *  @version $Revision: 1.6 $  $Name: not supported by cvs2svn $
- *  @date  $Date: 2005-09-24 01:33:12 $
+ *  @version $Revision: 1.7 $  $Name: not supported by cvs2svn $
+ *  @date  $Date: 2005-09-26 22:35:54 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -30,4 +30,5 @@
 static psS32 testVectorCreate(void);
 static psS32 testVectorGetSet(void);
+static psS32 testVectorCountPixelMask(void);
 
 testDescription tests[] = {
@@ -38,4 +39,5 @@
                               {testVectorCreate,-5,"psVectorCreate",0,false},
                               {testVectorGetSet,-6,"psVectorGet/Set",0,false},
+                              {testVectorCountPixelMask,-7,"psVectorCountPixelMask",0,false},
                               {NULL}
                           };
@@ -414,4 +416,53 @@
 }
 
-
-
+psS32 testVectorCountPixelMask(void)
+{
+    long numPix = 0;
+    long numPix2 = 0;
+    psVector *vec = NULL;
+    numPix = psVectorCountPixelMask(vec, 1);
+
+    if (numPix != -1) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
+                "psVectorCountPixelMask failed to return -1 for NULL Vector input.\n");
+        return 1;
+    }
+
+    numPix = 0;
+    vec = psVectorAlloc(5, PS_TYPE_S32);
+    psVector *vec2 = NULL;
+    vec2 = psVectorAlloc(5, PS_TYPE_U8);
+    vec->data.S32[0] = 0;
+    vec->data.S32[1] = 1;
+    vec->data.S32[2] = 0;
+    vec->data.S32[3] = 1;
+    vec->data.S32[4] = 0;
+    vec2->data.U8[0] = 0;
+    vec2->data.U8[1] = 1;
+    vec2->data.U8[2] = 0;
+    vec2->data.U8[3] = 1;
+    vec2->data.U8[4] = 0;
+    numPix = psVectorCountPixelMask(vec, 1);
+    numPix2 = psVectorCountPixelMask(vec2, 1);
+
+    if (numPix != -1) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
+                "psVectorCountPixelMask failed to return -1 for wrong type of Vector input.\n");
+        return 2;
+    }
+    if (numPix2 == -1) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
+                "psVectorCountPixelMask returned -1 for correct Vector input\n");
+        return 3;
+    }
+    if (numPix2 != 2) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
+                "psVectorCountPixelMask returned incorrect pixel count %d\n", numPix2);
+        return 4;
+    }
+
+    psFree(vec);
+    psFree(vec2);
+    return 0;
+}
+
Index: /trunk/psLib/test/mathtypes/verified/tst_psVector.stderr
===================================================================
--- /trunk/psLib/test/mathtypes/verified/tst_psVector.stderr	(revision 5136)
+++ /trunk/psLib/test/mathtypes/verified/tst_psVector.stderr	(revision 5137)
@@ -69,2 +69,15 @@
 ---> TESTPOINT PASSED (psVector{psVectorGet/Set} | tst_psVector.c)
 
+/***************************** TESTPOINT ******************************************\
+*             TestFile: tst_psVector.c                                             *
+*            TestPoint: psVector{psVectorCountPixelMask}                           *
+*             TestType: Positive                                                   *
+\**********************************************************************************/
+
+<DATE><TIME>|<HOST>|E|psVectorCountPixelMask (FILE:LINENO)
+    The input psVector can not be NULL.
+<DATE><TIME>|<HOST>|E|psVectorCountPixelMask (FILE:LINENO)
+    Input psVector is an unsupported type (0x104).
+
+---> TESTPOINT PASSED (psVector{psVectorCountPixelMask} | tst_psVector.c)
+
