src/boxfunc5.c
BOXA *
boxaConstrainSize(BOXA    *boxas,
                  l_int32  width,
                  l_int32  widthflag,
                  l_int32  height,
                  l_int32  heightflag)
{
l_int32  n, i, x, y, w, h, invalid;
l_int32  delw, delh, del_left, del_right, del_top, del_bot;
BOX     *medbox, *boxs, *boxd;
BOXA    *boxad;

    PROCNAME("boxaConstrainSize");

    if (!boxas)
        return (BOXA *)ERROR_PTR("boxas not defined", procName, NULL);

        /* Need median values if requested or if there are invalid boxes */
    invalid = boxaGetCount(boxas) - boxaGetValidCount(boxas);
    medbox = NULL;
    if (width == 0 || height == 0 || invalid > 0) {
        if (boxaGetMedianVals(boxas, &x, &y, NULL, NULL, &w, &h)) {
            L_ERROR("median vals not returned", procName);
            return boxaCopy(boxas, L_COPY);
        }
        medbox = boxCreate(x, y, w, h);
        if (width == 0) width = w;
        if (height == 0) height = h;
    }

    n = boxaGetCount(boxas);
    boxad = boxaCreate(n);
    for (i = 0; i < n; i++) {
        if ((boxs = boxaGetValidBox(boxas, i, L_COPY)) == NULL)
            boxs = boxCopy(medbox);
        boxGetGeometry(boxs, NULL, NULL, &w, &h);
        delw = width - w;
        delh = height - h;
        del_left = del_right = del_top = del_bot = 0;
        if (widthflag == L_ADJUST_LEFT) {
            del_left = -delw;
        } else if (widthflag == L_ADJUST_RIGHT) {
            del_right = delw;
        } else {
            del_left = -delw / 2;
            del_right = delw / 2 + L_SIGN(delw) * (delw & 1);
        }
        if (heightflag == L_ADJUST_TOP) {
            del_top = -delh;
        } else if (heightflag == L_ADJUST_BOT) {
            del_bot = delh;
        } else {
            del_top = -delh / 2;
            del_bot = delh / 2 + L_SIGN(delh) * (delh & 1);
        }
        boxd = boxAdjustSides(NULL, boxs, del_left, del_right,
                              del_top, del_bot);
        boxaAddBox(boxad, boxd, L_INSERT);
        boxDestroy(&boxs);
    }

    boxDestroy(&medbox);
    return boxad;
}
/*!
 * \brief   boxaConstrainSize()
 *
 * \param[in]    boxas
 * \param[in]    width        force width of all boxes to this size;
 *                            input 0 to use the median width
 * \param[in]    widthflag    L_ADJUST_SKIP, L_ADJUST_LEFT, L_ADJUST_RIGHT,
 *                            or L_ADJUST_LEFT_AND_RIGHT
 * \param[in]    height       force height of all boxes to this size;
 *                            input 0 to use the median height
 * \param[in]    heightflag   L_ADJUST_SKIP, L_ADJUST_TOP, L_ADJUST_BOT,
 *                            or L_ADJUST_TOP_AND_BOT
 * \return  boxad adjusted so all boxes are the same size
 *
 * <pre>
 * Notes:
 *      (1) Forces either width or height (or both) of every box in
 *          the boxa to a specified size, by moving the indicated sides.
 *      (2) Not all input boxes need to be valid.  Median values will be
 *          used with invalid boxes.
 *      (3) Typical input might be the output of boxaLinearFit(),
 *          where each side has been fit.
 *      (4) Unlike boxaAdjustWidthToTarget() and boxaAdjustHeightToTarget(),
 *          this is not dependent on a difference threshold to change the size.
 *      (5) On error, a message is issued and a copy of the input boxa
 *          is returned.
 * </pre>
 */



Notes:
(1) Forces either width or height (or both) of every box in
the boxa to a specified size, by moving the indicated sides.
(2) Not all input boxes need to be valid.  Median values will be
used with invalid boxes.
(3) Typical input might be the output of boxaLinearFit(),
where each side has been fit.
(4) Unlike boxaAdjustWidthToTarget() and boxaAdjustHeightToTarget(),
this is not dependent on a difference threshold to change the size.
(5) On error, a message is issued and a copy of the input boxa
is returned.
Notes : ( 1 ) Forces either width or height ( or both ) of every box in the boxa to a specified size by moving the indicated sides . ( 2 ) Not all input boxes need to be valid . Median values will be used with invalid boxes . ( 3 ) Typical input might be the output of boxaLinearFit () where each side has been fit . ( 4 ) Unlike boxaAdjustWidthToTarget () and boxaAdjustHeightToTarget () this is not dependent on a difference threshold to change the size . ( 5 ) On error a message is issued and a copy of the input boxa is returned .
BOXA *
boxaConstrainSize(BOXA    *boxas,
                  l_int32  width,
                  l_int32  widthflag,
                  l_int32  height,
                  l_int32  heightflag)
{
l_int32  n, i, x, y, w, h, invalid;
l_int32  delw, delh, del_left, del_right, del_top, del_bot;
BOX     *medbox, *boxs, *boxd;
BOXA    *boxad;
    PROCNAME("boxaConstrainSize");
    if (!boxas)
        return (BOXA *)ERROR_PTR("boxas not defined", procName, NULL);
    invalid = boxaGetCount(boxas) - boxaGetValidCount(boxas);
    medbox = NULL;
    if (width == 0 || height == 0 || invalid > 0) {
        if (boxaGetMedianVals(boxas, &x, &y, NULL, NULL, &w, &h)) {
            L_ERROR("median vals not returned", procName);
            return boxaCopy(boxas, L_COPY);
        }
        medbox = boxCreate(x, y, w, h);
        if (width == 0) width = w;
        if (height == 0) height = h;
    }
    n = boxaGetCount(boxas);
    boxad = boxaCreate(n);
    for (i = 0; i < n; i++) {
        if ((boxs = boxaGetValidBox(boxas, i, L_COPY)) == NULL)
            boxs = boxCopy(medbox);
        boxGetGeometry(boxs, NULL, NULL, &w, &h);
        delw = width - w;
        delh = height - h;
        del_left = del_right = del_top = del_bot = 0;
        if (widthflag == L_ADJUST_LEFT) {
            del_left = -delw;
        } else if (widthflag == L_ADJUST_RIGHT) {
            del_right = delw;
        } else {
            del_left = -delw / 2;
            del_right = delw / 2 + L_SIGN(delw) * (delw & 1);
        }
        if (heightflag == L_ADJUST_TOP) {
            del_top = -delh;
        } else if (heightflag == L_ADJUST_BOT) {
            del_bot = delh;
        } else {
            del_top = -delh / 2;
            del_bot = delh / 2 + L_SIGN(delh) * (delh & 1);
        }
        boxd = boxAdjustSides(NULL, boxs, del_left, del_right,
                              del_top, del_bot);
        boxaAddBox(boxad, boxd, L_INSERT);
        boxDestroy(&boxs);
    }
    boxDestroy(&medbox);
    return boxad;
}
BOXA * boxaConstrainSize ( BOXA * boxas , l_int32 width , l_int32 widthflag , l_int32 height , l_int32 heightflag ) { l_int32 n , i , x , y , w , h , invalid ; l_int32 delw , delh , del_left , del_right , del_top , del_bot ; BOX * medbox , * boxs , * boxd ; BOXA * boxad ; PROCNAME ( " " ) ; if ( ! boxas ) return ( BOXA * ) ERROR_PTR ( " " , procName , NULL ) ; invalid = boxaGetCount ( boxas ) - boxaGetValidCount ( boxas ) ; medbox = NULL ; if ( width == 0 || height == 0 || invalid > 0 ) { if ( boxaGetMedianVals ( boxas , & x , & y , NULL , NULL , & w , & h ) ) { L_ERROR ( " " , procName ) ; return boxaCopy ( boxas , L_COPY ) ; } medbox = boxCreate ( x , y , w , h ) ; if ( width == 0 ) width = w ; if ( height == 0 ) height = h ; } n = boxaGetCount ( boxas ) ; boxad = boxaCreate ( n ) ; for ( i = 0 ; i < n ; i ++ ) { if ( ( boxs = boxaGetValidBox ( boxas , i , L_COPY ) ) == NULL ) boxs = boxCopy ( medbox ) ; boxGetGeometry ( boxs , NULL , NULL , & w , & h ) ; delw = width - w ; delh = height - h ; del_left = del_right = del_top = del_bot = 0 ; if ( widthflag == L_ADJUST_LEFT ) { del_left = - delw ; } else if ( widthflag == L_ADJUST_RIGHT ) { del_right = delw ; } else { del_left = - delw / 2 ; del_right = delw / 2 + L_SIGN ( delw ) * ( delw & 1 ) ; } if ( heightflag == L_ADJUST_TOP ) { del_top = - delh ; } else if ( heightflag == L_ADJUST_BOT ) { del_bot = delh ; } else { del_top = - delh / 2 ; del_bot = delh / 2 + L_SIGN ( delh ) * ( delh & 1 ) ; } boxd = boxAdjustSides ( NULL , boxs , del_left , del_right , del_top , del_bot ) ; boxaAddBox ( boxad , boxd , L_INSERT ) ; boxDestroy ( & boxs ) ; } boxDestroy ( & medbox ) ; return boxad ; }
\brief   boxaConstrainSize()
 \param[in]    boxas
 \param[in]    width        force width of all boxes to this size;
                            input 0 to use the median width
 \param[in]    widthflag    L_ADJUST_SKIP, L_ADJUST_LEFT, L_ADJUST_RIGHT,
                            or L_ADJUST_LEFT_AND_RIGHT
 \param[in]    height       force height of all boxes to this size;
                            input 0 to use the median height
 \param[in]    heightflag   L_ADJUST_SKIP, L_ADJUST_TOP, L_ADJUST_BOT,
                            or L_ADJUST_TOP_AND_BOT
 \return  boxad adjusted so all boxes are the same size
\ brief boxaConstrainSize () \ param [ in ] boxas \ param [ in ] width force width of all boxes to this size ; input 0 to use the median width \ param [ in ] widthflag L_ADJUST_SKIP L_ADJUST_LEFT L_ADJUST_RIGHT or L_ADJUST_LEFT_AND_RIGHT \ param [ in ] height force height of all boxes to this size ; input 0 to use the median height \ param [ in ] heightflag L_ADJUST_SKIP L_ADJUST_TOP L_ADJUST_BOT or L_ADJUST_TOP_AND_BOT \ return boxad adjusted so all boxes are the same size
/* Need median values if requested or if there are invalid boxes */