68#include <config_auto.h>
72#include "allheaders.h"
105 return (
BOXA *)ERROR_PTR(
"boxas not defined", __func__, NULL);
107 return (
BOXA *)ERROR_PTR(
"invalid copyflag", __func__, NULL);
108 if ((n = boxaGetCount(boxas)) == 0) {
109 L_WARNING(
"boxas is empty\n", __func__);
110 return boxaCopy(boxas, copyflag);
112 first = L_MAX(0, first);
113 if (last < 0) last = n - 1;
115 return (
BOXA *)ERROR_PTR(
"invalid first", __func__, NULL);
117 L_WARNING(
"last = %d is beyond max index = %d; adjusting\n",
118 __func__, last, n - 1);
122 return (
BOXA *)ERROR_PTR(
"first > last", __func__, NULL);
124 nbox = last - first + 1;
125 boxad = boxaCreate(nbox);
126 for (i = first; i <= last; i++) {
127 box = boxaGetBox(boxas, i, copyflag);
161 return (
BOXAA *)ERROR_PTR(
"baas not defined", __func__, NULL);
163 return (
BOXAA *)ERROR_PTR(
"invalid copyflag", __func__, NULL);
164 if ((n = boxaaGetCount(baas)) == 0)
165 return (
BOXAA *)ERROR_PTR(
"empty baas", __func__, NULL);
166 first = L_MAX(0, first);
167 if (last < 0) last = n - 1;
169 return (
BOXAA *)ERROR_PTR(
"invalid first", __func__, NULL);
171 L_WARNING(
"last = %d is beyond max index = %d; adjusting\n",
172 __func__, last, n - 1);
176 return (
BOXAA *)ERROR_PTR(
"first > last", __func__, NULL);
178 nboxa = last - first + 1;
179 baad = boxaaCreate(nboxa);
180 for (i = first; i <= last; i++) {
181 boxa = boxaaGetBoxa(baas, i, copyflag);
227 if (pchanged) *pchanged = FALSE;
229 return (
BOXA *)ERROR_PTR(
"boxas not defined", __func__, NULL);
230 if (boxaGetCount(boxas) == 0) {
231 L_WARNING(
"boxas is empty\n", __func__);
232 return boxaCopy(boxas,
L_COPY);
236 return (
BOXA *)ERROR_PTR(
"invalid type", __func__, NULL);
239 return (
BOXA *)ERROR_PTR(
"invalid relation", __func__, NULL);
244 return (
BOXA *)ERROR_PTR(
"na not made", __func__, NULL);
284l_int32 i, n, w, h, ival;
288 return (
NUMA *)ERROR_PTR(
"boxa not defined", __func__, NULL);
289 if ((n = boxaGetCount(boxa)) == 0)
290 return (
NUMA *)ERROR_PTR(
"boxa is empty", __func__, NULL);
293 return (
NUMA *)ERROR_PTR(
"invalid type", __func__, NULL);
296 return (
NUMA *)ERROR_PTR(
"invalid relation", __func__, NULL);
299 for (i = 0; i < n; i++) {
301 boxaGetBoxGeometry(boxa, i, NULL, NULL, &w, &h);
319 if (((relation ==
L_SELECT_IF_LT) && (w < width || h < height)) ||
326 if (((relation ==
L_SELECT_IF_LT) && (w < width && h < height)) ||
333 L_WARNING(
"can't get here!\n", __func__);
336 numaAddNumber(na, ival);
371 if (pchanged) *pchanged = FALSE;
373 return (
BOXA *)ERROR_PTR(
"boxas not defined", __func__, NULL);
374 if (boxaGetCount(boxas) == 0) {
375 L_WARNING(
"boxas is empty\n", __func__);
376 return boxaCopy(boxas,
L_COPY);
380 return (
BOXA *)ERROR_PTR(
"invalid relation", __func__, NULL);
415l_int32 i, n, w, h, ival;
419 return (
NUMA *)ERROR_PTR(
"boxa not defined", __func__, NULL);
420 if ((n = boxaGetCount(boxa)) == 0)
421 return (
NUMA *)ERROR_PTR(
"boxa is empty", __func__, NULL);
424 return (
NUMA *)ERROR_PTR(
"invalid relation", __func__, NULL);
427 for (i = 0; i < n; i++) {
429 boxaGetBoxGeometry(boxa, i, NULL, NULL, &w, &h);
436 numaAddNumber(na, ival);
471 if (pchanged) *pchanged = FALSE;
473 return (
BOXA *)ERROR_PTR(
"boxas not defined", __func__, NULL);
474 if (boxaGetCount(boxas) == 0) {
475 L_WARNING(
"boxas is empty\n", __func__);
476 return boxaCopy(boxas,
L_COPY);
480 return (
BOXA *)ERROR_PTR(
"invalid relation", __func__, NULL);
515l_int32 i, n, w, h, ival;
520 return (
NUMA *)ERROR_PTR(
"boxa not defined", __func__, NULL);
521 if ((n = boxaGetCount(boxa)) == 0)
522 return (
NUMA *)ERROR_PTR(
"boxa is empty", __func__, NULL);
525 return (
NUMA *)ERROR_PTR(
"invalid relation", __func__, NULL);
528 for (i = 0; i < n; i++) {
530 boxaGetBoxGeometry(boxa, i, NULL, NULL, &w, &h);
531 whratio = (l_float32)w / (l_float32)h;
538 numaAddNumber(na, ival);
566l_int32 i, n, ival, nsave;
570 if (pchanged) *pchanged = FALSE;
572 return (
BOXA *)ERROR_PTR(
"boxas not defined", __func__, NULL);
574 return (
BOXA *)ERROR_PTR(
"na not defined", __func__, NULL);
577 n = numaGetCount(na);
578 for (i = 0; i < n; i++) {
579 numaGetIValue(na, i, &ival);
580 if (ival == 1) nsave++;
584 if (pchanged) *pchanged = FALSE;
585 return boxaCopy(boxas,
L_COPY);
587 if (pchanged) *pchanged = TRUE;
588 boxad = boxaCreate(nsave);
589 for (i = 0; i < n; i++) {
590 numaGetIValue(na, i, &ival);
591 if (ival == 0)
continue;
592 box = boxaGetBox(boxas, i,
L_COPY);
625 return (
BOXA *)ERROR_PTR(
"boxa not defined", __func__, NULL);
627 n = boxaGetCount(boxas);
628 na = numaPseudorandomSequence(n, 0);
629 boxad = boxaSortByIndex(boxas, na);
663 return (
BOXA *)ERROR_PTR(
"boxa not defined", __func__, NULL);
664 if (boxad && (boxad != boxas))
665 return (
BOXA *)ERROR_PTR(
"boxad defined but in-place", __func__, NULL);
668 boxad = boxaCopy(boxas,
L_COPY);
669 if ((n = boxaGetCount(boxad)) == 0)
671 index = (l_uint32)rand() % n;
672 index = L_MAX(1, index);
674 for (i = 1; i < n; i++) {
675 index = (l_uint32)rand() % n;
676 if (index == i) index--;
700 return ERROR_INT(
"boxa not defined", __func__, 1);
701 n = boxaGetCount(boxa);
703 return ERROR_INT(
"i invalid", __func__, 1);
705 return ERROR_INT(
"j invalid", __func__, 1);
707 return ERROR_INT(
"i == j", __func__, 1);
710 boxa->
box[i] = boxa->
box[j];
746 return (
PTA *)ERROR_PTR(
"boxa not defined", __func__, NULL);
747 if (ncorners != 2 && ncorners != 4)
748 return (
PTA *)ERROR_PTR(
"ncorners not 2 or 4", __func__, NULL);
750 n = boxaGetCount(boxa);
751 if ((pta = ptaCreate(n)) == NULL)
752 return (
PTA *)ERROR_PTR(
"pta not made", __func__, NULL);
753 for (i = 0; i < n; i++) {
754 box = boxaGetBox(boxa, i,
L_COPY);
756 ptaJoin(pta, pta1, 0, -1);
784l_int32 i, n, nbox, x1, y1, x2, y2, x3, y3, x4, y4, x, y, xmax, ymax;
789 return (
BOXA *)ERROR_PTR(
"pta not defined", __func__, NULL);
790 if (ncorners != 2 && ncorners != 4)
791 return (
BOXA *)ERROR_PTR(
"ncorners not 2 or 4", __func__, NULL);
792 n = ptaGetCount(pta);
793 if (n % ncorners != 0)
794 return (
BOXA *)ERROR_PTR(
"size % ncorners != 0", __func__, NULL);
796 if ((boxa = boxaCreate(nbox)) == NULL)
797 return (
BOXA *)ERROR_PTR(
"boxa not made", __func__, NULL);
798 for (i = 0; i < n; i += ncorners) {
799 ptaGetIPt(pta, i, &x1, &y1);
800 ptaGetIPt(pta, i + 1, &x2, &y2);
802 box = boxCreate(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
806 ptaGetIPt(pta, i + 2, &x3, &y3);
807 ptaGetIPt(pta, i + 3, &x4, &y4);
810 xmax = L_MAX(x2, x4);
811 ymax = L_MAX(y3, y4);
812 box = boxCreate(x, y, xmax - x + 1, ymax - y + 1);
841 return (
PTA *)ERROR_PTR(
"box not defined", __func__, NULL);
842 if (ncorners != 2 && ncorners != 4)
843 return (
PTA *)ERROR_PTR(
"ncorners not 2 or 4", __func__, NULL);
845 if ((pta = ptaCreate(ncorners)) == NULL)
846 return (
PTA *)ERROR_PTR(
"pta not made", __func__, NULL);
847 boxGetGeometry(box, &x, &y, &w, &h);
850 ptaAddPt(pta, x + w - 1, y + h - 1);
852 ptaAddPt(pta, x + w - 1, y);
853 ptaAddPt(pta, x, y + h - 1);
854 ptaAddPt(pta, x + w - 1, y + h - 1);
876l_int32 n, x1, y1, x2, y2, x3, y3, x4, y4, x, y, xmax, ymax;
879 return (
BOX *)ERROR_PTR(
"pta not defined", __func__, NULL);
880 n = ptaGetCount(pta);
881 if (n != 2 && n != 4)
882 return (
BOX *)ERROR_PTR(
"n must be 2 or 4", __func__, NULL);
883 ptaGetIPt(pta, 0, &x1, &y1);
884 ptaGetIPt(pta, 1, &x2, &y2);
886 return boxCreate(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
889 ptaGetIPt(pta, 2, &x3, &y3);
890 ptaGetIPt(pta, 3, &x4, &y4);
893 xmax = L_MAX(x2, x4);
894 ymax = L_MAX(y3, y4);
895 return boxCreate(x, y, xmax - x + 1, ymax - y + 1);
929l_int32 i, n, x, y, w, h, xmax, ymax, xmin, ymin, found;
931 if (!pw && !ph && !pbox)
932 return ERROR_INT(
"no ptrs defined", __func__, 1);
935 if (pbox) *pbox = NULL;
937 return ERROR_INT(
"boxa not defined", __func__, 1);
939 n = boxaGetCount(boxa);
941 xmin = ymin = 100000000;
943 for (i = 0; i < n; i++) {
944 boxaGetBoxGeometry(boxa, i, &x, &y, &w, &h);
945 if (w <= 0 || h <= 0)
948 xmin = L_MIN(xmin, x);
949 ymin = L_MIN(ymin, y);
950 xmax = L_MAX(xmax, x + w);
951 ymax = L_MAX(ymax, y + h);
958 *pbox = boxCreate(xmin, ymin, xmax - xmin, ymax - ymin);
994l_int32 i, n, x, y, w, h, sum;
999 return ERROR_INT(
"&fract not defined", __func__, 1);
1002 return ERROR_INT(
"boxa not defined", __func__, 1);
1004 n = boxaGetCount(boxa);
1006 return ERROR_INT(
"no boxes in boxa", __func__, 1);
1008 if (exactflag == 0) {
1010 for (i = 0; i < n; i++) {
1011 box = boxaGetBox(boxa, i,
L_CLONE);
1012 if ((boxc = boxClipToRectangle(box, wc, hc)) != NULL) {
1013 boxGetGeometry(boxc, NULL, NULL, &w, &h);
1020 pixt = pixCreate(wc, hc, 1);
1021 for (i = 0; i < n; i++) {
1022 box = boxaGetBox(boxa, i,
L_CLONE);
1023 boxGetGeometry(box, &x, &y, &w, &h);
1024 pixRasterop(pixt, x, y, w, h,
PIX_SET, NULL, 0, 0);
1027 pixCountPixels(pixt, &sum, NULL);
1031 *pfract = (l_float32)sum / (l_float32)(wc * hc);
1053l_int32 minw, minh, maxw, maxh, minbw, minbh, maxbw, maxbh, i, n;
1056 if (!pminw && !pmaxw && !pminh && !pmaxh)
1057 return ERROR_INT(
"no data can be returned", __func__, 1);
1058 if (pminw) *pminw = 0;
1059 if (pminh) *pminh = 0;
1060 if (pmaxw) *pmaxw = 0;
1061 if (pmaxh) *pmaxh = 0;
1063 return ERROR_INT(
"baa not defined", __func__, 1);
1065 minw = minh = 100000000;
1067 n = boxaaGetCount(baa);
1068 for (i = 0; i < n; i++) {
1069 boxa = boxaaGetBoxa(baa, i,
L_CLONE);
1082 if (pminw) *pminw = minw;
1083 if (pminh) *pminh = minh;
1084 if (pmaxw) *pmaxw = maxw;
1085 if (pmaxh) *pmaxh = maxh;
1107l_int32 minw, minh, maxw, maxh, i, n, w, h;
1109 if (!pminw && !pmaxw && !pminh && !pmaxh)
1110 return ERROR_INT(
"no data can be returned", __func__, 1);
1111 if (pminw) *pminw = 0;
1112 if (pminh) *pminh = 0;
1113 if (pmaxw) *pmaxw = 0;
1114 if (pmaxh) *pmaxh = 0;
1116 return ERROR_INT(
"boxa not defined", __func__, 1);
1118 minw = minh = 100000000;
1120 n = boxaGetCount(boxa);
1121 for (i = 0; i < n; i++) {
1122 boxaGetBoxGeometry(boxa, i, NULL, NULL, &w, &h);
1133 if (pminw) *pminw = minw;
1134 if (pminh) *pminh = minh;
1135 if (pmaxw) *pmaxw = maxw;
1136 if (pmaxh) *pmaxh = maxh;
1158l_int32 minx, miny, maxx, maxy, i, n, x, y;
1160 if (!pminx && !pminy && !pmaxx && !pmaxy)
1161 return ERROR_INT(
"no data can be returned", __func__, 1);
1162 if (pminx) *pminx = 0;
1163 if (pminy) *pminy = 0;
1164 if (pmaxx) *pmaxx = 0;
1165 if (pmaxy) *pmaxy = 0;
1167 return ERROR_INT(
"boxa not defined", __func__, 1);
1169 minx = miny = 100000000;
1171 n = boxaGetCount(boxa);
1172 for (i = 0; i < n; i++) {
1173 boxaGetBoxGeometry(boxa, i, &x, &y, NULL, NULL);
1184 if (pminx) *pminx = minx;
1185 if (pminy) *pminy = miny;
1186 if (pmaxx) *pmaxx = maxx;
1187 if (pmaxy) *pmaxy = maxy;
1209 if (pnaw) *pnaw = NULL;
1210 if (pnah) *pnah = NULL;
1212 return ERROR_INT(
"no output requested", __func__, 1);
1214 return ERROR_INT(
"boxa not defined", __func__, 1);
1216 n = boxaGetValidCount(boxa);
1217 if (pnaw) *pnaw = numaCreate(n);
1218 if (pnah) *pnah = numaCreate(n);
1219 for (i = 0; i < n; i++) {
1220 box = boxaGetValidBox(boxa, i,
L_COPY);
1222 boxGetGeometry(box, NULL, NULL, &w, &h);
1223 if (pnaw) numaAddNumber(*pnaw, w);
1224 if (pnah) numaAddNumber(*pnah, h);
1252 return ERROR_INT(
"&area not defined", __func__, 1);
1255 return ERROR_INT(
"boxa not defined", __func__, 1);
1257 n = boxaGetCount(boxa);
1258 for (i = 0; i < n; i++) {
1259 boxaGetBoxGeometry(boxa, i, NULL, NULL, &w, &h);
1300 l_float32 scalefactor,
1306l_int32 i, n, npix, w, h, fontsize;
1310PIX *pix1, *pix2, *pixd;
1314 return (
PIX *)ERROR_PTR(
"boxas not defined", __func__, NULL);
1316 boxa = boxaSaveValid(boxas,
L_COPY);
1317 n = boxaGetCount(boxa);
1319 npix = pixaGetCount(pixa);
1322 return (
PIX *)ERROR_PTR(
"boxa and pixa counts differ",
1326 first = L_MAX(0, first);
1327 if (last < 0) last = n - 1;
1330 return (
PIX *)ERROR_PTR(
"invalid first", __func__, NULL);
1333 L_WARNING(
"last = %d is beyond max index = %d; adjusting\n",
1334 __func__, last, n - 1);
1339 return (
PIX *)ERROR_PTR(
"first > last", __func__, NULL);
1344 if (scalefactor > 0.8)
1346 else if (scalefactor > 0.6)
1348 else if (scalefactor > 0.4)
1350 else if (scalefactor > 0.3)
1353 bmf = bmfCreate(NULL, fontsize);
1355 pixat = pixaCreate(n);
1357 for (i = first; i <= last; i++) {
1358 box = boxaGetBox(boxa, i,
L_CLONE);
1360 pix1 = pixCreate(w, h, 32);
1363 pix1 = pixaGetPix(pixa, i,
L_COPY);
1365 pixSetBorderVal(pix1, 0, 0, 0, 2, 0x0000ff00);
1366 snprintf(buf,
sizeof(buf),
"%d", i);
1367 pix2 = pixAddSingleTextblock(pix1, bmf, buf, 0x00ff0000,
1370 pixRenderBoxArb(pix2, box, linewidth, 255, 0, 0);
1377 pixd = pixaDisplayTiledInRows(pixat, 32, maxwidth, scalefactor, background,
1379 pixaDestroy(&pixat);
BOXA * boxaPermutePseudorandom(BOXA *boxas)
boxaPermutePseudorandom()
l_ok boxaSwapBoxes(BOXA *boxa, l_int32 i, l_int32 j)
boxaSwapBoxes()
NUMA * boxaMakeWHRatioIndicator(BOXA *boxa, l_float32 ratio, l_int32 relation)
boxaMakeWHRatioIndicator()
l_ok boxaaSizeRange(BOXAA *baa, l_int32 *pminw, l_int32 *pminh, l_int32 *pmaxw, l_int32 *pmaxh)
boxaaSizeRange()
PIX * boxaDisplayTiled(BOXA *boxas, PIXA *pixa, l_int32 first, l_int32 last, l_int32 maxwidth, l_int32 linewidth, l_float32 scalefactor, l_int32 background, l_int32 spacing, l_int32 border)
boxaDisplayTiled()
l_ok boxaGetSizes(BOXA *boxa, NUMA **pnaw, NUMA **pnah)
boxaGetSizes()
NUMA * boxaMakeSizeIndicator(BOXA *boxa, l_int32 width, l_int32 height, l_int32 type, l_int32 relation)
boxaMakeSizeIndicator()
BOXA * boxaSelectRange(BOXA *boxas, l_int32 first, l_int32 last, l_int32 copyflag)
boxaSelectRange()
l_ok boxaGetArea(BOXA *boxa, l_int32 *parea)
boxaGetArea()
BOXA * boxaSelectBySize(BOXA *boxas, l_int32 width, l_int32 height, l_int32 type, l_int32 relation, l_int32 *pchanged)
boxaSelectBySize()
PTA * boxConvertToPta(BOX *box, l_int32 ncorners)
boxConvertToPta()
BOXA * boxaPermuteRandom(BOXA *boxad, BOXA *boxas)
boxaPermuteRandom()
NUMA * boxaMakeAreaIndicator(BOXA *boxa, l_int32 area, l_int32 relation)
boxaMakeAreaIndicator()
BOXA * boxaSelectByWHRatio(BOXA *boxas, l_float32 ratio, l_int32 relation, l_int32 *pchanged)
boxaSelectByWHRatio()
l_ok boxaSizeRange(BOXA *boxa, l_int32 *pminw, l_int32 *pminh, l_int32 *pmaxw, l_int32 *pmaxh)
boxaSizeRange()
l_ok boxaGetExtent(BOXA *boxa, l_int32 *pw, l_int32 *ph, BOX **pbox)
boxaGetExtent()
PTA * boxaConvertToPta(BOXA *boxa, l_int32 ncorners)
boxaConvertToPta()
BOXA * boxaSelectByArea(BOXA *boxas, l_int32 area, l_int32 relation, l_int32 *pchanged)
boxaSelectByArea()
BOX * ptaConvertToBox(PTA *pta)
ptaConvertToBox()
BOXA * ptaConvertToBoxa(PTA *pta, l_int32 ncorners)
ptaConvertToBoxa()
l_ok boxaGetCoverage(BOXA *boxa, l_int32 wc, l_int32 hc, l_int32 exactflag, l_float32 *pfract)
boxaGetCoverage()
BOXA * boxaSelectWithIndicator(BOXA *boxas, NUMA *na, l_int32 *pchanged)
boxaSelectWithIndicator()
l_ok boxaLocationRange(BOXA *boxa, l_int32 *pminx, l_int32 *pminy, l_int32 *pmaxx, l_int32 *pmaxy)
boxaLocationRange()
BOXAA * boxaaSelectRange(BOXAA *baas, l_int32 first, l_int32 last, l_int32 copyflag)
boxaaSelectRange()