src/quadtree.c
l_int32
pixVarianceInRectangle(PIX        *pixs,
                       BOX        *box,
                       PIX        *pix_ma,
                       DPIX       *dpix_msa,
                       l_float32  *pvar,
                       l_float32  *prvar)
{
l_int32    w, h, bx, by, bw, bh;
l_uint32   val00, val01, val10, val11;
l_float64  dval00, dval01, dval10, dval11, mval, msval, var, norm;
BOX       *boxc;

    PROCNAME("pixVarianceInRectangle");

    if (!pvar && !prvar)
        return ERROR_INT("neither &var nor &rvar defined", procName, 1);
    if (pvar) *pvar = 0.0;
    if (prvar) *prvar = 0.0;
    if (!pixs || pixGetDepth(pixs) != 8)
        return ERROR_INT("pixs not defined", procName, 1);
    if (!box)
        return ERROR_INT("box not defined", procName, 1);
    if (!pix_ma)
        return ERROR_INT("pix_ma not defined", procName, 1);
    if (!dpix_msa)
        return ERROR_INT("dpix_msa not defined", procName, 1);

        /* Clip rectangle to image */
    pixGetDimensions(pixs, &w, &h, NULL);
    boxc = boxClipToRectangle(box, w, h);
    boxGetGeometry(boxc, &bx, &by, &bw, &bh);
    boxDestroy(&boxc);

    if (bw == 0 || bh == 0)
        return ERROR_INT("no pixels in box", procName, 1);

        /* Use up to 4 points in the accumulators */
    norm = 1.0 / (bw * bh);
    if (bx > 0 && by > 0) {
        pixGetPixel(pix_ma, bx + bw - 1, by + bh - 1, &val11);
        pixGetPixel(pix_ma, bx + bw - 1, by - 1, &val10);
        pixGetPixel(pix_ma, bx - 1, by + bh - 1, &val01);
        pixGetPixel(pix_ma, bx - 1, by - 1, &val00);
        dpixGetPixel(dpix_msa, bx + bw - 1, by + bh - 1, &dval11);
        dpixGetPixel(dpix_msa, bx + bw - 1, by - 1, &dval10);
        dpixGetPixel(dpix_msa, bx - 1, by + bh - 1, &dval01);
        dpixGetPixel(dpix_msa, bx - 1, by - 1, &dval00);
        mval = norm * (val11 - val01 + val00 - val10);
        msval = norm * (dval11 - dval01 + dval00 - dval10);
        var = (msval - mval * mval);
        if (pvar) *pvar = (l_float32)var;
        if (prvar) *prvar = (l_float32)(sqrt(var));
    }
    else if (by > 0) {  /* bx == 0 */
        pixGetPixel(pix_ma, bw - 1, by + bh - 1, &val11);
        pixGetPixel(pix_ma, bw - 1, by - 1, &val10);
        dpixGetPixel(dpix_msa, bw - 1, by + bh - 1, &dval11);
        dpixGetPixel(dpix_msa, bw - 1, by - 1, &dval10);
        mval = norm * (val11 - val10);
        msval = norm * (dval11 - dval10);
        var = (msval - mval * mval);
        if (pvar) *pvar = (l_float32)var;
        if (prvar) *prvar = (l_float32)(sqrt(var));
    }
    else if (bx > 0) {  /* by == 0 */
        pixGetPixel(pix_ma, bx + bw - 1, bh - 1, &val11);
        pixGetPixel(pix_ma, bx - 1, bh - 1, &val01);
        dpixGetPixel(dpix_msa, bx + bw - 1, bh - 1, &dval11);
        dpixGetPixel(dpix_msa, bx - 1, bh - 1, &dval01);
        mval = norm * (val11 - val01);
        msval = norm * (dval11 - dval01);
        var = (msval - mval * mval);
        if (pvar) *pvar = (l_float32)var;
        if (prvar) *prvar = (l_float32)(sqrt(var));
    }
    else {  /* bx == 0 && by == 0 */
        pixGetPixel(pix_ma, bw - 1, bh - 1, &val11);
        dpixGetPixel(dpix_msa, bw - 1, bh - 1, &dval11);
        mval = norm * val11;
        msval = norm * dval11;
        var = (msval - mval * mval);
        if (pvar) *pvar = (l_float32)var;
        if (prvar) *prvar = (l_float32)(sqrt(var));
    }

    return 0;
}
/*!
 *  pixVarianceInRectangle()
 *
 *      Input:  pix (8 bpp)
 *              box (region to compute variance and/or root variance)
 *              pix_ma (mean accumulator)
 *              dpix_msa (mean square accumulator)
 *              &var (<optional return> variance)
 *              &rvar (<optional return> root variance)
 *      Return: 0 if OK, 1 on error
 *
 *  Notes:
 *      (1) This function is intended to be used for many rectangles
 *          on the same image.  It can find the variance and/or the
 *          square root of the variance within a rectangle in O(1),
 *          independent of the size of the rectangle.
 */


(1) This function is intended to be used for many rectangles
on the same image.  It can find the variance and/or the
square root of the variance within a rectangle in O(1),
independent of the size of the rectangle.
( 1 ) This function is intended to be used for many rectangles on the same image . It can find the variance and / or the square root of the variance within a rectangle in O ( 1 ) independent of the size of the rectangle .
l_int32
pixVarianceInRectangle(PIX        *pixs,
                       BOX        *box,
                       PIX        *pix_ma,
                       DPIX       *dpix_msa,
                       l_float32  *pvar,
                       l_float32  *prvar)
{
l_int32    w, h, bx, by, bw, bh;
l_uint32   val00, val01, val10, val11;
l_float64  dval00, dval01, dval10, dval11, mval, msval, var, norm;
BOX       *boxc;
    PROCNAME("pixVarianceInRectangle");
    if (!pvar && !prvar)
        return ERROR_INT("neither &var nor &rvar defined", procName, 1);
    if (pvar) *pvar = 0.0;
    if (prvar) *prvar = 0.0;
    if (!pixs || pixGetDepth(pixs) != 8)
        return ERROR_INT("pixs not defined", procName, 1);
    if (!box)
        return ERROR_INT("box not defined", procName, 1);
    if (!pix_ma)
        return ERROR_INT("pix_ma not defined", procName, 1);
    if (!dpix_msa)
        return ERROR_INT("dpix_msa not defined", procName, 1);
    pixGetDimensions(pixs, &w, &h, NULL);
    boxc = boxClipToRectangle(box, w, h);
    boxGetGeometry(boxc, &bx, &by, &bw, &bh);
    boxDestroy(&boxc);
    if (bw == 0 || bh == 0)
        return ERROR_INT("no pixels in box", procName, 1);
    norm = 1.0 / (bw * bh);
    if (bx > 0 && by > 0) {
        pixGetPixel(pix_ma, bx + bw - 1, by + bh - 1, &val11);
        pixGetPixel(pix_ma, bx + bw - 1, by - 1, &val10);
        pixGetPixel(pix_ma, bx - 1, by + bh - 1, &val01);
        pixGetPixel(pix_ma, bx - 1, by - 1, &val00);
        dpixGetPixel(dpix_msa, bx + bw - 1, by + bh - 1, &dval11);
        dpixGetPixel(dpix_msa, bx + bw - 1, by - 1, &dval10);
        dpixGetPixel(dpix_msa, bx - 1, by + bh - 1, &dval01);
        dpixGetPixel(dpix_msa, bx - 1, by - 1, &dval00);
        mval = norm * (val11 - val01 + val00 - val10);
        msval = norm * (dval11 - dval01 + dval00 - dval10);
        var = (msval - mval * mval);
        if (pvar) *pvar = (l_float32)var;
        if (prvar) *prvar = (l_float32)(sqrt(var));
    }
    else if (by > 0) {  
        pixGetPixel(pix_ma, bw - 1, by + bh - 1, &val11);
        pixGetPixel(pix_ma, bw - 1, by - 1, &val10);
        dpixGetPixel(dpix_msa, bw - 1, by + bh - 1, &dval11);
        dpixGetPixel(dpix_msa, bw - 1, by - 1, &dval10);
        mval = norm * (val11 - val10);
        msval = norm * (dval11 - dval10);
        var = (msval - mval * mval);
        if (pvar) *pvar = (l_float32)var;
        if (prvar) *prvar = (l_float32)(sqrt(var));
    }
    else if (bx > 0) {  
        pixGetPixel(pix_ma, bx + bw - 1, bh - 1, &val11);
        pixGetPixel(pix_ma, bx - 1, bh - 1, &val01);
        dpixGetPixel(dpix_msa, bx + bw - 1, bh - 1, &dval11);
        dpixGetPixel(dpix_msa, bx - 1, bh - 1, &dval01);
        mval = norm * (val11 - val01);
        msval = norm * (dval11 - dval01);
        var = (msval - mval * mval);
        if (pvar) *pvar = (l_float32)var;
        if (prvar) *prvar = (l_float32)(sqrt(var));
    }
    else {  
        pixGetPixel(pix_ma, bw - 1, bh - 1, &val11);
        dpixGetPixel(dpix_msa, bw - 1, bh - 1, &dval11);
        mval = norm * val11;
        msval = norm * dval11;
        var = (msval - mval * mval);
        if (pvar) *pvar = (l_float32)var;
        if (prvar) *prvar = (l_float32)(sqrt(var));
    }
    return 0;
}
l_int32 pixVarianceInRectangle ( PIX * pixs , BOX * box , PIX * pix_ma , DPIX * dpix_msa , l_float32 * pvar , l_float32 * prvar ) { l_int32 w , h , bx , by , bw , bh ; l_uint32 val00 , val01 , val10 , val11 ; l_float64 dval00 , dval01 , dval10 , dval11 , mval , msval , var , norm ; BOX * boxc ; PROCNAME ( " " ) ; if ( ! pvar && ! prvar ) return ERROR_INT ( " " , procName , 1 ) ; if ( pvar ) * pvar = 0.0 ; if ( prvar ) * prvar = 0.0 ; if ( ! pixs || pixGetDepth ( pixs ) != 8 ) return ERROR_INT ( " " , procName , 1 ) ; if ( ! box ) return ERROR_INT ( " " , procName , 1 ) ; if ( ! pix_ma ) return ERROR_INT ( " " , procName , 1 ) ; if ( ! dpix_msa ) return ERROR_INT ( " " , procName , 1 ) ; pixGetDimensions ( pixs , & w , & h , NULL ) ; boxc = boxClipToRectangle ( box , w , h ) ; boxGetGeometry ( boxc , & bx , & by , & bw , & bh ) ; boxDestroy ( & boxc ) ; if ( bw == 0 || bh == 0 ) return ERROR_INT ( " " , procName , 1 ) ; norm = 1.0 / ( bw * bh ) ; if ( bx > 0 && by > 0 ) { pixGetPixel ( pix_ma , bx + bw - 1 , by + bh - 1 , & val11 ) ; pixGetPixel ( pix_ma , bx + bw - 1 , by - 1 , & val10 ) ; pixGetPixel ( pix_ma , bx - 1 , by + bh - 1 , & val01 ) ; pixGetPixel ( pix_ma , bx - 1 , by - 1 , & val00 ) ; dpixGetPixel ( dpix_msa , bx + bw - 1 , by + bh - 1 , & dval11 ) ; dpixGetPixel ( dpix_msa , bx + bw - 1 , by - 1 , & dval10 ) ; dpixGetPixel ( dpix_msa , bx - 1 , by + bh - 1 , & dval01 ) ; dpixGetPixel ( dpix_msa , bx - 1 , by - 1 , & dval00 ) ; mval = norm * ( val11 - val01 + val00 - val10 ) ; msval = norm * ( dval11 - dval01 + dval00 - dval10 ) ; var = ( msval - mval * mval ) ; if ( pvar ) * pvar = ( l_float32 ) var ; if ( prvar ) * prvar = ( l_float32 ) ( sqrt ( var ) ) ; } else if ( by > 0 ) { pixGetPixel ( pix_ma , bw - 1 , by + bh - 1 , & val11 ) ; pixGetPixel ( pix_ma , bw - 1 , by - 1 , & val10 ) ; dpixGetPixel ( dpix_msa , bw - 1 , by + bh - 1 , & dval11 ) ; dpixGetPixel ( dpix_msa , bw - 1 , by - 1 , & dval10 ) ; mval = norm * ( val11 - val10 ) ; msval = norm * ( dval11 - dval10 ) ; var = ( msval - mval * mval ) ; if ( pvar ) * pvar = ( l_float32 ) var ; if ( prvar ) * prvar = ( l_float32 ) ( sqrt ( var ) ) ; } else if ( bx > 0 ) { pixGetPixel ( pix_ma , bx + bw - 1 , bh - 1 , & val11 ) ; pixGetPixel ( pix_ma , bx - 1 , bh - 1 , & val01 ) ; dpixGetPixel ( dpix_msa , bx + bw - 1 , bh - 1 , & dval11 ) ; dpixGetPixel ( dpix_msa , bx - 1 , bh - 1 , & dval01 ) ; mval = norm * ( val11 - val01 ) ; msval = norm * ( dval11 - dval01 ) ; var = ( msval - mval * mval ) ; if ( pvar ) * pvar = ( l_float32 ) var ; if ( prvar ) * prvar = ( l_float32 ) ( sqrt ( var ) ) ; } else { pixGetPixel ( pix_ma , bw - 1 , bh - 1 , & val11 ) ; dpixGetPixel ( dpix_msa , bw - 1 , bh - 1 , & dval11 ) ; mval = norm * val11 ; msval = norm * dval11 ; var = ( msval - mval * mval ) ; if ( pvar ) * pvar = ( l_float32 ) var ; if ( prvar ) * prvar = ( l_float32 ) ( sqrt ( var ) ) ; } return 0 ; }
pixVarianceInRectangle()
      Input:  pix (8 bpp)
              box (region to compute variance and/or root variance)
              pix_ma (mean accumulator)
              dpix_msa (mean square accumulator)
              &var (<optional return> variance)
              &rvar (<optional return> root variance)
      Return: 0 if OK, 1 on error
pixVarianceInRectangle () Input : pix ( 8 bpp ) box ( region to compute variance and / or root variance ) pix_ma ( mean accumulator ) dpix_msa ( mean square accumulator ) &var ( <optional return > variance ) &rvar ( <optional return > root variance ) Return : 0 if OK 1 on error
/* Clip rectangle to image */ /* Use up to 4 points in the accumulators */ /* bx == 0 */ /* by == 0 */ /* bx == 0 && by == 0 */