Esempio n. 1
0
int main(int    argc,
         char **argv)
{
char        *filein, *fileout;
char         bigbuf[512];
l_int32      iplot, same;
l_float32    gam;
l_float64    gamma[] = {.5, 1.0, 1.5, 2.0, 2.5, -1.0};
GPLOT       *gplot;
NUMA        *na, *nax;
PIX         *pixs, *pixd;
static char  mainName[] = "gammatest";

    if (argc != 4)
        return ERROR_INT(" Syntax:  gammatest filein gam fileout", mainName, 1);

    lept_mkdir("lept/gamma");

    filein = argv[1];
    gam = atof(argv[2]);
    fileout = argv[3];
    if ((pixs = pixRead(filein)) == NULL)
        return ERROR_INT("pixs not made", mainName, 1);

    startTimer();
    pixd = pixGammaTRC(NULL, pixs, gam, MINVAL, MAXVAL);
    fprintf(stderr, "Time for gamma: %7.3f sec\n", stopTimer());
    pixGammaTRC(pixs, pixs, gam, MINVAL, MAXVAL);
    pixEqual(pixs, pixd, &same);
    if (!same)
        fprintf(stderr, "Error in pixGammaTRC!\n");
    pixWrite(fileout, pixs, IFF_JFIF_JPEG);
    pixDestroy(&pixs);

    na = numaGammaTRC(gam, MINVAL, MAXVAL);
    gplotSimple1(na, GPLOT_PNG, "/tmp/lept/gamma/trc", "gamma trc");
    l_fileDisplay("/tmp/lept/gamma/trc.png", 100, 100, 1.0);
    numaDestroy(&na);

        /* Plot gamma TRC maps */
    gplot = gplotCreate("/tmp/lept/gamma/corr", GPLOT_PNG,
                        "Mapping function for gamma correction",
                        "value in", "value out");
    nax = numaMakeSequence(0.0, 1.0, 256);
    for (iplot = 0; gamma[iplot] >= 0.0; iplot++) {
        na = numaGammaTRC(gamma[iplot], 30, 215);
        sprintf(bigbuf, "gamma = %3.1f", gamma[iplot]);
        gplotAddPlot(gplot, nax, na, GPLOT_LINES, bigbuf);
        numaDestroy(&na);
    }
    gplotMakeOutput(gplot);
    gplotDestroy(&gplot);
    l_fileDisplay("/tmp/lept/gamma/corr.png", 100, 100, 1.0);
    numaDestroy(&nax);
    return 0;
}
Esempio n. 2
0
int main(int    argc,
         char **argv)
{
char        *filein, *fileout;
char         bigbuf[512];
l_int32      iplot;
l_float32    factor;    /* scaled width of atan curve */
l_float32    fact[] = {.2, 0.4, 0.6, 0.8, 1.0, -1.0};
GPLOT       *gplot;
NUMA        *na, *nax;
PIX         *pixs;
static char  mainName[] = "contrasttest";

    if (argc != 4)
        return ERROR_INT(" Syntax:  contrasttest filein factor fileout",
               mainName, 1);

    lept_mkdir("lept/contrast");

    filein = argv[1];
    factor = atof(argv[2]);
    fileout = argv[3];

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

    na = numaContrastTRC(factor);
    gplotSimple1(na, GPLOT_PNG, "/tmp/lept/contrast/trc1", "contrast trc");
    l_fileDisplay("/tmp/lept/contrast/trc1.png", 0, 100, 1.0);
    numaDestroy(&na);

         /* Plot contrast TRC maps */
    nax = numaMakeSequence(0.0, 1.0, 256);
    gplot = gplotCreate("/tmp/lept/contrast/trc2", GPLOT_PNG,
        "Atan mapping function for contrast enhancement",
        "value in", "value out");
    for (iplot = 0; fact[iplot] >= 0.0; iplot++) {
        na = numaContrastTRC(fact[iplot]);
        sprintf(bigbuf, "factor = %3.1f", fact[iplot]);
        gplotAddPlot(gplot, nax, na, GPLOT_LINES, bigbuf);
        numaDestroy(&na);
    }
    gplotMakeOutput(gplot);
    gplotDestroy(&gplot);
    l_fileDisplay("/tmp/lept/contrast/trc2.png", 600, 100, 1.0);
    numaDestroy(&nax);

        /* Apply the input contrast enhancement */
    pixContrastTRC(pixs, pixs, factor);
    pixWrite(fileout, pixs, IFF_PNG);
    pixDestroy(&pixs);
    return 0;
}
Esempio n. 3
0
static void
DisplayMapHistogram(L_AMAP      *m,
                    PIXCMAP     *cmap,
                    const char  *rootname)
{
char      buf[128];
l_int32   i, n, ival;
l_uint32  val32;
NUMA     *na;
RB_TYPE   key;
RB_TYPE  *pval;

    n = pixcmapGetCount(cmap);
    na = numaCreate(n);
    for (i = 0; i < n; i++) {
        pixcmapGetColor32(cmap, i, &val32);
        key.utype = val32;
        pval = l_amapFind(m, key);
        if (pval) {
            ival = pval->itype;
            numaAddNumber(na, ival);
        }
    }
    gplotSimple1(na, GPLOT_PNG, rootname, NULL);
    snprintf(buf, sizeof(buf), "%s.png", rootname);
    l_fileDisplay(buf, 700, 0, 1.0);
    numaDestroy(&na);
    return;
}
Esempio n. 4
0
static void
DisplayMapRGBHistogram(L_AMAP      *m,
                       const char  *rootname)
{
char          buf[128];
l_int32       ncolors, npix, ival, maxn, maxn2;
l_uint32      val32, maxcolor;
L_AMAP_NODE  *n;
NUMA         *na;

    fprintf(stderr, "\n --------------- Display RGB histogram ------------\n");
    na = numaCreate(0);
    ncolors = npix = 0;
    maxn = 0;
    maxcolor = 0;
    n = l_amapGetFirst(m);
    while (n) {
        ncolors++;
        ival = n->value.itype;
        if (ival > maxn) {
            maxn = ival;
            maxcolor = n->key.utype;
        }
        numaAddNumber(na, ival);
        npix += ival;
        n = l_amapGetNext(n);
    }
    fprintf(stderr, " Num colors = %d, Num pixels = %d\n", ncolors, npix);
    fprintf(stderr, " Color %x has count %d\n", maxcolor, maxn);
    maxn2 = amapGetCountForColor(m, maxcolor);
    if (maxn != maxn2)
        fprintf(stderr, " Error: maxn2 = %d; not equal to %d\n", maxn, maxn2);
    gplotSimple1(na, GPLOT_PNG, rootname, NULL);
    snprintf(buf, sizeof(buf), "%s.png", rootname);
    l_fileDisplay(buf, 1400, 0, 1.0);
    numaDestroy(&na);
    return;
}
Esempio n. 5
0
l_int32 main(int    argc,
             char **argv)
{
    L_ASET     *set;
    L_DNA      *da1, *da2, *da3, *da4, *da5, *da6, *da7, *da8, *dav, *dac;
    L_DNAHASH  *dahash;
    NUMA       *nav, *nac;
    PTA        *pta1, *pta2, *pta3;
    SARRAY     *sa1, *sa2, *sa3, *sa4;

    lept_mkdir("lept/hash");

#if 1
    /* Test string hashing with aset */
    fprintf(stderr, "Set results with string hashing:\n");
    sa1 = BuildShortStrings(3, 0);
    sa2 = BuildShortStrings(3, 1);
    fprintf(stderr, "  size with unique strings: %d\n", sarrayGetCount(sa1));
    fprintf(stderr, "  size with dups: %d\n", sarrayGetCount(sa2));
    startTimer();
    set = l_asetCreateFromSarray(sa2);
    fprintf(stderr, "  time to make set: %5.3f sec\n", stopTimer());
    fprintf(stderr, "  size of set without dups: %d\n", l_asetSize(set));
    l_asetDestroy(&set);
    startTimer();
    sa3 = sarrayRemoveDupsByAset(sa2);
    fprintf(stderr, "  time to remove dups: %5.3f sec\n", stopTimer());
    fprintf(stderr, "  size without dups = %d\n", sarrayGetCount(sa3));
    startTimer();
    sa4 = sarrayIntersectionByAset(sa1, sa2);
    fprintf(stderr, "  time to intersect: %5.3f sec\n", stopTimer());
    fprintf(stderr, "  intersection size = %d\n", sarrayGetCount(sa4));
    sarrayDestroy(&sa3);
    sarrayDestroy(&sa4);

    /* Test sarray set operations with dna hash.
     * We use the same hash function as is used with aset. */
    fprintf(stderr, "\nDna hash results for sarray:\n");
    fprintf(stderr, "  size with unique strings: %d\n", sarrayGetCount(sa1));
    fprintf(stderr, "  size with dups: %d\n", sarrayGetCount(sa2));
    startTimer();
    dahash = l_dnaHashCreateFromSarray(sa2);
    fprintf(stderr, "  time to make hashmap: %5.3f sec\n", stopTimer());
    fprintf(stderr, "  entries in hashmap with dups: %d\n",
            l_dnaHashGetTotalCount(dahash));
    l_dnaHashDestroy(&dahash);
    startTimer();
    sarrayRemoveDupsByHash(sa2, &sa3, NULL);
    fprintf(stderr, "  time to remove dups: %5.3f sec\n", stopTimer());
    fprintf(stderr, "  size without dups = %d\n", sarrayGetCount(sa3));
    startTimer();
    sa4 = sarrayIntersectionByHash(sa1, sa2);
    fprintf(stderr, "  time to intersect: %5.3f sec\n", stopTimer());
    fprintf(stderr, "  intersection size = %d\n", sarrayGetCount(sa4));
    sarrayDestroy(&sa3);
    sarrayDestroy(&sa4);
    sarrayDestroy(&sa1);
    sarrayDestroy(&sa2);
#endif

#if 1
    /* Test point hashing with aset.
     * Enter all points within a 1500 x 1500 image in pta1, and include
     * 450,000 duplicates in pta2.  With this pt hashing function,
     * there are no hash collisions among any of the 400 million pixel
     * locations in a 20000 x 20000 image. */
    pta1 = BuildPointSet(1500, 1500, 0);
    pta2 = BuildPointSet(1500, 1500, 1);
    fprintf(stderr, "\nSet results for pta:\n");
    fprintf(stderr, "  pta1 size with unique points: %d\n", ptaGetCount(pta1));
    fprintf(stderr, "  pta2 size with dups: %d\n", ptaGetCount(pta2));
    startTimer();
    pta3 = ptaRemoveDupsByAset(pta2);
    fprintf(stderr, "  Time to remove dups: %5.3f sec\n", stopTimer());
    fprintf(stderr, "  size without dups = %d\n", ptaGetCount(pta3));
    ptaDestroy(&pta3);

    startTimer();
    pta3 = ptaIntersectionByAset(pta1, pta2);
    fprintf(stderr, "  Time to intersect: %5.3f sec\n", stopTimer());
    fprintf(stderr, "  intersection size = %d\n", ptaGetCount(pta3));
    ptaDestroy(&pta1);
    ptaDestroy(&pta2);
    ptaDestroy(&pta3);
#endif

#if 1
    /* Test pta set operations with dna hash, using the same pt hashing
     * function.  Although there are no collisions in 20K x 20K images,
     * the dna hash implementation works properly even if there are some. */
    pta1 = BuildPointSet(1500, 1500, 0);
    pta2 = BuildPointSet(1500, 1500, 1);
    fprintf(stderr, "\nDna hash results for pta:\n");
    fprintf(stderr, "  pta1 size with unique points: %d\n", ptaGetCount(pta1));
    fprintf(stderr, "  pta2 size with dups: %d\n", ptaGetCount(pta2));
    startTimer();
    ptaRemoveDupsByHash(pta2, &pta3, NULL);
    fprintf(stderr, "  Time to remove dups: %5.3f sec\n", stopTimer());
    fprintf(stderr, "  size without dups = %d\n", ptaGetCount(pta3));
    ptaDestroy(&pta3);

    startTimer();
    pta3 = ptaIntersectionByHash(pta1, pta2);
    fprintf(stderr, "  Time to intersect: %5.3f sec\n", stopTimer());
    fprintf(stderr, "  intersection size = %d\n", ptaGetCount(pta3));
    ptaDestroy(&pta1);
    ptaDestroy(&pta2);
    ptaDestroy(&pta3);
#endif

    /* Test dna set and histo operations using dna hash */
#if 1
    fprintf(stderr, "\nDna hash results for dna:\n");
    da1 = l_dnaMakeSequence(0.0, 0.125, 8000);
    da2 = l_dnaMakeSequence(300.0, 0.125, 8000);
    da3 = l_dnaMakeSequence(600.0, 0.125, 8000);
    da4 = l_dnaMakeSequence(900.0, 0.125, 8000);
    da5 = l_dnaMakeSequence(1200.0, 0.125, 8000);
    l_dnaJoin(da1, da2, 0, -1);
    l_dnaJoin(da1, da3, 0, -1);
    l_dnaJoin(da1, da4, 0, -1);
    l_dnaJoin(da1, da5, 0, -1);
    l_dnaRemoveDupsByHash(da1, &da6, &dahash);
    l_dnaHashDestroy(&dahash);
    fprintf(stderr, "  dna size with dups = %d\n", l_dnaGetCount(da1));
    fprintf(stderr, "  dna size of unique numbers = %d\n", l_dnaGetCount(da6));
    l_dnaMakeHistoByHash(da1, &dahash, &dav, &dac);
    nav = l_dnaConvertToNuma(dav);
    nac = l_dnaConvertToNuma(dac);
    fprintf(stderr, "  dna number of histo points = %d\n", l_dnaGetCount(dac));
    gplotSimpleXY1(nav, nac, GPLOT_IMPULSES, GPLOT_PNG,
                   "/tmp/lept/hash/histo", "Histo");
    da7 = l_dnaIntersectionByHash(da2, da3);
    fprintf(stderr, "  dna number of points: da2 = %d, da3 = %d\n",
            l_dnaGetCount(da2), l_dnaGetCount(da3));
    fprintf(stderr, "  dna number of da2/da3 intersection points = %d\n",
            l_dnaGetCount(da7));
    l_fileDisplay("/tmp/lept/hash/histo.png", 700, 100, 1.0);
    l_dnaDestroy(&da1);
    l_dnaDestroy(&da2);
    l_dnaDestroy(&da3);
    l_dnaDestroy(&da4);
    l_dnaDestroy(&da5);
    l_dnaDestroy(&da6);
    l_dnaDestroy(&da7);
    l_dnaDestroy(&dac);
    l_dnaDestroy(&dav);
    l_dnaHashDestroy(&dahash);
    numaDestroy(&nav);
    numaDestroy(&nac);
#endif

#if 1
    da1 = l_dnaMakeSequence(0, 3, 10000);
    da2 = l_dnaMakeSequence(0, 5, 10000);
    da3 = l_dnaMakeSequence(0, 7, 10000);
    l_dnaJoin(da1, da2, 0, -1);
    l_dnaJoin(da1, da3, 0, -1);

    fprintf(stderr, "\nDna results using set:\n");
    fprintf(stderr, "  da1 count: %d\n", l_dnaGetCount(da1));
    set = l_asetCreateFromDna(da1);
    fprintf(stderr, "  da1 set size: %d\n\n", l_asetSize(set));
    l_asetDestroy(&set);

    da4 = l_dnaUnionByAset(da2, da3);
    fprintf(stderr, "  da4 count: %d\n", l_dnaGetCount(da4));
    set = l_asetCreateFromDna(da4);
    fprintf(stderr, "  da4 set size: %d\n\n", l_asetSize(set));
    l_asetDestroy(&set);

    da5 = l_dnaIntersectionByAset(da1, da2);
    fprintf(stderr, "  da5 count: %d\n", l_dnaGetCount(da5));
    set = l_asetCreateFromDna(da5);
    fprintf(stderr, "  da5 set size: %d\n\n", l_asetSize(set));
    l_asetDestroy(&set);

    da6 = l_dnaMakeSequence(100000, 11, 5000);
    l_dnaJoin(da6, da1, 0, -1);
    fprintf(stderr, "  da6 count: %d\n", l_dnaGetCount(da6));
    set = l_asetCreateFromDna(da6);
    fprintf(stderr, "  da6 set size: %d\n\n", l_asetSize(set));
    l_asetDestroy(&set);

    da7 = l_dnaIntersectionByAset(da6, da3);
    fprintf(stderr, "  da7 count: %d\n", l_dnaGetCount(da7));
    set = l_asetCreateFromDna(da7);
    fprintf(stderr, "  da7 set size: %d\n\n", l_asetSize(set));
    l_asetDestroy(&set);

    da8 = l_dnaRemoveDupsByAset(da1);
    fprintf(stderr, "  da8 count: %d\n\n", l_dnaGetCount(da8));

    l_dnaDestroy(&da1);
    l_dnaDestroy(&da2);
    l_dnaDestroy(&da3);
    l_dnaDestroy(&da4);
    l_dnaDestroy(&da5);
    l_dnaDestroy(&da6);
    l_dnaDestroy(&da7);
    l_dnaDestroy(&da8);
#endif

    return 0;
}
int main(int    argc,
         char **argv)
{
l_int32      type, comptype, d1, d2, same, first, last;
l_float32    fract, diff, rmsdiff;
char        *filein1, *filein2, *fileout;
GPLOT       *gplot;
NUMA        *na1, *na2;
PIX         *pixs1, *pixs2, *pixd;
static char  mainName[] = "comparetest";

    if (argc != 5)
        return ERROR_INT(" Syntax:  comparetest filein1 filein2 type fileout",
                         mainName, 1);

    filein1 = argv[1];
    filein2 = argv[2];
    type = atoi(argv[3]);
    pixd = NULL;
    fileout = argv[4];
    l_pngSetReadStrip16To8(0);

    if ((pixs1 = pixRead(filein1)) == NULL)
        return ERROR_INT("pixs1 not made", mainName, 1);
    if ((pixs2 = pixRead(filein2)) == NULL)
        return ERROR_INT("pixs2 not made", mainName, 1);
    d1 = pixGetDepth(pixs1);
    d2 = pixGetDepth(pixs2);

    if (d1 == 1 && d2 == 1) {
        pixEqual(pixs1, pixs2, &same);
        if (same) {
            fprintf(stderr, "Images are identical\n");
            pixd = pixCreateTemplate(pixs1);  /* write empty pix for diff */
        }
        else {
            if (type == 0)
                comptype = L_COMPARE_XOR;
            else
                comptype = L_COMPARE_SUBTRACT;
            pixCompareBinary(pixs1, pixs2, comptype, &fract, &pixd);
            fprintf(stderr, "Fraction of different pixels: %10.6f\n", fract);
        }
        pixWrite(fileout, pixd, IFF_PNG);
    }
    else {
        if (type == 0)
            comptype = L_COMPARE_ABS_DIFF;
        else
            comptype = L_COMPARE_SUBTRACT;
        pixCompareGrayOrRGB(pixs1, pixs2, comptype, GPLOT_PNG, &same, &diff,
                            &rmsdiff, &pixd);
        if (type == 0) {
            if (same)
                fprintf(stderr, "Images are identical\n");
            else {
                fprintf(stderr, "Images differ: <diff> = %10.6f\n", diff);
                fprintf(stderr, "               <rmsdiff> = %10.6f\n", rmsdiff);
            }
        }
        else {  /* subtraction */
            if (same)
                fprintf(stderr, "pixs2 strictly greater than pixs1\n");
            else {
                fprintf(stderr, "Images differ: <diff> = %10.6f\n", diff);
                fprintf(stderr, "               <rmsdiff> = %10.6f\n", rmsdiff);
            }
        }
        if (d1 != 16)
            pixWrite(fileout, pixd, IFF_JFIF_JPEG);
        else
            pixWrite(fileout, pixd, IFF_PNG);

        if (d1 != 16 && !same) {
            na1 = pixCompareRankDifference(pixs1, pixs2, 1);
            if (na1) {
                fprintf(stderr, "na1[150] = %20.10f\n", na1->array[150]);
                fprintf(stderr, "na1[200] = %20.10f\n", na1->array[200]);
                fprintf(stderr, "na1[250] = %20.10f\n", na1->array[250]);
                numaGetNonzeroRange(na1, 0.00005, &first, &last);
                fprintf(stderr, "Nonzero diff range: first = %d, last = %d\n",
                        first, last);
                na2 = numaClipToInterval(na1, first, last);
                gplot = gplotCreate("/tmp/lept/comp/rank", GPLOT_PNG,
                                    "Pixel Rank Difference",
                                    "pixel val difference", "rank");
                gplotAddPlot(gplot, NULL, na2, GPLOT_LINES, "rank");
                gplotMakeOutput(gplot);
                gplotDestroy(&gplot);
                l_fileDisplay("/tmp/lept/comp/rank.png", 100, 100);
                numaDestroy(&na1);
                numaDestroy(&na2);
            }
        }
    }

    pixDestroy(&pixs1);
    pixDestroy(&pixs2);
    pixDestroy(&pixd);
    return 0;
}
Esempio n. 7
0
int main(int    argc,
         char **argv)
{
l_int32      i, w, h, d, rotflag;
PIX         *pixs, *pixt, *pixd;
l_float32    angle, deg2rad, pops, ang;
char        *filein, *fileout;
static char  mainName[] = "rotatetest1";

    if (argc != 4)
        return ERROR_INT(" Syntax:  rotatetest1 filein angle fileout",
                         mainName, 1);

    filein = argv[1];
    angle = atof(argv[2]);
    fileout = argv[3];
    deg2rad = 3.1415926535 / 180.;

    lept_mkdir("lept/rotate");

    if ((pixs = pixRead(filein)) == NULL)
        return ERROR_INT("pix not made", mainName, 1);
    if (pixGetDepth(pixs) == 1) {
        pixt = pixScaleToGray3(pixs);
        pixDestroy(&pixs);
        pixs = pixAddBorderGeneral(pixt, 1, 0, 1, 0, 255);
        pixDestroy(&pixt);
    }

    pixGetDimensions(pixs, &w, &h, &d);
    fprintf(stderr, "w = %d, h = %d\n", w, h);

#if 0
        /* repertory of rotation operations to choose from */
    pixd = pixRotateAM(pixs, deg2rad * angle, L_BRING_IN_WHITE);
    pixd = pixRotateAMColor(pixs, deg2rad * angle, 0xffffff00);
    pixd = pixRotateAMColorFast(pixs, deg2rad * angle, 255);
    pixd = pixRotateAMCorner(pixs, deg2rad * angle, L_BRING_IN_WHITE);
    pixd = pixRotateShear(pixs, w /2, h / 2, deg2rad * angle,
                          L_BRING_IN_WHITE);
    pixd = pixRotate3Shear(pixs, w /2, h / 2, deg2rad * angle,
                           L_BRING_IN_WHITE);
    pixRotateShearIP(pixs, w / 2, h / 2, deg2rad * angle); pixd = pixs;
#endif

#if 0
        /* timing of shear rotation */
    for (i = 0; i < NITERS; i++) {
        pixd = pixRotateShear(pixs, (i * w) / NITERS,
                              (i * h) / NITERS, deg2rad * angle,
                              L_BRING_IN_WHITE);
        pixDisplay(pixd, 100 + 20 * i, 100 + 20 * i);
        pixDestroy(&pixd);
    }
#endif

#if 0
        /* timing of in-place shear rotation */
    for (i = 0; i < NITERS; i++) {
        pixRotateShearIP(pixs, w/2, h/2, deg2rad * angle, L_BRING_IN_WHITE);
/*        pixRotateShearCenterIP(pixs, deg2rad * angle, L_BRING_IN_WHITE); */
        pixDisplay(pixs, 100 + 20 * i, 100 + 20 * i);
    }
    pixd = pixs;
    if (pixGetDepth(pixd) == 1)
        pixWrite(fileout, pixd, IFF_PNG);
    else
        pixWrite(fileout, pixd, IFF_JFIF_JPEG);
    pixDestroy(&pixs);
#endif

#if 0
        /* timing of various rotation operations (choose) */
    startTimer();
    w = pixGetWidth(pixs);
    h = pixGetHeight(pixs);
    for (i = 0; i < NTIMES; i++) {
        pixd = pixRotateShearCenter(pixs, deg2rad * angle, L_BRING_IN_WHITE);
        pixDestroy(&pixd);
    }
    pops = (l_float32)(w * h * NTIMES / 1000000.) / stopTimer();
    fprintf(stderr, "vers. 1, mpops: %f\n", pops);
    startTimer();
    w = pixGetWidth(pixs);
    h = pixGetHeight(pixs);
    for (i = 0; i < NTIMES; i++) {
        pixRotateShearIP(pixs, w/2, h/2, deg2rad * angle, L_BRING_IN_WHITE);
    }
    pops = (l_float32)(w * h * NTIMES / 1000000.) / stopTimer();
    fprintf(stderr, "shear, mpops: %f\n", pops);
    pixWrite(fileout, pixs, IFF_PNG);
    for (i = 0; i < NTIMES; i++) {
        pixRotateShearIP(pixs, w/2, h/2, -deg2rad * angle, L_BRING_IN_WHITE);
    }
    pixWrite("/usr/tmp/junkout", pixs, IFF_PNG);
#endif

#if 0
        /* area-mapping rotation operations */
    pixd = pixRotateAM(pixs, deg2rad * angle, L_BRING_IN_WHITE);
/*    pixd = pixRotateAMColorFast(pixs, deg2rad * angle, 255); */
    if (pixGetDepth(pixd) == 1)
        pixWrite(fileout, pixd, IFF_PNG);
    else
        pixWrite(fileout, pixd, IFF_JFIF_JPEG);
#endif

#if 0
        /* compare the standard area-map color rotation with
         * the fast area-map color rotation, on a pixel basis */
    {
    PIX    *pix1, *pix2;
    NUMA   *nar, *nag, *nab, *naseq;
    GPLOT  *gplot;

    startTimer();
    pix1 = pixRotateAMColor(pixs, 0.12, 0xffffff00);
    fprintf(stderr, " standard color rotate: %7.2f sec\n", stopTimer());
    pixWrite("/tmp/lept/rotate/color1.jpg", pix1, IFF_JFIF_JPEG);
    startTimer();
    pix2 = pixRotateAMColorFast(pixs, 0.12, 0xffffff00);
    fprintf(stderr, " fast color rotate: %7.2f sec\n", stopTimer());
    pixWrite("/tmp/lept/rotate/color2.jpg", pix2, IFF_JFIF_JPEG);
    pixd = pixAbsDifference(pix1, pix2);
    pixGetColorHistogram(pixd, 1, &nar, &nag, &nab);
    naseq = numaMakeSequence(0., 1., 256);
    gplot = gplotCreate("/tmp/lept/rotate/absdiff", GPLOT_PNG,
                        "Number vs diff", "diff", "number");
    gplotAddPlot(gplot, naseq, nar, GPLOT_POINTS, "red");
    gplotAddPlot(gplot, naseq, nag, GPLOT_POINTS, "green");
    gplotAddPlot(gplot, naseq, nab, GPLOT_POINTS, "blue");
    gplotMakeOutput(gplot);
    l_fileDisplay("/tmp/lept/rotate/absdiff.png", 100, 100, 1.0);
    pixDestroy(&pix1);
    pixDestroy(&pix2);
    pixDestroy(&pixd);
    numaDestroy(&nar);
    numaDestroy(&nag);
    numaDestroy(&nab);
    numaDestroy(&naseq);
    gplotDestroy(&gplot);
    }
#endif

        /* Do a succession of 180 7-degree rotations in a cw
         * direction, and unwind the result with another set in
         * a ccw direction.  Although there is a considerable amount
         * of distortion after successive rotations, after all
         * 360 rotations, the resulting image is restored to
         * its original pristine condition! */
#if 1
    rotflag = L_ROTATE_AREA_MAP;
/*    rotflag = L_ROTATE_SHEAR;     */
/*    rotflag = L_ROTATE_SAMPLING;   */
    ang = 7.0 * deg2rad;
    pixGetDimensions(pixs, &w, &h, NULL);
    pixd = pixRotate(pixs, ang, rotflag, L_BRING_IN_WHITE, w, h);
    pixWrite("/tmp/lept/rotate/rot7.png", pixd, IFF_PNG);
    for (i = 1; i < 180; i++) {
        pixs = pixd;
        pixd = pixRotate(pixs, ang, rotflag, L_BRING_IN_WHITE, w, h);
        if ((i % 30) == 0)  pixDisplay(pixd, 600, 0);
        pixDestroy(&pixs);
    }

    pixWrite("/tmp/lept/rotate/spin.png", pixd, IFF_PNG);
    pixDisplay(pixd, 0, 0);

    for (i = 0; i < 180; i++) {
        pixs = pixd;
        pixd = pixRotate(pixs, -ang, rotflag, L_BRING_IN_WHITE, w, h);
        if (i && (i % 30) == 0)  pixDisplay(pixd, 600, 500);
        pixDestroy(&pixs);
    }

    pixWrite("/tmp/lept/rotate/unspin.png", pixd, IFF_PNG);
    pixDisplay(pixd, 0, 500);
    pixDestroy(&pixd);
#endif

    return 0;
}
int main(int    argc,
         char **argv)
{
PIX         *pixs;
l_int32      d;
static char  mainName[] = "scaletest2";

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

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

    lept_mkdir("lept/scale");

#if 1
        /* Integer scale-to-gray functions */
    if (d == 1)
    {
    PIX  *pixd;

        pixd = pixScaleToGray2(pixs);
        pixWrite("/tmp/lept/scale/s2g_2x", pixd, IFF_PNG);
        pixDestroy(&pixd);
        pixd = pixScaleToGray3(pixs);
        pixWrite("/tmp/lept/scale/s2g_3x", pixd, IFF_PNG);
        pixDestroy(&pixd);
        pixd = pixScaleToGray4(pixs);
        pixWrite("/tmp/lept/scale/s2g_4x", pixd, IFF_PNG);
        pixDestroy(&pixd);
        pixd = pixScaleToGray6(pixs);
        pixWrite("/tmp/lept/scale/s2g_6x", pixd, IFF_PNG);
        pixDestroy(&pixd);
        pixd = pixScaleToGray8(pixs);
        pixWrite("/tmp/lept/scale/s2g_8x", pixd, IFF_PNG);
        pixDestroy(&pixd);
        pixd = pixScaleToGray16(pixs);
        pixWrite("/tmp/lept/scale/s2g_16x", pixd, IFF_PNG);
        pixDestroy(&pixd);
    }
#endif

#if 1
        /* Various non-integer scale-to-gray, compared with
	 * with different ways of getting similar results */
    if (d == 1)
    {
    PIX  *pixt, *pixd;

        pixd = pixScaleToGray8(pixs);
        pixWrite("/tmp/lept/scale/s2g_8.png", pixd, IFF_PNG);
        pixDestroy(&pixd);

        pixd = pixScaleToGray(pixs, 0.124);
        pixWrite("/tmp/lept/scale/s2g_124.png", pixd, IFF_PNG);
        pixDestroy(&pixd);

        pixd = pixScaleToGray(pixs, 0.284);
        pixWrite("/tmp/lept/scale/s2g_284.png", pixd, IFF_PNG);
        pixDestroy(&pixd);

        pixt = pixScaleToGray4(pixs);
        pixd = pixScaleBySampling(pixt, 284./250., 284./250.);
        pixWrite("/tmp/lept/scale/s2g_284.2.png", pixd, IFF_PNG);
        pixDestroy(&pixt);
        pixDestroy(&pixd);

        pixt = pixScaleToGray4(pixs);
        pixd = pixScaleGrayLI(pixt, 284./250., 284./250.);
        pixWrite("/tmp/lept/scale/s2g_284.3.png", pixd, IFF_PNG);
        pixDestroy(&pixt);
        pixDestroy(&pixd);

        pixt = pixScaleBinary(pixs, 284./250., 284./250.);
        pixd = pixScaleToGray4(pixt);
        pixWrite("/tmp/lept/scale/s2g_284.4.png", pixd, IFF_PNG);
        pixDestroy(&pixt);
        pixDestroy(&pixd);

        pixt = pixScaleToGray4(pixs);
        pixd = pixScaleGrayLI(pixt, 0.49, 0.49);
        pixWrite("/tmp/lept/scale/s2g_42.png", pixd, IFF_PNG);
        pixDestroy(&pixt);
        pixDestroy(&pixd);

        pixt = pixScaleToGray4(pixs);
        pixd = pixScaleSmooth(pixt, 0.49, 0.49);
        pixWrite("/tmp/lept/scale/s2g_4sm.png", pixd, IFF_PNG);
        pixDestroy(&pixt);
        pixDestroy(&pixd);

        pixt = pixScaleBinary(pixs, .16/.125, .16/.125);
        pixd = pixScaleToGray8(pixt);
        pixWrite("/tmp/lept/scale/s2g_16.png", pixd, IFF_PNG);
        pixDestroy(&pixt);
        pixDestroy(&pixd);

        pixd = pixScaleToGray(pixs, .16);
        pixWrite("/tmp/lept/scale/s2g_16.2.png", pixd, IFF_PNG);
        pixDestroy(&pixd);
    }
#endif

#if 1
        /* Antialiased (smoothed) reduction, along with sharpening */
    if (d != 1)
    {
    PIX *pixt1, *pixt2;
        startTimer();
        pixt1 = pixScaleSmooth(pixs, 0.154, 0.154);
        fprintf(stderr, "fast scale: %5.3f sec\n", stopTimer());
        pixDisplayWithTitle(pixt1, 0, 0, "smooth scaling", DISPLAY);
        pixWrite("/tmp/lept/scale/smooth1.png", pixt1, IFF_PNG);
        pixt2 = pixUnsharpMasking(pixt1, 1, 0.3);
        pixWrite("/tmp/lept/scale/smooth2.png", pixt2, IFF_PNG);
        pixDisplayWithTitle(pixt2, 200, 0, "sharp scaling", DISPLAY);
        pixDestroy(&pixt1);
        pixDestroy(&pixt2);
    }
#endif


#if 1
        /* Test a large range of scale-to-gray reductions */
    if (d == 1)
    {
    l_int32    i;
    l_float32  scale;
    PIX       *pixd;
        for (i = 2; i < 15; i++) {
            scale = 1. / (l_float32)i;
            startTimer();
            pixd = pixScaleToGray(pixs, scale);
            fprintf(stderr, "Time for scale %7.3f: %7.3f sec\n",
            scale, stopTimer());
            pixDisplayWithTitle(pixd, 75 * i, 100, "scaletogray", DISPLAY);
            pixDestroy(&pixd);
        }
        for (i = 8; i < 14; i++) {
            scale = 1. / (l_float32)(2 * i);
            startTimer();
            pixd = pixScaleToGray(pixs, scale);
            fprintf(stderr, "Time for scale %7.3f: %7.3f sec\n",
            scale, stopTimer());
            pixDisplayWithTitle(pixd, 100 * i, 600, "scaletogray", DISPLAY);
            pixDestroy(&pixd);
        }
    }
#endif


#if 1
        /* Test the same range of scale-to-gray mipmap reductions */
    if (d == 1)
    {
    l_int32    i;
    l_float32  scale;
    PIX       *pixd;
        for (i = 2; i < 15; i++) {
            scale = 1. / (l_float32)i;
            startTimer();
            pixd = pixScaleToGrayMipmap(pixs, scale);
            fprintf(stderr, "Time for scale %7.3f: %7.3f sec\n",
            scale, stopTimer());
            pixDisplayWithTitle(pixd, 75 * i, 100, "scale mipmap", DISPLAY);
            pixDestroy(&pixd);
        }
        for (i = 8; i < 12; i++) {
            scale = 1. / (l_float32)(2 * i);
            startTimer();
            pixd = pixScaleToGrayMipmap(pixs, scale);
            fprintf(stderr, "Time for scale %7.3f: %7.3f sec\n",
            scale, stopTimer());
            pixDisplayWithTitle(pixd, 100 * i, 600, "scale mipmap", DISPLAY);
            pixDestroy(&pixd);
        }
    }
#endif

#if 1
        /* Test several methods for antialiased reduction,
         * along with sharpening */
    if (d != 1)
    {
        PIX *pixt1, *pixt2, *pixt3, *pixt4, *pixt5, *pixt6, *pixt7;
        l_float32 SCALING = 0.27;
        l_int32   SIZE = 7;
        l_int32   smooth;
        l_float32 FRACT = 1.0;

        smooth = SIZE / 2;

        startTimer();
        pixt1 = pixScaleSmooth(pixs, SCALING, SCALING);
        fprintf(stderr, "fast scale: %5.3f sec\n", stopTimer());
        pixDisplayWithTitle(pixt1, 0, 0, "smooth scaling", DISPLAY);
        pixWrite("/tmp/lept/scale/sm_1.png", pixt1, IFF_PNG);
        pixt2 = pixUnsharpMasking(pixt1, 1, 0.3);
        pixDisplayWithTitle(pixt2, 150, 0, "sharpened scaling", DISPLAY);

        startTimer();
        pixt3 = pixBlockconv(pixs, smooth, smooth);
        pixt4 = pixScaleBySampling(pixt3, SCALING, SCALING);
        fprintf(stderr, "slow scale: %5.3f sec\n", stopTimer());
        pixDisplayWithTitle(pixt4, 200, 200, "sampled scaling", DISPLAY);
        pixWrite("/tmp/lept/scale/sm_2.png", pixt4, IFF_PNG);

        startTimer();
        pixt5 = pixUnsharpMasking(pixs, smooth, FRACT);
        pixt6 = pixBlockconv(pixt5, smooth, smooth);
        pixt7 = pixScaleBySampling(pixt6, SCALING, SCALING);
        fprintf(stderr, "very slow scale + sharp: %5.3f sec\n", stopTimer());
        pixDisplayWithTitle(pixt7, 500, 200, "sampled scaling", DISPLAY);
        pixWrite("/tmp/lept/scale/sm_3.jpg", pixt7, IFF_JFIF_JPEG);

        pixDestroy(&pixt1);
        pixDestroy(&pixt2);
        pixDestroy(&pixt3);
        pixDestroy(&pixt4);
        pixDestroy(&pixt5);
        pixDestroy(&pixt6);
        pixDestroy(&pixt7);
    }
#endif


#if 1
        /* Test the color scaling function, comparing the
         * special case of scaling factor 2.0 with the
         * general case. */
    if (d == 32)
    {
    PIX    *pix1, *pix2, *pixd;
    NUMA   *nar, *nag, *nab, *naseq;
    GPLOT  *gplot;

        startTimer();
        pix1 = pixScaleColorLI(pixs, 2.00001, 2.0);
        fprintf(stderr, " Time with regular LI: %7.3f\n", stopTimer());
        pixWrite("/tmp/lept/scale/color1.jpg", pix1, IFF_JFIF_JPEG);
        startTimer();
        pix2 = pixScaleColorLI(pixs, 2.0, 2.0);
        fprintf(stderr, " Time with 2x LI: %7.3f\n", stopTimer());
        pixWrite("/tmp/lept/scale/color2.jpg", pix2, IFF_JFIF_JPEG);

        pixd = pixAbsDifference(pix1, pix2);
        pixGetColorHistogram(pixd, 1, &nar, &nag, &nab);
        naseq = numaMakeSequence(0., 1., 256);
        gplot = gplotCreate("/tmp/lept/scale/c_absdiff", GPLOT_PNG,
                            "Number vs diff", "diff", "number");
        gplotSetScaling(gplot, GPLOT_LOG_SCALE_Y);
        gplotAddPlot(gplot, naseq, nar, GPLOT_POINTS, "red");
        gplotAddPlot(gplot, naseq, nag, GPLOT_POINTS, "green");
        gplotAddPlot(gplot, naseq, nab, GPLOT_POINTS, "blue");
        gplotMakeOutput(gplot);
        l_fileDisplay("/tmp/lept/scale/c_absdiff.png", 0, 100);
        pixDestroy(&pix1);
        pixDestroy(&pix2);
        pixDestroy(&pixd);
        numaDestroy(&naseq);
        numaDestroy(&nar);
        numaDestroy(&nag);
        numaDestroy(&nab);
        gplotDestroy(&gplot);
    }
#endif


#if 1
        /* Test the gray LI scaling function, comparing the
         * special cases of scaling factor 2.0 and 4.0 with the
         * general case */
    if (d == 8 || d == 32)
    {
    PIX    *pixt, *pix0, *pix1, *pix2, *pixd;
    NUMA   *nagray, *naseq;
    GPLOT  *gplot;

        if (d == 8)
            pixt = pixClone(pixs);
        else
            pixt = pixConvertRGBToGray(pixs, 0.33, 0.34, 0.33);
        pix0 = pixScaleGrayLI(pixt, 0.5, 0.5);

#if 1
        startTimer();
        pix1 = pixScaleGrayLI(pix0, 2.00001, 2.0);
        fprintf(stderr, " Time with regular LI 2x: %7.3f\n", stopTimer());
        startTimer();
        pix2 = pixScaleGrayLI(pix0, 2.0, 2.0);
        fprintf(stderr, " Time with 2x LI: %7.3f\n", stopTimer());
#else
        startTimer();
        pix1 = pixScaleGrayLI(pix0, 4.00001, 4.0);
        fprintf(stderr, " Time with regular LI 4x: %7.3f\n", stopTimer());
        startTimer();
        pix2 = pixScaleGrayLI(pix0, 4.0, 4.0);
        fprintf(stderr, " Time with 2x LI: %7.3f\n", stopTimer());
#endif
        pixWrite("/tmp/lept/scale/gray1", pix1, IFF_JFIF_JPEG);
        pixWrite("/tmp/lept/scale/gray2", pix2, IFF_JFIF_JPEG);

        pixd = pixAbsDifference(pix1, pix2);
        nagray = pixGetGrayHistogram(pixd, 1);
        naseq = numaMakeSequence(0., 1., 256);
        gplot = gplotCreate("/tmp/lept/scale/g_absdiff", GPLOT_PNG,
                            "Number vs diff", "diff", "number");
        gplotSetScaling(gplot, GPLOT_LOG_SCALE_Y);
        gplotAddPlot(gplot, naseq, nagray, GPLOT_POINTS, "gray");
        gplotMakeOutput(gplot);
        l_fileDisplay("/tmp/lept/scale/g_absdiff.png", 750, 100);
        pixDestroy(&pixt);
        pixDestroy(&pix0);
        pixDestroy(&pix1);
        pixDestroy(&pix2);
        pixDestroy(&pixd);
        numaDestroy(&naseq);
        numaDestroy(&nagray);
        gplotDestroy(&gplot);
    }
#endif

    pixDestroy(&pixs);
    return 0;
}
Esempio n. 9
0
int main(int    argc,
         char **argv)
{
    char        *str1, *str2, *pngname;
    l_int32      i;
    size_t       size1, size2;
    l_float32    x, y1, y2, pi;
    GPLOT       *gplot1, *gplot2, *gplot3, *gplot4, *gplot5;
    NUMA        *nax, *nay1, *nay2;
    static char  mainName[] = "plottest";

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

    lept_mkdir("lept/plot");

    /* Generate plot data */
    nax = numaCreate(0);
    nay1 = numaCreate(0);
    nay2 = numaCreate(0);
    pi = 3.1415926535;
    for (i = 0; i < 180; i++) {
        x = (pi / 180.) * i;
        y1 = (l_float32)sin(2.4 * x);
        y2 = (l_float32)cos(2.4 * x);
        numaAddNumber(nax, x);
        numaAddNumber(nay1, y1);
        numaAddNumber(nay2, y2);
    }

    /* Show the plot */
    gplot1 = gplotCreate("/tmp/lept/plot/set1", GPLOT_OUTPUT, "Example plots",
                         "theta", "f(theta)");
    gplotAddPlot(gplot1, nax, nay1, GPLOT_STYLE, "sin (2.4 * theta)");
    gplotAddPlot(gplot1, nax, nay2, GPLOT_STYLE, "cos (2.4 * theta)");
    gplotMakeOutput(gplot1);

    /* Also save the plot to png */
    gplot1->outformat = GPLOT_PNG;
    pngname = genPathname("/tmp/lept/plot", "set1.png");
    stringReplace(&gplot1->outname, pngname);
    gplotMakeOutput(gplot1);
    l_fileDisplay("/tmp/lept/plot/set1.png", 100, 100, 1.0);
    lept_free(pngname);

    /* Test gplot serialization */
    gplotWrite("/tmp/lept/plot/plot1.gp", gplot1);
    if ((gplot2 = gplotRead("/tmp/lept/plot/plot1.gp")) == NULL)
        return ERROR_INT("gplotRead failure!", mainName, 1);
    gplotWrite("/tmp/lept/plot/plot2.gp", gplot2);

    /* Are the two written gplot files the same? */
    str1 = (char *)l_binaryRead("/tmp/lept/plot/plot1.gp", &size1);
    str2 = (char *)l_binaryRead("/tmp/lept/plot/plot2.gp", &size2);
    if (size1 != size2)
        fprintf(stderr, "Error: size1 = %lu, size2 = %lu\n",
                (unsigned long)size1, (unsigned long)size2);
    else
        fprintf(stderr, "Correct: size1 = size2 = %lu\n", (unsigned long)size1);
    if (strcmp(str1, str2))
        fprintf(stderr, "Error: str1 != str2\n");
    else
        fprintf(stderr, "Correct: str1 == str2\n");
    lept_free(str1);
    lept_free(str2);

    /* Read from file and regenerate the plot */
    gplot3 = gplotRead("/tmp/lept/plot/plot2.gp");
    stringReplace(&gplot3->title , "Example plots regen");
    gplot3->outformat = GPLOT_PNG;
    gplotMakeOutput(gplot3);

    /* Build gplot but do not make the output formatted stuff */
    gplot4 = gplotCreate("/tmp/lept/plot/set2", GPLOT_OUTPUT,
                         "Example plots 2", "theta", "f(theta)");
    gplotAddPlot(gplot4, nax, nay1, GPLOT_STYLE, "sin (2.4 * theta)");
    gplotAddPlot(gplot4, nax, nay2, GPLOT_STYLE, "cos (2.4 * theta)");

    /* Write, read back, and generate the plot */
    gplotWrite("/tmp/lept/plot/plot4.gp", gplot4);
    if ((gplot5 = gplotRead("/tmp/lept/plot/plot4.gp")) == NULL)
        return ERROR_INT("gplotRead failure!", mainName, 1);
    gplotMakeOutput(gplot5);
    l_fileDisplay("/tmp/lept/plot/set2.png", 750, 100, 1.0);

    gplotDestroy(&gplot1);
    gplotDestroy(&gplot2);
    gplotDestroy(&gplot3);
    gplotDestroy(&gplot4);
    gplotDestroy(&gplot5);
    numaDestroy(&nax);
    numaDestroy(&nay1);
    numaDestroy(&nay2);
    return 0;
}