/** @file psImageStats.c
 *  \brief Routines for calculating statistics on images.
 *  @ingroup ImageStats
 *
 *  This file will hold the prototypes for procedures which calculate
 *  statistic on images, histograms on images, and fit/evaluate Chebyshev
 *  polynomials to images.
 *
 *  @author GLG, MHPCC
 *
 *  @version $Revision: 1.83 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2005-10-12 21:02:20 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <float.h>
#include <math.h>
#include "psMemory.h"
#include "psVector.h"
#include "psTrace.h"
#include "psError.h"
#include "psStats.h"
#include "psImage.h"
#include "psPolynomial.h"
#include "psImageStats.h"
#include "psConstants.h"
#include "psErrorText.h"

/// This routine must determine the various statistics for the image.
/*****************************************************************************
psImageStats(stats, in, mask, maskVal): this routine simply calls the
psVectorStats() routine, which does the actual statistical calculation.  In
order to do so, we create dummy psVectors and set their "data" pointer to that
of the input psImages.
 
XXX: use static psVectors
 
XXX: optimize this.  2k vs 4k, sample mean, takes8 seconds on Gene's machine.
Should take .2.
 *****************************************************************************/
psStats* psImageStats(psStats* stats,
                      const psImage* in,
                      const psImage* mask,
                      psMaskType maskVal)
{
    psVector *junkData = NULL;
    psVector *junkMask = NULL;

    PS_ASSERT_PTR_NON_NULL(stats, NULL);
    PS_ASSERT_INT_NONZERO(stats->options, NULL);
    PS_ASSERT_IMAGE_NON_NULL(in, NULL)
    if (mask != NULL) {
        PS_ASSERT_IMAGE_TYPE(mask, PS_TYPE_U8, NULL);
        PS_ASSERT_IMAGES_SIZE_EQUAL(in, mask, NULL);
    }

    if (in->parent == NULL) {
        // stuff the image data into a psVector struct.
        junkData = (psVector *) psAlloc(sizeof(psVector));
        junkData->type = in->type;
        *(int*)&junkData->nalloc = in->numRows * in->numCols;
        junkData->n = junkData->nalloc;
        junkData->data.U8 = in->data.V[0];      // since psImage data is contiguous...
    } else {
        // image not necessarily contiguous
        int numRows = in->numRows;
        int numCols = in->numCols;
        int rowSize = numCols * (PSELEMTYPE_SIZEOF(in->type.type));

        junkData = psVectorAlloc(numRows*numCols, in->type.type);
        junkData->n = junkData->nalloc;

        psU8* data = junkData->data.U8;
        for (int row = 0; row < numRows; row++) {
            memcpy(data, in->data.V[row], rowSize);
            data += rowSize;
        }
    }

    if (mask != NULL) {
        if (mask->parent == NULL) {
            // stuff the mask data into a psVector struct.
            junkMask = psAlloc(sizeof(psVector));
            junkMask->type = mask->type;
            *(int*)&junkMask->nalloc = mask->numRows * mask->numCols;
            junkMask->n = junkMask->nalloc;
            junkMask->data.U8 = mask->data.V[0];
        } else {
            // image not necessarily contiguous
            int numRows = mask->numRows;
            int numCols = mask->numCols;
            int rowSize = numCols * (PSELEMTYPE_SIZEOF(mask->type.type));

            junkMask = psVectorAlloc(numRows*numCols, mask->type.type);
            junkMask->n = junkMask->nalloc;

            psU8* data = junkMask->data.U8;
            for (int row = 0; row < numRows; row++) {
                memcpy(data, mask->data.V[row], rowSize);
                data += rowSize;
            }
        }
    }

    stats = psVectorStats(stats, junkData, NULL, junkMask, maskVal);

    psFree(junkMask);
    psFree(junkData);
    return (stats);
}

/*****************************************************************************
NOTE: We assume that the psHistogram structure out has already been allocated
and initialized.
 *****************************************************************************/
psHistogram* psImageHistogram(psHistogram* out,
                              const psImage* in,
                              const psImage* mask,
                              psMaskType maskVal)
{
    PS_ASSERT_PTR_NON_NULL(out, NULL);
    PS_ASSERT_PTR_NON_NULL(in, NULL);
    if (mask != NULL) {
        PS_ASSERT_IMAGE_TYPE(mask, PS_TYPE_U8, NULL);
        PS_ASSERT_IMAGES_SIZE_EQUAL(in, mask, NULL);
    }
    psVector* junkData = NULL;
    psVector* junkMask = NULL;

    if (in->parent == NULL) {
        // stuff the image data into a psVector struct.
        junkData = (psVector *) psAlloc(sizeof(psVector));
        junkData->type = in->type;
        *(int*)&junkData->nalloc = in->numRows * in->numCols;
        junkData->n = junkData->nalloc;
        junkData->data.U8 = in->data.V[0];      // since psImage data is contiguous...
    } else {
        // image not necessarily contiguous
        int numRows = in->numRows;
        int numCols = in->numCols;
        int rowSize = numCols * (PSELEMTYPE_SIZEOF(in->type.type));

        junkData = psVectorAlloc(numRows*numCols, in->type.type);
        junkData->n = junkData->nalloc;

        psU8* data = junkData->data.U8;
        for (int row = 0; row < numRows; row++) {
            memcpy(data, in->data.V[row], rowSize);
            data += rowSize;
        }
    }

    if (mask != NULL) {
        if (mask->parent == NULL) {
            // stuff the mask data into a psVector struct.
            junkMask = psAlloc(sizeof(psVector));
            junkMask->type = mask->type;
            *(int*)&junkMask->nalloc = mask->numRows * mask->numCols;
            junkMask->n = junkMask->nalloc;
            junkMask->data.U8 = mask->data.V[0];
        } else {
            // image not necessarily contiguous
            int numRows = mask->numRows;
            int numCols = mask->numCols;
            int rowSize = numCols * (PSELEMTYPE_SIZEOF(mask->type.type));

            junkMask = psVectorAlloc(numRows*numCols, mask->type.type);
            junkMask->n = junkMask->nalloc;

            psU8* data = junkMask->data.U8;
            for (int row = 0; row < numRows; row++) {
                memcpy(data, mask->data.V[row], rowSize);
                data += rowSize;
            }
        }
    }

    out = psVectorHistogram(out, junkData, NULL, junkMask, maskVal);

    psFree(junkMask);
    psFree(junkData);

    return (out);
}

/*****************************************************************************
calcScaleFactorsEval(n): The Chebyshev polynomials are defined over the
interval [-1.0 : 1.0].  Images typically have sizes of 512x512 or more.  In
order to use Chebyshev polynomials, we must scale the coordinates from
0:512 to -1:1.  This routine takes as input an integer N and produces as
output a vector of evenly spaced floating point values between -1.0:1.0.
 
XXX: Use the p_psNormalizeVector here?
 *****************************************************************************/
double* calcScaleFactors(psS32 n)
{
    PS_ASSERT_INT_NONNEGATIVE(n, NULL);
    psS32 i = 0;
    double tmp = 0.0;
    double *scalingFactors = (double *)psAlloc(n * sizeof(double));

    for (i = 0; i < n; i++) {
        tmp = (double)(n - i);
        tmp = (M_PI * (tmp - 0.5)) / ((double)n);
        scalingFactors[i] = cos(tmp);
    }

    return (scalingFactors);
}

// XXX: Use a static array of Chebyshev polynomials.
psPolynomial1D **p_psCreateChebyshevPolys(psS32 maxChebyPoly)
{
    PS_ASSERT_INT_POSITIVE(maxChebyPoly, NULL);
    psPolynomial1D **chebPolys = NULL;
    psS32 i = 0;
    psS32 j = 0;

    chebPolys = (psPolynomial1D **) psAlloc(maxChebyPoly * sizeof(psPolynomial1D *));
    for (i = 0; i < maxChebyPoly; i++) {
        // XXX: verify this, poly nOrder/nTerms change.
        chebPolys[i] = psPolynomial1DAlloc(i, PS_POLYNOMIAL_ORD);
    }

    // Create the Chebyshev polynomials.
    // Polynomial i has i-th order.
    chebPolys[0]->coeff[0] = 1;
    chebPolys[1]->coeff[1] = 1;
    for (i = 2; i < maxChebyPoly; i++) {
        for (j = 0; j < (1 + chebPolys[i - 1]->nX); j++) {
            chebPolys[i]->coeff[j + 1] = 2 * chebPolys[i - 1]->coeff[j];
        }
        for (j = 0; j < (1 + chebPolys[i - 2]->nX); j++) {
            chebPolys[i]->coeff[j] -= chebPolys[i - 2]->coeff[j];
        }
    }

    return (chebPolys);
}

/*****************************************************************************
psImageFitPolynomial(): This routine takes as input a 2-D image and produces
as output the coefficients of the Chebyshev polynomials which match that
input image.
  Input:
  Output:
  Internal Data Structures:
    chebPolys[i][j]
    sums[i][j]: This will contain the sum of
                input->data.F32[x][y] *
                psPolynomial1DEval(
chebPolys[i],
(float) x) *
                psPolynomial1DEval(
chebPolys[j],
(float) y,
);
        over all pixels (x,y) in the image.
  *****************************************************************************/
psPolynomial2D* psImageFitPolynomial(psPolynomial2D* coeffs,
                                     const psImage* input)
{
    PS_ASSERT_IMAGE_NON_NULL(input, NULL);
    PS_ASSERT_IMAGE_NON_EMPTY(input, NULL);
    if ((input->type.type != PS_TYPE_S8) &&
            (input->type.type != PS_TYPE_U16) &&
            (input->type.type != PS_TYPE_F32) &&
            (input->type.type != PS_TYPE_F64)) {
        psError(PS_ERR_BAD_PARAMETER_TYPE, true, "Unallowable image type.\n");
    }
    PS_ASSERT_POLY_NON_NULL(coeffs, NULL);
    PS_ASSERT_POLY_TYPE(coeffs, PS_POLYNOMIAL_CHEB, NULL);
    psS32 x = 0;
    psS32 y = 0;
    psS32 i = 0;
    psS32 j = 0;
    double **sums = NULL;
    psPolynomial1D* *chebPolys = NULL;
    psS32 maxChebyPoly = 0;
    double *cScalingFactors = NULL;
    double *rScalingFactors = NULL;

    // Create the sums[][] data structure.  This
    // will hold the LHS of
    // equation
    // 29 in the ADD: sums[k][l] = SUM {
    // image(x,y) * Tk(x) * Tl(y) }
    sums = (double **)psAlloc((1 + coeffs->nX) * sizeof(double *));
    for (i = 0; i < (1 + coeffs->nX); i++) {
        sums[i] = (double *)psAlloc((1 + coeffs->nY) * sizeof(double));
    }
    // We scale the pixel positions to values
    // between -1.0 and 1.0
    rScalingFactors = calcScaleFactors(input->numRows);
    cScalingFactors = calcScaleFactors(input->numCols);

    // Determine how many Chebyshev polynomials
    // are needed, then create them.
    // XXX: recorde or verify the poly order/nterm change
    maxChebyPoly = coeffs->nX;
    if (coeffs->nY > coeffs->nX) {
        maxChebyPoly = coeffs->nY;
    }
    chebPolys = p_psCreateChebyshevPolys(maxChebyPoly + 1);

    // Compute the sums[][] data structure.
    for (i = 0; i < (1 + coeffs->nX); i++) {
        for (j = 0; j < (1 + coeffs->nY); j++) {
            sums[i][j] = 0.0;
            for (x = 0; x < input->numRows; x++) {
                for (y = 0; y < input->numCols; y++) {
                    double pixel = 0.0;
                    if (input->type.type == PS_TYPE_S8) {
                        pixel = (double) input->data.S8[x][y];
                    } else if (input->type.type == PS_TYPE_U16) {
                        pixel = (double) input->data.U16[x][y];
                    } else if (input->type.type == PS_TYPE_F32) {
                        pixel = (double) input->data.F32[x][y];
                    } else if (input->type.type == PS_TYPE_F64) {
                        pixel = input->data.F64[x][y];
                    }
                    sums[i][j] += pixel * psPolynomial1DEval(chebPolys[i],rScalingFactors[x]) *
                                  psPolynomial1DEval(chebPolys[j], cScalingFactors[y]);
                }
            }
        }
    }

    for (i = 0; i < (1 + coeffs->nX); i++) {
        for (j = 0; j < (1 + coeffs->nY); j++) {
            coeffs->coeff[i][j] = sums[i][j];
            coeffs->coeff[i][j] /= (double)(input->numRows * input->numCols);

            if ((i != 0) && (j != 0)) {
                coeffs->coeff[i][j] *= 4.0;
            } else if ((i == 0) && (j == 0)) {
                coeffs->coeff[i][j] *= 1.0;
            } else {
                coeffs->coeff[i][j] *= 2.0;
            }
        }
    }

    // Free the Chebyshev polynomials that were
    // created in this routine.
    for (i = 0; i < maxChebyPoly + 1; i++) {
        psFree(chebPolys[i]);
    }
    psFree(chebPolys);

    // Free some data
    for (i = 0; i < (1 + coeffs->nX); i++) {
        psFree(sums[i]);
    }
    psFree(sums);
    psFree(cScalingFactors);
    psFree(rScalingFactors);

    return (coeffs);
}




/*****************************************************************************
psImageFitPolynomial(): This routine takes as input a 2-D image and produces
as output the coefficients of the Chebyshev polynomials which match that input
image.  This is a TEST version of the code.  It is not used by anything.
  Input:
  Output:
  Internal Data Structures:
    chebPolys[i][j]
    sums[i][j]: This will contain the sum of
                input->data.F32[x][y] *
                psPolynomial1DEval(
chebPolys[i],
(float) x) *
                psPolynomial1DEval(
chebPolys[j],
(float) y,
);
        over all pixels (x,y) in the image.
  *****************************************************************************/
psPolynomial2D* psImageFitPolynomialTest(psPolynomial2D* coeffs,
        const psImage* input)
{
    PS_ASSERT_IMAGE_NON_NULL(input, NULL);
    PS_ASSERT_IMAGE_NON_EMPTY(input, NULL);
    if ((input->type.type != PS_TYPE_S8) &&
            (input->type.type != PS_TYPE_U16) &&
            (input->type.type != PS_TYPE_F32) &&
            (input->type.type != PS_TYPE_F64)) {
        psError(PS_ERR_BAD_PARAMETER_TYPE, true, "Unallowable image type.\n");
    }
    PS_ASSERT_POLY_NON_NULL(coeffs, NULL);
    PS_ASSERT_POLY_TYPE(coeffs, PS_POLYNOMIAL_CHEB, NULL);
    psS32 x = 0;
    psS32 y = 0;
    psS32 i = 0;
    psS32 j = 0;
    double **sums = NULL;
    psPolynomial1D* *chebPolys = NULL;
    psS32 maxChebyPoly = 0;
    double *cScalingFactors = NULL;
    double *rScalingFactors = NULL;
    psImage *nodes = psImageAlloc(input->numCols, input->numRows, PS_TYPE_F64);

    double min = -1.0;
    double max = 1.0;
    double bma = 0.5 * (max-min);  // 1
    double bpa = 0.5 * (max+min);  // 0
    // We must calculate the value of the image at the nodes where the
    // Chebyshev polynomials are 0.
    for (x = 0; x < input->numRows; x++) {
        double xTmp = cos(M_PI * (0.5 + ((float) x)) / ((float) input->numRows));
        double xNode = - ((xTmp + bma + bpa) - 1.0);
        double xOrig = ((float) input->numRows) * (xNode - min) / (max - min);

        for (y = 0; y < input->numCols; y++) {
            double yTmp = cos(M_PI * (0.5 + ((float) y)) / ((float) input->numCols));
            double yNode = - ((yTmp + bma + bpa) - 1.0);
            double yOrig = ((float) input->numCols) * (yNode - min) / (max - min);

            //            nodes->data.F64[x][y] = psImagePixelInterpolate(input, yNode, xNode, NULL, 0, 0.0, PS_INTERPOLATE_BILINEAR);
            //            nodes->data.F64[x][y] = psImagePixelInterpolate(input, yTmp, xTmp, NULL, 0, 0.0, PS_INTERPOLATE_BILINEAR);
            nodes->data.F64[x][y] = psImagePixelInterpolate(input, yOrig, xOrig, NULL, 0, 0.0, PS_INTERPOLATE_BILINEAR);
        }
    }

    // Create the sums[][] data structure.  This
    // will hold the LHS of
    // equation
    // 29 in the ADD: sums[k][l] = SUM {
    // image(x,y) * Tk(x) * Tl(y) }
    sums = (double **)psAlloc((1 + coeffs->nX) * sizeof(double *));
    for (i = 0; i < (1 + coeffs->nX); i++) {
        sums[i] = (double *)psAlloc((1 + coeffs->nY) * sizeof(double));
    }
    // We scale the pixel positions to values
    // between -1.0 and 1.0
    rScalingFactors = calcScaleFactors(input->numRows);
    cScalingFactors = calcScaleFactors(input->numCols);

    // Determine how many Chebyshev polynomials
    // are needed, then create them.
    maxChebyPoly = coeffs->nX;
    if (coeffs->nY > coeffs->nX) {
        maxChebyPoly = coeffs->nY;
    }
    chebPolys = p_psCreateChebyshevPolys(maxChebyPoly + 1);

    // Compute the sums[][] data structure.
    for (i = 0; i < (1 + coeffs->nX); i++) {
        for (j = 0; j < (1 + coeffs->nY); j++) {
            sums[i][j] = 0.0;
            for (x = 0; x < input->numRows; x++) {
                for (y = 0; y < input->numCols; y++) {
                    double pixel;
                    /*
                                        if (input->type.type == PS_TYPE_S8) {
                                            pixel = (double) input->data.S8[x][y];
                                        } else if (input->type.type == PS_TYPE_U16) {
                                            pixel = (double) input->data.U16[x][y];
                                        } else if (input->type.type == PS_TYPE_F32) {
                                            pixel = (double) input->data.F32[x][y];
                                        } else if (input->type.type == PS_TYPE_F64) {
                                            pixel = input->data.F64[x][y];
                                        }
                    */
                    pixel = nodes->data.F64[x][y];
                    sums[i][j] += pixel * psPolynomial1DEval(chebPolys[i], rScalingFactors[x]) *
                                  psPolynomial1DEval(chebPolys[j], cScalingFactors[y]);
                }
            }
        }
    }

    for (i = 0; i < (1 + coeffs->nX); i++) {
        for (j = 0; j < (1 + coeffs->nY); j++) {
            coeffs->coeff[i][j] = sums[i][j];
            coeffs->coeff[i][j] /= (double)(input->numRows * input->numCols);

            if ((i != 0) && (j != 0)) {
                coeffs->coeff[i][j] *= 4.0;
            } else if ((i == 0) && (j == 0)) {
                coeffs->coeff[i][j] *= 1.0;
            } else {
                coeffs->coeff[i][j] *= 2.0;
            }
        }
    }

    // Free the Chebyshev polynomials that were
    // created in this routine.
    for (i = 0; i < maxChebyPoly + 1; i++) {
        psFree(chebPolys[i]);
    }
    psFree(chebPolys);

    // Free some data
    for (i = 0; i < (1 + coeffs->nX); i++) {
        psFree(sums[i]);
    }
    psFree(sums);
    psFree(cScalingFactors);
    psFree(rScalingFactors);
    psFree(nodes);

    return (coeffs);
}

/*****************************************************************************
XXX: Use static variables for Chebyshev polynomials and scaling factors.
 *****************************************************************************/
psImage* p_psImageEvalPolynomialCheb(psImage* input,
                                     const psPolynomial2D* coeffs)
{
    PS_ASSERT_POLY_TYPE(coeffs, PS_POLYNOMIAL_CHEB, NULL);

    psS32 x = 0;
    psS32 y = 0;
    psS32 i = 0;
    psS32 j = 0;
    psPolynomial1D* *chebPolys = NULL;
    psS32 maxChebyPoly = 0;
    double *cScalingFactors = NULL;
    double *rScalingFactors = NULL;
    float polySum = 0.0;

    // We scale the pixel positions to values between -1.0 and 1.0
    // Use static data structures here.
    rScalingFactors = calcScaleFactors(input->numRows);
    cScalingFactors = calcScaleFactors(input->numCols);

    // Determine how many Chebyshev polynomials
    // are needed, then create them.
    maxChebyPoly = coeffs->nX;
    if (coeffs->nY > coeffs->nX) {
        maxChebyPoly = coeffs->nY;
    }

    chebPolys = p_psCreateChebyshevPolys(maxChebyPoly + 1);

    for (x = 0; x < input->numRows; x++) {
        for (y = 0; y < input->numCols; y++) {
            polySum = 0.0;
            for (i = 0; i < (1 + coeffs->nX); i++) {
                for (j = 0; j < (1 + coeffs->nY); j++) {
                    polySum +=
                        psPolynomial1DEval(chebPolys[i], rScalingFactors[x]) *
                        psPolynomial1DEval(chebPolys[j], cScalingFactors[y]) *
                        coeffs->coeff[i][j];
                }
            }

            if (input->type.type == PS_TYPE_S8) {
                input->data.S8[x][y] = (char) polySum;
            } else if (input->type.type == PS_TYPE_U16) {
                input->data.U16[x][y] = (short int) polySum;
            } else if (input->type.type == PS_TYPE_F32) {
                input->data.F32[x][y] = (float) polySum;
            } else if (input->type.type == PS_TYPE_F64) {
                input->data.F64[x][y] = polySum;
            }
        }
    }

    // Free the Chebyshev polynomials that were
    // created in this routine.
    // XXX: Use static data structures here.
    for (i = 0; i < maxChebyPoly + 1; i++) {
        psFree(chebPolys[i]);
    }
    psFree(chebPolys);

    psFree(cScalingFactors);
    psFree(rScalingFactors);

    return input;
}

psImage* p_psImageEvalPolynomialOrd(psImage* input,
                                    const psPolynomial2D* coeffs)
{
    PS_ASSERT_POLY_TYPE(coeffs, PS_POLYNOMIAL_ORD, NULL);

    for (int row = 0; row < input->numRows ; row++) {
        for (int col = 0; col < input->numCols ; col++) {
            if (input->type.type == PS_TYPE_S8) {
                input->data.S8[row][col] = (psS8) psPolynomial2DEval(coeffs, (psF32) row, (psF32) col);
            } else if (input->type.type == PS_TYPE_U16) {
                input->data.U16[row][col] = (psS16) psPolynomial2DEval(coeffs, (psF32) row, (psF32) col);
            } else if (input->type.type == PS_TYPE_F32) {
                input->data.F32[row][col] = psPolynomial2DEval(coeffs, (psF32) row, (psF32) col);
            } else if (input->type.type == PS_TYPE_F64) {
                input->data.F64[row][col] = (psF64) psPolynomial2DEval(coeffs, (psF32) row, (psF32) col);
            }
        }
    }

    return(input);
}


/*****************************************************************************
XXX: I added normal polynomials to this routine.  Let IfA know, put it in the
psLib SDR.
 *****************************************************************************/
psImage* psImageEvalPolynomial(psImage* input,
                               const psPolynomial2D* coeffs)
{
    PS_ASSERT_IMAGE_NON_NULL(input, NULL);
    PS_ASSERT_IMAGE_NON_EMPTY(input, NULL);
    if ((input->type.type != PS_TYPE_S8) &&
            (input->type.type != PS_TYPE_U16) &&
            (input->type.type != PS_TYPE_F32) &&
            (input->type.type != PS_TYPE_F64)) {
        psError(PS_ERR_BAD_PARAMETER_TYPE, true,
                "Unallowable image type.\n");
    }
    PS_ASSERT_POLY_NON_NULL(coeffs, NULL);

    if (coeffs->type == PS_POLYNOMIAL_ORD) {
        return(p_psImageEvalPolynomialOrd(input, coeffs));
    } else if (coeffs->type == PS_POLYNOMIAL_CHEB) {
        return(p_psImageEvalPolynomialCheb(input, coeffs));
    }
    printf("XXX: Error: wrong polynomial type\n");
    // XXX: psError()
    return(NULL);
}

// count number of pixels with given mask value
long psImageCountPixelMask (psImage *mask,
                            psRegion region,
                            psMaskType value)
{
    long Npixels = 0;
    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;
    if (type != PS_TYPE_MASK) {
        psError(PS_ERR_BAD_PARAMETER_TYPE, true,
                "psImage type does not match the specified psMaskType!\n");
        return -1;
    }

    switch (type) {
    case PS_TYPE_U8:
    case PS_TYPE_U16:
        for (long i = x0; i < x1; i++) {
            for (long j = y0; j < y1; j++) {
                if (mask->data.PS_TYPE_MASK_DATA[i][j] & value) {
                    Npixels ++;
                }
            }
        }
        break;
    case PS_TYPE_S8:
    case PS_TYPE_S16:
    case PS_TYPE_S32:
    case PS_TYPE_S64:
    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);


}

