src/quadtree.c
l_int32
pixQuadtreeVariance(PIX     *pixs,
                    l_int32  nlevels,
                    PIX     *pix_ma,
                    DPIX    *dpix_msa,
                    FPIXA  **pfpixa_v,
                    FPIXA  **pfpixa_rv)
{
l_int32    i, j, w, h, size, n;
l_float32  var, rvar;
BOX       *box;
BOXA      *boxa;
BOXAA     *baa;
FPIX      *fpixv, *fpixrv;
PIX       *pix_mac;  /* copy of mean accumulator */
DPIX      *dpix_msac;  /* msa clone */

    PROCNAME("pixQuadtreeVariance");

    if (!pfpixa_v && !pfpixa_rv)
        return ERROR_INT("neither &fpixav nor &fpixarv defined", procName, 1);
    if (pfpixa_v) *pfpixa_v = NULL;
    if (pfpixa_rv) *pfpixa_rv = NULL;
    if (!pixs || pixGetDepth(pixs) != 8)
        return ERROR_INT("pixs not defined or not 8 bpp", procName, 1);
    pixGetDimensions(pixs, &w, &h, NULL);
    if (nlevels > quadtreeMaxLevels(w, h))
        return ERROR_INT("nlevels too large for image", procName, 1);

    if (!pix_ma)
        pix_mac = pixBlockconvAccum(pixs);
    else
        pix_mac = pixClone(pix_ma);
    if (!pix_mac)
        return ERROR_INT("pix_mac not made", procName, 1);
    if (!dpix_msa)
        dpix_msac = pixMeanSquareAccum(pixs);
    else
        dpix_msac = dpixClone(dpix_msa);
    if (!dpix_msac)
        return ERROR_INT("dpix_msac not made", procName, 1);

    if ((baa = boxaaQuadtreeRegions(w, h, nlevels)) == NULL) {
        pixDestroy(&pix_mac);
        dpixDestroy(&dpix_msac);
        return ERROR_INT("baa not made", procName, 1);
    }

    if (pfpixa_v) *pfpixa_v = fpixaCreate(nlevels);
    if (pfpixa_rv) *pfpixa_rv = fpixaCreate(nlevels);
    for (i = 0; i < nlevels; i++) {
        boxa = boxaaGetBoxa(baa, i, L_CLONE);
        size = 1 << i;
        n = boxaGetCount(boxa);  /* n == size * size */
        if (pfpixa_v) fpixv = fpixCreate(size, size);
        if (pfpixa_rv) fpixrv = fpixCreate(size, size);
        for (j = 0; j < n; j++) {
            box = boxaGetBox(boxa, j, L_CLONE);
            pixVarianceInRectangle(pixs, box, pix_mac, dpix_msac, &var, &rvar);
            if (pfpixa_v) fpixSetPixel(fpixv, j % size, j / size, var);
            if (pfpixa_rv) fpixSetPixel(fpixrv, j % size, j / size, rvar);
            boxDestroy(&box);
        }
        if (pfpixa_v) fpixaAddFPix(*pfpixa_v, fpixv, L_INSERT);
        if (pfpixa_rv) fpixaAddFPix(*pfpixa_rv, fpixrv, L_INSERT);
        boxaDestroy(&boxa);
    }

    pixDestroy(&pix_mac);
    dpixDestroy(&dpix_msac);
    boxaaDestroy(&baa);
    return 0;
}
/*!
 *  pixQuadtreeVariance()
 *
 *      Input:  pixs (8 bpp, no colormap)
 *              nlevels (in quadtree)
 *             *pix_ma (input mean accumulator; can be null)
 *             *dpix_msa (input mean square accumulator; can be null)
 *             *pfpixa_v (<optional return> variance values in quadtree)
 *             *pfpixa_rv (<optional return> root variance values in quadtree)
 *      Return: 0 if OK, 1 on error
 *
 *  Notes:
 *      (1) The returned fpixav and fpixarv have @nlevels of fpix,
 *          each containing at the respective levels the variance
 *          and root variance values.
 */


(1) The returned fpixav and fpixarv have @nlevels of fpix,
each containing at the respective levels the variance
and root variance values.
( 1 ) The returned fpixav and fpixarv have @nlevels of fpix each containing at the respective levels the variance and root variance values .
l_int32
pixQuadtreeVariance(PIX     *pixs,
                    l_int32  nlevels,
                    PIX     *pix_ma,
                    DPIX    *dpix_msa,
                    FPIXA  **pfpixa_v,
                    FPIXA  **pfpixa_rv)
{
l_int32    i, j, w, h, size, n;
l_float32  var, rvar;
BOX       *box;
BOXA      *boxa;
BOXAA     *baa;
FPIX      *fpixv, *fpixrv;
PIX       *pix_mac;  
DPIX      *dpix_msac;  
    PROCNAME("pixQuadtreeVariance");
    if (!pfpixa_v && !pfpixa_rv)
        return ERROR_INT("neither &fpixav nor &fpixarv defined", procName, 1);
    if (pfpixa_v) *pfpixa_v = NULL;
    if (pfpixa_rv) *pfpixa_rv = NULL;
    if (!pixs || pixGetDepth(pixs) != 8)
        return ERROR_INT("pixs not defined or not 8 bpp", procName, 1);
    pixGetDimensions(pixs, &w, &h, NULL);
    if (nlevels > quadtreeMaxLevels(w, h))
        return ERROR_INT("nlevels too large for image", procName, 1);
    if (!pix_ma)
        pix_mac = pixBlockconvAccum(pixs);
    else
        pix_mac = pixClone(pix_ma);
    if (!pix_mac)
        return ERROR_INT("pix_mac not made", procName, 1);
    if (!dpix_msa)
        dpix_msac = pixMeanSquareAccum(pixs);
    else
        dpix_msac = dpixClone(dpix_msa);
    if (!dpix_msac)
        return ERROR_INT("dpix_msac not made", procName, 1);
    if ((baa = boxaaQuadtreeRegions(w, h, nlevels)) == NULL) {
        pixDestroy(&pix_mac);
        dpixDestroy(&dpix_msac);
        return ERROR_INT("baa not made", procName, 1);
    }
    if (pfpixa_v) *pfpixa_v = fpixaCreate(nlevels);
    if (pfpixa_rv) *pfpixa_rv = fpixaCreate(nlevels);
    for (i = 0; i < nlevels; i++) {
        boxa = boxaaGetBoxa(baa, i, L_CLONE);
        size = 1 << i;
        n = boxaGetCount(boxa);  
        if (pfpixa_v) fpixv = fpixCreate(size, size);
        if (pfpixa_rv) fpixrv = fpixCreate(size, size);
        for (j = 0; j < n; j++) {
            box = boxaGetBox(boxa, j, L_CLONE);
            pixVarianceInRectangle(pixs, box, pix_mac, dpix_msac, &var, &rvar);
            if (pfpixa_v) fpixSetPixel(fpixv, j % size, j / size, var);
            if (pfpixa_rv) fpixSetPixel(fpixrv, j % size, j / size, rvar);
            boxDestroy(&box);
        }
        if (pfpixa_v) fpixaAddFPix(*pfpixa_v, fpixv, L_INSERT);
        if (pfpixa_rv) fpixaAddFPix(*pfpixa_rv, fpixrv, L_INSERT);
        boxaDestroy(&boxa);
    }
    pixDestroy(&pix_mac);
    dpixDestroy(&dpix_msac);
    boxaaDestroy(&baa);
    return 0;
}
l_int32 pixQuadtreeVariance ( PIX * pixs , l_int32 nlevels , PIX * pix_ma , DPIX * dpix_msa , FPIXA * * pfpixa_v , FPIXA * * pfpixa_rv ) { l_int32 i , j , w , h , size , n ; l_float32 var , rvar ; BOX * box ; BOXA * boxa ; BOXAA * baa ; FPIX * fpixv , * fpixrv ; PIX * pix_mac ; DPIX * dpix_msac ; PROCNAME ( " " ) ; if ( ! pfpixa_v && ! pfpixa_rv ) return ERROR_INT ( " " , procName , 1 ) ; if ( pfpixa_v ) * pfpixa_v = NULL ; if ( pfpixa_rv ) * pfpixa_rv = NULL ; if ( ! pixs || pixGetDepth ( pixs ) != 8 ) return ERROR_INT ( " " , procName , 1 ) ; pixGetDimensions ( pixs , & w , & h , NULL ) ; if ( nlevels > quadtreeMaxLevels ( w , h ) ) return ERROR_INT ( " " , procName , 1 ) ; if ( ! pix_ma ) pix_mac = pixBlockconvAccum ( pixs ) ; else pix_mac = pixClone ( pix_ma ) ; if ( ! pix_mac ) return ERROR_INT ( " " , procName , 1 ) ; if ( ! dpix_msa ) dpix_msac = pixMeanSquareAccum ( pixs ) ; else dpix_msac = dpixClone ( dpix_msa ) ; if ( ! dpix_msac ) return ERROR_INT ( " " , procName , 1 ) ; if ( ( baa = boxaaQuadtreeRegions ( w , h , nlevels ) ) == NULL ) { pixDestroy ( & pix_mac ) ; dpixDestroy ( & dpix_msac ) ; return ERROR_INT ( " " , procName , 1 ) ; } if ( pfpixa_v ) * pfpixa_v = fpixaCreate ( nlevels ) ; if ( pfpixa_rv ) * pfpixa_rv = fpixaCreate ( nlevels ) ; for ( i = 0 ; i < nlevels ; i ++ ) { boxa = boxaaGetBoxa ( baa , i , L_CLONE ) ; size = 1 << i ; n = boxaGetCount ( boxa ) ; if ( pfpixa_v ) fpixv = fpixCreate ( size , size ) ; if ( pfpixa_rv ) fpixrv = fpixCreate ( size , size ) ; for ( j = 0 ; j < n ; j ++ ) { box = boxaGetBox ( boxa , j , L_CLONE ) ; pixVarianceInRectangle ( pixs , box , pix_mac , dpix_msac , & var , & rvar ) ; if ( pfpixa_v ) fpixSetPixel ( fpixv , j % size , j / size , var ) ; if ( pfpixa_rv ) fpixSetPixel ( fpixrv , j % size , j / size , rvar ) ; boxDestroy ( & box ) ; } if ( pfpixa_v ) fpixaAddFPix ( * pfpixa_v , fpixv , L_INSERT ) ; if ( pfpixa_rv ) fpixaAddFPix ( * pfpixa_rv , fpixrv , L_INSERT ) ; boxaDestroy ( & boxa ) ; } pixDestroy ( & pix_mac ) ; dpixDestroy ( & dpix_msac ) ; boxaaDestroy ( & baa ) ; return 0 ; }
pixQuadtreeVariance()
      Input:  pixs (8 bpp, no colormap)
              nlevels (in quadtree)
             *pix_ma (input mean accumulator; can be null)
             *dpix_msa (input mean square accumulator; can be null)
             *pfpixa_v (<optional return> variance values in quadtree)
             *pfpixa_rv (<optional return> root variance values in quadtree)
      Return: 0 if OK, 1 on error
pixQuadtreeVariance () Input : pixs ( 8 bpp no colormap ) nlevels ( in quadtree ) * pix_ma ( input mean accumulator ; can be null ) * dpix_msa ( input mean square accumulator ; can be null ) * pfpixa_v ( <optional return > variance values in quadtree ) * pfpixa_rv ( <optional return > root variance values in quadtree ) Return : 0 if OK 1 on error
/* copy of mean accumulator */ /* msa clone */ /* n == size * size */