103#include <config_auto.h>
107#include "allheaders.h"
111#define M_PI 3.14159265358979323846
133 return (
PTA *)ERROR_PTR(
"ptas not defined", __func__, NULL);
135 return (
PTA *)ERROR_PTR(
"subfactor < 1", __func__, NULL);
138 n = ptaGetCount(ptas);
139 for (i = 0; i < n; i++) {
140 if (i % subfactor != 0)
continue;
141 ptaGetPt(ptas, i, &x, &y);
142 ptaAddPt(ptad, x, y);
174 return ERROR_INT(
"ptad not defined", __func__, 1);
180 n = ptaGetCount(ptas);
181 if (iend < 0 || iend >= n)
184 return ERROR_INT(
"istart > iend; no pts", __func__, 1);
186 for (i = istart; i <= iend; i++) {
187 ptaGetIPt(ptas, i, &x, &y);
188 if (ptaAddPt(ptad, x, y) == 1) {
189 L_ERROR(
"failed to add pt at i = %d\n", __func__, i);
223 return ERROR_INT(
"ptaad not defined", __func__, 1);
229 n = ptaaGetCount(ptaas);
230 if (iend < 0 || iend >= n)
233 return ERROR_INT(
"istart > iend; no pts", __func__, 1);
235 for (i = istart; i <= iend; i++) {
236 pta = ptaaGetPta(ptaas, i,
L_CLONE);
260 return (
PTA *)ERROR_PTR(
"ptas not defined", __func__, NULL);
262 n = ptaGetCount(ptas);
263 if ((ptad = ptaCreate(n)) == NULL)
264 return (
PTA *)ERROR_PTR(
"ptad not made", __func__, NULL);
265 for (i = n - 1; i >= 0; i--) {
267 ptaGetPt(ptas, i, &x, &y);
268 ptaAddPt(ptad, x, y);
270 ptaGetIPt(ptas, i, &ix, &iy);
271 ptaAddPt(ptad, ix, iy);
293 return (
PTA *)ERROR_PTR(
"ptas not defined", __func__, NULL);
295 n = ptaGetCount(ptas);
296 if ((ptad = ptaCreate(n)) == NULL)
297 return (
PTA *)ERROR_PTR(
"ptad not made", __func__, NULL);
298 for (i = 0; i < n; i++) {
299 ptaGetPt(ptas, i, &x, &y);
300 ptaAddPt(ptad, y, x);
328l_int32 n, i, x, y, j, index, state;
329l_int32 x1, y1, x2, y2;
333 return (
PTA *)ERROR_PTR(
"ptas not defined", __func__, NULL);
335 n = ptaGetCount(ptas);
338 ptaGetIPt(ptas, 0, &x1, &y1);
339 ptaGetIPt(ptas, n - 1, &x2, &y2);
340 if (x1 != x2 || y1 != y2)
341 return (
PTA *)ERROR_PTR(
"start and end pts not same", __func__, NULL);
343 for (i = 0; i < n; i++) {
344 ptaGetIPt(ptas, i, &x, &y);
345 if (x == xs && y == ys) {
350 if (state == L_NOT_FOUND)
351 return (
PTA *)ERROR_PTR(
"start pt not in ptas", __func__, NULL);
353 if ((ptad = ptaCreate(n)) == NULL)
354 return (
PTA *)ERROR_PTR(
"ptad not made", __func__, NULL);
355 for (j = 0; j < n - 1; j++) {
359 index = (i + j + 1) % n;
360 ptaGetIPt(ptas, index, &x, &y);
361 ptaAddPt(ptad, x, y);
363 ptaAddPt(ptad, xs, ys);
387 return (
PTA *)ERROR_PTR(
"ptas not defined", __func__, NULL);
388 if ((n = ptaGetCount(ptas)) == 0) {
389 L_WARNING(
"ptas is empty\n", __func__);
390 return ptaCopy(ptas);
392 first = L_MAX(0, first);
393 if (last < 0) last = n - 1;
395 return (
PTA *)ERROR_PTR(
"invalid first", __func__, NULL);
397 L_WARNING(
"last = %d is beyond max index = %d; adjusting\n",
398 __func__, last, n - 1);
402 return (
PTA *)ERROR_PTR(
"first > last", __func__, NULL);
404 npt = last - first + 1;
405 ptad = ptaCreate(npt);
406 for (i = first; i <= last; i++) {
407 ptaGetPt(ptas, i, &x, &y);
408 ptaAddPt(ptad, x, y);
433l_int32 n, i, x, y, minx, maxx, miny, maxy;
436 return (
BOX *)ERROR_PTR(
"pta not defined", __func__, NULL);
442 n = ptaGetCount(pta);
443 for (i = 0; i < n; i++) {
444 ptaGetIPt(pta, i, &x, &y);
445 if (x < minx) minx = x;
446 if (x > maxx) maxx = x;
447 if (y < miny) miny = y;
448 if (y > maxy) maxy = y;
451 return boxCreate(minx, miny, maxx - minx + 1, maxy - miny + 1);
480l_float32 x, y, minx, maxx, miny, maxy;
482 if (!pminx && !pmaxx && !pminy && !pmaxy)
483 return ERROR_INT(
"no output requested", __func__, 1);
484 if (pminx) *pminx = 0;
485 if (pmaxx) *pmaxx = 0;
486 if (pminy) *pminy = 0;
487 if (pmaxy) *pmaxy = 0;
489 return ERROR_INT(
"pta not defined", __func__, 1);
490 if ((n = ptaGetCount(pta)) == 0)
491 return ERROR_INT(
"no points in pta", __func__, 1);
493 ptaGetPt(pta, 0, &x, &y);
498 for (i = 1; i < n; i++) {
499 ptaGetPt(pta, i, &x, &y);
500 if (x < minx) minx = x;
501 if (x > maxx) maxx = x;
502 if (y < miny) miny = y;
503 if (y > maxy) maxy = y;
505 if (pminx) *pminx = minx;
506 if (pmaxx) *pmaxx = maxx;
507 if (pminy) *pminy = miny;
508 if (pmaxy) *pmaxy = maxy;
525l_int32 n, i, contains;
529 return (
PTA *)ERROR_PTR(
"ptas not defined", __func__, NULL);
531 return (
PTA *)ERROR_PTR(
"box not defined", __func__, NULL);
533 n = ptaGetCount(ptas);
535 for (i = 0; i < n; i++) {
536 ptaGetPt(ptas, i, &x, &y);
537 boxContainsPt(box, x, y, &contains);
539 ptaAddPt(ptad, x, y);
561l_int32 i, j, x, y, w, h, wpl, mindim, found;
562l_uint32 *data, *line;
566 return (
PTA *)ERROR_PTR(
"pixs not defined", __func__, NULL);
567 if (pixGetDepth(pixs) != 1)
568 return (
PTA *)ERROR_PTR(
"pixs not 1 bpp", __func__, NULL);
570 w = pixGetWidth(pixs);
571 h = pixGetHeight(pixs);
572 mindim = L_MIN(w, h);
573 data = pixGetData(pixs);
574 wpl = pixGetWpl(pixs);
576 if ((pta = ptaCreate(4)) == NULL)
577 return (
PTA *)ERROR_PTR(
"pta not made", __func__, NULL);
579 for (found = FALSE, i = 0; i < mindim; i++) {
580 for (j = 0; j <= i; j++) {
582 line = data + y * wpl;
593 for (found = FALSE, i = 0; i < mindim; i++) {
594 for (j = 0; j <= i; j++) {
596 line = data + y * wpl;
608 for (found = FALSE, i = 0; i < mindim; i++) {
609 for (j = 0; j <= i; j++) {
611 line = data + y * wpl;
622 for (found = FALSE, i = 0; i < mindim; i++) {
623 for (j = 0; j <= i; j++) {
625 line = data + y * wpl;
656 return ERROR_INT(
"pta not defined", __func__, 0);
658 n = ptaGetCount(pta);
659 for (i = 0; i < n; i++) {
660 ptaGetIPt(pta, i, &ix, &iy);
661 if (x == ix && y == iy)
679l_int32 i, j, n1, n2, x1, y1, x2, y2;
682 return ERROR_INT(
"pta1 not defined", __func__, 0);
684 return ERROR_INT(
"pta2 not defined", __func__, 0);
686 n1 = ptaGetCount(pta1);
687 n2 = ptaGetCount(pta2);
688 for (i = 0; i < n1; i++) {
689 ptaGetIPt(pta1, i, &x1, &y1);
690 for (j = 0; j < n2; j++) {
691 ptaGetIPt(pta2, i, &x2, &y2);
692 if (x1 == x2 && y1 == y2)
725 return (
PTA *)ERROR_PTR(
"ptas not defined", __func__, NULL);
726 n = ptaGetCount(ptas);
728 for (i = 0; i < n; i++) {
729 ptaGetIPt(ptas, i, &x, &y);
730 x = (l_int32)(scalex * (x + shiftx) + 0.5);
731 y = (l_int32)(scaley * (y + shifty) + 0.5);
732 ptaAddPt(ptad, x, y);
759l_float32 sum, x1, y1, x2, y2, xp1, yp1, xp2, yp2;
762 return ERROR_INT(
"&inside not defined", __func__, 1);
765 return ERROR_INT(
"pta not defined", __func__, 1);
769 n = ptaGetCount(pta);
771 for (i = 0; i < n; i++) {
772 ptaGetPt(pta, i, &xp1, &yp1);
773 ptaGetPt(pta, (i + 1) % n, &xp2, &yp2);
781 if (L_ABS(sum) > M_PI)
810 ang = atan2(y2, x2) - atan2(y1, x1);
811 if (ang > M_PI) ang -= 2.0 * M_PI;
812 if (ang < -M_PI) ang += 2.0 * M_PI;
842l_float32 x0, y0, x1, y1, x2, y2;
846 return ERROR_INT(
"&isconvex not defined", __func__, 1);
849 return ERROR_INT(
"pta not defined", __func__, 1);
850 if ((n = ptaGetCount(pta)) < 3)
851 return ERROR_INT(
"pta has < 3 pts", __func__, 1);
853 for (i = 0; i < n; i++) {
854 ptaGetPt(pta, i, &x0, &y0);
855 ptaGetPt(pta, (i + 1) % n, &x1, &y1);
856 ptaGetPt(pta, (i + 2) % n, &x2, &y2);
863 cprod = (x2 - x0) * (y1 - y0) - (x1 - x0) * (y2 - y0);
894l_float32 x, y, xmin, ymin, xmax, ymax;
896 if (pxmin) *pxmin = -1.0;
897 if (pymin) *pymin = -1.0;
898 if (pxmax) *pxmax = -1.0;
899 if (pymax) *pymax = -1.0;
901 return ERROR_INT(
"pta not defined", __func__, 1);
902 if (!pxmin && !pxmax && !pymin && !pymax)
903 return ERROR_INT(
"no output requested", __func__, 1);
904 if ((n = ptaGetCount(pta)) == 0) {
905 L_WARNING(
"pta is empty\n", __func__);
909 xmin = ymin = 1.0e20f;
910 xmax = ymax = -1.0e20f;
911 for (i = 0; i < n; i++) {
912 ptaGetPt(pta, i, &x, &y);
913 if (x < xmin) xmin = x;
914 if (y < ymin) ymin = y;
915 if (x > xmax) xmax = x;
916 if (y > ymax) ymax = y;
918 if (pxmin) *pxmin = xmin;
919 if (pymin) *pymin = ymin;
920 if (pxmax) *pxmax = xmax;
921 if (pymax) *pymax = ymax;
949 return (
PTA *)ERROR_PTR(
"ptas not defined", __func__, NULL);
950 if (ptaGetCount(ptas) == 0) {
951 L_WARNING(
"ptas is empty\n", __func__);
952 return ptaCopy(ptas);
956 return (
PTA *)ERROR_PTR(
"invalid type", __func__, NULL);
959 return (
PTA *)ERROR_PTR(
"invalid relation", __func__, NULL);
961 n = ptaGetCount(ptas);
963 for (i = 0; i < n; i++) {
964 ptaGetPt(ptas, i, &x, &y);
970 ptaAddPt(ptad, x, y);
976 ptaAddPt(ptad, x, y);
982 ptaAddPt(ptad, x, y);
988 ptaAddPt(ptad, x, y);
1012 return (
PTA *)ERROR_PTR(
"ptas not defined", __func__, NULL);
1013 if (!pixm || pixGetDepth(pixm) != 1)
1014 return (
PTA *)ERROR_PTR(
"pixm undefined or not 1 bpp", __func__, NULL);
1015 if (ptaGetCount(ptas) == 0) {
1016 L_INFO(
"ptas is empty\n", __func__);
1017 return ptaCopy(ptas);
1020 n = ptaGetCount(ptas);
1021 ptad = ptaCreate(n);
1022 for (i = 0; i < n; i++) {
1023 ptaGetIPt(ptas, i, &x, &y);
1024 pixGetPixel(pixm, x, y, &val);
1026 ptaAddPt(ptad, x, y);
1075l_float32 a, b, factor, sx, sy, sxx, sxy, val;
1080 if (pnafit) *pnafit = NULL;
1081 if (!pa && !pb && !pnafit)
1082 return ERROR_INT(
"no output requested", __func__, 1);
1084 return ERROR_INT(
"pta not defined", __func__, 1);
1085 if ((n = ptaGetCount(pta)) < 2)
1086 return ERROR_INT(
"less than 2 pts found", __func__, 1);
1090 sx = sy = sxx = sxy = 0.;
1092 for (i = 0; i < n; i++) {
1095 sxx += xa[i] * xa[i];
1096 sxy += xa[i] * ya[i];
1098 factor = n * sxx - sx * sx;
1100 return ERROR_INT(
"no solution found", __func__, 1);
1101 factor = 1.f / factor;
1103 a = factor * ((l_float32)n * sxy - sx * sy);
1104 b = factor * (sxx * sy - sx * sxy);
1106 for (i = 0; i < n; i++) {
1107 sxx += xa[i] * xa[i];
1108 sxy += xa[i] * ya[i];
1111 return ERROR_INT(
"no solution found", __func__, 1);
1115 for (i = 0; i < n; i++)
1118 b = sy / (l_float32)n;
1122 *pnafit = numaCreate(n);
1123 for (i = 0; i < n; i++) {
1124 val = a * xa[i] + b;
1125 numaAddNumber(*pnafit, val);
1175l_float32 x, y, sx, sy, sx2, sx3, sx4, sxy, sx2y;
1183 if (pnafit) *pnafit = NULL;
1184 if (!pa && !pb && !pc && !pnafit)
1185 return ERROR_INT(
"no output requested", __func__, 1);
1187 return ERROR_INT(
"pta not defined", __func__, 1);
1188 if ((n = ptaGetCount(pta)) < 3)
1189 return ERROR_INT(
"less than 3 pts found", __func__, 1);
1193 sx = sy = sx2 = sx3 = sx4 = sxy = sx2y = 0.;
1194 for (i = 0; i < n; i++) {
1201 sx4 += x * x * x * x;
1206 for (i = 0; i < 3; i++)
1207 f[i] = (l_float32 *)LEPT_CALLOC(3,
sizeof(l_float32));
1223 for (i = 0; i < 3; i++)
1226 return ERROR_INT(
"quadratic solution failed", __func__, 1);
1232 *pnafit = numaCreate(n);
1233 for (i = 0; i < n; i++) {
1235 y = g[0] * x * x + g[1] * x + g[2];
1236 numaAddNumber(*pnafit, y);
1287l_float32 x, y, sx, sy, sx2, sx3, sx4, sx5, sx6, sxy, sx2y, sx3y;
1296 if (pnafit) *pnafit = NULL;
1297 if (!pa && !pb && !pc && !pd && !pnafit)
1298 return ERROR_INT(
"no output requested", __func__, 1);
1300 return ERROR_INT(
"pta not defined", __func__, 1);
1301 if ((n = ptaGetCount(pta)) < 4)
1302 return ERROR_INT(
"less than 4 pts found", __func__, 1);
1306 sx = sy = sx2 = sx3 = sx4 = sx5 = sx6 = sxy = sx2y = sx3y = 0.;
1307 for (i = 0; i < n; i++) {
1314 sx4 += x * x * x * x;
1315 sx5 += x * x * x * x * x;
1316 sx6 += x * x * x * x * x * x;
1319 sx3y += x * x * x * y;
1322 for (i = 0; i < 4; i++)
1323 f[i] = (l_float32 *)LEPT_CALLOC(4,
sizeof(l_float32));
1347 for (i = 0; i < 4; i++)
1350 return ERROR_INT(
"cubic solution failed", __func__, 1);
1357 *pnafit = numaCreate(n);
1358 for (i = 0; i < n; i++) {
1360 y = g[0] * x * x * x + g[1] * x * x + g[2] * x + g[3];
1361 numaAddNumber(*pnafit, y);
1416l_float32 x, y, sx, sy, sx2, sx3, sx4, sx5, sx6, sx7, sx8;
1417l_float32 sxy, sx2y, sx3y, sx4y;
1427 if (pnafit) *pnafit = NULL;
1428 if (!pa && !pb && !pc && !pd && !pe && !pnafit)
1429 return ERROR_INT(
"no output requested", __func__, 1);
1431 return ERROR_INT(
"pta not defined", __func__, 1);
1432 if ((n = ptaGetCount(pta)) < 5)
1433 return ERROR_INT(
"less than 5 pts found", __func__, 1);
1437 sx = sy = sx2 = sx3 = sx4 = sx5 = sx6 = sx7 = sx8 = 0;
1438 sxy = sx2y = sx3y = sx4y = 0.;
1439 for (i = 0; i < n; i++) {
1446 sx4 += x * x * x * x;
1447 sx5 += x * x * x * x * x;
1448 sx6 += x * x * x * x * x * x;
1449 sx7 += x * x * x * x * x * x * x;
1450 sx8 += x * x * x * x * x * x * x * x;
1453 sx3y += x * x * x * y;
1454 sx4y += x * x * x * x * y;
1457 for (i = 0; i < 5; i++)
1458 f[i] = (l_float32 *)LEPT_CALLOC(5,
sizeof(l_float32));
1492 for (i = 0; i < 5; i++)
1495 return ERROR_INT(
"quartic solution failed", __func__, 1);
1503 *pnafit = numaCreate(n);
1504 for (i = 0; i < n; i++) {
1506 y = g[0] * x * x * x * x + g[1] * x * x * x + g[2] * x * x
1508 numaAddNumber(*pnafit, y);
1550l_float32 x, y, yf, val, mederr;
1551NUMA *nafit, *naerror;
1554 if (pptad) *pptad = NULL;
1557 if (pmederr) *pmederr = 0.0;
1558 if (pnafit) *pnafit = NULL;
1559 if (!pptad && !pa && !pb && !pnafit)
1560 return ERROR_INT(
"no output requested", __func__, 1);
1562 return ERROR_INT(
"pta not defined", __func__, 1);
1564 return ERROR_INT(
"factor must be > 0.0", __func__, 1);
1565 if ((n = ptaGetCount(pta)) < 3)
1566 return ERROR_INT(
"less than 2 pts found", __func__, 1);
1569 return ERROR_INT(
"error in linear LSF", __func__, 1);
1572 naerror = numaCreate(n);
1573 for (i = 0; i < n; i++) {
1574 ptaGetPt(pta, i, &x, &y);
1575 numaGetFValue(nafit, i, &yf);
1576 numaAddNumber(naerror, L_ABS(y - yf));
1578 numaGetMedian(naerror, &mederr);
1579 if (pmederr) *pmederr = mederr;
1580 numaDestroy(&nafit);
1583 ptad = ptaCreate(n);
1584 for (i = 0; i < n; i++) {
1585 ptaGetPt(pta, i, &x, &y);
1586 numaGetFValue(naerror, i, &val);
1587 if (val <= factor * mederr)
1588 ptaAddPt(ptad, x, y);
1590 numaDestroy(&naerror);
1636l_float32 x, y, yf, val, mederr;
1637NUMA *nafit, *naerror;
1640 if (pptad) *pptad = NULL;
1644 if (pmederr) *pmederr = 0.0;
1645 if (pnafit) *pnafit = NULL;
1646 if (!pptad && !pa && !pb && !pc && !pnafit)
1647 return ERROR_INT(
"no output requested", __func__, 1);
1649 return ERROR_INT(
"factor must be > 0.0", __func__, 1);
1651 return ERROR_INT(
"pta not defined", __func__, 1);
1652 if ((n = ptaGetCount(pta)) < 3)
1653 return ERROR_INT(
"less than 3 pts found", __func__, 1);
1656 return ERROR_INT(
"error in quadratic LSF", __func__, 1);
1659 naerror = numaCreate(n);
1660 for (i = 0; i < n; i++) {
1661 ptaGetPt(pta, i, &x, &y);
1662 numaGetFValue(nafit, i, &yf);
1663 numaAddNumber(naerror, L_ABS(y - yf));
1665 numaGetMedian(naerror, &mederr);
1666 if (pmederr) *pmederr = mederr;
1667 numaDestroy(&nafit);
1670 ptad = ptaCreate(n);
1671 for (i = 0; i < n; i++) {
1672 ptaGetPt(pta, i, &x, &y);
1673 numaGetFValue(naerror, i, &val);
1674 if (val <= factor * mederr)
1675 ptaAddPt(ptad, x, y);
1677 numaDestroy(&naerror);
1678 n = ptaGetCount(ptad);
1679 if ((n = ptaGetCount(ptad)) < 3) {
1681 return ERROR_INT(
"less than 3 pts found", __func__, 1);
1710 return ERROR_INT(
"&y not defined", __func__, 1);
1733 return ERROR_INT(
"&y not defined", __func__, 1);
1735 *py = a * x * x + b * x + c;
1757 return ERROR_INT(
"&y not defined", __func__, 1);
1759 *py = a * x * x * x + b * x * x + c * x + d;
1784 return ERROR_INT(
"&y not defined", __func__, 1);
1787 *py = a * x2 * x2 + b * x2 * x + c * x2 + d * x + e;
1818char *rtitle, *gtitle, *btitle;
1819static l_int32 count = 0;
1820l_int32 i, x, y, d, w, h, npts, rval, gval, bval;
1822NUMA *na, *nar, *nag, *nab;
1825 lept_mkdir(
"lept/plot");
1828 return ERROR_INT(
"pixs not defined", __func__, 1);
1830 return ERROR_INT(
"pta not defined", __func__, 1);
1831 if (outformat != GPLOT_PNG && outformat != GPLOT_PS &&
1832 outformat != GPLOT_EPS && outformat != GPLOT_LATEX) {
1833 L_WARNING(
"outformat invalid; using GPLOT_PNG\n", __func__);
1834 outformat = GPLOT_PNG;
1838 d = pixGetDepth(pixt);
1839 w = pixGetWidth(pixt);
1840 h = pixGetHeight(pixt);
1841 npts = ptaGetCount(pta);
1843 nar = numaCreate(npts);
1844 nag = numaCreate(npts);
1845 nab = numaCreate(npts);
1846 for (i = 0; i < npts; i++) {
1847 ptaGetIPt(pta, i, &x, &y);
1848 if (x < 0 || x >= w)
1850 if (y < 0 || y >= h)
1852 pixGetPixel(pixt, x, y, &val);
1856 numaAddNumber(nar, rval);
1857 numaAddNumber(nag, gval);
1858 numaAddNumber(nab, bval);
1861 snprintf(buffer,
sizeof(buffer),
"/tmp/lept/plot/%03d", count++);
1862 rtitle = stringJoin(
"Red: ", title);
1863 gplotSimple1(nar, outformat, buffer, rtitle);
1864 snprintf(buffer,
sizeof(buffer),
"/tmp/lept/plot/%03d", count++);
1865 gtitle = stringJoin(
"Green: ", title);
1866 gplotSimple1(nag, outformat, buffer, gtitle);
1867 snprintf(buffer,
sizeof(buffer),
"/tmp/lept/plot/%03d", count++);
1868 btitle = stringJoin(
"Blue: ", title);
1869 gplotSimple1(nab, outformat, buffer, btitle);
1877 na = numaCreate(npts);
1878 for (i = 0; i < npts; i++) {
1879 ptaGetIPt(pta, i, &x, &y);
1880 if (x < 0 || x >= w)
1882 if (y < 0 || y >= h)
1884 pixGetPixel(pixt, x, y, &val);
1885 numaAddNumber(na, (l_float32)val);
1888 snprintf(buffer,
sizeof(buffer),
"/tmp/lept/plot/%03d", count++);
1889 gplotSimple1(na, outformat, buffer, title);
1914l_int32 i, j, w, h, wpl, xstart, xend, ystart, yend, bw, bh;
1915l_uint32 *data, *line;
1918 if (!pixs || (pixGetDepth(pixs) != 1))
1919 return (
PTA *)ERROR_PTR(
"pixs undefined or not 1 bpp", __func__, NULL);
1921 pixGetDimensions(pixs, &w, &h, NULL);
1922 data = pixGetData(pixs);
1923 wpl = pixGetWpl(pixs);
1924 xstart = ystart = 0;
1928 boxGetGeometry(box, &xstart, &ystart, &bw, &bh);
1929 xend = xstart + bw - 1;
1930 yend = ystart + bh - 1;
1933 if ((pta = ptaCreate(0)) == NULL)
1934 return (
PTA *)ERROR_PTR(
"pta not made", __func__, NULL);
1935 for (i = ystart; i <= yend; i++) {
1936 line = data + i * wpl;
1937 for (j = xstart; j <= xend; j++) {
1939 ptaAddPt(pta, j, i);
1970 return (
PIX *)ERROR_PTR(
"pta not defined", __func__, NULL);
1972 if ((pix = pixCreate(w, h, 1)) == NULL)
1973 return (
PIX *)ERROR_PTR(
"pix not made", __func__, NULL);
1974 n = ptaGetCount(pta);
1975 for (i = 0; i < n; i++) {
1976 ptaGetIPt(pta, i, &x, &y);
1977 if (x < 0 || x >= w || y < 0 || y >= h)
1979 pixSetPixel(pix, x, y, 1);
2007 if (!pixs || (pixGetDepth(pixs) != 1))
2008 return (
PTA *)ERROR_PTR(
"pixs undefined or not 1 bpp", __func__, NULL);
2009 if (type != L_BOUNDARY_FG && type != L_BOUNDARY_BG)
2010 return (
PTA *)ERROR_PTR(
"invalid type", __func__, NULL);
2012 if (type == L_BOUNDARY_FG)
2013 pixt = pixMorphSequence(pixs,
"e3.3", 0);
2015 pixt = pixMorphSequence(pixs,
"d3.3", 0);
2016 pixXor(pixt, pixt, pixs);
2050 l_int32 connectivity,
2054l_int32 i, n, w, h, x, y, bw, bh, left, right, top, bot;
2061 if (pboxa) *pboxa = NULL;
2062 if (ppixa) *ppixa = NULL;
2063 if (!pixs || (pixGetDepth(pixs) != 1))
2064 return (
PTAA *)ERROR_PTR(
"pixs undefined or not 1 bpp", __func__, NULL);
2065 if (type != L_BOUNDARY_FG && type != L_BOUNDARY_BG)
2066 return (
PTAA *)ERROR_PTR(
"invalid type", __func__, NULL);
2067 if (connectivity != 4 && connectivity != 8)
2068 return (
PTAA *)ERROR_PTR(
"connectivity not 4 or 8", __func__, NULL);
2070 pixGetDimensions(pixs, &w, &h, NULL);
2071 boxa = pixConnComp(pixs, &pixa, connectivity);
2072 n = boxaGetCount(boxa);
2073 ptaa = ptaaCreate(0);
2074 for (i = 0; i < n; i++) {
2075 pixt1 = pixaGetPix(pixa, i,
L_CLONE);
2076 boxaGetBoxGeometry(boxa, i, &x, &y, &bw, &bh);
2077 left = right = top = bot = 0;
2078 if (type == L_BOUNDARY_BG) {
2079 if (x > 0) left = 1;
2081 if (x + bw < w) right = 1;
2082 if (y + bh < h) bot = 1;
2083 pixt2 = pixAddBorderGeneral(pixt1, left, right, top, bot, 0);
2085 pixt2 = pixClone(pixt1);
2088 pta2 =
ptaTransform(pta1, x - left, y - top, 1.0, 1.0);
2132l_int32 wpl, index, i, j, w, h;
2134l_uint32 *data, *line;
2138 if (pncc) *pncc = 0;
2139 if (!pixs || (pixGetDepth(pixs) != 32))
2140 return (
PTAA *)ERROR_PTR(
"pixs undef or not 32 bpp", __func__, NULL);
2144 pixGetMaxValueInRect(pixs, NULL, &maxval, NULL, NULL);
2145 if (pncc) *pncc = maxval;
2147 ptaa = ptaaCreate(maxval + 1);
2148 ptaaInitFull(ptaa, pta);
2153 pixGetDimensions(pixs, &w, &h, NULL);
2154 data = pixGetData(pixs);
2155 wpl = pixGetWpl(pixs);
2156 for (i = 0; i < h; i++) {
2157 line = data + wpl * i;
2158 for (j = 0; j < w; j++) {
2161 ptaaAddPt(ptaa, index, j, i);
2193 return (
PTA *)ERROR_PTR(
"pixs not defined", __func__, NULL);
2194 pixGetDimensions(pixs, &w, &h, NULL);
2195 if (x < 0 || x >= w || y < 0 || y >= h)
2196 return (
PTA *)ERROR_PTR(
"(x,y) not in pixs", __func__, NULL);
2197 if (conn != 4 && conn != 8)
2198 return (
PTA *)ERROR_PTR(
"conn not 4 or 8", __func__, NULL);
2200 pta = ptaCreate(conn);
2202 ptaAddPt(pta, x - 1, y);
2204 ptaAddPt(pta, x + 1, y);
2206 ptaAddPt(pta, x, y - 1);
2208 ptaAddPt(pta, x, y + 1);
2212 ptaAddPt(pta, x - 1, y - 1);
2214 ptaAddPt(pta, x - 1, y + 1);
2218 ptaAddPt(pta, x + 1, y - 1);
2220 ptaAddPt(pta, x + 1, y + 1);
2241l_float32 startx, delx, val;
2245 return (
PTA *)ERROR_PTR(
"na not defined", __func__, NULL);
2247 n = numaGetCount(na);
2249 numaGetParameters(na, &startx, &delx);
2250 for (i = 0; i < n; i++) {
2251 numaGetFValue(na, i, &val);
2252 ptaAddPt(pta, startx + i * delx, val);
2269l_int32 i, n, nx, ny;
2270l_float32 valx, valy;
2274 return (
PTA *)ERROR_PTR(
"nax and nay not both defined", __func__, NULL);
2276 nx = numaGetCount(nax);
2277 ny = numaGetCount(nay);
2280 L_WARNING(
"nx = %d does not equal ny = %d\n", __func__, nx, ny);
2282 for (i = 0; i < n; i++) {
2283 numaGetFValue(nax, i, &valx);
2284 numaGetFValue(nay, i, &valy);
2285 ptaAddPt(pta, valx, valy);
2305l_float32 valx, valy;
2307 if (pnax) *pnax = NULL;
2308 if (pnay) *pnay = NULL;
2310 return ERROR_INT(
"&nax and &nay not both defined", __func__, 1);
2312 return ERROR_INT(
"pta not defined", __func__, 1);
2314 n = ptaGetCount(pta);
2315 *pnax = numaCreate(n);
2316 *pnay = numaCreate(n);
2317 for (i = 0; i < n; i++) {
2318 ptaGetPt(pta, i, &valx, &valy);
2319 numaAddNumber(*pnax, valx);
2320 numaAddNumber(*pnay, valy);
2353l_int32 i, n, w, h, x, y;
2354l_uint32 rpixel, gpixel, bpixel;
2357 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, pixd);
2359 return (
PIX *)ERROR_PTR(
"pta not defined", __func__, pixd);
2360 if (pixd && (pixd != pixs || pixGetDepth(pixd) != 32))
2361 return (
PIX *)ERROR_PTR(
"invalid pixd", __func__, pixd);
2364 pixd = pixConvertTo32(pixs);
2365 pixGetDimensions(pixd, &w, &h, NULL);
2366 composeRGBPixel(255, 0, 0, &rpixel);
2367 composeRGBPixel(0, 255, 0, &gpixel);
2368 composeRGBPixel(0, 0, 255, &bpixel);
2370 n = ptaGetCount(pta);
2371 for (i = 0; i < n; i++) {
2372 ptaGetIPt(pta, i, &x, &y);
2373 if (x < 0 || x >= w || y < 0 || y >= h)
2376 pixSetPixel(pixd, x, y, rpixel);
2378 pixSetPixel(pixd, x, y, gpixel);
2380 pixSetPixel(pixd, x, y, bpixel);
2426 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, pixd);
2428 return (
PIX *)ERROR_PTR(
"ptaa not defined", __func__, pixd);
2429 if (pixd && (pixd != pixs || pixGetDepth(pixd) != 32))
2430 return (
PIX *)ERROR_PTR(
"invalid pixd", __func__, pixd);
2432 return (
PIX *)ERROR_PTR(
"pixp not defined", __func__, pixd);
2435 pixd = pixConvertTo32(pixs);
2438 cmap = pixcmapCreateRandom(8, 0, 0);
2439 n = ptaaGetCount(ptaa);
2440 for (i = 0; i < n; i++) {
2441 pixcmapGetColor32(cmap, i % 256, &color);
2442 pta = ptaaGetPta(ptaa, i,
L_CLONE);
2447 pixcmapDestroy(&cmap);
2486l_int32 i, n, w, h, x, y;
2490 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, pixd);
2492 return (
PIX *)ERROR_PTR(
"pta not defined", __func__, pixd);
2493 if (pixd && (pixd != pixs || pixGetDepth(pixd) != 32))
2494 return (
PIX *)ERROR_PTR(
"invalid pixd", __func__, pixd);
2496 return (
PIX *)ERROR_PTR(
"pixp not defined", __func__, pixd);
2499 pixd = pixConvertTo32(pixs);
2500 pixGetDimensions(pixs, &w, &h, NULL);
2503 n = ptaGetCount(ptat);
2504 for (i = 0; i < n; i++) {
2505 ptaGetIPt(ptat, i, &x, &y);
2506 if (x < 0 || x >= w || y < 0 || y >= h)
2508 pixSetPixel(pixd, x, y, color);
2545l_int32 i, j, n, np, x, y, xp, yp, xf, yf;
2549 return (
PTA *)ERROR_PTR(
"ptas not defined", __func__, NULL);
2551 return (
PTA *)ERROR_PTR(
"no pattern is defined", __func__, NULL);
2553 L_WARNING(
"pixp and ptap defined; using ptap\n", __func__);
2555 n = ptaGetCount(ptas);
2556 ptad = ptaCreate(n);
2558 ptat = ptaClone(ptap);
2561 np = ptaGetCount(ptat);
2562 for (i = 0; i < n; i++) {
2563 ptaGetIPt(ptas, i, &x, &y);
2564 for (j = 0; j < np; j++) {
2565 ptaGetIPt(ptat, j, &xp, &yp);
2568 if (xf >= 0 && xf < w && yf >= 0 && yf < h)
2569 ptaAddPt(ptad, xf, yf);
2590l_int32 i, j, w, h, npta, npt, x, y, rv, gv, bv;
2592NUMA *na1, *na2, *na3;
2597 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
2599 return (
PIX *)ERROR_PTR(
"ptaa not defined", __func__, NULL);
2600 npta = ptaaGetCount(ptaa);
2602 return (
PIX *)ERROR_PTR(
"no pta", __func__, NULL);
2604 if ((pixd = pixConvertTo32(pixs)) == NULL)
2605 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, NULL);
2606 pixGetDimensions(pixd, &w, &h, NULL);
2609 if ((pixela = (l_uint32 *)LEPT_CALLOC(npta,
sizeof(l_uint32))) == NULL) {
2611 return (
PIX *)ERROR_PTR(
"calloc fail for pixela", __func__, NULL);
2613 na1 = numaPseudorandomSequence(256, 14657);
2614 na2 = numaPseudorandomSequence(256, 34631);
2615 na3 = numaPseudorandomSequence(256, 54617);
2616 for (i = 0; i < npta; i++) {
2617 numaGetIValue(na1, i % 256, &rv);
2618 numaGetIValue(na2, i % 256, &gv);
2619 numaGetIValue(na3, i % 256, &bv);
2620 composeRGBPixel(rv, gv, bv, &pixela[i]);
2626 for (i = 0; i < npta; i++) {
2627 pta = ptaaGetPta(ptaa, i,
L_CLONE);
2628 npt = ptaGetCount(pta);
2629 for (j = 0; j < npt; j++) {
2630 ptaGetIPt(pta, j, &x, &y);
2631 if (x < 0 || x >= w || y < 0 || y >= h)
2633 pixSetPixel(pixd, x, y, pixela[i]);
l_int32 gaussjordan(l_float32 **a, l_float32 *b, l_int32 n)
gaussjordan()
#define GET_DATA_BYTE(pdata, n)
#define GET_DATA_BIT(pdata, n)
@ REMOVE_CMAP_BASED_ON_SRC
l_ok applyLinearFit(l_float32 a, l_float32 b, l_float32 x, l_float32 *py)
applyLinearFit()
PTA * numaConvertToPta2(NUMA *nax, NUMA *nay)
numaConvertToPta2()
l_int32 ptaContainsPt(PTA *pta, l_int32 x, l_int32 y)
ptaContainsPt()
PTA * ptaTranspose(PTA *ptas)
ptaTranspose()
PIX * pixGenerateFromPta(PTA *pta, l_int32 w, l_int32 h)
pixGenerateFromPta()
l_ok pixPlotAlongPta(PIX *pixs, PTA *pta, l_int32 outformat, const char *title)
pixPlotAlongPta()
PTA * ptaCyclicPerm(PTA *ptas, l_int32 xs, l_int32 ys)
ptaCyclicPerm()
PTA * ptaCropToMask(PTA *ptas, PIX *pixm)
ptaCropToMask()
PIX * pixDisplayPtaPattern(PIX *pixd, PIX *pixs, PTA *pta, PIX *pixp, l_int32 cx, l_int32 cy, l_uint32 color)
pixDisplayPtaPattern()
l_ok ptaGetQuarticLSF(PTA *pta, l_float32 *pa, l_float32 *pb, l_float32 *pc, l_float32 *pd, l_float32 *pe, NUMA **pnafit)
ptaGetQuarticLSF()
PTA * ptaSelectRange(PTA *ptas, l_int32 first, l_int32 last)
ptaSelectRange()
PTAA * ptaaGetBoundaryPixels(PIX *pixs, l_int32 type, l_int32 connectivity, BOXA **pboxa, PIXA **ppixa)
ptaaGetBoundaryPixels()
PTA * pixFindCornerPixels(PIX *pixs)
pixFindCornerPixels()
PTA * ptaReplicatePattern(PTA *ptas, PIX *pixp, PTA *ptap, l_int32 cx, l_int32 cy, l_int32 w, l_int32 h)
ptaReplicatePattern()
l_ok applyQuadraticFit(l_float32 a, l_float32 b, l_float32 c, l_float32 x, l_float32 *py)
applyQuadraticFit()
l_int32 ptaPtInsidePolygon(PTA *pta, l_float32 x, l_float32 y, l_int32 *pinside)
ptaPtInsidePolygon()
l_int32 ptaTestIntersection(PTA *pta1, PTA *pta2)
ptaTestIntersection()
PTAA * ptaaIndexLabeledPixels(PIX *pixs, l_int32 *pncc)
ptaaIndexLabeledPixels()
l_ok ptaGetMinMax(PTA *pta, l_float32 *pxmin, l_float32 *pymin, l_float32 *pxmax, l_float32 *pymax)
ptaGetMinMax()
PTA * ptaGetBoundaryPixels(PIX *pixs, l_int32 type)
ptaGetBoundaryPixels()
PTA * ptaGetPixelsFromPix(PIX *pixs, BOX *box)
ptaGetPixelsFromPix()
l_ok ptaGetCubicLSF(PTA *pta, l_float32 *pa, l_float32 *pb, l_float32 *pc, l_float32 *pd, NUMA **pnafit)
ptaGetCubicLSF()
l_ok applyQuarticFit(l_float32 a, l_float32 b, l_float32 c, l_float32 d, l_float32 e, l_float32 x, l_float32 *py)
applyQuarticFit()
l_ok ptaConvertToNuma(PTA *pta, NUMA **pnax, NUMA **pnay)
ptaConvertToNuma()
l_float32 l_angleBetweenVectors(l_float32 x1, l_float32 y1, l_float32 x2, l_float32 y2)
l_angleBetweenVectors()
l_ok ptaGetQuadraticLSF(PTA *pta, l_float32 *pa, l_float32 *pb, l_float32 *pc, NUMA **pnafit)
ptaGetQuadraticLSF()
l_ok ptaaJoin(PTAA *ptaad, PTAA *ptaas, l_int32 istart, l_int32 iend)
ptaaJoin()
PTA * numaConvertToPta1(NUMA *na)
numaConvertToPta1()
l_ok ptaGetLinearLSF(PTA *pta, l_float32 *pa, l_float32 *pb, NUMA **pnafit)
ptaGetLinearLSF()
l_ok ptaNoisyLinearLSF(PTA *pta, l_float32 factor, PTA **pptad, l_float32 *pa, l_float32 *pb, l_float32 *pmederr, NUMA **pnafit)
ptaNoisyLinearLSF()
PTA * ptaSubsample(PTA *ptas, l_int32 subfactor)
ptaSubsample()
BOX * ptaGetBoundingRegion(PTA *pta)
ptaGetBoundingRegion()
PTA * ptaTransform(PTA *ptas, l_int32 shiftx, l_int32 shifty, l_float32 scalex, l_float32 scaley)
ptaTransform()
l_ok ptaJoin(PTA *ptad, PTA *ptas, l_int32 istart, l_int32 iend)
ptaJoin()
PTA * ptaReverse(PTA *ptas, l_int32 type)
ptaReverse()
l_ok ptaGetRange(PTA *pta, l_float32 *pminx, l_float32 *pmaxx, l_float32 *pminy, l_float32 *pmaxy)
ptaGetRange()
PIX * pixDisplayPtaaPattern(PIX *pixd, PIX *pixs, PTAA *ptaa, PIX *pixp, l_int32 cx, l_int32 cy)
pixDisplayPtaaPattern()
PTA * ptaSelectByValue(PTA *ptas, l_float32 xth, l_float32 yth, l_int32 type, l_int32 relation)
ptaSelectByValue()
PTA * ptaGetInsideBox(PTA *ptas, BOX *box)
ptaGetInsideBox()
l_int32 ptaPolygonIsConvex(PTA *pta, l_int32 *pisconvex)
ptaPolygonIsConvex()
PIX * pixDisplayPtaa(PIX *pixs, PTAA *ptaa)
pixDisplayPtaa()
l_ok ptaNoisyQuadraticLSF(PTA *pta, l_float32 factor, PTA **pptad, l_float32 *pa, l_float32 *pb, l_float32 *pc, l_float32 *pmederr, NUMA **pnafit)
ptaNoisyQuadraticLSF()
PTA * ptaGetNeighborPixLocs(PIX *pixs, l_int32 x, l_int32 y, l_int32 conn)
ptaGetNeighborPixLocs()
PIX * pixDisplayPta(PIX *pixd, PIX *pixs, PTA *pta)
pixDisplayPta()
l_ok applyCubicFit(l_float32 a, l_float32 b, l_float32 c, l_float32 d, l_float32 x, l_float32 *py)
applyCubicFit()