106 #include <config_auto.h>
111 #include "allheaders.h"
113 static const l_uint32 rmask32[] = {0x0,
114 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
115 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
116 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
117 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
118 0x0001ffff, 0x0003ffff, 0x0007ffff, 0x000fffff,
119 0x001fffff, 0x003fffff, 0x007fffff, 0x00ffffff,
120 0x01ffffff, 0x03ffffff, 0x07ffffff, 0x0fffffff,
121 0x1fffffff, 0x3fffffff, 0x7fffffff, 0xffffffff};
123 #ifndef NO_CONSOLE_IO
124 #define DEBUG_EDGES 0
147 PROCNAME(
"pixaFindDimensions");
149 if (pnaw) *pnaw = NULL;
150 if (pnah) *pnah = NULL;
152 return ERROR_INT(
"no output requested", procName, 1);
154 return ERROR_INT(
"pixa not defined", procName, 1);
159 for (i = 0; i < n; i++) {
198 PROCNAME(
"pixFindAreaPerimRatio");
201 return ERROR_INT(
"&fract not defined", procName, 1);
203 if (!pixs || pixGetDepth(pixs) != 1)
204 return ERROR_INT(
"pixs not defined or not 1 bpp", procName, 1);
215 if (!tab) LEPT_FREE(tab8);
220 *pfract = (l_float32)nfg / (l_float32)nbound;
223 if (!tab) LEPT_FREE(tab8);
249 PROCNAME(
"pixaFindPerimToAreaRatio");
252 return (
NUMA *)ERROR_PTR(
"pixa not defined", procName, NULL);
257 for (i = 0; i < n; i++) {
299 PROCNAME(
"pixFindPerimToAreaRatio");
302 return ERROR_INT(
"&fract not defined", procName, 1);
304 if (!pixs || pixGetDepth(pixs) != 1)
305 return ERROR_INT(
"pixs not defined or not 1 bpp", procName, 1);
314 if (!tab) LEPT_FREE(tab8);
320 *pfract = (l_float32)nbound / (l_float32)nfg;
323 if (!tab) LEPT_FREE(tab8);
353 PROCNAME(
"pixaFindPerimSizeRatio");
356 return (
NUMA *)ERROR_PTR(
"pixa not defined", procName, NULL);
361 for (i = 0; i < n; i++) {
400 l_int32 w, h, nbound;
403 PROCNAME(
"pixFindPerimSizeRatio");
406 return ERROR_INT(
"&ratio not defined", procName, 1);
408 if (!pixs || pixGetDepth(pixs) != 1)
409 return ERROR_INT(
"pixs not defined or not 1 bpp", procName, 1);
420 *pratio = (0.5 * nbound) / (l_float32)(w + h);
423 if (!tab) LEPT_FREE(tab8);
449 PROCNAME(
"pixaFindAreaFraction");
452 return (
NUMA *)ERROR_PTR(
"pixa not defined", procName, NULL);
457 for (i = 0; i < n; i++) {
491 PROCNAME(
"pixFindAreaFraction");
494 return ERROR_INT(
"&fract not defined", procName, 1);
496 if (!pixs || pixGetDepth(pixs) != 1)
497 return ERROR_INT(
"pixs not defined or not 1 bpp", procName, 1);
505 *pfract = (l_float32)sum / (l_float32)(w * h);
507 if (!tab) LEPT_FREE(tab8);
543 PROCNAME(
"pixaFindAreaFractionMasked");
546 return (
NUMA *)ERROR_PTR(
"pixa not defined", procName, NULL);
547 if (!pixm || pixGetDepth(pixm) != 1)
548 return (
NUMA *)ERROR_PTR(
"pixm undefined or not 1 bpp", procName, NULL);
555 for (i = 0; i < n; i++) {
577 pixDisplay(pix2, 100, 100);
617 l_int32 x, y, w, h, sum, masksum;
621 PROCNAME(
"pixFindAreaFractionMasked");
624 return ERROR_INT(
"&fract not defined", procName, 1);
626 if (!pixs || pixGetDepth(pixs) != 1)
627 return ERROR_INT(
"pixs not defined or not 1 bpp", procName, 1);
628 if (!pixm || pixGetDepth(pixm) != 1)
629 return ERROR_INT(
"pixm not defined or not 1 bpp", procName, 1);
645 if (!tab) LEPT_FREE(tab8);
649 *pfract = (l_float32)masksum / (l_float32)sum;
651 if (!tab) LEPT_FREE(tab8);
676 PROCNAME(
"pixaFindWidthHeightRatio");
679 return (
NUMA *)ERROR_PTR(
"pixa not defined", procName, NULL);
683 for (i = 0; i < n; i++) {
712 PROCNAME(
"pixaFindWidthHeightProduct");
715 return (
NUMA *)ERROR_PTR(
"pixa not defined", procName, NULL);
719 for (i = 0; i < n; i++) {
755 l_int32 w, h, nintersect, nunion;
758 PROCNAME(
"pixFindOverlapFraction");
760 if (pnoverlap) *pnoverlap = 0;
762 return ERROR_INT(
"&ratio not defined", procName, 1);
764 if (!pixs1 || pixGetDepth(pixs1) != 1)
765 return ERROR_INT(
"pixs1 not defined or not 1 bpp", procName, 1);
766 if (!pixs2 || pixGetDepth(pixs2) != 1)
767 return ERROR_INT(
"pixs2 not defined or not 1 bpp", procName, 1);
779 *pnoverlap = nintersect;
783 if (!tab) LEPT_FREE(tab8);
787 *pratio = (l_float32)nintersect / (l_float32)nunion;
818 l_int32 w, h, i, n, conforms;
824 PROCNAME(
"pixFindRectangleComps");
826 if (!pixs || pixGetDepth(pixs) != 1)
827 return (
BOXA *)ERROR_PTR(
"pixs undefined or not 1 bpp", procName, NULL);
829 return (
BOXA *)ERROR_PTR(
"dist must be >= 0", procName, NULL);
830 if (minw <= 2 * dist && minh <= 2 * dist)
831 return (
BOXA *)ERROR_PTR(
"invalid parameters", procName, NULL);
836 for (i = 0; i < n; i++) {
839 if (w < minw || h < minh) {
901 PROCNAME(
"pixConformsToRectangle");
904 return ERROR_INT(
"&conforms not defined", procName, 1);
906 if (!pixs || pixGetDepth(pixs) != 1)
907 return ERROR_INT(
"pixs not defined or not 1 bpp", procName, 1);
909 return ERROR_INT(
"dist must be >= 0", procName, 1);
911 if (w <= 2 * dist || h <= 2 * dist) {
912 L_WARNING(
"automatic conformation: distance too large\n", procName);
938 *pconforms = (empty) ? 1 : 0;
968 PROCNAME(
"pixClipRectangles");
971 return (
PIXA *)ERROR_PTR(
"pixs not defined", procName, NULL);
973 return (
PIXA *)ERROR_PTR(
"boxa not defined", procName, NULL);
977 for (i = 0; i < n; i++) {
1030 l_int32 w, h, d, bx, by, bw, bh;
1034 PROCNAME(
"pixClipRectangle");
1036 if (pboxc) *pboxc = NULL;
1038 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
1040 return (
PIX *)ERROR_PTR(
"box not defined", procName, NULL);
1045 L_WARNING(
"box doesn't overlap pix\n", procName);
1051 if ((pixd =
pixCreate(bw, bh, d)) == NULL) {
1053 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
1055 pixCopyResolution(pixd, pixs);
1057 pixCopyText(pixd, pixs);
1094 l_int32 w, h, bx, by, bw, bh, bord;
1098 PROCNAME(
"pixClipRectangleWithBorder");
1101 return (
PIX *)ERROR_PTR(
"&boxn not defined", procName, NULL);
1104 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
1106 return (
PIX *)ERROR_PTR(
"box not defined", procName, NULL);
1111 bord = L_MIN(bx, by);
1112 bord = L_MIN(bord, w - bx - bw);
1113 bord = L_MIN(bord, h - by - bh);
1114 bord = L_MIN(bord, maxbord);
1168 l_int32 wm, hm, index, rval, gval, bval;
1174 PROCNAME(
"pixClipMasked");
1177 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
1178 if (!pixm || pixGetDepth(pixm) != 1)
1179 return (
PIX *)ERROR_PTR(
"pixm undefined or not 1 bpp", procName, NULL);
1188 cmap = pixGetColormap(pixd);
1229 l_int32 w1, h1, w2, h2, w, h;
1231 PROCNAME(
"pixCropToMatch");
1233 if (!ppixd1 || !ppixd2)
1234 return ERROR_INT(
"&pixd1 and &pixd2 not both defined", procName, 1);
1235 *ppixd1 = *ppixd2 = NULL;
1236 if (!pixs1 || !pixs2)
1237 return ERROR_INT(
"pixs1 and pixs2 not defined", procName, 1);
1246 if (*ppixd1 == NULL || *ppixd2 == NULL)
1247 return ERROR_INT(
"cropped image failure", procName, 1);
1271 l_int32 ws, hs, wd, hd, d;
1274 PROCNAME(
"pixCropToSize");
1277 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
1280 if (ws <= w && hs <= h)
1285 if ((pixd =
pixCreate(wd, hd, d)) == NULL)
1286 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
1287 pixCopyResolution(pixd, pixs);
1289 pixCopyText(pixd, pixs);
1290 pixCopyInputFormat(pixd, pixs);
1326 l_int32 i, j, ws, hs, d;
1329 PROCNAME(
"pixResizeToMatch");
1332 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
1333 if (!pixt && (w <= 0 || h <= 0))
1334 return (
PIX *)ERROR_PTR(
"both w and h not > 0", procName, NULL);
1339 if (ws == w && hs == h)
1342 if ((pixd =
pixCreate(w, h, d)) == NULL)
1343 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
1344 pixCopyResolution(pixd, pixs);
1346 pixCopyText(pixd, pixs);
1347 pixCopyInputFormat(pixd, pixs);
1349 if (ws >= w && hs >= h)
1354 for (j = ws; j < w; j++)
1358 for (i = hs; i < h; i++)
1390 pixSelectComponentBySize(
PIX *pixs,
1393 l_int32 connectivity,
1396 l_int32 n, empty, sorttype, index;
1400 PIXA *pixa1, *pixa2;
1402 PROCNAME(
"pixSelectComponentBySize");
1404 if (pbox) *pbox = NULL;
1405 if (!pixs || pixGetDepth(pixs) != 1)
1406 return (
PIX *)ERROR_PTR(
"pixs undefined or not 1 bpp", procName, NULL);
1418 return (
PIX *)ERROR_PTR(
"invalid selection type", procName, NULL);
1419 if (connectivity != 4 && connectivity != 8)
1420 return (
PIX *)ERROR_PTR(
"connectivity not 4 or 8", procName, NULL);
1423 return (
PIX *)ERROR_PTR(
"no foreground pixels", procName, NULL);
1427 if (rankorder < 0 || rankorder >= n)
1463 pixFilterComponentBySize(
PIX *pixs,
1466 l_int32 connectivity,
1473 PROCNAME(
"pixFilterComponentBySize");
1475 if (!pixs || pixGetDepth(pixs) != 1)
1476 return (
PIX *)ERROR_PTR(
"pixs undefined or not 1 bpp", procName, NULL);
1478 pix1 = pixSelectComponentBySize(pixs, rankorder, type, connectivity, &box);
1481 return (
PIX *)ERROR_PTR(
"pix1 not made", procName, NULL);
1538 PROCNAME(
"pixMakeSymmetricMask");
1540 if (w <= 0 || h <= 0)
1541 return (
PIX *)ERROR_PTR(
"mask size 0", procName, NULL);
1542 if (hf < 0.0 || hf > 1.0)
1543 return (
PIX *)ERROR_PTR(
"invalid horiz fractions", procName, NULL);
1544 if (vf < 0.0 || vf > 1.0)
1545 return (
PIX *)ERROR_PTR(
"invalid vert fractions", procName, NULL);
1552 return (
PIX *)ERROR_PTR(
"invalid type", procName, NULL);
1596 l_int32 h1, h2, v1, v2;
1599 PROCNAME(
"pixMakeFrameMask");
1601 if (w <= 0 || h <= 0)
1602 return (
PIX *)ERROR_PTR(
"mask size 0", procName, NULL);
1603 if (hf1 < 0.0 || hf1 > 1.0 || hf2 < 0.0 || hf2 > 1.0)
1604 return (
PIX *)ERROR_PTR(
"invalid horiz fractions", procName, NULL);
1605 if (vf1 < 0.0 || vf1 > 1.0 || vf2 < 0.0 || vf2 > 1.0)
1606 return (
PIX *)ERROR_PTR(
"invalid vert fractions", procName, NULL);
1607 if (hf1 > hf2 || vf1 > vf2)
1608 return (
PIX *)ERROR_PTR(
"invalid relative sizes", procName, NULL);
1613 if (hf1 == 0.0 && vf1 == 0.0 && hf2 == 1.0 && vf2 == 1.0) {
1617 if (hf1 == hf2 && vf1 == vf2) {
1627 if (hf2 < 1.0 && vf2 < 1.0)
1658 l_int32 empty, same, niters;
1662 PROCNAME(
"pixMakeCoveringOfRectangles");
1664 if (!pixs || pixGetDepth(pixs) != 1)
1665 return (
PIX *)ERROR_PTR(
"pixs undefined or not 1 bpp", procName, NULL);
1667 return (
PIX *)ERROR_PTR(
"maxiters must be >= 0", procName, NULL);
1668 if (maxiters == 0) maxiters = 50;
1672 if (empty)
return pix1;
1678 if (maxiters == 1)
return pix1;
1681 while (niters < maxiters) {
1690 L_INFO(
"%d iterations\n", procName, niters - 1);
1694 L_INFO(
"maxiters = %d reached\n", procName, niters);
1732 l_int32 w1, h1, w2, h2, empty, count1, count3;
1735 PROCNAME(
"pixFractionFgInMask");
1738 return ERROR_INT(
"&fract not defined", procName, 1);
1740 if (!pix1 || pixGetDepth(pix1) != 1)
1741 return ERROR_INT(
"pix1 not defined or not 1 bpp", procName, 1);
1742 if (!pix2 || pixGetDepth(pix2) != 1)
1743 return ERROR_INT(
"pix2 not defined or not 1 bpp", procName, 1);
1747 if (w1 != w2 || h1 != h2) {
1748 L_INFO(
"sizes unequal: (w1,w2) = (%d,%d), (h1,h2) = (%d,%d)\n",
1749 procName, w1, w2, h1, h2);
1752 if (empty)
return 0;
1754 if (empty)
return 0;
1757 pixAnd(pix3, pix3, pix2);
1760 *pfract = (l_float32)count3 / (l_float32)count1;
1788 l_int32 w, h, wpl, nfullwords, extra, i, j;
1789 l_int32 minx, miny, maxx, maxy;
1790 l_uint32 result, mask;
1791 l_uint32 *data, *line;
1794 PROCNAME(
"pixClipToForeground");
1796 if (ppixd) *ppixd = NULL;
1797 if (pbox) *pbox = NULL;
1798 if (!ppixd && !pbox)
1799 return ERROR_INT(
"no output requested", procName, 1);
1800 if (!pixs || (pixGetDepth(pixs) != 1))
1801 return ERROR_INT(
"pixs not defined or not 1 bpp", procName, 1);
1804 nfullwords = w / 32;
1806 mask = ~rmask32[32 - extra];
1807 wpl = pixGetWpl(pixs);
1811 for (i = 0, miny = 0; i < h; i++, miny++) {
1812 line = data + i * wpl;
1813 for (j = 0; j < nfullwords; j++)
1816 result |= (line[j] & mask);
1824 for (i = h - 1, maxy = h - 1; i >= 0; i--, maxy--) {
1825 line = data + i * wpl;
1826 for (j = 0; j < nfullwords; j++)
1829 result |= (line[j] & mask);
1835 for (j = 0, minx = 0; j < w; j++, minx++) {
1836 for (i = 0; i < h; i++) {
1837 line = data + i * wpl;
1844 for (j = w - 1, maxx = w - 1; j >= 0; j--, maxx--) {
1845 for (i = 0; i < h; i++) {
1846 line = data + i * wpl;
1853 box =
boxCreate(minx, miny, maxx - minx + 1, maxy - miny + 1);
1887 l_int32 i, j, w, h, wpl, found;
1888 l_uint32 *data, *line;
1890 PROCNAME(
"pixTestClipToForeground");
1893 return ERROR_INT(
"&canclip not defined", procName, 1);
1895 if (!pixs || (pixGetDepth(pixs) != 1))
1896 return ERROR_INT(
"pixs not defined or not 1 bpp", procName, 1);
1901 wpl = pixGetWpl(pixs);
1903 for (j = 0; found == FALSE && j < w; j++)
1910 line = data + (h - 1) * wpl;
1912 for (j = 0; found == FALSE && j < w; j++)
1921 for (i = 0, line = data; found == FALSE && i < h; line += wpl, i++)
1929 for (i = 0, line = data; found == FALSE && i < h; line += wpl, i++)
1961 l_int32 w, h, bx, by, bw, bh, cbw, cbh, left, right, top, bottom;
1964 PROCNAME(
"pixClipBoxToForeground");
1966 if (ppixd) *ppixd = NULL;
1967 if (pboxd) *pboxd = NULL;
1968 if (!ppixd && !pboxd)
1969 return ERROR_INT(
"no output requested", procName, 1);
1970 if (!pixs || (pixGetDepth(pixs) != 1))
1971 return ERROR_INT(
"pixs not defined or not 1 bpp", procName, 1);
1978 cbw = L_MIN(bw, w - bx);
1979 cbh = L_MIN(bh, h - by);
1980 if (cbw < 0 || cbh < 0)
1981 return ERROR_INT(
"box not within image", procName, 1);
1992 boxd =
boxCreate(left, top, right - left + 1, bottom - top + 1);
2027 l_int32 bx, by, bw, bh, x, xstart, xend, y, ystart, yend, wpl;
2028 l_uint32 *data, *line;
2031 PROCNAME(
"pixScanForForeground");
2034 return ERROR_INT(
"&loc not defined", procName, 1);
2036 if (!pixs || (pixGetDepth(pixs) != 1))
2037 return ERROR_INT(
"pixs not defined or not 1 bpp", procName, 1);
2043 return ERROR_INT(
"invalid box", procName, 1);
2055 wpl = pixGetWpl(pixs);
2057 for (x = xstart; x <= xend; x++) {
2058 for (y = ystart; y <= yend; y++) {
2059 line = data + y * wpl;
2067 for (x = xend; x >= xstart; x--) {
2068 for (y = ystart; y <= yend; y++) {
2069 line = data + y * wpl;
2077 for (y = ystart; y <= yend; y++) {
2078 line = data + y * wpl;
2079 for (x = xstart; x <= xend; x++) {
2087 for (y = yend; y >= ystart; y--) {
2088 line = data + y * wpl;
2089 for (x = xstart; x <= xend; x++) {
2097 return ERROR_INT(
"invalid scanflag", procName, 1);
2147 l_int32 w, h, bx, by, bw, bh, cbw, cbh, left, right, top, bottom;
2148 l_int32 lfound, rfound, tfound, bfound, change;
2151 PROCNAME(
"pixClipBoxToEdges");
2153 if (ppixd) *ppixd = NULL;
2154 if (pboxd) *pboxd = NULL;
2155 if (!ppixd && !pboxd)
2156 return ERROR_INT(
"no output requested", procName, 1);
2157 if (!pixs || (pixGetDepth(pixs) != 1))
2158 return ERROR_INT(
"pixs not defined or not 1 bpp", procName, 1);
2159 if (lowthresh < 1 || highthresh < 1 ||
2160 lowthresh > highthresh || maxwidth < 1)
2161 return ERROR_INT(
"invalid thresholds", procName, 1);
2162 factor = L_MIN(1, factor);
2164 if (lowthresh == 1 && highthresh == 1)
2170 cbw = L_MIN(bw, w - bx);
2171 cbh = L_MIN(bh, h - by);
2172 if (cbw < 0 || cbh < 0)
2173 return ERROR_INT(
"box not within image", procName, 1);
2179 lfound = rfound = tfound = bfound = 0;
2180 while (!lfound || !rfound || !tfound || !bfound) {
2216 lept_stderr(
"iter: %d %d %d %d\n", lfound, rfound, tfound, bfound);
2219 if (change == 0)
break;
2224 return ERROR_INT(
"not all edges found", procName, 1);
2226 boxd =
boxCreate(left, top, right - left + 1, bottom - top + 1);
2277 l_int32 bx, by, bw, bh, foundmin, loc, sum, wpl;
2278 l_int32 x, xstart, xend, y, ystart, yend;
2279 l_uint32 *data, *line;
2282 PROCNAME(
"pixScanForEdge");
2285 return ERROR_INT(
"&ploc not defined", procName, 1);
2287 if (!pixs || (pixGetDepth(pixs) != 1))
2288 return ERROR_INT(
"pixs not defined or not 1 bpp", procName, 1);
2289 if (lowthresh < 1 || highthresh < 1 ||
2290 lowthresh > highthresh || maxwidth < 1)
2291 return ERROR_INT(
"invalid thresholds", procName, 1);
2292 factor = L_MIN(1, factor);
2298 return ERROR_INT(
"invalid box", procName, 1);
2310 wpl = pixGetWpl(pixs);
2313 for (x = xstart; x <= xend; x++) {
2315 for (y = ystart; y <= yend; y += factor) {
2316 line = data + y * wpl;
2320 if (!foundmin && sum < lowthresh)
2326 if (sum >= highthresh) {
2330 if (x - loc < maxwidth) {
2339 for (x = xend; x >= xstart; x--) {
2341 for (y = ystart; y <= yend; y += factor) {
2342 line = data + y * wpl;
2346 if (!foundmin && sum < lowthresh)
2352 if (sum >= highthresh) {
2356 if (loc - x < maxwidth) {
2365 for (y = ystart; y <= yend; y++) {
2367 line = data + y * wpl;
2368 for (x = xstart; x <= xend; x += factor) {
2372 if (!foundmin && sum < lowthresh)
2378 if (sum >= highthresh) {
2382 if (y - loc < maxwidth) {
2391 for (y = yend; y >= ystart; y--) {
2393 line = data + y * wpl;
2394 for (x = xstart; x <= xend; x += factor) {
2398 if (!foundmin && sum < lowthresh)
2404 if (sum >= highthresh) {
2406 lept_stderr(
"Bottom: y = %d, loc = %d\n", y, loc);
2408 if (loc - y < maxwidth) {
2417 return ERROR_INT(
"invalid scanflag", procName, 1);
2456 l_int32 i, w, h, d, xmin, ymin, xmax, ymax, npts, direction;
2463 PROCNAME(
"pixExtractOnLine");
2466 return (
NUMA *)ERROR_PTR(
"pixs not defined", procName, NULL);
2468 if (d != 1 && d != 8)
2469 return (
NUMA *)ERROR_PTR(
"d not 1 or 8 bpp", procName, NULL);
2470 if (pixGetColormap(pixs))
2471 return (
NUMA *)ERROR_PTR(
"pixs has a colormap", procName, NULL);
2473 L_WARNING(
"factor must be >= 1; setting to 1\n", procName);
2478 x1 = L_MAX(0, L_MIN(x1, w - 1));
2479 x2 = L_MAX(0, L_MIN(x2, w - 1));
2480 y1 = L_MAX(0, L_MIN(y1, h - 1));
2481 y2 = L_MAX(0, L_MIN(y2, h - 1));
2483 if (x1 == x2 && y1 == y2) {
2499 xmin = L_MIN(x1, x2);
2500 xmax = L_MAX(x1, x2);
2502 for (i = xmin; i <= xmax; i += factor) {
2507 ymin = L_MIN(y1, y2);
2508 ymax = L_MAX(y1, y2);
2510 for (i = ymin; i <= ymax; i += factor) {
2515 slope = (l_float64)((y2 - y1) / (x2 - x1));
2516 if (L_ABS(slope) < 1.0) {
2517 xmin = L_MIN(x1, x2);
2518 xmax = L_MAX(x1, x2);
2519 ymin = (xmin == x1) ? y1 : y2;
2520 ymax = (ymin == y1) ? y2 : y1;
2524 ymin = L_MIN(y1, y2);
2525 ymax = L_MAX(y1, y2);
2526 xmin = (ymin == y1) ? x1 : x2;
2527 xmax = (xmin == x1) ? x2 : x1;
2532 for (i = 0; i < npts; i += factor) {
2576 l_int32 i, j, w, h, d, direction, count, wpl;
2577 l_uint32 *data, *line;
2580 PROCNAME(
"pixAverageOnLine");
2583 return ERROR_INT(
"pixs not defined", procName, 1);
2585 if (d != 1 && d != 8)
2586 return ERROR_INT(
"d not 1 or 8 bpp", procName, 1);
2587 if (pixGetColormap(pixs))
2588 return ERROR_INT(
"pixs has a colormap", procName, 1);
2589 if (x1 > x2 || y1 > y2)
2590 return ERROR_INT(
"x1 > x2 or y1 > y2", procName, 1);
2594 x2 = L_MIN(w - 1, x2);
2595 y1 = L_MAX(0, L_MIN(y1, h - 1));
2597 }
else if (x1 == x2) {
2599 y2 = L_MIN(h - 1, y2);
2600 x1 = L_MAX(0, L_MIN(x1, w - 1));
2603 return ERROR_INT(
"line neither horiz nor vert", procName, 1);
2607 L_WARNING(
"factor must be >= 1; setting to 1\n", procName);
2612 wpl = pixGetWpl(pixs);
2616 line = data + y1 * wpl;
2617 for (j = x1, count = 0; j <= x2; count++, j += factor) {
2624 for (i = y1, count = 0; i <= y2; count++, i += factor) {
2625 line = data + i * wpl;
2633 return sum / (l_float32)count;
2676 l_int32 i, j, w, h, d, start, end;
2681 PROCNAME(
"pixAverageIntensityProfile");
2684 return (
NUMA *)ERROR_PTR(
"pixs not defined", procName, NULL);
2685 if (fract < 0.0 || fract > 1.0)
2686 return (
NUMA *)ERROR_PTR(
"fract < 0.0 or > 1.0", procName, NULL);
2688 return (
NUMA *)ERROR_PTR(
"invalid direction", procName, NULL);
2689 if (first < 0) first = 0;
2691 return (
NUMA *)ERROR_PTR(
"last must be >= first", procName, NULL);
2693 L_WARNING(
"factor1 must be >= 1; setting to 1\n", procName);
2697 L_WARNING(
"factor2 must be >= 1; setting to 1\n", procName);
2702 if (pixGetColormap(pixs))
2715 start = (l_int32)(0.5 * (1.0 - fract) * (l_float32)w);
2718 L_WARNING(
"last > h - 1; clipping\n", procName);
2721 for (i = first; i <= last; i += factor2) {
2726 start = (l_int32)(0.5 * (1.0 - fract) * (l_float32)h);
2729 L_WARNING(
"last > w - 1; clipping\n", procName);
2732 for (j = first; j <= last; j += factor2) {
2788 l_int32 minreversal,
2792 l_int32 i, j, w, h, d, start, end, nr;
2796 PROCNAME(
"pixReversalProfile");
2799 return (
NUMA *)ERROR_PTR(
"pixs not defined", procName, NULL);
2800 if (fract < 0.0 || fract > 1.0)
2801 return (
NUMA *)ERROR_PTR(
"fract < 0.0 or > 1.0", procName, NULL);
2803 return (
NUMA *)ERROR_PTR(
"invalid direction", procName, NULL);
2804 if (first < 0) first = 0;
2806 return (
NUMA *)ERROR_PTR(
"last must be >= first", procName, NULL);
2808 L_WARNING(
"factor1 must be >= 1; setting to 1\n", procName);
2812 L_WARNING(
"factor2 must be >= 1; setting to 1\n", procName);
2817 if (pixGetColormap(pixs))
2832 start = (l_int32)(0.5 * (1.0 - fract) * (l_float32)w);
2835 L_WARNING(
"last > h - 1; clipping\n", procName);
2838 for (i = first; i <= last; i += factor2) {
2845 start = (l_int32)(0.5 * (1.0 - fract) * (l_float32)h);
2848 L_WARNING(
"last > w - 1; clipping\n", procName);
2851 for (j = first; j <= last; j += factor2) {
2900 l_int32 i, j, w, h, cmin, cmax, maxloc, n, x, y;
2902 l_float32 norm, rootvar;
2904 l_float64 sum1, sum2, ave, var;
2908 PROCNAME(
"pixWindowedVarianceOnLine");
2911 return ERROR_INT(
"&nad not defined", procName, 1);
2913 if (!pixs || pixGetDepth(pixs) != 8)
2914 return ERROR_INT(
"pixs not defined or not 8bpp", procName, 1);
2916 return ERROR_INT(
"window size must be > 1", procName, 1);
2918 return ERROR_INT(
"invalid direction", procName, 1);
2921 if (loc < 0 || loc > maxloc)
2922 return ERROR_INT(
"invalid line position", procName, 1);
2925 cmin = L_MIN(c1, c2);
2926 cmax = L_MAX(c1, c2);
2928 cmin = L_MAX(0, L_MIN(cmin, maxloc));
2929 cmax = L_MAX(0, L_MIN(cmax, maxloc));
2930 n = cmax - cmin + 1;
2935 for (i = cmin; i <= cmax; i++)
2938 for (i = cmin; i <= cmax; i++)
2945 for (i = 0; i < n; i++) {
2957 norm = 1.0 / (l_float32)size;
2958 for (i = 0; i < n - size; i++) {
2960 for (j = 0; j < size; j++) {
2963 sum2 += (l_float64)(val) * val;
2966 var = norm * sum2 - ave * ave;
2967 rootvar = (l_float32)sqrt(var);
3022 l_int32 i, j, w, h, d, x, y, n, dir, found, minval, maxval, negloc, posloc;
3025 NUMA *namin, *namax;
3028 PROCNAME(
"pixMinMaxNearLine");
3030 if (pnamin) *pnamin = NULL;
3031 if (pnamax) *pnamax = NULL;
3032 if (pminave) *pminave = UNDEF;
3033 if (pmaxave) *pmaxave = UNDEF;
3034 if (!pnamin && !pnamax && !pminave && !pmaxave)
3035 return ERROR_INT(
"no output requested", procName, 1);
3037 return ERROR_INT(
"pixs not defined", procName, 1);
3039 if (d != 8 || pixGetColormap(pixs))
3040 return ERROR_INT(
"pixs not 8 bpp or has colormap", procName, 1);
3044 return ERROR_INT(
"invalid direction", procName, 1);
3048 dir = (L_ABS(x1 - x2) == n - 1) ? L_HORIZ : L_VERT;
3057 for (i = 0; i < n; i++) {
3062 if (dir == L_HORIZ) {
3063 if (x < 0 || x >= w)
continue;
3064 for (j = negloc; j <= posloc; j++) {
3065 if (y + j < 0 || y + j >= h)
continue;
3068 if (val < minval) minval = val;
3069 if (val > maxval) maxval = val;
3072 if (y < 0 || y >= h)
continue;
3073 for (j = negloc; j <= posloc; j++) {
3074 if (x + j < 0 || x + j >= w)
continue;
3077 if (val < minval) minval = val;
3078 if (val > maxval) maxval = val;
3092 return ERROR_INT(
"no output from this line", procName, 1);
3135 l_int32 i, j, k, m, w, h, wpl, val;
3137 l_uint32 *datas, *datad, *lines, *lined;
3140 PROCNAME(
"pixRankRowTransform");
3143 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
3144 if (pixGetDepth(pixs) != 8)
3145 return (
PIX *)ERROR_PTR(
"pixs not 8 bpp", procName, NULL);
3146 if (pixGetColormap(pixs))
3147 return (
PIX *)ERROR_PTR(
"pixs has a colormap", procName, NULL);
3153 wpl = pixGetWpl(pixs);
3154 for (i = 0; i < h; i++) {
3155 memset(histo, 0, 1024);
3156 lines = datas + i * wpl;
3157 lined = datad + i * wpl;
3158 for (j = 0; j < w; j++) {
3162 for (m = 0, j = 0; m < 256; m++) {
3163 for (k = 0; k < histo[m]; k++, j++)
3188 l_int32 i, j, k, m, w, h, val;
3190 void **lines8, **lined8;
3193 PROCNAME(
"pixRankColumnTransform");
3196 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
3197 if (pixGetDepth(pixs) != 8)
3198 return (
PIX *)ERROR_PTR(
"pixs not 8 bpp", procName, NULL);
3199 if (pixGetColormap(pixs))
3200 return (
PIX *)ERROR_PTR(
"pixs has a colormap", procName, NULL);
3206 for (j = 0; j < w; j++) {
3207 memset(histo, 0, 1024);
3208 for (i = 0; i < h; i++) {
3212 for (m = 0, i = 0; m < 256; m++) {
3213 for (k = 0; k < histo[m]; k++, i++)
#define GET_DATA_BYTE(pdata, n)
#define SET_DATA_BYTE(pdata, n, val)
#define GET_DATA_BIT(pdata, n)
void boxDestroy(BOX **pbox)
boxDestroy()
l_int32 boxaGetCount(BOXA *boxa)
boxaGetCount()
l_ok boxGetGeometry(BOX *box, l_int32 *px, l_int32 *py, l_int32 *pw, l_int32 *ph)
boxGetGeometry()
l_ok boxaAddBox(BOXA *boxa, BOX *box, l_int32 copyflag)
boxaAddBox()
void boxaDestroy(BOXA **pboxa)
boxaDestroy()
BOX * boxaGetBox(BOXA *boxa, l_int32 index, l_int32 accessflag)
boxaGetBox()
BOX * boxCreate(l_int32 x, l_int32 y, l_int32 w, l_int32 h)
boxCreate()
BOXA * boxaCreate(l_int32 n)
boxaCreate()
BOX * boxRelocateOneSide(BOX *boxd, BOX *boxs, l_int32 loc, l_int32 sideflag)
boxRelocateOneSide()
BOX * boxClipToRectangle(BOX *box, l_int32 wi, l_int32 hi)
boxClipToRectangle()
BOX * boxAdjustSides(BOX *boxd, BOX *boxs, l_int32 delleft, l_int32 delright, l_int32 deltop, l_int32 delbot)
boxAdjustSides()
PIX * pixMaskBoxa(PIX *pixd, PIX *pixs, BOXA *boxa, l_int32 op)
pixMaskBoxa()
PIXCMAP * pixcmapCreate(l_int32 depth)
pixcmapCreate()
l_ok pixcmapGetNearestIndex(PIXCMAP *cmap, l_int32 rval, l_int32 gval, l_int32 bval, l_int32 *pindex)
pixcmapGetNearestIndex()
l_ok pixcmapGetColor(PIXCMAP *cmap, l_int32 index, l_int32 *prval, l_int32 *pgval, l_int32 *pbval)
pixcmapGetColor()
l_ok pixEqual(PIX *pix1, PIX *pix2, l_int32 *psame)
pixEqual()
BOXA * pixConnCompBB(PIX *pixs, l_int32 connectivity)
pixConnCompBB()
BOXA * pixConnComp(PIX *pixs, PIXA **ppixa, l_int32 connectivity)
pixConnComp()
PTA * generatePtaLine(l_int32 x1, l_int32 y1, l_int32 x2, l_int32 y2)
generatePtaLine()
PIX * pixErodeBrick(PIX *pixd, PIX *pixs, l_int32 hsize, l_int32 vsize)
pixErodeBrick()
l_ok numaAddNumber(NUMA *na, l_float32 val)
numaAddNumber()
NUMA * numaCreate(l_int32 n)
numaCreate()
void numaDestroy(NUMA **pna)
numaDestroy()
l_int32 numaGetCount(NUMA *na)
numaGetCount()
l_ok numaGetIValue(NUMA *na, l_int32 index, l_int32 *pival)
numaGetIValue()
l_ok numaSetParameters(NUMA *na, l_float32 startx, l_float32 delx)
numaSetParameters()
l_float32 * numaGetFArray(NUMA *na, l_int32 copyflag)
numaGetFArray()
l_ok numaGetSum(NUMA *na, l_float32 *psum)
numaGetSum()
l_ok numaCountReversals(NUMA *nas, l_float32 minreversal, l_int32 *pnr, l_float32 *prd)
numaCountReversals()
l_ok pixSetMaskedCmap(PIX *pixs, PIX *pixm, l_int32 x, l_int32 y, l_int32 rval, l_int32 gval, l_int32 bval)
pixSetMaskedCmap()
l_uint32 * pixGetData(PIX *pix)
pixGetData()
l_ok pixSetColormap(PIX *pix, PIXCMAP *colormap)
pixSetColormap()
void pixDestroy(PIX **ppix)
pixDestroy()
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
PIX * pixCopy(PIX *pixd, const PIX *pixs)
pixCopy()
PIX * pixCreateTemplate(const PIX *pixs)
pixCreateTemplate()
l_ok pixCopyColormap(PIX *pixd, const PIX *pixs)
pixCopyColormap()
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
PIX * pixClone(PIX *pixs)
pixClone()
void ** pixGetLinePtrs(PIX *pix, l_int32 *psize)
pixGetLinePtrs()
l_ok pixGetPixel(PIX *pix, l_int32 x, l_int32 y, l_uint32 *pval)
pixGetPixel()
l_ok pixSetAll(PIX *pix)
pixSetAll()
l_ok pixSetOrClearBorder(PIX *pixs, l_int32 left, l_int32 right, l_int32 top, l_int32 bot, l_int32 op)
pixSetOrClearBorder()
l_ok composeRGBPixel(l_int32 rval, l_int32 gval, l_int32 bval, l_uint32 *ppixel)
composeRGBPixel()
void extractRGBValues(l_uint32 pixel, l_int32 *prval, l_int32 *pgval, l_int32 *pbval)
extractRGBValues()
l_ok pixSetBlackOrWhite(PIX *pixs, l_int32 op)
pixSetBlackOrWhite()
l_ok pixZero(PIX *pix, l_int32 *pempty)
pixZero()
PIX * pixInvert(PIX *pixd, PIX *pixs)
pixInvert()
l_ok pixPaintThroughMask(PIX *pixd, PIX *pixm, l_int32 x, l_int32 y, l_uint32 val)
pixPaintThroughMask()
l_ok pixCountPixels(PIX *pixs, l_int32 *pcount, l_int32 *tab8)
pixCountPixels()
PIX * pixAnd(PIX *pixd, PIX *pixs1, PIX *pixs2)
pixAnd()
l_int32 * makePixelSumTab8(void)
makePixelSumTab8()
PIX * pixXor(PIX *pixd, PIX *pixs1, PIX *pixs2)
pixXor()
PIX * pixResizeToMatch(PIX *pixs, PIX *pixt, l_int32 w, l_int32 h)
pixResizeToMatch()
l_ok pixWindowedVarianceOnLine(PIX *pixs, l_int32 dir, l_int32 loc, l_int32 c1, l_int32 c2, l_int32 size, NUMA **pnad)
pixWindowedVarianceOnLine()
l_ok pixFractionFgInMask(PIX *pix1, PIX *pix2, l_float32 *pfract)
pixFractionFgInMask()
l_ok pixFindAreaPerimRatio(PIX *pixs, l_int32 *tab, l_float32 *pfract)
pixFindAreaPerimRatio()
NUMA * pixAverageIntensityProfile(PIX *pixs, l_float32 fract, l_int32 dir, l_int32 first, l_int32 last, l_int32 factor1, l_int32 factor2)
pixAverageIntensityProfile()
l_ok pixFindPerimToAreaRatio(PIX *pixs, l_int32 *tab, l_float32 *pfract)
pixFindPerimToAreaRatio()
NUMA * pixaFindAreaFraction(PIXA *pixa)
pixaFindAreaFraction()
PIX * pixCropToSize(PIX *pixs, l_int32 w, l_int32 h)
pixCropToSize()
NUMA * pixaFindPerimSizeRatio(PIXA *pixa)
pixaFindPerimSizeRatio()
PIX * pixMakeSymmetricMask(l_int32 w, l_int32 h, l_float32 hf, l_float32 vf, l_int32 type)
pixSelectComponentBySize()
NUMA * pixaFindPerimToAreaRatio(PIXA *pixa)
pixaFindPerimToAreaRatio()
PIX * pixRankRowTransform(PIX *pixs)
pixRankRowTransform()
l_ok pixFindPerimSizeRatio(PIX *pixs, l_int32 *tab, l_float32 *pratio)
pixFindPerimSizeRatio()
NUMA * pixaFindWidthHeightProduct(PIXA *pixa)
pixaFindWidthHeightProduct()
l_ok pixFindAreaFraction(PIX *pixs, l_int32 *tab, l_float32 *pfract)
pixFindAreaFraction()
l_ok pixScanForForeground(PIX *pixs, BOX *box, l_int32 scanflag, l_int32 *ploc)
pixScanForForeground()
BOXA * pixFindRectangleComps(PIX *pixs, l_int32 dist, l_int32 minw, l_int32 minh)
pixFindRectangleComps()
PIX * pixClipRectangle(PIX *pixs, BOX *box, BOX **pboxc)
pixClipRectangle()
l_ok pixClipBoxToEdges(PIX *pixs, BOX *boxs, l_int32 lowthresh, l_int32 highthresh, l_int32 maxwidth, l_int32 factor, PIX **ppixd, BOX **pboxd)
pixClipBoxToEdges()
l_float32 pixAverageOnLine(PIX *pixs, l_int32 x1, l_int32 y1, l_int32 x2, l_int32 y2, l_int32 factor)
pixAverageOnLine()
PIX * pixClipRectangleWithBorder(PIX *pixs, BOX *box, l_int32 maxbord, BOX **pboxn)
pixClipRectangleWithBorder()
l_ok pixTestClipToForeground(PIX *pixs, l_int32 *pcanclip)
pixTestClipToForeground()
PIX * pixRankColumnTransform(PIX *pixs)
pixRankColumnTransform()
l_ok pixClipBoxToForeground(PIX *pixs, BOX *boxs, PIX **ppixd, BOX **pboxd)
pixClipBoxToForeground()
l_ok pixFindAreaFractionMasked(PIX *pixs, BOX *box, PIX *pixm, l_int32 *tab, l_float32 *pfract)
pixFindAreaFractionMasked()
NUMA * pixExtractOnLine(PIX *pixs, l_int32 x1, l_int32 y1, l_int32 x2, l_int32 y2, l_int32 factor)
pixExtractOnLine()
l_ok pixScanForEdge(PIX *pixs, BOX *box, l_int32 lowthresh, l_int32 highthresh, l_int32 maxwidth, l_int32 factor, l_int32 scanflag, l_int32 *ploc)
pixScanForEdge()
l_ok pixMinMaxNearLine(PIX *pixs, l_int32 x1, l_int32 y1, l_int32 x2, l_int32 y2, l_int32 dist, l_int32 direction, NUMA **pnamin, NUMA **pnamax, l_float32 *pminave, l_float32 *pmaxave)
pixMinMaxNearLine()
PIX * pixMakeCoveringOfRectangles(PIX *pixs, l_int32 maxiters)
pixMakeCoveringOfRectangles()
NUMA * pixaFindWidthHeightRatio(PIXA *pixa)
pixaFindWidthHeightRatio()
l_ok pixClipToForeground(PIX *pixs, PIX **ppixd, BOX **pbox)
pixClipToForeground()
l_ok pixaFindDimensions(PIXA *pixa, NUMA **pnaw, NUMA **pnah)
pixaFindDimensions()
l_ok pixConformsToRectangle(PIX *pixs, BOX *box, l_int32 dist, l_int32 *pconforms)
pixConformsToRectangle()
NUMA * pixaFindAreaFractionMasked(PIXA *pixa, PIX *pixm, l_int32 debug)
pixaFindAreaFractionMasked()
PIX * pixClipMasked(PIX *pixs, PIX *pixm, l_int32 x, l_int32 y, l_uint32 outval)
pixClipMasked()
l_ok pixFindOverlapFraction(PIX *pixs1, PIX *pixs2, l_int32 x2, l_int32 y2, l_int32 *tab, l_float32 *pratio, l_int32 *pnoverlap)
pixFindOverlapFraction()
l_ok pixCropToMatch(PIX *pixs1, PIX *pixs2, PIX **ppixd1, PIX **ppixd2)
pixCropToMatch()
NUMA * pixReversalProfile(PIX *pixs, l_float32 fract, l_int32 dir, l_int32 first, l_int32 last, l_int32 minreversal, l_int32 factor1, l_int32 factor2)
pixReversalProfile()
PIXA * pixClipRectangles(PIX *pixs, BOXA *boxa)
pixClipRectangles()
PIX * pixMakeFrameMask(l_int32 w, l_int32 h, l_float32 hf1, l_float32 hf2, l_float32 vf1, l_float32 vf2)
pixMakeFrameMask()
@ L_SORT_BY_MAX_DIMENSION
@ REMOVE_CMAP_TO_GRAYSCALE
@ L_SELECT_BY_MAX_DIMENSION
l_ok pixaAddPix(PIXA *pixa, PIX *pix, l_int32 copyflag)
pixaAddPix()
void pixaDestroy(PIXA **ppixa)
pixaDestroy()
BOX * pixaGetBox(PIXA *pixa, l_int32 index, l_int32 accesstype)
pixaGetBox()
PIXA * pixaCreate(l_int32 n)
pixaCreate()
l_int32 pixaGetCount(PIXA *pixa)
pixaGetCount()
l_ok pixaAddBox(PIXA *pixa, BOX *box, l_int32 copyflag)
pixaAddBox()
l_ok pixaIsFull(PIXA *pixa, l_int32 *pfullpa, l_int32 *pfullba)
pixaIsFull()
PIX * pixaGetPix(PIXA *pixa, l_int32 index, l_int32 accesstype)
pixaGetPix()
PIXA * pixaSort(PIXA *pixas, l_int32 sorttype, l_int32 sortorder, NUMA **pnaindex, l_int32 copyflag)
pixaSort()
PIX * pixaDisplay(PIXA *pixa, l_int32 w, l_int32 h)
pixaDisplay()
PIX * pixRemoveColormap(PIX *pixs, l_int32 type)
pixRemoveColormap()
PIX * pixConvertTo8(PIX *pixs, l_int32 cmapflag)
pixConvertTo8()
l_ok ptaGetIPt(PTA *pta, l_int32 index, l_int32 *px, l_int32 *py)
ptaGetIPt()
l_ok ptaAddPt(PTA *pta, l_float32 x, l_float32 y)
ptaAddPt()
l_ok ptaGetPt(PTA *pta, l_int32 index, l_float32 *px, l_float32 *py)
ptaGetPt()
l_int32 ptaGetCount(PTA *pta)
ptaGetCount()
PTA * ptaCreate(l_int32 n)
ptaCreate()
void ptaDestroy(PTA **ppta)
ptaDestroy()
l_ok pixPlotAlongPta(PIX *pixs, PTA *pta, l_int32 outformat, const char *title)
pixPlotAlongPta()
l_ok pixRasterop(PIX *pixd, l_int32 dx, l_int32 dy, l_int32 dw, l_int32 dh, l_int32 op, PIX *pixs, l_int32 sx, l_int32 sy)
pixRasterop()
PIX * pixExtractBorderConnComps(PIX *pixs, l_int32 connectivity)
pixExtractBorderConnComps()
void lept_stderr(const char *fmt,...)
lept_stderr()