154l_int32 ws, hs, w, h, x, y, ix, iy, i, npt;
155PIX *pixt1, *pixt2, *pixt3, *pixfg, *pixbg;
156SEL *selh, *selm, *sel_3, *sel;
157PTA *ptah = NULL, *ptam = NULL;
159 if (ppixe) *ppixe = NULL;
161 return (
SEL *)ERROR_PTR(
"pixs not defined", __func__, NULL);
162 if (pixGetDepth(pixs) != 1)
163 return (
SEL *)ERROR_PTR(
"pixs not 1 bpp", __func__, NULL);
164 if (hitdist < 0 || hitdist > 4 || missdist < 0 || missdist > 4)
165 return (
SEL *)ERROR_PTR(
"dist not in {0 .. 4}", __func__, NULL);
166 if (hitskip < 0 && missskip < 0)
167 return (
SEL *)ERROR_PTR(
"no hits or misses", __func__, NULL);
170 pixClipToForeground(pixs, &pixt1, NULL);
172 return (
SEL *)ERROR_PTR(
"pixt1 not made", __func__, NULL);
173 ws = pixGetWidth(pixt1);
174 hs = pixGetHeight(pixt1);
180 if (topflag || botflag || leftflag || rightflag) {
194 pixt2 = pixCreate(w, h, 1);
195 pixRasterop(pixt2, x, y, ws, hs,
PIX_SRC, pixt1, 0, 0);
197 pixt2 = pixClone(pixt1);
200 *ppixe = pixClone(pixt2);
206 sel_3 = selCreateBrick(3, 3, 1, 1, SEL_HIT);
208 selh = selCreateBrick(2 * hitdist + 1, 2 * hitdist + 1,
209 hitdist, hitdist, SEL_HIT);
210 pixt3 = pixErode(NULL, pixt2, selh);
211 pixfg = pixErode(NULL, pixt3, sel_3);
212 pixXor(pixfg, pixfg, pixt3);
219 selm = selCreateBrick(2 * missdist + 1, 2 * missdist + 1,
220 missdist, missdist, SEL_HIT);
221 pixt3 = pixDilate(NULL, pixt2, selm);
222 pixbg = pixDilate(NULL, pixt3, sel_3);
223 pixXor(pixbg, pixbg, pixt3);
233 sel = selCreateBrick(h, w, h / 2, w / 2, SEL_DONT_CARE);
235 npt = ptaGetCount(ptah);
236 for (i = 0; i < npt; i++) {
237 ptaGetIPt(ptah, i, &ix, &iy);
238 selSetElement(sel, iy, ix, SEL_HIT);
242 npt = ptaGetCount(ptam);
243 for (i = 0; i < npt; i++) {
244 ptaGetIPt(ptam, i, &ix, &iy);
245 selSetElement(sel, iy, ix, SEL_MISS);
317l_int32 ws, hs, w, h, x, y, xval, yval, i, j, nh, nm;
320PIX *pixt1, *pixt2, *pixfg, *pixbg;
324 if (ppixe) *ppixe = NULL;
326 return (
SEL *)ERROR_PTR(
"pixs not defined", __func__, NULL);
327 if (pixGetDepth(pixs) != 1)
328 return (
SEL *)ERROR_PTR(
"pixs not 1 bpp", __func__, NULL);
329 if (nhlines < 1 && nvlines < 1)
330 return (
SEL *)ERROR_PTR(
"nvlines and nhlines both < 1", __func__, NULL);
333 distance = DefaultDistanceToBoundary;
335 minlength = DefaultMinRunlength;
336 if (distance > MaxDistanceToBoundary) {
337 L_WARNING(
"distance too large; setting to max value\n", __func__);
338 distance = MaxDistanceToBoundary;
342 pixClipToForeground(pixs, &pixt1, NULL);
344 return (
SEL *)ERROR_PTR(
"pixt1 not made", __func__, NULL);
345 ws = pixGetWidth(pixt1);
346 hs = pixGetHeight(pixt1);
352 if (toppix || botpix || leftpix || rightpix) {
357 if (toppix < distance + minlength)
358 L_WARNING(
"no miss elements in added top pixels\n", __func__);
362 if (botpix < distance + minlength)
363 L_WARNING(
"no miss elements in added bot pixels\n", __func__);
368 if (leftpix < distance + minlength)
369 L_WARNING(
"no miss elements in added left pixels\n", __func__);
373 if (rightpix < distance + minlength)
374 L_WARNING(
"no miss elements in added right pixels\n", __func__);
376 pixt2 = pixCreate(w, h, 1);
377 pixRasterop(pixt2, x, y, ws, hs,
PIX_SRC, pixt1, 0, 0);
379 pixt2 = pixClone(pixt1);
382 *ppixe = pixClone(pixt2);
387 seld = selCreateBrick(2 * distance + 1, 2 * distance + 1,
388 distance, distance, SEL_HIT);
389 pixfg = pixErode(NULL, pixt2, seld);
390 pixbg = pixDilate(NULL, pixt2, seld);
391 pixInvert(pixbg, pixbg);
399 delh = (l_float32)h / (l_float32)(nhlines + 1);
400 for (i = 0, y = 0; i < nhlines; i++) {
401 y += (l_int32)(delh + 0.5);
404 nh = numaGetCount(nah);
405 nm = numaGetCount(nam);
406 for (j = 0; j < nh; j++) {
407 numaGetIValue(nah, j, &xval);
408 ptaAddPt(ptah, xval, y);
410 for (j = 0; j < nm; j++) {
411 numaGetIValue(nam, j, &xval);
412 ptaAddPt(ptam, xval, y);
419 delw = (l_float32)w / (l_float32)(nvlines + 1);
420 for (i = 0, x = 0; i < nvlines; i++) {
421 x += (l_int32)(delw + 0.5);
424 nh = numaGetCount(nah);
425 nm = numaGetCount(nam);
426 for (j = 0; j < nh; j++) {
427 numaGetIValue(nah, j, &yval);
428 ptaAddPt(ptah, x, yval);
430 for (j = 0; j < nm; j++) {
431 numaGetIValue(nam, j, &yval);
432 ptaAddPt(ptam, x, yval);
440 sel = selCreateBrick(h, w, h / 2, w / 2, SEL_DONT_CARE);
441 nh = ptaGetCount(ptah);
442 for (i = 0; i < nh; i++) {
443 ptaGetIPt(ptah, i, &x, &y);
444 selSetElement(sel, y, x, SEL_HIT);
446 nm = ptaGetCount(ptam);
447 for (i = 0; i < nm; i++) {
448 ptaGetIPt(ptam, i, &x, &y);
449 selSetElement(sel, y, x, SEL_MISS);
505l_int32 ws, hs, w, h, x, y, i, j, thresh;
507PIX *pixt1, *pixt2, *pixfg, *pixbg;
510 if (ppixe) *ppixe = NULL;
512 return (
SEL *)ERROR_PTR(
"pixs not defined", __func__, NULL);
513 if (pixGetDepth(pixs) != 1)
514 return (
SEL *)ERROR_PTR(
"pixs not 1 bpp", __func__, NULL);
515 if (hitfract <= 0.0 && missfract <= 0.0)
516 return (
SEL *)ERROR_PTR(
"no hits or misses", __func__, NULL);
517 if (hitfract > 1.0 || missfract > 1.0)
518 return (
SEL *)ERROR_PTR(
"fraction can't be > 1.0", __func__, NULL);
521 distance = DefaultDistanceToBoundary;
522 if (distance > MaxDistanceToBoundary) {
523 L_WARNING(
"distance too large; setting to max value\n", __func__);
524 distance = MaxDistanceToBoundary;
528 pixClipToForeground(pixs, &pixt1, NULL);
530 return (
SEL *)ERROR_PTR(
"pixt1 not made", __func__, NULL);
531 ws = pixGetWidth(pixt1);
532 hs = pixGetHeight(pixt1);
538 if (toppix || botpix || leftpix || rightpix) {
552 pixt2 = pixCreate(w, h, 1);
553 pixRasterop(pixt2, x, y, ws, hs,
PIX_SRC, pixt1, 0, 0);
555 pixt2 = pixClone(pixt1);
558 *ppixe = pixClone(pixt2);
563 seld = selCreateBrick(2 * distance + 1, 2 * distance + 1,
564 distance, distance, SEL_HIT);
565 pixfg = pixErode(NULL, pixt2, seld);
566 pixbg = pixDilate(NULL, pixt2, seld);
567 pixInvert(pixbg, pixbg);
572 sel = selCreateBrick(h, w, h / 2, w / 2, SEL_DONT_CARE);
573 if (hitfract > 0.0) {
574 thresh = (l_int32)(hitfract * (l_float64)RAND_MAX);
575 for (i = 0; i < h; i++) {
576 for (j = 0; j < w; j++) {
577 pixGetPixel(pixfg, j, i, &val);
580 selSetElement(sel, i, j, SEL_HIT);
585 if (missfract > 0.0) {
586 thresh = (l_int32)(missfract * (l_float64)RAND_MAX);
587 for (i = 0; i < h; i++) {
588 for (j = 0; j < w; j++) {
589 pixGetPixel(pixbg, j, i, &val);
592 selSetElement(sel, i, j, SEL_MISS);
642l_int32 w, h, i, r, nruns, len;
646 return (
NUMA *)ERROR_PTR(
"pixs not defined", __func__, NULL);
647 if (pixGetDepth(pixs) != 1)
648 return (
NUMA *)ERROR_PTR(
"pixs not 1 bpp", __func__, NULL);
649 if (x != -1 && y != -1)
650 return (
NUMA *)ERROR_PTR(
"x or y must be -1", __func__, NULL);
651 if (x == -1 && y == -1)
652 return (
NUMA *)ERROR_PTR(
"x or y cannot both be -1", __func__, NULL);
654 if ((nad = numaCreate(0)) == NULL)
655 return (
NUMA *)ERROR_PTR(
"nad not made", __func__, NULL);
656 w = pixGetWidth(pixs);
657 h = pixGetHeight(pixs);
667 nruns = numaGetCount(naruns);
671 for (i = 0; i < nruns; i++) {
673 numaGetIValue(naruns, i, &len);
677 numaGetIValue(naruns, i, &len);
678 if (len >= minlength)
679 numaAddNumber(nad, r + len / 2);
684 numaDestroy(&naruns);
713l_int32 w, h, x, y, npts;
714l_int32 i, runlen, preval;
720 return (
NUMA *)ERROR_PTR(
"pixs not defined", __func__, NULL);
721 if (pixGetDepth(pixs) != 1)
722 return (
NUMA *)ERROR_PTR(
"pixs not 1 bpp", __func__, NULL);
724 w = pixGetWidth(pixs);
725 h = pixGetHeight(pixs);
726 if (x1 < 0 || x1 >= w)
727 return (
NUMA *)ERROR_PTR(
"x1 not valid", __func__, NULL);
728 if (x2 < 0 || x2 >= w)
729 return (
NUMA *)ERROR_PTR(
"x2 not valid", __func__, NULL);
730 if (y1 < 0 || y1 >= h)
731 return (
NUMA *)ERROR_PTR(
"y1 not valid", __func__, NULL);
732 if (y2 < 0 || y2 >= h)
733 return (
NUMA *)ERROR_PTR(
"y2 not valid", __func__, NULL);
735 if ((pta = generatePtaLine(x1, y1, x2, y2)) == NULL)
736 return (
NUMA *)ERROR_PTR(
"pta not made", __func__, NULL);
737 if ((npts = ptaGetCount(pta)) == 0) {
739 return (
NUMA *)ERROR_PTR(
"pta has no pts", __func__, NULL);
741 if ((numa = numaCreate(0)) == NULL) {
743 return (
NUMA *)ERROR_PTR(
"numa not made", __func__, NULL);
746 for (i = 0; i < npts; i++) {
747 ptaGetIPt(pta, i, &x, &y);
748 pixGetPixel(pixs, x, y, &val);
751 numaAddNumber(numa, 0);
761 numaAddNumber(numa, runlen);
766 numaAddNumber(numa, runlen);
803l_int32 x, y, xn, yn, xs, ys, xa, ya, count;
808 return (
PTA *)ERROR_PTR(
"pixs not defined", __func__, NULL);
809 if (pixGetDepth(pixs) != 1)
810 return (
PTA *)ERROR_PTR(
"pixs not 1 bpp", __func__, NULL);
812 return (
PTA *)ERROR_PTR(
"skip < 0", __func__, NULL);
815 return ptaGetPixelsFromPix(pixs, NULL);
818 pixt = pixCopy(NULL, pixs);
820 while (nextOnPixelInRaster(pixt, xs, ys, &xn, &yn)) {
825 ptaAddPt(pta, xs, ys);
830 pixSetPixel(pixt, x, y, 0);
835 pixSetPixel(pixt, x, y, 0);
871l_int32 w, h, i, xa, ya, found;
872l_int32 xdel[] = {-1, 0, 1, 0, -1, 1, 1, -1};
873l_int32 ydel[] = {0, 1, 0, -1, 1, 1, -1, -1};
877 return ERROR_INT(
"pixs not defined", __func__, 0);
878 if (pixGetDepth(pixs) != 1)
879 return ERROR_INT(
"pixs not 1 bpp", __func__, 0);
880 w = pixGetWidth(pixs);
881 h = pixGetHeight(pixs);
883 for (i = 0; i < 8; i++) {
886 if (xa < 0 || xa >= w || ya < 0 || ya >= h)
888 pixGetPixel(pixs, xa, ya, &val);
933 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
934 if (pixGetDepth(pixs) != 1)
935 return (
PIX *)ERROR_PTR(
"pixs not 1 bpp", __func__, NULL);
937 return (
PIX *)ERROR_PTR(
"sel not defined", __func__, NULL);
939 if (scalefactor <= 0)
940 scalefactor = DefaultSelScalefactor;
941 if (scalefactor > MaxSelScalefactor) {
942 L_WARNING(
"scalefactor too large; using max value\n", __func__);
943 scalefactor = MaxSelScalefactor;
947 pixt = pixConvert1To8(NULL, pixs, 0, 1);
948 cmap = pixcmapCreate(8);
949 pixcmapAddColor(cmap, 255, 255, 255);
950 pixcmapAddColor(cmap, 0, 0, 0);
951 pixcmapAddColor(cmap, hitcolor >> 24, (hitcolor >> 16) & 0xff,
952 (hitcolor >> 8) & 0xff);
953 pixcmapAddColor(cmap, misscolor >> 24, (misscolor >> 16) & 0xff,
954 (misscolor >> 8) & 0xff);
955 pixSetColormap(pixt, cmap);
958 for (i = 0; i < sel->
sy; i++) {
959 for (j = 0; j < sel->
sx; j++) {
960 selGetElement(sel, i, j, &type);
961 if (type == SEL_DONT_CARE)
964 pixSetPixel(pixt, j, i, 2);
966 pixSetPixel(pixt, j, i, 3);
971 fscale = (l_float32)scalefactor;
972 pixd = pixScaleBySampling(pixt, fscale, fscale);