159l_int32 ws, hs, w, h, x, y, xval, yval, i, j, nh, nm;
162PIX *pixt1, *pixt2, *pixfg, *pixbg;
166 if (ppixe) *ppixe = NULL;
168 return (
SEL *)ERROR_PTR(
"pixs not defined", __func__, NULL);
169 if (pixGetDepth(pixs) != 1)
170 return (
SEL *)ERROR_PTR(
"pixs not 1 bpp", __func__, NULL);
171 if (nhlines < 1 && nvlines < 1)
172 return (
SEL *)ERROR_PTR(
"nvlines and nhlines both < 1", __func__, NULL);
175 distance = DefaultDistanceToBoundary;
177 minlength = DefaultMinRunlength;
178 if (distance > MaxDistanceToBoundary) {
179 L_WARNING(
"distance too large; setting to max value\n", __func__);
180 distance = MaxDistanceToBoundary;
184 pixClipToForeground(pixs, &pixt1, NULL);
186 return (
SEL *)ERROR_PTR(
"pixt1 not made", __func__, NULL);
187 ws = pixGetWidth(pixt1);
188 hs = pixGetHeight(pixt1);
194 if (toppix || botpix || leftpix || rightpix) {
199 if (toppix < distance + minlength)
200 L_WARNING(
"no miss elements in added top pixels\n", __func__);
204 if (botpix < distance + minlength)
205 L_WARNING(
"no miss elements in added bot pixels\n", __func__);
210 if (leftpix < distance + minlength)
211 L_WARNING(
"no miss elements in added left pixels\n", __func__);
215 if (rightpix < distance + minlength)
216 L_WARNING(
"no miss elements in added right pixels\n", __func__);
218 pixt2 = pixCreate(w, h, 1);
219 pixRasterop(pixt2, x, y, ws, hs,
PIX_SRC, pixt1, 0, 0);
221 pixt2 = pixClone(pixt1);
224 *ppixe = pixClone(pixt2);
229 seld = selCreateBrick(2 * distance + 1, 2 * distance + 1,
230 distance, distance, SEL_HIT);
231 pixfg = pixErode(NULL, pixt2, seld);
232 pixbg = pixDilate(NULL, pixt2, seld);
233 pixInvert(pixbg, pixbg);
241 delh = (l_float32)h / (l_float32)(nhlines + 1);
242 for (i = 0, y = 0; i < nhlines; i++) {
243 y += (l_int32)(delh + 0.5);
246 nh = numaGetCount(nah);
247 nm = numaGetCount(nam);
248 for (j = 0; j < nh; j++) {
249 numaGetIValue(nah, j, &xval);
250 ptaAddPt(ptah, xval, y);
252 for (j = 0; j < nm; j++) {
253 numaGetIValue(nam, j, &xval);
254 ptaAddPt(ptam, xval, y);
261 delw = (l_float32)w / (l_float32)(nvlines + 1);
262 for (i = 0, x = 0; i < nvlines; i++) {
263 x += (l_int32)(delw + 0.5);
266 nh = numaGetCount(nah);
267 nm = numaGetCount(nam);
268 for (j = 0; j < nh; j++) {
269 numaGetIValue(nah, j, &yval);
270 ptaAddPt(ptah, x, yval);
272 for (j = 0; j < nm; j++) {
273 numaGetIValue(nam, j, &yval);
274 ptaAddPt(ptam, x, yval);
282 sel = selCreateBrick(h, w, h / 2, w / 2, SEL_DONT_CARE);
283 nh = ptaGetCount(ptah);
284 for (i = 0; i < nh; i++) {
285 ptaGetIPt(ptah, i, &x, &y);
286 selSetElement(sel, y, x, SEL_HIT);
288 nm = ptaGetCount(ptam);
289 for (i = 0; i < nm; i++) {
290 ptaGetIPt(ptam, i, &x, &y);
291 selSetElement(sel, y, x, SEL_MISS);
346l_int32 ws, hs, w, h, x, y, i, j, thresh;
348PIX *pixt1, *pixt2, *pixfg, *pixbg;
351 if (ppixe) *ppixe = NULL;
353 return (
SEL *)ERROR_PTR(
"pixs not defined", __func__, NULL);
354 if (pixGetDepth(pixs) != 1)
355 return (
SEL *)ERROR_PTR(
"pixs not 1 bpp", __func__, NULL);
356 if (hitfract <= 0.0 && missfract <= 0.0)
357 return (
SEL *)ERROR_PTR(
"no hits or misses", __func__, NULL);
358 if (hitfract > 1.0 || missfract > 1.0)
359 return (
SEL *)ERROR_PTR(
"fraction can't be > 1.0", __func__, NULL);
362 distance = DefaultDistanceToBoundary;
363 if (distance > MaxDistanceToBoundary) {
364 L_WARNING(
"distance too large; setting to max value\n", __func__);
365 distance = MaxDistanceToBoundary;
369 pixClipToForeground(pixs, &pixt1, NULL);
371 return (
SEL *)ERROR_PTR(
"pixt1 not made", __func__, NULL);
372 ws = pixGetWidth(pixt1);
373 hs = pixGetHeight(pixt1);
379 if (toppix || botpix || leftpix || rightpix) {
393 pixt2 = pixCreate(w, h, 1);
394 pixRasterop(pixt2, x, y, ws, hs,
PIX_SRC, pixt1, 0, 0);
396 pixt2 = pixClone(pixt1);
399 *ppixe = pixClone(pixt2);
404 seld = selCreateBrick(2 * distance + 1, 2 * distance + 1,
405 distance, distance, SEL_HIT);
406 pixfg = pixErode(NULL, pixt2, seld);
407 pixbg = pixDilate(NULL, pixt2, seld);
408 pixInvert(pixbg, pixbg);
413 sel = selCreateBrick(h, w, h / 2, w / 2, SEL_DONT_CARE);
414 if (hitfract > 0.0) {
415 thresh = (l_int32)(hitfract * (l_float64)RAND_MAX);
416 for (i = 0; i < h; i++) {
417 for (j = 0; j < w; j++) {
418 pixGetPixel(pixfg, j, i, &val);
421 selSetElement(sel, i, j, SEL_HIT);
426 if (missfract > 0.0) {
427 thresh = (l_int32)(missfract * (l_float64)RAND_MAX);
428 for (i = 0; i < h; i++) {
429 for (j = 0; j < w; j++) {
430 pixGetPixel(pixbg, j, i, &val);
433 selSetElement(sel, i, j, SEL_MISS);
498l_int32 ws, hs, w, h, x, y, ix, iy, i, npt;
499PIX *pixt1, *pixt2, *pixt3, *pixfg, *pixbg;
500SEL *selh, *selm, *sel_3, *sel;
501PTA *ptah = NULL, *ptam = NULL;
503 if (ppixe) *ppixe = NULL;
505 return (
SEL *)ERROR_PTR(
"pixs not defined", __func__, NULL);
506 if (pixGetDepth(pixs) != 1)
507 return (
SEL *)ERROR_PTR(
"pixs not 1 bpp", __func__, NULL);
508 if (hitdist < 0 || hitdist > 4 || missdist < 0 || missdist > 4)
509 return (
SEL *)ERROR_PTR(
"dist not in {0 .. 4}", __func__, NULL);
510 if (hitskip < 0 && missskip < 0)
511 return (
SEL *)ERROR_PTR(
"no hits or misses", __func__, NULL);
514 pixClipToForeground(pixs, &pixt1, NULL);
516 return (
SEL *)ERROR_PTR(
"pixt1 not made", __func__, NULL);
517 ws = pixGetWidth(pixt1);
518 hs = pixGetHeight(pixt1);
524 if (topflag || botflag || leftflag || rightflag) {
538 pixt2 = pixCreate(w, h, 1);
539 pixRasterop(pixt2, x, y, ws, hs,
PIX_SRC, pixt1, 0, 0);
541 pixt2 = pixClone(pixt1);
544 *ppixe = pixClone(pixt2);
550 sel_3 = selCreateBrick(3, 3, 1, 1, SEL_HIT);
552 selh = selCreateBrick(2 * hitdist + 1, 2 * hitdist + 1,
553 hitdist, hitdist, SEL_HIT);
554 pixt3 = pixErode(NULL, pixt2, selh);
555 pixfg = pixErode(NULL, pixt3, sel_3);
556 pixXor(pixfg, pixfg, pixt3);
563 selm = selCreateBrick(2 * missdist + 1, 2 * missdist + 1,
564 missdist, missdist, SEL_HIT);
565 pixt3 = pixDilate(NULL, pixt2, selm);
566 pixbg = pixDilate(NULL, pixt3, sel_3);
567 pixXor(pixbg, pixbg, pixt3);
577 sel = selCreateBrick(h, w, h / 2, w / 2, SEL_DONT_CARE);
579 npt = ptaGetCount(ptah);
580 for (i = 0; i < npt; i++) {
581 ptaGetIPt(ptah, i, &ix, &iy);
582 selSetElement(sel, iy, ix, SEL_HIT);
586 npt = ptaGetCount(ptam);
587 for (i = 0; i < npt; i++) {
588 ptaGetIPt(ptam, i, &ix, &iy);
589 selSetElement(sel, iy, ix, SEL_MISS);
637l_int32 w, h, i, r, nruns, len;
641 return (
NUMA *)ERROR_PTR(
"pixs not defined", __func__, NULL);
642 if (pixGetDepth(pixs) != 1)
643 return (
NUMA *)ERROR_PTR(
"pixs not 1 bpp", __func__, NULL);
644 if (x != -1 && y != -1)
645 return (
NUMA *)ERROR_PTR(
"x or y must be -1", __func__, NULL);
646 if (x == -1 && y == -1)
647 return (
NUMA *)ERROR_PTR(
"x or y cannot both be -1", __func__, NULL);
649 if ((nad = numaCreate(0)) == NULL)
650 return (
NUMA *)ERROR_PTR(
"nad not made", __func__, NULL);
651 w = pixGetWidth(pixs);
652 h = pixGetHeight(pixs);
662 nruns = numaGetCount(naruns);
666 for (i = 0; i < nruns; i++) {
668 numaGetIValue(naruns, i, &len);
672 numaGetIValue(naruns, i, &len);
673 if (len >= minlength)
674 numaAddNumber(nad, r + len / 2);
679 numaDestroy(&naruns);
708l_int32 w, h, x, y, npts;
709l_int32 i, runlen, preval;
715 return (
NUMA *)ERROR_PTR(
"pixs not defined", __func__, NULL);
716 if (pixGetDepth(pixs) != 1)
717 return (
NUMA *)ERROR_PTR(
"pixs not 1 bpp", __func__, NULL);
719 w = pixGetWidth(pixs);
720 h = pixGetHeight(pixs);
721 if (x1 < 0 || x1 >= w)
722 return (
NUMA *)ERROR_PTR(
"x1 not valid", __func__, NULL);
723 if (x2 < 0 || x2 >= w)
724 return (
NUMA *)ERROR_PTR(
"x2 not valid", __func__, NULL);
725 if (y1 < 0 || y1 >= h)
726 return (
NUMA *)ERROR_PTR(
"y1 not valid", __func__, NULL);
727 if (y2 < 0 || y2 >= h)
728 return (
NUMA *)ERROR_PTR(
"y2 not valid", __func__, NULL);
730 if ((pta = generatePtaLine(x1, y1, x2, y2)) == NULL)
731 return (
NUMA *)ERROR_PTR(
"pta not made", __func__, NULL);
732 if ((npts = ptaGetCount(pta)) == 0) {
734 return (
NUMA *)ERROR_PTR(
"pta has no pts", __func__, NULL);
736 if ((numa = numaCreate(0)) == NULL) {
738 return (
NUMA *)ERROR_PTR(
"numa not made", __func__, NULL);
741 for (i = 0; i < npts; i++) {
742 ptaGetIPt(pta, i, &x, &y);
743 pixGetPixel(pixs, x, y, &val);
746 numaAddNumber(numa, 0);
756 numaAddNumber(numa, runlen);
761 numaAddNumber(numa, runlen);
798l_int32 x, y, xn, yn, xs, ys, xa, ya, count;
803 return (
PTA *)ERROR_PTR(
"pixs not defined", __func__, NULL);
804 if (pixGetDepth(pixs) != 1)
805 return (
PTA *)ERROR_PTR(
"pixs not 1 bpp", __func__, NULL);
807 return (
PTA *)ERROR_PTR(
"skip < 0", __func__, NULL);
810 return ptaGetPixelsFromPix(pixs, NULL);
813 pixt = pixCopy(NULL, pixs);
815 while (nextOnPixelInRaster(pixt, xs, ys, &xn, &yn)) {
820 ptaAddPt(pta, xs, ys);
825 pixSetPixel(pixt, x, y, 0);
830 pixSetPixel(pixt, x, y, 0);
866l_int32 w, h, i, xa, ya, found;
867l_int32 xdel[] = {-1, 0, 1, 0, -1, 1, 1, -1};
868l_int32 ydel[] = {0, 1, 0, -1, 1, 1, -1, -1};
872 return ERROR_INT(
"pixs not defined", __func__, 0);
873 if (pixGetDepth(pixs) != 1)
874 return ERROR_INT(
"pixs not 1 bpp", __func__, 0);
875 w = pixGetWidth(pixs);
876 h = pixGetHeight(pixs);
878 for (i = 0; i < 8; i++) {
881 if (xa < 0 || xa >= w || ya < 0 || ya >= h)
883 pixGetPixel(pixs, xa, ya, &val);
928 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
929 if (pixGetDepth(pixs) != 1)
930 return (
PIX *)ERROR_PTR(
"pixs not 1 bpp", __func__, NULL);
932 return (
PIX *)ERROR_PTR(
"sel not defined", __func__, NULL);
934 if (scalefactor <= 0)
935 scalefactor = DefaultSelScalefactor;
936 if (scalefactor > MaxSelScalefactor) {
937 L_WARNING(
"scalefactor too large; using max value\n", __func__);
938 scalefactor = MaxSelScalefactor;
942 pixt = pixConvert1To8(NULL, pixs, 0, 1);
943 cmap = pixcmapCreate(8);
944 pixcmapAddColor(cmap, 255, 255, 255);
945 pixcmapAddColor(cmap, 0, 0, 0);
946 pixcmapAddColor(cmap, hitcolor >> 24, (hitcolor >> 16) & 0xff,
947 (hitcolor >> 8) & 0xff);
948 pixcmapAddColor(cmap, misscolor >> 24, (misscolor >> 16) & 0xff,
949 (misscolor >> 8) & 0xff);
950 pixSetColormap(pixt, cmap);
953 for (i = 0; i < sel->
sy; i++) {
954 for (j = 0; j < sel->
sx; j++) {
955 selGetElement(sel, i, j, &type);
956 if (type == SEL_DONT_CARE)
959 pixSetPixel(pixt, j, i, 2);
961 pixSetPixel(pixt, j, i, 3);
966 fscale = (l_float32)scalefactor;
967 pixd = pixScaleBySampling(pixt, fscale, fscale);