#include <stdio.h>
#include <stdlib.h>
#include "allheaders.h"
Input: boxas box (for clipping) Return boxad (boxa with boxes in boxas clipped to box), or null on error
Notes: (1) All boxes in boxa not intersecting with box are removed, and the remaining boxes are clipped to box.
Input: boxas box (for containment) Return: boxad (boxa with all boxes in boxas that are entirely contained in box), or null on error
Notes: (1) All boxes in boxa that are entirely outside box are removed.
Input: boxa ncorners (2 or 4 for the representation of each box) Return: pta (with points for each box in the boxa), or null on error
Notes: (1) If ncorners == 2, we select the UL and LR corners. Otherwise we save all 4 corners in this order: UL, UR, LL, LR.
BOX* boxAdjustSides | ( | BOX * | boxd, | |
BOX * | boxs, | |||
l_int32 | delleft, | |||
l_int32 | delright, | |||
l_int32 | deltop, | |||
l_int32 | delbot | |||
) |
Input: boxd (<optional>; this can be null, equal to boxs, or different from boxs) boxs (starting box; to have sides adjusted) delleft, delright, deltop, delbot (changes in location of each side) Return: boxd, or null on error or if the computed boxd has width or height <= 0.
Notes: (1) Set boxd == NULL to get new box; boxd == boxs for in-place; or otherwise to resize existing boxd. (2) For usage, suggest one of these: boxd = boxAdjustSides(NULL, boxs, ...); // new boxAdjustSides(boxs, boxs, ...); // in-place boxAdjustSides(boxd, boxs, ...); // other (1) New box dimensions are cropped at left and top to x >= 0 and y >= 0. (2) For example, to expand in-place by 20 pixels on each side, use boxAdjustSides(box, box, -20, 20, -20, 20);
l_int32 boxaEqual | ( | BOXA * | boxa1, | |
BOXA * | boxa2, | |||
l_int32 | maxdist, | |||
NUMA ** | pnaindex, | |||
l_int32 * | psame | |||
) |
Input: boxa1 boxa2 maxdist &naindex (<optional return>=""> index array of correspondences &same (<return> 1 if equal; 0 otherwise) Return 0 if OK, 1 on error
Notes: (1) The two boxa are the "same" if they contain the same boxes and each box is within of its counterpart in their positions within the boxa. This allows for small rearrangements. Use 0 for maxdist if the boxa must be identical. (2) This applies only to geometry and ordering; refcounts are not considered. (3) allows some latitude in the ordering of the boxes. For the boxa to be the "same", corresponding boxes must be within of each other. Note that for large , we should use a hash function for efficiency. (4) naindex[i] gives the position of the box in boxa2 that corresponds to box i in boxa1. It is only returned if the boxa are equal.
l_int32 boxaGetCoverage | ( | BOXA * | boxa, | |
l_int32 | wc, | |||
l_int32 | hc, | |||
l_int32 | exactflag, | |||
l_float32 * | pfract | |||
) |
Input: boxa wc, hc (dimensions of overall clipping rectangle with UL corner at (0, 0) that is covered by the boxes. exactflag (1 for guaranteeing an exact result; 0 for getting an exact result only if the boxes do not overlap) &fract (<return> sum of box area as fraction of w * h) Return: 0 if OK, 1 on error
Notes: (1) The boxes in boxa are clipped to the input rectangle. (2) * When == 1, we generate a 1 bpp pix of size wc x hc, paint all the boxes black, and count the fg pixels. This can take 1 msec on a large page with many boxes. * When == 0, we clip each box to the wc x hc region and sum the resulting areas. This is faster. * The results are the same when none of the boxes overlap within the wc x hc region.
Input: boxa &w (<optional return>=""> width) &h (<optional return>=""> height) &box (<optional return>="">, minimum box containing all boxes in boxa) Return: 0 if OK, 1 on error
Notes: (1) The returned w and h are the minimum size image that would contain all boxes untranslated.
Input: boxa x, y (point) Return box (box with centroid closest to the given point [x,y]), or NULL if no boxes in boxa)
Notes: (1) Uses euclidean distance between centroid and point.
Input: boxas box (for intersecting) Return boxad (boxa with all boxes in boxas that intersect box), or null on error
Notes: (1) All boxes in boxa that intersect with box (i.e., are completely or partially contained in box) are retained.
Input: boxad (dest boxa; add to this one) boxas (source boxa; add from this one) istart (starting index in nas) iend (ending index in nas; use 0 to cat all) Return: 0 if OK, 1 on error
Notes: (1) This appends a clone of each indicated box in boxas to boxad (2) istart < 0 is taken to mean 'read from the start' (istart = 0) (3) iend <= 0 means 'read to the end'
l_int32 boxaLocationRange | ( | BOXA * | boxa, | |
l_int32 * | pminx, | |||
l_int32 * | pminy, | |||
l_int32 * | pmaxx, | |||
l_int32 * | pmaxy | |||
) |
Input: boxa &minx, &miny, &maxx, &maxy (<optional return>=""> range of UL corner positions) Return: 0 if OK, 1 on error
NUMA* boxaMakeSizeIndicator | ( | BOXA * | boxa, | |
l_int32 | width, | |||
l_int32 | height, | |||
l_int32 | type, | |||
l_int32 | relation | |||
) |
Input: boxa width, height (threshold dimensions) type (L_SELECT_WIDTH, L_SELECT_HEIGHT, L_SELECT_IF_EITHER, L_SELECT_IF_BOTH) relation (L_SELECT_IF_LT, L_SELECT_IF_GT, L_SELECT_IF_LTE, L_SELECT_IF_GTE) Return: na (indicator array), or null on error
Notes: (1) The args specify constraints on the size of the components that are kept. (2) If the selection type is L_SELECT_WIDTH, the input height is ignored, and v.v. (3) To keep small components, use relation = L_SELECT_IF_LT or L_SELECT_IF_LTE. To keep large components, use relation = L_SELECT_IF_GT or L_SELECT_IF_GTE.
Input: boxas (input boxa) Return: boxad (with boxes permuted), or null on error
Notes: (1) This does a pseudorandom in-place permutation of the boxes. (2) The result is guaranteed not to have any boxes in their original position, but it is not very random. If you need randomness, use boxaPermuteRandom().
Input: boxad (<optional> can be null or equal to boxas) boxas (input boxa) Return: boxad (with boxes permuted), or null on error
Notes: (1) If boxad is null, make a copy of boxas and permute the copy. Otherwise, boxad must be equal to boxas, and the operation is done in-place. (2) This does a random in-place permutation of the boxes, by swapping each box in turn with a random box. The result is almost guaranteed not to have any boxes in their original position. (3) MSVC rand() has MAX_RAND = 2^15 - 1, so it will not do a proper permutation is the number of boxes exceeds this.
BOXA* boxaSelectBySize | ( | BOXA * | boxas, | |
l_int32 | width, | |||
l_int32 | height, | |||
l_int32 | type, | |||
l_int32 | relation, | |||
l_int32 * | pchanged | |||
) |
Input: boxas width, height (threshold dimensions) type (L_SELECT_WIDTH, L_SELECT_HEIGHT, L_SELECT_IF_EITHER, L_SELECT_IF_BOTH) relation (L_SELECT_IF_LT, L_SELECT_IF_GT, L_SELECT_IF_LTE, L_SELECT_IF_GTE) &changed (<optional return>=""> 1 if changed; 0 if clone returned) Return: boxad (filtered set), or null on error
Notes: (1) The args specify constraints on the size of the components that are kept. (2) Uses box clones in the new boxa. (3) If the selection type is L_SELECT_WIDTH, the input height is ignored, and v.v. (4) To keep small components, use relation = L_SELECT_IF_LT or L_SELECT_IF_LTE. To keep large components, use relation = L_SELECT_IF_GT or L_SELECT_IF_GTE.
Input: boxas na (indicator numa) &changed (<optional return>=""> 1 if changed; 0 if clone returned) Return: boxad, or null on error
Notes: (1) Returns a boxa clone if no components are removed. (2) Uses box clones in the new boxa. (3) The indicator numa has values 0 (ignore) and 1 (accept).
l_int32 boxaSizeRange | ( | BOXA * | boxa, | |
l_int32 * | pminw, | |||
l_int32 * | pminh, | |||
l_int32 * | pmaxw, | |||
l_int32 * | pmaxh | |||
) |
Input: boxa &minw, &minh, &maxw, &maxh (<optional return>=""> range of dimensions of box in the array) Return: 0 if OK, 1 on error
Input: boxa i, j (two indices of boxes, that are to be swapped) Return: 0 if OK, 1 on error
Input: box1, box2 (two boxes) Return: box (of bounding region containing the input boxes), or null on error
Input: box wi, hi (rectangle representing image) Return: part of box within given rectangle, or NULL on error or if box is entirely outside the rectangle
Note: the rectangle is assumed to go from (0,0) to (wi - 1, hi - 1)
Input: box1, box2 &result (<return> 1 if box2 is entirely contained within box1, and 0 otherwise) Return: 0 if OK, 1 on error
Input: box x, y (a point) &contains (<return> 1 if box contains point; 0 otherwise) Return: 0 if OK, 1 on error.
Input: box1 box2 &same (<return> 1 if equal; 0 otherwise) Return 0 if OK, 1 on error
Input: box &x, &y (<return> location of center of box) Return 0 if OK, 1 on error
l_int32 boxIntersectByLine | ( | BOX * | box, | |
l_int32 | x, | |||
l_int32 | y, | |||
l_float32 | slope, | |||
l_int32 * | px1, | |||
l_int32 * | py1, | |||
l_int32 * | px2, | |||
l_int32 * | py2, | |||
l_int32 * | pn | |||
) |
Input: box x, y (point that line goes through) slope (of line) (&x1, &y1) (<return> 1st point of intersection with box) (&x2, &y2) (<return> 2nd point of intersection with box) &n (<return> number of points of intersection) Return: 0 if OK, 1 on error
Notes: (1) If the intersection is at only one point (a corner), the coordinates are returned in (x1, y1). (2) Represent a vertical line by one with a large but finite slope.
Input: box1, box2 &result (<return> 1 if any part of box2 is contained in box1, and 0 otherwise) Return: 0 if OK, 1 on error
Input: box1, box2 (two boxes) &fract (<return> the fraction of box2 overlapped by box1) Return: 0 if OK, 1 on error.
Notes: (1) The result depends on the order of the input boxes, because the overlap is taken as a fraction of box2.
Input: box1, box2 (two boxes) Return: box (of overlap region between input boxes), or null if no overlap or on error
Input: boxd (<optional>; this can be null, equal to boxs, or different from boxs); boxs (starting box; to have one side relocated) loc (new location of the side that is changing) sideflag (L_FROM_LEFT, etc., indicating the side that moves) Return: boxd, or null on error or if the computed boxd has width or height <= 0.
Notes: (1) Set boxd == NULL to get new box; boxd == boxs for in-place; or otherwise to resize existing boxd. (2) For usage, suggest one of these: boxd = boxRelocateOneSide(NULL, boxs, ...); // new boxRelocateOneSide(boxs, boxs, ...); // in-place boxRelocateOneSide(boxd, boxs, ...); // other
Input: pta ncorners (2 or 4 for the representation of each box) Return: boxa (with one box for each 2 or 4 points in the pta), or null on error
Notes: (1) For 2 corners, the order of the 2 points is UL, LR. For 4 corners, the order of points is UL, UR, LL, LR. (2) Each derived box is the minimum szie containing all corners.