91 #include "allheaders.h" 109 l_int32 wpl, l_int32 xstart,
110 l_int32 ystart, l_int32 *px, l_int32 *py);
114 l_int32
y, l_int32
dy, l_int32 ymax,
115 l_int32 *pminx, l_int32 *pmaxx,
116 l_int32 *pminy, l_int32 *pmaxy);
118 l_int32
y, l_int32
dy, l_int32 ymax);
120 l_int32 *py, l_int32 *pdy);
123 #ifndef NO_CONSOLE_IO 149 l_int32 connectivity)
152 PROCNAME(
"pixConnComp");
154 if (ppixa) *ppixa = NULL;
155 if (!pixs || pixGetDepth(pixs) != 1)
156 return (
BOXA *)ERROR_PTR(
"pixs undefined or not 1 bpp", procName, NULL);
157 if (connectivity != 4 && connectivity != 8)
158 return (
BOXA *)ERROR_PTR(
"connectivity not 4 or 8", procName, NULL);
193 l_int32 connectivity)
196 l_int32 x,
y, xstart, ystart;
197 PIX *pix1, *pix2, *pix3, *pix4;
203 PROCNAME(
"pixConnCompPixa");
206 return (
BOXA *)ERROR_PTR(
"&pixa not defined", procName, NULL);
208 if (!pixs || pixGetDepth(pixs) != 1)
209 return (
BOXA *)ERROR_PTR(
"pixs undefined or not 1 bpp", procName, NULL);
210 if (connectivity != 4 && connectivity != 8)
211 return (
BOXA *)ERROR_PTR(
"connectivity not 4 or 8", procName, NULL);
213 pix1 = pix2 = pix3 = pix4 = NULL;
225 if (!pix1 || !pix2) {
226 L_ERROR(
"pix1 or pix2 not made\n", procName);
231 h = pixGetHeight(pixs);
233 L_ERROR(
"stack not made\n", procName);
247 if ((box =
pixSeedfillBB(pix1, stack, x,
y, connectivity)) == NULL) {
250 L_ERROR(
"box not made\n", procName);
270 fprintf(stderr,
"Number of remaining pixels = %d\n", iszero);
272 pixWriteDebug(
"/tmp/lept/cc/remain.png", pix1, IFF_PNG);
307 l_int32 connectivity)
310 l_int32 x,
y, xstart, ystart;
316 PROCNAME(
"pixConnCompBB");
318 if (!pixs || pixGetDepth(pixs) != 1)
319 return (
BOXA *)ERROR_PTR(
"pixs undefined or not 1 bpp", procName, NULL);
320 if (connectivity != 4 && connectivity != 8)
321 return (
BOXA *)ERROR_PTR(
"connectivity not 4 or 8", procName, NULL);
331 if ((pix1 =
pixCopy(NULL, pixs)) == NULL)
332 return (
BOXA *)ERROR_PTR(
"pix1 not made", procName, NULL);
334 h = pixGetHeight(pixs);
336 L_ERROR(
"stack not made\n", procName);
349 if ((box =
pixSeedfillBB(pix1, stack, x,
y, connectivity)) == NULL) {
350 L_ERROR(
"box not made\n", procName);
362 fprintf(stderr,
"Number of remaining pixels = %d\n", iszero);
364 pixWriteDebug(
"/tmp/lept/cc/remain.png", pix1, IFF_PNG);
391 l_int32 connectivity,
395 l_int32 x,
y, xstart, ystart;
399 PROCNAME(
"pixCountConnComp");
402 return ERROR_INT(
"&count not defined", procName, 1);
404 if (!pixs || pixGetDepth(pixs) != 1)
405 return ERROR_INT(
"pixs not defined or not 1 bpp", procName, 1);
406 if (connectivity != 4 && connectivity != 8)
407 return ERROR_INT(
"connectivity not 4 or 8", procName, 1);
415 if ((pix1 =
pixCopy(NULL, pixs)) == NULL)
416 return ERROR_INT(
"pix1 not made", procName, 1);
417 h = pixGetHeight(pixs);
420 return ERROR_INT(
"stack not made\n", procName, 1);
459 l_int32 w, h, d, wpl;
462 PROCNAME(
"nextOnPixelInRaster");
465 return ERROR_INT(
"pixs not defined", procName, 0);
468 return ERROR_INT(
"pixs not 1 bpp", procName, 0);
470 wpl = pixGetWpl(pixs);
496 l_int32 i, x,
y, xend, startword;
497 l_uint32 *line, *pword;
500 line = data + ystart * wpl;
501 pword = line + (xstart / 32);
503 xend = xstart - (xstart % 32) + 31;
504 for (x = xstart; x <= xend && x < w; x++) {
514 startword = (xstart / 32) + 1;
516 for (pword = line + startword; x < w; pword++, x += 32) {
518 for (i = 0; i < 32 && x < w; i++, x++) {
529 for (
y = ystart + 1;
y < h;
y++) {
530 line = data +
y * wpl;
531 for (pword = line, x = 0; x < w; pword++, x += 32) {
533 for (i = 0; i < 32 && x < w; i++, x++) {
568 l_int32 connectivity)
572 PROCNAME(
"pixSeedfillBB");
574 if (!pixs || pixGetDepth(pixs) != 1)
575 return (
BOX *)ERROR_PTR(
"pixs undefined or not 1 bpp", procName, NULL);
577 return (
BOX *)ERROR_PTR(
"stack not defined", procName, NULL);
578 if (connectivity != 4 && connectivity != 8)
579 return (
BOX *)ERROR_PTR(
"connectivity not 4 or 8", procName, NULL);
581 if (connectivity == 4) {
583 return (
BOX *)ERROR_PTR(
"box not made", procName, NULL);
584 }
else if (connectivity == 8) {
586 return (
BOX *)ERROR_PTR(
"box not made", procName, NULL);
588 return (
BOX *)ERROR_PTR(
"connectivity not 4 or 8", procName, NULL);
632 l_int32 w, h, xstart, wpl, x1, x2,
dy;
634 l_int32 minx, maxx, miny, maxy;
635 l_uint32 *data, *line;
638 PROCNAME(
"pixSeedfill4BB");
640 if (!pixs || pixGetDepth(pixs) != 1)
641 return (
BOX *)ERROR_PTR(
"pixs undefined or not 1 bpp", procName, NULL);
643 return (
BOX *)ERROR_PTR(
"stack not defined", procName, NULL);
651 wpl = pixGetWpl(pixs);
652 line = data +
y * wpl;
655 if (x < 0 || x > xmax || y < 0 || y > ymax || (
GET_DATA_BIT(line, x) == 0))
661 minx = miny = 100000;
663 pushFillsegBB(stack, x, x,
y, 1, ymax, &minx, &maxx, &miny, &maxy);
664 pushFillsegBB(stack, x, x,
y + 1, -1, ymax, &minx, &maxx, &miny, &maxy);
671 line = data +
y * wpl;
684 for (x = x1; x >= 0 && (
GET_DATA_BIT(line, x) == 1); x--)
691 ymax, &minx, &maxx, &miny, &maxy);
698 ymax, &minx, &maxx, &miny, &maxy);
701 ymax, &minx, &maxx, &miny, &maxy);
702 skip:
for (x++; x <= x2 &&
707 }
while (x <= x2 && x <= xmax);
710 if ((box =
boxCreate(minx, miny, maxx - minx + 1, maxy - miny + 1))
712 return (
BOX *)ERROR_PTR(
"box not made", procName, NULL);
747 l_int32 w, h, xstart, wpl, x1, x2,
dy;
749 l_int32 minx, maxx, miny, maxy;
750 l_uint32 *data, *line;
753 PROCNAME(
"pixSeedfill8BB");
755 if (!pixs || pixGetDepth(pixs) != 1)
756 return (
BOX *)ERROR_PTR(
"pixs undefined or not 1 bpp", procName, NULL);
758 return (
BOX *)ERROR_PTR(
"stack not defined", procName, NULL);
766 wpl = pixGetWpl(pixs);
767 line = data +
y * wpl;
770 if (x < 0 || x > xmax || y < 0 || y > ymax || (
GET_DATA_BIT(line, x) == 0))
776 minx = miny = 100000;
778 pushFillsegBB(stack, x, x,
y, 1, ymax, &minx, &maxx, &miny, &maxy);
779 pushFillsegBB(stack, x, x,
y + 1, -1, ymax, &minx, &maxx, &miny, &maxy);
786 line = data +
y * wpl;
799 for (x = x1 - 1; x >= 0 && (
GET_DATA_BIT(line, x) == 1); x--)
806 ymax, &minx, &maxx, &miny, &maxy);
813 ymax, &minx, &maxx, &miny, &maxy);
816 ymax, &minx, &maxx, &miny, &maxy);
817 skip:
for (x++; x <= x2 + 1 &&
822 }
while (x <= x2 + 1 && x <= xmax);
825 if ((box =
boxCreate(minx, miny, maxx - minx + 1, maxy - miny + 1))
827 return (
BOX *)ERROR_PTR(
"box not made", procName, NULL);
852 l_int32 connectivity)
856 PROCNAME(
"pixSeedfill");
858 if (!pixs || pixGetDepth(pixs) != 1)
859 return ERROR_INT(
"pixs not defined or not 1 bpp", procName, 1);
861 return ERROR_INT(
"stack not defined", procName, 1);
862 if (connectivity != 4 && connectivity != 8)
863 return ERROR_INT(
"connectivity not 4 or 8", procName, 1);
865 if (connectivity == 4)
897 l_int32 w, h, xstart, wpl, x1, x2,
dy;
899 l_uint32 *data, *line;
901 PROCNAME(
"pixSeedfill4");
903 if (!pixs || pixGetDepth(pixs) != 1)
904 return ERROR_INT(
"pixs not defined or not 1 bpp", procName, 1);
906 return ERROR_INT(
"stack not defined", procName, 1);
914 wpl = pixGetWpl(pixs);
915 line = data +
y * wpl;
918 if (x < 0 || x > xmax || y < 0 || y > ymax || (
GET_DATA_BIT(line, x) == 0))
928 line = data +
y * wpl;
941 for (x = x1; x >= 0 && (
GET_DATA_BIT(line, x) == 1); x--)
956 skip:
for (x++; x <= x2 &&
961 }
while (x <= x2 && x <= xmax);
991 l_int32 w, h, xstart, wpl, x1, x2,
dy;
993 l_uint32 *data, *line;
995 PROCNAME(
"pixSeedfill8");
997 if (!pixs || pixGetDepth(pixs) != 1)
998 return ERROR_INT(
"pixs not defined or not 1 bpp", procName, 1);
1000 return ERROR_INT(
"stack not defined", procName, 1);
1008 wpl = pixGetWpl(pixs);
1009 line = data +
y * wpl;
1012 if (x < 0 || x > xmax || y < 0 || y > ymax || (
GET_DATA_BIT(line, x) == 0))
1022 line = data +
y * wpl;
1035 for (x = x1 - 1; x >= 0 && (
GET_DATA_BIT(line, x) == 1); x--)
1050 skip:
for (x++; x <= x2 + 1 &&
1055 }
while (x <= x2 + 1 && x <= xmax);
1103 PROCNAME(
"pushFillsegBB");
1106 L_ERROR(
"stack not defined\n", procName);
1110 *pminx = L_MIN(*pminx,
xleft);
1111 *pmaxx = L_MAX(*pmaxx,
xright);
1112 *pminy = L_MIN(*pminy,
y);
1113 *pmaxy = L_MAX(*pmaxy,
y);
1115 if (
y +
dy >= 0 &&
y +
dy <= ymax) {
1116 if ((auxstack = stack->
auxstack) == NULL) {
1117 L_ERROR(
"auxstack not defined\n", procName);
1125 if ((fseg = (
FILLSEG *)LEPT_CALLOC(1,
sizeof(
FILLSEG))) == NULL) {
1126 L_ERROR(
"fillseg not made\n", procName);
1170 PROCNAME(
"pushFillseg");
1173 L_ERROR(
"stack not defined\n", procName);
1177 if (
y +
dy >= 0 &&
y +
dy <= ymax) {
1178 if ((auxstack = stack->
auxstack) == NULL) {
1179 L_ERROR(
"auxstack not defined\n", procName);
1187 if ((fseg = (
FILLSEG *)LEPT_CALLOC(1,
sizeof(
FILLSEG))) == NULL) {
1188 L_ERROR(
"fillseg not made\n", procName);
1230 PROCNAME(
"popFillseg");
1233 L_ERROR(
"stack not defined\n", procName);
1236 if ((auxstack = stack->
auxstack) == NULL) {
1237 L_ERROR(
"auxstack not defined\n", procName);
1244 *pxleft = fseg->
xleft;
1246 *py = fseg->
y + fseg->
dy;
BOX * pixSeedfill4BB(PIX *pixs, L_STACK *stack, l_int32 x, l_int32 y)
pixSeedfill4BB()
l_ok pixSeedfill4(PIX *pixs, L_STACK *stack, l_int32 x, l_int32 y)
pixSeedfill4()
l_int32 lept_mkdir(const char *subdir)
lept_mkdir()
The struct FillSeg is used by the Heckbert seedfill algorithm to hold information about image segment...
BOX * pixSeedfillBB(PIX *pixs, L_STACK *stack, l_int32 x, l_int32 y, l_int32 connectivity)
pixSeedfillBB()
void lstackDestroy(L_STACK **plstack, l_int32 freeflag)
lstackDestroy()
l_int32 nextOnPixelInRaster(PIX *pixs, l_int32 xstart, l_int32 ystart, l_int32 *px, l_int32 *py)
nextOnPixelInRaster()
PIXA * pixaCreate(l_int32 n)
pixaCreate()
BOX * pixSeedfill8BB(PIX *pixs, L_STACK *stack, l_int32 x, l_int32 y)
pixSeedfill8BB()
l_int32 lstackGetCount(L_STACK *lstack)
lstackGetCount()
l_ok pixRasterop(PIX *pixd, l_int32 dx, l_int32 dy, l_int32 dw, l_int32 dh, l_int32 op, PIX *pixs, l_int32 sx, l_int32 sy)
pixRasterop()
static void pushFillsegBB(L_STACK *stack, l_int32 xleft, l_int32 xright, l_int32 y, l_int32 dy, l_int32 ymax, l_int32 *pminx, l_int32 *pmaxx, l_int32 *pminy, l_int32 *pmaxy)
pushFillsegBB()
BOXA * boxaCopy(BOXA *boxa, l_int32 copyflag)
boxaCopy()
void boxaDestroy(BOXA **pboxa)
boxaDestroy()
l_uint32 * pixGetData(PIX *pix)
pixGetData()
static l_int32 nextOnPixelInRasterLow(l_uint32 *data, l_int32 w, l_int32 h, l_int32 wpl, l_int32 xstart, l_int32 ystart, l_int32 *px, l_int32 *py)
nextOnPixelInRasterLow()
#define GET_DATA_BIT(pdata, n)
PIX * pixClipRectangle(PIX *pixs, BOX *box, BOX **pboxc)
pixClipRectangle()
l_ok pixSeedfill(PIX *pixs, L_STACK *stack, l_int32 x, l_int32 y, l_int32 connectivity)
pixSeedfill()
l_ok lstackAdd(L_STACK *lstack, void *item)
lstackAdd()
#define CLEAR_DATA_BIT(pdata, n)
BOXA * pixConnComp(PIX *pixs, PIXA **ppixa, l_int32 connectivity)
pixConnComp()
l_ok pixaAddPix(PIXA *pixa, PIX *pix, l_int32 copyflag)
pixaAddPix()
PIX * pixXor(PIX *pixd, PIX *pixs1, PIX *pixs2)
pixXor()
void * lstackRemove(L_STACK *lstack)
lstackRemove()
BOXA * pixConnCompBB(PIX *pixs, l_int32 connectivity)
pixConnCompBB()
l_ok pixCountPixels(PIX *pixs, l_int32 *pcount, l_int32 *tab8)
pixCountPixels()
l_ok boxaAddBox(BOXA *boxa, BOX *box, l_int32 copyflag)
boxaAddBox()
l_ok pixSetPadBits(PIX *pix, l_int32 val)
pixSetPadBits()
L_STACK * lstackCreate(l_int32 nalloc)
lstackCreate()
void pixDestroy(PIX **ppix)
pixDestroy()
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
BOXA * pixConnCompPixa(PIX *pixs, PIXA **ppixa, l_int32 connectivity)
pixConnCompPixa()
static void pushFillseg(L_STACK *stack, l_int32 xleft, l_int32 xright, l_int32 y, l_int32 dy, l_int32 ymax)
pushFillseg()
struct L_Stack * auxstack
static void popFillseg(L_STACK *stack, l_int32 *pxleft, l_int32 *pxright, l_int32 *py, l_int32 *pdy)
popFillseg()
l_ok pixZero(PIX *pix, l_int32 *pempty)
pixZero()
BOXA * boxaCreate(l_int32 n)
boxaCreate()
PIX * pixCopy(PIX *pixd, PIX *pixs)
pixCopy()
l_ok pixSeedfill8(PIX *pixs, L_STACK *stack, l_int32 x, l_int32 y)
pixSeedfill8()
void pixaDestroy(PIXA **ppixa)
pixaDestroy()
BOX * boxCreate(l_int32 x, l_int32 y, l_int32 w, l_int32 h)
boxCreate()
l_ok pixCountConnComp(PIX *pixs, l_int32 connectivity, l_int32 *pcount)
pixCountConnComp()