void GenCleans(const char *fname, l_int32 *pindex, l_int32 thresh, L_BMF *bmf) { l_int32 index, blackval, whiteval; char buf[256]; PIX *pix1, *pix2, *pix3, *pix4, *pix5; blackval = 70; whiteval = 180; index = *pindex; pix1 = pixRead(fname); snprintf(buf, sizeof(buf), "/tmp/lept/adapt_%03d.jpg", index++); pixWrite(buf, pix1, IFF_JFIF_JPEG); pix2 = pixBackgroundNorm(pix1, NULL, NULL, 10, 15, thresh, 25, 200, 2, 1); snprintf(buf, sizeof(buf), "Norm color: fg thresh = %d", thresh); fprintf(stderr, "%s\n", buf); pix3 = pixAddSingleTextline(pix2, bmf, buf, 0x00ff0000, L_ADD_BELOW); snprintf(buf, sizeof(buf), "/tmp/lept/adapt_%03d.jpg", index++); pixWrite(buf, pix3, IFF_JFIF_JPEG); pixDestroy(&pix3); pix3 = pixGammaTRC(NULL, pix2, 1.0, blackval, whiteval); snprintf(buf, sizeof(buf), "Clean color: fg thresh = %d", thresh); pix4 = pixAddSingleTextblock(pix3, bmf, buf, 0x00ff0000, L_ADD_BELOW, NULL); snprintf(buf, sizeof(buf), "/tmp/lept/adapt_%03d.jpg", index++); pixWrite(buf, pix4, IFF_JFIF_JPEG); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); pix2 = pixConvertRGBToGray(pix1, 0.33, 0.34, 0.33); pix3 = pixBackgroundNorm(pix2, NULL, NULL, 10, 15, thresh, 25, 200, 2, 1); pix4 = pixGammaTRC(NULL, pix3, 1.0, blackval, whiteval); snprintf(buf, sizeof(buf), "Clean gray: fg thresh = %d", thresh); pix5 = pixAddSingleTextblock(pix4, bmf, buf, 0x00ff0000, L_ADD_BELOW, NULL); snprintf(buf, sizeof(buf), "/tmp/lept/adapt_%03d.jpg", index++); pixWrite(buf, pix5, IFF_JFIF_JPEG); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); pixDestroy(&pix5); pixDestroy(&pix1); *pindex = index; return; }
void AddTransformsYUV(PIXA *pixa, L_BMF *bmf, l_int32 yval) { char textbuf[256]; l_int32 i, j, wpls; l_uint32 *datas, *lines; PIX *pixs, *pixt1, *pixt2, *pixt3, *pixt4; PIXA *pixat; pixs = pixCreate(225, 225, 32); wpls = pixGetWpl(pixs); datas = pixGetData(pixs); for (i = 0; i < 225; i++) { /* v */ lines = datas + i * wpls; for (j = 0; j < 225; j++) /* u */ composeRGBPixel(yval + 16, j + 16, i + 16, lines + j); } pixat = pixaCreate(3); pixaAddPix(pixat, pixs, L_INSERT); pixt1 = pixConvertYUVToRGB(NULL, pixs); pixaAddPix(pixat, pixt1, L_INSERT); pixt2 = pixConvertRGBToYUV(NULL, pixt1); pixaAddPix(pixat, pixt2, L_INSERT); pixt3 = pixaDisplayTiledAndScaled(pixat, 32, 225, 3, 0, 20, 2); snprintf(textbuf, sizeof(textbuf), "yval = %d", yval); pixt4 = pixAddSingleTextblock(pixt3, bmf, textbuf, 0xff000000, L_ADD_BELOW, NULL); pixaAddPix(pixa, pixt4, L_INSERT); pixDestroy(&pixt3); pixaDestroy(&pixat); return; }
/*! * \brief fpixaDisplayQuadtree() * * \param[in] fpixa mean, variance or root variance * \param[in] factor replication factor at lowest level * \param[in] fontsize 4, ... 20 * \return pixd 8 bpp, mosaic of quadtree images, or NULL on error * * <pre> * Notes: * (1) The mean and root variance fall naturally in the 8 bpp range, * but the variance is typically outside the range. This * function displays 8 bpp pix clipped to 255, so the image * pixels will mostly be 255 (white). * </pre> */ PIX * fpixaDisplayQuadtree(FPIXA *fpixa, l_int32 factor, l_int32 fontsize) { char buf[256]; l_int32 nlevels, i, mag, w; L_BMF *bmf; FPIX *fpix; PIX *pixt1, *pixt2, *pixt3, *pixt4, *pixd; PIXA *pixat; PROCNAME("fpixaDisplayQuadtree"); if (!fpixa) return (PIX *)ERROR_PTR("fpixa not defined", procName, NULL); if ((nlevels = fpixaGetCount(fpixa)) == 0) return (PIX *)ERROR_PTR("pixas empty", procName, NULL); if ((bmf = bmfCreate(NULL, fontsize)) == NULL) L_ERROR("bmf not made; text will not be added", procName); pixat = pixaCreate(nlevels); for (i = 0; i < nlevels; i++) { fpix = fpixaGetFPix(fpixa, i, L_CLONE); pixt1 = fpixConvertToPix(fpix, 8, L_CLIP_TO_ZERO, 0); mag = factor * (1 << (nlevels - i - 1)); pixt2 = pixExpandReplicate(pixt1, mag); pixt3 = pixConvertTo32(pixt2); snprintf(buf, sizeof(buf), "Level %d\n", i); pixt4 = pixAddSingleTextblock(pixt3, bmf, buf, 0xff000000, L_ADD_BELOW, NULL); pixaAddPix(pixat, pixt4, L_INSERT); fpixDestroy(&fpix); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); } w = pixGetWidth(pixt4); pixd = pixaDisplayTiledInRows(pixat, 32, nlevels * (w + 80), 1.0, 0, 30, 2); pixaDestroy(&pixat); bmfDestroy(&bmf); return pixd; }
void AddTextAndSave(PIXA *pixa, PIX *pixs, l_int32 newrow, L_BMF *bmf, const char *textstr, l_int32 location, l_uint32 val) { l_int32 n, ovf; PIX *pixt; pixt = pixAddSingleTextblock(pixs, bmf, textstr, val, location, &ovf); n = pixaGetCount(pixa); pixSaveTiledOutline(pixt, pixa, 1, newrow, 30, 2, 32); if (ovf) fprintf(stderr, "Overflow writing text in image %d\n", n + 1); pixDestroy(&pixt); return; }
/*! * pixSaveTiledWithText() * * Input: pixs (1, 2, 4, 8, 32 bpp) * pixa (the pix are accumulated here; as 32 bpp) * outwidth (in pixels; use 0 to disable entirely) * newrow (1 to start a new row; 0 to go on same row as previous) * space (horizontal and vertical spacing, in pixels) * linewidth (width of added outline for image; 0 for no outline) * bmf (<optional> font struct) * textstr (<optional> text string to be added) * val (color to set the text) * location (L_ADD_ABOVE, L_ADD_AT_TOP, L_ADD_AT_BOT, L_ADD_BELOW) * 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) @outwidth is the scaled width. After scaling, 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 @outwidth == 0. * (3) @newrow and @space specify the location of the new pix * with respect to the last one(s) that were entered. * (4) All pix are saved as 32 bpp RGB. * (5) If both @bmf and @textstr are defined, this generates a pix * with the additional text; otherwise, no text is written. * (6) The text is written before scaling, so it is properly * antialiased in the scaled pix. However, if the pix on * different calls have different widths, the size of the * text will vary. * (7) See pixSaveTiledOutline() for other implementation details. */ l_int32 pixSaveTiledWithText(PIX *pixs, PIXA *pixa, l_int32 outwidth, l_int32 newrow, l_int32 space, l_int32 linewidth, L_BMF *bmf, const char *textstr, l_uint32 val, l_int32 location) { PIX *pix1, *pix2, *pix3, *pix4; PROCNAME("pixSaveTiledWithText"); if (outwidth == 0) return 0; if (!pixs) return ERROR_INT("pixs not defined", procName, 1); if (!pixa) return ERROR_INT("pixa not defined", procName, 1); pix1 = pixConvertTo32(pixs); if (linewidth > 0) pix2 = pixAddBorder(pix1, linewidth, 0); else pix2 = pixClone(pix1); if (bmf && textstr) pix3 = pixAddSingleTextblock(pix2, bmf, textstr, val, location, NULL); else pix3 = pixClone(pix2); pix4 = pixScaleToSize(pix3, outwidth, 0); pixSaveTiled(pix4, pixa, 1.0, newrow, space, 32); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); return 0; }
PIX * TestForRedColor(L_REGPARAMS *rp, const char *fname, l_float32 gold_red, L_BMF *bmf) { char text[32]; PIX *pix1, *pix2; l_int32 hasred; l_float32 ratio; pix1 = pixRead(fname); pixHasHighlightRed(pix1, 1, 0.0001, 2.5, &hasred, &ratio, NULL); regTestCompareValues(rp, gold_red, hasred, 0.0); if (hasred) snprintf(text, sizeof(text), "Has red: ratio = %6.1f", ratio); else snprintf(text, sizeof(text), "Does not have red: ratio = %6.1f", ratio); pix2 = pixAddSingleTextblock(pix1, bmf, text, 0x0000ff00, L_ADD_BELOW, NULL); pixDestroy(&pix1); return pix2; }
int main(int argc, char **argv) { char buf[256]; l_int32 w, h, i, j, k, index, op, dir, stretch; l_float32 del, angle, angledeg; BOX *box; L_BMF *bmf; PIX *pixs, *pix1, *pix2, *pixd; PIXA *pixa; static char mainName[] = "warpertest"; if (argc != 1) return ERROR_INT("syntax: warpertest", mainName, 1); bmf = bmfCreate(NULL, 6); /* -------- Stereoscopic warping --------------*/ #if RUN_WARP pixs = pixRead("german.png"); pixGetDimensions(pixs, &w, &h, NULL); pixa = pixaCreate(50); for (i = 0; i < 50; i++) { /* need to test > 2 widths ! */ j = 7 * i; box = boxCreate(0, 0, w - j, h - j); pix1 = pixClipRectangle(pixs, box, NULL); pixd = pixWarpStereoscopic(pix1, 15, 22, 8, 30, -20, 1); pixSetChromaSampling(pixd, 0); pixaAddPix(pixa, pixd, L_INSERT); pixDestroy(&pix1); boxDestroy(&box); } pixDestroy(&pixs); pixaConvertToPdf(pixa, 100, 1.0, L_JPEG_ENCODE, 0, "warp.pdf", "/tmp/warp.pdf"); pixd = pixaDisplayTiledInRows(pixa, 32, 2000, 1.0, 0, 20, 2); pixWrite("/tmp/warp.jpg", pixd, IFF_JFIF_JPEG); pixaDestroy(&pixa); pixDestroy(&pixd); #endif /* -------- Quadratic Vertical Shear --------------*/ #if RUN_QUAD_VERT_SHEAR pixs = pixCreate(501, 501, 32); pixGetDimensions(pixs, &w, &h, NULL); pixSetAll(pixs); pixRenderLineArb(pixs, 0, 30, 500, 30, 5, 0, 0, 255); pixRenderLineArb(pixs, 0, 110, 500, 110, 5, 0, 255, 0); pixRenderLineArb(pixs, 0, 190, 500, 190, 5, 0, 255, 255); pixRenderLineArb(pixs, 0, 270, 500, 270, 5, 255, 0, 0); pixRenderLineArb(pixs, 0, 360, 500, 360, 5, 255, 0, 255); pixRenderLineArb(pixs, 0, 450, 500, 450, 5, 255, 255, 0); pixa = pixaCreate(50); for (i = 0; i < 50; i++) { j = 3 * i; dir = ((i / 2) & 1) ? L_WARP_TO_RIGHT : L_WARP_TO_LEFT; op = (i & 1) ? L_INTERPOLATED : L_SAMPLED; box = boxCreate(0, 0, w - j, h - j); pix1 = pixClipRectangle(pixs, box, NULL); pix2 = pixQuadraticVShear(pix1, dir, 60, -20, op, L_BRING_IN_WHITE); snprintf(buf, sizeof(buf), "%s, %s", dirstr[dir], opstr[op]); pixd = pixAddSingleTextblock(pix2, bmf, buf, 0xff000000, L_ADD_BELOW, 0); pixaAddPix(pixa, pixd, L_INSERT); pixDestroy(&pix1); pixDestroy(&pix2); boxDestroy(&box); } pixDestroy(&pixs); pixaConvertToPdf(pixa, 100, 1.0, L_FLATE_ENCODE, 0, "quad_vshear.pdf", "/tmp/quad_vshear.pdf"); pixd = pixaDisplayTiledInRows(pixa, 32, 2000, 1.0, 0, 20, 2); pixWrite("/tmp/quad_vshear.jpg", pixd, IFF_PNG); pixaDestroy(&pixa); pixDestroy(&pixd); #endif /* -------- Linear Horizontal stretching --------------*/ #if RUN_LIN_HORIZ_STRETCH pixs = pixRead("german.png"); pixa = pixaCreate(50); for (k = 0; k < 2; k++) { for (i = 0; i < 25; i++) { index = 25 * k + i; stretch = 10 + 4 * i; if (k == 0) stretch = -stretch; dir = (k == 1) ? L_WARP_TO_RIGHT : L_WARP_TO_LEFT; op = (i & 1) ? L_INTERPOLATED : L_SAMPLED; pix1 = pixStretchHorizontal(pixs, dir, L_LINEAR_WARP, stretch, op, L_BRING_IN_WHITE); snprintf(buf, sizeof(buf), "%s, %s", dirstr[dir], opstr[op]); pixd = pixAddSingleTextblock(pix1, bmf, buf, 0xff000000, L_ADD_BELOW, 0); pixaAddPix(pixa, pixd, L_INSERT); pixDestroy(&pix1); } } pixDestroy(&pixs); pixaConvertToPdf(pixa, 100, 1.0, L_JPEG_ENCODE, 0, "linear_hstretch.pdf", "/tmp/linear_hstretch.pdf"); pixd = pixaDisplayTiledInRows(pixa, 32, 2500, 1.0, 0, 20, 2); pixWrite("/tmp/linear_hstretch.jpg", pixd, IFF_JFIF_JPEG); pixaDestroy(&pixa); pixDestroy(&pixd); #endif /* -------- Quadratic Horizontal stretching --------------*/ #if RUN_QUAD_HORIZ_STRETCH pixs = pixRead("german.png"); pixa = pixaCreate(50); for (k = 0; k < 2; k++) { for (i = 0; i < 25; i++) { index = 25 * k + i; stretch = 10 + 4 * i; if (k == 0) stretch = -stretch; dir = (k == 1) ? L_WARP_TO_RIGHT : L_WARP_TO_LEFT; op = (i & 1) ? L_INTERPOLATED : L_SAMPLED; pix1 = pixStretchHorizontal(pixs, dir, L_QUADRATIC_WARP, stretch, op, L_BRING_IN_WHITE); snprintf(buf, sizeof(buf), "%s, %s", dirstr[dir], opstr[op]); pixd = pixAddSingleTextblock(pix1, bmf, buf, 0xff000000, L_ADD_BELOW, 0); pixaAddPix(pixa, pixd, L_INSERT); pixDestroy(&pix1); } } pixDestroy(&pixs); pixaConvertToPdf(pixa, 100, 1.0, L_JPEG_ENCODE, 0, "quad_hstretch.pdf", "/tmp/quad_hstretch.pdf"); pixd = pixaDisplayTiledInRows(pixa, 32, 2500, 1.0, 0, 20, 2); pixWrite("/tmp/quad_hstretch.jpg", pixd, IFF_JFIF_JPEG); pixaDestroy(&pixa); pixDestroy(&pixd); #endif /* -------- Horizontal Shear --------------*/ #if RUN_HORIZ_SHEAR pixs = pixRead("german.png"); pixGetDimensions(pixs, &w, &h, NULL); pixa = pixaCreate(50); for (i = 0; i < 25; i++) { del = 0.2 / 12.; angle = -0.2 + (i - (i & 1)) * del; angledeg = 180. * angle / 3.14159265; op = (i & 1) ? L_INTERPOLATED : L_SAMPLED; if (op == L_SAMPLED) pix1 = pixHShear(NULL, pixs, h / 2, angle, L_BRING_IN_WHITE); else pix1 = pixHShearLI(pixs, h / 2, angle, L_BRING_IN_WHITE); snprintf(buf, sizeof(buf), "%6.2f degree, %s", angledeg, opstr[op]); pixd = pixAddSingleTextblock(pix1, bmf, buf, 0xff000000, L_ADD_BELOW, 0); pixaAddPix(pixa, pixd, L_INSERT); pixDestroy(&pix1); } pixDestroy(&pixs); pixaConvertToPdf(pixa, 100, 1.0, L_JPEG_ENCODE, 0, "hshear.pdf", "/tmp/hshear.pdf"); pixd = pixaDisplayTiledInRows(pixa, 32, 2500, 1.0, 0, 20, 2); pixWrite("/tmp/hshear.jpg", pixd, IFF_JFIF_JPEG); pixaDestroy(&pixa); pixDestroy(&pixd); #endif /* -------- Vertical Shear --------------*/ #if RUN_VERT_SHEAR pixs = pixRead("german.png"); pixGetDimensions(pixs, &w, &h, NULL); pixa = pixaCreate(50); for (i = 0; i < 25; i++) { del = 0.2 / 12.; angle = -0.2 + (i - (i & 1)) * del; angledeg = 180. * angle / 3.14159265; op = (i & 1) ? L_INTERPOLATED : L_SAMPLED; if (op == L_SAMPLED) pix1 = pixVShear(NULL, pixs, w / 2, angle, L_BRING_IN_WHITE); else pix1 = pixVShearLI(pixs, w / 2, angle, L_BRING_IN_WHITE); snprintf(buf, sizeof(buf), "%6.2f degree, %s", angledeg, opstr[op]); pixd = pixAddSingleTextblock(pix1, bmf, buf, 0xff000000, L_ADD_BELOW, 0); pixaAddPix(pixa, pixd, L_INSERT); pixDestroy(&pix1); } pixDestroy(&pixs); pixaConvertToPdf(pixa, 100, 1.0, L_JPEG_ENCODE, 0, "vshear.pdf", "/tmp/vshear.pdf"); pixd = pixaDisplayTiledInRows(pixa, 32, 2500, 1.0, 0, 20, 2); pixWrite("/tmp/vshear.jpg", pixd, IFF_JFIF_JPEG); pixaDestroy(&pixa); pixDestroy(&pixd); #endif bmfDestroy(&bmf); return 0; }
/*! * dewarpaShowArrays() * * Input: dewa * scalefact (on contour images; typ. 0.5) * first (first page model to render) * last (last page model to render; use 0 to go to end) * fontdir (for text bitmap fonts) * Return: 0 if OK, 1 on error * * Notes: * (1) Generates a pdf of contour plots of the disparity arrays. * (2) This only shows actual models; not ref models */ l_int32 dewarpaShowArrays(L_DEWARPA *dewa, l_float32 scalefact, l_int32 first, l_int32 last, const char *fontdir) { char buf[256]; char *pathname; l_int32 i, svd, shd; L_BMF *bmf; L_DEWARP *dew; PIX *pixv, *pixvs, *pixh, *pixhs, *pixt, *pixd; PIXA *pixa; PROCNAME("dewarpaShowArrays"); if (!dewa) return ERROR_INT("dew not defined", procName, 1); if (first < 0 || first > dewa->maxpage) return ERROR_INT("first out of bounds", procName, 1); if (last <= 0 || last > dewa->maxpage) last = dewa->maxpage; if (last < first) return ERROR_INT("last < first", procName, 1); lept_rmdir("lept"); lept_mkdir("lept"); if ((bmf = bmfCreate(fontdir, 8)) == NULL) L_ERROR("bmf not made; page info not displayed", procName); fprintf(stderr, "Generating contour plots\n"); for (i = first; i <= last; i++) { if (i && ((i % 10) == 0)) fprintf(stderr, " .. %d", i); dew = dewarpaGetDewarp(dewa, i); if (!dew) continue; if (dew->hasref == 1) continue; svd = shd = 0; if (dew->sampvdispar) svd = 1; if (dew->samphdispar) shd = 1; if (!svd) { L_ERROR("sampvdispar not made for page %d!\n", procName, i); continue; } /* Generate contour plots at reduced resolution */ dewarpPopulateFullRes(dew, NULL, 0, 0); pixv = fpixRenderContours(dew->fullvdispar, 3.0, 0.15); pixvs = pixScaleBySampling(pixv, scalefact, scalefact); pixDestroy(&pixv); if (shd) { pixh = fpixRenderContours(dew->fullhdispar, 3.0, 0.15); pixhs = pixScaleBySampling(pixh, scalefact, scalefact); pixDestroy(&pixh); } dewarpMinimize(dew); /* Save side-by-side */ pixa = pixaCreate(2); pixaAddPix(pixa, pixvs, L_INSERT); if (shd) pixaAddPix(pixa, pixhs, L_INSERT); pixt = pixaDisplayTiledInRows(pixa, 32, 1500, 1.0, 0, 30, 2); snprintf(buf, sizeof(buf), "Page %d", i); pixd = pixAddSingleTextblock(pixt, bmf, buf, 0x0000ff00, L_ADD_BELOW, NULL); snprintf(buf, sizeof(buf), "arrays_%04d.png", i); pathname = genPathname("/tmp/lept", buf); pixWrite(pathname, pixd, IFF_PNG); pixaDestroy(&pixa); pixDestroy(&pixt); pixDestroy(&pixd); FREE(pathname); } bmfDestroy(&bmf); fprintf(stderr, "\n"); fprintf(stderr, "Generating pdf of contour plots\n"); convertFilesToPdf("/tmp/lept", "arrays_", 90, 1.0, L_FLATE_ENCODE, 0, "Disparity arrays", "/tmp/lept/disparity_arrays.pdf"); fprintf(stderr, "Output written to: /tmp/lept/disparity_arrays.pdf\n"); return 0; }
/*! * dewarpShowResults() * * Input: dewa * sarray (of indexed input images) * boxa (crop boxes for input images; can be null) * firstpage, lastpage * fontdir (for text bitmap fonts) * pdfout (filename) * Return: 0 if OK, 1 on error * * Notes: * (1) This generates a pdf of image pairs (before, after) for * the designated set of input pages. * (2) If the boxa exists, its elements are aligned with numbers * in the filenames in @sa. It is used to crop the input images. * It is assumed that the dewa was generated from the cropped * images. No undercropping is applied before rendering. */ l_int32 dewarpShowResults(L_DEWARPA *dewa, SARRAY *sa, BOXA *boxa, l_int32 firstpage, l_int32 lastpage, const char *fontdir, const char *pdfout) { char bufstr[256]; char *outpath; l_int32 i, modelpage; L_BMF *bmf; BOX *box; L_DEWARP *dew; PIX *pixs, *pixc, *pixd, *pixt1, *pixt2; PIXA *pixa; PROCNAME("dewarpShowResults"); if (!dewa) return ERROR_INT("dewa not defined", procName, 1); if (!sa) return ERROR_INT("sa not defined", procName, 1); if (!pdfout) return ERROR_INT("pdfout not defined", procName, 1); if (firstpage > lastpage) return ERROR_INT("invalid first/last page numbers", procName, 1); lept_rmdir("dewarp_pdfout"); lept_mkdir("dewarp_pdfout"); if ((bmf = bmfCreate(fontdir, 6)) == NULL) L_ERROR("bmf not made; page info not displayed", procName); fprintf(stderr, "Dewarping and generating s/by/s view\n"); for (i = firstpage; i <= lastpage; i++) { if (i && (i % 10 == 0)) fprintf(stderr, ".. %d ", i); pixs = pixReadIndexed(sa, i); if (boxa) { box = boxaGetBox(boxa, i, L_CLONE); pixc = pixClipRectangle(pixs, box, NULL); boxDestroy(&box); } else pixc = pixClone(pixs); dew = dewarpaGetDewarp(dewa, i); pixd = NULL; if (dew) { dewarpaApplyDisparity(dewa, dew->pageno, pixc, GRAYIN_VALUE, 0, 0, &pixd, NULL); dewarpMinimize(dew); } pixa = pixaCreate(2); pixaAddPix(pixa, pixc, L_INSERT); if (pixd) pixaAddPix(pixa, pixd, L_INSERT); pixt1 = pixaDisplayTiledAndScaled(pixa, 32, 500, 2, 0, 35, 2); if (dew) { modelpage = (dew->hasref) ? dew->refpage : dew->pageno; snprintf(bufstr, sizeof(bufstr), "Page %d; using %d\n", i, modelpage); } else snprintf(bufstr, sizeof(bufstr), "Page %d; no dewarp\n", i); pixt2 = pixAddSingleTextblock(pixt1, bmf, bufstr, 0x0000ff00, L_ADD_BELOW, 0); snprintf(bufstr, sizeof(bufstr), "/tmp/dewarp_pdfout/%05d", i); pixWrite(bufstr, pixt2, IFF_JFIF_JPEG); pixaDestroy(&pixa); pixDestroy(&pixs); pixDestroy(&pixt1); pixDestroy(&pixt2); } fprintf(stderr, "\n"); fprintf(stderr, "Generating pdf of result\n"); convertFilesToPdf("/tmp/dewarp_pdfout", NULL, 100, 1.0, L_JPEG_ENCODE, 0, "Dewarp sequence", pdfout); outpath = genPathname(pdfout, NULL); fprintf(stderr, "Output written to: %s\n", outpath); FREE(outpath); bmfDestroy(&bmf); return 0; }
int main(int argc, char **argv) { char textstr[256]; l_int32 w, h, d, i; l_uint32 srcval, dstval; l_float32 scalefact, sat, fract; L_BMF *bmf8; L_KERNEL *kel; NUMA *na; PIX *pix, *pixs, *pixs1, *pixs2, *pixd; PIX *pixt0, *pixt1, *pixt2, *pixt3, *pixt4; PIXA *pixa, *pixaf; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; pix = pixRead(filein); pixGetDimensions(pix, &w, &h, &d); if (d != 32) return ERROR_INT("file not 32 bpp", argv[0], 1); scalefact = (l_float32)WIDTH / (l_float32)w; pixs = pixScale(pix, scalefact, scalefact); w = pixGetWidth(pixs); pixaf = pixaCreate(5); /* TRC: vary gamma */ pixa = pixaCreate(20); for (i = 0; i < 20; i++) { pixt0 = pixGammaTRC(NULL, pixs, 0.3 + 0.15 * i, 0, 255); pixaAddPix(pixa, pixt0, L_INSERT); } pixt1 = pixaDisplayTiledAndScaled(pixa, 32, w, 5, 0, 10, 2); pixSaveTiled(pixt1, pixaf, 1.0, 1, 20, 32); regTestWritePixAndCheck(rp, pixt1, IFF_PNG); /* 0 */ pixDisplayWithTitle(pixt1, 0, 100, "TRC Gamma", rp->display); pixDestroy(&pixt1); pixaDestroy(&pixa); /* TRC: vary black point */ pixa = pixaCreate(20); for (i = 0; i < 20; i++) { pixt0 = pixGammaTRC(NULL, pixs, 1.0, 5 * i, 255); pixaAddPix(pixa, pixt0, L_INSERT); } pixt1 = pixaDisplayTiledAndScaled(pixa, 32, w, 5, 0, 10, 2); pixSaveTiled(pixt1, pixaf, 1.0, 1, 20, 0); regTestWritePixAndCheck(rp, pixt1, IFF_PNG); /* 1 */ pixDisplayWithTitle(pixt1, 300, 100, "TRC", rp->display); pixDestroy(&pixt1); pixaDestroy(&pixa); /* Vary hue */ pixa = pixaCreate(20); for (i = 0; i < 20; i++) { pixt0 = pixModifyHue(NULL, pixs, 0.01 + 0.05 * i); pixaAddPix(pixa, pixt0, L_INSERT); } pixt1 = pixaDisplayTiledAndScaled(pixa, 32, w, 5, 0, 10, 2); pixSaveTiled(pixt1, pixaf, 1.0, 1, 20, 0); regTestWritePixAndCheck(rp, pixt1, IFF_PNG); /* 2 */ pixDisplayWithTitle(pixt1, 600, 100, "Hue", rp->display); pixDestroy(&pixt1); pixaDestroy(&pixa); /* Vary saturation */ pixa = pixaCreate(20); na = numaCreate(20); for (i = 0; i < 20; i++) { pixt0 = pixModifySaturation(NULL, pixs, -0.9 + 0.1 * i); pixMeasureSaturation(pixt0, 1, &sat); pixaAddPix(pixa, pixt0, L_INSERT); numaAddNumber(na, sat); } pixt1 = pixaDisplayTiledAndScaled(pixa, 32, w, 5, 0, 10, 2); pixSaveTiled(pixt1, pixaf, 1.0, 1, 20, 0); gplotSimple1(na, GPLOT_PNG, "/tmp/regout/enhance.7", "Average Saturation"); regTestWritePixAndCheck(rp, pixt1, IFF_PNG); /* 3 */ pixDisplayWithTitle(pixt1, 900, 100, "Saturation", rp->display); numaDestroy(&na); pixDestroy(&pixt1); pixaDestroy(&pixa); /* Vary contrast */ pixa = pixaCreate(20); for (i = 0; i < 20; i++) { pixt0 = pixContrastTRC(NULL, pixs, 0.1 * i); pixaAddPix(pixa, pixt0, L_INSERT); } pixt1 = pixaDisplayTiledAndScaled(pixa, 32, w, 5, 0, 10, 2); pixSaveTiled(pixt1, pixaf, 1.0, 1, 20, 0); regTestWritePixAndCheck(rp, pixt1, IFF_PNG); /* 4 */ pixDisplayWithTitle(pixt1, 0, 400, "Contrast", rp->display); pixDestroy(&pixt1); pixaDestroy(&pixa); /* Vary sharpening */ pixa = pixaCreate(20); for (i = 0; i < 20; i++) { pixt0 = pixUnsharpMasking(pixs, 3, 0.01 + 0.15 * i); pixaAddPix(pixa, pixt0, L_INSERT); } pixt1 = pixaDisplayTiledAndScaled(pixa, 32, w, 5, 0, 10, 2); pixSaveTiled(pixt1, pixaf, 1.0, 1, 20, 0); regTestWritePixAndCheck(rp, pixt1, IFF_PNG); /* 5 */ pixDisplayWithTitle(pixt1, 300, 400, "Sharp", rp->display); pixDestroy(&pixt1); pixaDestroy(&pixa); /* Hue constant mapping to lighter background */ pixa = pixaCreate(11); bmf8 = bmfCreate("fonts", 8); pixt0 = pixRead("candelabrum-11.jpg"); composeRGBPixel(230, 185, 144, &srcval); /* select typical bg pixel */ for (i = 0; i <= 10; i++) { fract = 0.10 * i; pixelFractionalShift(230, 185, 144, fract, &dstval); pixt1 = pixLinearMapToTargetColor(NULL, pixt0, srcval, dstval); snprintf(textstr, 50, "Fract = %5.1f", fract); pixt2 = pixAddSingleTextblock(pixt1, bmf8, textstr, 0xff000000, L_ADD_BELOW, NULL); pixSaveTiledOutline(pixt2, pixa, 1.0, (i % 4 == 0) ? 1 : 0, 30, 2, 32); pixDestroy(&pixt1); pixDestroy(&pixt2); } pixDestroy(&pixt0); pixd = pixaDisplay(pixa, 0, 0); regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 6 */ pixDisplayWithTitle(pixd, 600, 400, "Constant hue", rp->display); bmfDestroy(&bmf8); pixaDestroy(&pixa); pixDestroy(&pixd); /* Delayed testing of saturation plot */ regTestCheckFile(rp, "/tmp/regout/enhance.7.png"); /* 7 */ /* Display results */ pixd = pixaDisplay(pixaf, 0, 0); regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 8 */ pixDisplayWithTitle(pixd, 100, 100, "All", rp->display); pixDestroy(&pixd); pixaDestroy(&pixaf); pixDestroy(&pix); pixDestroy(&pixs); /* -----------------------------------------------* * Test global color transforms * * -----------------------------------------------*/ /* Make identical cmap and rgb images */ pix = pixRead("wet-day.jpg"); pixs1 = pixOctreeColorQuant(pix, 200, 0); pixs2 = pixRemoveColormap(pixs1, REMOVE_CMAP_TO_FULL_COLOR); regTestComparePix(rp, pixs1, pixs2); /* 9 */ /* Make a diagonal color transform matrix */ kel = kernelCreate(3, 3); kernelSetElement(kel, 0, 0, 0.7); kernelSetElement(kel, 1, 1, 0.4); kernelSetElement(kel, 2, 2, 1.3); /* Apply to both cmap and rgb images. */ pixt1 = pixMultMatrixColor(pixs1, kel); pixt2 = pixMultMatrixColor(pixs2, kel); regTestComparePix(rp, pixt1, pixt2); /* 10 */ kernelDestroy(&kel); /* Apply the same transform in the simpler interface */ pixt3 = pixMultConstantColor(pixs1, 0.7, 0.4, 1.3); pixt4 = pixMultConstantColor(pixs2, 0.7, 0.4, 1.3); regTestComparePix(rp, pixt3, pixt4); /* 11 */ regTestComparePix(rp, pixt1, pixt3); /* 12 */ regTestWritePixAndCheck(rp, pixt1, IFF_JFIF_JPEG); /* 13 */ pixDestroy(&pix); pixDestroy(&pixs1); pixDestroy(&pixs2); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); return regTestCleanup(rp); }
/*! * \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; }
l_int32 main(int argc, char **argv) { char buf[512]; l_int32 i, n, index; l_int32 rval[4], gval[4], bval[4]; l_uint32 scolor, dcolor; L_BMF *bmf; PIX *pix0, *pix1, *pix2, *pix3, *pix4, *pix5; PIXA *pixa; PIXCMAP *cmap; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; /* Read in the bg colors */ for (i = 0; i < 4; i++) sscanf(bgcolors[i], "%d %d %d", &rval[i], &gval[i], &bval[i]); bmf = bmfCreate("fonts", 8); /* Get the input image (100 ppi resolution) */ pix0 = pixRead("harmoniam100-11.png"); cmap = pixGetColormap(pix0); pixa = pixaCreate(0); /* Do cmapped coloring on the white pixels only */ pixcmapGetIndex(cmap, 255, 255, 255, &index); /* index of white pixels */ for (i = 0; i < 4; i++) { pixcmapResetColor(cmap, index, rval[i], gval[i], bval[i]); snprintf(buf, sizeof(buf), "(rval, bval, gval) = (%d, %d, %d)", rval[i], gval[i], bval[i]); pix1 = pixAddSingleTextblock(pix0, bmf, buf, 0xff000000, L_ADD_AT_BOT, NULL); pixaAddPix(pixa, pix1, L_INSERT); } /* Do cmapped background coloring on all the pixels */ for (i = 0; i < 4; i++) { scolor = 0xffffff00; /* source color */ composeRGBPixel(rval[i], gval[i], bval[i], &dcolor); /* dest color */ pix1 = pixShiftByComponent(NULL, pix0, scolor, dcolor); snprintf(buf, sizeof(buf), "(rval, bval, gval) = (%d, %d, %d)", rval[i], gval[i], bval[i]); pix2 = pixAddSingleTextblock(pix1, bmf, buf, 0xff000000, L_ADD_AT_BOT, NULL); pixaAddPix(pixa, pix2, L_INSERT); pixDestroy(&pix1); } /* Do background coloring on rgb */ pix1 = pixConvertTo32(pix0); for (i = 0; i < 4; i++) { scolor = 0xffffff00; composeRGBPixel(rval[i], gval[i], bval[i], &dcolor); pix2 = pixShiftByComponent(NULL, pix1, scolor, dcolor); snprintf(buf, sizeof(buf), "(rval, bval, gval) = (%d, %d, %d)", rval[i], gval[i], bval[i]); pix3 = pixAddSingleTextblock(pix2, bmf, buf, 0xff000000, L_ADD_AT_BOT, NULL); pixaAddPix(pixa, pix3, L_INSERT); pixDestroy(&pix2); } pixDestroy(&pix1); /* Compare cmapped & rgb foreground coloring */ scolor = 0x0; /* source color */ composeRGBPixel(200, 30, 150, &dcolor); /* ugly fg dest color */ pix1 = pixShiftByComponent(NULL, pix0, scolor, dcolor); /* cmapped */ snprintf(buf, sizeof(buf), "(rval, bval, gval) = (%d, %d, %d)", 200, 100, 50); pix2 = pixAddSingleTextblock(pix1, bmf, buf, 0xff000000, L_ADD_AT_BOT, NULL); pixaAddPix(pixa, pix2, L_INSERT); pix3 = pixConvertTo32(pix0); pix4 = pixShiftByComponent(NULL, pix3, scolor, dcolor); /* rgb */ snprintf(buf, sizeof(buf), "(rval, bval, gval) = (%d, %d, %d)", 200, 100, 50); pix5 = pixAddSingleTextblock(pix4, bmf, buf, 0xff000000, L_ADD_AT_BOT, NULL); pixaAddPix(pixa, pix5, L_INSERT); regTestComparePix(rp, pix1, pix4); regTestComparePix(rp, pix2, pix5); pixDestroy(&pix1); pixDestroy(&pix3); pixDestroy(&pix4); /* Log all the results */ n = pixaGetCount(pixa); for (i = 0; i < n; i++) { pix1 = pixaGetPix(pixa, i, L_CLONE); regTestWritePixAndCheck(rp, pix1, IFF_PNG); pixDestroy(&pix1); } /* If in testing mode, make a pdf */ if (rp->display) { pixaConvertToPdf(pixa, 100, 1.0, L_FLATE_ENCODE, 0, "Colored background", "/tmp/regout/coloring.pdf"); L_INFO("Output pdf: /tmp/regout/coloring.pdf\n", rp->testname); } pixaDestroy(&pixa); pixDestroy(&pix0); bmfDestroy(&bmf); return regTestCleanup(rp); }
int main(int argc, char **argv) { char textstr[256]; l_int32 i, thresh, fgval, bgval; l_float32 scorefract; L_BMF *bmf; PIX *pixs, *pixb, *pixb2, *pixb3, *pixg, *pixp, *pixt1, *pixt2; PIXA *pixa; pixs = pixRead("1555-7.jpg"); pixg = pixConvertTo8(pixs, 0); bmf = bmfCreate("fonts", 8); for (i = 0; i < 3; i++) { pixa = pixaCreate(3); scorefract = 0.1 * i; pixOtsuAdaptiveThreshold(pixg, 2000, 2000, 0, 0, scorefract, NULL, &pixb); pixSaveTiledOutline(pixb, pixa, 0.5, 1, 20, 2, 32); pixSplitDistributionFgBg(pixg, scorefract, 1, &thresh, &fgval, &bgval, 1); fprintf(stderr, "thresh = %d, fgval = %d, bgval = %d\n", thresh, fgval, bgval); /* Give gnuplot time to write out the plot */ #ifndef _WIN32 sleep(1); #else Sleep(1000); #endif /* _WIN32 */ pixp = pixRead("/tmp/histplot.png"); pixSaveTiled(pixp, pixa, 1.0, 0, 20, 1); pixt1 = pixaDisplay(pixa, 0, 0); snprintf(textstr, sizeof(textstr), "Scorefract = %3.1f ........... Thresh = %d", scorefract, thresh); pixt2 = pixAddSingleTextblock(pixt1, bmf, textstr, 0x00ff0000, L_ADD_BELOW, NULL); pixDisplay(pixt2, 100, 100); snprintf(textstr, sizeof(textstr), "/tmp/otsu.%d.png", i); pixWrite(textstr, pixt2, IFF_PNG); pixDestroy(&pixb); pixDestroy(&pixp); pixDestroy(&pixt1); pixDestroy(&pixt2); pixaDestroy(&pixa); } pixa = pixaCreate(2); for (i = 0; i < 2; i++) { scorefract = 0.1 * i; pixOtsuAdaptiveThreshold(pixg, 300, 300, 0, 0, scorefract, NULL, &pixb); pixb2 = pixAddBlackOrWhiteBorder(pixb, 2, 2, 2, 2, L_GET_BLACK_VAL); snprintf(textstr, sizeof(textstr), "Scorefract = %3.1f", scorefract); pixb3 = pixAddSingleTextblock(pixb2, bmf, textstr, 1, L_ADD_BELOW, NULL); pixSaveTiled(pixb3, pixa, 2, (i + 1) % 1, 20, 32); pixDestroy(&pixb); pixDestroy(&pixb2); } pixb = pixaDisplay(pixa, 0, 0); pixWrite("/tmp/otsu-tiled.jpg", pixb, IFF_PNG); pixDestroy(&pixb); pixaDestroy(&pixa); bmfDestroy(&bmf); pixDestroy(&pixs); pixDestroy(&pixg); return 0; }
main(int argc, char **argv) { char outname[256]; l_int32 loc, display, success; L_BMF *bmf, *bmftop; FILE *fp; PIX *pixs, *pixt, *pixd; PIX *pix1, *pix2, *pix3, *pix4, *pix5, *pix6, *pix7, *pix8; PIXA *pixa; if (regTestSetup(argc, argv, &fp, &display, &success, NULL)) return 1; bmf = bmfCreate("./fonts", 6); bmftop = bmfCreate("./fonts", 10); pixs = pixRead("lucasta-47.jpg"); pix1 = pixScale(pixs, 0.4, 0.4); /* 8 bpp grayscale */ pix2 = pixConvertTo32(pix1); /* 32 bpp rgb */ pix3 = pixThresholdOn8bpp(pix1, 12, 1); /* 8 bpp cmapped */ pix4 = pixThresholdTo4bpp(pix1, 10, 1); /* 4 bpp cmapped */ pix5 = pixThresholdTo4bpp(pix1, 10, 0); /* 4 bpp not cmapped */ pix6 = pixThresholdTo2bpp(pix1, 3, 1); /* 2 bpp cmapped */ pix7 = pixThresholdTo2bpp(pix1, 3, 0); /* 2 bpp not cmapped */ pix8 = pixThresholdToBinary(pix1, 160); /* 1 bpp */ for (loc = 1; loc < 5; loc++) { pixa = pixaCreate(0); AddTextAndSave(pixa, pix1, bmf, textstr[0], loc, 190); AddTextAndSave(pixa, pix2, bmf, textstr[1], loc, 0xff000000); AddTextAndSave(pixa, pix3, bmf, textstr[2], loc, 0x00ff0000); AddTextAndSave(pixa, pix4, bmf, textstr[3], loc, 0x0000ff00); AddTextAndSave(pixa, pix5, bmf, textstr[4], loc, 11); AddTextAndSave(pixa, pix6, bmf, textstr[5], loc, 0xff000000); AddTextAndSave(pixa, pix7, bmf, textstr[6], loc, 2); AddTextAndSave(pixa, pix8, bmf, textstr[7], loc, 1); pixt = pixaDisplay(pixa, 0, 0); pixd = pixAddSingleTextblock(pixt, bmftop, topstr[loc - 1], 0xff00ff00, L_ADD_ABOVE, NULL); snprintf(outname, 240, "/tmp/writetext.%d.png", loc - 1); pixWrite(outname, pixd, IFF_PNG); regTestCheckFile(fp, argv, outname, loc - 1, &success); pixDisplayWithTitle(pixd, 50 * loc, 50, NULL, display); pixDestroy(&pixt); pixDestroy(&pixd); pixaDestroy(&pixa); } pixDestroy(&pixs); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); pixDestroy(&pix5); pixDestroy(&pix6); pixDestroy(&pix7); pixDestroy(&pix8); bmfDestroy(&bmf); bmfDestroy(&bmftop); regTestCleanup(argc, argv, fp, success, NULL); return 0; }
main(int argc, char **argv) { char outname[256], buf[512]; l_int32 loc, i; L_BMF *bmf, *bmftop; PIX *pixs, *pixt, *pixd; PIX *pix1, *pix2, *pix3, *pix4, *pix5, *pix6, *pix7, *pix8; PIXA *pixa; L_REGPARAMS *rp; SARRAY *sa; if (regTestSetup(argc, argv, &rp)) return 1; bmf = bmfCreate("./fonts", 6); bmftop = bmfCreate("./fonts", 10); pixs = pixRead("lucasta-47.jpg"); pix1 = pixScale(pixs, 0.4, 0.4); /* 8 bpp grayscale */ pix2 = pixConvertTo32(pix1); /* 32 bpp rgb */ pix3 = pixThresholdOn8bpp(pix1, 12, 1); /* 8 bpp cmapped */ pix4 = pixThresholdTo4bpp(pix1, 10, 1); /* 4 bpp cmapped */ pix5 = pixThresholdTo4bpp(pix1, 10, 0); /* 4 bpp not cmapped */ pix6 = pixThresholdTo2bpp(pix1, 3, 1); /* 2 bpp cmapped */ pix7 = pixThresholdTo2bpp(pix1, 3, 0); /* 2 bpp not cmapped */ pix8 = pixThresholdToBinary(pix1, 160); /* 1 bpp */ for (loc = 1; loc < 5; loc++) { pixa = pixaCreate(0); AddTextAndSave(pixa, pix1, bmf, textstr[0], loc, 800); AddTextAndSave(pixa, pix2, bmf, textstr[1], loc, 0xff000000); AddTextAndSave(pixa, pix3, bmf, textstr[2], loc, 0x00ff0000); AddTextAndSave(pixa, pix4, bmf, textstr[3], loc, 0x0000ff00); AddTextAndSave(pixa, pix5, bmf, textstr[4], loc, 800); AddTextAndSave(pixa, pix6, bmf, textstr[5], loc, 0xff000000); AddTextAndSave(pixa, pix7, bmf, textstr[6], loc, 800); AddTextAndSave(pixa, pix8, bmf, textstr[7], loc, 800); pixt = pixaDisplay(pixa, 0, 0); pixd = pixAddSingleTextblock(pixt, bmftop, topstr[loc - 1], 0xff00ff00, L_ADD_ABOVE, NULL); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 0 - 4 */ pixDisplayWithTitle(pixd, 50 * loc, 50, NULL, rp->display); pixDestroy(&pixt); pixDestroy(&pixd); pixaDestroy(&pixa); } pixDestroy(&pixs); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); pixDestroy(&pix5); pixDestroy(&pix6); pixDestroy(&pix7); pixDestroy(&pix8); bmfDestroy(&bmf); bmfDestroy(&bmftop); /* Write multiple lines in different colors, filling up * the colormap and requesting even more colors. */ pixs = pixRead("weasel4.11c.png"); pix1 = pixConvertTo8(pixs, 0); pix2 = pixScale(pixs, 8.0, 8.0); pix3 = pixQuantFromCmap(pix2, pixGetColormap(pixs), 4, 5, L_EUCLIDEAN_DISTANCE); regTestWritePixAndCheck(rp, pix3, IFF_PNG); /* 5 */ pixDisplayWithTitle(pix3, 0, 500, NULL, rp->display); bmf = bmfCreate("fonts", 10); sa = sarrayCreate(6); for (i = 0; i < 6; i++) { snprintf(buf, sizeof(buf), "This is textline %d\n", i); sarrayAddString(sa, buf, L_COPY); } for (i = 0; i < 6; i++) { pixSetTextline(pix3, bmf, sarrayGetString(sa, i, L_NOCOPY), colors[i], 50, 120 + 60 * i, NULL, NULL); } regTestWritePixAndCheck(rp, pix3, IFF_PNG); /* 6 */ pixDisplayWithTitle(pix3, 600, 500, NULL, rp->display); pixDestroy(&pixs); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); bmfDestroy(&bmf); sarrayDestroy(&sa); return regTestCleanup(rp); }
int main(int argc, char **argv) { char buf[32]; char *filein, *fileout, *fontdir, *textstr; l_int32 n, i, maxdepth, ntext, border, lossless, display, showtext; l_float32 scalefact; L_BMF *bmf; PIX *pix1, *pix2, *pix3, *pix4, *pixd; PIXA *pixa, *pixad; static char mainName[] = "displaypixa"; if (argc != 3 && argc != 4 && argc != 7 && argc != 8) { fprintf(stderr, "Syntax error in displaypixa:\n" " displaypixa filein fileout [showtext]\n" " displaypixa filein scalefact border" " lossless disp fileout [showtext]\n"); return 1; } filein = argv[1]; if ((pixa = pixaRead(filein)) == NULL) return ERROR_INT("pixa not made", mainName, 1); pixaCountText(pixa, &ntext); if (argc == 3 || argc == 4) fileout = argv[2]; if (argc == 4) showtext = atoi(argv[3]); /* Simple specification; no output text */ if (argc == 3 || (argc == 4 && (ntext == 0 || showtext == 0))) { /* no text output */ pixaVerifyDepth(pixa, &maxdepth); pixd = pixaDisplayTiledInRows(pixa, maxdepth, 1400, 1.0, 0, 10, 0); pixDisplay(pixd, 100, 100); if (pixGetDepth(pixd) == 1) pixWrite(fileout, pixd, IFF_PNG); else pixWrite(fileout, pixd, IFF_JFIF_JPEG); pixDestroy(&pixd); pixaDestroy(&pixa); return 0; } /* Simple specification with output text */ if (argc == 4) { /* showtext == 1 && ntext > 0 */ n = pixaGetCount(pixa); bmf = bmfCreate(NULL, 6); pixad = pixaCreate(n); for (i = 0; i < n; i++) { pix1 = pixaGetPix(pixa, i, L_CLONE); pix2 = pixConvertTo32(pix1); pix3 = pixAddBorderGeneral(pix2, 10, 10, 5, 5, 0xffffff00); textstr = pixGetText(pix1); if (textstr && strlen(textstr) > 0) { snprintf(buf, sizeof(buf), "%s", textstr); pix4 = pixAddSingleTextblock(pix3, bmf, buf, 0xff000000, L_ADD_BELOW, NULL); } else { pix4 = pixClone(pix3); } pixaAddPix(pixad, pix4, L_INSERT); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); } bmfDestroy(&bmf); pixaVerifyDepth(pixad, &maxdepth); pixd = pixaDisplayTiledInRows(pixad, maxdepth, 1400, 1.0, 0, 10, 0); pixDisplay(pixd, 100, 100); if (pixGetDepth(pixd) == 1) pixWrite(fileout, pixd, IFF_PNG); else pixWrite(fileout, pixd, IFF_JFIF_JPEG); pixDestroy(&pixd); pixaDestroy(&pixa); pixaDestroy(&pixad); return 0; } /* Full specification */ scalefact = atof(argv[2]); border = atoi(argv[3]); lossless = atoi(argv[4]); display = atoi(argv[5]); fileout = argv[6]; showtext = (argc == 8) ? atoi(argv[7]) : 0; if (showtext && ntext == 0) L_INFO("No text found in any of the pix\n", mainName); bmf = (showtext && ntext > 0) ? bmfCreate(NULL, 6) : NULL; n = pixaGetCount(pixa); pixad = pixaCreate(n); for (i = 0; i < n; i++) { pix1 = pixaGetPix(pixa, i, L_CLONE); pix2 = pixConvertTo32(pix1); pix3 = pixAddBorderGeneral(pix2, 10, 10, 5, 5, 0xffffff00); textstr = pixGetText(pix1); if (bmf && textstr && strlen(textstr) > 0) { snprintf(buf, sizeof(buf), "%s", textstr); pix4 = pixAddSingleTextblock(pix3, bmf, buf, 0xff000000, L_ADD_BELOW, NULL); } else { pix4 = pixClone(pix3); } pixaAddPix(pixad, pix4, L_INSERT); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); } bmfDestroy(&bmf); pixaVerifyDepth(pixad, &maxdepth); pixd = pixaDisplayTiledInRows(pixad, maxdepth, 1400, scalefact, 0, 10, border); if (display) pixDisplay(pixd, 20, 20); if (pixGetDepth(pixd) == 1 || lossless) pixWrite(fileout, pixd, IFF_PNG); else pixWrite(fileout, pixd, IFF_JFIF_JPEG); pixDestroy(&pixd); pixaDestroy(&pixa); pixaDestroy(&pixad); return 0; }
main(int argc, char **argv) { l_float32 scalefact; L_BMF *bmf, *bmftop; L_KERNEL *kel, *kelx, *kely; PIX *pixs, *pixg, *pixt, *pixd; PIX *pix1, *pix2, *pix3, *pix4, *pix5, *pix6, *pix7, *pix8; PIXA *pixa; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; /* ----------------- Test on 8 bpp grayscale ---------------------*/ pixa = pixaCreate(5); bmf = bmfCreate("./fonts", 6); bmftop = bmfCreate("./fonts", 10); pixs = pixRead("lucasta-47.jpg"); pixg = pixScale(pixs, 0.4, 0.4); /* 8 bpp grayscale */ pix1 = pixConvertTo32(pixg); /* 32 bpp rgb */ AddTextAndSave(pixa, pix1, 1, bmf, textstr[0], L_ADD_BELOW, 0xff000000); pix2 = pixConvertGrayToSubpixelRGB(pixs, 0.4, 0.4, L_SUBPIXEL_ORDER_RGB); AddTextAndSave(pixa, pix2, 0, bmf, textstr[1], L_ADD_BELOW, 0x00ff0000); pix3 = pixConvertGrayToSubpixelRGB(pixs, 0.4, 0.4, L_SUBPIXEL_ORDER_BGR); AddTextAndSave(pixa, pix3, 0, bmf, textstr[2], L_ADD_BELOW, 0x0000ff00); pix4 = pixConvertGrayToSubpixelRGB(pixs, 0.4, 0.4, L_SUBPIXEL_ORDER_VRGB); AddTextAndSave(pixa, pix4, 0, bmf, textstr[3], L_ADD_BELOW, 0x00ff0000); pix5 = pixConvertGrayToSubpixelRGB(pixs, 0.4, 0.4, L_SUBPIXEL_ORDER_VBGR); AddTextAndSave(pixa, pix5, 0, bmf, textstr[4], L_ADD_BELOW, 0x0000ff00); pixt = pixaDisplay(pixa, 0, 0); pixd = pixAddSingleTextblock(pixt, bmftop, "Regression test for subpixel scaling: gray", 0xff00ff00, L_ADD_ABOVE, NULL); regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 0 */ pixDisplayWithTitle(pixd, 50, 50, NULL, rp->display); pixaDestroy(&pixa); pixDestroy(&pixs); pixDestroy(&pixg); pixDestroy(&pixt); pixDestroy(&pixd); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); pixDestroy(&pix5); /* ----------------- Test on 32 bpp rgb ---------------------*/ pixa = pixaCreate(5); pixs = pixRead("fish24.jpg"); pix1 = pixScale(pixs, 0.4, 0.4); AddTextAndSave(pixa, pix1, 1, bmf, textstr[0], L_ADD_BELOW, 0xff000000); pix2 = pixConvertToSubpixelRGB(pixs, 0.4, 0.4, L_SUBPIXEL_ORDER_RGB); AddTextAndSave(pixa, pix2, 0, bmf, textstr[1], L_ADD_BELOW, 0x00ff0000); pix3 = pixConvertToSubpixelRGB(pixs, 0.4, 0.35, L_SUBPIXEL_ORDER_BGR); AddTextAndSave(pixa, pix3, 0, bmf, textstr[2], L_ADD_BELOW, 0x0000ff00); pix4 = pixConvertToSubpixelRGB(pixs, 0.4, 0.45, L_SUBPIXEL_ORDER_VRGB); AddTextAndSave(pixa, pix4, 0, bmf, textstr[3], L_ADD_BELOW, 0x00ff0000); pix5 = pixConvertToSubpixelRGB(pixs, 0.4, 0.4, L_SUBPIXEL_ORDER_VBGR); AddTextAndSave(pixa, pix5, 0, bmf, textstr[4], L_ADD_BELOW, 0x0000ff00); pixt = pixaDisplay(pixa, 0, 0); pixd = pixAddSingleTextblock(pixt, bmftop, "Regression test for subpixel scaling: color", 0xff00ff00, L_ADD_ABOVE, NULL); regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 1 */ pixDisplayWithTitle(pixd, 50, 350, NULL, rp->display); pixaDestroy(&pixa); pixDestroy(&pixs); pixDestroy(&pixt); pixDestroy(&pixd); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); pixDestroy(&pix5); bmfDestroy(&bmf); bmfDestroy(&bmftop); /* --------------- Test on images that are initially 1 bpp ------------*/ /* For these, it is better to apply a lowpass filter before scaling */ /* Normal scaling of 8 bpp grayscale */ scalefact = 800. / 2320.; pixs = pixRead("patent.png"); /* sharp, 300 ppi, 1 bpp image */ pix1 = pixConvertTo8(pixs, FALSE); /* use 8 bpp input */ pix2 = pixScale(pix1, scalefact, scalefact); regTestWritePixAndCheck(rp, pix2, IFF_PNG); /* 2 */ /* Subpixel scaling; bad because there is very little aliasing. */ pix3 = pixConvertToSubpixelRGB(pix1, scalefact, scalefact, L_SUBPIXEL_ORDER_RGB); regTestWritePixAndCheck(rp, pix3, IFF_PNG); /* 3 */ /* Get same (bad) result doing subpixel rendering on RGB input */ pix4 = pixConvertTo32(pixs); pix5 = pixConvertToSubpixelRGB(pix4, scalefact, scalefact, L_SUBPIXEL_ORDER_RGB); regTestComparePix(rp, pix3, pix5); /* 4 */ regTestWritePixAndCheck(rp, pix5, IFF_PNG); /* 5 */ /* Now apply a small lowpass filter before scaling. */ makeGaussianKernelSep(2, 2, 1.0, 1.0, &kelx, &kely); startTimer(); pix6 = pixConvolveSep(pix1, kelx, kely, 8, 1); /* normalized */ fprintf(stderr, "Time sep: %7.3f\n", stopTimer()); regTestWritePixAndCheck(rp, pix6, IFF_PNG); /* 6 */ /* Get same lowpass result with non-separated convolution */ kel = makeGaussianKernel(2, 2, 1.0, 1.0); startTimer(); pix7 = pixConvolve(pix1, kel, 8, 1); /* normalized */ fprintf(stderr, "Time non-sep: %7.3f\n", stopTimer()); regTestComparePix(rp, pix6, pix7); /* 7 */ /* Now do the subpixel scaling on this slightly blurred image */ pix8 = pixConvertToSubpixelRGB(pix6, scalefact, scalefact, L_SUBPIXEL_ORDER_RGB); regTestWritePixAndCheck(rp, pix8, IFF_PNG); /* 8 */ kernelDestroy(&kelx); kernelDestroy(&kely); kernelDestroy(&kel); pixDestroy(&pixs); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); pixDestroy(&pix5); pixDestroy(&pix6); pixDestroy(&pix7); pixDestroy(&pix8); return regTestCleanup(rp); }
main(int argc, char **argv) { char buf[256]; l_int32 w, h, i, j, k, index, op, dir, stretch; l_float32 del, angle, angledeg; BOX *box; L_BMF *bmf; PIX *pixs, *pixt, *pixt2, *pixd; static char mainName[] = "warpertest"; if (argc != 1) exit(ERROR_INT("syntax: warpertest", mainName, 1)); /* -------- Stereoscopic warping --------------*/ #if RUN_WARP pixs = pixRead("german.png"); pixGetDimensions(pixs, &w, &h, NULL); l_jpegSetNoChromaSampling(1); for (i = 0; i < 50; i++) { /* need to test > 2 widths ! */ j = 7 * i; box = boxCreate(0, 0, w - j, h - j); pixt = pixClipRectangle(pixs, box, NULL); pixd = pixWarpStereoscopic(pixt, 15, 22, 8, 30, -20, 1); snprintf(buf, sizeof(buf), "/tmp/junkpixw.%02d.jpg", i); pixWrite(buf, pixd, IFF_JFIF_JPEG); pixDestroy(&pixd); pixDestroy(&pixt); boxDestroy(&box); } pixDestroy(&pixs); pixDisplayMultiple("/tmp/junkpixw*.jpg"); #endif /* -------- Quadratic Vertical Shear --------------*/ #if RUN_QUAD_VERT_SHEAR pixs = pixCreate(501, 501, 32); pixGetDimensions(pixs, &w, &h, NULL); pixSetAll(pixs); pixRenderLineArb(pixs, 0, 30, 500, 30, 5, 0, 0, 255); pixRenderLineArb(pixs, 0, 110, 500, 110, 5, 0, 255, 0); pixRenderLineArb(pixs, 0, 190, 500, 190, 5, 0, 255, 255); pixRenderLineArb(pixs, 0, 270, 500, 270, 5, 255, 0, 0); pixRenderLineArb(pixs, 0, 360, 500, 360, 5, 255, 0, 255); pixRenderLineArb(pixs, 0, 450, 500, 450, 5, 255, 255, 0); bmf = bmfCreate("./fonts", 6); for (i = 0; i < 50; i++) { j = 3 * i; dir = ((i / 2) & 1) ? L_WARP_TO_RIGHT : L_WARP_TO_LEFT; op = (i & 1) ? L_INTERPOLATED : L_SAMPLED; Box *box = boxCreate(0, 0, w - j, h - j); pixt = pixClipRectangle(pixs, box, NULL); pixt2 = pixQuadraticVShear(pixt, dir, 60, -20, op, L_BRING_IN_WHITE); snprintf(buf, sizeof(buf), "%s, %s", dirstr[dir], opstr[op]); pixd = pixAddSingleTextblock(pixt2, bmf, buf, 0xff000000, L_ADD_BELOW, 0); snprintf(buf, sizeof(buf), "/tmp/junkpixvs.%02d.png", i); pixWrite(buf, pixd, IFF_PNG); pixDestroy(&pixd); pixDestroy(&pixt); pixDestroy(&pixt2); boxDestroy(&box); } pixDestroy(&pixs); bmfDestroy(&bmf); pixDisplayMultiple("/tmp/junkpixvs*.png"); #endif /* -------- Linear Horizontal stretching --------------*/ #if RUN_LIN_HORIZ_STRETCH pixs = pixRead("german.png"); bmf = bmfCreate("./fonts", 6); for (k = 0; k < 2; k++) { for (i = 0; i < 25; i++) { index = 25 * k + i; stretch = 10 + 4 * i; if (k == 0) stretch = -stretch; dir = (k == 1) ? L_WARP_TO_RIGHT : L_WARP_TO_LEFT; op = (i & 1) ? L_INTERPOLATED : L_SAMPLED; pixt = pixStretchHorizontal(pixs, dir, L_LINEAR_WARP, stretch, op, L_BRING_IN_WHITE); snprintf(buf, sizeof(buf), "%s, %s", dirstr[dir], opstr[op]); pixd = pixAddSingleTextblock(pixt, bmf, buf, 0xff000000, L_ADD_BELOW, 0); snprintf(buf, sizeof(buf), "/tmp/junkpixlhs.%02d.jpg", index); pixWrite(buf, pixd, IFF_JFIF_JPEG); pixDestroy(&pixd); pixDestroy(&pixt); } } pixDestroy(&pixs); bmfDestroy(&bmf); pixDisplayMultiple("/tmp/junkpixlhs*.jpg"); #endif /* -------- Quadratic Horizontal stretching --------------*/ #if RUN_QUAD_HORIZ_STRETCH pixs = pixRead("german.png"); bmf = bmfCreate("./fonts", 6); for (k = 0; k < 2; k++) { for (i = 0; i < 25; i++) { index = 25 * k + i; stretch = 10 + 4 * i; if (k == 0) stretch = -stretch; dir = (k == 1) ? L_WARP_TO_RIGHT : L_WARP_TO_LEFT; op = (i & 1) ? L_INTERPOLATED : L_SAMPLED; pixt = pixStretchHorizontal(pixs, dir, L_QUADRATIC_WARP, stretch, op, L_BRING_IN_WHITE); snprintf(buf, sizeof(buf), "%s, %s", dirstr[dir], opstr[op]); pixd = pixAddSingleTextblock(pixt, bmf, buf, 0xff000000, L_ADD_BELOW, 0); snprintf(buf, sizeof(buf), "/tmp/junkpixqhs.%02d.jpg", index); pixWrite(buf, pixd, IFF_JFIF_JPEG); pixDestroy(&pixd); pixDestroy(&pixt); } } pixDestroy(&pixs); bmfDestroy(&bmf); pixDisplayMultiple("/tmp/junkpixqhs*.jpg"); #endif /* -------- Horizontal Shear --------------*/ #if RUN_HORIZ_SHEAR pixs = pixRead("german.png"); pixGetDimensions(pixs, &w, &h, NULL); bmf = bmfCreate("./fonts", 6); for (i = 0; i < 25; i++) { del = 0.2 / 12.; angle = -0.2 + (i - (i & 1)) * del; angledeg = 180. * angle / 3.14159265; op = (i & 1) ? L_INTERPOLATED : L_SAMPLED; if (op == L_SAMPLED) pixt = pixHShear(NULL, pixs, h / 2, angle, L_BRING_IN_WHITE); else pixt = pixHShearLI(pixs, h / 2, angle, L_BRING_IN_WHITE); snprintf(buf, sizeof(buf), "%6.2f degree, %s", angledeg, opstr[op]); pixd = pixAddSingleTextblock(pixt, bmf, buf, 0xff000000, L_ADD_BELOW, 0); snprintf(buf, sizeof(buf), "/tmp/junkpixsh.%02d.jpg", i); pixWrite(buf, pixd, IFF_JFIF_JPEG); pixDestroy(&pixd); pixDestroy(&pixt); } pixDestroy(&pixs); bmfDestroy(&bmf); pixDisplayMultiple("/tmp/junkpixsh*.jpg"); #endif /* -------- Vertical Shear --------------*/ #if RUN_VERT_SHEAR pixs = pixRead("german.png"); pixGetDimensions(pixs, &w, &h, NULL); bmf = bmfCreate("./fonts", 6); for (i = 0; i < 25; i++) { del = 0.2 / 12.; angle = -0.2 + (i - (i & 1)) * del; angledeg = 180. * angle / 3.14159265; op = (i & 1) ? L_INTERPOLATED : L_SAMPLED; if (op == L_SAMPLED) pixt = pixVShear(NULL, pixs, w / 2, angle, L_BRING_IN_WHITE); else pixt = pixVShearLI(pixs, w / 2, angle, L_BRING_IN_WHITE); snprintf(buf, sizeof(buf), "%6.2f degree, %s", angledeg, opstr[op]); pixd = pixAddSingleTextblock(pixt, bmf, buf, 0xff000000, L_ADD_BELOW, 0); snprintf(buf, sizeof(buf), "/tmp/junkpixsv.%02d.jpg", i); pixWrite(buf, pixd, IFF_JFIF_JPEG); pixDestroy(&pixd); pixDestroy(&pixt); } pixDestroy(&pixs); bmfDestroy(&bmf); pixDisplayMultiple("/tmp/junkpixsv*.jpg"); #endif return 0; }