Pix Pix::ThresholdTo4bpp(int nlevels, int cmapflag) { if(IsEmpty()) return Pix(); PIX *dPix = pixThresholdTo4bpp(pix, nlevels, cmapflag); if(!dPix) return Pix(); return Pix(&dPix); } // END Pix::ThresholdTo4bpp()
l_int32 main(int argc, char **argv) { l_int32 irval, igval, ibval; l_float32 rval, gval, bval, fract, fgfract; L_BMF *bmf; BOX *box; BOXA *boxa; FPIX *fpix; PIX *pixs, *pix1, *pix2, *pix3, *pix4, *pix5, *pix6, *pix7; PIX *pix8, *pix9, *pix10, *pix11, *pix12, *pix13, *pix14, *pix15; PIXA *pixa; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; pixa = pixaCreate(0); pixs = pixRead("breviar38.150.jpg"); /* pixs = pixRead("breviar32.150.jpg"); */ pixaAddPix(pixa, pixs, L_CLONE); regTestWritePixAndCheck(rp, pixs, IFF_JFIF_JPEG); /* 0 */ pixDisplayWithTitle(pixs, 0, 0, "Input image", rp->display); /* Extract the blue component, which is small in all the text * regions, including in the highlight color region */ pix1 = pixGetRGBComponent(pixs, COLOR_BLUE); pixaAddPix(pixa, pix1, L_CLONE); regTestWritePixAndCheck(rp, pix1, IFF_JFIF_JPEG); /* 1 */ pixDisplayWithTitle(pix1, 200, 0, "Blue component", rp->display); /* Do a background normalization, with the background set to * approximately 200 */ pix2 = pixBackgroundNormSimple(pix1, NULL, NULL); pixaAddPix(pixa, pix2, L_COPY); regTestWritePixAndCheck(rp, pix2, IFF_JFIF_JPEG); /* 2 */ pixDisplayWithTitle(pix2, 400, 0, "BG normalized to 200", rp->display); /* Do a linear transform on the gray pixels, with 50 going to * black and 160 going to white. 50 is sufficiently low to * make both the red and black print quite dark. Quantize * to a few equally spaced gray levels. This is the image * to which highlight color will be applied. */ pixGammaTRC(pix2, pix2, 1.0, 50, 160); pix3 = pixThresholdOn8bpp(pix2, 7, 1); pixaAddPix(pixa, pix3, L_CLONE); regTestWritePixAndCheck(rp, pix3, IFF_JFIF_JPEG); /* 3 */ pixDisplayWithTitle(pix3, 600, 0, "Basic quantized with white bg", rp->display); /* Identify the regions of red text. First, make a mask * consisting of all pixels such that (R-B)/B is larger * than 2.0. This will have all the red, plus a lot of * the dark pixels. */ fpix = pixComponentFunction(pixs, 1.0, 0.0, -1.0, 0.0, 0.0, 1.0); pix4 = fpixThresholdToPix(fpix, 2.0); pixInvert(pix4, pix4); /* red plus some dark text */ pixaAddPix(pixa, pix4, L_CLONE); regTestWritePixAndCheck(rp, pix4, IFF_PNG); /* 4 */ pixDisplayWithTitle(pix4, 800, 0, "Red plus dark pixels", rp->display); /* Make a mask consisting of all the red and background pixels */ pix5 = pixGetRGBComponent(pixs, COLOR_RED); pix6 = pixThresholdToBinary(pix5, 128); pixInvert(pix6, pix6); /* red plus background (white) */ /* Intersect the two masks to get a mask consisting of pixels * that are almost certainly red. This is the seed. */ pix7 = pixAnd(NULL, pix4, pix6); /* red only (seed) */ pixaAddPix(pixa, pix7, L_COPY); regTestWritePixAndCheck(rp, pix7, IFF_PNG); /* 5 */ pixDisplayWithTitle(pix7, 0, 600, "Seed for red color", rp->display); /* Make the clipping mask by thresholding the image with * the background cleaned to white. */ pix8 = pixThresholdToBinary(pix2, 230); /* mask */ pixaAddPix(pixa, pix8, L_CLONE); regTestWritePixAndCheck(rp, pix8, IFF_PNG); /* 6 */ pixDisplayWithTitle(pix8, 200, 600, "Clipping mask for red components", rp->display); /* Fill into the mask from the seed */ pixSeedfillBinary(pix7, pix7, pix8, 8); /* filled: red plus touching */ regTestWritePixAndCheck(rp, pix7, IFF_PNG); /* 7 */ pixDisplayWithTitle(pix7, 400, 600, "Red component mask filled", rp->display); /* Remove long horizontal and vertical lines from the filled result */ pix9 = pixMorphSequence(pix7, "o40.1", 0); pixSubtract(pix7, pix7, pix9); /* remove long horizontal lines */ pixDestroy(&pix9); pix9 = pixMorphSequence(pix7, "o1.40", 0); pixSubtract(pix7, pix7, pix9); /* remove long vertical lines */ /* Close the regions to be colored */ pix10 = pixMorphSequence(pix7, "c5.1", 0); pixaAddPix(pixa, pix10, L_CLONE); regTestWritePixAndCheck(rp, pix10, IFF_PNG); /* 8 */ pixDisplayWithTitle(pix10, 600, 600, "Components defining regions allowing coloring", rp->display); /* Sanity check on amount to be colored. Only accept images * with less than 10% of all the pixels with highlight color */ pixForegroundFraction(pix10, &fgfract); if (fgfract >= 0.10) { L_INFO("too much highlighting: fract = %6.3f; removing it\n", rp->testname, fgfract); pixClearAll(pix10); pixSetPixel(pix10, 0, 0, 1); } /* Get the bounding boxes of the regions to be colored */ boxa = pixConnCompBB(pix10, 8); /* Get a color to paint that is representative of the * actual highlight color in the image. Scale each * color component up from the average by an amount necessary * to saturate the red. Then divide the green and * blue components by 2.0. */ pixGetAverageMaskedRGB(pixs, pix7, 0, 0, 1, L_MEAN_ABSVAL, &rval, &gval, &bval); fract = 255.0 / rval; irval = lept_roundftoi(fract * rval); igval = lept_roundftoi(fract * gval / 2.0); ibval = lept_roundftoi(fract * bval / 2.0); fprintf(stderr, "(r,g,b) = (%d,%d,%d)\n", irval, igval, ibval); /* Color the quantized gray version in the selected regions */ pix11 = pixColorGrayRegions(pix3, boxa, L_PAINT_DARK, 220, irval, igval, ibval); pixaAddPix(pixa, pix11, L_CLONE); regTestWritePixAndCheck(rp, pix11, IFF_PNG); /* 9 */ pixDisplayWithTitle(pix11, 800, 600, "Final colored result", rp->display); pixaAddPix(pixa, pixs, L_CLONE); /* Test colorization on gray and cmapped gray */ pix12 = pixColorGrayRegions(pix2, boxa, L_PAINT_DARK, 220, 0, 255, 0); pixaAddPix(pixa, pix12, L_CLONE); regTestWritePixAndCheck(rp, pix12, IFF_PNG); /* 10 */ pixDisplayWithTitle(pix12, 900, 600, "Colorizing boxa gray", rp->display); box = boxCreate(200, 200, 250, 350); pix13 = pixCopy(NULL, pix2); pixColorGray(pix13, box, L_PAINT_DARK, 220, 0, 0, 255); pixaAddPix(pixa, pix13, L_CLONE); regTestWritePixAndCheck(rp, pix13, IFF_PNG); /* 11 */ pixDisplayWithTitle(pix13, 1000, 600, "Colorizing box gray", rp->display); pix14 = pixThresholdTo4bpp(pix2, 6, 1); pix15 = pixColorGrayRegions(pix14, boxa, L_PAINT_DARK, 220, 0, 0, 255); pixaAddPix(pixa, pix15, L_CLONE); regTestWritePixAndCheck(rp, pix15, IFF_PNG); /* 12 */ pixDisplayWithTitle(pix15, 1100, 600, "Colorizing boxa cmap", rp->display); pixColorGrayCmap(pix14, box, L_PAINT_DARK, 0, 255, 255); pixaAddPix(pixa, pix14, L_CLONE); regTestWritePixAndCheck(rp, pix14, IFF_PNG); /* 13 */ pixDisplayWithTitle(pix14, 1200, 600, "Colorizing box cmap", rp->display); boxDestroy(&box); /* Generate a pdf of the intermediate results */ lept_mkdir("lept"); L_INFO("Writing to /tmp/lept/colorize.pdf\n", rp->testname); pixaConvertToPdf(pixa, 90, 1.0, 0, 0, "Colorizing highlighted text", "/tmp/lept/colorize.pdf"); pixaDestroy(&pixa); fpixDestroy(&fpix); boxDestroy(&box); boxaDestroy(&boxa); pixDestroy(&pixs); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); pixDestroy(&pix5); pixDestroy(&pix6); pixDestroy(&pix7); pixDestroy(&pix8); pixDestroy(&pix9); pixDestroy(&pix10); pixDestroy(&pix11); pixDestroy(&pix12); pixDestroy(&pix13); pixDestroy(&pix14); pixDestroy(&pix15); /* Test the color detector */ pixa = pixaCreate(7); bmf = bmfCreate("./fonts", 4); pix1 = TestForRedColor(rp, "brev06.75.jpg", 1, bmf); /* 14 */ pixaAddPix(pixa, pix1, L_INSERT); pix1 = TestForRedColor(rp, "brev10.75.jpg", 0, bmf); /* 15 */ pixaAddPix(pixa, pix1, L_INSERT); pix1 = TestForRedColor(rp, "brev14.75.jpg", 1, bmf); /* 16 */ pixaAddPix(pixa, pix1, L_INSERT); pix1 = TestForRedColor(rp, "brev20.75.jpg", 1, bmf); /* 17 */ pixaAddPix(pixa, pix1, L_INSERT); pix1 = TestForRedColor(rp, "brev36.75.jpg", 0, bmf); /* 18 */ pixaAddPix(pixa, pix1, L_INSERT); pix1 = TestForRedColor(rp, "brev53.75.jpg", 1, bmf); /* 19 */ pixaAddPix(pixa, pix1, L_INSERT); pix1 = TestForRedColor(rp, "brev56.75.jpg", 1, bmf); /* 20 */ pixaAddPix(pixa, pix1, L_INSERT); /* Generate a pdf of the color detector results */ L_INFO("Writing to /tmp/lept/colordetect.pdf\n", rp->testname); pixaConvertToPdf(pixa, 45, 1.0, 0, 0, "Color detection", "/tmp/lept/colordetect.pdf"); pixaDestroy(&pixa); bmfDestroy(&bmf); return regTestCleanup(rp); }
main(int argc, char **argv) { char *errorstr; l_int32 same, error; PIX *pixs1, *pixs2, *pixs4, *pixs8, *pixs16, *pixs32, *pixd; PIX *pixc2, *pixc4, *pixc8; PIX *pixt1, *pixt2, *pixt3, *pixt4, *pixt5, *pixt6; PIXCMAP *cmap; SARRAY *sa; static char mainName[] = "convert_reg"; if (argc != 1) exit(ERROR_INT(" Syntax: convert_rt", mainName, 1)); if ((pixs1 = pixRead("test1.png")) == NULL) exit(ERROR_INT("pixs1 not made", mainName, 1)); if ((pixs2 = pixRead("dreyfus2.png")) == NULL) exit(ERROR_INT("pixs2 not made", mainName, 1)); if ((pixc2 = pixRead("weasel2.4c.png")) == NULL) exit(ERROR_INT("pixc2 not made", mainName, 1)); if ((pixs4 = pixRead("weasel4.16g.png")) == NULL) exit(ERROR_INT("pixs4 not made", mainName, 1)); if ((pixc4 = pixRead("weasel4.11c.png")) == NULL) exit(ERROR_INT("pixc4 not made", mainName, 1)); if ((pixs8 = pixRead("karen8.jpg")) == NULL) exit(ERROR_INT("pixs8 not made", mainName, 1)); if ((pixc8 = pixRead("weasel8.240c.png")) == NULL) exit(ERROR_INT("pixc8 not made", mainName, 1)); if ((pixs16 = pixRead("test16.tif")) == NULL) exit(ERROR_INT("pixs16 not made", mainName, 1)); if ((pixs32 = pixRead("marge.jpg")) == NULL) exit(ERROR_INT("pixs32 not made", mainName, 1)); error = FALSE; sa = sarrayCreate(0); /* Conversion: 1 bpp --> 8 bpp --> 1 bpp */ pixt1 = pixConvertTo8(pixs1, FALSE); pixt2 = pixThreshold8(pixt1, 1, 0, 0); pixEqual(pixs1, pixt2, &same); if (!same) { pixDisplayWithTitle(pixs1, 100, 100, "1 bpp, no cmap", DFLAG); pixDisplayWithTitle(pixt2, 500, 100, "1 bpp, no cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 1 bpp <==> 8 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 1 bpp <==> 8 bpp\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); /* Conversion: 2 bpp --> 8 bpp --> 2 bpp */ /* Conversion: 2 bpp cmap --> 8 bpp cmap --> 2 bpp cmap */ pixt1 = pixRemoveColormap(pixs2, REMOVE_CMAP_TO_GRAYSCALE); pixt2 = pixThreshold8(pixt1, 2, 4, 0); pixt3 = pixConvertTo8(pixt2, FALSE); pixt4 = pixThreshold8(pixt3, 2, 4, 0); pixEqual(pixt2, pixt4, &same); if (!same) { pixDisplayWithTitle(pixt2, 100, 100, "2 bpp, no cmap", DFLAG); pixDisplayWithTitle(pixt4, 500, 100, "2 bpp, no cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 2 bpp <==> 8 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 2 bpp <==> 8 bpp\n"); pixt5 = pixConvertTo8(pixs2, TRUE); pixt6 = pixThreshold8(pixt5, 2, 4, 1); pixEqual(pixs2, pixt6, &same); if (!same) { pixDisplayWithTitle(pixs2, 100, 100, "2 bpp, cmap", DFLAG); pixDisplayWithTitle(pixt6, 500, 100, "2 bpp, cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 2 bpp <==> 8 bpp; cmap", L_COPY); } else fprintf(stderr, "OK: conversion 2 bpp <==> 8 bpp; cmap\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); pixDestroy(&pixt5); pixDestroy(&pixt6); /* Conversion: 4 bpp --> 8 bpp --> 4 bpp */ /* Conversion: 4 bpp cmap --> 8 bpp cmap --> 4 bpp cmap */ pixt1 = pixRemoveColormap(pixs4, REMOVE_CMAP_TO_GRAYSCALE); pixt2 = pixThreshold8(pixt1, 4, 16, 0); pixt3 = pixConvertTo8(pixt2, FALSE); pixt4 = pixThreshold8(pixt3, 4, 16, 0); pixEqual(pixt2, pixt4, &same); if (!same) { pixDisplayWithTitle(pixt2, 100, 100, "4 bpp, no cmap", DFLAG); pixDisplayWithTitle(pixt4, 500, 100, "4 bpp, no cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 4 bpp <==> 8 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 4 bpp <==> 8 bpp\n"); pixt5 = pixConvertTo8(pixs4, TRUE); pixt6 = pixThreshold8(pixt5, 4, 16, 1); pixEqual(pixs4, pixt6, &same); if (!same) { pixDisplayWithTitle(pixs4, 100, 100, "4 bpp, cmap", DFLAG); pixDisplayWithTitle(pixt6, 500, 100, "4 bpp, cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 4 bpp <==> 8 bpp, cmap", L_COPY); } else fprintf(stderr, "OK: conversion 4 bpp <==> 8 bpp; cmap\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); pixDestroy(&pixt5); pixDestroy(&pixt6); /* Conversion: 2 bpp cmap --> 2 bpp --> 2 bpp cmap --> 2 bpp */ pixt1 = pixRemoveColormap(pixs2, REMOVE_CMAP_TO_GRAYSCALE); pixt2 = pixConvertGrayToColormap(pixt1); pixt3 = pixRemoveColormap(pixt2, REMOVE_CMAP_TO_GRAYSCALE); pixt4 = pixThresholdTo2bpp(pixt3, 4, 1); pixEqual(pixt1, pixt4, &same); if (!same) { pixDisplayWithTitle(pixs2, 100, 100, "2 bpp, cmap", DFLAG); pixDisplayWithTitle(pixt4, 500, 100, "2 bpp, cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 2 bpp <==> 2 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 2 bpp <==> 2 bpp\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); /* Conversion: 4 bpp cmap --> 4 bpp --> 4 bpp cmap --> 4 bpp */ pixt1 = pixRemoveColormap(pixs4, REMOVE_CMAP_TO_GRAYSCALE); pixt2 = pixConvertGrayToColormap(pixt1); pixt3 = pixRemoveColormap(pixt2, REMOVE_CMAP_TO_GRAYSCALE); pixt4 = pixThresholdTo4bpp(pixt3, 16, 1); pixEqual(pixt1, pixt4, &same); if (!same) { pixDisplayWithTitle(pixs4, 100, 100, "4 bpp, cmap", DFLAG); pixDisplayWithTitle(pixt4, 500, 100, "4 bpp, cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 4 bpp <==> 4 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 4 bpp <==> 4 bpp\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); /* Conversion: 8 bpp --> 8 bpp cmap --> 8 bpp */ pixt1 = pixConvertTo8(pixs8, TRUE); pixt2 = pixConvertTo8(pixt1, FALSE); pixEqual(pixs8, pixt2, &same); if (!same) { pixDisplayWithTitle(pixt1, 100, 100, "8 bpp, cmap", DFLAG); pixDisplayWithTitle(pixt2, 500, 100, "8 bpp, no cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 8 bpp <==> 8 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 8 bpp <==> 8 bpp\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); /* Conversion: 2 bpp cmap --> 32 bpp --> 2 bpp cmap */ pixt1 = pixConvertTo8(pixc2, TRUE); pixt2 = pixConvertTo32(pixt1); pixt3 = pixConvertTo32(pixc2); pixEqual(pixt2, pixt3, &same); if (!same) { pixDisplayWithTitle(pixt2, 100, 100, "32 bpp", DFLAG); pixDisplayWithTitle(pixt3, 500, 100, "32 bpp", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 2 bpp ==> 32 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 2 bpp <==> 32 bpp\n"); cmap = pixGetColormap(pixc2); pixt4 = pixOctcubeQuantFromCmap(pixt3, cmap, 2, 4, L_EUCLIDEAN_DISTANCE); pixEqual(pixc2, pixt4, &same); if (!same) { pixDisplayWithTitle(pixc2, 100, 100, "4 bpp, cmap", DFLAG); pixDisplayWithTitle(pixt4, 500, 100, "4 bpp, cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 2 bpp <==> 32 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 2 bpp <==> 32 bpp\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); /* Conversion: 4 bpp cmap --> 32 bpp --> 4 bpp cmap */ pixt1 = pixConvertTo8(pixc4, TRUE); pixt2 = pixConvertTo32(pixt1); pixt3 = pixConvertTo32(pixc4); pixEqual(pixt2, pixt3, &same); if (!same) { pixDisplayWithTitle(pixt2, 100, 100, "32 bpp", DFLAG); pixDisplayWithTitle(pixt3, 500, 100, "32 bpp", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 4 bpp ==> 32 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 4 bpp <==> 32 bpp\n"); cmap = pixGetColormap(pixc4); pixt4 = pixOctcubeQuantFromCmap(pixt3, cmap, 2, 4, L_EUCLIDEAN_DISTANCE); pixEqual(pixc4, pixt4, &same); if (!same) { pixDisplayWithTitle(pixc4, 100, 100, "4 bpp, cmap", DFLAG); pixDisplayWithTitle(pixt4, 500, 100, "4 bpp, cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 4 bpp <==> 32 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 4 bpp <==> 32 bpp\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); /* Conversion: 8 bpp --> 32 bpp --> 8 bpp */ pixt1 = pixConvertTo32(pixs8); pixt2 = pixConvertTo8(pixt1, FALSE); pixEqual(pixs8, pixt2, &same); if (!same) { pixDisplayWithTitle(pixs8, 100, 100, "8 bpp", DFLAG); pixDisplayWithTitle(pixt2, 500, 100, "8 bpp", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 8 bpp <==> 32 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 8 bpp <==> 32 bpp\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); /* Conversion: 8 bpp --> 16 bpp --> 8 bpp */ pixt1 = pixConvert8To16(pixs8, 8); pixt2 = pixConvertTo8(pixt1, FALSE); pixEqual(pixs8, pixt2, &same); if (!same) { pixDisplayWithTitle(pixs8, 100, 100, "8 bpp", DFLAG); pixDisplayWithTitle(pixt2, 500, 100, "8 bpp", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 8 bpp <==> 16 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 8 bpp <==> 16 bpp\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); /* Conversion: 16 bpp --> 8 bpp --> 16 bpp */ pixt1 = pixConvert16To8(pixs16, 1); pixt2 = pixConvertTo16(pixt1); pixWrite("/tmp/junkpix.png", pixt2, IFF_PNG); pixEqual(pixs16, pixt2, &same); if (!same) { pixDisplayWithTitle(pixs16, 100, 100, "16 bpp", DFLAG); pixDisplayWithTitle(pixt2, 500, 100, "16 bpp", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 16 bpp <==> 8 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 16 bpp <==> 8 bpp\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); /* Conversion: 8 bpp cmap --> 32 bpp --> 8 bpp cmap */ /* Required to go to level 6 of octcube to get identical result */ pixt1 = pixConvertTo32(pixc8); cmap = pixGetColormap(pixc8); pixt2 = pixOctcubeQuantFromCmap(pixt1, cmap, 2, 6, L_EUCLIDEAN_DISTANCE); pixEqual(pixc8, pixt2, &same); if (!same) { pixDisplayWithTitle(pixc8, 100, 100, "8 bpp cmap", DFLAG); pixDisplayWithTitle(pixt2, 500, 100, "8 bpp cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 8 bpp cmap <==> 32 bpp cmap", L_COPY); } else fprintf(stderr, "OK: conversion 8 bpp <==> 32 bpp\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); /* Summarize results */ if (error == FALSE) fprintf(stderr, "No errors found\n"); else { errorstr = sarrayToString(sa, 1); fprintf(stderr, "Errors in the following:\n %s", errorstr); lept_free(errorstr); } sarrayDestroy(&sa); pixDestroy(&pixs1); pixDestroy(&pixs2); pixDestroy(&pixs4); pixDestroy(&pixc2); pixDestroy(&pixc4); pixDestroy(&pixs8); pixDestroy(&pixc8); pixDestroy(&pixs16); pixDestroy(&pixs32); return 0; }
main(int argc, char **argv) { l_int32 errorfound, same; PIX *pixs, *pixt1, *pixt2, *pixt3, *pixt4; static char mainName[] = "equal_reg"; if (argc != 1) exit(ERROR_INT(" Syntax: equal_reg", mainName, 1)); errorfound = FALSE; pixs = pixRead(FEYN1); pixWrite("/tmp/junkfeyn.png", pixs, IFF_PNG); pixt1 = pixRead("/tmp/junkfeyn.png"); pixEqual(pixs, pixt1, &same); if (same) L_INFO("equal for feyn1", mainName); else { L_INFO("FAILURE for equal for feyn1", mainName); errorfound = TRUE; } pixDestroy(&pixs); pixDestroy(&pixt1); pixs = pixRead(DREYFUS2); pixt1 = pixRemoveColormap(pixs, REMOVE_CMAP_BASED_ON_SRC); pixWrite("/tmp/junkdrey2-1.png", pixt1, IFF_PNG); pixt2 = pixRemoveColormap(pixs, REMOVE_CMAP_TO_FULL_COLOR); pixWrite("/tmp/junkdrey2-2.png", pixt2, IFF_PNG); pixt3 = pixOctreeQuantNumColors(pixt2, 64, 1); pixWrite("/tmp/junkdrey2-3.png", pixt3, IFF_PNG); pixt4 = pixConvertRGBToColormap(pixt2, 1); pixWrite("/tmp/junkdrey2-4.png", pixt4, IFF_PNG); pixEqual(pixs, pixt1, &same); if (same) L_INFO("equal for pixt1 of dreyfus2", mainName); else { L_INFO("FAILURE for pixt1 of dreyfus2", mainName); errorfound = TRUE; } pixEqual(pixs, pixt2, &same); if (same) L_INFO("equal for pixt2 of dreyfus2", mainName); else { L_INFO("FAILURE for pixt2 of dreyfus2", mainName); errorfound = TRUE; } pixEqual(pixs, pixt3, &same); if (same) L_INFO("equal for pixt3 of dreyfus2", mainName); else { L_INFO("FAILURE for pixt3 of dreyfus2", mainName); errorfound = TRUE; } pixEqual(pixs, pixt4, &same); if (same) L_INFO("equal for pixt4 of dreyfus2", mainName); else { L_INFO("FAILURE for pixt4 of dreyfus2", mainName); errorfound = TRUE; } pixDestroy(&pixs); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); pixs = pixRead(DREYFUS4); pixt1 = pixRemoveColormap(pixs, REMOVE_CMAP_BASED_ON_SRC); pixWrite("/tmp/junkdrey4-1.png", pixt1, IFF_PNG); pixt2 = pixRemoveColormap(pixs, REMOVE_CMAP_TO_FULL_COLOR); pixWrite("/tmp/junkdrey4-2.png", pixt2, IFF_PNG); pixt3 = pixOctreeQuantNumColors(pixt2, 256, 1); pixWrite("/tmp/junkdrey4-3.png", pixt3, IFF_PNG); pixt4 = pixConvertRGBToColormap(pixt2, 1); pixWrite("/tmp/junkdrey4-4.png", pixt4, IFF_PNG); pixEqual(pixs, pixt1, &same); if (same) L_INFO("equal for pixt1 of dreyfus4", mainName); else { L_INFO("FAILURE for pixt1 of dreyfus4", mainName); errorfound = TRUE; } pixEqual(pixs, pixt2, &same); if (same) L_INFO("equal for pixt2 of dreyfus4", mainName); else { L_INFO("FAILURE for pixt2 of dreyfus4", mainName); errorfound = TRUE; } pixEqual(pixs, pixt3, &same); if (same) L_INFO("equal for pixt3 of dreyfus4", mainName); else { L_INFO("FAILURE for pixt3 of dreyfus4", mainName); errorfound = TRUE; } pixEqual(pixs, pixt4, &same); if (same) L_INFO("equal for pixt4 of dreyfus4", mainName); else { L_INFO("FAILURE for pixt4 of dreyfus4", mainName); errorfound = TRUE; } pixDestroy(&pixs); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); pixs = pixRead(DREYFUS8); pixt1 = pixRemoveColormap(pixs, REMOVE_CMAP_BASED_ON_SRC); pixWrite("/tmp/junkdrey8-1.png", pixt1, IFF_PNG); pixt2 = pixRemoveColormap(pixs, REMOVE_CMAP_TO_FULL_COLOR); pixWrite("/tmp/junkdrey8-2.png", pixt2, IFF_PNG); pixt3 = pixConvertRGBToColormap(pixt2, 1); pixWrite("/tmp/junkdrey8-3.png", pixt3, IFF_PNG); pixEqual(pixs, pixt1, &same); if (same) L_INFO("equal for pixt1 of dreyfus8", mainName); else { L_INFO("FAILURE for pixt1 of dreyfus8", mainName); errorfound = TRUE; } pixEqual(pixs, pixt2, &same); if (same) L_INFO("equal for pixt2 of dreyfus8", mainName); else { L_INFO("FAILURE for pixt2 of dreyfus8", mainName); errorfound = TRUE; } pixDestroy(&pixs); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixs = pixRead(KAREN8); pixt1 = pixThresholdTo4bpp(pixs, 16, 1); pixWrite("/tmp/junkkar8-1.png", pixt1, IFF_PNG); pixt2 = pixRemoveColormap(pixt1, REMOVE_CMAP_BASED_ON_SRC); pixWrite("/tmp/junkkar8-2.png", pixt2, IFF_PNG); pixt3 = pixRemoveColormap(pixt1, REMOVE_CMAP_TO_FULL_COLOR); pixWrite("/tmp/junkkar8-3.png", pixt3, IFF_PNG); pixt4 = pixConvertRGBToColormap(pixt3, 1); pixEqual(pixt1, pixt2, &same); if (same) L_INFO("equal for pixt2 of karen8", mainName); else { L_INFO("FAILURE for pixt2 of karen8", mainName); errorfound = TRUE; } pixEqual(pixt1, pixt3, &same); if (same) L_INFO("equal for pixt3 of karen8", mainName); else { L_INFO("FAILURE for pixt3 of karen8", mainName); errorfound = TRUE; } pixEqual(pixt1, pixt4, &same); if (same) L_INFO("equal for pixt4 of karen8", mainName); else { L_INFO("FAILURE for pixt4 of karen8", mainName); errorfound = TRUE; } pixDestroy(&pixs); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); pixs = pixRead(MARGE32); pixt1 = pixOctreeQuantNumColors(pixs, 32, 0); pixWrite("/tmp/junkmarge8-1.png", pixt1, IFF_PNG); pixt2 = pixRemoveColormap(pixt1, REMOVE_CMAP_TO_FULL_COLOR); pixWrite("/tmp/junkmarge8-2.png", pixt2, IFF_PNG); pixt3 = pixConvertRGBToColormap(pixt2, 1); pixWrite("/tmp/junkmarge8-3.png", pixt3, IFF_PNG); pixt4 = pixOctreeQuantNumColors(pixt2, 64, 0); pixWrite("/tmp/junkmarge8-4.png", pixt4, IFF_PNG); pixEqual(pixt1, pixt2, &same); if (same) L_INFO("equal for pixt2 of marge32", mainName); else { L_INFO("FAILURE for pixt2 of marge32", mainName); errorfound = TRUE; } pixEqual(pixt1, pixt3, &same); if (same) L_INFO("equal for pixt3 of marge32", mainName); else { L_INFO("FAILURE for pixt3 of marge32", mainName); errorfound = TRUE; } pixEqual(pixt1, pixt4, &same); if (same) L_INFO("equal for pixt4 of marge32", mainName); else { L_INFO("FAILURE for pixt4 of marge32", mainName); errorfound = TRUE; } pixDestroy(&pixs); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); if (errorfound) L_INFO("FAILURE in processing this test", mainName); else L_INFO("SUCCESS in processing this test", mainName); exit(0); }
int main(int argc, char **argv) { l_int32 i, j, sindex, wb, hb, ws, hs, delx, dely, x, y, y0; PIX *pixs, *pixb, *pix1, *pix2; PIXA *pixa; PIXCMAP *cmap; setLeptDebugOK(1); lept_mkdir("lept/blend"); pixa = pixaCreate(0); pixs = pixRead("rabi.png"); /* blendee */ pixb = pixRead("weasel4.11c.png"); /* blender */ /* Fade the blender */ pixcmapShiftIntensity(pixGetColormap(pixb), FADE_FRACTION); /* Downscale the input */ wb = pixGetWidth(pixb); hb = pixGetHeight(pixb); pix1 = pixScaleToGray4(pixs); /* Threshold to 5 levels, 4 bpp */ ws = pixGetWidth(pix1); hs = pixGetHeight(pix1); pix2 = pixThresholdTo4bpp(pix1, 5, 1); pixaAddPix(pixa, pix2, L_COPY); pixaAddPix(pixa, pixb, L_COPY); cmap = pixGetColormap(pix2); pixcmapWriteStream(stderr, cmap); /* Overwrite the white pixels (at sindex in pix2) */ pixcmapGetIndex(cmap, 255, 255, 255, &sindex); /* Blend the weasel 20 times */ delx = ws / NX; dely = hs / NY; for (i = 0; i < NY; i++) { y = 20 + i * dely; if (y >= hs + hb) continue; for (j = 0; j < NX; j++) { x = 30 + j * delx; y0 = y; if (j & 1) { y0 = y + dely / 2; if (y0 >= hs + hb) continue; } if (x >= ws + wb) continue; pixBlendCmap(pix2, pixb, x, y0, sindex); } } pixaAddPix(pixa, pix2, L_COPY); cmap = pixGetColormap(pix2); pixcmapWriteStream(stderr, cmap); fprintf(stderr, "Writing to: /tmp/lept/blend/blendcmap.pdf\n"); pixaConvertToPdf(pixa, 0, 1.0, L_FLATE_ENCODE, 0, "cmap-blendtest", "/tmp/lept/blend/blendcmap.pdf"); pixDestroy(&pixs); pixDestroy(&pixb); pixDestroy(&pix1); pixDestroy(&pix2); pixaDestroy(&pixa); return 0; }
int main(int argc, char **argv) { l_int32 index; l_uint32 val32; BOX *box, *box1, *box2, *box3, *box4, *box5; BOXA *boxa; L_KERNEL *kel; PIX *pixs, *pixg, *pixb, *pixd, *pixt, *pix1, *pix2, *pix3, *pix4; PIXA *pixa; PIXCMAP *cmap; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; pixa = pixaCreate(0); /* Color non-white pixels on RGB */ pixs = pixRead("lucasta-frag.jpg"); pixt = pixConvert8To32(pixs); box = boxCreate(120, 30, 200, 200); pixColorGray(pixt, box, L_PAINT_DARK, 220, 0, 0, 255); regTestWritePixAndCheck(rp, pixt, IFF_JFIF_JPEG); /* 0 */ pixaAddPix(pixa, pixt, L_COPY); pixColorGray(pixt, NULL, L_PAINT_DARK, 220, 255, 100, 100); regTestWritePixAndCheck(rp, pixt, IFF_JFIF_JPEG); /* 1 */ pixaAddPix(pixa, pixt, L_INSERT); boxDestroy(&box); /* Color non-white pixels on colormap */ pixt = pixThresholdTo4bpp(pixs, 6, 1); box = boxCreate(120, 30, 200, 200); pixColorGray(pixt, box, L_PAINT_DARK, 220, 0, 0, 255); regTestWritePixAndCheck(rp, pixt, IFF_PNG); /* 2 */ pixaAddPix(pixa, pixt, L_COPY); pixColorGray(pixt, NULL, L_PAINT_DARK, 220, 255, 100, 100); regTestWritePixAndCheck(rp, pixt, IFF_PNG); /* 3 */ pixaAddPix(pixa, pixt, L_INSERT); boxDestroy(&box); /* Color non-black pixels on RGB */ pixt = pixConvert8To32(pixs); box = boxCreate(120, 30, 200, 200); pixColorGray(pixt, box, L_PAINT_LIGHT, 20, 0, 0, 255); regTestWritePixAndCheck(rp, pixt, IFF_PNG); /* 4 */ pixaAddPix(pixa, pixt, L_COPY); pixColorGray(pixt, NULL, L_PAINT_LIGHT, 80, 255, 100, 100); regTestWritePixAndCheck(rp, pixt, IFF_PNG); /* 5 */ pixaAddPix(pixa, pixt, L_INSERT); boxDestroy(&box); /* Color non-black pixels on colormap */ pixt = pixThresholdTo4bpp(pixs, 6, 1); box = boxCreate(120, 30, 200, 200); pixColorGray(pixt, box, L_PAINT_LIGHT, 20, 0, 0, 255); regTestWritePixAndCheck(rp, pixt, IFF_PNG); /* 6 */ pixaAddPix(pixa, pixt, L_COPY); pixColorGray(pixt, NULL, L_PAINT_LIGHT, 20, 255, 100, 100); regTestWritePixAndCheck(rp, pixt, IFF_PNG); /* 7 */ pixaAddPix(pixa, pixt, L_INSERT); boxDestroy(&box); /* Add highlight color to RGB */ pixt = pixConvert8To32(pixs); box = boxCreate(507, 5, 385, 45); pixg = pixClipRectangle(pixs, box, NULL); pixb = pixThresholdToBinary(pixg, 180); pixInvert(pixb, pixb); pixDisplayWrite(pixb, 1); composeRGBPixel(50, 0, 250, &val32); pixPaintThroughMask(pixt, pixb, box->x, box->y, val32); boxDestroy(&box); pixDestroy(&pixg); pixDestroy(&pixb); box = boxCreate(236, 107, 262, 40); pixg = pixClipRectangle(pixs, box, NULL); pixb = pixThresholdToBinary(pixg, 180); pixInvert(pixb, pixb); composeRGBPixel(250, 0, 50, &val32); pixPaintThroughMask(pixt, pixb, box->x, box->y, val32); boxDestroy(&box); pixDestroy(&pixg); pixDestroy(&pixb); box = boxCreate(222, 208, 247, 43); pixg = pixClipRectangle(pixs, box, NULL); pixb = pixThresholdToBinary(pixg, 180); pixInvert(pixb, pixb); composeRGBPixel(60, 250, 60, &val32); pixPaintThroughMask(pixt, pixb, box->x, box->y, val32); regTestWritePixAndCheck(rp, pixt, IFF_PNG); /* 8 */ pixaAddPix(pixa, pixt, L_INSERT); boxDestroy(&box); pixDestroy(&pixg); pixDestroy(&pixb); /* Add highlight color to colormap */ pixt = pixThresholdTo4bpp(pixs, 5, 1); cmap = pixGetColormap(pixt); pixcmapGetIndex(cmap, 255, 255, 255, &index); box = boxCreate(507, 5, 385, 45); pixSetSelectCmap(pixt, box, index, 50, 0, 250); boxDestroy(&box); box = boxCreate(236, 107, 262, 40); pixSetSelectCmap(pixt, box, index, 250, 0, 50); boxDestroy(&box); box = boxCreate(222, 208, 247, 43); pixSetSelectCmap(pixt, box, index, 60, 250, 60); regTestWritePixAndCheck(rp, pixt, IFF_PNG); /* 9 */ pixaAddPix(pixa, pixt, L_INSERT); boxDestroy(&box); /* Paint lines on RGB */ pixt = pixConvert8To32(pixs); pixRenderLineArb(pixt, 450, 20, 850, 320, 5, 200, 50, 125); pixRenderLineArb(pixt, 30, 40, 440, 40, 5, 100, 200, 25); box = boxCreate(70, 80, 300, 245); pixRenderBoxArb(pixt, box, 3, 200, 200, 25); regTestWritePixAndCheck(rp, pixt, IFF_JFIF_JPEG); /* 10 */ pixaAddPix(pixa, pixt, L_INSERT); boxDestroy(&box); /* Paint lines on colormap */ pixt = pixThresholdTo4bpp(pixs, 5, 1); pixRenderLineArb(pixt, 450, 20, 850, 320, 5, 200, 50, 125); pixRenderLineArb(pixt, 30, 40, 440, 40, 5, 100, 200, 25); box = boxCreate(70, 80, 300, 245); pixRenderBoxArb(pixt, box, 3, 200, 200, 25); regTestWritePixAndCheck(rp, pixt, IFF_PNG); /* 11 */ pixaAddPix(pixa, pixt, L_INSERT); boxDestroy(&box); /* Blend lines on RGB */ pixt = pixConvert8To32(pixs); pixRenderLineBlend(pixt, 450, 20, 850, 320, 5, 200, 50, 125, 0.35); pixRenderLineBlend(pixt, 30, 40, 440, 40, 5, 100, 200, 25, 0.35); box = boxCreate(70, 80, 300, 245); pixRenderBoxBlend(pixt, box, 3, 200, 200, 25, 0.6); regTestWritePixAndCheck(rp, pixt, IFF_JFIF_JPEG); /* 12 */ pixaAddPix(pixa, pixt, L_INSERT); boxDestroy(&box); /* Colorize gray on cmapped image. */ pix1 = pixRead("lucasta.150.jpg"); pix2 = pixThresholdTo4bpp(pix1, 7, 1); box1 = boxCreate(73, 206, 140, 27); pixColorGrayCmap(pix2, box1, L_PAINT_LIGHT, 130, 207, 43); regTestWritePixAndCheck(rp, pix2, IFF_PNG); /* 13 */ pixaAddPix(pixa, pix2, L_COPY); if (rp->display) pixPrintStreamInfo(stderr, pix2, "One box added"); box2 = boxCreate(255, 404, 197, 25); pixColorGrayCmap(pix2, box2, L_PAINT_LIGHT, 230, 67, 119); regTestWritePixAndCheck(rp, pix2, IFF_PNG); /* 14 */ pixaAddPix(pixa, pix2, L_COPY); if (rp->display) pixPrintStreamInfo(stderr, pix2, "Two boxes added"); box3 = boxCreate(122, 756, 224, 22); pixColorGrayCmap(pix2, box3, L_PAINT_DARK, 230, 67, 119); regTestWritePixAndCheck(rp, pix2, IFF_PNG); /* 15 */ pixaAddPix(pixa, pix2, L_COPY); if (rp->display) pixPrintStreamInfo(stderr, pix2, "Three boxes added"); box4 = boxCreate(11, 780, 147, 22); pixColorGrayCmap(pix2, box4, L_PAINT_LIGHT, 70, 137, 229); regTestWritePixAndCheck(rp, pix2, IFF_PNG); /* 16 */ pixaAddPix(pixa, pix2, L_COPY); if (rp->display) pixPrintStreamInfo(stderr, pix2, "Four boxes added"); box5 = boxCreate(163, 605, 78, 22); pixColorGrayCmap(pix2, box5, L_PAINT_LIGHT, 70, 137, 229); regTestWritePixAndCheck(rp, pix2, IFF_PNG); /* 17 */ pixaAddPix(pixa, pix2, L_INSERT); if (rp->display) pixPrintStreamInfo(stderr, pix2, "Five boxes added"); pixDestroy(&pix1); boxDestroy(&box1); boxDestroy(&box2); boxDestroy(&box3); boxDestroy(&box4); boxDestroy(&box5); pixDestroy(&pixs); /* Make a gray image and identify the fg pixels (val > 230) */ pixs = pixRead("feyn-fract.tif"); pix1 = pixConvertTo8(pixs, 0); kel = makeGaussianKernel(2, 2, 1.5, 1.0); pix2 = pixConvolve(pix1, kel, 8, 1); pix3 = pixThresholdToBinary(pix2, 230); boxa = pixConnComp(pix3, NULL, 8); pixDestroy(&pixs); pixDestroy(&pix1); pixDestroy(&pix3); kernelDestroy(&kel); /* Color the individual components in the gray image */ pix4 = pixColorGrayRegions(pix2, boxa, L_PAINT_DARK, 230, 255, 0, 0); regTestWritePixAndCheck(rp, pix4, IFF_PNG); /* 18 */ pixaAddPix(pixa, pix4, L_INSERT); pixDisplayWithTitle(pix4, 0, 0, NULL, rp->display); /* Threshold to 10 levels of gray */ pix3 = pixThresholdOn8bpp(pix2, 10, 1); regTestWritePixAndCheck(rp, pix3, IFF_PNG); /* 19 */ pixaAddPix(pixa, pix3, L_COPY); /* Color the individual components in the cmapped image */ pix4 = pixColorGrayRegions(pix3, boxa, L_PAINT_DARK, 230, 255, 0, 0); regTestWritePixAndCheck(rp, pix4, IFF_PNG); /* 20 */ pixaAddPix(pixa, pix4, L_INSERT); pixDisplayWithTitle(pix4, 0, 100, NULL, rp->display); boxaDestroy(&boxa); /* Color the entire gray image (not component-wise) */ pixColorGray(pix2, NULL, L_PAINT_DARK, 230, 255, 0, 0); regTestWritePixAndCheck(rp, pix2, IFF_PNG); /* 21 */ pixaAddPix(pixa, pix2, L_INSERT); /* Color the entire cmapped image (not component-wise) */ pixColorGray(pix3, NULL, L_PAINT_DARK, 230, 255, 0, 0); regTestWritePixAndCheck(rp, pix3, IFF_PNG); /* 22 */ pixaAddPix(pixa, pix3, L_INSERT); /* Reconstruct cmapped images */ pixd = ReconstructByValue(rp, "weasel2.4c.png"); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 23 */ pixaAddPix(pixa, pixd, L_INSERT); pixd = ReconstructByValue(rp, "weasel4.11c.png"); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 24 */ pixaAddPix(pixa, pixd, L_INSERT); pixd = ReconstructByValue(rp, "weasel8.240c.png"); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 25 */ pixaAddPix(pixa, pixd, L_INSERT); /* Fake reconstruct cmapped images, with one color into a band */ pixd = FakeReconstructByBand(rp, "weasel2.4c.png"); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 26 */ pixaAddPix(pixa, pixd, L_INSERT); pixd = FakeReconstructByBand(rp, "weasel4.11c.png"); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 27 */ pixaAddPix(pixa, pixd, L_INSERT); pixd = FakeReconstructByBand(rp, "weasel8.240c.png"); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 28 */ pixaAddPix(pixa, pixd, L_INSERT); /* If in testing mode, make a pdf */ if (rp->display) { pixaConvertToPdf(pixa, 100, 1.0, L_FLATE_ENCODE, 0, "Colorize and paint", "/tmp/lept/regout/paint.pdf"); L_INFO("Output pdf: /tmp/lept/regout/paint.pdf\n", rp->testname); } pixaDestroy(&pixa); return regTestCleanup(rp); }
int main(int argc, char **argv) { char buffer[512]; char *tempfile1, *tempfile2; l_uint8 *data; l_int32 i, j, w, h, seq, ret, same; size_t nbytes; const char *title; BOX *box; BOXA *boxa1, *boxa2; L_BYTEA *ba; L_PDF_DATA *lpd; PIX *pix1, *pix2, *pix3, *pix4, *pix5, *pix6; PIX *pixs, *pixt, *pixg, *pixgc, *pixc; static char mainName[] = "pdfiotest"; if (argc != 1) return ERROR_INT("syntax: pdfiotest", mainName, 1); l_pdfSetDateAndVersion(0); lept_mkdir("lept/pdf"); #if 1 /* --------------- Single image tests ------------------- */ fprintf(stderr, "\n*** Writing single images as pdf files\n"); convertToPdf("weasel2.4c.png", L_FLATE_ENCODE, 0, "/tmp/lept/pdf/file01.pdf", 0, 0, 72, "weasel2.4c.png", NULL, 0); convertToPdf("test24.jpg", L_JPEG_ENCODE, 0, "/tmp/lept/pdf/file02.pdf", 0, 0, 72, "test24.jpg", NULL, 0); convertToPdf("feyn.tif", L_G4_ENCODE, 0, "/tmp/lept/pdf/file03.pdf", 0, 0, 300, "feyn.tif", NULL, 0); pixs = pixRead("feyn.tif"); pixConvertToPdf(pixs, L_G4_ENCODE, 0, "/tmp/lept/pdf/file04.pdf", 0, 0, 300, "feyn.tif", NULL, 0); pixDestroy(&pixs); pixs = pixRead("test24.jpg"); pixConvertToPdf(pixs, L_JPEG_ENCODE, 5, "/tmp/lept/pdf/file05.pdf", 0, 0, 72, "test24.jpg", NULL, 0); pixDestroy(&pixs); pixs = pixRead("feyn.tif"); pixt = pixScaleToGray2(pixs); pixWrite("/tmp/lept/pdf/feyn8.png", pixt, IFF_PNG); convertToPdf("/tmp/lept/pdf/feyn8.png", L_JPEG_ENCODE, 0, "/tmp/lept/pdf/file06.pdf", 0, 0, 150, "feyn8.png", NULL, 0); pixDestroy(&pixs); pixDestroy(&pixt); convertToPdf("weasel4.16g.png", L_FLATE_ENCODE, 0, "/tmp/lept/pdf/file07.pdf", 0, 0, 30, "weasel4.16g.png", NULL, 0); pixs = pixRead("test24.jpg"); pixg = pixConvertTo8(pixs, 0); box = boxCreate(100, 100, 100, 100); pixc = pixClipRectangle(pixs, box, NULL); pixgc = pixClipRectangle(pixg, box, NULL); pixWrite("/tmp/lept/pdf/pix32.jpg", pixc, IFF_JFIF_JPEG); pixWrite("/tmp/lept/pdf/pix8.jpg", pixgc, IFF_JFIF_JPEG); convertToPdf("/tmp/lept/pdf/pix32.jpg", L_FLATE_ENCODE, 0, "/tmp/lept/pdf/file08.pdf", 0, 0, 72, "pix32.jpg", NULL, 0); convertToPdf("/tmp/lept/pdf/pix8.jpg", L_FLATE_ENCODE, 0, "/tmp/lept/pdf/file09.pdf", 0, 0, 72, "pix8.jpg", NULL, 0); pixDestroy(&pixs); pixDestroy(&pixg); pixDestroy(&pixc); pixDestroy(&pixgc); boxDestroy(&box); #endif #if 1 /* --------------- Multiple image tests ------------------- */ fprintf(stderr, "\n*** Writing multiple images as single page pdf files\n"); pix1 = pixRead("feyn-fract.tif"); pix2 = pixRead("weasel8.240c.png"); /* l_pdfSetDateAndVersion(0); */ /* First, write the 1 bpp image through the mask onto the weasels */ for (i = 0; i < 5; i++) { for (j = 0; j < 10; j++) { seq = (i == 0 && j == 0) ? L_FIRST_IMAGE : L_NEXT_IMAGE; title = (i == 0 && j == 0) ? "feyn-fract.tif" : NULL; pixConvertToPdf(pix2, L_FLATE_ENCODE, 0, NULL, 100 * j, 100 * i, 70, title, &lpd, seq); } } pixConvertToPdf(pix1, L_G4_ENCODE, 0, "/tmp/lept/pdf/file10.pdf", 0, 0, 80, NULL, &lpd, L_LAST_IMAGE); /* Now, write the 1 bpp image over the weasels */ l_pdfSetG4ImageMask(0); for (i = 0; i < 5; i++) { for (j = 0; j < 10; j++) { seq = (i == 0 && j == 0) ? L_FIRST_IMAGE : L_NEXT_IMAGE; title = (i == 0 && j == 0) ? "feyn-fract.tif" : NULL; pixConvertToPdf(pix2, L_FLATE_ENCODE, 0, NULL, 100 * j, 100 * i, 70, title, &lpd, seq); } } pixConvertToPdf(pix1, L_G4_ENCODE, 0, "/tmp/lept/pdf/file11.pdf", 0, 0, 80, NULL, &lpd, L_LAST_IMAGE); l_pdfSetG4ImageMask(1); pixDestroy(&pix1); pixDestroy(&pix2); #endif #if 1 /* -------- pdf convert segmented with no image regions -------- */ fprintf(stderr, "\n*** Writing segmented images without image regions\n"); pix1 = pixRead("rabi.png"); pix2 = pixScaleToGray2(pix1); pixWrite("/tmp/lept/pdf/rabi8.jpg", pix2, IFF_JFIF_JPEG); pix3 = pixThresholdTo4bpp(pix2, 16, 1); pixWrite("/tmp/lept/pdf/rabi4.png", pix3, IFF_PNG); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); /* 1 bpp input */ convertToPdfSegmented("rabi.png", 300, L_G4_ENCODE, 128, NULL, 0, 0, NULL, "/tmp/lept/pdf/file12.pdf"); convertToPdfSegmented("rabi.png", 300, L_JPEG_ENCODE, 128, NULL, 0, 0, NULL, "/tmp/lept/pdf/file13.pdf"); convertToPdfSegmented("rabi.png", 300, L_FLATE_ENCODE, 128, NULL, 0, 0, NULL, "/tmp/lept/pdf/file14.pdf"); /* 8 bpp input, no cmap */ convertToPdfSegmented("/tmp/lept/pdf/rabi8.jpg", 150, L_G4_ENCODE, 128, NULL, 0, 0, NULL, "/tmp/lept/pdf/file15.pdf"); convertToPdfSegmented("/tmp/lept/pdf/rabi8.jpg", 150, L_JPEG_ENCODE, 128, NULL, 0, 0, NULL, "/tmp/lept/pdf/file16.pdf"); convertToPdfSegmented("/tmp/lept/pdf/rabi8.jpg", 150, L_FLATE_ENCODE, 128, NULL, 0, 0, NULL, "/tmp/lept/pdf/file17.pdf"); /* 4 bpp input, cmap */ convertToPdfSegmented("/tmp/lept/pdf/rabi4.png", 150, L_G4_ENCODE, 128, NULL, 0, 0, NULL, "/tmp/lept/pdf/file18.pdf"); convertToPdfSegmented("/tmp/lept/pdf/rabi4.png", 150, L_JPEG_ENCODE, 128, NULL, 0, 0, NULL, "/tmp/lept/pdf/file19.pdf"); convertToPdfSegmented("/tmp/lept/pdf/rabi4.png", 150, L_FLATE_ENCODE, 128, NULL, 0, 0, NULL, "/tmp/lept/pdf/file20.pdf"); #endif #if 1 /* ---------- pdf convert segmented with image regions ---------- */ fprintf(stderr, "\n*** Writing segmented images with image regions\n"); /* Get the image region(s) for rabi.png. There are two * small bogus regions at the top, but we'll keep them for * the demonstration. */ pix1 = pixRead("rabi.png"); pixSetResolution(pix1, 300, 300); pixGetDimensions(pix1, &w, &h, NULL); pix2 = pixGenerateHalftoneMask(pix1, NULL, NULL, NULL); pix3 = pixMorphSequence(pix2, "c20.1 + c1.20", 0); boxa1 = pixConnComp(pix3, NULL, 8); boxa2 = boxaTransform(boxa1, 0, 0, 0.5, 0.5); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); /* 1 bpp input */ convertToPdfSegmented("rabi.png", 300, L_G4_ENCODE, 128, boxa1, 0, 0.25, NULL, "/tmp/lept/pdf/file21.pdf"); convertToPdfSegmented("rabi.png", 300, L_JPEG_ENCODE, 128, boxa1, 0, 0.25, NULL, "/tmp/lept/pdf/file22.pdf"); convertToPdfSegmented("rabi.png", 300, L_FLATE_ENCODE, 128, boxa1, 0, 0.25, NULL, "/tmp/lept/pdf/file23.pdf"); /* 8 bpp input, no cmap */ convertToPdfSegmented("/tmp/lept/pdf/rabi8.jpg", 150, L_G4_ENCODE, 128, boxa2, 0, 0.5, NULL, "/tmp/lept/pdf/file24.pdf"); convertToPdfSegmented("/tmp/lept/pdf/rabi8.jpg", 150, L_JPEG_ENCODE, 128, boxa2, 0, 0.5, NULL, "/tmp/lept/pdf/file25.pdf"); convertToPdfSegmented("/tmp/lept/pdf/rabi8.jpg", 150, L_FLATE_ENCODE, 128, boxa2, 0, 0.5, NULL, "/tmp/lept/pdf/file26.pdf"); /* 4 bpp input, cmap */ convertToPdfSegmented("/tmp/lept/pdf/rabi4.png", 150, L_G4_ENCODE, 128, boxa2, 0, 0.5, NULL, "/tmp/lept/pdf/file27.pdf"); convertToPdfSegmented("/tmp/lept/pdf/rabi4.png", 150, L_JPEG_ENCODE, 128, boxa2, 0, 0.5, NULL, "/tmp/lept/pdf/file28.pdf"); convertToPdfSegmented("/tmp/lept/pdf/rabi4.png", 150, L_FLATE_ENCODE, 128, boxa2, 0, 0.5, NULL, "/tmp/lept/pdf/file29.pdf"); /* 4 bpp input, cmap, data output */ data = NULL; convertToPdfDataSegmented("/tmp/lept/pdf/rabi4.png", 150, L_G4_ENCODE, 128, boxa2, 0, 0.5, NULL, &data, &nbytes); l_binaryWrite("/tmp/lept/pdf/file30.pdf", "w", data, nbytes); lept_free(data); convertToPdfDataSegmented("/tmp/lept/pdf/rabi4.png", 150, L_JPEG_ENCODE, 128, boxa2, 0, 0.5, NULL, &data, &nbytes); l_binaryWrite("/tmp/lept/pdf/file31.pdf", "w", data, nbytes); lept_free(data); convertToPdfDataSegmented("/tmp/lept/pdf/rabi4.png", 150, L_FLATE_ENCODE, 128, boxa2, 0, 0.5, NULL, &data, &nbytes); l_binaryWrite("/tmp/lept/pdf/file32.pdf", "w", data, nbytes); lept_free(data); boxaDestroy(&boxa1); boxaDestroy(&boxa2); #endif #if 1 /* -------- pdf convert segmented from color image -------- */ fprintf(stderr, "\n*** Writing color segmented images\n"); pix1 = pixRead("candelabrum.011.jpg"); pix2 = pixScale(pix1, 3.0, 3.0); pixWrite("/tmp/lept/pdf/candelabrum3.jpg", pix2, IFF_JFIF_JPEG); GetImageMask(pix2, 200, &boxa1, "/tmp/lept/pdf/seg1.jpg"); convertToPdfSegmented("/tmp/lept/pdf/candelabrum3.jpg", 200, L_G4_ENCODE, 100, boxa1, 0, 0.25, NULL, "/tmp/lept/pdf/file33.pdf"); convertToPdfSegmented("/tmp/lept/pdf/candelabrum3.jpg", 200, L_JPEG_ENCODE, 100, boxa1, 0, 0.25, NULL, "/tmp/lept/pdf/file34.pdf"); convertToPdfSegmented("/tmp/lept/pdf/candelabrum3.jpg", 200, L_FLATE_ENCODE, 100, boxa1, 0, 0.25, NULL, "/tmp/lept/pdf/file35.pdf"); pixDestroy(&pix1); pixDestroy(&pix2); boxaDestroy(&boxa1); pix1 = pixRead("lion-page.00016.jpg"); pix2 = pixScale(pix1, 3.0, 3.0); pixWrite("/tmp/lept/pdf/lion16.jpg", pix2, IFF_JFIF_JPEG); pix3 = pixRead("lion-mask.00016.tif"); boxa1 = pixConnComp(pix3, NULL, 8); boxa2 = boxaTransform(boxa1, 0, 0, 3.0, 3.0); convertToPdfSegmented("/tmp/lept/pdf/lion16.jpg", 200, L_G4_ENCODE, 190, boxa2, 0, 0.5, NULL, "/tmp/lept/pdf/file36.pdf"); convertToPdfSegmented("/tmp/lept/pdf/lion16.jpg", 200, L_JPEG_ENCODE, 190, boxa2, 0, 0.5, NULL, "/tmp/lept/pdf/file37.pdf"); convertToPdfSegmented("/tmp/lept/pdf/lion16.jpg", 200, L_FLATE_ENCODE, 190, boxa2, 0, 0.5, NULL, "/tmp/lept/pdf/file38.pdf"); /* Quantize the non-image part and flate encode. * This is useful because it results in a smaller file than * when you flate-encode the un-quantized non-image regions. */ pix4 = pixScale(pix3, 3.0, 3.0); /* higher res mask, for combining */ pix5 = QuantizeNonImageRegion(pix2, pix4, 12); pixWrite("/tmp/lept/pdf/lion16-quant.png", pix5, IFF_PNG); convertToPdfSegmented("/tmp/lept/pdf/lion16-quant.png", 200, L_FLATE_ENCODE, 190, boxa2, 0, 0.5, NULL, "/tmp/lept/pdf/file39.pdf"); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); pixDestroy(&pix5); boxaDestroy(&boxa1); boxaDestroy(&boxa2); #endif #if 1 /* ------------------ Test multipage pdf generation ----------------- */ fprintf(stderr, "\n*** Writing multipage pdfs from single page pdfs\n"); /* Generate a multi-page pdf from all these files */ startTimer(); concatenatePdf("/tmp/lept/pdf", "file", "/tmp/lept/pdf/cat_lept.pdf"); fprintf(stderr, "All files have been concatenated: /tmp/lept/pdf/cat_lept.pdf\n" "Concatenation time: %7.3f\n", stopTimer()); #endif #if 1 /* ----------- Test corruption recovery by concatenation ------------ */ /* Put two good pdf files in a directory */ lept_rmdir("lept/good"); lept_mkdir("lept/good"); lept_cp("testfile1.pdf", "lept/good", NULL, NULL); lept_cp("testfile2.pdf", "lept/good", NULL, NULL); concatenatePdf("/tmp/lept/good", "file", "/tmp/lept/pdf/good.pdf"); /* Make a bad version with the pdf id removed, so that it is not * recognized as a pdf */ lept_rmdir("lept/bad"); lept_mkdir("lept/bad"); ba = l_byteaInitFromFile("testfile2.pdf"); data = l_byteaGetData(ba, &nbytes); l_binaryWrite("/tmp/lept/bad/testfile0.notpdf.pdf", "w", data + 10, nbytes - 10); /* Make a version with a corrupted trailer */ if (data) data[2297] = '2'; /* munge trailer object 6: change 458 --> 428 */ l_binaryWrite("/tmp/lept/bad/testfile2.bad.pdf", "w", data, nbytes); l_byteaDestroy(&ba); /* Copy testfile1.pdf to the /tmp/lept/bad directory. Then * run concat on the bad files. The "not pdf" file should be * ignored, and the corrupted pdf file should be properly parsed, * so the resulting concatenated pdf files should be identical. */ fprintf(stderr, "\nWe attempt to build from the bad directory\n"); lept_cp("testfile1.pdf", "lept/bad", NULL, NULL); concatenatePdf("/tmp/lept/bad", "file", "/tmp/lept/pdf/bad.pdf"); filesAreIdentical("/tmp/lept/pdf/good.pdf", "/tmp/lept/pdf/bad.pdf", &same); if (same) fprintf(stderr, "Fixed: files are the same\n" "Attempt succeeded\n"); else fprintf(stderr, "Busted: files are different\n"); #endif #if 0 fprintf(stderr, "\n*** pdftk writes multipage pdfs from images\n"); tempfile1 = genPathname("/tmp/lept/pdf", "file*.pdf"); tempfile2 = genPathname("/tmp/lept/pdf", "cat_pdftk.pdf"); snprintf(buffer, sizeof(buffer), "pdftk %s output %s", tempfile1, tempfile2); ret = system(buffer); /* pdftk */ lept_free(tempfile1); lept_free(tempfile2); #endif #if 1 /* -- Test simple interface for generating multi-page pdf from images -- */ fprintf(stderr, "\n*** Writing multipage pdfs from images\n"); /* Put four image files in a directory. They will be encoded thus: * file1.png: flate (8 bpp, only 10 colors) * file2.jpg: dct (8 bpp, 256 colors because of the jpeg encoding) * file3.tif: g4 (1 bpp) * file4.jpg: dct (32 bpp) */ lept_mkdir("lept/image"); pix1 = pixRead("feyn.tif"); pix2 = pixRead("rabi.png"); pix3 = pixScaleToGray3(pix1); pix4 = pixScaleToGray3(pix2); pix5 = pixScale(pix1, 0.33, 0.33); pix6 = pixRead("test24.jpg"); pixWrite("/tmp/lept/image/file1.png", pix3, IFF_PNG); /* 10 colors */ pixWrite("/tmp/lept/image/file2.jpg", pix4, IFF_JFIF_JPEG); /* 256 colors */ pixWrite("/tmp/lept/image/file3.tif", pix5, IFF_TIFF_G4); pixWrite("/tmp/lept/image/file4.jpg", pix6, IFF_JFIF_JPEG); startTimer(); convertFilesToPdf("/tmp/lept/image", "file", 100, 0.8, 0, 75, "4 file test", "/tmp/lept/pdf/fourimages.pdf"); fprintf(stderr, "4-page pdf generated: /tmp/lept/pdf/fourimages.pdf\n" "Time: %7.3f\n", stopTimer()); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); pixDestroy(&pix5); pixDestroy(&pix6); #endif return 0; }
main(int argc, char **argv) { l_int32 same; l_uint32 *rtab, *gtab, *btab; l_int32 *cmaptab; BOX *box; PIX *pixs, *pixt1, *pixt2, *pixt3, *pixt4; PIXCMAP *cmap; static char mainName[] = "cmapquant_reg"; pixs = pixRead("lucasta-frag.jpg"); if (argc != 1) exit(ERROR_INT("syntax: cmapquant_req", mainName, 1)); /* Convert to 4 bpp with 6 levels and a colormap */ pixt1 = pixThresholdTo4bpp(pixs, 6, 1); /* Color some non-white pixels, preserving antialiasing, and * adding these colors to the colormap */ box = boxCreate(120, 30, 200, 200); pixColorGray(pixt1, box, L_PAINT_DARK, 220, 0, 0, 255); pixDisplayWrite(pixt1, 1); boxDestroy(&box); /* Scale up by 1.5; losing the colormap */ startTimer(); pixt2 = pixScale(pixt1, 1.5, 1.5); fprintf(stderr, "Time to scale by 1.5x = %7.3f sec\n", stopTimer()); pixDisplayWrite(pixt2, 1); /* Re-quantize using the same colormap */ startTimer(); cmap = pixGetColormap(pixt1); pixt3 = pixOctcubeQuantFromCmap(pixt2, cmap, MIN_DEPTH, LEVEL, L_EUCLIDEAN_DISTANCE); fprintf(stderr, "Time to requantize to cmap = %7.3f sec\n", stopTimer()); pixDisplayWrite(pixt3, 1); /* Re-quantize first making the tables and then * using the lower-level function */ startTimer(); makeRGBToIndexTables(&rtab, >ab, &btab, LEVEL); cmaptab = pixcmapToOctcubeLUT(cmap, LEVEL, L_EUCLIDEAN_DISTANCE); fprintf(stderr, "Time to make tables = %7.3f sec\n", stopTimer()); startTimer(); pixt4 = pixOctcubeQuantFromCmapLUT(pixt2, cmap, MIN_DEPTH, cmaptab, rtab, gtab, btab); fprintf(stderr, "Time for lowlevel re-quant = %7.3f sec\n", stopTimer()); pixDisplayWrite(pixt4, 1); pixEqual(pixt3, pixt4, &same); if (same) fprintf(stderr, "Correct: images are the same\n"); else fprintf(stderr, "Error: images differ\n"); FREE(cmaptab); FREE(rtab); FREE(gtab); FREE(btab); pixDisplayMultiple("/tmp/junk_write_display*"); pixDestroy(&pixs); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); return 0; }
main(int argc, char **argv) { const char *str; l_int32 equal, index, w, h; BOX *box; PIX *pixs, *pixd, *pixt, *pixd1, *pixd2, *pixd3; PIX *pixt0, *pixt1, *pixt2, *pixt3, *pixt4; PIXA *pixa; PIXCMAP *cmap; static char mainName[] = "grayquant_reg"; if ((pixs = pixRead("test8.jpg")) == NULL) exit(ERROR_INT("pixs not made", mainName, 1)); pixa = pixaCreate(0); pixSaveTiled(pixs, pixa, 1, 1, 20, 8); /* threshold to 1 bpp */ pixd = pixThresholdToBinary(pixs, THRESHOLD); pixSaveTiled(pixd, pixa, 1, 1, 20, 0); pixWrite("/tmp/junkthr0.png", pixd, IFF_PNG); pixDestroy(&pixd); /* dither to 2 bpp, with and without colormap */ pixd = pixDitherTo2bpp(pixs, 1); pixt = pixDitherTo2bpp(pixs, 0); pixt2 = pixConvertGrayToColormap(pixt); pixSaveTiled(pixd, pixa, 1, 1, 20, 0); pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixSaveTiled(pixt2, pixa, 1, 0, 20, 0); pixWrite("/tmp/junkthr1.png", pixd, IFF_PNG); pixWrite("/tmp/junkthr2.png", pixt, IFF_PNG); pixWrite("/tmp/junkthr3.png", pixt2, IFF_PNG); /* pixcmapWriteStream(stderr, pixGetColormap(pixd)); */ pixEqual(pixd, pixt2, &equal); if (!equal) fprintf(stderr, "Error: thr2 != thr3\n"); pixDestroy(&pixt); pixDestroy(&pixt2); pixDestroy(&pixd); /* threshold to 2 bpp, with and without colormap */ pixd = pixThresholdTo2bpp(pixs, 4, 1); pixt = pixThresholdTo2bpp(pixs, 4, 0); pixt2 = pixConvertGrayToColormap(pixt); pixSaveTiled(pixd, pixa, 1, 1, 20, 0); pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixSaveTiled(pixt2, pixa, 1, 0, 20, 0); pixWrite("/tmp/junkthr4.png", pixd, IFF_PNG); pixWrite("/tmp/junkthr5.png", pixt2, IFF_PNG); pixEqual(pixd, pixt2, &equal); if (!equal) fprintf(stderr, "Error: thr4 != thr5\n"); pixDestroy(&pixt); pixDestroy(&pixt2); pixDestroy(&pixd); pixd = pixThresholdTo2bpp(pixs, 3, 1); pixt = pixThresholdTo2bpp(pixs, 3, 0); pixSaveTiled(pixd, pixa, 1, 1, 20, 0); pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixWrite("/tmp/junkthr6.png", pixd, IFF_PNG); pixWrite("/tmp/junkthr7.png", pixt, IFF_PNG); pixDestroy(&pixt); pixDestroy(&pixd); /* threshold to 4 bpp, with and without colormap */ pixd = pixThresholdTo4bpp(pixs, 9, 1); pixt = pixThresholdTo4bpp(pixs, 9, 0); pixt2 = pixConvertGrayToColormap(pixt); pixSaveTiled(pixd, pixa, 1, 1, 20, 0); pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixSaveTiled(pixt2, pixa, 1, 0, 20, 0); pixWrite("/tmp/junkthr8.png", pixd, IFF_PNG); pixWrite("/tmp/junkthr9.png", pixt, IFF_PNG); pixWrite("/tmp/junkthr10.png", pixt2, IFF_PNG); /* pixcmapWriteStream(stderr, pixGetColormap(pixd)); */ pixDestroy(&pixt); pixDestroy(&pixt2); pixDestroy(&pixd); /* threshold on 8 bpp, with and without colormap */ pixd = pixThresholdOn8bpp(pixs, 9, 1); pixt = pixThresholdOn8bpp(pixs, 9, 0); pixt2 = pixConvertGrayToColormap(pixt); pixSaveTiled(pixd, pixa, 1, 1, 20, 0); pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixSaveTiled(pixt2, pixa, 1, 0, 20, 0); pixWrite("/tmp/junkthr11.png", pixd, IFF_PNG); pixWrite("/tmp/junkthr12.png", pixt2, IFF_PNG); /* pixcmapWriteStream(stderr, pixGetColormap(pixd)); */ pixEqual(pixd, pixt2, &equal); if (!equal) fprintf(stderr, "Error: thr11 != thr12\n"); pixDestroy(&pixt); pixDestroy(&pixt2); pixDestroy(&pixd); pixd1 = pixaDisplay(pixa, 0, 0); pixDisplay(pixd1, 100, 100); pixWrite("/tmp/junkpixd1.jpg", pixd1, IFF_JFIF_JPEG); pixDestroy(&pixd1); pixaDestroy(&pixa); pixa = pixaCreate(0); pixSaveTiled(pixs, pixa, 1, 1, 20, 32); /* highlight 2 bpp with colormap */ pixd = pixThresholdTo2bpp(pixs, 3, 1); cmap = pixGetColormap(pixd); pixcmapWriteStream(stderr, cmap); box = boxCreate(278, 35, 122, 50); pixSetSelectCmap(pixd, box, 2, 255, 255, 100); pixcmapWriteStream(stderr, cmap); pixDisplay(pixd, 0, 0); pixSaveTiled(pixd, pixa, 1, 1, 20, 0); pixWrite("/tmp/junkthr13.png", pixd, IFF_PNG); pixDestroy(&pixd); boxDestroy(&box); /* test pixThreshold8() */ pixd = pixThreshold8(pixs, 1, 2, 1); /* cmap */ pixSaveTiled(pixd, pixa, 1, 1, 20, 0); pixWrite("/tmp/junkthr14.png", pixd, IFF_PNG); pixDisplay(pixd, 100, 0); pixDestroy(&pixd); pixd = pixThreshold8(pixs, 1, 2, 0); /* no cmap */ pixSaveTiled(pixd, pixa, 1, 0, 20, 0); pixWrite("/tmp/junkthr15.png", pixd, IFF_PNG); pixDisplay(pixd, 200, 0); pixDestroy(&pixd); pixd = pixThreshold8(pixs, 2, 3, 1); /* highlight one box */ pixSaveTiled(pixd, pixa, 1, 0, 20, 0); box = boxCreate(278, 35, 122, 50); pixSetSelectCmap(pixd, box, 2, 255, 255, 100); pixWrite("/tmp/junkthr16.png", pixd, IFF_PNG); pixDisplay(pixd, 300, 0); cmap = pixGetColormap(pixd); pixcmapWriteStream(stderr, cmap); boxDestroy(&box); pixDestroy(&pixd); pixd = pixThreshold8(pixs, 2, 4, 0); /* no cmap */ pixSaveTiled(pixd, pixa, 1, 0, 20, 0); pixWrite("/tmp/junkthr17.png", pixd, IFF_PNG); pixDisplay(pixd, 400, 0); pixDestroy(&pixd); pixd = pixThreshold8(pixs, 4, 6, 1); /* highlight one box */ box = boxCreate(278, 35, 122, 50); pixSetSelectCmap(pixd, box, 5, 255, 255, 100); pixSaveTiled(pixd, pixa, 1, 0, 20, 0); pixWrite("/tmp/junkthr18.png", pixd, IFF_PNG); cmap = pixGetColormap(pixd); pixcmapWriteStream(stderr, cmap); boxDestroy(&box); pixDisplay(pixd, 500, 0); pixDestroy(&pixd); pixd = pixThreshold8(pixs, 4, 6, 0); /* no cmap */ pixSaveTiled(pixd, pixa, 1, 0, 20, 0); pixWrite("/tmp/junkthr19.png", pixd, IFF_PNG); pixDisplay(pixd, 600, 0); pixDestroy(&pixd); /* highlight 4 bpp with 2 colormap entries */ /* Note: We use 5 levels (0-4) for gray. */ /* 5 & 6 are used for highlight color. */ pixd = pixThresholdTo4bpp(pixs, 5, 1); cmap = pixGetColormap(pixd); pixcmapGetIndex(cmap, 255, 255, 255, &index); box = boxCreate(278, 35, 122, 50); pixSetSelectCmap(pixd, box, index, 255, 255, 100); /* use 5 */ boxDestroy(&box); box = boxCreate(4, 6, 157, 33); pixSetSelectCmap(pixd, box, index, 100, 255, 255); /* use 6 */ boxDestroy(&box); pixcmapWriteStream(stderr, cmap); pixSaveTiled(pixd, pixa, 1, 1, 20, 0); pixDisplay(pixd, 700, 0); pixWrite("/tmp/junkthr20.png", pixd, IFF_PNG); pixDestroy(&pixd); /* comparison 8 bpp jpeg with 2 bpp (highlight) */ pixDestroy(&pixs); pixs = pixRead("feyn.tif"); pixt = pixScaleToGray4(pixs); pixt2 = pixReduceRankBinaryCascade(pixs, 2, 2, 0, 0); pixd = pixThresholdTo2bpp(pixt, 3, 1); box = boxCreate(175, 208, 228, 88); pixSetSelectCmap(pixd, box, 2, 255, 255, 100); pixDisplay(pixd, 100, 200); cmap = pixGetColormap(pixd); pixcmapWriteStream(stderr, cmap); pixSaveTiled(pixt, pixa, 1, 1, 20, 0); pixSaveTiled(pixt2, pixa, 1, 0, 20, 0); pixSaveTiled(pixd, pixa, 1, 0, 20, 0); pixWrite("/tmp/junkthr21.jpg", pixt, IFF_JFIF_JPEG); pixWrite("/tmp/junkthr22.png", pixt2, IFF_PNG); pixWrite("/tmp/junkthr23.png", pixd, IFF_PNG); pixDestroy(&pixd); pixDestroy(&pixt2); boxDestroy(&box); /* thresholding to 4 bpp (highlight); use pixt from above */ pixd = pixThresholdTo4bpp(pixt, NLEVELS, 1); box = boxCreate(175, 208, 228, 83); pixSetSelectCmap(pixd, box, NLEVELS - 1, 255, 255, 100); boxDestroy(&box); box = boxCreate(232, 298, 110, 25); pixSetSelectCmap(pixd, box, NLEVELS - 1, 100, 255, 255); boxDestroy(&box); box = boxCreate(21, 698, 246, 82); pixSetSelectCmap(pixd, box, NLEVELS - 1, 225, 100, 255); boxDestroy(&box); pixDisplay(pixd, 500, 200); cmap = pixGetColormap(pixd); pixcmapWriteStream(stderr, cmap); pixt2 = pixReduceRankBinaryCascade(pixs, 2, 2, 0, 0); pixSaveTiled(pixt2, pixa, 1, 1, 20, 0); pixSaveTiled(pixd, pixa, 1, 0, 20, 0); pixWrite("/tmp/junkthr24.png", pixt2, IFF_PNG); pixWrite("/tmp/junkthr25.png", pixd, IFF_PNG); pixDestroy(&pixt2); pixDestroy(&pixd); /* Thresholding to 4 bpp at 2, 3, 4, 5 and 6 levels */ box = boxCreate(25, 202, 136, 37); pixt1 = pixClipRectangle(pixt, box, NULL); pixt2 = pixScale(pixt1, 6., 6.); pixGetDimensions(pixt2, &w, &h, NULL); pixSaveTiled(pixt2, pixa, 1, 1, 20, 0); pixDisplay(pixt2, 0, 0); pixWrite("/tmp/junk-8.jpg", pixt2, IFF_JFIF_JPEG); pixd = pixCreate(w, 6 * h, 8); pixRasterop(pixd, 0, 0, w, h, PIX_SRC, pixt2, 0, 0); pixt3 = pixThresholdTo4bpp(pixt2, 6, 1); pixt4 = pixRemoveColormap(pixt3, REMOVE_CMAP_TO_GRAYSCALE); pixRasterop(pixd, 0, h, w, h, PIX_SRC, pixt4, 0, 0); pixSaveTiled(pixt3, pixa, 1, 0, 20, 0); pixDisplay(pixt3, 0, 100); pixWrite("/tmp/junk-4-6.png", pixt3, IFF_PNG); pixDestroy(&pixt3); pixDestroy(&pixt4); pixt3 = pixThresholdTo4bpp(pixt2, 5, 1); pixt4 = pixRemoveColormap(pixt3, REMOVE_CMAP_TO_GRAYSCALE); pixRasterop(pixd, 0, 2 * h, w, h, PIX_SRC, pixt4, 0, 0); pixSaveTiled(pixt3, pixa, 1, 0, 20, 0); pixDisplay(pixt3, 0, 200); pixWrite("/tmp/junk-4-5.png", pixt3, IFF_PNG); pixDestroy(&pixt3); pixDestroy(&pixt4); pixt3 = pixThresholdTo4bpp(pixt2, 4, 1); pixt4 = pixRemoveColormap(pixt3, REMOVE_CMAP_TO_GRAYSCALE); pixRasterop(pixd, 0, 3 * h, w, h, PIX_SRC, pixt4, 0, 0); pixSaveTiled(pixt3, pixa, 1, 0, 20, 0); pixDisplay(pixt3, 0, 300); pixWrite("/tmp/junk-4-4.png", pixt3, IFF_PNG); pixDestroy(&pixt3); pixDestroy(&pixt4); pixt3 = pixThresholdTo4bpp(pixt2, 3, 1); pixt4 = pixRemoveColormap(pixt3, REMOVE_CMAP_TO_GRAYSCALE); pixRasterop(pixd, 0, 4 * h, w, h, PIX_SRC, pixt4, 0, 0); pixSaveTiled(pixt3, pixa, 1, 1, 20, 0); pixDisplay(pixt3, 0, 400); pixWrite("/tmp/junk-4-3.png", pixt3, IFF_PNG); pixDestroy(&pixt3); pixDestroy(&pixt4); pixt3 = pixThresholdTo4bpp(pixt2, 2, 1); pixt4 = pixRemoveColormap(pixt3, REMOVE_CMAP_TO_GRAYSCALE); pixRasterop(pixd, 0, 5 * h, w, h, PIX_SRC, pixt4, 0, 0); pixDisplay(pixt3, 0, 500); pixSaveTiled(pixt3, pixa, 1, 0, 20, 0); pixWrite("/tmp/junk-4-2.png", pixt3, IFF_PNG); pixDestroy(&pixt3); pixDestroy(&pixt4); pixWrite("/tmp/junk-all.png", pixd, IFF_PNG); pixDestroy(&pixd); pixd2 = pixaDisplay(pixa, 0, 0); pixDisplay(pixd2, 100, 100); pixWrite("/tmp/junkpixd2.jpg", pixd2, IFF_JFIF_JPEG); pixDestroy(&pixd2); pixaDestroy(&pixa); #if 0 /* upscale 2x and threshold to 1 bpp; e.g., use test8.jpg */ startTimer(); pixd = pixScaleGray2xLIThresh(pixs, THRESHOLD); fprintf(stderr, " time for scale/dither = %7.3f sec\n", stopTimer()); pixWrite(fileout, pixd, IFF_PNG); pixDestroy(&pixd); #endif #if 0 /* upscale 4x and threshold to 1 bpp; e.g., use test8.jpg */ startTimer(); pixd = pixScaleGray4xLIThresh(pixs, THRESHOLD); fprintf(stderr, " time for scale/dither = %7.3f sec\n", stopTimer()); pixWrite(fileout, pixd, IFF_PNG); pixDestroy(&pixd); #endif boxDestroy(&box); pixDestroy(&pixt); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixs); /* Thresholding with fixed and arbitrary bin boundaries */ pixa = pixaCreate(0); pixs = pixRead("stampede2.jpg"); pixSaveTiled(pixs, pixa, 1, 1, 20, 8); pixt = pixThresholdTo4bpp(pixs, 5, 1); pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixDestroy(&pixt); pixt = pixThresholdTo4bpp(pixs, 7, 1); cmap = pixGetColormap(pixt); pixcmapWriteStream(stderr, cmap); pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixDestroy(&pixt); pixt = pixThresholdTo4bpp(pixs, 11, 1); pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixDestroy(&pixt); pixSaveTiled(pixs, pixa, 1, 1, 20, 8); str = "45 75 115 185"; pixt = pixThresholdGrayArb(pixs, str, 8, 0, 0, 0); pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixDestroy(&pixt); str = "38 65 85 115 160 210"; pixt = pixThresholdGrayArb(pixs, str, 8, 0, 1, 1); cmap = pixGetColormap(pixt); pixcmapWriteStream(stderr, cmap); pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixDestroy(&pixt); str = "38 60 75 90 110 130 155 185 208 239"; pixt = pixThresholdGrayArb(pixs, str, 8, 0, 0, 0); pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixDestroy(&pixt); pixSaveTiled(pixs, pixa, 1, 1, 20, 8); str = "45 75 115 185"; pixt = pixThresholdGrayArb(pixs, str, 0, 1, 0, 1); pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixDestroy(&pixt); str = "38 65 85 115 160 210"; pixt = pixThresholdGrayArb(pixs, str, 0, 1, 0, 1); cmap = pixGetColormap(pixt); pixcmapWriteStream(stderr, cmap); pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixDestroy(&pixt); str = "38 60 75 90 110 130 155 185 208 239"; pixt = pixThresholdGrayArb(pixs, str, 4, 1, 0, 1); pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixDestroy(&pixt); pixd3 = pixaDisplay(pixa, 0, 0); pixDisplay(pixd3, 100, 100); pixWrite("/tmp/junkpixd3.jpg", pixd3, IFF_JFIF_JPEG); pixDestroy(&pixd3); pixaDestroy(&pixa); pixDestroy(&pixs); return 0; }
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) { 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) { BOX *box; PIX *pixs, *pix1, *pix2, *pix3, *pix4, *pix5, *pix6, *pix7; PIX *pix8, *pix9; PIXCMAP *cmap; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; pixs = pixRead("lucasta-frag.jpg"); /* Convert to 4 bpp with 6 levels and a colormap */ pix1 = pixThresholdTo4bpp(pixs, 6, 1); /* Color some non-white pixels, preserving antialiasing, and * adding these colors to the colormap */ box = boxCreate(120, 30, 200, 200); pixColorGray(pix1, box, L_PAINT_DARK, 220, 0, 0, 255); regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 0 */ pixDisplayWithTitle(pix1, 0, 0, NULL, rp->display); boxDestroy(&box); /* Scale up by 1.5; losing the colormap */ pix2 = pixScale(pix1, 1.5, 1.5); regTestWritePixAndCheck(rp, pix2, IFF_JFIF_JPEG); /* 1 */ pixDisplayWithTitle(pix2, 0, 0, NULL, rp->display); /* Octcube quantize using the same colormap */ startTimer(); cmap = pixGetColormap(pix1); pix3 = pixOctcubeQuantFromCmap(pix2, cmap, MIN_DEPTH, LEVEL, L_EUCLIDEAN_DISTANCE); fprintf(stderr, "Time to re-quantize to cmap = %7.3f sec\n", stopTimer()); regTestWritePixAndCheck(rp, pix3, IFF_PNG); /* 2 */ pixDisplayWithTitle(pix3, 0, 0, NULL, rp->display); /* Convert the quantized image to rgb */ pix4 = pixConvertTo32(pix3); /* Re-quantize using median cut */ pix5 = pixMedianCutQuant(pix4, 0); regTestWritePixAndCheck(rp, pix5, IFF_PNG); /* 3 */ pixDisplayWithTitle(pix5, 0, 0, NULL, rp->display); /* Re-quantize to few colors using median cut */ pix6 = pixFewColorsMedianCutQuantMixed(pix4, 30, 30, 100, 0, 0, 0); regTestWritePixAndCheck(rp, pix6, IFF_PNG); /* 4 */ pixDisplayWithTitle(pix6, 0, 0, NULL, rp->display); /* Octcube quantize mixed with gray */ startTimer(); pix7 = pixOctcubeQuantMixedWithGray(pix2, 4, 5, 5); fprintf(stderr, "Time to re-quantize mixed = %7.3f sec\n", stopTimer()); regTestWritePixAndCheck(rp, pix7, IFF_PNG); /* 5 */ pixDisplayWithTitle(pix7, 0, 0, NULL, rp->display); /* Fixed octcube quantization */ startTimer(); pix8 = pixFixedOctcubeQuant256(pix2, 0); fprintf(stderr, "Time to re-quantize 256 = %7.3f sec\n", stopTimer()); regTestWritePixAndCheck(rp, pix8, IFF_PNG); /* 6 */ pixDisplayWithTitle(pix8, 0, 0, NULL, rp->display); /* Remove unused colors */ startTimer(); pix9 = pixCopy(NULL, pix8); pixRemoveUnusedColors(pix9); fprintf(stderr, "Time to remove unused colors = %7.3f sec\n", stopTimer()); regTestWritePixAndCheck(rp, pix9, IFF_PNG); /* 7 */ pixDisplayWithTitle(pix8, 0, 0, NULL, rp->display); /* Compare before and after colors removed */ regTestComparePix(rp, pix8, pix9); /* 8 */ pixDestroy(&pixs); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); pixDestroy(&pix5); pixDestroy(&pix6); pixDestroy(&pix7); pixDestroy(&pix8); pixDestroy(&pix9); return regTestCleanup(rp); }