/*! * pixOtsuAdaptiveThreshold() * * Input: pixs (8 bpp) * sx, sy (desired tile dimensions; actual size may vary) * smoothx, smoothy (half-width of convolution kernel applied to * threshold array: use 0 for no smoothing) * scorefract (fraction of the max Otsu score; typ. 0.1; * use 0.0 for standard Otsu) * &pixth (<optional return> array of threshold values * found for each tile) * &pixd (<optional return> thresholded input pixs, based on * the threshold array) * Return: 0 if OK, 1 on error * * Notes: * (1) The Otsu method finds a single global threshold for an image. * This function allows a locally adapted threshold to be * found for each tile into which the image is broken up. * (2) The array of threshold values, one for each tile, constitutes * a highly downscaled image. This array is optionally * smoothed using a convolution. The full width and height of the * convolution kernel are (2 * @smoothx + 1) and (2 * @smoothy + 1). * (3) The minimum tile dimension allowed is 16. If such small * tiles are used, it is recommended to use smoothing, because * without smoothing, each small tile determines the splitting * threshold independently. A tile that is entirely in the * image bg will then hallucinate fg, resulting in a very noisy * binarization. The smoothing should be large enough that no * tile is only influenced by one type (fg or bg) of pixels, * because it will force a split of its pixels. * (4) To get a single global threshold for the entire image, use * input values of @sx and @sy that are larger than the image. * For this situation, the smoothing parameters are ignored. * (5) The threshold values partition the image pixels into two classes: * one whose values are less than the threshold and another * whose values are greater than or equal to the threshold. * This is the same use of 'threshold' as in pixThresholdToBinary(). * (6) The scorefract is the fraction of the maximum Otsu score, which * is used to determine the range over which the histogram minimum * is searched. See numaSplitDistribution() for details on the * underlying method of choosing a threshold. * (7) This uses enables a modified version of the Otsu criterion for * splitting the distribution of pixels in each tile into a * fg and bg part. The modification consists of searching for * a minimum in the histogram over a range of pixel values where * the Otsu score is within a defined fraction, @scorefract, * of the max score. To get the original Otsu algorithm, set * @scorefract == 0. */ l_int32 pixOtsuAdaptiveThreshold(PIX *pixs, l_int32 sx, l_int32 sy, l_int32 smoothx, l_int32 smoothy, l_float32 scorefract, PIX **ppixth, PIX **ppixd) { l_int32 w, h, nx, ny, i, j, thresh; l_uint32 val; PIX *pixt, *pixb, *pixthresh, *pixth, *pixd; PIXTILING *pt; PROCNAME("pixOtsuAdaptiveThreshold"); if (!ppixth && !ppixd) return ERROR_INT("neither &pixth nor &pixd defined", procName, 1); if (ppixth) *ppixth = NULL; if (ppixd) *ppixd = NULL; if (!pixs || pixGetDepth(pixs) != 8) return ERROR_INT("pixs not defined or not 8 bpp", procName, 1); if (sx < 16 || sy < 16) return ERROR_INT("sx and sy must be >= 16", procName, 1); /* Compute the threshold array for the tiles */ pixGetDimensions(pixs, &w, &h, NULL); nx = L_MAX(1, w / sx); ny = L_MAX(1, h / sy); smoothx = L_MIN(smoothx, (nx - 1) / 2); smoothy = L_MIN(smoothy, (ny - 1) / 2); pt = pixTilingCreate(pixs, nx, ny, 0, 0, 0, 0); pixthresh = pixCreate(nx, ny, 8); for (i = 0; i < ny; i++) { for (j = 0; j < nx; j++) { pixt = pixTilingGetTile(pt, i, j); pixSplitDistributionFgBg(pixt, scorefract, 1, &thresh, NULL, NULL, 0); pixSetPixel(pixthresh, j, i, thresh); /* see note (4) */ pixDestroy(&pixt); } } /* Optionally smooth the threshold array */ if (smoothx > 0 || smoothy > 0) pixth = pixBlockconv(pixthresh, smoothx, smoothy); else pixth = pixClone(pixthresh); pixDestroy(&pixthresh); /* Optionally apply the threshold array to binarize pixs */ if (ppixd) { pixd = pixCreate(w, h, 1); for (i = 0; i < ny; i++) { for (j = 0; j < nx; j++) { pixt = pixTilingGetTile(pt, i, j); pixGetPixel(pixth, j, i, &val); pixb = pixThresholdToBinary(pixt, val); pixTilingPaintTile(pixd, i, j, pixb, pt); pixDestroy(&pixt); pixDestroy(&pixb); } } *ppixd = pixd; } if (ppixth) *ppixth = pixth; else pixDestroy(&pixth); pixTilingDestroy(&pt); return 0; }
/*! * \brief pixTilingGetTile() * * \param[in] pt pixtiling * \param[in] i tile row index * \param[in] j tile column index * \return pixd tile with appropriate boundary (overlap) pixels added, * or NULL on error */ PIX * pixTilingGetTile(PIXTILING *pt, l_int32 i, l_int32 j) { l_int32 wpix, hpix, wt, ht, nx, ny; l_int32 xoverlap, yoverlap, wtlast, htlast; l_int32 left, top, xtraleft, xtraright, xtratop, xtrabot, width, height; BOX *box; PIX *pixs, *pixt, *pixd; PROCNAME("pixTilingGetTile"); if (!pt) return (PIX *)ERROR_PTR("pt not defined", procName, NULL); if ((pixs = pt->pix) == NULL) return (PIX *)ERROR_PTR("pix not found", procName, NULL); pixTilingGetCount(pt, &nx, &ny); if (i < 0 || i >= ny) return (PIX *)ERROR_PTR("invalid row index i", procName, NULL); if (j < 0 || j >= nx) return (PIX *)ERROR_PTR("invalid column index j", procName, NULL); /* Grab the tile with as much overlap as exists within the * input pix. First, compute the (left, top) coordinates. */ pixGetDimensions(pixs, &wpix, &hpix, NULL); pixTilingGetSize(pt, &wt, &ht); xoverlap = pt->xoverlap; yoverlap = pt->yoverlap; wtlast = wpix - wt * (nx - 1); htlast = hpix - ht * (ny - 1); left = L_MAX(0, j * wt - xoverlap); top = L_MAX(0, i * ht - yoverlap); /* Get the width and height of the tile, including whatever * overlap is available. */ if (nx == 1) width = wpix; else if (j == 0) width = wt + xoverlap; else if (j == nx - 1) width = wtlast + xoverlap; else width = wt + 2 * xoverlap; if (ny == 1) height = hpix; else if (i == 0) height = ht + yoverlap; else if (i == ny - 1) height = htlast + yoverlap; else height = ht + 2 * yoverlap; box = boxCreate(left, top, width, height); pixt = pixClipRectangle(pixs, box, NULL); boxDestroy(&box); /* If no overlap, do not add any special case borders */ if (xoverlap == 0 && yoverlap == 0) return pixt; /* Add overlap as a mirrored border, in the 8 special cases where * the tile touches the border of the input pix. The xtratop (etc) * parameters are required where the tile is either full width * or full height. */ xtratop = xtrabot = xtraleft = xtraright = 0; if (nx == 1) xtraleft = xtraright = xoverlap; if (ny == 1) xtratop = xtrabot = yoverlap; if (i == 0 && j == 0) pixd = pixAddMirroredBorder(pixt, xoverlap, xtraright, yoverlap, xtrabot); else if (i == 0 && j == nx - 1) pixd = pixAddMirroredBorder(pixt, xtraleft, xoverlap, yoverlap, xtrabot); else if (i == ny - 1 && j == 0) pixd = pixAddMirroredBorder(pixt, xoverlap, xtraright, xtratop, yoverlap); else if (i == ny - 1 && j == nx - 1) pixd = pixAddMirroredBorder(pixt, xtraleft, xoverlap, xtratop, yoverlap); else if (i == 0) pixd = pixAddMirroredBorder(pixt, 0, 0, yoverlap, xtrabot); else if (i == ny - 1) pixd = pixAddMirroredBorder(pixt, 0, 0, xtratop, yoverlap); else if (j == 0) pixd = pixAddMirroredBorder(pixt, xoverlap, xtraright, 0, 0); else if (j == nx - 1) pixd = pixAddMirroredBorder(pixt, xtraleft, xoverlap, 0, 0); else pixd = pixClone(pixt); pixDestroy(&pixt); return pixd; }
/*! * wshedApply() * * Input: wshed (generated from wshedCreate()) * Return: 0 if OK, 1 on error * * Iportant note: * (1) This is buggy. It seems to locate watersheds that are * duplicates. The watershed extraction after complete fill * grabs some regions belonging to existing watersheds. * See prog/watershedtest.c for testing. */ l_int32 wshedApply(L_WSHED *wshed) { char two_new_watersheds[] = "Two new watersheds"; char seed_absorbed_into_seeded_basin[] = "Seed absorbed into seeded basin"; char one_new_watershed_label[] = "One new watershed (label)"; char one_new_watershed_index[] = "One new watershed (index)"; char minima_absorbed_into_seeded_basin[] = "Minima absorbed into seeded basin"; char minima_absorbed_by_filler_or_another[] = "Minima absorbed by filler or another"; l_int32 nseeds, nother, nboth, arraysize; l_int32 i, j, val, x, y, w, h, index, mindepth; l_int32 imin, imax, jmin, jmax, cindex, clabel, nindex; l_int32 hindex, hlabel, hmin, hmax, minhindex, maxhindex; l_int32 *lut; l_uint32 ulabel, uval; void **lines8, **linelab32; NUMA *nalut, *nalevels, *nash, *namh, *nasi; NUMA **links; L_HEAP *lh; PIX *pixmin, *pixsd; PIXA *pixad; L_STACK *rstack; PTA *ptas, *ptao; PROCNAME("wshedApply"); if (!wshed) return ERROR_INT("wshed not defined", procName, 1); /* ------------------------------------------------------------ * * Initialize priority queue and pixlab with seeds and minima * * ------------------------------------------------------------ */ lh = lheapCreate(0, L_SORT_INCREASING); /* remove lowest values first */ rstack = lstackCreate(0); /* for reusing the WSPixels */ pixGetDimensions(wshed->pixs, &w, &h, NULL); lines8 = wshed->lines8; /* wshed owns this */ linelab32 = wshed->linelab32; /* ditto */ /* Identify seed (marker) pixels, 1 for each c.c. in pixm */ pixSelectMinInConnComp(wshed->pixs, wshed->pixm, &ptas, &nash); pixsd = pixGenerateFromPta(ptas, w, h); nseeds = ptaGetCount(ptas); for (i = 0; i < nseeds; i++) { ptaGetIPt(ptas, i, &x, &y); uval = GET_DATA_BYTE(lines8[y], x); pushWSPixel(lh, rstack, (l_int32) uval, x, y, i); } wshed->ptas = ptas; nasi = numaMakeConstant(1, nseeds); /* indicator array */ wshed->nasi = nasi; wshed->nash = nash; wshed->nseeds = nseeds; /* Identify minima that are not seeds. Use these 4 steps: * (1) Get the local minima, which can have components * of arbitrary size. This will be a clipping mask. * (2) Get the image of the actual seeds (pixsd) * (3) Remove all elements of the clipping mask that have a seed. * (4) Shrink each of the remaining elements of the minima mask * to a single pixel. */ pixLocalExtrema(wshed->pixs, 200, 0, &pixmin, NULL); pixRemoveSeededComponents(pixmin, pixsd, pixmin, 8, 2); pixSelectMinInConnComp(wshed->pixs, pixmin, &ptao, &namh); nother = ptaGetCount(ptao); for (i = 0; i < nother; i++) { ptaGetIPt(ptao, i, &x, &y); uval = GET_DATA_BYTE(lines8[y], x); pushWSPixel(lh, rstack, (l_int32) uval, x, y, nseeds + i); } wshed->namh = namh; /* ------------------------------------------------------------ * * Initialize merging lookup tables * * ------------------------------------------------------------ */ /* nalut should always give the current after-merging index. * links are effectively backpointers: they are numas associated with * a dest index of all indices in nalut that point to that index. */ mindepth = wshed->mindepth; nboth = nseeds + nother; arraysize = 2 * nboth; wshed->arraysize = arraysize; nalut = numaMakeSequence(0, 1, arraysize); lut = numaGetIArray(nalut); wshed->lut = lut; /* wshed owns this */ links = (NUMA **) CALLOC(arraysize, sizeof(NUMA * )); wshed->links = links; /* wshed owns this */ nindex = nseeds + nother; /* the next unused index value */ /* ------------------------------------------------------------ * * Fill the basins, using the priority queue * * ------------------------------------------------------------ */ pixad = pixaCreate(nseeds); wshed->pixad = pixad; /* wshed owns this */ nalevels = numaCreate(nseeds); wshed->nalevels = nalevels; /* wshed owns this */ L_INFO("nseeds = %d, nother = %d\n", procName, nseeds, nother); while (lheapGetCount(lh) > 0) { popWSPixel(lh, rstack, &val, &x, &y, &index); /* fprintf(stderr, "x = %d, y = %d, index = %d\n", x, y, index); */ ulabel = GET_DATA_FOUR_BYTES(linelab32[y], x); if (ulabel == MAX_LABEL_VALUE) clabel = ulabel; else clabel = lut[ulabel]; cindex = lut[index]; if (clabel == cindex) continue; /* have already seen this one */ if (clabel == MAX_LABEL_VALUE) { /* new one; assign index and try to * propagate to all neighbors */ SET_DATA_FOUR_BYTES(linelab32[y], x, cindex); imin = L_MAX(0, y - 1); imax = L_MIN(h - 1, y + 1); jmin = L_MAX(0, x - 1); jmax = L_MIN(w - 1, x + 1); for (i = imin; i <= imax; i++) { for (j = jmin; j <= jmax; j++) { if (i == y && j == x) continue; uval = GET_DATA_BYTE(lines8[i], j); pushWSPixel(lh, rstack, (l_int32) uval, j, i, cindex); } } } else { /* pixel is already labeled (differently); must resolve */ /* If both indices are seeds, check if the min height is * greater than mindepth. If so, we have two new watersheds; * locate them and assign to both regions a new index * for further waterfill. If not, absorb the shallower * watershed into the deeper one and continue filling it. */ pixGetPixel(pixsd, x, y, &uval); if (clabel < nseeds && cindex < nseeds) { wshedGetHeight(wshed, val, clabel, &hlabel); wshedGetHeight(wshed, val, cindex, &hindex); hmin = L_MIN(hlabel, hindex); hmax = L_MAX(hlabel, hindex); if (hmin == hmax) { hmin = hlabel; hmax = hindex; } if (wshed->debug) { fprintf(stderr, "clabel,hlabel = %d,%d\n", clabel, hlabel); fprintf(stderr, "hmin = %d, hmax = %d\n", hmin, hmax); fprintf(stderr, "cindex,hindex = %d,%d\n", cindex, hindex); if (hmin < mindepth) fprintf(stderr, "Too shallow!\n"); } if (hmin >= mindepth) { debugWshedMerge(wshed, two_new_watersheds, x, y, clabel, cindex); wshedSaveBasin(wshed, cindex, val - 1); wshedSaveBasin(wshed, clabel, val - 1); numaSetValue(nasi, cindex, 0); numaSetValue(nasi, clabel, 0); if (wshed->debug) fprintf(stderr, "nindex = %d\n", nindex); debugPrintLUT(lut, nindex, wshed->debug); mergeLookup(wshed, clabel, nindex); debugPrintLUT(lut, nindex, wshed->debug); mergeLookup(wshed, cindex, nindex); debugPrintLUT(lut, nindex, wshed->debug); nindex++; } else /* extraneous seed within seeded basin; absorb */ { debugWshedMerge(wshed, seed_absorbed_into_seeded_basin, x, y, clabel, cindex); } maxhindex = clabel; /* TODO: is this part of above 'else'? */ minhindex = cindex; if (hindex > hlabel) { maxhindex = cindex; minhindex = clabel; } mergeLookup(wshed, minhindex, maxhindex); } else if (clabel < nseeds && cindex >= nboth) { /* If one index is a seed and the other is a merge of * 2 watersheds, generate a single watershed. */ debugWshedMerge(wshed, one_new_watershed_label, x, y, clabel, cindex); wshedSaveBasin(wshed, clabel, val - 1); numaSetValue(nasi, clabel, 0); mergeLookup(wshed, clabel, cindex); } else if (cindex < nseeds && clabel >= nboth) { debugWshedMerge(wshed, one_new_watershed_index, x, y, clabel, cindex); wshedSaveBasin(wshed, cindex, val - 1); numaSetValue(nasi, cindex, 0); mergeLookup(wshed, cindex, clabel); } else if (clabel < nseeds) { /* cindex from minima; absorb */ /* If one index is a seed and the other is from a minimum, * merge the minimum wshed into the seed wshed. */ debugWshedMerge(wshed, minima_absorbed_into_seeded_basin, x, y, clabel, cindex); mergeLookup(wshed, cindex, clabel); } else if (cindex < nseeds) { /* clabel from minima; absorb */ debugWshedMerge(wshed, minima_absorbed_into_seeded_basin, x, y, clabel, cindex); mergeLookup(wshed, clabel, cindex); } else { /* If neither index is a seed, just merge */ debugWshedMerge(wshed, minima_absorbed_by_filler_or_another, x, y, clabel, cindex); mergeLookup(wshed, clabel, cindex); } } } #if 0 /* Use the indicator array to save any watersheds that fill * to the maximum value. This seems to screw things up! */ for (i = 0; i < nseeds; i++) { numaGetIValue(nasi, i, &ival); if (ival == 1) { wshedSaveBasin(wshed, lut[i], val - 1); numaSetValue(nasi, i, 0); } } #endif numaDestroy(&nalut); pixDestroy(&pixmin); pixDestroy(&pixsd); ptaDestroy(&ptao); lheapDestroy(&lh, TRUE); lstackDestroy(&rstack, TRUE); return 0; }
/*! * pixDilateGray3v() * * Input: pixs (8 bpp, not cmapped) * Return: pixd, or null on error * * Notes: * (1) Special case for vertical 1x3 brick Sel; * also used as the second step for the 3x3 brick Sel. */ static PIX * pixDilateGray3v(PIX *pixs) { l_uint32 *datas, *datad, *linesi, *linedi; l_int32 w, h, wpl, i, j; l_int32 val0, val1, val2, val3, val4, val5, val6, val7, val8, val9, maxval; PIX *pixd; PROCNAME("pixDilateGray3v"); if (!pixs) return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); if (pixGetDepth(pixs) != 8) return (PIX *)ERROR_PTR("pixs not 8 bpp", procName, NULL); pixd = pixCreateTemplateNoInit(pixs); pixGetDimensions(pixs, &w, &h, NULL); datas = pixGetData(pixs); datad = pixGetData(pixd); wpl = pixGetWpl(pixs); for (j = 0; j < w; j++) { for (i = 1; i < h - 8; i += 8) { linesi = datas + i * wpl; linedi = datad + i * wpl; val0 = GET_DATA_BYTE(linesi - wpl, j); val1 = GET_DATA_BYTE(linesi, j); val2 = GET_DATA_BYTE(linesi + wpl, j); val3 = GET_DATA_BYTE(linesi + 2 * wpl, j); val4 = GET_DATA_BYTE(linesi + 3 * wpl, j); val5 = GET_DATA_BYTE(linesi + 4 * wpl, j); val6 = GET_DATA_BYTE(linesi + 5 * wpl, j); val7 = GET_DATA_BYTE(linesi + 6 * wpl, j); val8 = GET_DATA_BYTE(linesi + 7 * wpl, j); val9 = GET_DATA_BYTE(linesi + 8 * wpl, j); maxval = L_MAX(val1, val2); SET_DATA_BYTE(linedi, j, L_MAX(val0, maxval)); SET_DATA_BYTE(linedi + wpl, j, L_MAX(maxval, val3)); maxval = L_MAX(val3, val4); SET_DATA_BYTE(linedi + 2 * wpl, j, L_MAX(val2, maxval)); SET_DATA_BYTE(linedi + 3 * wpl, j, L_MAX(maxval, val5)); maxval = L_MAX(val5, val6); SET_DATA_BYTE(linedi + 4 * wpl, j, L_MAX(val4, maxval)); SET_DATA_BYTE(linedi + 5 * wpl, j, L_MAX(maxval, val7)); maxval = L_MAX(val7, val8); SET_DATA_BYTE(linedi + 6 * wpl, j, L_MAX(val6, maxval)); SET_DATA_BYTE(linedi + 7 * wpl, j, L_MAX(maxval, val9)); } } return pixd; }
/*! * dewarpaSetValidModels() * * Input: dewa * notests * debug (1 to output information on invalid page models) * Return: 0 if OK, 1 on error * * Notes: * (1) A valid model must meet the rendering requirements, which * include whether or not a vertical disparity model exists * and conditions on curvatures for vertical and horizontal * disparity models. * (2) If @notests == 1, this ignores the curvature constraints * and assumes that all successfully built models are valid. * (3) This function does not need to be called by the application. * It is called by dewarpaInsertRefModels(), which * will destroy all invalid dewarps. Consequently, to inspect * an invalid dewarp model, it must be done before calling * dewarpaInsertRefModels(). */ l_int32 dewarpaSetValidModels(L_DEWARPA *dewa, l_int32 notests, l_int32 debug) { l_int32 i, n, maxcurv, diffcurv, diffedge; L_DEWARP *dew; PROCNAME("dewarpaSetValidModels"); if (!dewa) return ERROR_INT("dewa not defined", procName, 1); n = dewa->maxpage + 1; for (i = 0; i < n; i++) { if ((dew = dewarpaGetDewarp(dewa, i)) == NULL) continue; if (debug) { if (dew->hasref == 1) { L_INFO("page %d: has only a ref model\n", procName, i); } else if (dew->vsuccess == 0) { L_INFO("page %d: no model successfully built\n", procName, i); } else if (!notests) { maxcurv = L_MAX(L_ABS(dew->mincurv), L_ABS(dew->maxcurv)); diffcurv = dew->maxcurv - dew->mincurv; if (dewa->useboth && !dew->hsuccess) L_INFO("page %d: useboth, but no horiz disparity\n", procName, i); if (maxcurv > dewa->max_linecurv) L_INFO("page %d: max curvature %d > max_linecurv\n", procName, i, diffcurv); if (diffcurv < dewa->min_diff_linecurv) L_INFO("page %d: diff curv %d < min_diff_linecurv\n", procName, i, diffcurv); if (diffcurv > dewa->max_diff_linecurv) L_INFO("page %d: abs diff curv %d > max_diff_linecurv\n", procName, i, diffcurv); if (dew->hsuccess) { if (L_ABS(dew->leftslope) > dewa->max_edgeslope) L_INFO("page %d: abs left slope %d > max_edgeslope\n", procName, i, dew->leftslope); if (L_ABS(dew->rightslope) > dewa->max_edgeslope) L_INFO("page %d: abs right slope %d > max_edgeslope\n", procName, i, dew->rightslope); diffedge = L_ABS(dew->leftcurv - dew->rightcurv); if (L_ABS(dew->leftcurv) > dewa->max_edgecurv) L_INFO("page %d: left curvature %d > max_edgecurv\n", procName, i, dew->leftcurv); if (L_ABS(dew->rightcurv) > dewa->max_edgecurv) L_INFO("page %d: right curvature %d > max_edgecurv\n", procName, i, dew->rightcurv); if (diffedge > dewa->max_diff_edgecurv) L_INFO("page %d: abs diff left-right curv %d > " "max_diff_edgecurv\n", procName, i, diffedge); } } } dewarpaTestForValidModel(dewa, dew, notests); } return 0; }
/*! * sarrayMakeWplsCode() */ static SARRAY * sarrayMakeWplsCode(SEL *sel) { char emptystring[] = ""; l_int32 i, j, ymax, dely, allvshifts; l_int32 vshift[32]; SARRAY *sa; PROCNAME("sarrayMakeWplsCode"); if (!sel) return (SARRAY *)ERROR_PTR("sel not defined", procName, NULL); for (i = 0; i < 32; i++) vshift[i] = 0; ymax = 0; for (i = 0; i < sel->sy; i++) { for (j = 0; j < sel->sx; j++) { if (sel->data[i][j] == 1) { dely = L_ABS(i - sel->cy); if (dely < 32) vshift[dely] = 1; ymax = L_MAX(ymax, dely); } } } if (ymax > 31) { L_WARNING("ymax > 31; truncating to 31\n", procName); ymax = 31; } /* Test if this is a vertical brick */ allvshifts = TRUE; for (i = 0; i < ymax; i++) { if (vshift[i] == 0) { allvshifts = FALSE; break; } } if ((sa = sarrayCreate(0)) == NULL) return (SARRAY *)ERROR_PTR("sa not made", procName, NULL); /* Add declarations */ if (allvshifts == TRUE) { /* packs them as well as possible */ if (ymax > 4) sarrayAddString(sa, wpldecls[2], L_COPY); if (ymax > 8) sarrayAddString(sa, wpldecls[6], L_COPY); if (ymax > 12) sarrayAddString(sa, wpldecls[10], L_COPY); if (ymax > 16) sarrayAddString(sa, wpldecls[14], L_COPY); if (ymax > 20) sarrayAddString(sa, wpldecls[18], L_COPY); if (ymax > 24) sarrayAddString(sa, wpldecls[22], L_COPY); if (ymax > 28) sarrayAddString(sa, wpldecls[26], L_COPY); if (ymax > 1) sarrayAddString(sa, wpldecls[ymax - 2], L_COPY); } else { /* puts them one/line */ for (i = 2; i <= ymax; i++) { if (vshift[i]) sarrayAddString(sa, wplgendecls[i - 2], L_COPY); } } sarrayAddString(sa, emptystring, L_COPY); /* Add definitions */ for (i = 2; i <= ymax; i++) { if (vshift[i]) sarrayAddString(sa, wpldefs[i - 2], L_COPY); } return sa; }
/*! * pixErodeGray() * * Input: pixs * hsize (of Sel; must be odd; origin implicitly in center) * vsize (ditto) * Return: pixd * * Notes: * (1) Sel is a brick with all elements being hits * (2) If hsize = vsize = 1, just returns a copy. */ PIX * pixErodeGray(PIX *pixs, l_int32 hsize, l_int32 vsize) { l_uint8 *buffer, *minarray; l_int32 w, h, wplb, wplt; l_int32 leftpix, rightpix, toppix, bottompix, maxsize; l_uint32 *datab, *datat; PIX *pixb, *pixt, *pixd; PROCNAME("pixErodeGray"); if (!pixs) return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); if (pixGetDepth(pixs) != 8) return (PIX *)ERROR_PTR("pixs not 8 bpp", procName, NULL); if (hsize < 1 || vsize < 1) return (PIX *)ERROR_PTR("hsize or vsize < 1", procName, NULL); if ((hsize & 1) == 0 ) { L_WARNING("horiz sel size must be odd; increasing by 1", procName); hsize++; } if ((vsize & 1) == 0 ) { L_WARNING("vert sel size must be odd; increasing by 1", procName); vsize++; } if (hsize == 1 && vsize == 1) return pixCopy(NULL, pixs); if (vsize == 1) { /* horizontal sel */ leftpix = (hsize + 1) / 2; rightpix = (3 * hsize + 1) / 2; toppix = 0; bottompix = 0; } else if (hsize == 1) { /* vertical sel */ leftpix = 0; rightpix = 0; toppix = (vsize + 1) / 2; bottompix = (3 * vsize + 1) / 2; } else { leftpix = (hsize + 1) / 2; rightpix = (3 * hsize + 1) / 2; toppix = (vsize + 1) / 2; bottompix = (3 * vsize + 1) / 2; } if ((pixb = pixAddBorderGeneral(pixs, leftpix, rightpix, toppix, bottompix, 255)) == NULL) return (PIX *)ERROR_PTR("pixb not made", procName, NULL); if ((pixt = pixCreateTemplate(pixb)) == NULL) return (PIX *)ERROR_PTR("pixt not made", procName, NULL); pixGetDimensions(pixt, &w, &h, NULL); datab = pixGetData(pixb); datat = pixGetData(pixt); wplb = pixGetWpl(pixb); wplt = pixGetWpl(pixt); if ((buffer = (l_uint8 *)CALLOC(L_MAX(w, h), sizeof(l_uint8))) == NULL) return (PIX *)ERROR_PTR("buffer not made", procName, NULL); maxsize = L_MAX(hsize, vsize); if ((minarray = (l_uint8 *)CALLOC(2 * maxsize, sizeof(l_uint8))) == NULL) return (PIX *)ERROR_PTR("minarray not made", procName, NULL); if (vsize == 1) erodeGrayLow(datat, w, h, wplt, datab, wplb, hsize, L_HORIZ, buffer, minarray); else if (hsize == 1) erodeGrayLow(datat, w, h, wplt, datab, wplb, vsize, L_VERT, buffer, minarray); else { erodeGrayLow(datat, w, h, wplt, datab, wplb, hsize, L_HORIZ, buffer, minarray); pixSetOrClearBorder(pixt, leftpix, rightpix, toppix, bottompix, PIX_SET); erodeGrayLow(datab, w, h, wplb, datat, wplt, vsize, L_VERT, buffer, minarray); pixDestroy(&pixt); pixt = pixClone(pixb); } if ((pixd = pixRemoveBorderGeneral(pixt, leftpix, rightpix, toppix, bottompix)) == NULL) return (PIX *)ERROR_PTR("pixd not made", procName, NULL); FREE(buffer); FREE(minarray); pixDestroy(&pixb); pixDestroy(&pixt); return pixd; }
/*! * pixReadStreamJpeg() * * Input: stream * colormap flag (0 means return RGB image if color; * 1 means create colormap and return 8 bpp * palette image if color) * reduction (scaling factor: 1, 2, 4 or 8) * &pnwarn (<optional return> number of warnings) * hint: (a bitwise OR of L_HINT_* values); use 0 for no hints * Return: pix, or null on error * * Usage: see pixReadJpeg() */ PIX * pixReadStreamJpeg(FILE *fp, l_int32 cmflag, l_int32 reduction, l_int32 *pnwarn, l_int32 hint) { l_uint8 cyan, yellow, magenta, black, white; l_int32 rval, gval, bval; l_int32 i, j, k; l_int32 w, h, wpl, spp, ncolors, cindex, ycck, cmyk; l_uint32 *data; l_uint32 *line, *ppixel; JSAMPROW rowbuffer; PIX *pix; PIXCMAP *cmap; struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; l_uint8 *comment = NULL; PROCNAME("pixReadStreamJpeg"); if (!fp) return (PIX *)ERROR_PTR("fp not defined", procName, NULL); if (pnwarn) *pnwarn = 0; /* init */ if (cmflag != 0 && cmflag != 1) cmflag = 0; /* default */ if (reduction != 1 && reduction != 2 && reduction != 4 && reduction != 8) return (PIX *)ERROR_PTR("reduction not in {1,2,4,8}", procName, NULL); if (BITS_IN_JSAMPLE != 8) /* set in jmorecfg.h */ return (PIX *)ERROR_PTR("BITS_IN_JSAMPLE != 8", procName, NULL); rewind(fp); pix = NULL; /* init */ if (setjmp(jpeg_jmpbuf)) { pixDestroy(&pix); FREE(rowbuffer); return (PIX *)ERROR_PTR("internal jpeg error", procName, NULL); } rowbuffer = NULL; cinfo.err = jpeg_std_error(&jerr); jerr.error_exit = jpeg_error_do_not_exit; /* catch error; do not exit! */ jpeg_create_decompress(&cinfo); cinfo.client_data = &comment; jpeg_set_marker_processor(&cinfo, JPEG_COM, jpeg_comment_callback); jpeg_stdio_src(&cinfo, fp); jpeg_read_header(&cinfo, TRUE); cinfo.scale_denom = reduction; cinfo.scale_num = 1; if (hint & L_HINT_GRAY) cinfo.out_color_space = JCS_GRAYSCALE; jpeg_calc_output_dimensions(&cinfo); /* Allocate the image and a row buffer */ spp = cinfo.out_color_components; w = cinfo.output_width; h = cinfo.output_height; ycck = (cinfo.jpeg_color_space == JCS_YCCK && spp == 4 && cmflag == 0); cmyk = (cinfo.jpeg_color_space == JCS_CMYK && spp == 4 && cmflag == 0); if (spp != 1 && spp != 3 && !ycck && !cmyk) { if (comment) FREE(comment); return (PIX *)ERROR_PTR("spp must be 1 or 3, or YCCK or CMYK", procName, NULL); } if ((spp == 3 && cmflag == 0) || ycck || cmyk) { /* rgb or 4 bpp color */ rowbuffer = (JSAMPROW)CALLOC(sizeof(JSAMPLE), spp * w); pix = pixCreate(w, h, 32); } else { /* 8 bpp gray or colormapped */ rowbuffer = (JSAMPROW)CALLOC(sizeof(JSAMPLE), w); pix = pixCreate(w, h, 8); } if (!rowbuffer || !pix) { if (comment) FREE(comment); if (rowbuffer) FREE(rowbuffer); pixDestroy(&pix); return (PIX *)ERROR_PTR("rowbuffer or pix not made", procName, NULL); } if (comment) { pixSetText(pix, (char *)comment); FREE(comment); } if (spp == 1) /* Grayscale or colormapped */ jpeg_start_decompress(&cinfo); else { /* Color; spp == 3 or YCCK or CMYK */ if (cmflag == 0) { /* -- 24 bit color in 32 bit pix or YCCK/CMYK -- */ cinfo.quantize_colors = FALSE; jpeg_start_decompress(&cinfo); } else { /* Color quantize to 8 bits */ cinfo.quantize_colors = TRUE; cinfo.desired_number_of_colors = 256; jpeg_start_decompress(&cinfo); /* Construct a pix cmap */ cmap = pixcmapCreate(8); ncolors = cinfo.actual_number_of_colors; for (cindex = 0; cindex < ncolors; cindex++) { rval = cinfo.colormap[0][cindex]; gval = cinfo.colormap[1][cindex]; bval = cinfo.colormap[2][cindex]; pixcmapAddColor(cmap, rval, gval, bval); } pixSetColormap(pix, cmap); } } wpl = pixGetWpl(pix); data = pixGetData(pix); /* Decompress */ if ((spp == 3 && cmflag == 0) || ycck || cmyk) { /* -- 24 bit color -- */ for (i = 0; i < h; i++) { if (jpeg_read_scanlines(&cinfo, &rowbuffer, (JDIMENSION)1) != 1) return (PIX *)ERROR_PTR("bad read scanline", procName, NULL); ppixel = data + i * wpl; if (spp == 3) { for (j = k = 0; j < w; j++) { SET_DATA_BYTE(ppixel, COLOR_RED, rowbuffer[k++]); SET_DATA_BYTE(ppixel, COLOR_GREEN, rowbuffer[k++]); SET_DATA_BYTE(ppixel, COLOR_BLUE, rowbuffer[k++]); ppixel++; } } else { /* This is a conversion from CMYK -> RGB that ignores color profiles, and is invoked when the image header claims to be in CMYK or YCCK colorspace. If in YCCK, libjpeg may be doing YCCK -> CMYK under the hood. To understand why the colors are inverted on read-in, see the "Special color spaces" section of "Using the IJG JPEG Library" by Thomas G. Lane. */ for (j = k = 0; j < w; j++) { cyan = 255 - rowbuffer[k++]; magenta = 255 - rowbuffer[k++]; yellow = 255 - rowbuffer[k++]; white = rowbuffer[k++]; black = 255 - white; rval = 255 - (cyan * white) / 255 - black; gval = 255 - (magenta * white) / 255 - black; bval = 255 - (yellow * white) / 255 - black; rval = L_MIN(L_MAX(rval, 0), 255); gval = L_MIN(L_MAX(gval, 0), 255); bval = L_MIN(L_MAX(bval, 0), 255); composeRGBPixel(rval, gval, bval, ppixel); ppixel++; } } } } else { /* 8 bpp grayscale or colormapped pix */ for (i = 0; i < h; i++) { if (jpeg_read_scanlines(&cinfo, &rowbuffer, (JDIMENSION)1) != 1) return (PIX *)ERROR_PTR("bad read scanline", procName, NULL); line = data + i * wpl; for (j = 0; j < w; j++) SET_DATA_BYTE(line, j, rowbuffer[j]); } } if (pnwarn) *pnwarn = cinfo.err->num_warnings; switch (cinfo.density_unit) { case 1: /* pixels per inch */ pixSetXRes(pix, cinfo.X_density); pixSetYRes(pix, cinfo.Y_density); break; case 2: /* pixels per centimeter */ pixSetXRes(pix, (l_int32)((l_float32)cinfo.X_density * 2.54 + 0.5)); pixSetYRes(pix, (l_int32)((l_float32)cinfo.Y_density * 2.54 + 0.5)); break; default: /* the pixel density may not be defined; ignore */ break; } jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); FREE(rowbuffer); return pix; }
/*! * pixaDisplayTiled() * * Input: pixa * maxwidth (of output image) * background (0 for white, 1 for black) * spacing * Return: pix of tiled images, or null on error * * Notes: * (1) This saves a pixa to a single image file of width not to * exceed maxwidth, with background color either white or black, * and with each subimage spaced on a regular lattice. * (2) The lattice size is determined from the largest width and height, * separately, of all pix in the pixa. * (3) All pix in the pixa must be of equal depth. * (4) If any pix has a colormap, all pix are rendered in rgb. * (5) Careful: because no components are omitted, this is * dangerous if there are thousands of small components and * one or more very large one, because the size of the * resulting pix can be huge! */ PIX * pixaDisplayTiled(PIXA *pixa, l_int32 maxwidth, l_int32 background, l_int32 spacing) { l_int32 w, h, wmax, hmax, wd, hd, d, hascmap; l_int32 i, j, n, ni, ncols, nrows; l_int32 ystart, xstart, wt, ht; PIX *pix, *pixt, *pixd; PIXA *pixat; PROCNAME("pixaDisplayTiled"); if (!pixa) return (PIX *)ERROR_PTR("pixa not defined", procName, NULL); /* If any pix have colormaps, generate rgb */ if ((n = pixaGetCount(pixa)) == 0) return (PIX *)ERROR_PTR("no components", procName, NULL); pixaAnyColormaps(pixa, &hascmap); if (hascmap) { pixat = pixaCreate(n); for (i = 0; i < n; i++) { pixt = pixaGetPix(pixa, i, L_CLONE); pix = pixConvertTo32(pixt); pixaAddPix(pixat, pix, L_INSERT); pixDestroy(&pixt); } } else pixat = pixaCopy(pixa, L_CLONE); /* Find the largest width and height of the subimages */ wmax = hmax = 0; for (i = 0; i < n; i++) { pix = pixaGetPix(pixat, i, L_CLONE); pixGetDimensions(pix, &w, &h, NULL); if (i == 0) d = pixGetDepth(pix); else if (d != pixGetDepth(pix)) { pixDestroy(&pix); pixaDestroy(&pixat); return (PIX *)ERROR_PTR("depths not equal", procName, NULL); } if (w > wmax) wmax = w; if (h > hmax) hmax = h; pixDestroy(&pix); } /* Get the number of rows and columns and the output image size */ spacing = L_MAX(spacing, 0); ncols = (l_int32)((l_float32)(maxwidth - spacing) / (l_float32)(wmax + spacing)); nrows = (n + ncols - 1) / ncols; wd = wmax * ncols + spacing * (ncols + 1); hd = hmax * nrows + spacing * (nrows + 1); if ((pixd = pixCreate(wd, hd, d)) == NULL) { pixaDestroy(&pixat); return (PIX *)ERROR_PTR("pixd not made", procName, NULL); } #if 0 fprintf(stderr, " nrows = %d, ncols = %d, wmax = %d, hmax = %d\n", nrows, ncols, wmax, hmax); fprintf(stderr, " space = %d, wd = %d, hd = %d, n = %d\n", space, wd, hd, n); #endif /* Reset the background color if necessary */ if ((background == 1 && d == 1) || (background == 0 && d != 1)) pixSetAll(pixd); /* Blit the images to the dest */ for (i = 0, ni = 0; i < nrows; i++) { ystart = spacing + i * (hmax + spacing); for (j = 0; j < ncols && ni < n; j++, ni++) { xstart = spacing + j * (wmax + spacing); pix = pixaGetPix(pixat, ni, L_CLONE); wt = pixGetWidth(pix); ht = pixGetHeight(pix); pixRasterop(pixd, xstart, ystart, wt, ht, PIX_SRC, pix, 0, 0); pixDestroy(&pix); } } pixaDestroy(&pixat); return pixd; }
/*! * pixFinalAccumulate() * * Input: pixs (32 bpp) * offset (same as used for initialization) * depth (8, 16 or 32 bpp, of destination) * Return: pixd (8, 16 or 32 bpp), or null on error * * Notes: * (1) The offset must be >= 0 and should not exceed 0x40000000. * (2) The offset is subtracted from the src 32 bpp image * (3) For 8 bpp dest, the result is clipped to [0, 0xff] * (4) For 16 bpp dest, the result is clipped to [0, 0xffff] */ PIX * pixFinalAccumulate(PIX *pixs, l_uint32 offset, l_int32 depth) { l_int32 i, j, w, h, wpls, wpld, val; l_uint32 *datas, *datad, *lines, *lined; PIX *pixd; PROCNAME("pixFinalAccumulate"); if (!pixs) return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); if (pixGetDepth(pixs) != 32) return (PIX *)ERROR_PTR("pixs not 32 bpp", procName, NULL); if (depth != 8 && depth != 16 && depth != 32) return (PIX *)ERROR_PTR("dest depth not 8, 16, 32 bpp", procName, NULL); if (offset > 0x40000000) offset = 0x40000000; pixGetDimensions(pixs, &w, &h, NULL); if ((pixd = pixCreate(w, h, depth)) == NULL) return (PIX *)ERROR_PTR("pixd not made", procName, NULL); pixCopyResolution(pixd, pixs); /* but how did pixs get it initially? */ datas = pixGetData(pixs); datad = pixGetData(pixd); wpls = pixGetWpl(pixs); wpld = pixGetWpl(pixd); if (depth == 8) { for (i = 0; i < h; i++) { lines = datas + i * wpls; lined = datad + i * wpld; for (j = 0; j < w; j++) { val = lines[j] - offset; val = L_MAX(0, val); val = L_MIN(255, val); SET_DATA_BYTE(lined, j, (l_uint8)val); } } } else if (depth == 16) { for (i = 0; i < h; i++) { lines = datas + i * wpls; lined = datad + i * wpld; for (j = 0; j < w; j++) { val = lines[j] - offset; val = L_MAX(0, val); val = L_MIN(0xffff, val); SET_DATA_TWO_BYTES(lined, j, (l_uint16)val); } } } else { /* depth == 32 */ for (i = 0; i < h; i++) { lines = datas + i * wpls; lined = datad + i * wpld; for (j = 0; j < w; j++) lined[j] = lines[j] - offset; } } return pixd; }
/*! * pixCorrelationScore() * * Input: pix1 (test pix, 1 bpp) * pix2 (exemplar pix, 1 bpp) * area1 (number of on pixels in pix1) * area2 (number of on pixels in pix2) * delx (x comp of centroid difference) * dely (y comp of centroid difference) * maxdiffw (max width difference of pix1 and pix2) * maxdiffh (max height difference of pix1 and pix2) * tab (sum tab for byte) * Return: correlation score * * Note: we check first that the two pix are roughly the same size. * For jbclass (jbig2) applications at roughly 300 ppi, maxdiffw and * maxdiffh should be at least 2. * * Only if they meet that criterion do we compare the bitmaps. * The centroid difference is used to align the two images to the * nearest integer for the correlation. * * The correlation score is the ratio of the square of the number of * pixels in the AND of the two bitmaps to the product of the number * of ON pixels in each. Denote the number of ON pixels in pix1 * by |1|, the number in pix2 by |2|, and the number in the AND * of pix1 and pix2 by |1 & 2|. The correlation score is then * (|1 & 2|)**2 / (|1|*|2|). * * This score is compared with an input threshold, which can * be modified depending on the weight of the template. * The modified threshold is * thresh + (1.0 - thresh) * weight * R * where * weight is a fixed input factor between 0.0 and 1.0 * R = |2| / area(2) * and area(2) is the total number of pixels in 2 (i.e., width x height). * * To understand why a weight factor is useful, consider what happens * with thick, sans-serif characters that look similar and have a value * of R near 1. Different characters can have a high correlation value, * and the classifier will make incorrect substitutions. The weight * factor raises the threshold for these characters. * * Yet another approach to reduce such substitutions is to run the classifier * in a non-greedy way, matching to the template with the highest * score, not the first template with a score satisfying the matching * constraint. However, this is not particularly effective. * * The implementation here gives the same result as in * pixCorrelationScoreSimple(), where a temporary Pix is made to hold * the AND and implementation uses rasterop: * pixt = pixCreateTemplate(pix1); * pixRasterop(pixt, idelx, idely, wt, ht, PIX_SRC, pix2, 0, 0); * pixRasterop(pixt, 0, 0, wi, hi, PIX_SRC & PIX_DST, pix1, 0, 0); * pixCountPixels(pixt, &count, tab); * pixDestroy(&pixt); * However, here it is done in a streaming fashion, counting as it goes, * and touching memory exactly once, giving a 3-4x speedup over the * simple implementation. This very fast correlation matcher was * contributed by William Rucklidge. */ l_float32 pixCorrelationScore(PIX *pix1, PIX *pix2, l_int32 area1, l_int32 area2, l_float32 delx, /* x(1) - x(3) */ l_float32 dely, /* y(1) - y(3) */ l_int32 maxdiffw, l_int32 maxdiffh, l_int32 *tab) { l_int32 wi, hi, wt, ht, delw, delh, idelx, idely, count; l_int32 wpl1, wpl2, lorow, hirow, locol, hicol; l_int32 x, y, pix1lskip, pix2lskip, rowwords1, rowwords2; l_uint32 word1, word2, andw; l_uint32 *row1, *row2; l_float32 score; PROCNAME("pixCorrelationScore"); if (!pix1 || pixGetDepth(pix1) != 1) return (l_float32)ERROR_FLOAT("pix1 not 1 bpp", procName, 0.0); if (!pix2 || pixGetDepth(pix2) != 1) return (l_float32)ERROR_FLOAT("pix2 not 1 bpp", procName, 0.0); if (!tab) return (l_float32)ERROR_FLOAT("tab not defined", procName, 0.0); if (area1 <= 0 || area2 <= 0) return (l_float32)ERROR_FLOAT("areas must be > 0", procName, 0.0); /* Eliminate based on size difference */ pixGetDimensions(pix1, &wi, &hi, NULL); pixGetDimensions(pix2, &wt, &ht, NULL); delw = L_ABS(wi - wt); if (delw > maxdiffw) return 0.0; delh = L_ABS(hi - ht); if (delh > maxdiffh) return 0.0; /* Round difference to nearest integer */ if (delx >= 0) idelx = (l_int32)(delx + 0.5); else idelx = (l_int32)(delx - 0.5); if (dely >= 0) idely = (l_int32)(dely + 0.5); else idely = (l_int32)(dely - 0.5); count = 0; wpl1 = pixGetWpl(pix1); wpl2 = pixGetWpl(pix2); rowwords2 = wpl2; /* What rows of pix1 need to be considered? Only those underlying the * shifted pix2. */ lorow = L_MAX(idely, 0); hirow = L_MIN(ht + idely, hi); /* Get the pointer to the first row of each image that will be * considered. */ row1 = pixGetData(pix1) + wpl1 * lorow; row2 = pixGetData(pix2) + wpl2 * (lorow - idely); /* Similarly, figure out which columns of pix1 will be considered. */ locol = L_MAX(idelx, 0); hicol = L_MIN(wt + idelx, wi); if (idelx >= 32) { /* pix2 is shifted far enough to the right that pix1's first * word(s) won't contribute to the count. Increment its * pointer to point to the first word that will contribute, * and adjust other values accordingly. */ pix1lskip = idelx >> 5; /* # of words to skip on left */ row1 += pix1lskip; locol -= pix1lskip << 5; hicol -= pix1lskip << 5; idelx &= 31; } else if (idelx <= -32) {
/*! * pixSubtractGray() * * Input: pixd (<optional>; this can be null, equal to pixs1, or * different from pixs1) * pixs1 (can be == to pixd) * pixs2 * Return: pixd always * * Notes: * (1) Arithmetic subtraction of two 8, 16 or 32 bpp images. * (2) Source pixs2 is always subtracted from source pixs1. * (3) Do explicit clipping to 0. * (4) Alignment is to UL corner. * (5) There are 3 cases. The result can go to a new dest, * in-place to pixs1, or to an existing input dest: * (a) pixd == null (src1 - src2) --> new pixd * (b) pixd == pixs1 (src1 - src2) --> src1 (in-place) * (d) pixd != pixs1 (src1 - src2) --> input pixd * (6) pixs2 must be different from both pixd and pixs1. */ PIX * pixSubtractGray(PIX *pixd, PIX *pixs1, PIX *pixs2) { l_int32 i, j, w, h, ws, hs, d, wpls, wpld, val, diff; l_uint32 *datas, *datad, *lines, *lined; PROCNAME("pixSubtractGray"); if (!pixs1) return (PIX *)ERROR_PTR("pixs1 not defined", procName, pixd); if (!pixs2) return (PIX *)ERROR_PTR("pixs2 not defined", procName, pixd); if (pixs2 == pixs1) return (PIX *)ERROR_PTR("pixs2 and pixs1 must differ", procName, pixd); if (pixs2 == pixd) return (PIX *)ERROR_PTR("pixs2 and pixd must differ", procName, pixd); d = pixGetDepth(pixs1); if (d != 8 && d != 16 && d != 32) return (PIX *)ERROR_PTR("pix are not 8, 16 or 32 bpp", procName, pixd); if (pixGetDepth(pixs2) != d) return (PIX *)ERROR_PTR("depths differ (pixs1, pixs2)", procName, pixd); if (pixd && (pixGetDepth(pixd) != d)) return (PIX *)ERROR_PTR("depths differ (pixs1, pixd)", procName, pixd); if (!pixSizesEqual(pixs1, pixs2)) L_WARNING("pixs1 and pixs2 not equal in size\n", procName); if (pixd && !pixSizesEqual(pixs1, pixd)) L_WARNING("pixs1 and pixd not equal in size\n", procName); if (pixs1 != pixd) pixd = pixCopy(pixd, pixs1); /* pixd - pixs2 ==> pixd */ datas = pixGetData(pixs2); datad = pixGetData(pixd); wpls = pixGetWpl(pixs2); wpld = pixGetWpl(pixd); pixGetDimensions(pixs2, &ws, &hs, NULL); pixGetDimensions(pixd, &w, &h, NULL); w = L_MIN(ws, w); h = L_MIN(hs, h); for (i = 0; i < h; i++) { lined = datad + i * wpld; lines = datas + i * wpls; if (d == 8) { for (j = 0; j < w; j++) { diff = GET_DATA_BYTE(lined, j) - GET_DATA_BYTE(lines, j); val = L_MAX(diff, 0); SET_DATA_BYTE(lined, j, val); } } else if (d == 16) { for (j = 0; j < w; j++) { diff = GET_DATA_TWO_BYTES(lined, j) - GET_DATA_TWO_BYTES(lines, j); val = L_MAX(diff, 0); SET_DATA_TWO_BYTES(lined, j, val); } } else { /* d == 32; no clipping */ for (j = 0; j < w; j++) *(lined + j) -= *(lines + j); } } return pixd; }
/*! * pixMinOrMax() * * Input: pixd (<optional> destination: this can be null, * equal to pixs1, or different from pixs1) * pixs1 (can be == to pixd) * pixs2 * type (L_CHOOSE_MIN, L_CHOOSE_MAX) * Return: pixd always * * Notes: * (1) This gives the min or max of two images, component-wise. * (2) The depth can be 8 or 16 bpp for 1 component, and 32 bpp * for a 3 component image. For 32 bpp, ignore the LSB * of each word (the alpha channel) * (3) There are 3 cases: * - if pixd == null, Min(src1, src2) --> new pixd * - if pixd == pixs1, Min(src1, src2) --> src1 (in-place) * - if pixd != pixs1, Min(src1, src2) --> input pixd */ PIX * pixMinOrMax(PIX *pixd, PIX *pixs1, PIX *pixs2, l_int32 type) { l_int32 d, ws, hs, w, h, wpls, wpld, i, j, vals, vald, val; l_int32 rval1, gval1, bval1, rval2, gval2, bval2, rval, gval, bval; l_uint32 *datas, *datad, *lines, *lined; PROCNAME("pixMinOrMax"); if (!pixs1) return (PIX *)ERROR_PTR("pixs1 not defined", procName, pixd); if (!pixs2) return (PIX *)ERROR_PTR("pixs2 not defined", procName, pixd); if (pixs1 == pixs2) return (PIX *)ERROR_PTR("pixs1 and pixs2 must differ", procName, pixd); if (type != L_CHOOSE_MIN && type != L_CHOOSE_MAX) return (PIX *)ERROR_PTR("invalid type", procName, pixd); d = pixGetDepth(pixs1); if (pixGetDepth(pixs2) != d) return (PIX *)ERROR_PTR("depths unequal", procName, pixd); if (d != 8 && d != 16 && d != 32) return (PIX *)ERROR_PTR("depth not 8, 16 or 32 bpp", procName, pixd); if (pixs1 != pixd) pixd = pixCopy(pixd, pixs1); pixGetDimensions(pixs2, &ws, &hs, NULL); pixGetDimensions(pixd, &w, &h, NULL); w = L_MIN(w, ws); h = L_MIN(h, hs); datas = pixGetData(pixs2); datad = pixGetData(pixd); wpls = pixGetWpl(pixs2); wpld = pixGetWpl(pixd); for (i = 0; i < h; i++) { lines = datas + i * wpls; lined = datad + i * wpld; if (d == 8) { for (j = 0; j < w; j++) { vals = GET_DATA_BYTE(lines, j); vald = GET_DATA_BYTE(lined, j); if (type == L_CHOOSE_MIN) val = L_MIN(vals, vald); else /* type == L_CHOOSE_MAX */ val = L_MAX(vals, vald); SET_DATA_BYTE(lined, j, val); } } else if (d == 16) { for (j = 0; j < w; j++) { vals = GET_DATA_TWO_BYTES(lines, j); vald = GET_DATA_TWO_BYTES(lined, j); if (type == L_CHOOSE_MIN) val = L_MIN(vals, vald); else /* type == L_CHOOSE_MAX */ val = L_MAX(vals, vald); SET_DATA_TWO_BYTES(lined, j, val); } } else { /* d == 32 */ for (j = 0; j < w; j++) { extractRGBValues(lines[j], &rval1, &gval1, &bval1); extractRGBValues(lined[j], &rval2, &gval2, &bval2); if (type == L_CHOOSE_MIN) { rval = L_MIN(rval1, rval2); gval = L_MIN(gval1, gval2); bval = L_MIN(bval1, bval2); } else { /* type == L_CHOOSE_MAX */ rval = L_MAX(rval1, rval2); gval = L_MAX(gval1, gval2); bval = L_MAX(bval1, bval2); } composeRGBPixel(rval, gval, bval, lined + j); } } } return pixd; }
/*! * pixSaveTiledOutline() * * Input: pixs (1, 2, 4, 8, 32 bpp) * pixa (the pix are accumulated here) * scalefactor (0.0 to disable; otherwise this is a scale factor) * newrow (0 if placed on the same row as previous; 1 otherwise) * space (horizontal and vertical spacing, in pixels) * linewidth (width of added outline for image; 0 for no outline) * dp (depth of pixa; 8 or 32 bpp; only used on first call) * Return: 0 if OK, 1 on error. * * Notes: * (1) Before calling this function for the first time, use * pixaCreate() to make the @pixa that will accumulate the pix. * This is passed in each time pixSaveTiled() is called. * (2) @scalefactor scales the input image. After scaling and * possible depth conversion, the image is saved in the input * pixa, along with a box that specifies the location to * place it when tiled later. Disable saving the pix by * setting @scalefactor == 0.0. * (3) @newrow and @space specify the location of the new pix * with respect to the last one(s) that were entered. * (4) @dp specifies the depth at which all pix are saved. It can * be only 8 or 32 bpp. Any colormap is removed. This is only * used at the first invocation. * (5) This function uses two variables from call to call. * If they were static, the function would not be .so or thread * safe, and furthermore, there would be interference with two or * more pixa accumulating images at a time. Consequently, * we use the first pix in the pixa to store and obtain both * the depth and the current position of the bottom (one pixel * below the lowest image raster line when laid out using * the boxa). The bottom variable is stored in the input format * field, which is the only field available for storing an int. */ l_int32 pixSaveTiledOutline(PIX *pixs, PIXA *pixa, l_float32 scalefactor, l_int32 newrow, l_int32 space, l_int32 linewidth, l_int32 dp) { l_int32 n, top, left, bx, by, bw, w, h, depth, bottom; BOX *box; PIX *pix1, *pix2, *pix3, *pix4; PROCNAME("pixSaveTiledOutline"); if (scalefactor == 0.0) return 0; if (!pixs) return ERROR_INT("pixs not defined", procName, 1); if (!pixa) return ERROR_INT("pixa not defined", procName, 1); n = pixaGetCount(pixa); if (n == 0) { bottom = 0; if (dp != 8 && dp != 32) { L_WARNING("dp not 8 or 32 bpp; using 32\n", procName); depth = 32; } else { depth = dp; } } else { /* extract the depth and bottom params from the first pix */ pix1 = pixaGetPix(pixa, 0, L_CLONE); depth = pixGetDepth(pix1); bottom = pixGetInputFormat(pix1); /* not typical usage! */ pixDestroy(&pix1); } /* Remove colormap if it exists; otherwise a copy. This * guarantees that pix4 is not a clone of pixs. */ pix1 = pixRemoveColormapGeneral(pixs, REMOVE_CMAP_BASED_ON_SRC, L_COPY); /* Scale and convert to output depth */ if (scalefactor == 1.0) { pix2 = pixClone(pix1); } else if (scalefactor > 1.0) { pix2 = pixScale(pix1, scalefactor, scalefactor); } else if (scalefactor < 1.0) { if (pixGetDepth(pix1) == 1) pix2 = pixScaleToGray(pix1, scalefactor); else pix2 = pixScale(pix1, scalefactor, scalefactor); } pixDestroy(&pix1); if (depth == 8) pix3 = pixConvertTo8(pix2, 0); else pix3 = pixConvertTo32(pix2); pixDestroy(&pix2); /* Add black outline */ if (linewidth > 0) pix4 = pixAddBorder(pix3, linewidth, 0); else pix4 = pixClone(pix3); pixDestroy(&pix3); /* Find position of current pix (UL corner plus size) */ if (n == 0) { top = 0; left = 0; } else if (newrow == 1) { top = bottom + space; left = 0; } else if (n > 0) { pixaGetBoxGeometry(pixa, n - 1, &bx, &by, &bw, NULL); top = by; left = bx + bw + space; } pixGetDimensions(pix4, &w, &h, NULL); bottom = L_MAX(bottom, top + h); box = boxCreate(left, top, w, h); pixaAddPix(pixa, pix4, L_INSERT); pixaAddBox(pixa, box, L_INSERT); /* Save the new bottom value */ pix1 = pixaGetPix(pixa, 0, L_CLONE); pixSetInputFormat(pix1, bottom); /* not typical usage! */ pixDestroy(&pix1); return 0; }
/*! * \brief boxaDisplayTiled() * * \param[in] boxas * \param[in] pixa [optional] background for each box * \param[in] first index of first box * \param[in] last index of last box; use -1 to go to end * \param[in] maxwidth of output image * \param[in] linewidth width of box outlines, before scaling * \param[in] scalefactor applied to every box; use 1.0 for no scaling * \param[in] background 0 for white, 1 for black; this is the color * of the spacing between the images * \param[in] spacing between images, and on outside * \param[in] border width of black border added to each image; * use 0 for no border * \return pixd of tiled images of boxes, or NULL on error * * <pre> * Notes: * (1) Displays each box separately in a tiled 32 bpp image. * (2) If pixa is defined, it must have the same count as the boxa, * and it will be a background over with each box is rendered. * If pixa is not defined, the boxes will be rendered over * blank images of identical size. * (3) See pixaDisplayTiledInRows() for other parameters. * </pre> */ PIX * boxaDisplayTiled(BOXA *boxas, PIXA *pixa, l_int32 first, l_int32 last, l_int32 maxwidth, l_int32 linewidth, l_float32 scalefactor, l_int32 background, l_int32 spacing, l_int32 border) { char buf[32]; l_int32 i, n, npix, w, h, fontsize; L_BMF *bmf; BOX *box; BOXA *boxa; PIX *pix1, *pix2, *pixd; PIXA *pixat; PROCNAME("boxaDisplayTiled"); if (!boxas) return (PIX *)ERROR_PTR("boxas not defined", procName, NULL); boxa = boxaSaveValid(boxas, L_COPY); n = boxaGetCount(boxa); if (pixa) { npix = pixaGetCount(pixa); if (n != npix) { boxaDestroy(&boxa); return (PIX *)ERROR_PTR("boxa and pixa counts differ", procName, NULL); } } first = L_MAX(0, first); if (last < 0) last = n - 1; if (first >= n) { boxaDestroy(&boxa); return (PIX *)ERROR_PTR("invalid first", procName, NULL); } if (last >= n) { L_WARNING("last = %d is beyond max index = %d; adjusting\n", procName, last, n - 1); last = n - 1; } if (first > last) { boxaDestroy(&boxa); return (PIX *)ERROR_PTR("first > last", procName, NULL); } /* Because the bitmap font will be reduced when tiled, choose the * font size inversely with the scale factor. */ if (scalefactor > 0.8) fontsize = 6; else if (scalefactor > 0.6) fontsize = 10; else if (scalefactor > 0.4) fontsize = 14; else if (scalefactor > 0.3) fontsize = 18; else fontsize = 20; bmf = bmfCreate(NULL, fontsize); pixat = pixaCreate(n); boxaGetExtent(boxa, &w, &h, NULL); for (i = first; i <= last; i++) { box = boxaGetBox(boxa, i, L_CLONE); if (!pixa) { pix1 = pixCreate(w, h, 32); pixSetAll(pix1); } else { pix1 = pixaGetPix(pixa, i, L_COPY); } pixSetBorderVal(pix1, 0, 0, 0, 2, 0x0000ff00); snprintf(buf, sizeof(buf), "%d", i); pix2 = pixAddSingleTextblock(pix1, bmf, buf, 0x00ff0000, L_ADD_BELOW, NULL); pixDestroy(&pix1); pixRenderBoxArb(pix2, box, linewidth, 255, 0, 0); pixaAddPix(pixat, pix2, L_INSERT); boxDestroy(&box); } bmfDestroy(&bmf); boxaDestroy(&boxa); pixd = pixaDisplayTiledInRows(pixat, 32, maxwidth, scalefactor, background, spacing, border); pixaDestroy(&pixat); return pixd; }
/*! * pixaDisplayTiledInRows() * * Input: pixa * outdepth (output depth: 1, 8 or 32 bpp) * maxwidth (of output image) * scalefactor (applied to every pix; use 1.0 for no scaling) * background (0 for white, 1 for black; this is the color * of the spacing between the images) * spacing (between images, and on outside) * border (width of black border added to each image; * use 0 for no border) * Return: pixd (of tiled images), or null on error * * Notes: * (1) This saves a pixa to a single image file of width not to * exceed maxwidth, with background color either white or black, * and with each row tiled such that the top of each pix is * aligned and separated by 'spacing' from the next one. * A black border can be added to each pix. * (2) All pix are converted to outdepth; existing colormaps are removed. * (3) This does a reasonably spacewise-efficient job of laying * out the individual pix images into a tiled composite. */ PIX * pixaDisplayTiledInRows(PIXA *pixa, l_int32 outdepth, l_int32 maxwidth, l_float32 scalefactor, l_int32 background, l_int32 spacing, l_int32 border) { l_int32 h; /* cumulative height over all the rows */ l_int32 w; /* cumulative height in the current row */ l_int32 bordval, wtry, wt, ht; l_int32 irow; /* index of current pix in current row */ l_int32 wmaxrow; /* width of the largest row */ l_int32 maxh; /* max height in row */ l_int32 i, j, index, n, x, y, nrows, ninrow; NUMA *nainrow; /* number of pix in the row */ NUMA *namaxh; /* height of max pix in the row */ PIX *pix, *pixn, *pixt, *pixd; PIXA *pixan; PROCNAME("pixaDisplayTiledInRows"); if (!pixa) return (PIX *)ERROR_PTR("pixa not defined", procName, NULL); if (outdepth != 1 && outdepth != 8 && outdepth != 32) return (PIX *)ERROR_PTR("outdepth not in {1, 8, 32}", procName, NULL); if (border < 0) border = 0; if (scalefactor <= 0.0) scalefactor = 1.0; if ((n = pixaGetCount(pixa)) == 0) return (PIX *)ERROR_PTR("no components", procName, NULL); /* Normalize depths, scale, remove colormaps; optionally add border */ pixan = pixaCreate(n); bordval = (outdepth == 1) ? 1 : 0; for (i = 0; i < n; i++) { if ((pix = pixaGetPix(pixa, i, L_CLONE)) == NULL) continue; if (outdepth == 1) pixn = pixConvertTo1(pix, 128); else if (outdepth == 8) pixn = pixConvertTo8(pix, FALSE); else /* outdepth == 32 */ pixn = pixConvertTo32(pix); pixDestroy(&pix); if (scalefactor != 1.0) pixt = pixScale(pixn, scalefactor, scalefactor); else pixt = pixClone(pixn); if (border) pixd = pixAddBorder(pixt, border, bordval); else pixd = pixClone(pixt); pixDestroy(&pixn); pixDestroy(&pixt); pixaAddPix(pixan, pixd, L_INSERT); } if (pixaGetCount(pixan) != n) { n = pixaGetCount(pixan); L_WARNING_INT("only got %d components", procName, n); if (n == 0) { pixaDestroy(&pixan); return (PIX *)ERROR_PTR("no components", procName, NULL); } } /* Compute parameters for layout */ nainrow = numaCreate(0); namaxh = numaCreate(0); wmaxrow = 0; w = h = spacing; maxh = 0; /* max height in row */ for (i = 0, irow = 0; i < n; i++, irow++) { pixaGetPixDimensions(pixan, i, &wt, &ht, NULL); wtry = w + wt + spacing; if (wtry > maxwidth) { /* end the current row and start next one */ numaAddNumber(nainrow, irow); numaAddNumber(namaxh, maxh); wmaxrow = L_MAX(wmaxrow, w); h += maxh + spacing; irow = 0; w = wt + 2 * spacing; maxh = ht; } else { w = wtry; maxh = L_MAX(maxh, ht); } } /* Enter the parameters for the last row */ numaAddNumber(nainrow, irow); numaAddNumber(namaxh, maxh); wmaxrow = L_MAX(wmaxrow, w); h += maxh + spacing; if ((pixd = pixCreate(wmaxrow, h, outdepth)) == NULL) { numaDestroy(&nainrow); numaDestroy(&namaxh); pixaDestroy(&pixan); return (PIX *)ERROR_PTR("pixd not made", procName, NULL); } /* Reset the background color if necessary */ if ((background == 1 && outdepth == 1) || (background == 0 && outdepth != 1)) pixSetAll(pixd); /* Blit the images to the dest */ nrows = numaGetCount(nainrow); y = spacing; for (i = 0, index = 0; i < nrows; i++) { /* over rows */ numaGetIValue(nainrow, i, &ninrow); numaGetIValue(namaxh, i, &maxh); x = spacing; for (j = 0; j < ninrow; j++, index++) { /* over pix in row */ pix = pixaGetPix(pixan, index, L_CLONE); pixGetDimensions(pix, &wt, &ht, NULL); pixRasterop(pixd, x, y, wt, ht, PIX_SRC, pix, 0, 0); pixDestroy(&pix); x += wt + spacing; } y += maxh + spacing; } numaDestroy(&nainrow); numaDestroy(&namaxh); pixaDestroy(&pixan); return pixd; }
/*! * \brief pixItalicWords() * * \param[in] pixs 1 bpp * \param[in] boxaw [optional] word bounding boxes; can be NULL * \param[in] pixw [optional] word box mask; can be NULL * \param[out] pboxa boxa of italic words * \param[in] debugflag 1 for debug output; 0 otherwise * \return 0 if OK, 1 on error * * <pre> * Notes: * (1) You can input the bounding boxes for the words in one of * two forms: as bounding boxes (%boxaw) or as a word mask with * the word bounding boxes filled (%pixw). For example, * to compute %pixw, you can use pixWordMaskByDilation(). * (2) Alternatively, you can set both of these inputs to NULL, * in which case the word mask is generated here. This is * done by dilating and closing the input image to connect * letters within a word, while leaving the words separated. * The parameters are chosen under the assumption that the * input is 10 to 12 pt text, scanned at about 300 ppi. * (3) sel_ital1 and sel_ital2 detect the right edges that are * nearly vertical, at approximately the angle of italic * strokes. We use the right edge to avoid getting seeds * from lower-case 'y'. The typical italic slant has a smaller * angle with the vertical than the 'W', so in most cases we * will not trigger on the slanted lines in the 'W'. * (4) Note that sel_ital2 is shorter than sel_ital1. It is * more appropriate for a typical font scanned at 200 ppi. * </pre> */ l_int32 pixItalicWords(PIX *pixs, BOXA *boxaw, PIX *pixw, BOXA **pboxa, l_int32 debugflag) { char opstring[32]; l_int32 size; BOXA *boxa; PIX *pixsd, *pixm, *pixd; SEL *sel_ital1, *sel_ital2, *sel_ital3; PROCNAME("pixItalicWords"); if (!pixs) return ERROR_INT("pixs not defined", procName, 1); if (!pboxa) return ERROR_INT("&boxa not defined", procName, 1); if (boxaw && pixw) return ERROR_INT("both boxaw and pixw are defined", procName, 1); sel_ital1 = selCreateFromString(str_ital1, 13, 6, NULL); sel_ital2 = selCreateFromString(str_ital2, 10, 6, NULL); sel_ital3 = selCreateFromString(str_ital3, 4, 2, NULL); /* Make the italic seed: extract with HMT; remove noise. * The noise removal close/open is important to exclude * situations where a small slanted line accidentally * matches sel_ital1. */ pixsd = pixHMT(NULL, pixs, sel_ital1); pixClose(pixsd, pixsd, sel_ital3); pixOpen(pixsd, pixsd, sel_ital3); /* Make the word mask. Use input boxes or mask if given. */ size = 0; /* init */ if (boxaw) { pixm = pixCreateTemplate(pixs); pixMaskBoxa(pixm, pixm, boxaw, L_SET_PIXELS); } else if (pixw) { pixm = pixClone(pixw); } else { pixWordMaskByDilation(pixs, NULL, &size, NULL); L_INFO("dilation size = %d\n", procName, size); snprintf(opstring, sizeof(opstring), "d1.5 + c%d.1", size); pixm = pixMorphSequence(pixs, opstring, 0); } /* Binary reconstruction to fill in those word mask * components for which there is at least one seed pixel. */ pixd = pixSeedfillBinary(NULL, pixsd, pixm, 8); boxa = pixConnComp(pixd, NULL, 8); *pboxa = boxa; if (debugflag) { /* Save results at at 2x reduction */ lept_mkdir("lept/ital"); l_int32 res, upper; BOXA *boxat; GPLOT *gplot; NUMA *na; PIXA *pad; PIX *pix1, *pix2, *pix3; pad = pixaCreate(0); boxat = pixConnComp(pixm, NULL, 8); boxaWrite("/tmp/lept/ital/ital.ba", boxat); pixSaveTiledOutline(pixs, pad, 0.5, 1, 20, 2, 32); /* orig */ pixSaveTiledOutline(pixsd, pad, 0.5, 1, 20, 2, 0); /* seed */ pix1 = pixConvertTo32(pixm); pixRenderBoxaArb(pix1, boxat, 3, 255, 0, 0); pixSaveTiledOutline(pix1, pad, 0.5, 1, 20, 2, 0); /* mask + outline */ pixDestroy(&pix1); pixSaveTiledOutline(pixd, pad, 0.5, 1, 20, 2, 0); /* ital mask */ pix1 = pixConvertTo32(pixs); pixRenderBoxaArb(pix1, boxa, 3, 255, 0, 0); pixSaveTiledOutline(pix1, pad, 0.5, 1, 20, 2, 0); /* orig + outline */ pixDestroy(&pix1); pix1 = pixCreateTemplate(pixs); pix2 = pixSetBlackOrWhiteBoxa(pix1, boxa, L_SET_BLACK); pixCopy(pix1, pixs); pix3 = pixDilateBrick(NULL, pixs, 3, 3); pixCombineMasked(pix1, pix3, pix2); pixSaveTiledOutline(pix1, pad, 0.5, 1, 20, 2, 0); /* ital bolded */ pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pix2 = pixaDisplay(pad, 0, 0); pixWrite("/tmp/lept/ital/ital.png", pix2, IFF_PNG); pixDestroy(&pix2); /* Assuming the image represents 6 inches of actual page width, * the pixs resolution is approximately * (width of pixs in pixels) / 6 * and the images have been saved at half this resolution. */ res = pixGetWidth(pixs) / 12; L_INFO("resolution = %d\n", procName, res); l_pdfSetDateAndVersion(0); pixaConvertToPdf(pad, res, 1.0, L_FLATE_ENCODE, 75, "Italic Finder", "/tmp/lept/ital/ital.pdf"); l_pdfSetDateAndVersion(1); pixaDestroy(&pad); boxaDestroy(&boxat); /* Plot histogram of horizontal white run sizes. A small * initial vertical dilation removes most runs that are neither * inter-character nor inter-word. The larger first peak is * from inter-character runs, and the smaller second peak is * from inter-word runs. */ pix1 = pixDilateBrick(NULL, pixs, 1, 15); upper = L_MAX(30, 3 * size); na = pixRunHistogramMorph(pix1, L_RUN_OFF, L_HORIZ, upper); pixDestroy(&pix1); gplot = gplotCreate("/tmp/lept/ital/runhisto", GPLOT_PNG, "Histogram of horizontal runs of white pixels, vs length", "run length", "number of runs"); gplotAddPlot(gplot, NULL, na, GPLOT_LINES, "plot1"); gplotMakeOutput(gplot); gplotDestroy(&gplot); numaDestroy(&na); } selDestroy(&sel_ital1); selDestroy(&sel_ital2); selDestroy(&sel_ital3); pixDestroy(&pixsd); pixDestroy(&pixm); pixDestroy(&pixd); return 0; }
/*! * pixaDisplayTiledAndScaled() * * Input: pixa * outdepth (output depth: 1, 8 or 32 bpp) * tilewidth (each pix is scaled to this width) * ncols (number of tiles in each row) * background (0 for white, 1 for black; this is the color * of the spacing between the images) * spacing (between images, and on outside) * border (width of additional black border on each image; * use 0 for no border) * Return: pix of tiled images, or null on error * * Notes: * (1) This can be used to tile a number of renderings of * an image that are at different scales and depths. * (2) Each image, after scaling and optionally adding the * black border, has width 'tilewidth'. Thus, the border does * not affect the spacing between the image tiles. The * maximum allowed border width is tilewidth / 5. */ PIX * pixaDisplayTiledAndScaled(PIXA *pixa, l_int32 outdepth, l_int32 tilewidth, l_int32 ncols, l_int32 background, l_int32 spacing, l_int32 border) { l_int32 x, y, w, h, wd, hd, d; l_int32 i, n, nrows, maxht, ninrow, irow, bordval; l_int32 *rowht; l_float32 scalefact; PIX *pix, *pixn, *pixt, *pixb, *pixd; PIXA *pixan; PROCNAME("pixaDisplayTiledAndScaled"); if (!pixa) return (PIX *)ERROR_PTR("pixa not defined", procName, NULL); if (outdepth != 1 && outdepth != 8 && outdepth != 32) return (PIX *)ERROR_PTR("outdepth not in {1, 8, 32}", procName, NULL); if (border < 0 || border > tilewidth / 5) border = 0; if ((n = pixaGetCount(pixa)) == 0) return (PIX *)ERROR_PTR("no components", procName, NULL); /* Normalize scale and depth for each pix; optionally add border */ pixan = pixaCreate(n); bordval = (outdepth == 1) ? 1 : 0; for (i = 0; i < n; i++) { if ((pix = pixaGetPix(pixa, i, L_CLONE)) == NULL) continue; pixGetDimensions(pix, &w, &h, &d); scalefact = (l_float32)(tilewidth - 2 * border) / (l_float32)w; if (d == 1 && outdepth > 1 && scalefact < 1.0) pixt = pixScaleToGray(pix, scalefact); else pixt = pixScale(pix, scalefact, scalefact); if (outdepth == 1) pixn = pixConvertTo1(pixt, 128); else if (outdepth == 8) pixn = pixConvertTo8(pixt, FALSE); else /* outdepth == 32 */ pixn = pixConvertTo32(pixt); pixDestroy(&pixt); if (border) pixb = pixAddBorder(pixn, border, bordval); else pixb = pixClone(pixn); pixaAddPix(pixan, pixb, L_INSERT); pixDestroy(&pix); pixDestroy(&pixn); } if ((n = pixaGetCount(pixan)) == 0) { /* should not have changed! */ pixaDestroy(&pixan); return (PIX *)ERROR_PTR("no components", procName, NULL); } /* Determine the size of each row and of pixd */ wd = tilewidth * ncols + spacing * (ncols + 1); nrows = (n + ncols - 1) / ncols; if ((rowht = (l_int32 *)CALLOC(nrows, sizeof(l_int32))) == NULL) return (PIX *)ERROR_PTR("rowht array not made", procName, NULL); maxht = 0; ninrow = 0; irow = 0; for (i = 0; i < n; i++) { pix = pixaGetPix(pixan, i, L_CLONE); ninrow++; pixGetDimensions(pix, &w, &h, NULL); maxht = L_MAX(h, maxht); if (ninrow == ncols) { rowht[irow] = maxht; maxht = ninrow = 0; /* reset */ irow++; } pixDestroy(&pix); } if (ninrow > 0) { /* last fencepost */ rowht[irow] = maxht; irow++; /* total number of rows */ } nrows = irow; hd = spacing * (nrows + 1); for (i = 0; i < nrows; i++) hd += rowht[i]; pixd = pixCreate(wd, hd, outdepth); if ((background == 1 && outdepth == 1) || (background == 0 && outdepth != 1)) pixSetAll(pixd); /* Now blit images to pixd */ x = y = spacing; irow = 0; for (i = 0; i < n; i++) { pix = pixaGetPix(pixan, i, L_CLONE); pixGetDimensions(pix, &w, &h, NULL); if (i && ((i % ncols) == 0)) { /* start new row */ x = spacing; y += spacing + rowht[irow]; irow++; } pixRasterop(pixd, x, y, w, h, PIX_SRC, pix, 0, 0); x += tilewidth + spacing; pixDestroy(&pix); } pixaDestroy(&pixan); FREE(rowht); return pixd; }
/*! * pixReadStreamJpeg() * * Input: stream * cmapflag (0 for no colormap in returned pix; * 1 to return an 8 bpp cmapped pix if spp = 3 or 4) * reduction (scaling factor: 1, 2, 4 or 8) * &nwarn (<optional return> number of warnings) * hint (a bitwise OR of L_JPEG_* values; 0 for default) * Return: pix, or null on error * * Usage: see pixReadJpeg() * Notes: * (1) The jpeg comment, if it exists, is not stored in the pix. */ PIX * pixReadStreamJpeg(FILE *fp, l_int32 cmapflag, l_int32 reduction, l_int32 *pnwarn, l_int32 hint) { l_int32 cyan, yellow, magenta, black, nwarn; l_int32 i, j, k, rval, gval, bval; l_int32 w, h, wpl, spp, ncolors, cindex, ycck, cmyk; l_uint32 *data; l_uint32 *line, *ppixel; JSAMPROW rowbuffer; PIX *pix; PIXCMAP *cmap; struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; jmp_buf jmpbuf; /* must be local to the function */ PROCNAME("pixReadStreamJpeg"); if (pnwarn) *pnwarn = 0; if (!fp) return (PIX *)ERROR_PTR("fp not defined", procName, NULL); if (cmapflag != 0 && cmapflag != 1) cmapflag = 0; /* default */ if (reduction != 1 && reduction != 2 && reduction != 4 && reduction != 8) return (PIX *)ERROR_PTR("reduction not in {1,2,4,8}", procName, NULL); if (BITS_IN_JSAMPLE != 8) /* set in jmorecfg.h */ return (PIX *)ERROR_PTR("BITS_IN_JSAMPLE != 8", procName, NULL); rewind(fp); pix = NULL; rowbuffer = NULL; /* Modify the jpeg error handling to catch fatal errors */ cinfo.err = jpeg_std_error(&jerr); jerr.error_exit = jpeg_error_catch_all_1; cinfo.client_data = (void *)&jmpbuf; if (setjmp(jmpbuf)) { pixDestroy(&pix); FREE(rowbuffer); return (PIX *)ERROR_PTR("internal jpeg error", procName, NULL); } /* Initialize jpeg structs for decompression */ jpeg_create_decompress(&cinfo); jpeg_stdio_src(&cinfo, fp); jpeg_read_header(&cinfo, TRUE); cinfo.scale_denom = reduction; cinfo.scale_num = 1; jpeg_calc_output_dimensions(&cinfo); if (hint & L_JPEG_READ_LUMINANCE) { cinfo.out_color_space = JCS_GRAYSCALE; spp = 1; L_INFO("reading luminance channel only\n", procName); } else { spp = cinfo.out_color_components; } /* Allocate the image and a row buffer */ w = cinfo.output_width; h = cinfo.output_height; ycck = (cinfo.jpeg_color_space == JCS_YCCK && spp == 4 && cmapflag == 0); cmyk = (cinfo.jpeg_color_space == JCS_CMYK && spp == 4 && cmapflag == 0); if (spp != 1 && spp != 3 && !ycck && !cmyk) { return (PIX *)ERROR_PTR("spp must be 1 or 3, or YCCK or CMYK", procName, NULL); } if ((spp == 3 && cmapflag == 0) || ycck || cmyk) { /* rgb or 4 bpp color */ rowbuffer = (JSAMPROW)CALLOC(sizeof(JSAMPLE), spp * w); pix = pixCreate(w, h, 32); } else { /* 8 bpp gray or colormapped */ rowbuffer = (JSAMPROW)CALLOC(sizeof(JSAMPLE), w); pix = pixCreate(w, h, 8); } if (!rowbuffer || !pix) { FREE(rowbuffer); pixDestroy(&pix); return (PIX *)ERROR_PTR("rowbuffer or pix not made", procName, NULL); } /* Initialize decompression. Set up a colormap for color * quantization if requested. */ if (spp == 1) { /* Grayscale or colormapped */ jpeg_start_decompress(&cinfo); } else { /* Color; spp == 3 or YCCK or CMYK */ if (cmapflag == 0) { /* 24 bit color in 32 bit pix or YCCK/CMYK */ cinfo.quantize_colors = FALSE; jpeg_start_decompress(&cinfo); } else { /* Color quantize to 8 bits */ cinfo.quantize_colors = TRUE; cinfo.desired_number_of_colors = 256; jpeg_start_decompress(&cinfo); /* Construct a pix cmap */ cmap = pixcmapCreate(8); ncolors = cinfo.actual_number_of_colors; for (cindex = 0; cindex < ncolors; cindex++) { rval = cinfo.colormap[0][cindex]; gval = cinfo.colormap[1][cindex]; bval = cinfo.colormap[2][cindex]; pixcmapAddColor(cmap, rval, gval, bval); } pixSetColormap(pix, cmap); } } wpl = pixGetWpl(pix); data = pixGetData(pix); /* Decompress. Unfortunately, we cannot use the return value * from jpeg_read_scanlines() to determine if there was a problem * with the data; it always appears to return 1. We can only * tell from the warnings during decoding, such as "premature * end of data segment". The default behavior is to return an * image even if there are warnings. However, by setting the * hint to have the same bit flag as L_JPEG_FAIL_ON_BAD_DATA, * no image will be returned if there are any warnings. */ for (i = 0; i < h; i++) { if (jpeg_read_scanlines(&cinfo, &rowbuffer, (JDIMENSION)1) == 0) { L_ERROR("read error at scanline %d\n", procName, i); pixDestroy(&pix); jpeg_destroy_decompress(&cinfo); FREE(rowbuffer); return (PIX *)ERROR_PTR("bad data", procName, NULL); } /* -- 24 bit color -- */ if ((spp == 3 && cmapflag == 0) || ycck || cmyk) { ppixel = data + i * wpl; if (spp == 3) { for (j = k = 0; j < w; j++) { SET_DATA_BYTE(ppixel, COLOR_RED, rowbuffer[k++]); SET_DATA_BYTE(ppixel, COLOR_GREEN, rowbuffer[k++]); SET_DATA_BYTE(ppixel, COLOR_BLUE, rowbuffer[k++]); ppixel++; } } else { /* This is a conversion from CMYK -> RGB that ignores color profiles, and is invoked when the image header claims to be in CMYK or YCCK colorspace. If in YCCK, libjpeg may be doing YCCK -> CMYK under the hood. To understand why the colors need to be inverted on read-in for the Adobe marker, see the "Special color spaces" section of "Using the IJG JPEG Library" by Thomas G. Lane: http://www.jpegcameras.com/libjpeg/libjpeg-3.html#ss3.1 The non-Adobe conversion is equivalent to: rval = black - black * cyan / 255 ... The Adobe conversion is equivalent to: rval = black - black * (255 - cyan) / 255 ... Note that cyan is the complement to red, and we are subtracting the complement color (weighted by black) from black. For Adobe conversions, where they've already inverted the CMY but not the K, we have to invert again. The results must be clipped to [0 ... 255]. */ for (j = k = 0; j < w; j++) { cyan = rowbuffer[k++]; magenta = rowbuffer[k++]; yellow = rowbuffer[k++]; black = rowbuffer[k++]; if (cinfo.saw_Adobe_marker) { rval = (black * cyan) / 255; gval = (black * magenta) / 255; bval = (black * yellow) / 255; } else { rval = black * (255 - cyan) / 255; gval = black * (255 - magenta) / 255; bval = black * (255 - yellow) / 255; } rval = L_MIN(L_MAX(rval, 0), 255); gval = L_MIN(L_MAX(gval, 0), 255); bval = L_MIN(L_MAX(bval, 0), 255); composeRGBPixel(rval, gval, bval, ppixel); ppixel++; } } } else { /* 8 bpp grayscale or colormapped pix */ line = data + i * wpl; for (j = 0; j < w; j++) SET_DATA_BYTE(line, j, rowbuffer[j]); } } nwarn = cinfo.err->num_warnings; if (pnwarn) *pnwarn = nwarn; /* If the pixel density is neither 1 nor 2, it may not be defined. * In that case, don't set the resolution. */ if (cinfo.density_unit == 1) { /* pixels per inch */ pixSetXRes(pix, cinfo.X_density); pixSetYRes(pix, cinfo.Y_density); } else if (cinfo.density_unit == 2) { /* pixels per centimeter */ pixSetXRes(pix, (l_int32)((l_float32)cinfo.X_density * 2.54 + 0.5)); pixSetYRes(pix, (l_int32)((l_float32)cinfo.Y_density * 2.54 + 0.5)); } if (cinfo.output_components != spp) fprintf(stderr, "output spp = %d, spp = %d\n", cinfo.output_components, spp); jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); FREE(rowbuffer); if (nwarn > 0) { if (hint & L_JPEG_FAIL_ON_BAD_DATA) { L_ERROR("fail with %d warning(s) of bad data\n", procName, nwarn); pixDestroy(&pix); } else { L_WARNING("%d warning(s) of bad data\n", procName, nwarn); } } return pix; }
/*! * pixaaDisplay() * * Input: pixaa * w, h (if set to 0, determines the size from the * b.b. of the components in pixaa) * Return: pix, or null on error * * Notes: * (1) Each pix of the pixaa is displayed at the location given by * its box, translated by the box of the containing pixa * if it exists. */ PIX * pixaaDisplay(PIXAA *pixaa, l_int32 w, l_int32 h) { l_int32 i, j, n, nbox, na, d, wmax, hmax, x, y, xb, yb, wb, hb; BOXA *boxa1; /* top-level boxa */ BOXA *boxa; PIX *pixt, *pixd; PIXA *pixa; PROCNAME("pixaaDisplay"); if (!pixaa) return (PIX *)ERROR_PTR("pixaa not defined", procName, NULL); n = pixaaGetCount(pixaa); if (n == 0) return (PIX *)ERROR_PTR("no components", procName, NULL); /* If w and h not input, determine the minimum size required * to contain the origin and all c.c. */ boxa1 = pixaaGetBoxa(pixaa, L_CLONE); nbox = boxaGetCount(boxa1); if (w == 0 || h == 0) { if (nbox == n) boxaGetExtent(boxa1, &w, &h, NULL); else { /* have to use the lower-level boxa for each pixa */ wmax = hmax = 0; for (i = 0; i < n; i++) { pixa = pixaaGetPixa(pixaa, i, L_CLONE); boxa = pixaGetBoxa(pixa, L_CLONE); boxaGetExtent(boxa, &w, &h, NULL); wmax = L_MAX(wmax, w); hmax = L_MAX(hmax, h); pixaDestroy(&pixa); boxaDestroy(&boxa); } w = wmax; h = hmax; } } /* Get depth from first pix */ pixa = pixaaGetPixa(pixaa, 0, L_CLONE); pixt = pixaGetPix(pixa, 0, L_CLONE); d = pixGetDepth(pixt); pixaDestroy(&pixa); pixDestroy(&pixt); if ((pixd = pixCreate(w, h, d)) == NULL) return (PIX *)ERROR_PTR("pixd not made", procName, NULL); x = y = 0; for (i = 0; i < n; i++) { pixa = pixaaGetPixa(pixaa, i, L_CLONE); if (nbox == n) boxaGetBoxGeometry(boxa1, i, &x, &y, NULL, NULL); na = pixaGetCount(pixa); for (j = 0; j < na; j++) { pixaGetBoxGeometry(pixa, j, &xb, &yb, &wb, &hb); pixt = pixaGetPix(pixa, j, L_CLONE); pixRasterop(pixd, x + xb, y + yb, wb, hb, PIX_PAINT, pixt, 0, 0); pixDestroy(&pixt); } pixaDestroy(&pixa); } boxaDestroy(&boxa1); return pixd; }
/*! * pixDilateGray3h() * * Input: pixs (8 bpp, not cmapped) * Return: pixd, or null on error * * Notes: * (1) Special case for horizontal 3x1 brick Sel; * also used as the first step for the 3x3 brick Sel. */ static PIX * pixDilateGray3h(PIX *pixs) { l_uint32 *datas, *datad, *lines, *lined; l_int32 w, h, wpl, i, j; l_int32 val0, val1, val2, val3, val4, val5, val6, val7, val8, val9, maxval; PIX *pixd; PROCNAME("pixDilateGray3h"); if (!pixs) return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); if (pixGetDepth(pixs) != 8) return (PIX *)ERROR_PTR("pixs not 8 bpp", procName, NULL); pixd = pixCreateTemplateNoInit(pixs); pixSetBorderVal(pixd, 4, 8, 2, 8, 0); /* only to silence valgrind */ pixGetDimensions(pixs, &w, &h, NULL); datas = pixGetData(pixs); datad = pixGetData(pixd); wpl = pixGetWpl(pixs); for (i = 0; i < h; i++) { lines = datas + i * wpl; lined = datad + i * wpl; for (j = 1; j < w - 8; j += 8) { val0 = GET_DATA_BYTE(lines, j - 1); val1 = GET_DATA_BYTE(lines, j); val2 = GET_DATA_BYTE(lines, j + 1); val3 = GET_DATA_BYTE(lines, j + 2); val4 = GET_DATA_BYTE(lines, j + 3); val5 = GET_DATA_BYTE(lines, j + 4); val6 = GET_DATA_BYTE(lines, j + 5); val7 = GET_DATA_BYTE(lines, j + 6); val8 = GET_DATA_BYTE(lines, j + 7); val9 = GET_DATA_BYTE(lines, j + 8); maxval = L_MAX(val1, val2); SET_DATA_BYTE(lined, j, L_MAX(val0, maxval)); SET_DATA_BYTE(lined, j + 1, L_MAX(maxval, val3)); maxval = L_MAX(val3, val4); SET_DATA_BYTE(lined, j + 2, L_MAX(val2, maxval)); SET_DATA_BYTE(lined, j + 3, L_MAX(maxval, val5)); maxval = L_MAX(val5, val6); SET_DATA_BYTE(lined, j + 4, L_MAX(val4, maxval)); SET_DATA_BYTE(lined, j + 5, L_MAX(maxval, val7)); maxval = L_MAX(val7, val8); SET_DATA_BYTE(lined, j + 6, L_MAX(val6, maxval)); SET_DATA_BYTE(lined, j + 7, L_MAX(maxval, val9)); } } return pixd; }
/*! * pixaaDisplayByPixa() * * Input: pixaa * xspace between pix in pixa * yspace between pixa * max width of output pix * Return: pix, or null on error * * Notes: * (1) Displays each pixa on a line (or set of lines), * in order from top to bottom. Within each pixa, * the pix are displayed in order from left to right. * (2) The size of each pix in each pixa is assumed to be * approximately equal to the size of the first pix in * the pixa. If this assumption is not correct, this * function will not work properly. * (3) This ignores the boxa of the pixaa. */ PIX * pixaaDisplayByPixa(PIXAA *pixaa, l_int32 xspace, l_int32 yspace, l_int32 maxw) { l_int32 i, j, npixa, npix; l_int32 width, height, depth, nlines, lwidth; l_int32 x, y, w, h, w0, h0; PIX *pixt, *pixd; PIXA *pixa; PROCNAME("pixaaDisplayByPixa"); if (!pixaa) return (PIX *)ERROR_PTR("pixaa not defined", procName, NULL); if ((npixa = pixaaGetCount(pixaa)) == 0) return (PIX *)ERROR_PTR("no components", procName, NULL); /* Get size of output pix. The width is the minimum of the * maxw and the largest pixa line width. The height is whatever * it needs to be to accommodate all pixa. */ height = 2 * yspace; width = 0; for (i = 0; i < npixa; i++) { pixa = pixaaGetPixa(pixaa, i, L_CLONE); npix = pixaGetCount(pixa); pixt = pixaGetPix(pixa, 0, L_CLONE); if (i == 0) depth = pixGetDepth(pixt); w = pixGetWidth(pixt); lwidth = npix * (w + xspace); nlines = (lwidth + maxw - 1) / maxw; if (nlines > 1) width = maxw; else width = L_MAX(lwidth, width); height += nlines * (pixGetHeight(pixt) + yspace); pixDestroy(&pixt); pixaDestroy(&pixa); } if ((pixd = pixCreate(width, height, depth)) == NULL) return (PIX *)ERROR_PTR("pixd not made", procName, NULL); /* Now layout the pix by pixa */ y = yspace; for (i = 0; i < npixa; i++) { x = 0; pixa = pixaaGetPixa(pixaa, i, L_CLONE); npix = pixaGetCount(pixa); for (j = 0; j < npix; j++) { pixt = pixaGetPix(pixa, j, L_CLONE); if (j == 0) { w0 = pixGetWidth(pixt); h0 = pixGetHeight(pixt); } w = pixGetWidth(pixt); if (width == maxw && x + w >= maxw) { x = 0; y += h0 + yspace; } h = pixGetHeight(pixt); pixRasterop(pixd, x, y, w, h, PIX_PAINT, pixt, 0, 0); pixDestroy(&pixt); x += w0 + xspace; } y += h0 + yspace; pixaDestroy(&pixa); } return pixd; }
/*! * \brief pixGetLocalSkewAngles() * * \param[in] pixs 1 bpp * \param[in] nslices the number of horizontal overlapping slices; must * be larger than 1 and not exceed 20; 0 for default * \param[in] redsweep sweep reduction factor: 1, 2, 4 or 8; * use 0 for default value * \param[in] redsearch search reduction factor: 1, 2, 4 or 8, and not * larger than redsweep; use 0 for default value * \param[in] sweeprange half the full range, assumed about 0; in degrees; * use 0.0 for default value * \param[in] sweepdelta angle increment of sweep; in degrees; * use 0.0 for default value * \param[in] minbsdelta min binary search increment angle; in degrees; * use 0.0 for default value * \param[out] pa [optional] slope of skew as fctn of y * \param[out] pb [optional] intercept at y=0 of skew as fctn of y * \param[in] debug 1 for generating plot of skew angle vs. y; 0 otherwise * \return naskew, or NULL on error * * <pre> * Notes: * (1) The local skew is measured in a set of overlapping strips. * We then do a least square linear fit parameters to get * the slope and intercept parameters a and b in * skew-angle = a * y + b (degrees) * for the local skew as a function of raster line y. * This is then used to make naskew, which can be interpreted * as the computed skew angle (in degrees) at the left edge * of each raster line. * (2) naskew can then be used to find the baselines of text, because * each text line has a baseline that should intersect * the left edge of the image with the angle given by this * array, evaluated at the raster line of intersection. * </pre> */ NUMA * pixGetLocalSkewAngles(PIX *pixs, l_int32 nslices, l_int32 redsweep, l_int32 redsearch, l_float32 sweeprange, l_float32 sweepdelta, l_float32 minbsdelta, l_float32 *pa, l_float32 *pb, l_int32 debug) { l_int32 w, h, hs, i, ystart, yend, ovlap, npts; l_float32 angle, conf, ycenter, a, b; BOX *box; GPLOT *gplot; NUMA *naskew, *nax, *nay; PIX *pix; PTA *pta; PROCNAME("pixGetLocalSkewAngles"); if (!pixs || pixGetDepth(pixs) != 1) return (NUMA *)ERROR_PTR("pixs undefined or not 1 bpp", procName, NULL); if (nslices < 2 || nslices > 20) nslices = DEFAULT_SLICES; if (redsweep < 1 || redsweep > 8) redsweep = DEFAULT_SWEEP_REDUCTION; if (redsearch < 1 || redsearch > redsweep) redsearch = DEFAULT_BS_REDUCTION; if (sweeprange == 0.0) sweeprange = DEFAULT_SWEEP_RANGE; if (sweepdelta == 0.0) sweepdelta = DEFAULT_SWEEP_DELTA; if (minbsdelta == 0.0) minbsdelta = DEFAULT_MINBS_DELTA; pixGetDimensions(pixs, &w, &h, NULL); hs = h / nslices; ovlap = (l_int32)(OVERLAP_FRACTION * hs); pta = ptaCreate(nslices); for (i = 0; i < nslices; i++) { ystart = L_MAX(0, hs * i - ovlap); yend = L_MIN(h - 1, hs * (i + 1) + ovlap); ycenter = (l_float32)(ystart + yend) / 2; box = boxCreate(0, ystart, w, yend - ystart + 1); pix = pixClipRectangle(pixs, box, NULL); pixFindSkewSweepAndSearch(pix, &angle, &conf, redsweep, redsearch, sweeprange, sweepdelta, minbsdelta); if (conf > MIN_ALLOWED_CONFIDENCE) ptaAddPt(pta, ycenter, angle); pixDestroy(&pix); boxDestroy(&box); } /* Do linear least squares fit */ if ((npts = ptaGetCount(pta)) < 2) { ptaDestroy(&pta); return (NUMA *)ERROR_PTR("can't fit skew", procName, NULL); } ptaGetLinearLSF(pta, &a, &b, NULL); if (pa) *pa = a; if (pb) *pb = b; /* Make skew angle array as function of raster line */ naskew = numaCreate(h); for (i = 0; i < h; i++) { angle = a * i + b; numaAddNumber(naskew, angle); } if (debug) { lept_mkdir("lept/baseline"); ptaGetArrays(pta, &nax, &nay); gplot = gplotCreate("/tmp/lept/baseline/skew", GPLOT_PNG, "skew as fctn of y", "y (in raster lines from top)", "angle (in degrees)"); gplotAddPlot(gplot, NULL, naskew, GPLOT_POINTS, "linear lsf"); gplotAddPlot(gplot, nax, nay, GPLOT_POINTS, "actual data pts"); gplotMakeOutput(gplot); gplotDestroy(&gplot); numaDestroy(&nax); numaDestroy(&nay); } ptaDestroy(&pta); return naskew; }
/*! * selaAddTJunctions() * * Input: sela (<optional>) * hlsize (length of each line of hits from origin) * mdist (distance of misses from the origin) * norient (number of orientations; max of 8) * debugflag (1 for debug output) * Return: sela with additional sels, or null on error * * Notes: * (1) Adds hitmiss Sels for the T-junction of two lines. * If the lines are very thin, they must be nearly orthogonal * to register. * (2) The number of Sels generated is 4 * @norient. * (3) It is suggested that @hlsize be chosen at least 1 greater * than @mdist. Try values of (@hlsize, @mdist) such as * (6,5), (7,6), (8,7), (9,7), etc. */ SELA * selaAddTJunctions(SELA *sela, l_float32 hlsize, l_float32 mdist, l_int32 norient, l_int32 debugflag) { char name[L_BUF_SIZE]; l_int32 i, j, k, w, xc, yc; l_float64 pi, halfpi, radincr, jang, radang; l_float64 angle[3], dist[3]; PIX *pixc, *pixm, *pixt; PIXA *pixa; PTA *pta1, *pta2, *pta3; SEL *sel; PROCNAME("selaAddTJunctions"); if (hlsize <= 2) return (SELA *)ERROR_PTR("hlsizel not > 1", procName, NULL); if (norient < 1 || norient > 8) return (SELA *)ERROR_PTR("norient not in [1, ... 8]", procName, NULL); if (!sela) { if ((sela = selaCreate(0)) == NULL) return (SELA *)ERROR_PTR("sela not made", procName, NULL); } pi = 3.1415926535; halfpi = 3.1415926535 / 2.0; radincr = halfpi / (l_float32)norient; w = (l_int32)(2.4 * (L_MAX(hlsize, mdist) + 0.5)); if (w % 2 == 0) w++; xc = w / 2; yc = w / 2; pixa = pixaCreate(4 * norient); for (i = 0; i < norient; i++) { for (j = 0; j < 4; j++) { /* 4 orthogonal orientations */ jang = (l_float32)j * halfpi; /* Set the don't cares */ pixc = pixCreate(w, w, 32); pixSetAll(pixc); /* Add the green lines of hits */ pixm = pixCreate(w, w, 1); radang = (l_float32)i * radincr; pta1 = generatePtaLineFromPt(xc, yc, hlsize + 1, jang + radang); pta2 = generatePtaLineFromPt(xc, yc, hlsize + 1, jang + radang + halfpi); pta3 = generatePtaLineFromPt(xc, yc, hlsize + 1, jang + radang + pi); ptaJoin(pta1, pta2, 0, -1); ptaJoin(pta1, pta3, 0, -1); pixRenderPta(pixm, pta1, L_SET_PIXELS); pixPaintThroughMask(pixc, pixm, 0, 0, 0x00ff0000); ptaDestroy(&pta1); ptaDestroy(&pta2); ptaDestroy(&pta3); /* Add red misses between the lines */ angle[0] = radang + jang - halfpi; angle[1] = radang + jang + 0.5 * halfpi; angle[2] = radang + jang + 1.5 * halfpi; dist[0] = 0.8 * mdist; dist[1] = dist[2] = mdist; for (k = 0; k < 3; k++) { pixSetPixel(pixc, xc + (l_int32)(dist[k] * cos(angle[k])), yc + (l_int32)(dist[k] * sin(angle[k])), 0xff000000); } /* Add dark green for origin */ pixSetPixel(pixc, xc, yc, 0x00550000); /* Generate the sel */ sel = selCreateFromColorPix(pixc, NULL); sprintf(name, "sel_cross_%d", 4 * i + j); selaAddSel(sela, sel, name, 0); if (debugflag) { pixt = pixScaleBySampling(pixc, 10.0, 10.0); pixaAddPix(pixa, pixt, L_INSERT); } pixDestroy(&pixm); pixDestroy(&pixc); } } if (debugflag) { l_int32 w; pixaGetPixDimensions(pixa, 0, &w, NULL, NULL); pixt = pixaDisplayTiledAndScaled(pixa, 32, w, 4, 0, 10, 2); pixWriteTempfile("/tmp", "tsel1.png", pixt, IFF_PNG, 0); pixDisplay(pixt, 0, 100); pixDestroy(&pixt); pixt = selaDisplayInPix(sela, 15, 2, 20, 4); pixWriteTempfile("/tmp", "tsel2.png", pixt, IFF_PNG, 0); pixDisplay(pixt, 500, 100); pixDestroy(&pixt); selaWriteStream(stderr, sela); } pixaDestroy(&pixa); return sela; }
/*! * dewarpaTestForValidModel() * * Input: dewa * dew * notests * Return: 0 if OK, 1 on error * * Notes: * (1) Computes validity of vertical (vvalid) model and both * vertical and horizontal (hvalid) models. * (2) If @notests == 1, this ignores the curvature constraints * and assumes that all successfully built models are valid. * (3) This is just about the models, not the rendering process, * so the value of useboth is not considered here. */ static l_int32 dewarpaTestForValidModel(L_DEWARPA *dewa, L_DEWARP *dew, l_int32 notests) { l_int32 maxcurv, diffcurv, diffedge; PROCNAME("dewarpaTestForValidModel"); if (!dewa || !dew) return ERROR_INT("dewa and dew not both defined", procName, 1); if (notests) { dew->vvalid = dew->vsuccess; dew->hvalid = dew->hsuccess; return 0; } /* No actual model was built */ if (dew->vsuccess == 0) return 0; /* Was previously found not to have a valid model */ if (dew->hasref == 1) return 0; /* vsuccess == 1; a vertical (line) model exists. * First test that the vertical curvatures are within allowed * bounds. Note that all curvatures are signed.*/ maxcurv = L_MAX(L_ABS(dew->mincurv), L_ABS(dew->maxcurv)); diffcurv = dew->maxcurv - dew->mincurv; if (maxcurv <= dewa->max_linecurv && diffcurv >= dewa->min_diff_linecurv && diffcurv <= dewa->max_diff_linecurv) { dew->vvalid = 1; } else { L_INFO("invalid vert model for page %d:\n", procName, dew->pageno); #if DEBUG_INVALID_MODELS fprintf(stderr, " max line curv = %d, max allowed = %d\n", maxcurv, dewa->max_linecurv); fprintf(stderr, " diff line curv = %d, max allowed = %d\n", diffcurv, dewa->max_diff_linecurv); #endif /* DEBUG_INVALID_MODELS */ } /* If a horizontal (edge) model exists, test for validity. */ if (dew->hsuccess) { diffedge = L_ABS(dew->leftcurv - dew->rightcurv); if (L_ABS(dew->leftslope) <= dewa->max_edgeslope && L_ABS(dew->rightslope) <= dewa->max_edgeslope && L_ABS(dew->leftcurv) <= dewa->max_edgecurv && L_ABS(dew->rightcurv) <= dewa->max_edgecurv && diffedge <= dewa->max_diff_edgecurv) { dew->hvalid = 1; } else { L_INFO("invalid horiz model for page %d:\n", procName, dew->pageno); #if DEBUG_INVALID_MODELS fprintf(stderr, " left edge slope = %d, max allowed = %d\n", dew->leftslope, dewa->max_edgeslope); fprintf(stderr, " right edge slope = %d, max allowed = %d\n", dew->rightslope, dewa->max_edgeslope); fprintf(stderr, " left edge curv = %d, max allowed = %d\n", dew->leftcurv, dewa->max_edgecurv); fprintf(stderr, " right edge curv = %d, max allowed = %d\n", dew->rightcurv, dewa->max_edgecurv); fprintf(stderr, " diff edge curv = %d, max allowed = %d\n", diffedge, dewa->max_diff_edgecurv); #endif /* DEBUG_INVALID_MODELS */ } } return 0; }
/*! * pixSaveTiledOutline() * * Input: pixs (1, 2, 4, 8, 32 bpp) * pixa (the pix are accumulated here) * reduction (0 to disable; otherwise this is a reduction factor) * newrow (0 if placed on the same row as previous; 1 otherwise) * space (horizontal and vertical spacing, in pixels) * linewidth (width of added outline for image; 0 for no outline) * dp (depth of pixa; 8 or 32 bpp; only used on first call) * Return: 0 if OK, 1 on error. * * Notes: * (1) Before calling this function for the first time, use * pixaCreate() to make the @pixa that will accumulate the pix. * This is passed in each time pixSaveTiled() is called. * (2) @reduction is the integer reduction factor for the input * image. After reduction and possible depth conversion, * the image is saved in the input pixa, along with a box * that specifies the location to place it when tiled later. * Disable saving the pix by setting reduction == 0. * (3) @newrow and @space specify the location of the new pix * with respect to the last one(s) that were entered. * (4) @dp specifies the depth at which all pix are saved. It can * be only 8 or 32 bpp. Any colormap is removed. This is only * used at the first invocation. * (5) This function uses two variables from call to call. * If they were static, the function would not be .so or thread * safe, and furthermore, there would be interference with two or * more pixa accumulating images at a time. Consequently, * we use the first pix in the pixa to store and obtain both * the depth and the current position of the bottom (one pixel * below the lowest image raster line when laid out using * the boxa). The bottom variable is stored in the input format * field, which is the only field available for storing an int. */ l_int32 pixSaveTiledOutline(PIX *pixs, PIXA *pixa, l_int32 reduction, l_int32 newrow, l_int32 space, l_int32 linewidth, l_int32 dp) { l_int32 n, top, left, bx, by, bw, w, h, depth, bottom; l_float32 scale; BOX *box; PIX *pix, *pixt1, *pixt2, *pixt3; PROCNAME("pixSaveTiledOutline"); if (reduction == 0) return 0; if (!pixs) return ERROR_INT("pixs not defined", procName, 1); if (!pixa) return ERROR_INT("pixa not defined", procName, 1); n = pixaGetCount(pixa); if (n == 0) { bottom = 0; if (dp != 8 && dp != 32) { L_WARNING("dp not 8 or 32 bpp; using 32", procName); depth = 32; } else depth = dp; } else { /* extract the depth and bottom params from the first pix */ pix = pixaGetPix(pixa, 0, L_CLONE); depth = pixGetDepth(pix); bottom = pixGetInputFormat(pix); /* not typical usage! */ pixDestroy(&pix); } /* Scale and convert to output depth */ if (reduction == 1) pixt1 = pixClone(pixs); else { scale = 1. / (l_float32)reduction; if (pixGetDepth(pixs) == 1) pixt1 = pixScaleToGray(pixs, scale); else pixt1 = pixScale(pixs, scale, scale); } if (depth == 8) pixt2 = pixConvertTo8(pixt1, 0); else pixt2 = pixConvertTo32(pixt1); pixDestroy(&pixt1); /* Add black outline */ if (linewidth > 0) pixt3 = pixAddBorder(pixt2, linewidth, 0); else pixt3 = pixClone(pixt2); pixDestroy(&pixt2); /* Find position of current pix (UL corner plus size) */ if (n == 0) { top = 0; left = 0; } else if (newrow == 1) { top = bottom + space; left = 0; } else if (n > 0) { pixaGetBoxGeometry(pixa, n - 1, &bx, &by, &bw, NULL); top = by; left = bx + bw + space; } pixGetDimensions(pixt3, &w, &h, NULL); bottom = L_MAX(bottom, top + h); box = boxCreate(left, top, w, h); pixaAddPix(pixa, pixt3, L_INSERT); pixaAddBox(pixa, box, L_INSERT); /* Save the new bottom value */ pix = pixaGetPix(pixa, 0, L_CLONE); pixSetInputFormat(pix, bottom); /* not typical usage! */ pixDestroy(&pix); return 0; }
/*! * kernelDisplayInPix() * * Input: kernel * size (of grid interiors; odd; minimum size of 17 is enforced) * gthick (grid thickness; minimum size of 2 is enforced) * Return: pix (display of kernel), or null on error * * Notes: * (1) This gives a visual representation of a kernel. * (2) The origin is outlined in red. */ PIX * kernelDisplayInPix(L_KERNEL *kel, l_int32 size, l_int32 gthick) { l_int32 i, j, w, h, sx, sy, cx, cy, width, x0, y0; l_int32 normval; l_float32 minval, maxval, max, val, norm; PIX *pixd, *pixt0, *pixt1; PROCNAME("kernelDisplayInPix"); if (!kel) return (PIX *)ERROR_PTR("kernel not defined", procName, NULL); if (size < 17) { L_WARNING("size < 17; setting to 17", procName); size = 17; } if (size % 2 == 0) size++; if (gthick < 2) { L_WARNING("grid thickness < 2; setting to 2", procName); gthick = 2; } /* Normalize the max value to be 255 for display */ kernelGetParameters(kel, &sy, &sx, &cy, &cx); kernelGetMinMax(kel, &minval, &maxval); max = L_MAX(maxval, -minval); norm = 255. / (l_float32)max; w = size * sx + gthick * (sx + 1); h = size * sy + gthick * (sy + 1); pixd = pixCreate(w, h, 8); /* Generate grid lines */ for (i = 0; i <= sy; i++) pixRenderLine(pixd, 0, gthick / 2 + i * (size + gthick), w - 1, gthick / 2 + i * (size + gthick), gthick, L_SET_PIXELS); for (j = 0; j <= sx; j++) pixRenderLine(pixd, gthick / 2 + j * (size + gthick), 0, gthick / 2 + j * (size + gthick), h - 1, gthick, L_SET_PIXELS); /* Generate mask for each element */ pixt0 = pixCreate(size, size, 1); pixSetAll(pixt0); /* Generate crossed lines for origin pattern */ pixt1 = pixCreate(size, size, 1); width = size / 8; pixRenderLine(pixt1, size / 2, (l_int32)(0.12 * size), size / 2, (l_int32)(0.88 * size), width, L_SET_PIXELS); pixRenderLine(pixt1, (l_int32)(0.15 * size), size / 2, (l_int32)(0.85 * size), size / 2, width, L_FLIP_PIXELS); pixRasterop(pixt1, size / 2 - width, size / 2 - width, 2 * width, 2 * width, PIX_NOT(PIX_DST), NULL, 0, 0); /* Paste the patterns in */ y0 = gthick; for (i = 0; i < sy; i++) { x0 = gthick; for (j = 0; j < sx; j++) { kernelGetElement(kel, i, j, &val); normval = (l_int32)(norm * L_ABS(val)); pixSetMaskedGeneral(pixd, pixt0, normval, x0, y0); if (i == cy && j == cx) pixPaintThroughMask(pixd, pixt1, x0, y0, 255 - normval); x0 += size + gthick; } y0 += size + gthick; } pixDestroy(&pixt0); pixDestroy(&pixt1); return pixd; }
/*! * pixRunlengthTransform() * * Input: pixs (1 bpp) * color (0 for white runs, 1 for black runs) * direction (L_HORIZONTAL_RUNS, L_VERTICAL_RUNS) * depth (8 or 16 bpp) * Return: pixd (8 or 16 bpp), or null on error * * Notes: * (1) The dest Pix is 8 or 16 bpp, with the pixel values * equal to the runlength in which it is a member. * The length is clipped to the max pixel value if necessary. * (2) The color determines if we're labelling white or black runs. * (3) A pixel that is not a member of the chosen color gets * value 0; it belongs to a run of length 0 of the * chosen color. * (4) To convert for maximum dynamic range, either linear or * log, use pixMaxDynamicRange(). */ PIX * pixRunlengthTransform(PIX *pixs, l_int32 color, l_int32 direction, l_int32 depth) { l_int32 i, j, w, h, wpld, bufsize, maxsize, n; l_int32 *start, *end, *buffer; l_uint32 *datad, *lined; PIX *pixt, *pixd; PROCNAME("pixRunlengthTransform"); if (!pixs) return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); if (pixGetDepth(pixs) != 1) return (PIX *)ERROR_PTR("pixs not 1 bpp", procName, NULL); if (depth != 8 && depth != 16) return (PIX *)ERROR_PTR("depth must be 8 or 16 bpp", procName, NULL); pixGetDimensions(pixs, &w, &h, NULL); if (direction == L_HORIZONTAL_RUNS) maxsize = 1 + w / 2; else if (direction == L_VERTICAL_RUNS) maxsize = 1 + h / 2; else return (PIX *)ERROR_PTR("invalid direction", procName, NULL); bufsize = L_MAX(w, h); if ((pixd = pixCreate(w, h, depth)) == NULL) return (PIX *)ERROR_PTR("pixd not made", procName, NULL); datad = pixGetData(pixd); wpld = pixGetWpl(pixd); if ((start = (l_int32 *)CALLOC(maxsize, sizeof(l_int32))) == NULL) return (PIX *)ERROR_PTR("start not made", procName, NULL); if ((end = (l_int32 *)CALLOC(maxsize, sizeof(l_int32))) == NULL) return (PIX *)ERROR_PTR("end not made", procName, NULL); if ((buffer = (l_int32 *)CALLOC(bufsize, sizeof(l_int32))) == NULL) return (PIX *)ERROR_PTR("buffer not made", procName, NULL); /* Use fg runs for evaluation */ if (color == 0) pixt = pixInvert(NULL, pixs); else pixt = pixClone(pixs); if (direction == L_HORIZONTAL_RUNS) { for (i = 0; i < h; i++) { pixFindHorizontalRuns(pixt, i, start, end, &n); runlengthMembershipOnLine(buffer, w, depth, start, end, n); lined = datad + i * wpld; if (depth == 8) { for (j = 0; j < w; j++) SET_DATA_BYTE(lined, j, buffer[j]); } else { /* depth == 16 */ for (j = 0; j < w; j++) SET_DATA_TWO_BYTES(lined, j, buffer[j]); } } } else { /* L_VERTICAL_RUNS */ for (j = 0; j < w; j++) { pixFindVerticalRuns(pixt, j, start, end, &n); runlengthMembershipOnLine(buffer, h, depth, start, end, n); if (depth == 8) { for (i = 0; i < h; i++) { lined = datad + i * wpld; SET_DATA_BYTE(lined, j, buffer[i]); } } else { /* depth == 16 */ for (i = 0; i < h; i++) { lined = datad + i * wpld; SET_DATA_TWO_BYTES(lined, j, buffer[i]); } } } } pixDestroy(&pixt); FREE(start); FREE(end); FREE(buffer); return pixd; }
/*! * identifyWatershedBasin() * * Input: wshed * index (index of basin to be located) * level (of basin at point at which the two basins met) * &box (<return> bounding box of basin) * &pixd (<return> pix of basin, cropped to its bounding box) * Return: 0 if OK, 1 on error * * Notes: * (1) This is a static function, so we assume pixlab, pixs and pixt * exist and are the same size. * (2) It selects all pixels that have the label @index in pixlab * and that have a value in pixs that is less than @level. * (3) It is used whenever two seeded basins meet (typically at a saddle), * or when one seeded basin meets a 'filler'. All identified * basins are saved as a watershed. */ static l_int32 identifyWatershedBasin(L_WSHED *wshed, l_int32 index, l_int32 level, BOX **pbox, PIX **ppixd) { l_int32 imin, imax, jmin, jmax, minx, miny, maxx, maxy; l_int32 bw, bh, i, j, w, h, x, y; l_int32 *lut; l_uint32 label, bval, lval; void **lines8, **linelab32, **linet1; BOX *box; PIX *pixs, *pixt, *pixd; L_QUEUE *lq; PROCNAME("identifyWatershedBasin"); if (!pbox) return ERROR_INT("&box not defined", procName, 1); *pbox = NULL; if (!ppixd) return ERROR_INT("&pixd not defined", procName, 1); *ppixd = NULL; if (!wshed) return ERROR_INT("wshed not defined", procName, 1); /* Make a queue and an auxiliary stack */ lq = lqueueCreate(0); lq->stack = lstackCreate(0); pixs = wshed->pixs; pixt = wshed->pixt; lines8 = wshed->lines8; linelab32 = wshed->linelab32; linet1 = wshed->linet1; lut = wshed->lut; pixGetDimensions(pixs, &w, &h, NULL); /* Prime the queue with the seed pixel for this watershed. */ minx = miny = 1000000; maxx = maxy = 0; ptaGetIPt(wshed->ptas, index, &x, &y); pixSetPixel(pixt, x, y, 1); pushNewPixel(lq, x, y, &minx, &maxx, &miny, &maxy); if (wshed->debug) fprintf(stderr, "prime: (x,y) = (%d, %d)\n", x, y); /* Each pixel in a spreading breadth-first search is inspected. * It is accepted as part of this watershed, and pushed on * the search queue, if: * (1) It has a label value equal to @index * (2) The pixel value is less than @level, the overflow * height at which the two basins join. * (3) It has not yet been seen in this search. */ while (lqueueGetCount(lq) > 0) { popNewPixel(lq, &x, &y); imin = L_MAX(0, y - 1); imax = L_MIN(h - 1, y + 1); jmin = L_MAX(0, x - 1); jmax = L_MIN(w - 1, x + 1); for (i = imin; i <= imax; i++) { for (j = jmin; j <= jmax; j++) { if (j == x && i == y) continue; /* parent */ label = GET_DATA_FOUR_BYTES(linelab32[i], j); if (label == MAX_LABEL_VALUE || lut[label] != index) continue; bval = GET_DATA_BIT(linet1[i], j); if (bval == 1) continue; /* already seen */ lval = GET_DATA_BYTE(lines8[i], j); if (lval >= level) continue; /* too high */ SET_DATA_BIT(linet1[i], j); pushNewPixel(lq, j, i, &minx, &maxx, &miny, &maxy); } } } /* Extract the box and pix, and clear pixt */ bw = maxx - minx + 1; bh = maxy - miny + 1; box = boxCreate(minx, miny, bw, bh); pixd = pixClipRectangle(pixt, box, NULL); pixRasterop(pixt, minx, miny, bw, bh, PIX_SRC ^ PIX_DST, pixd, 0, 0); *pbox = box; *ppixd = pixd; lqueueDestroy(&lq, 1); return 0; }
/*! * pixMirrorDetectDwa() * * Input: pixs (1 bpp, deskewed, English text) * &conf (<return> confidence that text is not LR mirror reversed) * mincount (min number of left + right; use 0 for default) * debug (1 for debug output; 0 otherwise) * Return: 0 if OK, 1 on error * * Notes: * (1) We assume the text is horizontally oriented, with * ascenders going up. * (2) See notes in pixMirrorDetect(). */ l_int32 pixMirrorDetectDwa(PIX *pixs, l_float32 *pconf, l_int32 mincount, l_int32 debug) { char flipsel1[] = "flipsel1"; char flipsel2[] = "flipsel2"; l_int32 count1, count2, nmax; l_float32 nleft, nright; PIX *pixt0, *pixt1, *pixt2, *pixt3; PROCNAME("pixMirrorDetectDwa"); if (!pconf) return ERROR_INT("&conf not defined", procName, 1); *pconf = 0.0; if (!pixs) return ERROR_INT("pixs not defined", procName, 1); if (mincount == 0) mincount = DEFAULT_MIN_MIRROR_FLIP_COUNT; /* Fill x-height characters but not space between them, sort of. */ pixt3 = pixMorphSequenceDwa(pixs, "d1.30", 0); pixXor(pixt3, pixt3, pixs); pixt0 = pixMorphSequenceDwa(pixs, "c15.1", 0); pixXor(pixt0, pixt0, pixs); pixAnd(pixt0, pixt0, pixt3); pixOr(pixt3, pixt0, pixs); pixDestroy(&pixt0); pixt0 = pixAddBorderGeneral(pixt3, ADDED_BORDER, ADDED_BORDER, ADDED_BORDER, ADDED_BORDER, 0); pixDestroy(&pixt3); /* Filter the right-facing characters. */ pixt1 = pixFlipFHMTGen(NULL, pixt0, flipsel1); pixt3 = pixReduceRankBinaryCascade(pixt1, 1, 1, 0, 0); pixCountPixels(pixt3, &count1, NULL); pixDestroy(&pixt1); pixDestroy(&pixt3); /* Filter the left-facing characters. */ pixt2 = pixFlipFHMTGen(NULL, pixt0, flipsel2); pixt3 = pixReduceRankBinaryCascade(pixt2, 1, 1, 0, 0); pixCountPixels(pixt3, &count2, NULL); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt0); nright = (l_float32)count1; nleft = (l_float32)count2; nmax = L_MAX(count1, count2); if (nmax > mincount) *pconf = 2. * ((nright - nleft) / sqrt(nright + nleft)); if (debug) { fprintf(stderr, "nright = %f, nleft = %f\n", nright, nleft); if (*pconf > DEFAULT_MIN_MIRROR_FLIP_CONF) fprintf(stderr, "Text is not mirror reversed\n"); if (*pconf < -DEFAULT_MIN_MIRROR_FLIP_CONF) fprintf(stderr, "Text is mirror reversed\n"); } return 0; }