/* * pixWriteSegmentedPageToPS() * * Input: pixs (all depths; colormap ok) * pixm (<optional> 1 bpp segmentation mask over image region) * textscale (scale of text output relative to pixs) * imagescale (scale of image output relative to pixs) * threshold (threshold for binarization; typ. 190) * pageno (page number in set; use 1 for new output file) * fileout (output ps file) * Return: 0 if OK, 1 on error * * Notes: * (1) This generates the PS string for a mixed text/image page, * and adds it to an existing file if @pageno > 1. * The PS output is determined by fitting the result to * a letter-size (8.5 x 11 inch) page. * (2) The two images (pixs and pixm) are at the same resolution * (typically 300 ppi). They are used to generate two compressed * images, pixb and pixc, that are put directly into the output * PS file. * (3) pixb is the text component. In the PostScript world, we think of * it as a mask through which we paint black. It is produced by * scaling pixs by @textscale, and thresholding to 1 bpp. * (4) pixc is the image component, which is that part of pixs under * the mask pixm. It is scaled from pixs by @imagescale. * (5) Typical values are textscale = 2.0 and imagescale = 0.5. * (6) If pixm == NULL, the page has only text. If it is all black, * the page is all image and has no text. * (7) This can be used to write a multi-page PS file, by using * sequential page numbers with the same output file. It can * also be used to write separate PS files for each page, * by using different output files with @pageno = 0 or 1. */ l_int32 pixWriteSegmentedPageToPS(PIX *pixs, PIX *pixm, l_float32 textscale, l_float32 imagescale, l_int32 threshold, l_int32 pageno, const char *fileout) { l_int32 alltext, notext, d, ret; l_uint32 val; l_float32 scaleratio; PIX *pixmi, *pixmis, *pixt, *pixg, *pixsc, *pixb, *pixc; PROCNAME("pixWriteSegmentedPageToPS"); if (!pixs) return ERROR_INT("pixs not defined", procName, 1); if (!fileout) return ERROR_INT("fileout not defined", procName, 1); if (imagescale <= 0.0 || textscale <= 0.0) return ERROR_INT("relative scales must be > 0.0", procName, 1); /* Analyze the page. Determine the ratio by which the * binary text mask is scaled relative to the image part. * If there is no image region (alltext == TRUE), the * text mask will be rendered directly to fit the page, * and scaleratio = 1.0. */ alltext = TRUE; notext = FALSE; scaleratio = 1.0; if (pixm) { pixZero(pixm, &alltext); /* pixm empty: all text */ if (alltext) pixm = NULL; /* treat it as not existing here */ else { pixmi = pixInvert(NULL, pixm); pixZero(pixmi, ¬ext); /* pixm full; no text */ pixDestroy(&pixmi); scaleratio = textscale / imagescale; } } if (pixGetDepth(pixs) == 1) { /* render tiff g4 */ pixb = pixClone(pixs); pixc = NULL; } else { pixt = pixConvertTo8Or32(pixs, 0, 0); /* this can be a clone of pixs */ /* Get the binary text mask. Note that pixg cannot be a * clone of pixs, because it may be altered by pixSetMasked(). */ pixb = NULL; if (notext == FALSE) { d = pixGetDepth(pixt); if (d == 8) pixg = pixCopy(NULL, pixt); else /* d == 32 */ pixg = pixConvertRGBToLuminance(pixt); if (pixm) /* clear out the image parts */ pixSetMasked(pixg, pixm, 255); if (textscale == 1.0) pixsc = pixClone(pixg); else if (textscale >= 0.7) pixsc = pixScaleGrayLI(pixg, textscale, textscale); else pixsc = pixScaleAreaMap(pixg, textscale, textscale); pixb = pixThresholdToBinary(pixsc, threshold); pixDestroy(&pixg); pixDestroy(&pixsc); } /* Get the scaled image region */ pixc = NULL; if (pixm) { if (imagescale == 1.0) pixsc = pixClone(pixt); /* can possibly be a clone of pixs */ else pixsc = pixScale(pixt, imagescale, imagescale); /* If pixm is not full, clear the pixels in pixsc * corresponding to bg in pixm, where there can be text * that is written through the mask pixb. Note that * we could skip this and use pixsc directly in * pixWriteMixedToPS(); however, clearing these * non-image regions to a white background will reduce * the size of pixc (relative to pixsc), and hence * reduce the size of the PS file that is generated. * Use a copy so that we don't accidentally alter pixs. */ if (notext == FALSE) { pixmis = pixScale(pixm, imagescale, imagescale); pixmi = pixInvert(NULL, pixmis); val = (d == 8) ? 0xff : 0xffffff00; pixc = pixCopy(NULL, pixsc); pixSetMasked(pixc, pixmi, val); /* clear non-image part */ pixDestroy(&pixmis); pixDestroy(&pixmi); } else pixc = pixClone(pixsc); pixDestroy(&pixsc); } pixDestroy(&pixt); } ret = pixWriteMixedToPS(pixb, pixc, scaleratio, pageno, fileout); pixDestroy(&pixb); pixDestroy(&pixc); return ret; }
int main(int argc, char **argv) { BOX *box; PIX *pixs, *pixs8, *pixm, *pixt1, *pixt2, *pixd; PIXA *pixa; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; pixa = pixaCreate(0); /* Start with a 32 bpp image and a mask. Use the * same mask for all clip/masked operations. */ pixs = pixRead("test24.jpg"); pixt1 = pixRead("rabi.png"); box = boxCreate(303, 1983, 800, 500); pixm = pixClipRectangle(pixt1, box, NULL); pixInvert(pixm, pixm); boxDestroy(&box); box = boxCreate(100, 100, 800, 500); /* clips on pixs and derivatives */ pixt2 = pixClipRectangle(pixs, box, NULL); regTestWritePixAndCheck(rp, pixt2, IFF_JFIF_JPEG); /* 0 */ pixaAddPix(pixa, pixt2, L_INSERT); pixDestroy(&pixt1); /* Clip 32 bpp RGB */ pixd = pixClipMasked(pixs, pixm, 100, 100, 0x03c08000); regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 1 */ pixaAddPix(pixa, pixd, L_INSERT); /* Clip 8 bpp colormapped */ pixt1 = pixMedianCutQuant(pixs, 0); pixt2 = pixClipRectangle(pixt1, box, NULL); regTestWritePixAndCheck(rp, pixt2, IFF_PNG); /* 2 */ pixaAddPix(pixa, pixt2, L_INSERT); pixd = pixClipMasked(pixt1, pixm, 100, 100, 0x03c08000); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 3 */ pixaAddPix(pixa, pixd, L_INSERT); pixDestroy(&pixt1); /* Clip 4 bpp colormapped */ pixt1 = pixOctreeQuantNumColors(pixs, 16, 1); pixt2 = pixClipRectangle(pixt1, box, NULL); regTestWritePixAndCheck(rp, pixt2, IFF_PNG); /* 4 */ pixaAddPix(pixa, pixt2, L_INSERT); pixd = pixClipMasked(pixt1, pixm, 100, 100, 0x03c08000); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 5 */ pixaAddPix(pixa, pixd, L_INSERT); pixDestroy(&pixt1); /* Clip 2 bpp colormapped */ pixt1 = pixMedianCutQuantGeneral(pixs, 0, 2, 4, 5, 1, 1); pixt2 = pixClipRectangle(pixt1, box, NULL); regTestWritePixAndCheck(rp, pixt2, IFF_PNG); /* 6 */ pixaAddPix(pixa, pixt2, L_INSERT); pixd = pixClipMasked(pixt1, pixm, 100, 100, 0x03608000); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 7 */ pixaAddPix(pixa, pixd, L_INSERT); pixDestroy(&pixt1); /* Clip 8 bpp gray */ pixs8 = pixConvertRGBToLuminance(pixs); pixt2 = pixClipRectangle(pixs8, box, NULL); regTestWritePixAndCheck(rp, pixt2, IFF_JFIF_JPEG); /* 8 */ pixaAddPix(pixa, pixt2, L_INSERT); pixd = pixClipMasked(pixs8, pixm, 100, 100, 90); regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 9 */ pixaAddPix(pixa, pixd, L_INSERT); /* Clip 4 bpp gray */ pixt1 = pixThresholdTo4bpp(pixs8, 16, 0); pixt2 = pixClipRectangle(pixt1, box, NULL); regTestWritePixAndCheck(rp, pixt2, IFF_PNG); /* 10 */ pixaAddPix(pixa, pixt2, L_INSERT); pixd = pixClipMasked(pixt1, pixm, 100, 100, 0); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 11 */ pixaAddPix(pixa, pixd, L_INSERT); pixd = pixClipMasked(pixt1, pixm, 100, 100, 5); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 12 */ pixaAddPix(pixa, pixd, L_INSERT); pixd = pixClipMasked(pixt1, pixm, 100, 100, 15); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 13 */ pixaAddPix(pixa, pixd, L_INSERT); pixDestroy(&pixt1); /* Clip 4 bpp gray, colormapped */ pixt1 = pixThresholdTo4bpp(pixs8, 16, 1); pixt2 = pixClipRectangle(pixt1, box, NULL); regTestWritePixAndCheck(rp, pixt2, IFF_PNG); /* 14 */ pixaAddPix(pixa, pixt2, L_INSERT); pixd = pixClipMasked(pixt1, pixm, 100, 100, 0x55555500); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 15 */ pixaAddPix(pixa, pixd, L_INSERT); pixDestroy(&pixt1); /* Clip 2 bpp gray */ pixt1 = pixThresholdTo2bpp(pixs8, 4, 0); pixt2 = pixClipRectangle(pixt1, box, NULL); regTestWritePixAndCheck(rp, pixt2, IFF_PNG); /* 16 */ pixaAddPix(pixa, pixt2, L_INSERT); pixd = pixClipMasked(pixt1, pixm, 100, 100, 1); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 17 */ pixaAddPix(pixa, pixd, L_INSERT); pixDestroy(&pixt1); /* Clip 2 bpp gray, colormapped */ pixt1 = pixThresholdTo2bpp(pixs8, 4, 1); pixt2 = pixClipRectangle(pixt1, box, NULL); pixd = pixClipMasked(pixt1, pixm, 100, 100, 0x55555500); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 18 */ pixaAddPix(pixa, pixd, L_INSERT); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixm); pixDestroy(&pixs); pixDestroy(&pixs8); boxDestroy(&box); /* Finally, do the 1 bpp painting through clipped region. * We start with two 1 bpp text sources, use the inverse * of the 2nd for the mask (so we take all of the 1st * pixels under this mask), and for the remainder, which * are the fg pixels in the 2nd, we paint them black (1). * So this is a simple and fast blending of two 1 bpp pix. */ pixs = pixRead("feyn.tif"); box = boxCreate(670, 827, 800, 500); pixt2 = pixClipRectangle(pixs, box, NULL); regTestWritePixAndCheck(rp, pixt2, IFF_PNG); /* 19 */ pixaAddPix(pixa, pixt2, L_INSERT); boxDestroy(&box); pixt1 = pixRead("rabi.png"); box = boxCreate(303, 1983, 800, 500); pixm = pixClipRectangle(pixt1, box, NULL); pixInvert(pixm, pixm); regTestWritePixAndCheck(rp, pixm, IFF_PNG); /* 20 */ pixaAddPix(pixa, pixm, L_INSERT); pixd = pixClipMasked(pixs, pixm, 670, 827, 1); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 21 */ pixaAddPix(pixa, pixd, L_INSERT); pixDestroy(&pixs); pixDestroy(&pixt1); boxDestroy(&box); /* If in testing mode, make a pdf */ if (rp->display) { pixaConvertToPdf(pixa, 100, 1.0, L_FLATE_ENCODE, 0, "Paint through mask", "/tmp/lept/regout/paintmask.pdf"); L_INFO("Output pdf: /tmp/lept/regout/paintmask.pdf\n", rp->testname); } pixaDestroy(&pixa); return regTestCleanup(rp); }
main(int argc, char **argv) { l_int32 d; PIX *pixs, *pixc, *pixr, *pixg, *pixb, *pixsg, *pixsm, *pixd; PIXA *pixa; static char mainName[] = "livre_adapt"; if (argc != 1) exit(ERROR_INT(" Syntax: livre_adapt", mainName, 1)); /* Read the image in at 150 ppi. */ pixDisplayWrite(NULL, -1); if ((pixs = pixRead("brothers.150.jpg")) == NULL) exit(ERROR_INT("pix not made", mainName, 1)); pixDisplayWriteFormat(pixs, 2, IFF_JFIF_JPEG); /* Normalize for uneven illumination on RGB image */ pixBackgroundNormRGBArraysMorph(pixs, NULL, 4, 5, 200, &pixr, &pixg, &pixb); pixd = pixApplyInvBackgroundRGBMap(pixs, pixr, pixg, pixb, 4, 4); pixDisplayWriteFormat(pixd, 2, IFF_JFIF_JPEG); pixDestroy(&pixr); pixDestroy(&pixg); pixDestroy(&pixb); pixDestroy(&pixd); /* Convert the RGB image to grayscale. */ pixsg = pixConvertRGBToLuminance(pixs); pixDisplayWriteFormat(pixsg, 2, IFF_JFIF_JPEG); /* Remove the text in the fg. */ pixc = pixCloseGray(pixsg, 25, 25); pixDisplayWriteFormat(pixc, 2, IFF_JFIF_JPEG); /* Smooth the bg with a convolution. */ pixsm = pixBlockconv(pixc, 15, 15); pixDisplayWriteFormat(pixsm, 2, IFF_JFIF_JPEG); pixDestroy(&pixc); /* Normalize for uneven illumination on gray image. */ pixBackgroundNormGrayArrayMorph(pixsg, NULL, 4, 5, 200, &pixg); pixc = pixApplyInvBackgroundGrayMap(pixsg, pixg, 4, 4); pixDisplayWriteFormat(pixc, 2, IFF_JFIF_JPEG); pixDestroy(&pixg); /* Increase the dynamic range. */ pixd = pixGammaTRC(NULL, pixc, 1.0, 30, 180); pixDisplayWriteFormat(pixd, 2, IFF_JFIF_JPEG); pixDestroy(&pixc); /* Threshold to 1 bpp. */ pixb = pixThresholdToBinary(pixd, 120); pixDisplayWriteFormat(pixb, 2, IFF_PNG); pixDestroy(&pixd); pixDestroy(&pixb); /* Generate the output image */ pixa = pixaReadFiles("/tmp", "junk_write_display"); pixd = pixaDisplayTiledAndScaled(pixa, 8, 350, 4, 0, 25, 2); pixWrite("/tmp/adapt.jpg", pixd, IFF_JFIF_JPEG); pixDisplayWithTitle(pixd, 100, 100, NULL, 1); pixDestroy(&pixd); pixDestroy(&pixs); pixDestroy(&pixsg); return 0; }
/*! * pixConvertToFPix() * * Input: pix (1, 2, 4, 8, 16 or 32 bpp) * ncomps (number of components: 3 for RGB, 1 otherwise) * Return: fpix, or null on error * * Notes: * (1) If colormapped, remove to grayscale. * (2) If 32 bpp and @ncomps == 3, this is RGB; convert to luminance. * In all other cases the src image is treated as having a single * component of pixel values. */ FPIX * pixConvertToFPix(PIX *pixs, l_int32 ncomps) { l_int32 w, h, d, i, j, val, wplt, wpld; l_uint32 uval; l_uint32 *datat, *linet; l_float32 *datad, *lined; PIX *pixt; FPIX *fpixd; PROCNAME("pixConvertToFPix"); if (!pixs) return (FPIX *)ERROR_PTR("pixs not defined", procName, NULL); if (pixGetColormap(pixs)) pixt = pixRemoveColormap(pixs, REMOVE_CMAP_TO_GRAYSCALE); else if (pixGetDepth(pixs) == 32 && ncomps == 3) pixt = pixConvertRGBToLuminance(pixs); else pixt = pixClone(pixs); pixGetDimensions(pixt, &w, &h, &d); if ((fpixd = fpixCreate(w, h)) == NULL) return (FPIX *)ERROR_PTR("fpixd not made", procName, NULL); datat = pixGetData(pixt); wplt = pixGetWpl(pixt); datad = fpixGetData(fpixd); wpld = fpixGetWpl(fpixd); for (i = 0; i < h; i++) { linet = datat + i * wplt; lined = datad + i * wpld; if (d == 1) { for (j = 0; j < w; j++) { val = GET_DATA_BIT(linet, j); lined[j] = (l_float32)val; } } else if (d == 2) { for (j = 0; j < w; j++) { val = GET_DATA_DIBIT(linet, j); lined[j] = (l_float32)val; } } else if (d == 4) { for (j = 0; j < w; j++) { val = GET_DATA_QBIT(linet, j); lined[j] = (l_float32)val; } } else if (d == 8) { for (j = 0; j < w; j++) { val = GET_DATA_BYTE(linet, j); lined[j] = (l_float32)val; } } else if (d == 16) { for (j = 0; j < w; j++) { val = GET_DATA_TWO_BYTES(linet, j); lined[j] = (l_float32)val; } } else if (d == 32) { for (j = 0; j < w; j++) { uval = GET_DATA_FOUR_BYTES(linet, j); lined[j] = (l_float32)uval; } } } pixDestroy(&pixt); return fpixd; }
/*! * convertFilesTo1bpp() * * Input: dirin * substr (<optional> substring filter on filenames; can be NULL) * upscaling (1, 2 or 4; only for input color or grayscale) * thresh (global threshold for binarization; use 0 for default) * firstpage * npages (use 0 to do all from @firstpage to the end) * dirout * outformat (IFF_PNG, IFF_TIFF_G4) * Return: 0 if OK, 1 on error * * Notes: * (1) Images are sorted lexicographically, and the names in the * output directory are retained except for the extension. */ l_int32 convertFilesTo1bpp(const char *dirin, const char *substr, l_int32 upscaling, l_int32 thresh, l_int32 firstpage, l_int32 npages, const char *dirout, l_int32 outformat) { l_int32 i, nfiles; char buf[512]; char *fname, *tail, *basename; PIX *pixs, *pixg1, *pixg2, *pixb; SARRAY *safiles; PROCNAME("convertFilesTo1bpp"); if (!dirin) return ERROR_INT("dirin", procName, 1); if (!dirout) return ERROR_INT("dirout", procName, 1); if (upscaling != 1 && upscaling != 2 && upscaling != 4) return ERROR_INT("invalid upscaling factor", procName, 1); if (thresh <= 0) thresh = 180; if (firstpage < 0) firstpage = 0; if (npages < 0) npages = 0; if (outformat != IFF_TIFF_G4) outformat = IFF_PNG; safiles = getSortedPathnamesInDirectory(dirin, substr, firstpage, npages); if (!safiles) return ERROR_INT("safiles not made", procName, 1); if ((nfiles = sarrayGetCount(safiles)) == 0) { sarrayDestroy(&safiles); return ERROR_INT("no matching files in the directory", procName, 1); } for (i = 0; i < nfiles; i++) { fname = sarrayGetString(safiles, i, L_NOCOPY); if ((pixs = pixRead(fname)) == NULL) { L_WARNING_STRING("Couldn't read file %s\n", procName, fname); continue; } if (pixGetDepth(pixs) == 32) pixg1 = pixConvertRGBToLuminance(pixs); else pixg1 = pixClone(pixs); pixg2 = pixRemoveColormap(pixg1, REMOVE_CMAP_TO_GRAYSCALE); if (pixGetDepth(pixg2) == 1) pixb = pixClone(pixg2); else { if (upscaling == 1) pixb = pixThresholdToBinary(pixg2, thresh); else if (upscaling == 2) pixb = pixScaleGray2xLIThresh(pixg2, thresh); else /* upscaling == 4 */ pixb = pixScaleGray4xLIThresh(pixg2, thresh); } pixDestroy(&pixs); pixDestroy(&pixg1); pixDestroy(&pixg2); splitPathAtDirectory(fname, NULL, &tail); splitPathAtExtension(tail, &basename, NULL); if (outformat == IFF_TIFF_G4) { snprintf(buf, sizeof(buf), "%s/%s.tif", dirout, basename); pixWrite(buf, pixb, IFF_TIFF_G4); } else { snprintf(buf, sizeof(buf), "%s/%s.png", dirout, basename); pixWrite(buf, pixb, IFF_PNG); } pixDestroy(&pixb); FREE(tail); FREE(basename); } sarrayDestroy(&safiles); return 0; }
/*! * pixConvertToDPix() * * Input: pix (1, 2, 4, 8, 16 or 32 bpp) * ncomps (number of components: 3 for RGB, 1 otherwise) * shiftflag (L_NO_SHIFTING or L_WITH_SHIFTING) * Return: dpix, or null on error * * Notes: * (1) If colormapped, remove to grayscale. * (2) If 32 bpp and @ncomps == 3, this is RGB; convert to luminance. * In all other cases the src image is treated as having a single * component of pixel values. * (3) Set @shiftflag to L_WITH_SHIFTING to move the DC of the * the DFT to the center of pix. */ DPIX * pixConvertToDPix(PIX *pixs, l_int32 ncomps, l_int32 shiftflag) { l_int32 w, h, d; l_int32 i, j, val, wplt, wpld; l_uint32 uval; l_uint32 *datat, *linet; l_float64 *datad, *lined; PIX *pixt; DPIX *dpixd; PROCNAME("pixConvertToDPix"); if (!pixs) return (DPIX *)ERROR_PTR("pixs not defined", procName, NULL); if (shiftflag != L_NO_SHIFTING && shiftflag != L_WITH_SHIFTING) return (DPIX *)ERROR_PTR("invalid shiftflag", procName, NULL); if (pixGetColormap(pixs)) pixt = pixRemoveColormap(pixs, REMOVE_CMAP_TO_GRAYSCALE); else if (pixGetDepth(pixs) == 32 && ncomps == 3) pixt = pixConvertRGBToLuminance(pixs); else pixt = pixClone(pixs); pixGetDimensions(pixt, &w, &h, &d); if ((dpixd = dpixCreate(w, h)) == NULL) return (DPIX *)ERROR_PTR("dpixd not made", procName, NULL); datat = pixGetData(pixt); wplt = pixGetWpl(pixt); datad = dpixGetData(dpixd); wpld = dpixGetWpl(dpixd); for (i = 0; i < h; i++) { linet = datat + i * wplt; lined = datad + i * wpld; if (d == 1) { for (j = 0; j < w; j++) { val = GET_DATA_BIT(linet, j); lined[j] = shiftflag ? (l_float64)(val * pow(-1, i + j)) : (l_float64)val; } } else if (d == 2) { for (j = 0; j < w; j++) { val = GET_DATA_DIBIT(linet, j); lined[j] = shiftflag ? (l_float64)(val * pow(-1, i + j)) : (l_float64)val; } } else if (d == 4) { for (j = 0; j < w; j++) { val = GET_DATA_QBIT(linet, j); lined[j] = shiftflag ? (l_float64)(val * pow(-1, i + j)) : (l_float64)val; } } else if (d == 8) { for (j = 0; j < w; j++) { val = GET_DATA_BYTE(linet, j); lined[j] = shiftflag ? (l_float64)(val * pow(-1, i + j)) : (l_float64)val; } } else if (d == 16) { for (j = 0; j < w; j++) { val = GET_DATA_TWO_BYTES(linet, j); lined[j] = shiftflag ? (l_float64)(val * pow(-1, i + j)) : (l_float64)val; } } else if (d == 32) { for (j = 0; j < w; j++) { uval = GET_DATA_FOUR_BYTES(linet, j); lined[j] = shiftflag ? (l_float64)(uval * pow(-1, i + j)) : (l_float64)uval; } } } pixDestroy(&pixt); return dpixd; }
main(int argc, char **argv) { PIX *pixs, *pixg, *pixc, *pixt, *pixd; PIXA *pixa; static char mainName[] = "blend_reg"; pixg = pixRead("blender8.png"); pixt = pixRead("weasel4.11c.png"); pixc = pixRemoveColormap(pixt, REMOVE_CMAP_TO_FULL_COLOR); pixDisplayWrite(NULL, -1); pixa = pixaCreate(0); #if 1 /* Gray blend (straight) */ pixs = pixRead("test24.jpg"); GrayBlend(pixs, pixg, L_BLEND_GRAY, 0.3); pixSaveTiled(pixs, pixa, 1, 1, 20, 32); pixDisplayWithTitle(pixs, 100, 100, NULL, DISPLAY); pixDestroy(&pixs); pixs = pixRead("marge.jpg"); GrayBlend(pixs, pixg, L_BLEND_GRAY, 0.2); pixSaveTiled(pixs, pixa, 1, 1, 20, 32); pixDisplayWithTitle(pixs, 100, 100, NULL, DISPLAY); pixDestroy(&pixs); pixs = pixRead("marge.jpg"); pixt = pixConvertRGBToLuminance(pixs); GrayBlend(pixt, pixg, L_BLEND_GRAY, 0.2); pixSaveTiled(pixt, pixa, 1, 0, 20, 32); pixDisplayWithTitle(pixt, 100, 100, NULL, DISPLAY); pixDestroy(&pixs); pixDestroy(&pixt); #endif #if 1 /* Gray blend (inverse) */ pixs = pixRead("test24.jpg"); GrayBlend(pixs, pixg, L_BLEND_GRAY_WITH_INVERSE, 0.6); pixSaveTiled(pixs, pixa, 1, 1, 20, 32); pixDisplayWithTitle(pixs, 100, 100, NULL, DISPLAY); pixDestroy(&pixs); pixs = pixRead("marge.jpg"); GrayBlend(pixs, pixg, L_BLEND_GRAY_WITH_INVERSE, 0.6); pixSaveTiled(pixs, pixa, 1, 1, 20, 32); pixDisplayWithTitle(pixs, 100, 100, NULL, DISPLAY); pixDestroy(&pixs); pixs = pixRead("marge.jpg"); pixt = pixConvertRGBToLuminance(pixs); GrayBlend(pixt, pixg, L_BLEND_GRAY_WITH_INVERSE, 0.6); pixSaveTiled(pixt, pixa, 1, 0, 20, 32); pixDisplayWithTitle(pixt, 100, 100, NULL, DISPLAY); pixDestroy(&pixs); pixDestroy(&pixt); pixs = MakeGrayWash(1000, 120); GrayBlend(pixs, pixg, L_BLEND_GRAY_WITH_INVERSE, 0.3); pixSaveTiled(pixs, pixa, 1, 1, 20, 32); pixDisplayWithTitle(pixs, 200, 100, NULL, DISPLAY); pixDestroy(&pixs); pixs = MakeColorWash(1000, 120, COLOR_RED); GrayBlend(pixs, pixg, L_BLEND_GRAY_WITH_INVERSE, 1.0); pixSaveTiled(pixs, pixa, 1, 1, 20, 32); pixDisplayWithTitle(pixs, 200, 100, NULL, DISPLAY); pixDestroy(&pixs); #endif #if 1 /* Adaptive gray blend */ pixs = pixRead("test24.jpg"); AdaptiveGrayBlend(pixs, pixg, 0.8); pixSaveTiled(pixs, pixa, 1, 1, 20, 32); pixDisplayWithTitle(pixs, 200, 100, NULL, DISPLAY); pixDestroy(&pixs); pixs = pixRead("marge.jpg"); AdaptiveGrayBlend(pixs, pixg, 0.8); pixSaveTiled(pixs, pixa, 1, 1, 20, 32); pixDisplayWithTitle(pixs, 200, 100, NULL, DISPLAY); pixt = pixConvertRGBToLuminance(pixs); AdaptiveGrayBlend(pixt, pixg, 0.1); pixSaveTiled(pixt, pixa, 1, 0, 20, 32); pixDisplayWithTitle(pixt, 200, 100, NULL, DISPLAY); pixDestroy(&pixs); pixDestroy(&pixt); pixs = MakeGrayWash(1000, 120); AdaptiveGrayBlend(pixs, pixg, 0.3); pixSaveTiled(pixs, pixa, 1, 1, 20, 32); pixDisplayWithTitle(pixs, 200, 100, NULL, DISPLAY); pixDestroy(&pixs); pixs = MakeColorWash(1000, 120, COLOR_RED); AdaptiveGrayBlend(pixs, pixg, 0.5); pixSaveTiled(pixs, pixa, 1, 1, 20, 32); pixDisplayWithTitle(pixs, 200, 100, NULL, DISPLAY); pixDestroy(&pixs); #endif #if 1 /* Color blend */ pixs = pixRead("test24.jpg"); ColorBlend(pixs, pixc, 0.3); pixSaveTiled(pixs, pixa, 1, 1, 20, 32); pixDisplayWithTitle(pixs, 300, 100, NULL, DISPLAY); pixDestroy(&pixs); pixs = pixRead("marge.jpg"); ColorBlend(pixs, pixc, 0.30); pixSaveTiled(pixs, pixa, 1, 1, 20, 32); pixDisplayWithTitle(pixs, 300, 100, NULL, DISPLAY); pixDestroy(&pixs); pixs = pixRead("marge.jpg"); ColorBlend(pixs, pixc, 0.15); pixSaveTiled(pixs, pixa, 1, 0, 20, 32); pixDisplayWithTitle(pixs, 300, 100, NULL, DISPLAY); pixDestroy(&pixs); #endif /* Display results */ pixd = pixaDisplay(pixa, 0, 0); pixDisplay(pixd, 100, 100); pixWrite("/tmp/junkblend.jpg", pixd, IFF_JFIF_JPEG); pixDestroy(&pixd); pixaDestroy(&pixa); pixDisplayMultiple("/tmp/junk_write_display*"); pixDestroy(&pixg); pixDestroy(&pixt); pixDestroy(&pixc); return 0; }
main(int argc, char **argv) { l_int32 i, j, wc, hc, d; L_KERNEL *kel1, *kel2; PIX *pixs, *pixg, *pixacc, *pixd, *pixt; char *filein, *fileout; static char mainName[] = "convolvetest"; if (argc != 5) exit(ERROR_INT(" Syntax: convolvetest filein wc hc fileout", mainName, 1)); filein = argv[1]; wc = atoi(argv[2]); hc = atoi(argv[3]); fileout = argv[4]; if ((pixs = pixRead(filein)) == NULL) exit(ERROR_INT("pix not made", mainName, 1)); #if 0 /* Measure speed */ pixacc = pixBlockconvAccum(pixs); for (i = 0; i < NTIMES; i++) { pixd = pixBlockconvGray(pixs, pixacc, wc, hc); if ((i+1) % 10 == 0) fprintf(stderr, "%d iters\n", i + 1); pixDestroy(&pixd); } pixd = pixBlockconvGray(pixs, pixacc, wc, hc); pixWrite(fileout, pixd, IFF_JFIF_JPEG); pixDestroy(&pixacc); #endif #if 0 /* Test pixBlockconvGray() */ pixacc = pixBlockconvAccum(pixs); pixd = pixBlockconvGray(pixs, pixacc, wc, hc); pixWrite(fileout, pixd, IFF_JFIF_JPEG); pixDestroy(&pixacc); #endif #if 0 /* Test pixBlockconv() */ pixd = pixBlockconv(pixs, wc, hc); pixWrite(fileout, pixd, IFF_JFIF_JPEG); #endif #if 0 /* Test pixBlockrank() */ pixacc = pixBlockconvAccum(pixs); pixd = pixBlockrank(pixs, pixacc, wc, hc, 0.5); pixWrite(fileout, pixd, IFF_TIFF_G4); pixDestroy(&pixacc); #endif #if 0 /* Test pixBlocksum() */ pixacc = pixBlockconvAccum(pixs); pixd = pixBlocksum(pixs, pixacc, wc, hc); pixInvert(pixd, pixd); pixWrite(fileout, pixd, IFF_JFIF_JPEG); pixDestroy(&pixacc); #endif #if 0 /* Test pixCensusTransform() */ d = pixGetDepth(pixs); if (d == 32) pixt = pixConvertRGBToLuminance(pixs); else pixt = pixClone(pixs); pixacc = pixBlockconvAccum(pixt); pixd = pixCensusTransform(pixt, wc, NULL); pixDestroy(&pixt); pixDestroy(&pixacc); pixWrite(fileout, pixd, IFF_PNG); #endif #if 1 /* Test generic convolution with kel1 */ if (pixGetDepth(pixs) == 32) pixg = pixScaleRGBToGrayFast(pixs, 2, COLOR_GREEN); else pixg = pixScale(pixs, 0.5, 0.5); pixDisplay(pixg, 0, 600); kel1 = kernelCreateFromString(5, 5, 2, 2, kdatastr); pixd = pixConvolve(pixg, kel1, 8, 1); pixDisplay(pixd, 700, 0); pixWrite("/tmp/junkpixd4.bmp", pixd, IFF_BMP); pixDestroy(&pixd); kernelDestroy(&kel1); /* Test convolution with flat rectangular kel */ kel2 = kernelCreate(11, 11); kernelSetOrigin(kel2, 5, 5); for (i = 0; i < 11; i++) { for (j = 0; j < 11; j++) kernelSetElement(kel2, i, j, 1); } startTimer(); pixd = pixConvolve(pixg, kel2, 8, 1); fprintf(stderr, "Generic convolution: %7.3f sec\n", stopTimer()); pixDisplay(pixd, 1200, 0); pixWrite("/tmp/junkpixd5.bmp", pixd, IFF_BMP); startTimer(); pixt = pixBlockconv(pixg, 5, 5); fprintf(stderr, "Block convolution: %7.3f sec\n", stopTimer()); pixDisplay(pixd, 1200, 600); pixWrite("/tmp/junkpixd6.bmp", pixt, IFF_BMP); pixCompareGray(pixd, pixt, L_COMPARE_ABS_DIFF, GPLOT_X11, NULL, NULL, NULL, NULL); pixDestroy(&pixg); pixDestroy(&pixt); kernelDestroy(&kel2); #endif pixDestroy(&pixs); pixDestroy(&pixd); return 0; }
int main(int argc, char **argv) { l_int32 i, j, wc, hc, d, bias; L_KERNEL *kel1, *kel2, *kel3x, *kel3y; PIX *pix, *pixs, *pixg, *pixacc, *pixd, *pixt; char *filein, *fileout; static char mainName[] = "convolvetest"; if (argc != 5) return ERROR_INT(" Syntax: convolvetest filein wc hc fileout", mainName, 1); filein = argv[1]; wc = atoi(argv[2]); hc = atoi(argv[3]); fileout = argv[4]; if ((pix = pixRead(filein)) == NULL) return ERROR_INT("pix not made", mainName, 1); d = pixGetDepth(pix); if (d != 1 && d != 8 && d != 32) pixs = pixConvertTo8(pix, 0); else pixs = pixClone(pix); pixDestroy(&pix); d = pixGetDepth(pixs); if (d == 8 && (ALL || 0)) { /* Measure speed */ pixacc = pixBlockconvAccum(pixs); for (i = 0; i < NTIMES; i++) { pixd = pixBlockconvGray(pixs, pixacc, wc, hc); if ((i+1) % 10 == 0) fprintf(stderr, "%d iters\n", i + 1); pixDestroy(&pixd); } pixd = pixBlockconvGray(pixs, pixacc, wc, hc); pixWrite(fileout, pixd, IFF_JFIF_JPEG); pixDestroy(&pixacc); } if (d == 8 && (ALL || 0)) { /* Test pixBlockconvGray() */ pixacc = pixBlockconvAccum(pixs); pixd = pixBlockconvGray(pixs, pixacc, wc, hc); pixWrite(fileout, pixd, IFF_JFIF_JPEG); pixDestroy(&pixacc); } if (ALL || 0) { /* Test pixBlockconv() */ pixd = pixBlockconv(pixs, wc, hc); pixWrite(fileout, pixd, IFF_JFIF_JPEG); } if (d == 1 && (ALL || 0)) { /* Test pixBlockrank() */ pixacc = pixBlockconvAccum(pixs); pixd = pixBlockrank(pixs, pixacc, wc, hc, 0.5); pixWrite(fileout, pixd, IFF_TIFF_G4); pixDestroy(&pixacc); } if (d == 1 && (ALL || 0)) { /* Test pixBlocksum() */ pixacc = pixBlockconvAccum(pixs); pixd = pixBlocksum(pixs, pixacc, wc, hc); pixInvert(pixd, pixd); pixWrite(fileout, pixd, IFF_JFIF_JPEG); pixDestroy(&pixacc); } if (ALL || 0) { /* Test pixCensusTransform() */ d = pixGetDepth(pixs); if (d == 32) pixt = pixConvertRGBToLuminance(pixs); else pixt = pixClone(pixs); pixacc = pixBlockconvAccum(pixt); pixd = pixCensusTransform(pixt, wc, NULL); pixDestroy(&pixt); pixDestroy(&pixacc); pixWrite(fileout, pixd, IFF_PNG); } if (ALL || 0) { /* Test generic convolution with kel1 */ lept_mkdir("lept"); if (pixGetDepth(pixs) == 32) pixg = pixScaleRGBToGrayFast(pixs, 2, COLOR_GREEN); else pixg = pixScale(pixs, 0.5, 0.5); pixDisplay(pixg, 0, 600); kel1 = kernelCreateFromString(5, 5, 2, 2, kel1str); pixd = pixConvolve(pixg, kel1, 8, 1); pixDisplay(pixd, 700, 0); pixWrite("/tmp/lept/convol_d4.bmp", pixd, IFF_BMP); pixDestroy(&pixd); kernelDestroy(&kel1); /* Test convolution with flat rectangular kel */ kel2 = kernelCreate(11, 11); kernelSetOrigin(kel2, 5, 5); for (i = 0; i < 11; i++) { for (j = 0; j < 11; j++) kernelSetElement(kel2, i, j, 1); } startTimer(); pixd = pixConvolve(pixg, kel2, 8, 1); fprintf(stderr, "Generic convolution: %7.3f sec\n", stopTimer()); pixDisplay(pixd, 1200, 0); pixWrite("/tmp/lept/convol_d5.bmp", pixd, IFF_BMP); startTimer(); pixt = pixBlockconv(pixg, 5, 5); fprintf(stderr, "Block convolution: %7.3f sec\n", stopTimer()); pixDisplay(pixd, 1200, 600); pixWrite("/tmp/lept/convol_d6.bmp", pixt, IFF_BMP); pixCompareGray(pixd, pixt, L_COMPARE_ABS_DIFF, GPLOT_X11, NULL, NULL, NULL, NULL); pixDestroy(&pixg); pixDestroy(&pixt); kernelDestroy(&kel2); } if (ALL || 0) { /* Test bias convolution with kel2 */ if (pixGetDepth(pixs) == 32) pixg = pixScaleRGBToGrayFast(pixs, 2, COLOR_GREEN); else pixg = pixScale(pixs, 0.5, 0.5); pixDisplay(pixg, 0, 600); kel2 = kernelCreateFromString(5, 5, 2, 2, kel2str); pixd = pixConvolveWithBias(pixg, kel2, NULL, TRUE, &bias); pixDisplay(pixd, 700, 0); fprintf(stderr, "bias = %d\n", bias); pixWrite("/tmp/lept/convol_d6.png", pixd, IFF_PNG); pixDestroy(&pixg); kernelDestroy(&kel2); pixDestroy(&pixd); } if (ALL || 1) { /* Test separable bias convolution with kel3x, kel3y */ if (pixGetDepth(pixs) == 32) pixg = pixScaleRGBToGrayFast(pixs, 2, COLOR_GREEN); else pixg = pixScale(pixs, 0.5, 0.5); pixDisplay(pixg, 0, 600); kel3x = kernelCreateFromString(1, 5, 0, 2, kel3xstr); kel3y = kernelCreateFromString(7, 1, 3, 0, kel3ystr); pixd = pixConvolveWithBias(pixg, kel3x, kel3y, TRUE, &bias); pixDisplay(pixd, 700, 0); fprintf(stderr, "bias = %d\n", bias); pixWrite("/tmp/lept/convol_d7.png", pixd, IFF_PNG); pixDestroy(&pixg); kernelDestroy(&kel3x); kernelDestroy(&kel3y); pixDestroy(&pixd); } pixDestroy(&pixs); return 0; }
int main(int argc, char **argv) { PIX *pixs, *pixg, *pixc, *pix1, *pix2; PIXA *pixa; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; /* Set up blenders */ pixg = pixRead("blender8.png"); pix1 = pixRead("weasel4.11c.png"); pixc = pixRemoveColormap(pix1, REMOVE_CMAP_TO_FULL_COLOR); pixDestroy(&pix1); pixa = pixaCreate(0); /* Gray blend (straight) */ pixs = pixRead("test24.jpg"); pix1 = pixScale(pixs, 0.4, 0.4); GrayBlend(pix1, pixg, L_BLEND_GRAY, 0.3); regTestWritePixAndCheck(rp, pix1, IFF_JFIF_JPEG); /* 0 */ pixaAddPix(pixa, pix1, L_INSERT); pixDisplayWithTitle(pix1, 0, 100, NULL, rp->display); pixDestroy(&pixs); pixs = pixRead("marge.jpg"); GrayBlend(pixs, pixg, L_BLEND_GRAY, 0.2); regTestWritePixAndCheck(rp, pixs, IFF_JFIF_JPEG); /* 1 */ pixaAddPix(pixa, pixs, L_INSERT); pixDisplayWithTitle(pixs, 100, 100, NULL, rp->display); pixs = pixRead("marge.jpg"); pix1 = pixConvertRGBToLuminance(pixs); GrayBlend(pix1, pixg, L_BLEND_GRAY, 0.2); regTestWritePixAndCheck(rp, pix1, IFF_JFIF_JPEG); /* 2 */ pixaAddPix(pixa, pix1, L_INSERT); pixDisplayWithTitle(pix1, 200, 100, NULL, rp->display); pixDestroy(&pixs); /* Gray blend (inverse) */ pixs = pixRead("test24.jpg"); pix1 = pixScale(pixs, 0.4, 0.4); GrayBlend(pix1, pixg, L_BLEND_GRAY_WITH_INVERSE, 0.6); regTestWritePixAndCheck(rp, pix1, IFF_JFIF_JPEG); /* 3 */ pixaAddPix(pixa, pix1, L_INSERT); pixDisplayWithTitle(pix1, 300, 100, NULL, rp->display); pixDestroy(&pixs); pixs = pixRead("marge.jpg"); GrayBlend(pixs, pixg, L_BLEND_GRAY_WITH_INVERSE, 0.6); regTestWritePixAndCheck(rp, pixs, IFF_JFIF_JPEG); /* 4 */ pixaAddPix(pixa, pixs, L_INSERT); pixDisplayWithTitle(pixs, 400, 100, NULL, rp->display); pixs = pixRead("marge.jpg"); pix1 = pixConvertRGBToLuminance(pixs); GrayBlend(pix1, pixg, L_BLEND_GRAY_WITH_INVERSE, 0.6); regTestWritePixAndCheck(rp, pix1, IFF_JFIF_JPEG); /* 5 */ pixaAddPix(pixa, pix1, L_INSERT); pixDisplayWithTitle(pix1, 500, 100, NULL, rp->display); pixDestroy(&pixs); pixs = MakeGrayWash(1000, 120); GrayBlend(pixs, pixg, L_BLEND_GRAY_WITH_INVERSE, 0.3); regTestWritePixAndCheck(rp, pixs, IFF_PNG); /* 6 */ pixaAddPix(pixa, pixs, L_INSERT); pixDisplayWithTitle(pixs, 0, 600, NULL, rp->display); pixs = MakeColorWash(1000, 120, COLOR_RED); GrayBlend(pixs, pixg, L_BLEND_GRAY_WITH_INVERSE, 1.0); regTestWritePixAndCheck(rp, pixs, IFF_PNG); /* 7 */ pixaAddPix(pixa, pixs, L_INSERT); pixDisplayWithTitle(pixs, 0, 750, NULL, rp->display); /* Adaptive gray blend */ pixs = pixRead("test24.jpg"); pix1 = pixScale(pixs, 0.4, 0.4); AdaptiveGrayBlend(pix1, pixg, 0.8); regTestWritePixAndCheck(rp, pix1, IFF_JFIF_JPEG); /* 8 */ pixaAddPix(pixa, pix1, L_INSERT); pixDisplayWithTitle(pix1, 600, 100, NULL, rp->display); pixDestroy(&pixs); pixs = pixRead("marge.jpg"); AdaptiveGrayBlend(pixs, pixg, 0.8); regTestWritePixAndCheck(rp, pixs, IFF_JFIF_JPEG); /* 9 */ pixaAddPix(pixa, pixs, L_INSERT); pixDisplayWithTitle(pixs, 700, 100, NULL, rp->display); pix1 = pixConvertRGBToLuminance(pixs); AdaptiveGrayBlend(pix1, pixg, 0.1); regTestWritePixAndCheck(rp, pix1, IFF_JFIF_JPEG); /* 10 */ pixaAddPix(pixa, pix1, L_INSERT); pixDisplayWithTitle(pix1, 800, 100, NULL, rp->display); pixs = MakeGrayWash(1000, 120); AdaptiveGrayBlend(pixs, pixg, 0.3); regTestWritePixAndCheck(rp, pixs, IFF_PNG); /* 11 */ pixaAddPix(pixa, pixs, L_INSERT); pixDisplayWithTitle(pixs, 0, 900, NULL, rp->display); pixs = MakeColorWash(1000, 120, COLOR_RED); AdaptiveGrayBlend(pixs, pixg, 0.5); regTestWritePixAndCheck(rp, pixs, IFF_PNG); /* 12 */ pixaAddPix(pixa, pixs, L_INSERT); pixDisplayWithTitle(pixs, 0, 1050, NULL, rp->display); /* Color blend */ pixs = pixRead("test24.jpg"); pix1 = pixScale(pixs, 0.4, 0.4); ColorBlend(pix1, pixc, 0.3); regTestWritePixAndCheck(rp, pix1, IFF_JFIF_JPEG); /* 13 */ pixaAddPix(pixa, pix1, L_INSERT); pixDisplayWithTitle(pix1, 900, 100, NULL, rp->display); pixDestroy(&pixs); pixs = pixRead("marge.jpg"); ColorBlend(pixs, pixc, 0.30); regTestWritePixAndCheck(rp, pixs, IFF_JFIF_JPEG); /* 14 */ pixaAddPix(pixa, pixs, L_INSERT); pixDisplayWithTitle(pixs, 1000, 100, NULL, rp->display); pixs = pixRead("marge.jpg"); ColorBlend(pixs, pixc, 0.15); regTestWritePixAndCheck(rp, pixs, IFF_JFIF_JPEG); /* 15 */ pixaAddPix(pixa, pixs, L_INSERT); pixDisplayWithTitle(pixs, 1100, 100, NULL, rp->display); /* Mosaic all results */ pix1 = pixaDisplayTiledInRows(pixa, 32, 1700, 1.0, 0, 20, 2); regTestWritePixAndCheck(rp, pix1, IFF_JFIF_JPEG); /* 16 */ pixDisplayWithTitle(pix1, 0, 0, NULL, rp->display); pixaDestroy(&pixa); pixDestroy(&pix1); pixDestroy(&pixg); pixDestroy(&pixc); return regTestCleanup(rp); }