Example #1
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);
}
Example #2
0
int main(int    argc,
         char **argv)
{
char         label[512];
l_int32      rval, gval, bval, w, h, i, j, rwhite, gwhite, bwhite, count;
l_uint32     pixel;
GPLOT       *gplot1, *gplot2;
NUMA        *naseq, *na;
NUMAA       *naa1, *naa2;
PIX         *pixs, *pixt, *pixt0, *pixt1, *pixt2;
PIX         *pixr, *pixg, *pixb;
PIXA        *pixa;
PIXCMAP     *cmap;
static char  mainName[] = "colorspacetest";

    if (argc != 2)
        return ERROR_INT(" Syntax:  colorspacetest filein", mainName, 1);

    if ((pixs = pixRead(argv[1])) == NULL)
        return ERROR_INT("pixs not made", mainName, 1);

        /* Generate colors by sampling hue with max sat and value.
         * This was used to make the color strip 19-colors.png.  */
    pixa = pixaCreate(19);
    for (i = 0; i < 19; i++) {
        convertHSVToRGB((240 * i / 18), 255, 255, &rval, &gval, &bval);
        composeRGBPixel(rval, gval, bval, &pixel);
        pixt1 = pixCreate(50, 100, 32);
        pixSetAllArbitrary(pixt1, pixel);
        pixaAddPix(pixa, pixt1, L_INSERT);
    }
    pixt2 = pixaDisplayTiledInRows(pixa, 32, 1100, 1.0, 0, 0, 0);
    pixDisplayWrite(pixt2, 1);
    pixDestroy(&pixt2);
    pixaDestroy(&pixa);

        /* Colorspace conversion in rgb */
    pixDisplayWrite(pixs, 1);
    pixt = pixConvertRGBToHSV(NULL, pixs);
    pixDisplayWrite(pixt, 1);
    pixConvertHSVToRGB(pixt, pixt);
    pixDisplayWrite(pixt, 1);
    pixDestroy(&pixt);

        /* Colorspace conversion on a colormap */
    pixt = pixOctreeQuantNumColors(pixs, 25, 0);
    pixDisplayWrite(pixt, 1);
    cmap = pixGetColormap(pixt);
    pixcmapWriteStream(stderr, cmap);
    pixcmapConvertRGBToHSV(cmap);
    pixcmapWriteStream(stderr, cmap);
    pixDisplayWrite(pixt, 1);
    pixcmapConvertHSVToRGB(cmap);
    pixcmapWriteStream(stderr, cmap);
    pixDisplayWrite(pixt, 1);
    pixDestroy(&pixt);

        /* Color content extraction */
    pixColorContent(pixs, 0, 0, 0, 0, &pixr, &pixg, &pixb);
    pixDisplayWrite(pixr, 1);
    pixDisplayWrite(pixg, 1);
    pixDisplayWrite(pixb, 1);
    pixDestroy(&pixr);
    pixDestroy(&pixg);
    pixDestroy(&pixb);

        /* Color content measurement */
    pixa = pixaCreate(20);
    naseq = numaMakeSequence(100, 5, 20);
    naa1 = numaaCreate(6);
    naa2 = numaaCreate(6);
    for (i = 0; i < 6; i++) {
        na = numaCreate(20);
        numaaAddNuma(naa1, na, L_COPY);
        numaaAddNuma(naa2, na, L_INSERT);
    }
    pixGetDimensions(pixs, &w, &h, NULL);
    for (i = 0; i < 20; i++) {
        rwhite = 100 + 5 * i;
        gwhite = 200 - 5 * i;
        bwhite = 150;
        pixt0 = pixGlobalNormRGB(NULL, pixs, rwhite, gwhite, bwhite, 255);
        pixaAddPix(pixa, pixt0, L_INSERT);
        pixt1 = pixColorMagnitude(pixs, rwhite, gwhite, bwhite,
                                  L_MAX_DIFF_FROM_AVERAGE_2);
        for (j = 0; j < 6; j++) {
            pixt2 = pixThresholdToBinary(pixt1, 30 + 10 * j);
            pixInvert(pixt2, pixt2);
            pixCountPixels(pixt2, &count, NULL);
            na = numaaGetNuma(naa1, j, L_CLONE);
            numaAddNumber(na, (l_float32)count / (l_float32)(w * h));
            numaDestroy(&na);
            pixDestroy(&pixt2);
        }
        pixDestroy(&pixt1);
        pixt1 = pixColorMagnitude(pixs, rwhite, gwhite, bwhite,
                                  L_MAX_MIN_DIFF_FROM_2);
        for (j = 0; j < 6; j++) {
            pixt2 = pixThresholdToBinary(pixt1, 30 + 10 * j);
            pixInvert(pixt2, pixt2);
            pixCountPixels(pixt2, &count, NULL);
            na = numaaGetNuma(naa2, j, L_CLONE);
            numaAddNumber(na, (l_float32)count / (l_float32)(w * h));
            numaDestroy(&na);
            pixDestroy(&pixt2);
        }
        pixDestroy(&pixt1);
    }
    gplot1 = gplotCreate("/tmp/junkplot1", GPLOT_X11,
                         "Fraction with given color (diff from average)",
                         "white point space for red", "amount of color");
    gplot2 = gplotCreate("/tmp/junkplot2", GPLOT_X11,
                         "Fraction with given color (min diff)",
                         "white point space for red", "amount of color");
    for (j = 0; j < 6; j++) {
        na = numaaGetNuma(naa1, j, L_CLONE);
        sprintf(label, "thresh %d", 30 + 10 * j);
        gplotAddPlot(gplot1, naseq, na, GPLOT_LINES, label);
        numaDestroy(&na);
        na = numaaGetNuma(naa2, j, L_CLONE);
        gplotAddPlot(gplot2, naseq, na, GPLOT_LINES, label);
        numaDestroy(&na);
    }
    gplotMakeOutput(gplot1);
    gplotMakeOutput(gplot2);
    gplotDestroy(&gplot1);
    gplotDestroy(&gplot2);
    pixt1 = pixaDisplayTiledAndScaled(pixa, 32, 250, 4, 0, 10, 2);
    pixWrite("/tmp/junkcolormag", pixt1, IFF_PNG);
    pixDisplayWithTitle(pixt1, 0, 100, "Color magnitude", 1);
    pixDestroy(&pixt1);
    pixaDestroy(&pixa);
    numaDestroy(&naseq);
    numaaDestroy(&naa1);
    numaaDestroy(&naa2);

    pixDisplayMultiple("/tmp/display/file*");

    pixDestroy(&pixs);
    return 0;
}
Example #3
0
static l_int32
TestImage(const char   *filename,
          l_int32       i,
          L_REGPARAMS  *rp)
{
char       buf[256];
l_int32    w, h;
l_float32  factor;
PIX       *pix, *pixs, *pixc, *pix32, *pixt, *pixd;
PIXA      *pixa;

    PROCNAME("TestImage");

    if ((pix = pixRead(filename)) == NULL) {
        rp->success = FALSE;
        return ERROR_INT("pix not made", procName, 1);
    }
    pixGetDimensions(pix, &w, &h, NULL);
    if (w > MAX_WIDTH) {
        factor = (l_float32)MAX_WIDTH / (l_float32)w;
        pixs = pixScale(pix, factor, factor);
    }
    else
        pixs = pixClone(pix);
    pixDestroy(&pix);

    pixa = pixaCreate(0);

        /* Median cut quantizer (no dither; 5 sigbits) */
    pixSaveTiled(pixs, pixa, 1.0, 1, SPACE, 32);
    pixc = pixMedianCutQuantGeneral(pixs, 0, 0, 16, 5, 1, 1);
    PixSave32(pixa, pixc, rp);
    pixc = pixMedianCutQuantGeneral(pixs, 0, 0, 128, 5, 1, 1);
    PixSave32(pixa, pixc, rp);
    pixc = pixMedianCutQuantGeneral(pixs, 0, 0, 256, 5, 1, 1);
    PixSave32(pixa, pixc, rp);

        /* Median cut quantizer (with dither; 5 sigbits) */
    pixSaveTiled(pixs, pixa, 1.0, 1, SPACE, 0);
    pixc = pixMedianCutQuantGeneral(pixs, 1, 0, 16, 5, 1, 1);
    PixSave32(pixa, pixc, rp);
    pixc = pixMedianCutQuantGeneral(pixs, 1, 0, 128, 5, 1, 1);
    PixSave32(pixa, pixc, rp);
    pixc = pixMedianCutQuantGeneral(pixs, 1, 0, 256, 5, 1, 1);
    PixSave32(pixa, pixc, rp);

        /* Median cut quantizer (no dither; 6 sigbits) */
    pixSaveTiled(pixs, pixa, 1.0, 1, SPACE, 32);
    pixc = pixMedianCutQuantGeneral(pixs, 0, 0, 16, 6, 1, 1);
    PixSave32(pixa, pixc, rp);
    pixc = pixMedianCutQuantGeneral(pixs, 0, 0, 128, 6, 1, 1);
    PixSave32(pixa, pixc, rp);
    pixc = pixMedianCutQuantGeneral(pixs, 0, 0, 256, 6, 1, 1);
    PixSave32(pixa, pixc, rp);

        /* Median cut quantizer (with dither; 6 sigbits) */
    pixSaveTiled(pixs, pixa, 1.0, 1, SPACE, 0);
    pixc = pixMedianCutQuantGeneral(pixs, 1, 0, 16, 6, 1, 1);
    PixSave32(pixa, pixc, rp);
    pixc = pixMedianCutQuantGeneral(pixs, 1, 0, 128, 6, 1, 1);
    PixSave32(pixa, pixc, rp);
    pixc = pixMedianCutQuantGeneral(pixs, 1, 0, 256, 6, 10, 1);
    PixSave32(pixa, pixc, rp);

        /* Median cut quantizer (mixed color/gray) */
    pixSaveTiled(pixs, pixa, 1.0, 1, SPACE, 0);
    pixc = pixMedianCutQuantMixed(pixs, 20, 10, 0, 0, 0);
    PixSave32(pixa, pixc, rp);
    pixc = pixMedianCutQuantMixed(pixs, 60, 20, 0, 0, 0);
    PixSave32(pixa, pixc, rp);
    pixc = pixMedianCutQuantMixed(pixs, 180, 40, 0, 0, 0);
    PixSave32(pixa, pixc, rp);

        /* Simple 256 cube octcube quantizer */
    pixSaveTiled(pixs, pixa, 1.0, 1, SPACE, 0);
    pixc = pixFixedOctcubeQuant256(pixs, 0);  /* no dither */
    PixSave32(pixa, pixc, rp);
    pixc = pixFixedOctcubeQuant256(pixs, 1);  /* dither */
    PixSave32(pixa, pixc, rp);

        /* 2-pass octree quantizer */
    pixSaveTiled(pixs, pixa, 1.0, 1, SPACE, 0);
    pixc = pixOctreeColorQuant(pixs, 128, 0);  /* no dither */
    PixSave32(pixa, pixc, rp);
    pixc = pixOctreeColorQuant(pixs, 240, 0);  /* no dither */
    PixSave32(pixa, pixc, rp);
    pixc = pixOctreeColorQuant(pixs, 128, 1);  /* dither */
    PixSave32(pixa, pixc, rp);
    pixc = pixOctreeColorQuant(pixs, 240, 1);  /* dither */
    PixSave32(pixa, pixc, rp);

        /* Simple adaptive quantization to 4 or 8 bpp, specifying ncolors */
    pixSaveTiled(pixs, pixa, 1.0, 1, SPACE, 0);
    pixc = pixOctreeQuantNumColors(pixs, 8, 0);    /* fixed: 8 colors */
    PixSave32(pixa, pixc, rp);
    pixc = pixOctreeQuantNumColors(pixs, 16, 0);   /* fixed: 16 colors */
    PixSave32(pixa, pixc, rp);
    pixc = pixOctreeQuantNumColors(pixs, 64, 0);   /* fixed: 64 colors */
    PixSave32(pixa, pixc, rp);
    pixc = pixOctreeQuantNumColors(pixs, 256, 0);   /* fixed: 256 colors */
    PixSave32(pixa, pixc, rp);

        /* Quantize to fully populated octree (RGB) at given level */
    pixSaveTiled(pixs, pixa, 1.0, 1, SPACE, 0);
    pixc = pixFixedOctcubeQuantGenRGB(pixs, 2);  /* level 2 */
    PixSave32(pixa, pixc, rp);
    pixc = pixFixedOctcubeQuantGenRGB(pixs, 3);  /* level 3 */
    PixSave32(pixa, pixc, rp);
    pixc = pixFixedOctcubeQuantGenRGB(pixs, 4);  /* level 4 */
    PixSave32(pixa, pixc, rp);
    pixc = pixFixedOctcubeQuantGenRGB(pixs, 5);  /* level 5 */
    PixSave32(pixa, pixc, rp);

        /* Generate 32 bpp RGB image with num colors <= 256 */
    pixt = pixOctreeQuantNumColors(pixs, 256, 0);   /* cmapped version */
    pix32 = pixRemoveColormap(pixt, REMOVE_CMAP_BASED_ON_SRC);

        /* Quantize image with few colors at fixed octree leaf level */
    pixSaveTiled(pixt, pixa, 1.0, 1, SPACE, 0);
    pixc = pixFewColorsOctcubeQuant1(pix32, 2);   /* level 2 */
    PixSave32(pixa, pixc, rp);
    pixc = pixFewColorsOctcubeQuant1(pix32, 3);   /* level 3 */
    PixSave32(pixa, pixc, rp);
    pixc = pixFewColorsOctcubeQuant1(pix32, 4);   /* level 4 */
    PixSave32(pixa, pixc, rp);
    pixc = pixFewColorsOctcubeQuant1(pix32, 5);   /* level 5 */
    PixSave32(pixa, pixc, rp);

        /* Quantize image by population */
    pixSaveTiled(pixt, pixa, 1.0, 1, SPACE, 0);
    pixc = pixOctreeQuantByPopulation(pixs, 3, 0);  /* level 3, no dither */
    PixSave32(pixa, pixc, rp);
    pixc = pixOctreeQuantByPopulation(pixs, 3, 1);  /* level 3, dither */
    PixSave32(pixa, pixc, rp);
    pixc = pixOctreeQuantByPopulation(pixs, 4, 0);  /* level 4, no dither */
    PixSave32(pixa, pixc, rp);
    pixc = pixOctreeQuantByPopulation(pixs, 4, 1);  /* level 4, dither */
    PixSave32(pixa, pixc, rp);

        /* Mixed color/gray octree quantizer */
    pixSaveTiled(pixt, pixa, 1.0, 1, SPACE, 0);
    pixc = pixOctcubeQuantMixedWithGray(pix32, 8, 64, 10);  /* max delta = 10 */
    PixSave32(pixa, pixc, rp);
    pixc = pixOctcubeQuantMixedWithGray(pix32, 8, 64, 30);  /* max delta = 30 */
    PixSave32(pixa, pixc, rp);
    pixc = pixOctcubeQuantMixedWithGray(pix32, 8, 64, 50);  /* max delta = 50 */
    PixSave32(pixa, pixc, rp);

        /* Run the high-level converter */
    pixSaveTiled(pixt, pixa, 1.0, 1, SPACE, 0);
    pixc = pixConvertRGBToColormap(pix32, 1);
    PixSave32(pixa, pixc, rp);

    pixDestroy(&pix32);
    pixDestroy(&pixt);

    pixd = pixaDisplay(pixa, 0, 0);
    pixDisplayWithTitle(pixd, 100, 100, NULL, rp->display);
    sprintf(buf, "/tmp/regout/disp.%d.jpg", i);
    pixWrite(buf, pixd, IFF_JFIF_JPEG);

    pixDestroy(&pixs);
    pixDestroy(&pixd);
    pixaDestroy(&pixa);

    return 0;
}
Example #4
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);
}
Example #5
0
int main(int    argc,
         char **argv)
{
PIX         *pixs, *pixc, *pixd;
PIXA        *pixa;
static char  mainName[] = "snapcolortest";

    if (argc != 1)
        return ERROR_INT(" Syntax:  snapcolortest", mainName, 1);

    if ((pixs = pixRead("Leptonica.jpg")) == NULL)
        return ERROR_INT("pixs not made", mainName, 1);
    pixa = pixaCreate(0);

        /* First, snap the color directly on the input rgb image. */
    pixSaveTiledOutline(pixs, pixa, 1.0, 1, 25, 2, 32);
    pixd = pixSnapColor(NULL, pixs, 0xffffff00, LEPTONICA_YELLOW, 30);
    pixSaveTiledOutline(pixd, pixa, 1.0, 0, 25, 2, 32);
    pixWrite("/tmp/logo1.jpg", pixd, IFF_JFIF_JPEG);
    pixDestroy(&pixd);

        /* Then make a colormapped version and snap the color */
    pixd = pixOctreeQuantNumColors(pixs, 250, 0);
    pixSaveTiledOutline(pixd, pixa, 1.0, 1, 25, 2, 32);
    pixSnapColor(pixd, pixd, 0xffffff00, LEPTONICA_YELLOW, 30);
    pixSaveTiledOutline(pixd, pixa, 1.0, 0, 25, 2, 32);
    pixWrite("/tmp/logo2.png", pixd, IFF_PNG);
    pixDestroy(&pixd);
    pixDestroy(&pixs);


        /* Set the background of the google searchbox to yellow.
         * The input image is colormapped with all 256 colors used. */
    if ((pixs = pixRead("google-searchbox.png")) == NULL)
        return ERROR_INT("pixs not made", mainName, 1);
    pixSaveTiledOutline(pixs, pixa, 1.0, 1, 25, 2, 32);
    pixd = pixSnapColor(NULL, pixs, 0xffffff00, LEPTONICA_YELLOW, 30);
    pixSaveTiledOutline(pixd, pixa, 1.0, 0, 25, 2, 32);
    pixWrite("/tmp/logo3.png", pixd, IFF_PNG);
    pixDestroy(&pixd);
    pixDestroy(&pixs);

        /* A couple of more, setting pixels near white to strange colors */
    pixs = pixRead("weasel4.11c.png");
    pixSaveTiledOutline(pixs, pixa, 1.0, 1, 25, 2, 32);
    pixd = pixSnapColor(NULL, pixs, 0xfefefe00, 0x80800000, 50);
    pixSaveTiledOutline(pixd, pixa, 1.0, 0, 25, 2, 32);
    pixDestroy(&pixs);
    pixDestroy(&pixd);

    pixs = pixRead("wyom.jpg");
    pixc = pixFixedOctcubeQuant256(pixs, 0);
    pixSaveTiledOutline(pixc, pixa, 1.0, 1, 25, 2, 32);
    pixd = pixSnapColor(NULL, pixc, 0xf0f0f000, 0x80008000, 100);
    pixSaveTiledOutline(pixd, pixa, 1.0, 0, 25, 2, 32);
    pixDestroy(&pixs);
    pixDestroy(&pixc);
    pixDestroy(&pixd);

        /* --- Display results --- */
    pixd = pixaDisplay(pixa, 0, 0);
    pixDisplay(pixd, 100, 100);
    pixWrite("/tmp/snap.jpg", pixd, IFF_PNG);
    pixDestroy(&pixd);
    pixaDestroy(&pixa);
    return 0;
}
Example #6
0
int main(int    argc,
         char **argv)
{
char          label[512];
l_int32       rval, gval, bval, w, h, i, j, rwhite, gwhite, bwhite, count;
l_uint32      pixel;
GPLOT        *gplot1, *gplot2;
NUMA         *naseq, *na;
NUMAA        *naa1, *naa2;
PIX          *pixs, *pixt, *pixt0, *pixt1, *pixt2;
PIX          *pixr, *pixg, *pixb;  /* for color content extraction */
PIXA         *pixa, *pixat;
PIXCMAP      *cmap;
L_REGPARAMS  *rp;

    if (regTestSetup(argc, argv, &rp))
        return 1;

        /* Generate a pdf of results when called with display */
    pixa = pixaCreate(0);

        /* Generate colors by sampling hue with max sat and value.
         * This image has been saved as 19-colors.png.  */
    pixat = pixaCreate(19);
    for (i = 0; i < 19; i++) {
        convertHSVToRGB((240 * i / 18), 255, 255, &rval, &gval, &bval);
        composeRGBPixel(rval, gval, bval, &pixel);
        pixt1 = pixCreate(50, 100, 32);
        pixSetAllArbitrary(pixt1, pixel);
        pixaAddPix(pixat, pixt1, L_INSERT);
    }
    pixt2 = pixaDisplayTiledInRows(pixat, 32, 1100, 1.0, 0, 0, 0);
    regTestWritePixAndCheck(rp, pixt2, IFF_PNG);  /* 0 */
    pixaAddPix(pixa, pixt2, L_INSERT);
    pixaDestroy(&pixat);

        /* Colorspace conversion in rgb */
    pixs = pixRead("wyom.jpg");
    pixaAddPix(pixa, pixs, L_INSERT);
    pixt = pixConvertRGBToHSV(NULL, pixs);
    regTestWritePixAndCheck(rp, pixt, IFF_JFIF_JPEG);  /* 1 */
    pixaAddPix(pixa, pixt, L_COPY);
    pixConvertHSVToRGB(pixt, pixt);
    regTestWritePixAndCheck(rp, pixt, IFF_JFIF_JPEG);  /* 2 */
    pixaAddPix(pixa, pixt, L_INSERT);

        /* Colorspace conversion on a colormap */
    pixt = pixOctreeQuantNumColors(pixs, 25, 0);
    regTestWritePixAndCheck(rp, pixt, IFF_JFIF_JPEG);  /* 3 */
    pixaAddPix(pixa, pixt, L_COPY);
    cmap = pixGetColormap(pixt);
    if (rp->display) pixcmapWriteStream(stderr, cmap);
    pixcmapConvertRGBToHSV(cmap);
    if (rp->display) pixcmapWriteStream(stderr, cmap);
    regTestWritePixAndCheck(rp, pixt, IFF_JFIF_JPEG);  /* 4 */
    pixaAddPix(pixa, pixt, L_COPY);
    pixcmapConvertHSVToRGB(cmap);
    if (rp->display) pixcmapWriteStream(stderr, cmap);
    regTestWritePixAndCheck(rp, pixt, IFF_JFIF_JPEG);  /* 5 */
    pixaAddPix(pixa, pixt, L_INSERT);

        /* Color content extraction */
    pixColorContent(pixs, 0, 0, 0, 0, &pixr, &pixg, &pixb);
    regTestWritePixAndCheck(rp, pixr, IFF_JFIF_JPEG);  /* 6 */
    pixaAddPix(pixa, pixr, L_INSERT);
    regTestWritePixAndCheck(rp, pixg, IFF_JFIF_JPEG);  /* 7 */
    pixaAddPix(pixa, pixg, L_INSERT);
    regTestWritePixAndCheck(rp, pixb, IFF_JFIF_JPEG);  /* 8 */
    pixaAddPix(pixa, pixb, L_INSERT);

        /* Color content measurement.  This tests the global
         * mapping of (r,g,b) --> (white), for 20 different
         * values of (r,g,b).   For each mappings, we compute
         * the color magnitude and threshold it at six values.
         * For each of those six thresholds, we plot the
         * fraction of pixels that exceeds the threshold
         * color magnitude, where the red value (mapped to
         * white) goes between 100 and 195.  */
    pixat = pixaCreate(20);
    naseq = numaMakeSequence(100, 5, 20);
    naa1 = numaaCreate(6);
    naa2 = numaaCreate(6);
    for (i = 0; i < 6; i++) {
        na = numaCreate(20);
        numaaAddNuma(naa1, na, L_COPY);
        numaaAddNuma(naa2, na, L_INSERT);
    }
    pixGetDimensions(pixs, &w, &h, NULL);
    for (i = 0; i < 20; i++) {
        rwhite = 100 + 5 * i;
        gwhite = 200 - 5 * i;
        bwhite = 150;
        pixt0 = pixGlobalNormRGB(NULL, pixs, rwhite, gwhite, bwhite, 255);
        pixaAddPix(pixat, pixt0, L_INSERT);
        pixt1 = pixColorMagnitude(pixs, rwhite, gwhite, bwhite,
                                  L_MAX_DIFF_FROM_AVERAGE_2);
        for (j = 0; j < 6; j++) {
            pixt2 = pixThresholdToBinary(pixt1, 30 + 10 * j);
            pixInvert(pixt2, pixt2);
            pixCountPixels(pixt2, &count, NULL);
            na = numaaGetNuma(naa1, j, L_CLONE);
            numaAddNumber(na, (l_float32)count / (l_float32)(w * h));
            numaDestroy(&na);
            pixDestroy(&pixt2);
        }
        pixDestroy(&pixt1);
        pixt1 = pixColorMagnitude(pixs, rwhite, gwhite, bwhite,
                                  L_MAX_MIN_DIFF_FROM_2);
        for (j = 0; j < 6; j++) {
            pixt2 = pixThresholdToBinary(pixt1, 30 + 10 * j);
            pixInvert(pixt2, pixt2);
            pixCountPixels(pixt2, &count, NULL);
            na = numaaGetNuma(naa2, j, L_CLONE);
            numaAddNumber(na, (l_float32)count / (l_float32)(w * h));
            numaDestroy(&na);
            pixDestroy(&pixt2);
        }
        pixDestroy(&pixt1);
    }
    gplot1 = gplotCreate("/tmp/regout/colorspace.10", GPLOT_PNG,
                         "Fraction with given color (diff from average)",
                         "white point space for red", "amount of color");
    gplot2 = gplotCreate("/tmp/regout/colorspace.11", GPLOT_PNG,
                         "Fraction with given color (min diff)",
                         "white point space for red", "amount of color");
    for (j = 0; j < 6; j++) {
        na = numaaGetNuma(naa1, j, L_CLONE);
        sprintf(label, "thresh %d", 30 + 10 * j);
        gplotAddPlot(gplot1, naseq, na, GPLOT_LINES, label);
        numaDestroy(&na);
        na = numaaGetNuma(naa2, j, L_CLONE);
        gplotAddPlot(gplot2, naseq, na, GPLOT_LINES, label);
        numaDestroy(&na);
    }
    gplotMakeOutput(gplot1);
    gplotMakeOutput(gplot2);
    gplotDestroy(&gplot1);
    gplotDestroy(&gplot2);
    pixt1 = pixaDisplayTiledAndScaled(pixat, 32, 250, 4, 0, 10, 2);
    regTestWritePixAndCheck(rp, pixt1, IFF_JFIF_JPEG);  /* 9 */
    pixaAddPix(pixa, pixt1, L_INSERT);
    pixDisplayWithTitle(pixt1, 0, 100, "Color magnitude", rp->display);
    pixaDestroy(&pixat);
    numaDestroy(&naseq);
    numaaDestroy(&naa1);
    numaaDestroy(&naa2);

        /* Give gnuplot time to write out the files */
#ifndef  _WIN32
    sleep(1);
#else
    Sleep(1000);
#endif  /* _WIN32 */

        /* Save as golden files, or check against them */
    regTestCheckFile(rp, "/tmp/regout/colorspace.10.png");  /* 10 */
    regTestCheckFile(rp, "/tmp/regout/colorspace.11.png");  /* 11 */

    if (rp->display) {
        pixt = pixRead("/tmp/regout/colorspace.10.png");
        pixaAddPix(pixa, pixt, L_INSERT);
        pixt = pixRead("/tmp/regout/colorspace.11.png");
        pixaAddPix(pixa, pixt, L_INSERT);
        pixaConvertToPdf(pixa, 0, 1.0, 0, 0, "colorspace tests",
                         "/tmp/regout/colorspace.pdf");
        L_INFO("Output pdf: /tmp/regout/colorspace.pdf\n", rp->testname);
    }
    pixaDestroy(&pixa);

    return regTestCleanup(rp);
}