Leptonica 1.68
C Image Processing Library

scaletest2.c

Go to the documentation of this file.
00001 /*====================================================================*
00002  -  Copyright (C) 2001 Leptonica.  All rights reserved.
00003  -  This software is distributed in the hope that it will be
00004  -  useful, but with NO WARRANTY OF ANY KIND.
00005  -  No author or distributor accepts responsibility to anyone for the
00006  -  consequences of using this software, or for whether it serves any
00007  -  particular purpose or works at all, unless he or she says so in
00008  -  writing.  Everyone is granted permission to copy, modify and
00009  -  redistribute this source code, for commercial or non-commercial
00010  -  purposes, with the following restrictions: (1) the origin of this
00011  -  source code must not be misrepresented; (2) modified versions must
00012  -  be plainly marked as such; and (3) this notice may not be removed
00013  -  or altered from any source or modified source distribution.
00014  *====================================================================*/
00015 
00016 /*
00017  * scaletest2.c
00018  *
00019  *   Tests scale-to-gray, unsharp masking, smoothing, and color scaling
00020  */
00021 
00022 #include "allheaders.h"
00023 
00024 #define   DISPLAY      0    /* set to 1 to see the results */
00025 
00026 main(int    argc,
00027      char **argv)
00028 {
00029 PIX         *pixs;
00030 l_int32      d;
00031 static char  mainName[] = "scaletest2";
00032 
00033     if (argc != 2)
00034         return ERROR_INT(" Syntax:  scaletest2 filein", mainName, 1);
00035 
00036     if ((pixs = pixRead(argv[1])) == NULL)
00037         return ERROR_INT("pixs not made", mainName, 1);
00038     d = pixGetDepth(pixs);
00039             
00040 #if 1
00041         /* Integer scale-to-gray functions */
00042     if (d == 1)
00043     {
00044     PIX  *pixd;
00045 
00046         pixd = pixScaleToGray2(pixs);
00047         pixWrite("/tmp/s2g_2x", pixd, IFF_PNG);
00048         pixDestroy(&pixd);
00049         pixd = pixScaleToGray3(pixs);
00050         pixWrite("/tmp/s2g_3x", pixd, IFF_PNG);
00051         pixDestroy(&pixd);
00052         pixd = pixScaleToGray4(pixs);
00053         pixWrite("/tmp/s2g_4x", pixd, IFF_PNG);
00054         pixDestroy(&pixd);
00055         pixd = pixScaleToGray6(pixs);
00056         pixWrite("/tmp/s2g_6x", pixd, IFF_PNG);
00057         pixDestroy(&pixd);
00058         pixd = pixScaleToGray8(pixs);
00059         pixWrite("/tmp/s2g_8x", pixd, IFF_PNG);
00060         pixDestroy(&pixd);
00061         pixd = pixScaleToGray16(pixs);
00062         pixWrite("/tmp/s2g_16x", pixd, IFF_PNG);
00063         pixDestroy(&pixd);
00064     }
00065 #endif
00066 
00067 #if 1
00068         /* Various non-integer scale-to-gray, compared with
00069          * with different ways of getting similar results */
00070     if (d == 1)
00071     {
00072     PIX  *pixt, *pixd;
00073 
00074         pixd = pixScaleToGray8(pixs);
00075         pixWrite("/tmp/s2g_8.png", pixd, IFF_PNG);
00076         pixDestroy(&pixd);
00077 
00078         pixd = pixScaleToGray(pixs, 0.124);
00079         pixWrite("/tmp/s2g_124.png", pixd, IFF_PNG);
00080         pixDestroy(&pixd);
00081 
00082         pixd = pixScaleToGray(pixs, 0.284);
00083         pixWrite("/tmp/s2g_284.png", pixd, IFF_PNG);
00084         pixDestroy(&pixd);
00085 
00086         pixt = pixScaleToGray4(pixs);
00087         pixd = pixScaleBySampling(pixt, 284./250., 284./250.);
00088         pixWrite("/tmp/s2g_284.2.png", pixd, IFF_PNG);
00089         pixDestroy(&pixt);
00090         pixDestroy(&pixd);
00091 
00092         pixt = pixScaleToGray4(pixs);
00093         pixd = pixScaleGrayLI(pixt, 284./250., 284./250.);
00094         pixWrite("/tmp/s2g_284.3.png", pixd, IFF_PNG);
00095         pixDestroy(&pixt);
00096         pixDestroy(&pixd);
00097 
00098         pixt = pixScaleBinary(pixs, 284./250., 284./250.);
00099         pixd = pixScaleToGray4(pixt);
00100         pixWrite("/tmp/s2g_284.4.png", pixd, IFF_PNG);
00101         pixDestroy(&pixt);
00102         pixDestroy(&pixd);
00103 
00104         pixt = pixScaleToGray4(pixs);
00105         pixd = pixScaleGrayLI(pixt, 0.49, 0.49);
00106         pixWrite("/tmp/s2g_42.png", pixd, IFF_PNG);
00107         pixDestroy(&pixt);
00108         pixDestroy(&pixd);
00109 
00110         pixt = pixScaleToGray4(pixs);
00111         pixd = pixScaleSmooth(pixt, 0.49, 0.49);
00112         pixWrite("/tmp/s2g_4sm.png", pixd, IFF_PNG);
00113         pixDestroy(&pixt);
00114         pixDestroy(&pixd);
00115 
00116         pixt = pixScaleBinary(pixs, .16/.125, .16/.125);
00117         pixd = pixScaleToGray8(pixt);
00118         pixWrite("/tmp/s2g_16.png", pixd, IFF_PNG);
00119         pixDestroy(&pixt);
00120         pixDestroy(&pixd);
00121 
00122         pixd = pixScaleToGray(pixs, .16);
00123         pixWrite("/tmp/s2g_16.2.png", pixd, IFF_PNG);
00124         pixDestroy(&pixd);
00125     }
00126 #endif
00127 
00128 #if 1
00129         /* Antialiased (smoothed) reduction, along with sharpening */
00130     if (d != 1)
00131     {
00132     PIX *pixt1, *pixt2;
00133         startTimer();
00134         pixt1 = pixScaleSmooth(pixs, 0.154, 0.154);
00135         fprintf(stderr, "fast scale: %5.3f sec\n", stopTimer());
00136         pixDisplayWithTitle(pixt1, 0, 0, "smooth scaling", DISPLAY);
00137         pixWrite("/tmp/smooth1.png", pixt1, IFF_PNG);
00138         pixt2 = pixUnsharpMasking(pixt1, 1, 0.3);
00139         pixWrite("/tmp/smooth2.png", pixt2, IFF_PNG);
00140         pixDisplayWithTitle(pixt2, 200, 0, "sharp scaling", DISPLAY);
00141         pixDestroy(&pixt1);
00142         pixDestroy(&pixt2);
00143     }
00144 #endif
00145 
00146 
00147 #if 1
00148         /* Test a large range of scale-to-gray reductions */
00149     if (d == 1)
00150     {
00151     l_int32    i;
00152     l_float32  scale;
00153     PIX       *pixd;
00154         for (i = 2; i < 15; i++) {
00155             scale = 1. / (l_float32)i;
00156             startTimer();
00157             pixd = pixScaleToGray(pixs, scale);
00158             fprintf(stderr, "Time for scale %7.3f: %7.3f sec\n",
00159             scale, stopTimer());
00160             pixDisplayWithTitle(pixd, 75 * i, 100, "scaletogray", DISPLAY);
00161             pixDestroy(&pixd);
00162         }
00163         for (i = 8; i < 14; i++) {
00164             scale = 1. / (l_float32)(2 * i);
00165             startTimer();
00166             pixd = pixScaleToGray(pixs, scale);
00167             fprintf(stderr, "Time for scale %7.3f: %7.3f sec\n",
00168             scale, stopTimer());
00169             pixDisplayWithTitle(pixd, 100 * i, 600, "scaletogray", DISPLAY);
00170             pixDestroy(&pixd);
00171         }
00172     }
00173 #endif
00174 
00175 
00176 #if 1
00177         /* Test the same range of scale-to-gray mipmap reductions */
00178     if (d == 1)
00179     {
00180     l_int32    i;
00181     l_float32  scale;
00182     PIX       *pixd;
00183         for (i = 2; i < 15; i++) {
00184             scale = 1. / (l_float32)i;
00185             startTimer();
00186             pixd = pixScaleToGrayMipmap(pixs, scale);
00187             fprintf(stderr, "Time for scale %7.3f: %7.3f sec\n",
00188             scale, stopTimer());
00189             pixDisplayWithTitle(pixd, 75 * i, 100, "scale mipmap", DISPLAY);
00190             pixDestroy(&pixd);
00191         }
00192         for (i = 8; i < 12; i++) {
00193             scale = 1. / (l_float32)(2 * i);
00194             startTimer();
00195             pixd = pixScaleToGrayMipmap(pixs, scale);
00196             fprintf(stderr, "Time for scale %7.3f: %7.3f sec\n",
00197             scale, stopTimer());
00198             pixDisplayWithTitle(pixd, 100 * i, 600, "scale mipmap", DISPLAY);
00199             pixDestroy(&pixd);
00200         }
00201     }
00202 #endif
00203 
00204 #if 1
00205         /* Test several methods for antialiased reduction,
00206          * along with sharpening */
00207     if (d != 1)
00208     {
00209         PIX *pixt1, *pixt2, *pixt3, *pixt4, *pixt5, *pixt6, *pixt7;
00210         l_float32 SCALING = 0.27;
00211         l_int32   SIZE = 7;
00212         l_int32   smooth;
00213         l_float32 FRACT = 1.0;
00214 
00215         smooth = SIZE / 2;
00216 
00217         startTimer();
00218         pixt1 = pixScaleSmooth(pixs, SCALING, SCALING);
00219         fprintf(stderr, "fast scale: %5.3f sec\n", stopTimer());
00220         pixDisplayWithTitle(pixt1, 0, 0, "smooth scaling", DISPLAY);
00221         pixWrite("/tmp/sm_1.png", pixt1, IFF_PNG);
00222         pixt2 = pixUnsharpMasking(pixt1, 1, 0.3);
00223         pixDisplayWithTitle(pixt2, 150, 0, "sharpened scaling", DISPLAY);
00224 
00225         startTimer();
00226         pixt3 = pixBlockconv(pixs, smooth, smooth);
00227         pixt4 = pixScaleBySampling(pixt3, SCALING, SCALING);
00228         fprintf(stderr, "slow scale: %5.3f sec\n", stopTimer());
00229         pixDisplayWithTitle(pixt4, 200, 200, "sampled scaling", DISPLAY);
00230         pixWrite("/tmp/sm_2.png", pixt4, IFF_PNG);
00231 
00232         startTimer();
00233         pixt5 = pixUnsharpMasking(pixs, smooth, FRACT);
00234         pixt6 = pixBlockconv(pixt5, smooth, smooth);
00235         pixt7 = pixScaleBySampling(pixt6, SCALING, SCALING);
00236         fprintf(stderr, "very slow scale + sharp: %5.3f sec\n", stopTimer());
00237         pixDisplayWithTitle(pixt7, 500, 200, "sampled scaling", DISPLAY);
00238         pixWrite("/tmp/sm_3.jpg", pixt7, IFF_JFIF_JPEG);
00239 
00240         pixDestroy(&pixt1);
00241         pixDestroy(&pixt2);
00242         pixDestroy(&pixt3);
00243         pixDestroy(&pixt4);
00244         pixDestroy(&pixt5);
00245         pixDestroy(&pixt6);
00246         pixDestroy(&pixt7);
00247     }
00248 #endif
00249 
00250 
00251 #if 1
00252         /* Test the color scaling function, comparing the
00253          * special case of scaling factor 2.0 with the 
00254          * general case. */
00255     if (d == 32) 
00256     {
00257     PIX    *pix1, *pix2, *pixd;
00258     NUMA   *nar, *nag, *nab, *naseq;
00259     GPLOT  *gplot;
00260 
00261         startTimer();
00262         pix1 = pixScaleColorLI(pixs, 2.00001, 2.0);
00263         fprintf(stderr, " Time with regular LI: %7.3f\n", stopTimer());
00264         pixWrite("/tmp/color1.jpg", pix1, IFF_JFIF_JPEG);
00265         startTimer();
00266         pix2 = pixScaleColorLI(pixs, 2.0, 2.0);
00267         fprintf(stderr, " Time with 2x LI: %7.3f\n", stopTimer());
00268         pixWrite("/tmp/color2.jpg", pix2, IFF_JFIF_JPEG);
00269 
00270         pixd = pixAbsDifference(pix1, pix2);
00271         pixGetColorHistogram(pixd, 1, &nar, &nag, &nab);
00272         naseq = numaMakeSequence(0., 1., 256);
00273         gplot = gplotCreate("/tmp/plot_absdiff", GPLOT_X11, "Number vs diff",
00274                             "diff", "number");
00275         gplotSetScaling(gplot, GPLOT_LOG_SCALE_Y);
00276         gplotAddPlot(gplot, naseq, nar, GPLOT_POINTS, "red");
00277         gplotAddPlot(gplot, naseq, nag, GPLOT_POINTS, "green");
00278         gplotAddPlot(gplot, naseq, nab, GPLOT_POINTS, "blue");
00279         gplotMakeOutput(gplot);
00280         pixDestroy(&pix1);
00281         pixDestroy(&pix2);
00282         pixDestroy(&pixd);
00283         numaDestroy(&naseq);
00284         numaDestroy(&nar);
00285         numaDestroy(&nag);
00286         numaDestroy(&nab);
00287         gplotDestroy(&gplot);
00288     }
00289 #endif
00290 
00291 
00292 #if 1
00293         /* Test the gray LI scaling function, comparing the
00294          * special cases of scaling factor 2.0 and 4.0 with the 
00295          * general case */
00296     if (d == 8 || d == 32)
00297     {
00298     PIX    *pixt, *pix0, *pix1, *pix2, *pixd;
00299     NUMA   *nagray, *naseq;
00300     GPLOT  *gplot;
00301 
00302         if (d == 8)
00303             pixt = pixClone(pixs);
00304         else
00305             pixt = pixConvertRGBToGray(pixs, 0.33, 0.34, 0.33);
00306         pix0 = pixScaleGrayLI(pixt, 0.5, 0.5);
00307 
00308 #if 1
00309         startTimer();
00310         pix1 = pixScaleGrayLI(pix0, 2.00001, 2.0);
00311         fprintf(stderr, " Time with regular LI 2x: %7.3f\n", stopTimer());
00312         startTimer();
00313         pix2 = pixScaleGrayLI(pix0, 2.0, 2.0);
00314         fprintf(stderr, " Time with 2x LI: %7.3f\n", stopTimer());
00315 #else
00316         startTimer();
00317         pix1 = pixScaleGrayLI(pix0, 4.00001, 4.0);
00318         fprintf(stderr, " Time with regular LI 4x: %7.3f\n", stopTimer());
00319         startTimer();
00320         pix2 = pixScaleGrayLI(pix0, 4.0, 4.0);
00321         fprintf(stderr, " Time with 2x LI: %7.3f\n", stopTimer());
00322 #endif
00323         pixWrite("/tmp/gray1", pix1, IFF_JFIF_JPEG);
00324         pixWrite("/tmp/gray2", pix2, IFF_JFIF_JPEG);
00325 
00326         pixd = pixAbsDifference(pix1, pix2);
00327         nagray = pixGetGrayHistogram(pixd, 1);
00328         naseq = numaMakeSequence(0., 1., 256);
00329         gplot = gplotCreate("/tmp/g_absdiff", GPLOT_X11, "Number vs diff",
00330                             "diff", "number");
00331         gplotSetScaling(gplot, GPLOT_LOG_SCALE_Y);
00332         gplotAddPlot(gplot, naseq, nagray, GPLOT_POINTS, "gray");
00333         gplotMakeOutput(gplot);
00334         pixDestroy(&pixt);
00335         pixDestroy(&pix0);
00336         pixDestroy(&pix1);
00337         pixDestroy(&pix2);
00338         pixDestroy(&pixd);
00339         numaDestroy(&naseq);
00340         numaDestroy(&nagray);
00341         gplotDestroy(&gplot);
00342     }
00343 #endif
00344 
00345     pixDestroy(&pixs);
00346     return 0;
00347 }
00348 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines