Leptonica 1.68
C Image Processing Library

enhance_reg.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  * enhance_reg.c
00018  *
00019  *   This tests the following global "enhancement" functions:
00020  *     * TRC transforms with variation of gamma and black point
00021  *     * HSV transforms with variation of hue, saturation and intensity
00022  *     * Contrast variation
00023  *     * Sharpening
00024  *     * Color mapping to lighten background with constant hue
00025  *     * Linear color transform without mixing (diagonal)
00026  */
00027 
00028 #include "allheaders.h"
00029 
00030 static const char *filein = "test24.jpg";
00031 static const l_int32 WIDTH = 150;
00032 
00033 main(int    argc,
00034      char **argv)
00035 {
00036 char          textstr[256];
00037 l_int32       w, h, d, i, same;
00038 l_uint32      srcval, dstval;
00039 l_float32     scalefact, sat, fract;
00040 L_BMF        *bmf8;
00041 L_KERNEL     *kel;
00042 NUMA         *na;
00043 PIX          *pix, *pixs, *pixs1, *pixs2, *pixd;
00044 PIX          *pixt0, *pixt1, *pixt2, *pixt3, *pixt4;
00045 PIXA         *pixa, *pixaf;
00046 L_REGPARAMS  *rp;
00047 
00048     if (regTestSetup(argc, argv, &rp))
00049         return 1;
00050 
00051     pix = pixRead(filein);
00052     pixGetDimensions(pix, &w, &h, &d);
00053     if (d != 32)
00054         return ERROR_INT("file not 32 bpp", argv[0], 1);
00055     scalefact = (l_float32)WIDTH / (l_float32)w;
00056     pixs = pixScale(pix, scalefact, scalefact);
00057     w = pixGetWidth(pixs);
00058     pixaf = pixaCreate(5);
00059 
00060         /* TRC: vary gamma */
00061     pixa = pixaCreate(20);
00062     for (i = 0; i < 20; i++) {
00063         pixt0 = pixGammaTRC(NULL, pixs, 0.3 + 0.15 * i, 0, 255);
00064         pixaAddPix(pixa, pixt0, L_INSERT);
00065     }
00066     pixt1 = pixaDisplayTiledAndScaled(pixa, 32, w, 5, 0, 10, 2);
00067     pixSaveTiled(pixt1, pixaf, 1, 1, 20, 32);
00068     regTestWritePixAndCheck(rp, pixt1, IFF_PNG);  /* 0 */
00069     pixDisplayWithTitle(pixt1, 0, 100, "TRC Gamma", rp->display);
00070     pixDestroy(&pixt1);
00071     pixaDestroy(&pixa);
00072 
00073         /* TRC: vary black point */
00074     pixa = pixaCreate(20);
00075     for (i = 0; i < 20; i++) {
00076         pixt0 = pixGammaTRC(NULL, pixs, 1.0, 5 * i, 255);
00077         pixaAddPix(pixa, pixt0, L_INSERT);
00078     }
00079     pixt1 = pixaDisplayTiledAndScaled(pixa, 32, w, 5, 0, 10, 2);
00080     pixSaveTiled(pixt1, pixaf, 1, 1, 20, 0);
00081     regTestWritePixAndCheck(rp, pixt1, IFF_PNG);  /* 1 */
00082     pixDisplayWithTitle(pixt1, 300, 100, "TRC", rp->display);
00083     pixDestroy(&pixt1);
00084     pixaDestroy(&pixa);
00085 
00086         /* Vary hue */
00087     pixa = pixaCreate(20);
00088     for (i = 0; i < 20; i++) {
00089         pixt0 = pixModifyHue(NULL, pixs, 0.01 + 0.05 * i);
00090         pixaAddPix(pixa, pixt0, L_INSERT);
00091     }
00092     pixt1 = pixaDisplayTiledAndScaled(pixa, 32, w, 5, 0, 10, 2);
00093     pixSaveTiled(pixt1, pixaf, 1, 1, 20, 0);
00094     regTestWritePixAndCheck(rp, pixt1, IFF_PNG);  /* 2 */
00095     pixDisplayWithTitle(pixt1, 600, 100, "Hue", rp->display);
00096     pixDestroy(&pixt1);
00097     pixaDestroy(&pixa);
00098 
00099         /* Vary saturation */
00100     pixa = pixaCreate(20);
00101     na = numaCreate(20);
00102     for (i = 0; i < 20; i++) {
00103         pixt0 = pixModifySaturation(NULL, pixs, -0.9 + 0.1 * i);
00104         pixMeasureSaturation(pixt0, 1, &sat);
00105         pixaAddPix(pixa, pixt0, L_INSERT);
00106         numaAddNumber(na, sat);
00107     }
00108     pixt1 = pixaDisplayTiledAndScaled(pixa, 32, w, 5, 0, 10, 2);
00109     pixSaveTiled(pixt1, pixaf, 1, 1, 20, 0);
00110     gplotSimple1(na, GPLOT_PNG, "/tmp/enhance.7", "Average Saturation");
00111     regTestWritePixAndCheck(rp, pixt1, IFF_PNG);  /* 3 */
00112     pixDisplayWithTitle(pixt1, 900, 100, "Saturation", rp->display);
00113     numaDestroy(&na);
00114     pixDestroy(&pixt1);
00115     pixaDestroy(&pixa);
00116 
00117         /* Vary contrast */
00118     pixa = pixaCreate(20);
00119     for (i = 0; i < 20; i++) {
00120         pixt0 = pixContrastTRC(NULL, pixs, 0.1 * i);
00121         pixaAddPix(pixa, pixt0, L_INSERT);
00122     }
00123     pixt1 = pixaDisplayTiledAndScaled(pixa, 32, w, 5, 0, 10, 2);
00124     pixSaveTiled(pixt1, pixaf, 1, 1, 20, 0);
00125     regTestWritePixAndCheck(rp, pixt1, IFF_PNG);  /* 4 */
00126     pixDisplayWithTitle(pixt1, 0, 400, "Contrast", rp->display);
00127     pixDestroy(&pixt1);
00128     pixaDestroy(&pixa);
00129 
00130         /* Vary sharpening */
00131     pixa = pixaCreate(20);
00132     for (i = 0; i < 20; i++) {
00133         pixt0 = pixUnsharpMasking(pixs, 3, 0.01 + 0.15 * i);
00134         pixaAddPix(pixa, pixt0, L_INSERT);
00135     }
00136     pixt1 = pixaDisplayTiledAndScaled(pixa, 32, w, 5, 0, 10, 2);
00137     pixSaveTiled(pixt1, pixaf, 1, 1, 20, 0);
00138     regTestWritePixAndCheck(rp, pixt1, IFF_PNG);  /* 5 */
00139     pixDisplayWithTitle(pixt1, 300, 400, "Sharp", rp->display);
00140     pixDestroy(&pixt1);
00141     pixaDestroy(&pixa);
00142 
00143         /* Hue constant mapping to lighter background */
00144     pixa = pixaCreate(11);
00145     bmf8 = bmfCreate("fonts", 8);
00146     pixt0 = pixRead("candelabrum-11.jpg");
00147     composeRGBPixel(230, 185, 144, &srcval);
00148     for (i = 0; i <= 10; i++) {
00149         fract = 0.10 * i;
00150         pixelFractionalShift(230, 185, 144, fract, &dstval);
00151         pixt1 = pixLinearMapToTargetColor(NULL, pixt0, srcval, dstval);
00152         snprintf(textstr, 50, "Fract = %5.1f", fract);
00153         pixt2 = pixAddSingleTextblock(pixt1, bmf8, textstr, 0xff000000,
00154                                       L_ADD_BELOW, NULL);
00155         pixSaveTiledOutline(pixt2, pixa, 1, (i % 4 == 0) ? 1 : 0, 30, 2, 32);
00156         pixDestroy(&pixt1);
00157         pixDestroy(&pixt2);
00158     }
00159     pixDestroy(&pixt0);
00160 
00161     pixd = pixaDisplay(pixa, 0, 0);
00162     regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG);  /* 6 */
00163     pixDisplayWithTitle(pixd, 600, 400, "Constant hue", rp->display);
00164     bmfDestroy(&bmf8);
00165     pixaDestroy(&pixa);
00166     pixDestroy(&pixd);
00167 
00168         /* Delayed testing of saturation plot */
00169     regTestCheckFile(rp, "/tmp/enhance.7.png");  /* 7 */
00170 
00171         /* Display results */
00172     pixd = pixaDisplay(pixaf, 0, 0);
00173     regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG);  /* 8 */
00174     pixDisplayWithTitle(pixd, 100, 100, "All", rp->display);
00175     pixDestroy(&pixd);
00176     pixaDestroy(&pixaf);
00177 
00178     pixDestroy(&pix);
00179     pixDestroy(&pixs);
00180 
00181     /* -----------------------------------------------*
00182      *           Test global color transforms         *
00183      * -----------------------------------------------*/
00184         /* Make identical cmap and rgb images */
00185     pix = pixRead("wet-day.jpg");
00186     pixs1 = pixOctreeColorQuant(pix, 200, 0);
00187     pixs2 = pixRemoveColormap(pixs1, REMOVE_CMAP_TO_FULL_COLOR);
00188     regTestComparePix(rp, pixs1, pixs2);  /* 9 */
00189 
00190         /* Make a diagonal color transform matrix */
00191     kel = kernelCreate(3, 3);
00192     kernelSetElement(kel, 0, 0, 0.7);
00193     kernelSetElement(kel, 1, 1, 0.4);
00194     kernelSetElement(kel, 2, 2, 1.3);
00195 
00196         /* Apply to both cmap and rgb images. */
00197     pixt1 = pixMultMatrixColor(pixs1, kel);
00198     pixt2 = pixMultMatrixColor(pixs2, kel);
00199     regTestComparePix(rp, pixt1, pixt2);  /* 10 */
00200     kernelDestroy(&kel);
00201 
00202         /* Apply the same transform in the simpler interface */
00203     pixt3 = pixMultConstantColor(pixs1, 0.7, 0.4, 1.3);
00204     pixt4 = pixMultConstantColor(pixs2, 0.7, 0.4, 1.3);
00205     regTestComparePix(rp, pixt3, pixt4);  /* 11 */
00206     regTestComparePix(rp, pixt1, pixt3);  /* 12 */
00207     regTestWritePixAndCheck(rp, pixt1, IFF_JFIF_JPEG);  /* 13 */
00208 
00209     regTestCleanup(rp);
00210     pixDestroy(&pix);
00211     pixDestroy(&pixs1);
00212     pixDestroy(&pixs2);
00213     pixDestroy(&pixt1);
00214     pixDestroy(&pixt2);
00215     pixDestroy(&pixt3);
00216     pixDestroy(&pixt4);
00217     return 0;
00218 }
00219 
00220 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines