96#include <config_auto.h>
100#include "allheaders.h"
106static const l_int32 MinCompsForBinSort = 200;
109static const l_float32 MinAngleToRotate = 0.001f;
223 l_int32 connectivity,
228l_int32 w, h, empty, changed, count;
234 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
235 if (connectivity != 4 && connectivity != 8)
236 return (
PIX *)ERROR_PTR(
"connectivity not 4 or 8", __func__, NULL);
239 return (
PIX *)ERROR_PTR(
"invalid type", __func__, NULL);
242 return (
PIX *)ERROR_PTR(
"invalid relation", __func__, NULL);
243 if (pchanged) *pchanged = FALSE;
246 pixZero(pixs, &empty);
248 return pixCopy(NULL, pixs);
251 boxa = pixConnComp(pixs, &pixas, connectivity);
258 return pixCopy(NULL, pixs);
262 if (pchanged) *pchanged = TRUE;
263 pixGetDimensions(pixs, &w, &h, NULL);
264 count = pixaGetCount(pixad);
266 pixd = pixCreateTemplate(pixs);
268 pixd = pixaDisplay(pixad, w, h);
269 pixCopyResolution(pixd, pixs);
270 pixCopyColormap(pixd, pixs);
271 pixCopyText(pixd, pixs);
272 pixCopyInputFormat(pixd, pixs);
316 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
319 return (
PIXA *)ERROR_PTR(
"invalid type", __func__, NULL);
322 return (
PIXA *)ERROR_PTR(
"invalid relation", __func__, NULL);
365l_int32 i, n, w, h, ival;
369 return (
NUMA *)ERROR_PTR(
"pixa not defined", __func__, NULL);
372 return (
NUMA *)ERROR_PTR(
"invalid type", __func__, NULL);
375 return (
NUMA *)ERROR_PTR(
"invalid relation", __func__, NULL);
377 n = pixaGetCount(pixa);
379 for (i = 0; i < n; i++) {
381 pixaGetPixDimensions(pixa, i, &w, &h, NULL);
399 if (((relation ==
L_SELECT_IF_LT) && (w < width || h < height)) ||
406 if (((relation ==
L_SELECT_IF_LT) && (w < width && h < height)) ||
413 L_WARNING(
"can't get here!\n", __func__);
416 numaAddNumber(na, ival);
450 l_int32 connectivity,
454l_int32 w, h, empty, changed, count;
460 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
461 if (connectivity != 4 && connectivity != 8)
462 return (
PIX *)ERROR_PTR(
"connectivity not 4 or 8", __func__, NULL);
465 return (
PIX *)ERROR_PTR(
"invalid type", __func__, NULL);
466 if (pchanged) *pchanged = FALSE;
469 pixZero(pixs, &empty);
471 return pixCopy(NULL, pixs);
474 boxa = pixConnComp(pixs, &pixas, connectivity);
481 return pixCopy(NULL, pixs);
485 if (pchanged) *pchanged = TRUE;
486 pixGetDimensions(pixs, &w, &h, NULL);
487 count = pixaGetCount(pixad);
489 pixd = pixCreateTemplate(pixs);
491 pixd = pixaDisplay(pixad, w, h);
492 pixCopyResolution(pixd, pixs);
493 pixCopyColormap(pixd, pixs);
494 pixCopyText(pixd, pixs);
495 pixCopyInputFormat(pixd, pixs);
529 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
532 return (
PIXA *)ERROR_PTR(
"invalid type", __func__, NULL);
535 na = pixaFindPerimToAreaRatio(pixas);
538 nai = numaMakeThresholdIndicator(na, thresh, type);
577 l_int32 connectivity,
581l_int32 w, h, empty, changed, count;
587 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
588 if (connectivity != 4 && connectivity != 8)
589 return (
PIX *)ERROR_PTR(
"connectivity not 4 or 8", __func__, NULL);
592 return (
PIX *)ERROR_PTR(
"invalid type", __func__, NULL);
593 if (pchanged) *pchanged = FALSE;
596 pixZero(pixs, &empty);
598 return pixCopy(NULL, pixs);
601 boxa = pixConnComp(pixs, &pixas, connectivity);
608 return pixCopy(NULL, pixs);
612 if (pchanged) *pchanged = TRUE;
613 pixGetDimensions(pixs, &w, &h, NULL);
614 count = pixaGetCount(pixad);
616 pixd = pixCreateTemplate(pixs);
618 pixd = pixaDisplay(pixad, w, h);
619 pixCopyResolution(pixd, pixs);
620 pixCopyColormap(pixd, pixs);
621 pixCopyText(pixd, pixs);
622 pixCopyInputFormat(pixd, pixs);
656 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
659 return (
PIXA *)ERROR_PTR(
"invalid type", __func__, NULL);
662 na = pixaFindPerimSizeRatio(pixas);
665 nai = numaMakeThresholdIndicator(na, thresh, type);
703 l_int32 connectivity,
707l_int32 w, h, empty, changed, count;
713 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
714 if (connectivity != 4 && connectivity != 8)
715 return (
PIX *)ERROR_PTR(
"connectivity not 4 or 8", __func__, NULL);
718 return (
PIX *)ERROR_PTR(
"invalid type", __func__, NULL);
719 if (pchanged) *pchanged = FALSE;
722 pixZero(pixs, &empty);
724 return pixCopy(NULL, pixs);
727 boxa = pixConnComp(pixs, &pixas, connectivity);
734 return pixCopy(NULL, pixs);
738 if (pchanged) *pchanged = TRUE;
739 pixGetDimensions(pixs, &w, &h, NULL);
740 count = pixaGetCount(pixad);
742 pixd = pixCreateTemplate(pixs);
744 pixd = pixaDisplay(pixad, w, h);
745 pixCopyResolution(pixd, pixs);
746 pixCopyColormap(pixd, pixs);
747 pixCopyText(pixd, pixs);
748 pixCopyInputFormat(pixd, pixs);
786 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
789 return (
PIXA *)ERROR_PTR(
"invalid type", __func__, NULL);
792 na = pixaFindAreaFraction(pixas);
795 nai = numaMakeThresholdIndicator(na, thresh, type);
833 l_int32 connectivity,
837l_int32 w, h, empty, changed, count;
843 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
844 if (connectivity != 4 && connectivity != 8)
845 return (
PIX *)ERROR_PTR(
"connectivity not 4 or 8", __func__, NULL);
848 return (
PIX *)ERROR_PTR(
"invalid type", __func__, NULL);
849 if (pchanged) *pchanged = FALSE;
852 pixZero(pixs, &empty);
854 return pixCopy(NULL, pixs);
857 boxa = pixConnComp(pixs, &pixas, connectivity);
864 return pixCopy(NULL, pixs);
868 if (pchanged) *pchanged = TRUE;
869 pixGetDimensions(pixs, &w, &h, NULL);
870 count = pixaGetCount(pixad);
872 pixd = pixCreateTemplate(pixs);
874 pixd = pixaDisplay(pixad, w, h);
875 pixCopyResolution(pixd, pixs);
876 pixCopyColormap(pixd, pixs);
877 pixCopyText(pixd, pixs);
878 pixCopyInputFormat(pixd, pixs);
916 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
919 return (
PIXA *)ERROR_PTR(
"invalid type", __func__, NULL);
922 na = pixaCountPixels(pixas);
925 nai = numaMakeThresholdIndicator(na, thresh, type);
962 l_int32 connectivity,
966l_int32 w, h, empty, changed, count;
972 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
973 if (connectivity != 4 && connectivity != 8)
974 return (
PIX *)ERROR_PTR(
"connectivity not 4 or 8", __func__, NULL);
977 return (
PIX *)ERROR_PTR(
"invalid type", __func__, NULL);
978 if (pchanged) *pchanged = FALSE;
981 pixZero(pixs, &empty);
983 return pixCopy(NULL, pixs);
986 boxa = pixConnComp(pixs, &pixas, connectivity);
993 return pixCopy(NULL, pixs);
997 if (pchanged) *pchanged = TRUE;
998 pixGetDimensions(pixs, &w, &h, NULL);
999 count = pixaGetCount(pixad);
1001 pixd = pixCreateTemplate(pixs);
1003 pixd = pixaDisplay(pixad, w, h);
1004 pixCopyResolution(pixd, pixs);
1005 pixCopyColormap(pixd, pixs);
1006 pixCopyText(pixd, pixs);
1007 pixCopyInputFormat(pixd, pixs);
1009 pixaDestroy(&pixad);
1045 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
1048 return (
PIXA *)ERROR_PTR(
"invalid type", __func__, NULL);
1051 na = pixaFindWidthHeightRatio(pixas);
1054 nai = numaMakeThresholdIndicator(na, thresh, type);
1087 l_int32 connectivity,
1095 if (pchanged) *pchanged = 0;
1097 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
1099 return (
PIXA *)ERROR_PTR(
"nmin > nmax", __func__, NULL);
1100 if (connectivity != 4 && connectivity != 8)
1101 return (
PIXA *)ERROR_PTR(
"connectivity not 4 or 8", __func__, NULL);
1104 n = pixaGetCount(pixas);
1106 for (i = 0; i < n; i++) {
1107 pix = pixaGetPix(pixas, i,
L_CLONE);
1108 pixCountConnComp(pix, connectivity, &count);
1109 if (count >= nmin && count <= nmax)
1110 numaAddNumber(na, 1);
1112 numaAddNumber(na, 0);
1145l_int32 i, n, nbox, ival, nsave;
1151 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
1153 return (
PIXA *)ERROR_PTR(
"na not defined", __func__, NULL);
1156 n = numaGetCount(na);
1157 for (i = 0; i < n; i++) {
1158 numaGetIValue(na, i, &ival);
1159 if (ival == 1) nsave++;
1163 if (pchanged) *pchanged = FALSE;
1164 return pixaCopy(pixas,
L_CLONE);
1166 if (pchanged) *pchanged = TRUE;
1167 pixad = pixaCreate(nsave);
1168 nbox = pixaGetBoxaCount(pixas);
1169 for (i = 0; i < n; i++) {
1170 numaGetIValue(na, i, &ival);
1171 if (ival == 0)
continue;
1172 pix1 = pixaGetPix(pixas, i,
L_CLONE);
1175 box = pixaGetBox(pixas, i,
L_CLONE);
1203l_int32 i, n, ival, x, y, w, h;
1208 return ERROR_INT(
"pixs not defined", __func__, 1);
1210 return ERROR_INT(
"pixa not defined", __func__, 1);
1212 return ERROR_INT(
"na not defined", __func__, 1);
1213 n = pixaGetCount(pixa);
1214 if (n != numaGetCount(na))
1215 return ERROR_INT(
"pixa and na sizes not equal", __func__, 1);
1217 for (i = 0; i < n; i++) {
1218 numaGetIValue(na, i, &ival);
1220 pix = pixaGetPix(pixa, i,
L_CLONE);
1221 box = pixaGetBox(pixa, i,
L_CLONE);
1222 boxGetGeometry(box, &x, &y, &w, &h);
1254l_int32 i, n, ival, x, y, w, h;
1259 return ERROR_INT(
"pixs not defined", __func__, 1);
1261 return ERROR_INT(
"pixa not defined", __func__, 1);
1263 return ERROR_INT(
"na not defined", __func__, 1);
1264 n = pixaGetCount(pixa);
1265 if (n != numaGetCount(na))
1266 return ERROR_INT(
"pixa and na sizes not equal", __func__, 1);
1268 for (i = 0; i < n; i++) {
1269 numaGetIValue(na, i, &ival);
1271 pix = pixaGetPix(pixa, i,
L_CLONE);
1272 box = pixaGetBox(pixa, i,
L_CLONE);
1273 boxGetGeometry(box, &x, &y, &w, &h);
1305l_int32 i, nval, npix, nbox, val, imaxval;
1312 if (perror) *perror = 0;
1314 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
1316 return (
PIXA *)ERROR_PTR(
"str not defined", __func__, NULL);
1318 if ((na = numaCreateFromString(str)) == NULL)
1319 return (
PIXA *)ERROR_PTR(
"na not made", __func__, NULL);
1320 if ((nval = numaGetCount(na)) == 0) {
1322 return (
PIXA *)ERROR_PTR(
"no indices found", __func__, NULL);
1324 numaGetMax(na, &maxval, NULL);
1325 imaxval = (l_int32)(maxval + 0.1);
1326 nbox = pixaGetBoxaCount(pixas);
1327 npix = pixaGetCount(pixas);
1328 if (imaxval >= npix) {
1329 if (perror) *perror = 1;
1330 L_ERROR(
"max index = %d, size of pixa = %d\n", __func__, imaxval, npix);
1333 pixad = pixaCreate(nval);
1334 for (i = 0; i < nval; i++) {
1335 numaGetIValue(na, i, &val);
1336 if (val < 0 || val >= npix) {
1337 L_ERROR(
"index %d out of range of pix\n", __func__, val);
1340 pix1 = pixaGetPix(pixas, val,
L_COPY);
1343 box = pixaGetBox(pixas, val,
L_COPY);
1374l_int32 n, x, y, w, h, same, maxd;
1380 return (
PIX *)ERROR_PTR(
"pixa not defined", __func__, pixs);
1381 n = pixaGetCount(pixa);
1382 if (index < 0 || index >= n)
1383 return (
PIX *)ERROR_PTR(
"invalid index", __func__, pixs);
1384 if (pixs && (pixGetDepth(pixs) != 1))
1385 return (
PIX *)ERROR_PTR(
"pixs not 1 bpp", __func__, pixs);
1386 pixaVerifyDepth(pixa, &same, &maxd);
1388 return (
PIX *)ERROR_PTR(
"not all pix with d == 1", __func__, pixs);
1390 boxa = pixaGetBoxa(pixa,
L_CLONE);
1392 boxaGetExtent(boxa, &w, &h, NULL);
1393 pixs = pixCreate(w, h, 1);
1396 pix = pixaGetPix(pixa, index,
L_CLONE);
1397 box = boxaGetBox(boxa, index,
L_CLONE);
1398 boxGetGeometry(box, &x, &y, &w, &h);
1444l_int32 i, n, nb, x, y, w, h;
1449 if (pnaindex) *pnaindex = NULL;
1451 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
1459 return (
PIXA *)ERROR_PTR(
"invalid sort type", __func__, NULL);
1461 return (
PIXA *)ERROR_PTR(
"invalid sort order", __func__, NULL);
1463 return (
PIXA *)ERROR_PTR(
"invalid copy flag", __func__, NULL);
1466 if ((n = pixaGetCount(pixas)) == 0) {
1467 L_INFO(
"no pix in pixa\n", __func__);
1468 return pixaCopy(pixas, copyflag);
1470 if ((boxa = pixas->
boxa) == NULL)
1471 return (
PIXA *)ERROR_PTR(
"boxa not found!", __func__, NULL);
1472 nb = boxaGetCount(boxa);
1478 L_WARNING(
"sort by x or y where all values are 0\n", __func__);
1481 return (
PIXA *)ERROR_PTR(
"boxa and pixa counts differ", __func__, NULL);
1484 if (n > MinCompsForBinSort &&
1488 return pixaBinSort(pixas, sorttype, sortorder, pnaindex, copyflag);
1491 if ((na = numaCreate(n)) == NULL)
1492 return (
PIXA *)ERROR_PTR(
"na not made", __func__, NULL);
1493 for (i = 0; i < n; i++) {
1494 boxaGetBoxGeometry(boxa, i, &x, &y, &w, &h);
1498 numaAddNumber(na, x);
1501 numaAddNumber(na, y);
1504 numaAddNumber(na, w);
1507 numaAddNumber(na, h);
1510 numaAddNumber(na, L_MIN(w, h));
1513 numaAddNumber(na, L_MAX(w, h));
1516 numaAddNumber(na, w + h);
1519 numaAddNumber(na, w * h);
1522 numaAddNumber(na, (l_float32)w / (l_float32)h);
1525 L_WARNING(
"invalid sort type\n", __func__);
1530 naindex = numaGetSortIndex(na, sortorder);
1533 return (
PIXA *)ERROR_PTR(
"naindex not made", __func__, NULL);
1537 numaDestroy(&naindex);
1538 return (
PIXA *)ERROR_PTR(
"pixad not made", __func__, NULL);
1542 *pnaindex = naindex;
1544 numaDestroy(&naindex);
1582l_int32 i, n, x, y, w, h;
1587 if (pnaindex) *pnaindex = NULL;
1589 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
1593 return (
PIXA *)ERROR_PTR(
"invalid sort type", __func__, NULL);
1595 return (
PIXA *)ERROR_PTR(
"invalid sort order", __func__, NULL);
1597 return (
PIXA *)ERROR_PTR(
"invalid copy flag", __func__, NULL);
1600 if ((boxa = pixas->
boxa) == NULL)
1601 return (
PIXA *)ERROR_PTR(
"boxa not found", __func__, NULL);
1602 n = pixaGetCount(pixas);
1603 if (boxaGetCount(boxa) != n)
1604 return (
PIXA *)ERROR_PTR(
"boxa and pixa counts differ", __func__, NULL);
1607 if ((na = numaCreate(n)) == NULL)
1608 return (
PIXA *)ERROR_PTR(
"na not made", __func__, NULL);
1609 for (i = 0; i < n; i++) {
1610 boxaGetBoxGeometry(boxa, i, &x, &y, &w, &h);
1614 numaAddNumber(na, x);
1617 numaAddNumber(na, y);
1620 numaAddNumber(na, w);
1623 numaAddNumber(na, h);
1626 numaAddNumber(na, w + h);
1629 L_WARNING(
"invalid sort type\n", __func__);
1634 naindex = numaGetBinSortIndex(na, sortorder);
1637 return (
PIXA *)ERROR_PTR(
"naindex not made", __func__, NULL);
1641 numaDestroy(&naindex);
1642 return (
PIXA *)ERROR_PTR(
"pixad not made", __func__, NULL);
1646 *pnaindex = naindex;
1648 numaDestroy(&naindex);
1672 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
1674 return (
PIXA *)ERROR_PTR(
"naindex not defined", __func__, NULL);
1676 return (
PIXA *)ERROR_PTR(
"invalid copyflag", __func__, NULL);
1678 n = pixaGetCount(pixas);
1679 pixad = pixaCreate(n);
1680 for (i = 0; i < n; i++) {
1681 numaGetIValue(naindex, i, &index);
1682 pix = pixaGetPix(pixas, index, copyflag);
1683 box = pixaGetBox(pixas, index, copyflag);
1705l_int32 pixtot, ntot, i, j, n, nn, index;
1713 return (
PIXAA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
1715 return (
PIXAA *)ERROR_PTR(
"naindex not defined", __func__, NULL);
1718 ntot = numaaGetNumberCount(naa);
1719 pixtot = pixaGetCount(pixas);
1721 return (
PIXAA *)ERROR_PTR(
"element count mismatch", __func__, NULL);
1723 n = numaaGetCount(naa);
1724 paa = pixaaCreate(n);
1725 for (i = 0; i < n; i++) {
1726 na = numaaGetNuma(naa, i,
L_CLONE);
1727 nn = numaGetCount(na);
1728 pixa = pixaCreate(nn);
1729 for (j = 0; j < nn; j++) {
1730 numaGetIValue(na, j, &index);
1731 pix = pixaGetPix(pixas, index, copyflag);
1732 box = pixaGetBox(pixas, index, copyflag);
1774 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
1776 return (
PIXA *)ERROR_PTR(
"invalid copyflag", __func__, NULL);
1777 n = pixaGetCount(pixas);
1778 first = L_MAX(0, first);
1779 if (last < 0) last = n - 1;
1781 return (
PIXA *)ERROR_PTR(
"invalid first", __func__, NULL);
1783 L_WARNING(
"last = %d is beyond max index = %d; adjusting\n",
1784 __func__, last, n - 1);
1788 return (
PIXA *)ERROR_PTR(
"first > last", __func__, NULL);
1790 npix = last - first + 1;
1791 pixad = pixaCreate(npix);
1792 for (i = first; i <= last; i++) {
1793 pix = pixaGetPix(pixas, i, copyflag);
1827 return (
PIXAA *)ERROR_PTR(
"paas not defined", __func__, NULL);
1829 return (
PIXAA *)ERROR_PTR(
"invalid copyflag", __func__, NULL);
1830 n = pixaaGetCount(paas, NULL);
1831 first = L_MAX(0, first);
1832 if (last < 0) last = n - 1;
1834 return (
PIXAA *)ERROR_PTR(
"invalid first", __func__, NULL);
1836 L_WARNING(
"last = %d is beyond max index = %d; adjusting\n",
1837 __func__, last, n - 1);
1841 return (
PIXAA *)ERROR_PTR(
"first > last", __func__, NULL);
1843 npixa = last - first + 1;
1844 paad = pixaaCreate(npixa);
1845 for (i = first; i <= last; i++) {
1846 pixa = pixaaGetPixa(paas, i, copyflag);
1847 pixaaAddPixa(paad, pixa,
L_INSERT);
1885 return (
PIXAA *)ERROR_PTR(
"paas not defined", __func__, NULL);
1886 if (wd <= 0 && hd <= 0)
1887 return (
PIXAA *)ERROR_PTR(
"neither wd nor hd > 0", __func__, NULL);
1889 n = pixaaGetCount(paas, NULL);
1890 paad = pixaaCreate(n);
1891 for (i = 0; i < n; i++) {
1892 pixa1 = pixaaGetPixa(paas, i,
L_CLONE);
1894 pixaaAddPixa(paad, pixa2,
L_INSERT);
1895 pixaDestroy(&pixa1);
1927l_int32 n, i, wd, hd;
1932 return (
PIXAA *)ERROR_PTR(
"paas not defined", __func__, NULL);
1934 return (
PIXAA *)ERROR_PTR(
"!nawd && !nahd", __func__, NULL);
1936 n = pixaaGetCount(paas, NULL);
1937 if (nawd && (n != numaGetCount(nawd)))
1938 return (
PIXAA *)ERROR_PTR(
"nawd wrong size", __func__, NULL);
1939 if (nahd && (n != numaGetCount(nahd)))
1940 return (
PIXAA *)ERROR_PTR(
"nahd wrong size", __func__, NULL);
1941 paad = pixaaCreate(n);
1942 for (i = 0; i < n; i++) {
1944 if (nawd) numaGetIValue(nawd, i, &wd);
1945 if (nahd) numaGetIValue(nahd, i, &hd);
1946 pixa1 = pixaaGetPixa(paas, i,
L_CLONE);
1948 pixaaAddPixa(paad, pixa2,
L_INSERT);
1949 pixaDestroy(&pixa1);
1978 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
1980 if (wd <= 0 && hd <= 0)
1981 return pixaCopy(pixas,
L_CLONE);
1983 n = pixaGetCount(pixas);
1984 pixad = pixaCreate(n);
1985 for (i = 0; i < n; i++) {
1986 pix1 = pixaGetPix(pixas, i,
L_CLONE);
1987 pix2 = pixScaleToSize(pix1, wd, hd);
1988 pixCopyText(pix2, pix1);
2021 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
2023 n = pixaGetCount(pixas);
2024 pixad = pixaCreate(n);
2025 for (i = 0; i < n; i++) {
2026 pix1 = pixaGetPix(pixas, i,
L_CLONE);
2027 pix2 = pixScaleToSizeRel(pix1, delw, delh);
2031 L_WARNING(
"relative scale to size failed; use a copy\n", __func__);
2032 pixaAddPix(pixad, pix1,
L_COPY);
2064 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
2065 if (scalex <= 0.0 || scaley <= 0.0)
2066 return (
PIXA *)ERROR_PTR(
"invalid scaling parameters", __func__, NULL);
2068 n = pixaGetCount(pixas);
2069 pixad = pixaCreate(n);
2070 for (i = 0; i < n; i++) {
2071 pix1 = pixaGetPix(pixas, i,
L_CLONE);
2072 pix2 = pixScale(pix1, scalex, scaley);
2073 pixCopyText(pix2, pix1);
2078 boxa1 = pixaGetBoxa(pixas,
L_CLONE);
2079 nb = boxaGetCount(boxa1);
2081 boxa2 = boxaTransform(boxa1, 0, 0, scalex, scaley);
2082 pixaSetBoxa(pixad, boxa2,
L_INSERT);
2084 boxaDestroy(&boxa1);
2113 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
2114 if (scalex <= 0.0 || scaley <= 0.0)
2115 return (
PIXA *)ERROR_PTR(
"invalid scaling parameters", __func__, NULL);
2117 n = pixaGetCount(pixas);
2118 pixad = pixaCreate(n);
2119 for (i = 0; i < n; i++) {
2120 pix1 = pixaGetPix(pixas, i,
L_CLONE);
2121 pix2 = pixScaleBySampling(pix1, scalex, scaley);
2122 pixCopyText(pix2, pix1);
2127 boxa1 = pixaGetBoxa(pixas,
L_CLONE);
2128 nb = boxaGetCount(boxa1);
2130 boxa2 = boxaTransform(boxa1, 0, 0, scalex, scaley);
2131 pixaSetBoxa(pixad, boxa2,
L_INSERT);
2133 boxaDestroy(&boxa1);
2179 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
2182 return (
PIXA *)ERROR_PTR(
"invalid type", __func__, NULL);
2184 return (
PIXA *)ERROR_PTR(
"invalid incolor", __func__, NULL);
2185 if (L_ABS(angle) < MinAngleToRotate)
2186 return pixaCopy(pixas,
L_COPY);
2188 n = pixaGetCount(pixas);
2189 if ((pixad = pixaCreate(n)) == NULL)
2190 return (
PIXA *)ERROR_PTR(
"pixad not made", __func__, NULL);
2191 boxa = pixaGetBoxa(pixad,
L_COPY);
2192 pixaSetBoxa(pixad, boxa,
L_INSERT);
2193 for (i = 0; i < n; i++) {
2194 if ((pixs = pixaGetPix(pixas, i,
L_CLONE)) == NULL) {
2195 pixaDestroy(&pixad);
2196 return (
PIXA *)ERROR_PTR(
"pixs not found", __func__, NULL);
2198 pixd = pixRotate(pixs, angle, type, incolor, width, height);
2225l_int32 i, n, nb, w, h;
2231 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
2232 if (rotation < 0 || rotation > 3)
2233 return (
PIXA *)ERROR_PTR(
"rotation not in {0,1,2,3}", __func__, NULL);
2235 return pixaCopy(pixas,
L_COPY);
2237 n = pixaGetCount(pixas);
2238 nb = pixaGetBoxaCount(pixas);
2239 if ((pixad = pixaCreate(n)) == NULL)
2240 return (
PIXA *)ERROR_PTR(
"pixad not made", __func__, NULL);
2241 for (i = 0; i < n; i++) {
2242 if ((pixs = pixaGetPix(pixas, i,
L_CLONE)) == NULL) {
2243 pixaDestroy(&pixad);
2244 return (
PIXA *)ERROR_PTR(
"pixs not found", __func__, NULL);
2246 pixd = pixRotateOrth(pixs, rotation);
2249 boxs = pixaGetBox(pixas, i,
L_COPY);
2250 pixGetDimensions(pixs, &w, &h, NULL);
2251 boxd = boxRotateOrth(boxs, w, h, rotation);
2283 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
2284 if (hshift == 0 && vshift == 0)
2285 return pixaCopy(pixas,
L_COPY);
2287 n = pixaGetCount(pixas);
2288 nb = pixaGetBoxaCount(pixas);
2289 if ((pixad = pixaCreate(n)) == NULL)
2290 return (
PIXA *)ERROR_PTR(
"pixad not made", __func__, NULL);
2291 for (i = 0; i < n; i++) {
2292 if ((pixs = pixaGetPix(pixas, i,
L_CLONE)) == NULL) {
2293 pixaDestroy(&pixad);
2294 return (
PIXA *)ERROR_PTR(
"pixs not found", __func__, NULL);
2296 pixd = pixTranslate(NULL, pixs, hshift, vshift, incolor);
2301 boxas = pixaGetBoxa(pixas,
L_CLONE);
2302 boxad = boxaTransform(boxas, hshift, vshift, 1.0, 1.0);
2303 pixaSetBoxa(pixad, boxad,
L_INSERT);
2304 boxaDestroy(&boxas);
2358 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, pixad);
2359 if (left < 0 || right < 0 || top < 0 || bot < 0)
2360 return (
PIXA *)ERROR_PTR(
"negative border added!", __func__, pixad);
2361 if (pixad && (pixad != pixas))
2362 return (
PIXA *)ERROR_PTR(
"pixad defined but != pixas", __func__, pixad);
2364 n = pixaGetCount(pixas);
2366 pixad = pixaCreate(n);
2367 for (i = 0; i < n; i++) {
2368 pixs = pixaGetPix(pixas, i,
L_CLONE);
2369 pixd = pixAddBorderGeneral(pixs, left, right, top, bot, val);
2371 pixaReplacePix(pixad, i, pixd, NULL);
2377 nbox = pixaGetBoxaCount(pixas);
2378 boxad = pixaGetBoxa(pixad,
L_CLONE);
2379 for (i = 0; i < nbox; i++) {
2380 if ((box = pixaGetBox(pixas, i,
L_COPY)) == NULL) {
2381 L_WARNING(
"box %d not found\n", __func__, i);
2384 boxAdjustSides(box, box, -left, right, -top, bot);
2386 boxaReplaceBox(boxad, i, box);
2390 boxaDestroy(&boxad);
2417l_int32 i, j, m, mb, n;
2419NUMA *naindex = NULL;
2423 if (pnaindex) *pnaindex = NULL;
2425 return (
PIXA *)ERROR_PTR(
"paa not defined", __func__, NULL);
2427 return (
PIXA *)ERROR_PTR(
"invalid copyflag", __func__, NULL);
2430 naindex = numaCreate(0);
2431 *pnaindex = naindex;
2434 n = pixaaGetCount(paa, NULL);
2435 pixa = pixaCreate(n);
2436 for (i = 0; i < n; i++) {
2437 pixat = pixaaGetPixa(paa, i,
L_CLONE);
2438 m = pixaGetCount(pixat);
2439 mb = pixaGetBoxaCount(pixat);
2440 for (j = 0; j < m; j++) {
2441 pix = pixaGetPix(pixat, j, copyflag);
2444 box = pixaGetBox(pixat, j, copyflag);
2448 numaAddNumber(naindex, i);
2450 pixaDestroy(&pixat);
2472l_int32 minw, minh, maxw, maxh, minpw, minph, maxpw, maxph, i, n;
2475 if (pminw) *pminw = 0;
2476 if (pminh) *pminh = 0;
2477 if (pmaxw) *pmaxw = 0;
2478 if (pmaxh) *pmaxh = 0;
2480 return ERROR_INT(
"paa not defined", __func__, 1);
2481 if (!pminw && !pmaxw && !pminh && !pmaxh)
2482 return ERROR_INT(
"no data can be returned", __func__, 1);
2484 minw = minh = 100000000;
2486 n = pixaaGetCount(paa, NULL);
2487 for (i = 0; i < n; i++) {
2488 pixa = pixaaGetPixa(paa, i,
L_CLONE);
2501 if (pminw) *pminw = minw;
2502 if (pminh) *pminh = minh;
2503 if (pmaxw) *pmaxw = maxw;
2504 if (pmaxh) *pmaxh = maxh;
2524l_int32 minw, minh, maxw, maxh, i, n, w, h;
2527 if (pminw) *pminw = 0;
2528 if (pminh) *pminh = 0;
2529 if (pmaxw) *pmaxw = 0;
2530 if (pmaxh) *pmaxh = 0;
2532 return ERROR_INT(
"pixa not defined", __func__, 1);
2533 if (!pminw && !pmaxw && !pminh && !pmaxh)
2534 return ERROR_INT(
"no data can be returned", __func__, 1);
2536 minw = minh = 1000000;
2538 n = pixaGetCount(pixa);
2539 for (i = 0; i < n; i++) {
2540 pix = pixaGetPix(pixa, i,
L_CLONE);
2541 w = pixGetWidth(pix);
2542 h = pixGetHeight(pix);
2554 if (pminw) *pminw = minw;
2555 if (pminh) *pminh = minh;
2556 if (pmaxw) *pmaxw = maxw;
2557 if (pmaxh) *pmaxh = maxh;
2595 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
2597 return (
PIXA *)ERROR_PTR(
"pixs not defined", __func__, NULL);
2599 n = pixaGetCount(pixas);
2600 if ((pixad = pixaCreate(n)) == NULL)
2601 return (
PIXA *)ERROR_PTR(
"pixad not made", __func__, NULL);
2603 for (i = 0; i < n; i++) {
2604 pix = pixaGetPix(pixas, i,
L_CLONE);
2605 box = pixaGetBox(pixas, i,
L_COPY);
2606 pixc = pixClipRectangle(pixs, box, NULL);
2607 pixAnd(pixc, pixc, pix);
2641 if (ppixad) *ppixad = NULL;
2642 if (pboxa) *pboxa = NULL;
2644 return ERROR_INT(
"pixas not defined", __func__, 1);
2645 if (!ppixad && !pboxa)
2646 return ERROR_INT(
"no output requested", __func__, 1);
2648 n = pixaGetCount(pixas);
2649 if (ppixad) *ppixad = pixaCreate(n);
2650 if (pboxa) *pboxa = boxaCreate(n);
2651 for (i = 0; i < n; i++) {
2652 pix1 = pixaGetPix(pixas, i,
L_CLONE);
2653 pixClipToForeground(pix1, &pix2, &box1);
2656 pixaAddPix(*ppixad, pix2,
L_INSERT);
2660 boxaAddBox(*pboxa, box1,
L_INSERT);
2691l_int32 hascolor, maxdepth;
2694 return ERROR_INT(
"&depth not defined", __func__, 1);
2697 return ERROR_INT(
"pixa not defined", __func__, 1);
2726l_int32 i, n, hascolor, d;
2731 return ERROR_INT(
"&hascolor not defined", __func__, 1);
2734 return ERROR_INT(
"pixa not defined", __func__, 1);
2736 n = pixaGetCount(pixa);
2738 for (i = 0; i < n; i++) {
2739 pix = pixaGetPix(pixa, i,
L_CLONE);
2740 if ((cmap = pixGetColormap(pix)) != NULL)
2741 pixcmapHasColor(cmap, &hascolor);
2742 d = pixGetDepth(pix);
2744 if (d == 32 || hascolor == 1) {
2770 return ERROR_INT(
"&hascmap not defined", __func__, 1);
2773 return ERROR_INT(
"pixa not defined", __func__, 1);
2775 n = pixaGetCount(pixa);
2776 for (i = 0; i < n; i++) {
2777 pix = pixaGetPix(pixa, i,
L_CLONE);
2778 cmap = pixGetColormap(pix);
2806 if (pmaxdepth) *pmaxdepth = 0;
2807 if (psame) *psame = TRUE;
2808 if (!pmaxdepth && !psame)
return 0;
2810 return ERROR_INT(
"pixa not defined", __func__, 1);
2811 if ((n = pixaGetCount(pixa)) == 0)
2812 return ERROR_INT(
"pixa is empty", __func__, 1);
2816 for (i = 0; i < n; i++) {
2817 pixaGetPixDimensions(pixa, i, NULL, NULL, &d);
2822 if (d > maxd) maxd = d;
2825 if (pmaxdepth) *pmaxdepth = maxd;
2826 if (psame) *psame = same;
2852l_int32 i, n, depth, same, hascmap, maxdepth;
2858 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
2859 if ((n = pixaGetCount(pixas)) == 0)
2860 return (
PIXA *)ERROR_PTR(
"no components", __func__, NULL);
2867 pixa1 = pixaCreate(n);
2868 for (i = 0; i < n; i++) {
2869 pix1 = pixaGetPix(pixas, i,
L_CLONE);
2871 pix2 = pixConvertTo32(pix1);
2873 pix2 = pixConvertTo8(pix1, 0);
2878 pixa1 = pixaCopy(pixas,
L_CLONE);
2883 pixad = pixaCreate(n);
2884 for (i = 0; i < n; i++) {
2885 pix1 = pixaGetPix(pixa1, i,
L_CLONE);
2887 pix2 = pixConvertTo8(pix1, 0);
2889 pix2 = pixConvertTo32(pix1);
2894 pixad = pixaCopy(pixa1,
L_CLONE);
2897 boxa = pixaGetBoxa(pixas,
L_COPY);
2898 pixaSetBoxa(pixad, boxa,
L_INSERT);
2899 pixaDestroy(&pixa1);
2929 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
2930 if ((n = pixaGetCount(pixas)) == 0)
2931 return (
PIXA *)ERROR_PTR(
"no components", __func__, NULL);
2932 if (depth != 8 && depth != 32)
2933 return (
PIXA *)ERROR_PTR(
"depth not 8 or 32", __func__, NULL);
2938 L_WARNING(
"All pix are 1 bpp; converting to %d bpp\n", __func__, depth);
2940 L_WARNING(
"Lossy conversion: max rendering depth %d > input %d\n",
2941 __func__, maxd, depth);
2943 pixad = pixaCreate(n);
2944 for (i = 0; i < n; i++) {
2945 pix1 = pixaGetPix(pixas, i,
L_CLONE);
2947 pix2 = (pixGetDepth(pix1) == 32) ? pixClone(pix1) :
2948 pixConvertTo32(pix1);
2950 pix2 = pixConvertTo8(pix1, 0);
2956 boxa = pixaGetBoxa(pixas,
L_COPY);
2957 pixaSetBoxa(pixad, boxa,
L_INSERT);
2997l_int32 i, j, n, empty1, empty2, same, sameboxa;
3002 if (pnaindex) *pnaindex = NULL;
3004 return ERROR_INT(
"&same not defined", __func__, 1);
3008 if (!pixa1 || !pixa2)
3009 return ERROR_INT(
"pixa1 and pixa2 not both defined", __func__, 1);
3010 n = pixaGetCount(pixa1);
3011 if (n != pixaGetCount(pixa2))
3016 boxa1 = pixaGetBoxa(pixa1,
L_CLONE);
3017 boxa2 = pixaGetBoxa(pixa2,
L_CLONE);
3018 empty1 = (boxaGetCount(boxa1) == 0) ? 1 : 0;
3019 empty2 = (boxaGetCount(boxa2) == 0) ? 1 : 0;
3020 if (!empty1 && !empty2) {
3021 boxaEqual(boxa1, boxa2, maxdist, &na, &sameboxa);
3023 boxaDestroy(&boxa1);
3024 boxaDestroy(&boxa2);
3029 boxaDestroy(&boxa1);
3030 boxaDestroy(&boxa2);
3031 if ((!empty1 && empty2) || (empty1 && !empty2))
3034 for (i = 0; i < n; i++) {
3035 pix1 = pixaGetPix(pixa1, i,
L_CLONE);
3037 numaGetIValue(na, i, &j);
3040 pix2 = pixaGetPix(pixa2, j,
L_CLONE);
3041 pixEqual(pix1, pix2, &same);
3081 return ERROR_INT(
"pixa not defined", __func__, 1);
3082 if ((n = pixaGetCount(pixa)) == 0) {
3083 L_INFO(
"pixa contains no pix\n", __func__);
3087 boxa = boxaCreate(n);
3089 for (i = 0; i < n; i++) {
3090 pix = pixaGetPix(pixa, i,
L_CLONE);
3091 pixGetDimensions(pix, &w, &h, NULL);
3092 box = boxCreate(0, 0, w, h);
@ L_SORT_BY_MIN_DIMENSION
@ L_SORT_BY_MAX_DIMENSION
l_ok pixaGetRenderingDepth(PIXA *pixa, l_int32 *pdepth)
pixaGetRenderingDepth()
PIXA * pixaSelectByNumConnComp(PIXA *pixas, l_int32 nmin, l_int32 nmax, l_int32 connectivity, l_int32 *pchanged)
pixaSelectByNumConnComp()
PIXA * pixaAddBorderGeneral(PIXA *pixad, PIXA *pixas, l_int32 left, l_int32 right, l_int32 top, l_int32 bot, l_uint32 val)
pixaAddBorderGeneral()
l_ok pixaHasColor(PIXA *pixa, l_int32 *phascolor)
pixaHasColor()
PIXA * pixaSelectByPerimToAreaRatio(PIXA *pixas, l_float32 thresh, l_int32 type, l_int32 *pchanged)
pixaSelectByPerimToAreaRatio()
l_ok pixaSizeRange(PIXA *pixa, l_int32 *pminw, l_int32 *pminh, l_int32 *pmaxw, l_int32 *pmaxh)
pixaSizeRange()
PIXA * pixaRotate(PIXA *pixas, l_float32 angle, l_int32 type, l_int32 incolor, l_int32 width, l_int32 height)
pixaRotate()
PIXA * pixaSelectByAreaFraction(PIXA *pixas, l_float32 thresh, l_int32 type, l_int32 *pchanged)
pixaSelectByAreaFraction()
l_ok pixaaSizeRange(PIXAA *paa, l_int32 *pminw, l_int32 *pminh, l_int32 *pmaxw, l_int32 *pmaxh)
pixaaSizeRange()
l_ok pixaSetFullSizeBoxa(PIXA *pixa)
pixaSetFullSizeBoxa()
PIXAA * pixaaSelectRange(PIXAA *paas, l_int32 first, l_int32 last, l_int32 copyflag)
pixaaSelectRange()
PIXAA * pixaaScaleToSizeVar(PIXAA *paas, NUMA *nawd, NUMA *nahd)
pixaaScaleToSizeVar()
PIXA * pixaSelectByWidthHeightRatio(PIXA *pixas, l_float32 thresh, l_int32 type, l_int32 *pchanged)
pixaSelectByWidthHeightRatio()
l_ok pixaAnyColormaps(PIXA *pixa, l_int32 *phascmap)
pixaAnyColormaps()
PIXA * pixaaFlattenToPixa(PIXAA *paa, NUMA **pnaindex, l_int32 copyflag)
pixaaFlattenToPixa()
PIXA * pixaSelectWithIndicator(PIXA *pixas, NUMA *na, l_int32 *pchanged)
pixaSelectWithIndicator()
PIXA * pixaSelectRange(PIXA *pixas, l_int32 first, l_int32 last, l_int32 copyflag)
pixaSelectRange()
PIXA * pixaConvertToGivenDepth(PIXA *pixas, l_int32 depth)
pixaConvertToGivenDepth()
PIXA * pixaSelectByPerimSizeRatio(PIXA *pixas, l_float32 thresh, l_int32 type, l_int32 *pchanged)
pixaSelectByPerimSizeRatio()
PIX * pixSelectByWidthHeightRatio(PIX *pixs, l_float32 thresh, l_int32 connectivity, l_int32 type, l_int32 *pchanged)
pixSelectByWidthHeightRatio()
l_ok pixRemoveWithIndicator(PIX *pixs, PIXA *pixa, NUMA *na)
pixRemoveWithIndicator()
PIXA * pixaScaleToSize(PIXA *pixas, l_int32 wd, l_int32 hd)
pixaScaleToSize()
l_ok pixAddWithIndicator(PIX *pixs, PIXA *pixa, NUMA *na)
pixAddWithIndicator()
PIXA * pixaConvertToSameDepth(PIXA *pixas)
pixaConvertToSameDepth()
PIXA * pixaTranslate(PIXA *pixas, l_int32 hshift, l_int32 vshift, l_int32 incolor)
pixaTranslate()
PIXA * pixaSelectBySize(PIXA *pixas, l_int32 width, l_int32 height, l_int32 type, l_int32 relation, l_int32 *pchanged)
pixaSelectBySize()
l_ok pixaClipToForeground(PIXA *pixas, PIXA **ppixad, BOXA **pboxa)
pixaClipToForeground()
PIXA * pixaRotateOrth(PIXA *pixas, l_int32 rotation)
pixaRotateOrth()
l_ok pixaGetDepthInfo(PIXA *pixa, l_int32 *pmaxdepth, l_int32 *psame)
pixaGetDepthInfo()
PIX * pixSelectByPerimToAreaRatio(PIX *pixs, l_float32 thresh, l_int32 connectivity, l_int32 type, l_int32 *pchanged)
pixSelectByPerimToAreaRatio()
PIXA * pixaBinSort(PIXA *pixas, l_int32 sorttype, l_int32 sortorder, NUMA **pnaindex, l_int32 copyflag)
pixaBinSort()
PIX * pixaRenderComponent(PIX *pixs, PIXA *pixa, l_int32 index)
pixaRenderComponent()
PIXA * pixaSelectWithString(PIXA *pixas, const char *str, l_int32 *perror)
pixaSelectWithString()
PIXA * pixaScale(PIXA *pixas, l_float32 scalex, l_float32 scaley)
pixaScale()
PIXA * pixaSortByIndex(PIXA *pixas, NUMA *naindex, l_int32 copyflag)
pixaSortByIndex()
PIXAA * pixaSort2dByIndex(PIXA *pixas, NUMAA *naa, l_int32 copyflag)
pixaSort2dByIndex()
l_ok pixaEqual(PIXA *pixa1, PIXA *pixa2, l_int32 maxdist, NUMA **pnaindex, l_int32 *psame)
pixaEqual()
PIX * pixSelectByArea(PIX *pixs, l_float32 thresh, l_int32 connectivity, l_int32 type, l_int32 *pchanged)
pixSelectByArea()
PIXA * pixaSort(PIXA *pixas, l_int32 sorttype, l_int32 sortorder, NUMA **pnaindex, l_int32 copyflag)
pixaSort()
PIXA * pixaSelectByArea(PIXA *pixas, l_float32 thresh, l_int32 type, l_int32 *pchanged)
pixaSelectByArea()
PIXAA * pixaaScaleToSize(PIXAA *paas, l_int32 wd, l_int32 hd)
pixaaScaleToSize()
PIX * pixSelectBySize(PIX *pixs, l_int32 width, l_int32 height, l_int32 connectivity, l_int32 type, l_int32 relation, l_int32 *pchanged)
pixSelectBySize()
NUMA * pixaMakeSizeIndicator(PIXA *pixa, l_int32 width, l_int32 height, l_int32 type, l_int32 relation)
pixaMakeSizeIndicator()
PIXA * pixaScaleToSizeRel(PIXA *pixas, l_int32 delw, l_int32 delh)
pixaScaleToSizeRel()
PIXA * pixaScaleBySampling(PIXA *pixas, l_float32 scalex, l_float32 scaley)
pixaScaleBySampling()
PIXA * pixaClipToPix(PIXA *pixas, PIX *pixs)
pixaClipToPix()
PIX * pixSelectByAreaFraction(PIX *pixs, l_float32 thresh, l_int32 connectivity, l_int32 type, l_int32 *pchanged)
pixSelectByAreaFraction()
PIX * pixSelectByPerimSizeRatio(PIX *pixs, l_float32 thresh, l_int32 connectivity, l_int32 type, l_int32 *pchanged)
pixSelectByPerimSizeRatio()