154l_int32 i, n, x, y, w, h;
158 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
159 if (pixGetColormap(pixs))
160 return (
PIX *)ERROR_PTR(
"pixs is cmapped", __func__, NULL);
161 if (pixd && (pixd != pixs))
162 return (
PIX *)ERROR_PTR(
"if pixd, must be in-place", __func__, NULL);
164 return (
PIX *)ERROR_PTR(
"boxa not defined", __func__, NULL);
166 return (
PIX *)ERROR_PTR(
"invalid op", __func__, NULL);
168 pixd = pixCopy(pixd, pixs);
169 if ((n = boxaGetCount(boxa)) == 0) {
170 L_WARNING(
"no boxes to mask\n", __func__);
174 for (i = 0; i < n; i++) {
175 box = boxaGetBox(boxa, i,
L_CLONE);
176 boxGetGeometry(box, &x, &y, &w, &h);
178 pixRasterop(pixd, x, y, w, h,
PIX_SET, NULL, 0, 0);
180 pixRasterop(pixd, x, y, w, h,
PIX_CLR, NULL, 0, 0);
220l_int32 i, n, d, rval, gval, bval, newindex;
227 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
229 return (
PIX *)ERROR_PTR(
"boxa not defined", __func__, NULL);
231 if ((n = boxaGetCount(boxa)) == 0) {
232 L_WARNING(
"no boxes to paint; returning a copy\n", __func__);
233 return pixCopy(NULL, pixs);
237 if ((cmap = pixGetColormap(pixs)) != NULL) {
238 if (pixcmapGetCount(cmap) < 256)
241 if (pixGetDepth(pixs) == 1 || mapvacancy)
242 pixd = pixConvertTo8(pixs, TRUE);
244 pixd = pixConvertTo32(pixs);
246 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, NULL);
248 d = pixGetDepth(pixd);
250 cmap = pixGetColormap(pixd);
251 extractRGBValues(val, &rval, &gval, &bval);
252 if (pixcmapAddNewColor(cmap, rval, gval, bval, &newindex)) {
254 return (
PIX *)ERROR_PTR(
"cmap full; can't add", __func__, NULL);
258 for (i = 0; i < n; i++) {
259 box = boxaGetBox(boxa, i,
L_CLONE);
261 pixSetInRectArbitrary(pixd, box, newindex);
263 pixSetInRectArbitrary(pixd, box, val);
362l_int32 i, n, d, rval, gval, bval, index;
369 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
371 return (
PIX *)ERROR_PTR(
"boxa not defined", __func__, NULL);
373 if ((n = boxaGetCount(boxa)) == 0) {
374 L_WARNING(
"no boxes to paint; returning a copy\n", __func__);
375 return pixCopy(NULL, pixs);
378 if (pixGetDepth(pixs) == 1)
379 pixd = pixConvert1To8(NULL, pixs, 255, 0);
381 pixd = pixConvertTo32(pixs);
383 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, NULL);
385 cmap = pixcmapCreateRandom(8, 1, 1);
386 d = pixGetDepth(pixd);
388 pixSetColormap(pixd, cmap);
390 for (i = 0; i < n; i++) {
391 box = boxaGetBox(boxa, i,
L_CLONE);
392 index = 1 + (i % 254);
394 pixSetInRectArbitrary(pixd, box, index);
396 pixcmapGetColor(cmap, index, &rval, &gval, &bval);
397 composeRGBPixel(rval, gval, bval, &val);
398 pixSetInRectArbitrary(pixd, box, val);
404 pixcmapDestroy(&cmap);
432l_int32 i, n, rval, gval, bval, index;
439 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
441 return (
PIX *)ERROR_PTR(
"boxa not defined", __func__, NULL);
442 if (fract < 0.0 || fract > 1.0) {
443 L_WARNING(
"fract must be in [0.0, 1.0]; setting to 0.5\n", __func__);
447 if ((n = boxaGetCount(boxa)) == 0) {
448 L_WARNING(
"no boxes to paint; returning a copy\n", __func__);
449 return pixCopy(NULL, pixs);
452 if ((pixd = pixConvertTo32(pixs)) == NULL)
453 return (
PIX *)ERROR_PTR(
"pixd not defined", __func__, NULL);
455 cmap = pixcmapCreateRandom(8, 1, 1);
456 for (i = 0; i < n; i++) {
457 box = boxaGetBox(boxa, i,
L_CLONE);
458 index = 1 + (i % 254);
459 pixcmapGetColor(cmap, index, &rval, &gval, &bval);
460 composeRGBPixel(rval, gval, bval, &val);
461 pixBlendInRect(pixd, box, val, fract);
465 pixcmapDestroy(&cmap);
492l_int32 rval, gval, bval, newindex;
498 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
500 return (
PIX *)ERROR_PTR(
"boxa not defined", __func__, NULL);
502 return (
PIX *)ERROR_PTR(
"width must be >= 1", __func__, NULL);
504 if (boxaGetCount(boxa) == 0) {
505 L_WARNING(
"no boxes to draw; returning a copy\n", __func__);
506 return pixCopy(NULL, pixs);
510 if ((cmap = pixGetColormap(pixs)) != NULL) {
511 if (pixcmapGetCount(cmap) < 256)
514 if (pixGetDepth(pixs) == 1 || mapvacancy)
515 pixd = pixConvertTo8(pixs, TRUE);
517 pixd = pixConvertTo32(pixs);
519 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, NULL);
521 extractRGBValues(val, &rval, &gval, &bval);
522 if (pixGetDepth(pixd) == 8) {
523 cmap = pixGetColormap(pixd);
524 pixcmapAddNewColor(cmap, rval, gval, bval, &newindex);
527 pixRenderBoxaArb(pixd, boxa, width, rval, gval, bval);
553l_int32 i, n, rval, gval, bval, index;
560 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
562 return (
PIX *)ERROR_PTR(
"boxa not defined", __func__, NULL);
564 return (
PIX *)ERROR_PTR(
"width must be >= 1", __func__, NULL);
566 if ((n = boxaGetCount(boxa)) == 0) {
567 L_WARNING(
"no boxes to draw; returning a copy\n", __func__);
568 return pixCopy(NULL, pixs);
572 if (pixGetDepth(pixs) == 1) {
573 ptaa = generatePtaaBoxa(boxa);
574 pixd = pixRenderRandomCmapPtaa(pixs, ptaa, 1, width, 1);
580 pixd = pixConvertTo32(pixs);
581 cmap = pixcmapCreateRandom(8, 1, 1);
582 for (i = 0; i < n; i++) {
583 box = boxaGetBox(boxa, i,
L_CLONE);
584 index = 1 + (i % 254);
585 pixcmapGetColor(cmap, index, &rval, &gval, &bval);
586 pixRenderBoxArb(pixd, box, width, rval, gval, bval);
589 pixcmapDestroy(&cmap);
630l_int32 i, j, n, m, rbox, gbox, bbox, rboxa, gboxa, bboxa;
637 return (
PIX *)ERROR_PTR(
"baa not defined", __func__, NULL);
639 if (w <= 0 || h <= 0) {
641 pixGetDimensions(pixs, &w, &h, NULL);
643 boxaaGetExtent(baa, &w, &h, NULL, NULL);
647 pixd = pixConvertTo8(pixs, 1);
648 cmap = pixGetColormap(pixd);
650 pixd = pixCreate(w, h, 8);
651 cmap = pixcmapCreate(8);
652 pixSetColormap(pixd, cmap);
653 pixcmapAddColor(cmap, 255, 255, 255);
655 extractRGBValues(colorb, &rbox, &gbox, &bbox);
656 extractRGBValues(colorba, &rboxa, &gboxa, &bboxa);
657 pixcmapAddColor(cmap, rbox, gbox, bbox);
658 pixcmapAddColor(cmap, rboxa, gboxa, bboxa);
660 n = boxaaGetCount(baa);
661 for (i = 0; i < n; i++) {
662 boxa = boxaaGetBoxa(baa, i,
L_CLONE);
663 boxaGetExtent(boxa, NULL, NULL, &box);
664 pixRenderBoxArb(pixd, box, linewba, rboxa, gboxa, bboxa);
666 m = boxaGetCount(boxa);
667 for (j = 0; j < m; j++) {
668 box = boxaGetBox(boxa, j,
L_CLONE);
669 pixRenderBoxArb(pixd, box, linewb, rbox, gbox, bbox);
706l_int32 i, j, nba, n, nbox, rval, gval, bval;
715 return (
PIXA *)ERROR_PTR(
"pixas not defined", __func__, NULL);
717 return (
PIXA *)ERROR_PTR(
"baa not defined", __func__, NULL);
719 return (
PIXA *)ERROR_PTR(
"width must be >= 1", __func__, NULL);
720 if ((nba = boxaaGetCount(baa)) < 1)
721 return (
PIXA *)ERROR_PTR(
"no boxa in baa", __func__, NULL);
722 if ((n = pixaGetCount(pixas)) == 0)
723 return (
PIXA *)ERROR_PTR(
"no pix in pixas", __func__, NULL);
725 return (
PIXA *)ERROR_PTR(
"num pix != num boxa", __func__, NULL);
737 return (
PIXA *)ERROR_PTR(
"invalid colorflag", __func__, NULL);
741 for (i = 0; i < 255; i++)
744 for (i = 0; i < 255; i++) {
746 colors[i] = 0xff000000;
748 colors[i] = 0x00ff0000;
750 colors[i] = 0x0000ff00;
753 for (i = 0; i < 255; i++) {
754 rval = (l_uint32)rand() & 0xff;
755 gval = (l_uint32)rand() & 0xff;
756 bval = (l_uint32)rand() & 0xff;
757 composeRGBPixel(rval, gval, bval, &colors[i]);
761 pixad = pixaCreate(n);
762 for (i = 0; i < n; i++) {
763 pix = pixaGetPix(pixas, i,
L_COPY);
764 boxa = boxaaGetBoxa(baa, i,
L_CLONE);
765 nbox = boxaGetCount(boxa);
766 for (j = 0; j < nbox; j++) {
767 box = boxaGetBox(boxa, j,
L_CLONE);
768 extractRGBValues(colors[j % 255], &rval, &gval, &bval);
769 pixRenderBoxArb(pix, box, width, rval, gval, bval);
934l_int32 i, w, h, boxx, boxy, bx, by, bw, bh, maxdir, maxscore;
937BOX *boxt1, *boxt2, *boxt3;
943 if (!pix || pixGetDepth(pix) != 1)
944 return (
BOXA *)ERROR_PTR(
"pix undefined or not 1 bpp", __func__, NULL);
946 pixs = pixCopy(NULL, pix);
947 pixGetDimensions(pixs, &w, &h, NULL);
949 boxGetGeometry(box, &boxx, &boxy, NULL, NULL);
952 boxs = boxCreate(0, 0, w, h);
953 boxad = boxaCreate(0);
956 while (boxs != NULL) {
957 boxGetGeometry(boxs, &bx, &by, &bw, &bh);
958 boxat = boxaCreate(4);
959 nascore = numaCreate(4);
960 for (i = 0; i < 4; i++) {
965 numaGetIValue(nas, 0, &maxdir);
966 numaGetIValue(nascore, maxdir, &maxscore);
968 lept_stderr(
"Iteration: %d\n", iter);
969 boxPrintStreamInfo(stderr, boxs);
970 boxaWriteStderr(boxat);
971 lept_stderr(
"\nmaxdir = %d, maxscore = %d\n\n", maxdir, maxscore);
974 boxt1 = boxaGetBox(boxat, maxdir,
L_CLONE);
975 boxt2 = boxTransform(boxt1, boxx, boxy, 1.0, 1.0);
977 pixClearInRect(pixs, boxt1);
979 pixClipBoxToForeground(pixs, boxs, NULL, &boxt3);
983 boxGetGeometry(boxs, NULL, NULL, &bw, &bh);
984 if (bw < 2 || bh < 2)
988 if (remainder == 1) {
989 boxt1 = boxTransform(boxs, boxx, boxy, 1.0, 1.0);
995 numaDestroy(&nascore);
999 if ((iter == maxcomps) && boxs) {
1000 if (remainder == 1) {
1001 boxt1 = boxTransform(boxs, boxx, boxy, 1.0, 1.0);
1002 boxaAddBox(boxad, boxt1,
L_INSERT);
1048l_int32 bx, by, bw, bh, width, height, setref, atref;
1049l_int32 minincol, maxincol, mininrow, maxinrow, minval, maxval, bgref;
1050l_int32 x, y, x0, y0, xref, yref, colsum, rowsum, score, countref, diff;
1054 if (!pixs || pixGetDepth(pixs) != 1)
1055 return ERROR_INT(
"pixs undefined or not 1 bpp", __func__, 1);
1057 return ERROR_INT(
"boxs not defined", __func__, 1);
1059 return ERROR_INT(
"boxat not defined", __func__, 1);
1061 return ERROR_INT(
"nascore not defined", __func__, 1);
1063 lines1 = pixGetLinePtrs(pixs, NULL);
1064 boxGetGeometry(boxs, &bx, &by, &bw, &bh);
1071 xref = yref = 100000;
1073 for (x = bx; x < bx + bw; x++) {
1077 for (y = by; y < by + bh; y++) {
1080 if (y > maxincol) maxincol = y;
1081 if (y < minincol) minincol = y;
1087 if (!setref && colsum >= minsum) {
1090 if (xref >= bx + bw)
1096 if (setref && x == xref) {
1099 bgref = maxincol - minincol + 1 - countref;
1107 diff = L_ABS(colsum - countref);
1108 if (diff >= delta || x == bx + bw - 1) {
1109 height = maxval - minval + 1;
1111 if (x == bx + bw - 1) width = x - bx + 1;
1112 boxr = boxCreate(bx, minval, width, height);
1113 score = 2 * score - width * height;
1117 maxval = L_MAX(maxval, maxincol);
1118 minval = L_MIN(minval, minincol);
1122 for (x = bx + bw - 1; x >= bx; x--) {
1126 for (y = by; y < by + bh; y++) {
1129 if (y > maxincol) maxincol = y;
1130 if (y < minincol) minincol = y;
1134 if (!setref && colsum >= minsum) {
1140 if (setref && x == xref) {
1143 bgref = maxincol - minincol + 1 - countref;
1148 diff = L_ABS(colsum - countref);
1149 if (diff >= delta || x == bx) {
1150 height = maxval - minval + 1;
1152 if (x == bx) x0 = x;
1153 width = bx + bw - x0;
1154 boxr = boxCreate(x0, minval, width, height);
1155 score = 2 * score - width * height;
1159 maxval = L_MAX(maxval, maxincol);
1160 minval = L_MIN(minval, minincol);
1164 for (y = by; y < by + bh; y++) {
1168 for (x = bx; x < bx + bw; x++) {
1171 if (x > maxinrow) maxinrow = x;
1172 if (x < mininrow) mininrow = x;
1176 if (!setref && rowsum >= minsum) {
1179 if (yref >= by + bh)
1182 if (setref && y == yref) {
1185 bgref = maxinrow - mininrow + 1 - countref;
1190 diff = L_ABS(rowsum - countref);
1191 if (diff >= delta || y == by + bh - 1) {
1192 width = maxval - minval + 1;
1194 if (y == by + bh - 1) height = y - by + 1;
1195 boxr = boxCreate(minval, by, width, height);
1196 score = 2 * score - width * height;
1200 maxval = L_MAX(maxval, maxinrow);
1201 minval = L_MIN(minval, mininrow);
1205 for (y = by + bh - 1; y >= by; y--) {
1209 for (x = bx; x < bx + bw; x++) {
1212 if (x > maxinrow) maxinrow = x;
1213 if (x < mininrow) mininrow = x;
1217 if (!setref && rowsum >= minsum) {
1223 if (setref && y == yref) {
1226 bgref = maxinrow - mininrow + 1 - countref;
1231 diff = L_ABS(rowsum - countref);
1232 if (diff >= delta || y == by) {
1233 width = maxval - minval + 1;
1235 if (y == by) y0 = y;
1236 height = by + bh - y0;
1237 boxr = boxCreate(minval, y0, width, height);
1238 score = 2 * score - width * height;
1242 maxval = L_MAX(maxval, maxinrow);
1243 minval = L_MIN(minval, mininrow);
1249 numaAddNumber(nascore, 0);
1250 boxaAddBox(boxat, boxCreate(0, 0, 1, 1),
L_INSERT);
1255 numaAddNumber(nascore, score);
1366 l_float32 *pdiffarea,
1367 l_float32 *pdiffxor,
1370l_int32 w, h, x3, y3, w3, h3, x4, y4, w4, h4, n3, n4, area1, area2;
1371l_int32 count3, count4, countxor;
1374BOXA *boxa3, *boxa4, *boxa3t, *boxa4t;
1375PIX *pix1, *pix2, *pix3, *pix4, *pix5;
1378 if (pdiffxor) *pdiffxor = 1.0;
1379 if (ppixdb) *ppixdb = NULL;
1380 if (pnsame) *pnsame = FALSE;
1381 if (pdiffarea) *pdiffarea = 1.0;
1382 if (!boxa1 || !boxa2)
1383 return ERROR_INT(
"boxa1 and boxa2 not both defined", __func__, 1);
1385 return ERROR_INT(
"&nsame not defined", __func__, 1);
1387 return ERROR_INT(
"&diffarea not defined", __func__, 1);
1391 n3 = boxaGetCount(boxa3);
1392 n4 = boxaGetCount(boxa4);
1397 if (n3 == 0 || n4 == 0) {
1398 boxaDestroy(&boxa3);
1399 boxaDestroy(&boxa4);
1400 if (n3 == 0 && n4 == 0) {
1404 if (pdiffxor) *pdiffxor = 0.0;
1410 boxaGetArea(boxa3, &area1);
1411 boxaGetArea(boxa4, &area2);
1412 *pdiffarea = (l_float32)L_ABS(area1 - area2) / (l_float32)(area1 + area2);
1414 boxaDestroy(&boxa3);
1415 boxaDestroy(&boxa4);
1423 boxaGetExtent(boxa3, &w, &h, &box3);
1424 boxaGetExtent(boxa4, &w, &h, &box4);
1425 boxGetGeometry(box3, &x3, &y3, &w3, &h3);
1426 boxGetGeometry(box4, &x4, &y4, &w4, &h4);
1427 boxa3t = boxaTransform(boxa3, -x3, -y3, 1.0, 1.0);
1428 boxa4t = boxaTransform(boxa4, -x4, -y4, 1.0, 1.0);
1429 w = L_MAX(x3 + w3, x4 + w4);
1430 h = L_MAX(y3 + h3, y4 + h4);
1431 pix3 = pixCreate(w, h, 1);
1432 pix4 = pixCreate(w, h, 1);
1435 tab = makePixelSumTab8();
1436 pixCountPixels(pix3, &count3, tab);
1437 pixCountPixels(pix4, &count4, tab);
1438 pix5 = pixXor(NULL, pix3, pix4);
1439 pixCountPixels(pix5, &countxor, tab);
1441 *pdiffxor = (l_float32)countxor / (l_float32)(count3 + count4);
1444 pixa = pixaCreate(2);
1445 pix1 = pixCreate(w, h, 32);
1452 pix2 = pixCreate(w, h, 32);
1453 pixPaintThroughMask(pix2, pix3, x3, y3, 0xff000000);
1454 pixPaintThroughMask(pix2, pix4, x4, y4, 0x00ff0000);
1455 pixAnd(pix3, pix3, pix4);
1456 pixPaintThroughMask(pix2, pix3, x3, y3, 0x0000ff00);
1458 *ppixdb = pixaDisplayTiledInRows(pixa, 32, 1000, 1.0, 0, 30, 2);
1464 boxaDestroy(&boxa3);
1465 boxaDestroy(&boxa3t);
1466 boxaDestroy(&boxa4);
1467 boxaDestroy(&boxa4t);
1547l_int32 w, h, i, n, x1, y1, x2, y2, select;
1548l_float32 area, max_area;
1550BOXA *boxa1, *boxa2, *boxa3;
1553 return (
BOX *)ERROR_PTR(
"boxas not defined", __func__, NULL);
1554 if (boxaGetCount(boxas) == 0)
1555 return (
BOX *)ERROR_PTR(
"no boxes in boxas", __func__, NULL);
1556 if (areaslop < 0.0 || areaslop > 1.0)
1557 return (
BOX *)ERROR_PTR(
"invalid value for areaslop", __func__, NULL);
1558 yslop = L_MAX(0, yslop);
1562 n = boxaGetCount(boxa2);
1563 boxaGetBoxGeometry(boxa1, 0, NULL, NULL, &w, &h);
1564 max_area = (l_float32)(w * h);
1567 boxa3 = boxaCreate(4);
1568 for (i = 0; i < n; i++) {
1569 boxaGetBoxGeometry(boxa2, i, NULL, NULL, &w, &h);
1570 area = (l_float32)(w * h);
1571 if (area / max_area >= areaslop) {
1572 box = boxaGetBox(boxa2, i,
L_COPY);
1579 n = boxaGetCount(boxa3);
1580 boxaGetBoxGeometry(boxa3, 0, &x1, &y1, NULL, NULL);
1582 for (i = 1; i < n; i++) {
1583 boxaGetBoxGeometry(boxa3, i, &x2, &y2, NULL, NULL);
1584 if (y2 - y1 < yslop && x2 < x1) {
1590 box = boxaGetBox(boxa3, select,
L_COPY);
1591 boxaDestroy(&boxa1);
1592 boxaDestroy(&boxa2);
1593 boxaDestroy(&boxa3);