113#include <config_auto.h>
118#include "allheaders.h"
121 l_int32 searchdir, l_int32 mindist,
122 l_int32 tsize, l_int32 ntiles);
125#define EQUAL_SIZE_WARNING 0
167l_int32 wd, hd, wm, hm, w, h, d, wpld, wplm;
168l_int32 i, j, rval, gval, bval;
169l_uint32 *datad, *datam, *lined, *linem;
172 return ERROR_INT(
"pixd not defined", __func__, 1);
174 L_WARNING(
"no mask; nothing to do\n", __func__);
177 if (pixGetColormap(pixd)) {
178 extractRGBValues(val, &rval, &gval, &bval);
179 return pixSetMaskedCmap(pixd, pixm, 0, 0, rval, gval, bval);
182 if (pixGetDepth(pixm) != 1)
183 return ERROR_INT(
"pixm not 1 bpp", __func__, 1);
184 d = pixGetDepth(pixd);
196 return ERROR_INT(
"pixd not 1, 2, 4, 8, 16 or 32 bpp", __func__, 1);
197 pixGetDimensions(pixm, &wm, &hm, NULL);
203 pixRasterop(pixd, 0, 0, wm, hm,
PIX_MASK, pixmi, 0, 0);
206 pixRasterop(pixd, 0, 0, wm, hm,
PIX_PAINT, pixm, 0, 0);
212 if (d < 32 && val == 0) {
213 PIX *pixmd = pixUnpackBinary(pixm, d, 1);
214 pixRasterop(pixd, 0, 0, wm, hm,
PIX_MASK, pixmd, 0, 0);
220 if (d < 32 && val == ((1 << d) - 1)) {
221 PIX *pixmd = pixUnpackBinary(pixm, d, 0);
222 pixRasterop(pixd, 0, 0, wm, hm,
PIX_PAINT, pixmd, 0, 0);
227 pixGetDimensions(pixd, &wd, &hd, &d);
230 if (L_ABS(wd - wm) > 7 || L_ABS(hd - hm) > 7)
231 L_WARNING(
"pixd and pixm sizes differ\n", __func__);
233 datad = pixGetData(pixd);
234 datam = pixGetData(pixm);
235 wpld = pixGetWpl(pixd);
236 wplm = pixGetWpl(pixm);
237 for (i = 0; i < h; i++) {
238 lined = datad + i * wpld;
239 linem = datam + i * wplm;
240 for (j = 0; j < w; j++) {
260 return ERROR_INT(
"shouldn't get here", __func__, 1);
310 return ERROR_INT(
"pixd not defined", __func__, 1);
314 d = pixGetDepth(pixd);
315 if (d != 8 && d != 16 && d != 32)
316 return ERROR_INT(
"pixd not 8, 16 or 32 bpp", __func__, 1);
317 if (pixGetDepth(pixm) != 1)
318 return ERROR_INT(
"pixm not 1 bpp", __func__, 1);
321 if ((pixmu = pixUnpackBinary(pixm, d, 1)) == NULL)
322 return ERROR_INT(
"pixmu not made", __func__, 1);
325 pixGetDimensions(pixm, &wm, &hm, NULL);
329 if ((pixc = pixCreateTemplate(pixmu)) == NULL) {
331 return ERROR_INT(
"pixc not made", __func__, 1);
333 pixSetAllArbitrary(pixc, val);
337 pixAnd(pixmu, pixmu, pixc);
382l_int32 w, h, d, ws, hs, ds, wm, hm, dm, wmin, hmin;
383l_int32 wpl, wpls, wplm, i, j, val;
384l_uint32 *data, *datas, *datam, *line, *lines, *linem;
390 return ERROR_INT(
"pixd not defined", __func__, 1);
392 return ERROR_INT(
"pixs not defined", __func__, 1);
393 pixGetDimensions(pixd, &w, &h, &d);
394 pixGetDimensions(pixs, &ws, &hs, &ds);
395 pixGetDimensions(pixm, &wm, &hm, &dm);
397 return ERROR_INT(
"pixs and pixd depths differ", __func__, 1);
399 return ERROR_INT(
"pixm not 1 bpp", __func__, 1);
400 if (d != 1 && d != 8 && d != 32)
401 return ERROR_INT(
"pixd not 1, 8 or 32 bpp", __func__, 1);
402 if (pixGetColormap(pixd) || pixGetColormap(pixs))
403 return ERROR_INT(
"pixs and/or pixd is cmapped", __func__, 1);
409 wmin = L_MIN(w, L_MIN(ws, wm));
410 hmin = L_MIN(h, L_MIN(hs, hm));
412 pixt =
pixAnd(NULL, pixs, pixm);
415 pixRasterop(pixd, 0, 0, wmin, hmin,
PIX_SRC |
PIX_DST, pixt, 0, 0);
420 data = pixGetData(pixd);
421 datas = pixGetData(pixs);
422 datam = pixGetData(pixm);
423 wpl = pixGetWpl(pixd);
424 wpls = pixGetWpl(pixs);
425 wplm = pixGetWpl(pixm);
427 for (i = 0; i < hmin; i++) {
428 line = data + i * wpl;
429 lines = datas + i * wpls;
430 linem = datam + i * wplm;
431 for (j = 0; j < wmin; j++) {
439 for (i = 0; i < hmin; i++) {
440 line = data + i * wpl;
441 lines = datas + i * wpls;
442 linem = datam + i * wplm;
443 for (j = 0; j < wmin; j++) {
501l_int32 d, w, h, ws, hs, ds, wm, hm, dm, wmin, hmin;
502l_int32 wpl, wpls, wplm, i, j, val;
503l_uint32 *data, *datas, *datam, *line, *lines, *linem;
509 return ERROR_INT(
"pixd not defined", __func__, 1);
511 return ERROR_INT(
"pixs not defined", __func__, 1);
512 pixGetDimensions(pixd, &w, &h, &d);
513 pixGetDimensions(pixs, &ws, &hs, &ds);
514 pixGetDimensions(pixm, &wm, &hm, &dm);
516 return ERROR_INT(
"pixs and pixd depths differ", __func__, 1);
518 return ERROR_INT(
"pixm not 1 bpp", __func__, 1);
519 if (d != 1 && d != 8 && d != 32)
520 return ERROR_INT(
"pixd not 1, 8 or 32 bpp", __func__, 1);
521 if (pixGetColormap(pixd) || pixGetColormap(pixs))
522 return ERROR_INT(
"pixs and/or pixd is cmapped", __func__, 1);
528 wmin = L_MIN(ws, wm);
529 hmin = L_MIN(hs, hm);
531 pixt =
pixAnd(NULL, pixs, pixm);
534 pixRasterop(pixd, x, y, wmin, hmin,
PIX_SRC |
PIX_DST, pixt, 0, 0);
539 wpl = pixGetWpl(pixd);
540 data = pixGetData(pixd);
541 wpls = pixGetWpl(pixs);
542 datas = pixGetData(pixs);
543 wplm = pixGetWpl(pixm);
544 datam = pixGetData(pixm);
546 for (i = 0; i < hmin; i++) {
547 if (y + i < 0 || y + i >= h)
continue;
548 line = data + (y + i) * wpl;
549 lines = datas + i * wpls;
550 linem = datam + i * wplm;
551 for (j = 0; j < wmin; j++) {
552 if (x + j < 0 || x + j >= w)
continue;
561 *(line + x + j) = *(lines + j);
564 return ERROR_INT(
"shouldn't get here", __func__, 1);
624l_int32 d, w, h, wm, hm, wpl, wplm, i, j, rval, gval, bval;
625l_uint32 *data, *datam, *line, *linem;
630 return ERROR_INT(
"pixd not defined", __func__, 1);
631 if (pixGetColormap(pixd)) {
632 extractRGBValues(val, &rval, &gval, &bval);
633 return pixSetMaskedCmap(pixd, pixm, x, y, rval, gval, bval);
636 if (pixGetDepth(pixm) != 1)
637 return ERROR_INT(
"pixm not 1 bpp", __func__, 1);
638 d = pixGetDepth(pixd);
650 return ERROR_INT(
"pixd not 1, 2, 4, 8, 16 or 32 bpp", __func__, 1);
651 pixGetDimensions(pixm, &wm, &hm, NULL);
657 pixRasterop(pixd, x, y, wm, hm,
PIX_MASK, pixmi, 0, 0);
660 pixRasterop(pixd, x, y, wm, hm,
PIX_PAINT, pixm, 0, 0);
666 if (d < 32 && val == 0) {
667 PIX *pixmd = pixUnpackBinary(pixm, d, 1);
668 pixRasterop(pixd, x, y, wm, hm,
PIX_MASK, pixmd, 0, 0);
674 if (d < 32 && val == ((1 << d) - 1)) {
675 PIX *pixmd = pixUnpackBinary(pixm, d, 0);
676 pixRasterop(pixd, x, y, wm, hm,
PIX_PAINT, pixmd, 0, 0);
682 pixGetDimensions(pixd, &w, &h, NULL);
683 wpl = pixGetWpl(pixd);
684 data = pixGetData(pixd);
685 wplm = pixGetWpl(pixm);
686 datam = pixGetData(pixm);
687 for (i = 0; i < hm; i++) {
688 if (y + i < 0 || y + i >= h)
continue;
689 line = data + (y + i) * wpl;
690 linem = datam + i * wplm;
691 for (j = 0; j < wm; j++) {
692 if (x + j < 0 || x + j >= w)
continue;
709 *(line + x + j) = val;
712 return ERROR_INT(
"shouldn't get here", __func__, 1);
753l_int32 i, n, x, y, w, h;
757 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
759 return (
PIX *)ERROR_PTR(
"boxa not defined", __func__, NULL);
761 return (
PIX *)ERROR_PTR(
"invalid background", __func__, NULL);
763 pixd = pixCreateTemplate(pixs);
764 pixSetBlackOrWhite(pixd, background);
765 n = boxaGetCount(boxa);
766 for (i = 0; i < n; i++) {
767 boxaGetBoxGeometry(boxa, i, &x, &y, &w, &h);
768 pixRasterop(pixd, x, y, w, h,
PIX_SRC, pixs, x, y);
846l_int32 w, h, d, wm, hm, dm, i, n, bx, by, bw, bh, edgeblend, retval, minside;
848BOX *box, *boxv, *boxh;
850PIX *pixf, *pixv, *pixh, *pix1, *pix2, *pix3, *pix4, *pix5;
856 return ERROR_INT(
"pixd not defined", __func__, 1);
857 if (pixGetColormap(pixd) != NULL)
858 return ERROR_INT(
"pixd has colormap", __func__, 1);
859 pixGetDimensions(pixd, &w, &h, &d);
860 if (d != 8 && d != 32)
861 return ERROR_INT(
"pixd not 8 or 32 bpp", __func__, 1);
862 pixGetDimensions(pixm, &wm, &hm, &dm);
864 return ERROR_INT(
"pixm not 1 bpp", __func__, 1);
866 return ERROR_INT(
"x and y must be non-negative", __func__, 1);
867 if (searchdir != L_HORIZ && searchdir != L_VERT &&
868 searchdir != L_BOTH_DIRECTIONS)
869 return ERROR_INT(
"invalid searchdir", __func__, 1);
871 return ERROR_INT(
"tilesize must be >= 2", __func__, 1);
873 return ERROR_INT(
"distblend must be >= 0", __func__, 1);
876 if (wm < w || hm < h) {
877 pixf = pixCreate(w, h, 1);
878 pixRasterop(pixf, x, y, wm, hm,
PIX_SRC, pixm, 0, 0);
880 pixf = pixCopy(NULL, pixm);
884 boxa = pixConnComp(pixf, &pixa, 8);
885 if ((n = pixaGetCount(pixa)) == 0) {
886 L_WARNING(
"no fg in mask\n", __func__);
899 edgeblend = (n == 1 && distblend > 0) ? 1 : 0;
900 if (distblend > 0 && n > 1)
901 L_WARNING(
"%d components; can not blend at edges\n", __func__, n);
903 for (i = 0; i < n; i++) {
907 pix1 = pixaGetPix(pixa, i,
L_CLONE);
908 box = pixaGetBox(pixa, i,
L_CLONE);
910 boxGetGeometry(box, &bx, &by, &bw, &bh);
911 minside = L_MIN(bw, bh);
914 if (searchdir == L_HORIZ || searchdir == L_BOTH_DIRECTIONS) {
916 L_MIN(minside, tilesize), ntiles, &boxh, 0);
918 if (searchdir == L_VERT || searchdir == L_BOTH_DIRECTIONS) {
920 L_MIN(minside, tilesize), ntiles, &boxv, 0);
922 if (!boxh && !boxv) {
923 L_WARNING(
"tile region not selected; paint color near boundary\n",
926 pix1 = pixaGetPix(pixa, i,
L_CLONE);
927 pixaGetBoxGeometry(pixa, i, &bx, &by, NULL, NULL);
937 pixh = (boxh) ? pixClipRectangle(pixd, boxh, NULL) : NULL;
938 pixv = (boxv) ? pixClipRectangle(pixd, boxv, NULL) : NULL;
940 pix2 = pixBlend(pixh, pixv, 0, 0, 0.5);
942 pix2 = pixClone(pixh);
944 pix2 = pixClone(pixv);
956 pix4 = pixClipRectangle(pixd, box, NULL);
957 pix5 = pixBlendWithGrayMask(pix4, pix3, pix1, 0, 0);
958 pixRasterop(pixd, bx, by, bw, bh,
PIX_SRC, pix5, 0, 0);
994l_int32 w, h, d, i, j, sval, wpls, wpld;
995l_uint32 *datas, *datad, *lines, *lined;
999 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
1000 pixGetDimensions(pixs, &w, &h, &d);
1001 if (d != 2 && d != 4 && d != 8)
1002 return (
PIX *)ERROR_PTR(
"pix not 2, 4 or 8 bpp", __func__, NULL);
1004 pixd = pixCreate(w, h, 1);
1005 pixCopyResolution(pixd, pixs);
1006 pixCopyInputFormat(pixd, pixs);
1007 datas = pixGetData(pixs);
1008 datad = pixGetData(pixd);
1009 wpls = pixGetWpl(pixs);
1010 wpld = pixGetWpl(pixd);
1011 for (i = 0; i < h; i++) {
1012 lines = datas + i * wpls;
1013 lined = datad + i * wpld;
1014 for (j = 0; j < w; j++) {
1049l_int32 w, h, d, i, j, val, wpls, wpld;
1050l_uint32 *datas, *datad, *lines, *lined;
1054 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
1056 return (
PIX *)ERROR_PTR(
"tab not defined", __func__, NULL);
1057 pixGetDimensions(pixs, &w, &h, &d);
1058 if (d != 2 && d != 4 && d != 8)
1059 return (
PIX *)ERROR_PTR(
"pix not 2, 4 or 8 bpp", __func__, NULL);
1061 pixd = pixCreate(w, h, 1);
1062 pixCopyResolution(pixd, pixs);
1063 pixCopyInputFormat(pixd, pixs);
1064 datas = pixGetData(pixs);
1065 datad = pixGetData(pixd);
1066 wpls = pixGetWpl(pixs);
1067 wpld = pixGetWpl(pixd);
1068 for (i = 0; i < h; i++) {
1069 lines = datas + i * wpls;
1070 lined = datad + i * wpld;
1071 for (j = 0; j < w; j++) {
1121 if (!pixs || pixGetDepth(pixs) != 32)
1122 return (
PIX *)ERROR_PTR(
"pixs undefined or not 32 bpp", __func__, NULL);
1123 if (thresh >= 255.0) thresh = 254.0;
1125 if ((pix1 = pixConvertRGBToGrayArb(pixs, rc, gc, bc)) == NULL)
1126 return (
PIX *)ERROR_PTR(
"pix1 not made", __func__, NULL);
1127 pix2 = pixThresholdToBinary(pix1, thresh + 1);
1192PIX *pixg, *pixm, *pixt, *pixd;
1194 if (!pixs || pixGetDepth(pixs) != 32)
1195 return (
PIX *)ERROR_PTR(
"pixs not defined or not 32 bpp",
1198 if (pixGetSpp(pixs) != 4) {
1199 L_WARNING(
"no alpha channel; returning a copy\n", __func__);
1200 return pixCopy(NULL, pixs);
1214 pixm = pixThresholdToBinary(pixg, 1);
1217 pixt = pixDisplayLayersRGBA(pixs, 0xffffff00, 600);
1218 pixDisplay(pixt, 0, 0);
1222 pixd = pixCopy(NULL, pixs);
1270 if (pbox) *pbox = NULL;
1271 if (!pixs || pixGetDepth(pixs) != 1)
1272 return (
PIX *)ERROR_PTR(
"pixs undefined or not 1 bpp", __func__, NULL);
1274 return (
PIX *)ERROR_PTR(
"dist must be >= 0", __func__, NULL);
1278 pixClipToForeground(pixs, NULL, &box1);
1280 L_WARNING(
"no ON pixels in mask\n", __func__);
1281 return pixCreateTemplate(pixs);
1284 boxAdjustSides(box1, box1, -dist, dist, -dist, dist);
1285 pixGetDimensions(pixs, &w, &h, NULL);
1286 box2 = boxClipToRectangle(box1, w, h);
1288 pix1 = pixClipRectangle(pixs, box2, NULL);
1291 pix1 = pixCopy(NULL, pixs);
1295 pixd = pixConvert1To8(NULL, pix1, 0, 255);
1302 pixd = pixDistanceFunction(pix1, 8, 8, L_BOUNDARY_FG);
1303 pixMultConstantGray(pixd, 256.0f / dist);
1337l_int32 empty, bx, by;
1338l_float32 rval, gval, bval;
1340PIX *pix1, *pix2, *pix3;
1343 return ERROR_INT(
"&pval not defined", __func__, 1);
1345 if (!pixs || pixGetDepth(pixs) != 32)
1346 return ERROR_INT(
"pixs undefined or not 32 bpp", __func__, 1);
1347 if (!pixm || pixGetDepth(pixm) != 1)
1348 return ERROR_INT(
"pixm undefined or not 1 bpp", __func__, 1);
1350 return ERROR_INT(
"box not defined", __func__, 1);
1352 return ERROR_INT(
"dist must be >= 0", __func__, 1);
1357 box1 = boxAdjustSides(NULL, box, -dist - 5, dist + 5, -dist - 5, dist + 5);
1358 pix1 = pixClipRectangle(pixm, box1, &box2);
1362 pix2 = pixCopy(NULL, pix1);
1364 snprintf(op,
sizeof(op),
"d%d.%d", 2 * dist, 2 * dist);
1365 pix2 = pixMorphSequence(pix1, op, 0);
1370 pix3 = pixCopy(NULL, pix2);
1371 pixDilateBrick(pix3, pix3, 11, 11);
1372 pixXor(pix3, pix3, pix2);
1376 boxGetGeometry(box2, &bx, &by, NULL, NULL);
1377 pixGetAverageMaskedRGB(pixs, pix3, bx, by, 1,
L_MEAN_ABSVAL,
1378 &rval, &gval, &bval);
1379 composeRGBPixel((l_int32)(rval + 0.5), (l_int32)(gval + 0.5),
1380 (l_int32)(bval + 0.5), pval);
1382 L_WARNING(
"no pixels found\n", __func__);
1386 lept_rmdir(
"masknear");
1387 lept_mkdir(
"masknear");
1388 pixWriteDebug(
"/tmp/masknear/input.png", pix1, IFF_PNG);
1389 pixWriteDebug(
"/tmp/masknear/adjusted.png", pix2, IFF_PNG);
1390 pixWriteDebug(
"/tmp/masknear/outerfive.png", pix3, IFF_PNG);
1391 lept_stderr(
"Input box; with adjusted sides; clipped\n");
1392 boxPrintStreamInfo(stderr, box);
1393 boxPrintStreamInfo(stderr, box1);
1394 boxPrintStreamInfo(stderr, box2);
1435 if (!pixm || pixGetDepth(pixm) != 1)
1436 return (
PIX *)ERROR_PTR(
"pixm undefined or not 1 bpp", __func__, NULL);
1439 pix1 = pixConvertTo32(pixs);
1441 pixGetDimensions(pixm, &w, &h, NULL);
1442 pix1 = pixCreate(w, h, 32);
1447 pix2 = pixDilate(NULL, pixm, sel);
1449 pix2 = pixClone(pixm);
1485 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
1488 if ((pixd = pixCopy(pixd, pixs)) == NULL)
1489 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, NULL);
1491 pixRasterop(pixd, 0, 0, pixGetWidth(pixd), pixGetHeight(pixd),
1535 return (
PIX *)ERROR_PTR(
"pixs1 not defined", __func__, pixd);
1537 return (
PIX *)ERROR_PTR(
"pixs2 not defined", __func__, pixd);
1539 return (
PIX *)ERROR_PTR(
"cannot have pixs2 == pixd", __func__, pixd);
1540 if (pixGetDepth(pixs1) != pixGetDepth(pixs2))
1541 return (
PIX *)ERROR_PTR(
"depths of pixs* unequal", __func__, pixd);
1543#if EQUAL_SIZE_WARNING
1544 if (!pixSizesEqual(pixs1, pixs2))
1545 L_WARNING(
"pixs1 and pixs2 not equal sizes\n", __func__);
1549 if ((pixd = pixCopy(pixd, pixs1)) == NULL)
1550 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, pixd);
1553 pixRasterop(pixd, 0, 0, pixGetWidth(pixd), pixGetHeight(pixd),
1597 return (
PIX *)ERROR_PTR(
"pixs1 not defined", __func__, pixd);
1599 return (
PIX *)ERROR_PTR(
"pixs2 not defined", __func__, pixd);
1601 return (
PIX *)ERROR_PTR(
"cannot have pixs2 == pixd", __func__, pixd);
1602 if (pixGetDepth(pixs1) != pixGetDepth(pixs2))
1603 return (
PIX *)ERROR_PTR(
"depths of pixs* unequal", __func__, pixd);
1605#if EQUAL_SIZE_WARNING
1606 if (!pixSizesEqual(pixs1, pixs2))
1607 L_WARNING(
"pixs1 and pixs2 not equal sizes\n", __func__);
1611 if ((pixd = pixCopy(pixd, pixs1)) == NULL)
1612 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, pixd);
1615 pixRasterop(pixd, 0, 0, pixGetWidth(pixd), pixGetHeight(pixd),
1659 return (
PIX *)ERROR_PTR(
"pixs1 not defined", __func__, pixd);
1661 return (
PIX *)ERROR_PTR(
"pixs2 not defined", __func__, pixd);
1663 return (
PIX *)ERROR_PTR(
"cannot have pixs2 == pixd", __func__, pixd);
1664 if (pixGetDepth(pixs1) != pixGetDepth(pixs2))
1665 return (
PIX *)ERROR_PTR(
"depths of pixs* unequal", __func__, pixd);
1667#if EQUAL_SIZE_WARNING
1668 if (!pixSizesEqual(pixs1, pixs2))
1669 L_WARNING(
"pixs1 and pixs2 not equal sizes\n", __func__);
1673 if ((pixd = pixCopy(pixd, pixs1)) == NULL)
1674 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, pixd);
1677 pixRasterop(pixd, 0, 0, pixGetWidth(pixd), pixGetHeight(pixd),
1724 return (
PIX *)ERROR_PTR(
"pixs1 not defined", __func__, pixd);
1726 return (
PIX *)ERROR_PTR(
"pixs2 not defined", __func__, pixd);
1727 if (pixGetDepth(pixs1) != pixGetDepth(pixs2))
1728 return (
PIX *)ERROR_PTR(
"depths of pixs* unequal", __func__, pixd);
1730#if EQUAL_SIZE_WARNING
1731 if (!pixSizesEqual(pixs1, pixs2))
1732 L_WARNING(
"pixs1 and pixs2 not equal sizes\n", __func__);
1735 pixGetDimensions(pixs1, &w, &h, NULL);
1737 pixd = pixCopy(NULL, pixs1);
1740 }
else if (pixd == pixs1) {
1743 }
else if (pixd == pixs2) {
1747 pixCopy(pixd, pixs1);
1780l_int32 w, h, wpl, i, j, fullwords, endbits;
1782l_uint32 *data, *line;
1785 return ERROR_INT(
"&empty not defined", __func__, 1);
1788 return ERROR_INT(
"pix not defined", __func__, 1);
1790 w = pixGetWidth(pix) * pixGetDepth(pix);
1791 h = pixGetHeight(pix);
1792 wpl = pixGetWpl(pix);
1793 data = pixGetData(pix);
1796 endmask = (endbits == 0) ? 0 : (0xffffffffU << (32 - endbits));
1798 for (i = 0; i < h; i++) {
1799 line = data + wpl * i;
1800 for (j = 0; j < fullwords; j++)
1806 if (*line & endmask) {
1831 return ERROR_INT(
"&fract not defined", __func__, 1);
1833 if (!pix || pixGetDepth(pix) != 1)
1834 return ERROR_INT(
"pix not defined or not 1 bpp", __func__, 1);
1837 pixGetDimensions(pix, &w, &h, NULL);
1838 *pfract = (l_float32)count / (l_float32)(w * h);
1852l_int32 d, i, n, count;
1858 return (
NUMA *)ERROR_PTR(
"pix not defined", __func__, NULL);
1860 if ((n = pixaGetCount(pixa)) == 0)
1861 return numaCreate(1);
1863 pix = pixaGetPix(pixa, 0,
L_CLONE);
1864 d = pixGetDepth(pix);
1867 return (
NUMA *)ERROR_PTR(
"pixa not 1 bpp", __func__, NULL);
1869 if ((na = numaCreate(n)) == NULL)
1870 return (
NUMA *)ERROR_PTR(
"na not made", __func__, NULL);
1872 for (i = 0; i < n; i++) {
1873 pix = pixaGetPix(pixa, i,
L_CLONE);
1875 numaAddNumber(na, count);
1898l_int32 w, h, wpl, i, j;
1899l_int32 fullwords, endbits, sum;
1904 return ERROR_INT(
"&count not defined", __func__, 1);
1906 if (!pixs || pixGetDepth(pixs) != 1)
1907 return ERROR_INT(
"pixs not defined or not 1 bpp", __func__, 1);
1910 pixGetDimensions(pixs, &w, &h, NULL);
1911 wpl = pixGetWpl(pixs);
1912 data = pixGetData(pixs);
1915 endmask = (endbits == 0) ? 0 : (0xffffffffU << (32 - endbits));
1918 for (i = 0; i < h; i++, data += wpl) {
1919 for (j = 0; j < fullwords; j++) {
1920 l_uint32 word = data[j];
1922 sum += tab[word & 0xff] +
1923 tab[(word >> 8) & 0xff] +
1924 tab[(word >> 16) & 0xff] +
1925 tab[(word >> 24) & 0xff];
1929 l_uint32 word = data[j] & endmask;
1931 sum += tab[word & 0xff] +
1932 tab[(word >> 8) & 0xff] +
1933 tab[(word >> 16) & 0xff] +
1934 tab[(word >> 24) & 0xff];
1940 if (!tab8) LEPT_FREE(tab);
1960l_int32 w, h, bx, by, bw, bh;
1965 return ERROR_INT(
"&count not defined", __func__, 1);
1967 if (!pixs || pixGetDepth(pixs) != 1)
1968 return ERROR_INT(
"pixs not defined or not 1 bpp", __func__, 1);
1971 pixGetDimensions(pixs, &w, &h, NULL);
1972 if ((box1 = boxClipToRectangle(box, w, h)) == NULL)
1973 return ERROR_INT(
"box1 not made", __func__, 1);
1974 boxGetGeometry(box1, &bx, &by, &bw, &bh);
1975 pix1 = pixCreate(bw, bh, 1);
1976 pixRasterop(pix1, 0, 0, bw, bh,
PIX_SRC, pixs, bx, by);
2005l_int32 i, j, w, h, wpl, count, xstart, xend, ystart, yend, bw, bh;
2006l_uint32 *line, *data;
2009 if (!pix || pixGetDepth(pix) != 1)
2010 return (
NUMA *)ERROR_PTR(
"pix undefined or not 1 bpp", __func__, NULL);
2014 pixGetDimensions(pix, &w, &h, NULL);
2015 if (boxClipToRectangleParams(box, w, h, &xstart, &ystart, &xend, ¥d,
2017 return (
NUMA *)ERROR_PTR(
"invalid clipping box", __func__, NULL);
2019 if ((na = numaCreate(bh)) == NULL)
2020 return (
NUMA *)ERROR_PTR(
"na not made", __func__, NULL);
2021 numaSetParameters(na, ystart, 1);
2022 data = pixGetData(pix);
2023 wpl = pixGetWpl(pix);
2024 for (i = ystart; i < yend; i++) {
2026 line = data + i * wpl;
2027 for (j = xstart; j < xend; j++) {
2031 numaAddNumber(na, count);
2055l_int32 i, j, w, h, wpl, count, xstart, xend, ystart, yend, bw, bh;
2056l_uint32 *line, *data;
2059 if (!pix || pixGetDepth(pix) != 1)
2060 return (
NUMA *)ERROR_PTR(
"pix undefined or not 1 bpp", __func__, NULL);
2064 pixGetDimensions(pix, &w, &h, NULL);
2065 if (boxClipToRectangleParams(box, w, h, &xstart, &ystart, &xend, ¥d,
2067 return (
NUMA *)ERROR_PTR(
"invalid clipping box", __func__, NULL);
2069 if ((na = numaCreate(bw)) == NULL)
2070 return (
NUMA *)ERROR_PTR(
"na not made", __func__, NULL);
2071 numaSetParameters(na, xstart, 1);
2072 data = pixGetData(pix);
2073 wpl = pixGetWpl(pix);
2074 for (j = xstart; j < xend; j++) {
2076 for (i = ystart; i < yend; i++) {
2077 line = data + i * wpl;
2081 numaAddNumber(na, count);
2103 if (!pix || pixGetDepth(pix) != 1)
2104 return (
NUMA *)ERROR_PTR(
"pix undefined or not 1 bpp", __func__, NULL);
2106 h = pixGetHeight(pix);
2107 if ((na = numaCreate(h)) == NULL)
2108 return (
NUMA *)ERROR_PTR(
"na not made", __func__, NULL);
2111 for (i = 0; i < h; i++) {
2113 numaAddNumber(na, count);
2116 if (!tab8) LEPT_FREE(tab);
2130l_int32 i, j, w, h, wpl;
2131l_uint32 *line, *data;
2135 if (!pix || pixGetDepth(pix) != 1)
2136 return (
NUMA *)ERROR_PTR(
"pix undefined or not 1 bpp", __func__, NULL);
2138 pixGetDimensions(pix, &w, &h, NULL);
2139 if ((na = numaCreate(w)) == NULL)
2140 return (
NUMA *)ERROR_PTR(
"na not made", __func__, NULL);
2141 numaSetCount(na, w);
2142 array = numaGetFArray(na,
L_NOCOPY);
2143 data = pixGetData(pix);
2144 wpl = pixGetWpl(pix);
2145 for (i = 0; i < h; i++) {
2146 line = data + wpl * i;
2147 for (j = 0; j < w; j++) {
2172l_uint32 word, endmask;
2173l_int32 j, w, h, wpl;
2174l_int32 fullwords, endbits, sum;
2179 return ERROR_INT(
"&count not defined", __func__, 1);
2181 if (!pix || pixGetDepth(pix) != 1)
2182 return ERROR_INT(
"pix not defined or not 1 bpp", __func__, 1);
2184 pixGetDimensions(pix, &w, &h, NULL);
2185 if (row < 0 || row >= h)
2186 return ERROR_INT(
"row out of bounds", __func__, 1);
2187 wpl = pixGetWpl(pix);
2188 line = pixGetData(pix) + row * wpl;
2191 endmask = (endbits == 0) ? 0 : (0xffffffffU << (32 - endbits));
2195 for (j = 0; j < fullwords; j++) {
2198 sum += tab[word & 0xff] +
2199 tab[(word >> 8) & 0xff] +
2200 tab[(word >> 16) & 0xff] +
2201 tab[(word >> 24) & 0xff];
2205 word = line[j] & endmask;
2207 sum += tab[word & 0xff] +
2208 tab[(word >> 8) & 0xff] +
2209 tab[(word >> 16) & 0xff] +
2210 tab[(word >> 24) & 0xff];
2215 if (!tab8) LEPT_FREE(tab);
2231l_int32 i, j, w, h, wpl;
2232l_uint32 *line, *data;
2236 if (!pix || pixGetDepth(pix) != 1)
2237 return (
NUMA *)ERROR_PTR(
"pix undefined or not 1 bpp", __func__, NULL);
2238 if (order != 1 && order != 2)
2239 return (
NUMA *)ERROR_PTR(
"order of moment not 1 or 2", __func__, NULL);
2241 pixGetDimensions(pix, &w, &h, NULL);
2242 if ((na = numaCreate(w)) == NULL)
2243 return (
NUMA *)ERROR_PTR(
"na not made", __func__, NULL);
2244 numaSetCount(na, w);
2245 array = numaGetFArray(na,
L_NOCOPY);
2246 data = pixGetData(pix);
2247 wpl = pixGetWpl(pix);
2248 for (i = 0; i < h; i++) {
2249 line = data + wpl * i;
2250 for (j = 0; j < w; j++) {
2289l_uint32 word, endmask;
2291l_int32 w, h, wpl, i, j;
2292l_int32 fullwords, endbits, sum;
2293l_uint32 *line, *data;
2296 return ERROR_INT(
"&above not defined", __func__, 1);
2298 if (!pix || pixGetDepth(pix) != 1)
2299 return ERROR_INT(
"pix not defined or not 1 bpp", __func__, 1);
2302 pixGetDimensions(pix, &w, &h, NULL);
2303 wpl = pixGetWpl(pix);
2304 data = pixGetData(pix);
2307 endmask = 0xffffffff << (32 - endbits);
2310 for (i = 0; i < h; i++) {
2311 line = data + wpl * i;
2312 for (j = 0; j < fullwords; j++) {
2315 sum += tab[word & 0xff] +
2316 tab[(word >> 8) & 0xff] +
2317 tab[(word >> 16) & 0xff] +
2318 tab[(word >> 24) & 0xff];
2322 word = line[j] & endmask;
2324 sum += tab[word & 0xff] +
2325 tab[(word >> 8) & 0xff] +
2326 tab[(word >> 16) & 0xff] +
2327 tab[(word >> 24) & 0xff];
2332 if (!tab8) LEPT_FREE(tab);
2337 if (!tab8) LEPT_FREE(tab);
2360 tab = (l_int32 *)LEPT_CALLOC(256,
sizeof(l_int32));
2361 for (i = 0; i < 256; i++) {
2363 tab[i] = (
byte & 0x1) +
2364 ((
byte >> 1) & 0x1) +
2365 ((
byte >> 2) & 0x1) +
2366 ((
byte >> 3) & 0x1) +
2367 ((
byte >> 4) & 0x1) +
2368 ((
byte >> 5) & 0x1) +
2369 ((
byte >> 6) & 0x1) +
2370 ((
byte >> 7) & 0x1);
2399 tab = (l_int32 *)LEPT_CALLOC(256,
sizeof(l_int32));
2402 for (i = 2; i < 4; i++) {
2403 tab[i] = tab[i - 2] + 6;
2405 for (i = 4; i < 8; i++) {
2406 tab[i] = tab[i - 4] + 5;
2408 for (i = 8; i < 16; i++) {
2409 tab[i] = tab[i - 8] + 4;
2411 for (i = 16; i < 32; i++) {
2412 tab[i] = tab[i - 16] + 3;
2414 for (i = 32; i < 64; i++) {
2415 tab[i] = tab[i - 32] + 2;
2417 for (i = 64; i < 128; i++) {
2418 tab[i] = tab[i - 64] + 1;
2420 for (i = 128; i < 256; i++) {
2421 tab[i] = tab[i - 128];
2451l_int32 i, j, w, h, d, wpl, xstart, xend, ystart, yend, bw, bh;
2452l_uint32 *line, *data;
2457 return (
NUMA *)ERROR_PTR(
"pix not defined", __func__, NULL);
2458 pixGetDimensions(pix, &w, &h, &d);
2459 if (d != 8 && d != 16)
2460 return (
NUMA *)ERROR_PTR(
"pix not 8 or 16 bpp", __func__, NULL);
2462 return (
NUMA *)ERROR_PTR(
"invalid type", __func__, NULL);
2463 if (pixGetColormap(pix) != NULL)
2464 return (
NUMA *)ERROR_PTR(
"pix colormapped", __func__, NULL);
2466 if (boxClipToRectangleParams(box, w, h, &xstart, &ystart, &xend, ¥d,
2468 return (
NUMA *)ERROR_PTR(
"invalid clipping box", __func__, NULL);
2470 norm = 1. / (l_float32)bw;
2471 if ((na = numaCreate(bh)) == NULL)
2472 return (
NUMA *)ERROR_PTR(
"na not made", __func__, NULL);
2473 numaSetParameters(na, ystart, 1);
2474 data = pixGetData(pix);
2475 wpl = pixGetWpl(pix);
2476 for (i = ystart; i < yend; i++) {
2478 line = data + i * wpl;
2480 for (j = xstart; j < xend; j++)
2483 sum = bw * 255 - sum;
2485 for (j = xstart; j < xend; j++)
2488 sum = bw * 0xffff - sum;
2490 numaAddNumber(na, (l_float32)(norm * sum));
2518l_int32 i, j, w, h, d, wpl, xstart, xend, ystart, yend, bw, bh;
2519l_uint32 *line, *data;
2524 return (
NUMA *)ERROR_PTR(
"pix not defined", __func__, NULL);
2525 pixGetDimensions(pix, &w, &h, &d);
2527 if (d != 8 && d != 16)
2528 return (
NUMA *)ERROR_PTR(
"pix not 8 or 16 bpp", __func__, NULL);
2530 return (
NUMA *)ERROR_PTR(
"invalid type", __func__, NULL);
2531 if (pixGetColormap(pix) != NULL)
2532 return (
NUMA *)ERROR_PTR(
"pix colormapped", __func__, NULL);
2534 if (boxClipToRectangleParams(box, w, h, &xstart, &ystart, &xend, ¥d,
2536 return (
NUMA *)ERROR_PTR(
"invalid clipping box", __func__, NULL);
2538 if ((na = numaCreate(bw)) == NULL)
2539 return (
NUMA *)ERROR_PTR(
"na not made", __func__, NULL);
2540 numaSetParameters(na, xstart, 1);
2541 norm = 1.f / (l_float32)bh;
2542 data = pixGetData(pix);
2543 wpl = pixGetWpl(pix);
2544 for (j = xstart; j < xend; j++) {
2547 for (i = ystart; i < yend; i++) {
2548 line = data + i * wpl;
2552 sum = bh * 255 - sum;
2554 for (i = ystart; i < yend; i++) {
2555 line = data + i * wpl;
2559 sum = bh * 0xffff - sum;
2561 numaAddNumber(na, (l_float32)(norm * sum));
2607l_int32 w, h, d, wpls, wm, hm, dm, wplm, val, count;
2608l_int32 i, j, xstart, xend, ystart, yend;
2609l_uint32 *datas, *datam = NULL, *lines, *linem = NULL;
2613 return ERROR_INT(
"&ave not defined", __func__, 1);
2616 return ERROR_INT(
"pixs not defined", __func__, 1);
2617 if (pixGetColormap(pixs) != NULL)
2618 return ERROR_INT(
"pixs is colormapped", __func__, 1);
2619 pixGetDimensions(pixs, &w, &h, &d);
2620 if (d != 1 && d != 2 && d != 4 && d != 8)
2621 return ERROR_INT(
"pixs not 1, 2, 4 or 8 bpp", __func__, 1);
2623 pixGetDimensions(pixm, &wm, &hm, &dm);
2625 return ERROR_INT(
"pixm not 1 bpp", __func__, 1);
2630 return ERROR_INT(
"subsamp must be >= 1", __func__, 1);
2632 if (boxClipToRectangleParams(box, w, h, &xstart, &ystart, &xend, ¥d,
2634 return ERROR_INT(
"invalid clipping box", __func__, 1);
2636 datas = pixGetData(pixs);
2637 wpls = pixGetWpl(pixs);
2639 datam = pixGetData(pixm);
2640 wplm = pixGetWpl(pixm);
2644 for (i = ystart; i < yend; i += subsamp) {
2645 lines = datas + i * wpls;
2647 linem = datam + i * wplm;
2648 for (j = xstart; j < xend; j += subsamp) {
2659 if (val >= minval && val <= maxval) {
2668 *pave = sum / (l_float32)count;
2710l_int32 w, h, wpls, wm, hm, dm, wplm, i, j, xstart, xend, ystart, yend;
2711l_int32 rval, gval, bval, rave, gave, bave, count;
2712l_uint32 *datas, *datam = NULL, *lines, *linem = NULL;
2714l_float64 rsum, gsum, bsum;
2717 return ERROR_INT(
"&ave not defined", __func__, 1);
2719 if (!pixs || pixGetDepth(pixs) != 32)
2720 return ERROR_INT(
"pixs undefined or not 32 bpp", __func__, 1);
2721 pixGetDimensions(pixs, &w, &h, NULL);
2723 pixGetDimensions(pixm, &wm, &hm, &dm);
2725 return ERROR_INT(
"pixm not 1 bpp", __func__, 1);
2730 return ERROR_INT(
"subsamp must be >= 1", __func__, 1);
2732 if (boxClipToRectangleParams(box, w, h, &xstart, &ystart, &xend, ¥d,
2734 return ERROR_INT(
"invalid clipping box", __func__, 1);
2736 datas = pixGetData(pixs);
2737 wpls = pixGetWpl(pixs);
2739 datam = pixGetData(pixm);
2740 wplm = pixGetWpl(pixm);
2742 rsum = gsum = bsum = 0.0;
2744 for (i = ystart; i < yend; i += subsamp) {
2745 lines = datas + i * wpls;
2747 linem = datam + i * wplm;
2748 for (j = xstart; j < xend; j += subsamp) {
2751 pixel = *(lines + j);
2752 extractRGBValues(pixel, &rval, &gval, &bval);
2762 rave = (l_uint32)(rsum / (l_float64)count);
2763 gave = (l_uint32)(gsum / (l_float64)count);
2764 bave = (l_uint32)(bsum / (l_float64)count);
2765 composeRGBPixel(rave, gave, bave, pave);
2792l_int32 i, j, w, h, d, wpl, xstart, xend, ystart, yend, bw, bh, val;
2793l_uint32 *line, *data;
2794l_float64 sum1, sum2, norm, ave, var, rootvar;
2798 return (
NUMA *)ERROR_PTR(
"pix not defined", __func__, NULL);
2799 pixGetDimensions(pix, &w, &h, &d);
2800 if (d != 8 && d != 16)
2801 return (
NUMA *)ERROR_PTR(
"pix not 8 or 16 bpp", __func__, NULL);
2802 if (pixGetColormap(pix) != NULL)
2803 return (
NUMA *)ERROR_PTR(
"pix colormapped", __func__, NULL);
2805 if (boxClipToRectangleParams(box, w, h, &xstart, &ystart, &xend, ¥d,
2807 return (
NUMA *)ERROR_PTR(
"invalid clipping box", __func__, NULL);
2809 if ((na = numaCreate(bh)) == NULL)
2810 return (
NUMA *)ERROR_PTR(
"na not made", __func__, NULL);
2811 numaSetParameters(na, ystart, 1);
2812 norm = 1. / (l_float32)bw;
2813 data = pixGetData(pix);
2814 wpl = pixGetWpl(pix);
2815 for (i = ystart; i < yend; i++) {
2817 line = data + i * wpl;
2818 for (j = xstart; j < xend; j++) {
2824 sum2 += (l_float64)(val) * val;
2827 var = norm * sum2 - ave * ave;
2828 rootvar = sqrt(var);
2829 numaAddNumber(na, (l_float32)rootvar);
2855l_int32 i, j, w, h, d, wpl, xstart, xend, ystart, yend, bw, bh, val;
2856l_uint32 *line, *data;
2857l_float64 sum1, sum2, norm, ave, var, rootvar;
2861 return (
NUMA *)ERROR_PTR(
"pix not defined", __func__, NULL);
2862 pixGetDimensions(pix, &w, &h, &d);
2863 if (d != 8 && d != 16)
2864 return (
NUMA *)ERROR_PTR(
"pix not 8 or 16 bpp", __func__, NULL);
2865 if (pixGetColormap(pix) != NULL)
2866 return (
NUMA *)ERROR_PTR(
"pix colormapped", __func__, NULL);
2868 if (boxClipToRectangleParams(box, w, h, &xstart, &ystart, &xend, ¥d,
2870 return (
NUMA *)ERROR_PTR(
"invalid clipping box", __func__, NULL);
2872 if ((na = numaCreate(bw)) == NULL)
2873 return (
NUMA *)ERROR_PTR(
"na not made", __func__, NULL);
2874 numaSetParameters(na, xstart, 1);
2875 norm = 1. / (l_float32)bh;
2876 data = pixGetData(pix);
2877 wpl = pixGetWpl(pix);
2878 for (j = xstart; j < xend; j++) {
2880 for (i = ystart; i < yend; i++) {
2881 line = data + wpl * i;
2887 sum2 += (l_float64)(val) * val;
2890 var = norm * sum2 - ave * ave;
2891 rootvar = sqrt(var);
2892 numaAddNumber(na, (l_float32)rootvar);
2910 l_float32 *prootvar)
2912l_int32 w, h, d, wpl, i, j, xstart, xend, ystart, yend, bw, bh, val;
2913l_uint32 *data, *line;
2914l_float64 sum1, sum2, norm, ave, var;
2917 return ERROR_INT(
"&rootvar not defined", __func__, 1);
2920 return ERROR_INT(
"pix not defined", __func__, 1);
2921 pixGetDimensions(pix, &w, &h, &d);
2922 if (d != 1 && d != 2 && d != 4 && d != 8)
2923 return ERROR_INT(
"pix not 1, 2, 4 or 8 bpp", __func__, 1);
2924 if (pixGetColormap(pix) != NULL)
2925 return ERROR_INT(
"pix is colormapped", __func__, 1);
2927 if (boxClipToRectangleParams(box, w, h, &xstart, &ystart, &xend, ¥d,
2929 return ERROR_INT(
"invalid clipping box", __func__, 1);
2931 wpl = pixGetWpl(pix);
2932 data = pixGetData(pix);
2934 for (i = ystart; i < yend; i++) {
2935 line = data + i * wpl;
2936 for (j = xstart; j < xend; j++) {
2940 sum2 += (l_float64)(val) * val;
2941 }
else if (d == 2) {
2944 sum2 += (l_float64)(val) * val;
2945 }
else if (d == 4) {
2948 sum2 += (l_float64)(val) * val;
2952 sum2 += (l_float64)(val) * val;
2956 norm = 1.0 / ((l_float64)(bw) * bh);
2958 var = norm * sum2 - ave * ave;
2959 *prootvar = (l_float32)sqrt(var);
2986l_int32 i, j, w, h, wpl, xstart, xend, ystart, yend, bw, bh, val0, val1;
2987l_uint32 *line, *data;
2991 if (!pix || pixGetDepth(pix) != 8)
2992 return (
NUMA *)ERROR_PTR(
"pix undefined or not 8 bpp", __func__, NULL);
2993 if (pixGetColormap(pix) != NULL)
2994 return (
NUMA *)ERROR_PTR(
"pix colormapped", __func__, NULL);
2996 pixGetDimensions(pix, &w, &h, NULL);
2997 if (boxClipToRectangleParams(box, w, h, &xstart, &ystart, &xend, ¥d,
2999 return (
NUMA *)ERROR_PTR(
"invalid clipping box", __func__, NULL);
3001 return (
NUMA *)ERROR_PTR(
"row width must be >= 2", __func__, NULL);
3003 norm = 1. / (l_float32)(bw - 1);
3004 if ((na = numaCreate(bh)) == NULL)
3005 return (
NUMA *)ERROR_PTR(
"na not made", __func__, NULL);
3006 numaSetParameters(na, ystart, 1);
3007 data = pixGetData(pix);
3008 wpl = pixGetWpl(pix);
3009 for (i = ystart; i < yend; i++) {
3011 line = data + i * wpl;
3013 for (j = xstart + 1; j < xend; j++) {
3015 sum += L_ABS(val1 - val0);
3018 numaAddNumber(na, (l_float32)(norm * sum));
3045l_int32 i, j, w, h, wpl, xstart, xend, ystart, yend, bw, bh, val0, val1;
3046l_uint32 *line, *data;
3050 if (!pix || pixGetDepth(pix) != 8)
3051 return (
NUMA *)ERROR_PTR(
"pix undefined or not 8 bpp", __func__, NULL);
3052 if (pixGetColormap(pix) != NULL)
3053 return (
NUMA *)ERROR_PTR(
"pix colormapped", __func__, NULL);
3055 pixGetDimensions(pix, &w, &h, NULL);
3056 if (boxClipToRectangleParams(box, w, h, &xstart, &ystart, &xend, ¥d,
3058 return (
NUMA *)ERROR_PTR(
"invalid clipping box", __func__, NULL);
3060 return (
NUMA *)ERROR_PTR(
"column height must be >= 2", __func__, NULL);
3062 norm = 1. / (l_float32)(bh - 1);
3063 if ((na = numaCreate(bw)) == NULL)
3064 return (
NUMA *)ERROR_PTR(
"na not made", __func__, NULL);
3065 numaSetParameters(na, xstart, 1);
3066 data = pixGetData(pix);
3067 wpl = pixGetWpl(pix);
3068 for (j = xstart; j < xend; j++) {
3070 line = data + ystart * wpl;
3072 for (i = ystart + 1; i < yend; i++) {
3073 line = data + i * wpl;
3075 sum += L_ABS(val1 - val0);
3078 numaAddNumber(na, (l_float32)(norm * sum));
3106 l_float32 *pabsdiff)
3108l_int32 w, h, wpl, i, j, xstart, xend, ystart, yend, bw, bh, val0, val1;
3109l_uint32 *data, *line;
3113 return ERROR_INT(
"&absdiff not defined", __func__, 1);
3115 if (!pix || pixGetDepth(pix) != 8)
3116 return ERROR_INT(
"pix undefined or not 8 bpp", __func__, 1);
3118 return ERROR_INT(
"invalid direction", __func__, 1);
3119 if (pixGetColormap(pix) != NULL)
3120 return ERROR_INT(
"pix is colormapped", __func__, 1);
3122 pixGetDimensions(pix, &w, &h, NULL);
3123 if (boxClipToRectangleParams(box, w, h, &xstart, &ystart, &xend, ¥d,
3125 return ERROR_INT(
"invalid clipping box", __func__, 1);
3127 wpl = pixGetWpl(pix);
3128 data = pixGetData(pix);
3130 norm = 1. / (l_float32)(bh * (bw - 1));
3132 for (i = ystart; i < yend; i++) {
3133 line = data + i * wpl;
3135 for (j = xstart + 1; j < xend; j++) {
3137 sum += L_ABS(val1 - val0);
3142 norm = 1. / (l_float32)(bw * (bh - 1));
3144 for (j = xstart; j < xend; j++) {
3145 line = data + ystart * wpl;
3147 for (i = ystart + 1; i < yend; i++) {
3148 line = data + i * wpl;
3150 sum += L_ABS(val1 - val0);
3155 *pabsdiff = (l_float32)(norm * sum);
3183 l_float32 *pabsdiff)
3185l_int32 w, h, i, j, dir, size, sum;
3189 return ERROR_INT(
"&absdiff not defined", __func__, 1);
3191 if (!pix || pixGetDepth(pix) != 8)
3192 return ERROR_INT(
"pix undefined or not 8 bpp", __func__, 1);
3195 }
else if (x1 == x2) {
3198 return ERROR_INT(
"line is neither horiz nor vert", __func__, 1);
3200 if (pixGetColormap(pix) != NULL)
3201 return ERROR_INT(
"pix is colormapped", __func__, 1);
3203 pixGetDimensions(pix, &w, &h, NULL);
3207 x2 = L_MIN(x2, w - 1);
3209 return ERROR_INT(
"x1 >= x2", __func__, 1);
3211 pixGetPixel(pix, x1, y1, &val0);
3212 for (j = x1 + 1; j <= x2; j++) {
3213 pixGetPixel(pix, j, y1, &val1);
3214 sum += L_ABS((l_int32)val1 - (l_int32)val0);
3219 y2 = L_MIN(y2, h - 1);
3221 return ERROR_INT(
"y1 >= y2", __func__, 1);
3223 pixGetPixel(pix, x1, y1, &val0);
3224 for (i = y1 + 1; i <= y2; i++) {
3225 pixGetPixel(pix, x1, i, &val1);
3226 sum += L_ABS((l_int32)val1 - (l_int32)val0);
3230 *pabsdiff = (l_float32)sum / (l_float32)size;
3264l_int32 i, j, bx, by, bw, bh, w, h, d, wpl, pixval;
3265l_uint32 *data, *line;
3268 return ERROR_INT(
"&count not defined", __func__, 1);
3271 return ERROR_INT(
"pixs not defined", __func__, 1);
3272 d = pixGetDepth(pixs);
3273 if (d != 1 && d != 2 && d != 4 && d != 8)
3274 return ERROR_INT(
"pixs not 1, 2, 4 or 8 bpp", __func__, 1);
3276 return ERROR_INT(
"val < 0", __func__, 1);
3277 if (val > (1 << d) - 1) {
3278 L_ERROR(
"invalid val = %d for depth %d\n", __func__, val, d);
3282 return ERROR_INT(
"sampling factor < 1", __func__, 1);
3284 pixGetDimensions(pixs, &w, &h, NULL);
3285 data = pixGetData(pixs);
3286 wpl = pixGetWpl(pixs);
3288 for (i = 0; i < h; i += factor) {
3289 line = data + i * wpl;
3290 for (j = 0; j < w; j += factor) {
3293 }
else if (d == 1) {
3295 }
else if (d == 2) {
3300 if (pixval == val) (*pcount)++;
3304 boxGetGeometry(box, &bx, &by, &bw, &bh);
3305 for (i = 0; i < bh; i += factor) {
3306 if (by + i < 0 || by + i >= h)
continue;
3307 line = data + (by + i) * wpl;
3308 for (j = 0; j < bw; j += factor) {
3309 if (bx + j < 0 || bx + j >= w)
continue;
3312 }
else if (d == 1) {
3314 }
else if (d == 2) {
3319 if (pixval == val) (*pcount)++;
3325 *pcount = *pcount * factor * factor;
3359l_int32 wt, ht, d, i, j, nx, ny;
3360PIX *pixd, *pixsfx, *pixsfy, *pixsfxy, *pix;
3363 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
3364 pixGetDimensions(pixs, &wt, &ht, &d);
3365 if (wt <= 0 || ht <= 0)
3366 return (
PIX *)ERROR_PTR(
"pixs size illegal", __func__, NULL);
3367 if (d != 8 && d != 32)
3368 return (
PIX *)ERROR_PTR(
"depth not 32 bpp", __func__, NULL);
3370 if ((pixd = pixCreate(w, h, d)) == NULL)
3371 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, NULL);
3372 pixCopySpp(pixd, pixs);
3374 nx = (w + wt - 1) / wt;
3375 ny = (h + ht - 1) / ht;
3376 pixsfx = pixFlipLR(NULL, pixs);
3377 pixsfy = pixFlipTB(NULL, pixs);
3378 pixsfxy = pixFlipTB(NULL, pixsfx);
3379 for (i = 0; i < ny; i++) {
3380 for (j = 0; j < nx; j++) {
3382 if ((i & 1) && !(j & 1))
3384 else if (!(i & 1) && (j & 1))
3386 else if ((i & 1) && (j & 1))
3388 pixRasterop(pixd, j * wt, i * ht, wt, ht,
PIX_SRC, pix, 0, 0);
3392 pixDestroy(&pixsfx);
3393 pixDestroy(&pixsfy);
3394 pixDestroy(&pixsfxy);
3437l_int32 w, h, i, n, bestindex;
3438l_float32 var_of_mean, median_of_mean, median_of_stdev, mean_val, stdev_val;
3439l_float32 mindels, bestdelm, delm, dels, mean, stdev;
3441NUMA *namean, *nastdev;
3446 return ERROR_INT(
"&boxtile not defined", __func__, 1);
3449 return ERROR_INT(
"pixs not defined", __func__, 1);
3451 return ERROR_INT(
"box not defined", __func__, 1);
3452 if (searchdir != L_HORIZ && searchdir != L_VERT)
3453 return ERROR_INT(
"invalid searchdir", __func__, 1);
3455 return ERROR_INT(
"mindist must be >= 0", __func__, 1);
3457 return ERROR_INT(
"tsize must be > 1", __func__, 1);
3459 L_WARNING(
"ntiles = %d; larger than suggested max of 7\n",
3464 pixGetDimensions(pixs, &w, &h, NULL);
3468 return ERROR_INT(
"no tiles found", __func__, 1);
3471 pixa = pixClipRectangles(pixs, boxa);
3472 n = pixaGetCount(pixa);
3473 namean = numaCreate(n);
3474 nastdev = numaCreate(n);
3475 for (i = 0; i < n; i++) {
3476 pix = pixaGetPix(pixa, i,
L_CLONE);
3477 pixg = pixConvertRGBToGray(pix, 0.33f, 0.34f, 0.33f);
3478 pixGetAverageMasked(pixg, NULL, 0, 0, 1,
L_MEAN_ABSVAL, &mean);
3480 numaAddNumber(namean, mean);
3481 numaAddNumber(nastdev, stdev);
3493 numaGetStatsUsingHistogram(namean, 256, NULL, NULL, NULL, &var_of_mean,
3494 &median_of_mean, 0.0, NULL, NULL);
3495 numaGetStatsUsingHistogram(nastdev, 256, NULL, NULL, NULL, NULL,
3496 &median_of_stdev, 0.0, NULL, NULL);
3500 for (i = 0; i < n; i++) {
3501 numaGetFValue(namean, i, &mean_val);
3502 numaGetFValue(nastdev, i, &stdev_val);
3503 if (var_of_mean == 0.0) {
3507 delm = L_ABS(mean_val - median_of_mean) / sqrt(var_of_mean);
3508 dels = stdev_val / median_of_stdev;
3511 if (dels < mindels) {
3513 lept_stderr(
"i = %d, mean = %7.3f, delm = %7.3f,"
3514 " stdev = %7.3f, dels = %7.3f\n",
3515 i, mean_val, delm, stdev_val, dels);
3523 *pboxtile = boxaGetBox(boxa, bestindex,
L_COPY);
3526 L_INFO(
"median of mean = %7.3f\n", __func__, median_of_mean);
3527 L_INFO(
"standard dev of mean = %7.3f\n", __func__, sqrt(var_of_mean));
3528 L_INFO(
"median of stdev = %7.3f\n", __func__, median_of_stdev);
3529 L_INFO(
"best tile: index = %d\n", __func__, bestindex);
3530 L_INFO(
"delta from median in units of stdev = %5.3f\n",
3531 __func__, bestdelm);
3532 L_INFO(
"stdev as fraction of median stdev = %5.3f\n",
3536 numaDestroy(&namean);
3537 numaDestroy(&nastdev);
3569l_int32 bx, by, bw, bh, left, right, top, bot, i, j, nrows, ncols;
3570l_int32 x0, y0, x, y, w_avail, w_needed, h_avail, h_needed, t_avail;
3575 return (
BOXA *)ERROR_PTR(
"box not defined", __func__, NULL);
3577 return (
BOXA *)ERROR_PTR(
"no tiles requested", __func__, NULL);
3579 boxGetGeometry(box, &bx, &by, &bw, &bh);
3580 if (searchdir == L_HORIZ) {
3584 right = w - bx - bw + 1;
3585 w_avail = L_MAX(left, right) - mindist;
3586 if (tsize & 1) tsize++;
3587 if (w_avail < tsize) {
3588 L_ERROR(
"tsize = %d, w_avail = %d\n", __func__, tsize, w_avail);
3591 w_needed = tsize + (ntiles - 1) * (tsize / 2);
3592 if (w_needed > w_avail) {
3593 t_avail = 1 + 2 * (w_avail - tsize) / tsize;
3594 L_WARNING(
"ntiles = %d; room for only %d\n", __func__,
3597 w_needed = tsize + (ntiles - 1) * (tsize / 2);
3599 nrows = L_MAX(1, 1 + 2 * (bh - tsize) / tsize);
3602 boxa = boxaCreate(0);
3606 x0 = bx + bw + mindist;
3607 for (i = 0; i < nrows; i++) {
3608 y = by + i * tsize / 2;
3609 for (j = 0; j < ntiles; j++) {
3610 x = x0 + j * tsize / 2;
3611 box1 = boxCreate(x, y, tsize, tsize);
3618 bot = h - by - bh + 1;
3619 h_avail = L_MAX(top, bot) - mindist;
3620 if (h_avail < tsize) {
3621 L_ERROR(
"tsize = %d, h_avail = %d\n", __func__, tsize, h_avail);
3624 h_needed = tsize + (ntiles - 1) * (tsize / 2);
3625 if (h_needed > h_avail) {
3626 t_avail = 1 + 2 * (h_avail - tsize) / tsize;
3627 L_WARNING(
"ntiles = %d; room for only %d\n", __func__,
3630 h_needed = tsize + (ntiles - 1) * (tsize / 2);
3632 ncols = L_MAX(1, 1 + 2 * (bw - tsize) / tsize);
3635 boxa = boxaCreate(0);
3639 y0 = by + bh + mindist;
3640 for (j = 0; j < ncols; j++) {
3641 x = bx + j * tsize / 2;
3642 for (i = 0; i < ntiles; i++) {
3643 y = y0 + i * tsize / 2;
3644 box1 = boxCreate(x, y, tsize, tsize);
#define GET_DATA_QBIT(pdata, n)
#define GET_DATA_TWO_BYTES(pdata, n)
#define SET_DATA_BIT(pdata, n)
#define SET_DATA_DIBIT(pdata, n, val)
#define SET_DATA_TWO_BYTES(pdata, n, val)
#define GET_DATA_BYTE(pdata, n)
#define GET_DATA_DIBIT(pdata, n)
#define SET_DATA_BYTE(pdata, n, val)
#define GET_DATA_BIT(pdata, n)
#define SET_DATA_QBIT(pdata, n, val)
PIX * pixOr(PIX *pixd, PIX *pixs1, PIX *pixs2)
pixOr()
l_int32 * makePixelSumTab8(void)
makePixelSumTab8()
NUMA * pixaCountPixels(PIXA *pixa)
pixaCountPixels()
l_ok pixGetColorNearMaskBoundary(PIX *pixs, PIX *pixm, BOX *box, l_int32 dist, l_uint32 *pval, l_int32 debug)
pixGetColorNearMaskBoundary()
NUMA * pixAbsDiffByRow(PIX *pix, BOX *box)
pixAbsDiffByRow()
PIX * pixMirroredTiling(PIX *pixs, l_int32 w, l_int32 h)
pixMirroredTiling()
l_ok pixCombineMaskedGeneral(PIX *pixd, PIX *pixs, PIX *pixm, l_int32 x, l_int32 y)
pixCombineMaskedGeneral()
PIX * pixCopyWithBoxa(PIX *pixs, BOXA *boxa, l_int32 background)
pixCopyWithBoxa()
l_int32 pixCountArbInRect(PIX *pixs, BOX *box, l_int32 val, l_int32 factor, l_int32 *pcount)
pixCountArbInRect()
l_ok pixZero(PIX *pix, l_int32 *pempty)
pixZero()
NUMA * pixAbsDiffByColumn(PIX *pix, BOX *box)
pixAbsDiffByColumn()
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()
PIX * pixSubtract(PIX *pixd, PIX *pixs1, PIX *pixs2)
pixSubtract()
PIX * pixSetUnderTransparency(PIX *pixs, l_uint32 val, l_int32 debug)
pixSetUnderTransparency()
NUMA * pixAverageByColumn(PIX *pix, BOX *box, l_int32 type)
pixAverageByColumn()
l_int32 * makePixelCentroidTab8(void)
makePixelCentroidTab8()
static BOXA * findTileRegionsForSearch(BOX *box, l_int32 w, l_int32 h, l_int32 searchdir, l_int32 mindist, l_int32 tsize, l_int32 ntiles)
findTileRegionsForSearch()
l_ok pixCountPixelsInRect(PIX *pixs, BOX *box, l_int32 *pcount, l_int32 *tab8)
pixCountPixelsInRect()
l_ok pixSetMaskedGeneral(PIX *pixd, PIX *pixm, l_uint32 val, l_int32 x, l_int32 y)
pixSetMaskedGeneral()
NUMA * pixVarianceByColumn(PIX *pix, BOX *box)
pixVarianceByColumn()
l_ok pixAbsDiffInRect(PIX *pix, BOX *box, l_int32 dir, l_float32 *pabsdiff)
pixAbsDiffInRect()
l_ok pixPaintSelfThroughMask(PIX *pixd, PIX *pixm, l_int32 x, l_int32 y, l_int32 searchdir, l_int32 mindist, l_int32 tilesize, l_int32 ntiles, l_int32 distblend)
pixPaintSelfThroughMask()
NUMA * pixCountPixelsByRow(PIX *pix, l_int32 *tab8)
pixCountPixelsByRow()
PIX * pixMakeMaskFromVal(PIX *pixs, l_int32 val)
pixMakeMaskFromVal()
l_ok pixAverageInRectRGB(PIX *pixs, PIX *pixm, BOX *box, l_int32 subsamp, l_uint32 *pave)
pixAverageInRectRGB()
l_ok pixCountPixelsInRow(PIX *pix, l_int32 row, l_int32 *pcount, l_int32 *tab8)
pixCountPixelsInRow()
PIX * pixDisplaySelectedPixels(PIX *pixs, PIX *pixm, SEL *sel, l_uint32 val)
pixDisplaySelectedPixels()
l_ok pixAverageInRect(PIX *pixs, PIX *pixm, BOX *box, l_int32 minval, l_int32 maxval, l_int32 subsamp, l_float32 *pave)
pixAverageInRect()
l_ok pixSetMasked(PIX *pixd, PIX *pixm, l_uint32 val)
pixSetMasked()
NUMA * pixAverageByRow(PIX *pix, BOX *box, l_int32 type)
pixAverageByRow()
PIX * pixMakeMaskFromLUT(PIX *pixs, l_int32 *tab)
pixMakeMaskFromLUT()
NUMA * pixCountByColumn(PIX *pix, BOX *box)
pixCountByColumn()
l_ok pixForegroundFraction(PIX *pix, l_float32 *pfract)
pixForegroundFraction()
l_ok pixCombineMasked(PIX *pixd, PIX *pixs, PIX *pixm)
pixCombineMasked()
PIX * pixInvert(PIX *pixd, PIX *pixs)
pixInvert()
NUMA * pixCountByRow(PIX *pix, BOX *box)
pixCountByRow()
PIX * pixXor(PIX *pixd, PIX *pixs1, PIX *pixs2)
pixXor()
PIX * pixMakeAlphaFromMask(PIX *pixs, l_int32 dist, BOX **pbox)
pixMakeAlphaFromMask()
NUMA * pixVarianceByRow(PIX *pix, BOX *box)
pixVarianceByRow()
l_ok pixVarianceInRect(PIX *pix, BOX *box, l_float32 *prootvar)
pixVarianceInRect()
l_ok pixThresholdPixelSum(PIX *pix, l_int32 thresh, l_int32 *pabove, l_int32 *tab8)
pixThresholdPixelSum()
l_ok pixFindRepCloseTile(PIX *pixs, BOX *box, l_int32 searchdir, l_int32 mindist, l_int32 tsize, l_int32 ntiles, BOX **pboxtile, l_int32 debug)
pixFindRepCloseTile()
NUMA * pixCountPixelsByColumn(PIX *pix)
pixCountPixelsByColumn()
NUMA * pixGetMomentByColumn(PIX *pix, l_int32 order)
pixGetMomentByColumn()
PIX * pixMakeArbMaskFromRGB(PIX *pixs, l_float32 rc, l_float32 gc, l_float32 bc, l_float32 thresh)
pixMakeArbMaskFromRGB()
l_ok pixAbsDiffOnLine(PIX *pix, l_int32 x1, l_int32 y1, l_int32 x2, l_int32 y2, l_float32 *pabsdiff)
pixAbsDiffOnLine()