Leptonica 1.68
C Image Processing Library

adaptmaptest.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  * adaptmaptest.c
00018  *
00019  *   Generates adaptive mappings in both gray and color, testing
00020  *   individual parts.
00021  *
00022  *   e.g., use with wet-day.jpg
00023  */
00024 
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include "allheaders.h"
00028 
00029 
00030 #define  SIZE_X        10
00031 #define  SIZE_Y        30
00032 #define  BINTHRESH     50
00033 #define  MINCOUNT      30
00034 
00035 #define  BGVAL         200
00036 #define  SMOOTH_X      2
00037 #define  SMOOTH_Y      1
00038 
00039    /* Location of image region in wet-day.jpg */
00040 #define  XS     151
00041 #define  YS     225
00042 #define  WS     913
00043 #define  HS     1285
00044 
00045 
00046 main(int    argc,
00047      char **argv)
00048 {
00049 l_int32      w, h, d;
00050 PIX         *pixs, *pixc, *pixg, *pixgm, *pixm, *pixmi, *pixd, *pixd2;
00051 PIX         *pixmr, *pixmg, *pixmb, *pixmri, *pixmgi, *pixmbi;
00052 PIX         *pixim;
00053 PIXA        *pixa;
00054 char        *filein;
00055 static char  mainName[] = "adaptmaptest";
00056 
00057     if (argc != 2)
00058         exit(ERROR_INT(" Syntax:  adaptmaptest filein", mainName, 1));
00059 
00060     filein = argv[1];
00061 
00062     if ((pixs = pixRead(filein)) == NULL)
00063         exit(ERROR_INT("pix not made", mainName, 1));
00064     pixGetDimensions(pixs, &w, &h, &d);
00065     if (d != 8 && d != 32)
00066         exit(ERROR_INT("pix not 8 or 32 bpp", mainName, 1));
00067     pixDisplayWrite(NULL, -1);
00068     pixa = pixaCreate(0);
00069     pixSaveTiled(pixs, pixa, 1, 1, 20, 32);
00070     pixDisplayWrite(pixs, 1);
00071 
00072     if (d == 32) {
00073         pixc = pixClone(pixs);
00074         pixg = pixConvertRGBToGray(pixs, 0.33, 0.34, 0.33);
00075     } else {
00076         pixc = pixConvertTo32(pixs);
00077         pixg = pixClone(pixs);
00078     }
00079     pixSaveTiled(pixg, pixa, 1, 0, 20, 32);
00080     pixDisplayWrite(pixg, 1);
00081     
00082 #if 1
00083         /* Process in grayscale */
00084     startTimer();
00085     pixim = NULL;
00086     pixim = pixCreate(w, h, 1);
00087     pixRasterop(pixim, XS, YS, WS, HS, PIX_SET, NULL, 0, 0);
00088     pixGetBackgroundGrayMap(pixg, pixim, SIZE_X, SIZE_Y,
00089                             BINTHRESH, MINCOUNT, &pixgm);
00090     fprintf(stderr, "time for gray adaptmap gen: %7.3f\n", stopTimer());
00091     pixWrite("/tmp/junkpixgm1.png", pixgm, IFF_PNG);
00092     pixSaveTiled(pixgm, pixa, 1, 1, 20, 32);
00093     pixDisplayWrite(pixgm, 1);
00094 
00095     startTimer();
00096     pixmi = pixGetInvBackgroundMap(pixgm, BGVAL, SMOOTH_X, SMOOTH_Y);
00097     fprintf(stderr, "time for gray inv map generation: %7.3f\n", stopTimer());
00098     pixWrite("/tmp/junkpixmi1.png", pixmi, IFF_PNG);
00099     pixSaveTiled(pixmi, pixa, 1, 0, 20, 32);
00100     pixDisplayWrite(pixmi, 1);
00101 
00102     startTimer();
00103     pixd = pixApplyInvBackgroundGrayMap(pixg, pixmi, SIZE_X, SIZE_Y);
00104     fprintf(stderr, "time to apply gray inv map: %7.3f\n", stopTimer());
00105     pixWrite("/tmp/junkpixd1.jpg", pixd, IFF_JFIF_JPEG);
00106     pixSaveTiled(pixd, pixa, 1, 0, 20, 32);
00107     pixDisplayWrite(pixd, 1);
00108 
00109     pixd2 = pixGammaTRCMasked(NULL, pixd, pixim, 1.0, 0, 190);
00110     pixInvert(pixim, pixim);
00111     pixGammaTRCMasked(pixd2, pixd2, pixim, 1.0, 60, 190);
00112     pixWrite("/tmp/junkpixo1.jpg", pixd2, IFF_JFIF_JPEG);
00113     pixSaveTiled(pixd2, pixa, 1, 0, 20, 32);
00114     pixDisplayWrite(pixd2, 1);
00115     pixDestroy(&pixim);
00116     pixDestroy(&pixgm);
00117     pixDestroy(&pixmi);
00118     pixDestroy(&pixd);
00119     pixDestroy(&pixd2);
00120 #endif
00121 
00122 #if 1
00123         /* Process in color */
00124     startTimer();
00125     pixmr = pixmg = pixmb = NULL;
00126     pixim = pixCreate(w, h, 1);
00127     pixRasterop(pixim, XS, YS, WS, HS, PIX_SET, NULL, 0, 0);
00128     pixGetBackgroundRGBMap(pixc, pixim, NULL, SIZE_X, SIZE_Y,
00129                            BINTHRESH, MINCOUNT,
00130                            &pixmr, &pixmg, &pixmb);
00131     fprintf(stderr, "time for color adaptmap gen: %7.3f\n", stopTimer());
00132     pixWrite("/tmp/junkpixmr.png", pixmr, IFF_PNG);
00133     pixWrite("/tmp/junkpixmg.png", pixmg, IFF_PNG);
00134     pixWrite("/tmp/junkpixmb.png", pixmb, IFF_PNG);
00135 
00136     startTimer();
00137     pixmri = pixGetInvBackgroundMap(pixmr, BGVAL, SMOOTH_X, SMOOTH_Y);
00138     pixmgi = pixGetInvBackgroundMap(pixmg, BGVAL, SMOOTH_X, SMOOTH_Y);
00139     pixmbi = pixGetInvBackgroundMap(pixmb, BGVAL, SMOOTH_X, SMOOTH_Y);
00140     fprintf(stderr, "time for color inv map generation: %7.3f\n", stopTimer());
00141     pixWrite("/tmp/junkpixmri.png", pixmri, IFF_PNG);
00142     pixWrite("/tmp/junkpixmgi.png", pixmgi, IFF_PNG);
00143     pixWrite("/tmp/junkpixmbi.png", pixmbi, IFF_PNG);
00144 
00145     startTimer();
00146     pixd = pixApplyInvBackgroundRGBMap(pixc, pixmri, pixmgi, pixmbi,
00147                                        SIZE_X, SIZE_Y);
00148     fprintf(stderr, "time to apply color inv maps: %7.3f\n", stopTimer());
00149     pixWrite("/tmp/junkpixd2.jpg", pixd, IFF_JFIF_JPEG);
00150     pixSaveTiled(pixd, pixa, 1, 1, 20, 32);
00151     pixDisplayWrite(pixd, 1);
00152 
00153     pixd2 = pixGammaTRCMasked(NULL, pixd, pixim, 1.0, 0, 190);
00154     pixInvert(pixim, pixim);
00155     pixGammaTRCMasked(pixd2, pixd2, pixim, 1.0, 60, 190);
00156     pixWrite("/tmp/junkpixo2.jpg", pixd2, IFF_JFIF_JPEG);
00157     pixSaveTiled(pixd2, pixa, 1, 0, 20, 32);
00158     pixDisplayWrite(pixd2, 1);
00159     pixDestroy(&pixmr);
00160     pixDestroy(&pixmg);
00161     pixDestroy(&pixmb);
00162     pixDestroy(&pixmri);
00163     pixDestroy(&pixmgi);
00164     pixDestroy(&pixmbi);
00165     pixDestroy(&pixim);
00166     pixDestroy(&pixd);
00167     pixDestroy(&pixd2);
00168 #endif
00169 
00170 #if 1
00171         /* Process in either gray or color, depending on the source */
00172     startTimer();
00173     pixim = pixCreate(w, h, 1);
00174     pixRasterop(pixim, XS, YS, WS, HS, PIX_SET, NULL, 0, 0);
00175 /*    pixd = pixBackgroundNorm(pixs, pixim, NULL,SIZE_X, SIZE_Y,
00176                                BINTHRESH, MINCOUNT,
00177                                BGVAL, SMOOTH_X, SMOOTH_Y); */
00178     pixd = pixBackgroundNorm(pixs, pixim, NULL, 5, 10, BINTHRESH, 20,
00179                              BGVAL, SMOOTH_X, SMOOTH_Y);
00180     fprintf(stderr, "time for bg normalization: %7.3f\n", stopTimer());
00181     pixWrite("/tmp/junkpixd3.jpg", pixd, IFF_JFIF_JPEG);
00182     pixSaveTiled(pixd, pixa, 1, 1, 20, 32);
00183     pixDisplayWrite(pixd, 1);
00184 
00185     pixd2 = pixGammaTRCMasked(NULL, pixd, pixim, 1.0, 0, 190);
00186     pixInvert(pixim, pixim);
00187     pixGammaTRCMasked(pixd2, pixd2, pixim, 1.0, 60, 190);
00188     pixWrite("/tmp/junkpixo3.jpg", pixd2, IFF_JFIF_JPEG);
00189     pixSaveTiled(pixd2, pixa, 1, 0, 20, 32);
00190     pixDisplayWrite(pixd2, 1);
00191 
00192     pixDestroy(&pixd);
00193     pixDestroy(&pixd2);
00194     pixDestroy(&pixim);
00195 #endif
00196     
00197         /* Display results */
00198     pixd = pixaDisplay(pixa, 0, 0);
00199     pixDisplay(pixd, 100, 100);
00200     pixWrite("/tmp/junkadapt.jpg", pixd, IFF_JFIF_JPEG);
00201     pixDestroy(&pixd);
00202     pixaDestroy(&pixa);
00203 
00204     pixDisplayMultiple("/tmp/junk_write_display*");
00205 
00206     pixDestroy(&pixs);
00207     pixDestroy(&pixg);
00208     pixDestroy(&pixc);
00209     exit(0);
00210 }
00211 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines