Leptonica 1.68
C Image Processing Library

colorquant_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  * colorquant_reg.c
00018  *
00019  *    Regression test for various color quantizers
00020  */
00021 
00022 #include "allheaders.h"
00023 
00024 static const l_int32 SPACE = 30;
00025 static const l_int32 MAX_WIDTH = 350;
00026 static const char *image[4] = {"marge.jpg",
00027                                "test24.jpg",
00028                                "juditharismax.jpg",
00029                                "hardlight2_2.jpg"};
00030 
00031 static l_int32 TestImage(const char *filename, l_int32 i, L_REGPARAMS *rp);
00032 static void PixSave32(PIXA *pixa, PIX *pixc, L_REGPARAMS *rp);
00033 
00034 
00035 main(int    argc,
00036      char **argv)
00037 {
00038 l_int32       i;
00039 L_REGPARAMS  *rp;
00040 
00041     if (regTestSetup(argc, argv, &rp))
00042         return 1;
00043 
00044     for (i = 0; i < 4; i++) {
00045 /*        if (i != 2) continue; */
00046         TestImage(image[i], i, rp);
00047     }
00048 
00049     regTestCleanup(rp);
00050     return 0;
00051 }
00052 
00053 
00054 static l_int32
00055 TestImage(const char   *filename,
00056           l_int32       i,
00057           L_REGPARAMS  *rp)
00058 {
00059 char       buf[256];
00060 l_int32    w, h, nerrors;
00061 l_float32  factor;
00062 PIX       *pix, *pixs, *pixc, *pix32, *pixt, *pixd;
00063 PIXA      *pixa;
00064 char      *fileout;
00065 
00066     PROCNAME("TestImage");
00067 
00068     if ((pix = pixRead(filename)) == NULL) {
00069         rp->success = FALSE;
00070         return ERROR_INT("pix not made", procName, 1);
00071     }
00072     pixGetDimensions(pix, &w, &h, NULL);
00073     if (w > MAX_WIDTH) {
00074         factor = (l_float32)MAX_WIDTH / (l_float32)w;
00075         pixs = pixScale(pix, factor, factor);
00076     }
00077     else
00078         pixs = pixClone(pix);
00079     pixDestroy(&pix);
00080 
00081     pixa = pixaCreate(0);
00082 
00083         /* Median cut quantizer (no dither; 5 sigbits) */
00084     pixSaveTiled(pixs, pixa, 1, 1, SPACE, 32);
00085     pixc = pixMedianCutQuantGeneral(pixs, 0, 0, 16, 5, 1, 1);
00086     PixSave32(pixa, pixc, rp);
00087     pixc = pixMedianCutQuantGeneral(pixs, 0, 0, 128, 5, 1, 1);
00088     PixSave32(pixa, pixc, rp);
00089     pixc = pixMedianCutQuantGeneral(pixs, 0, 0, 256, 5, 1, 1);
00090     PixSave32(pixa, pixc, rp);
00091 
00092         /* Median cut quantizer (with dither; 5 sigbits) */
00093     pixSaveTiled(pixs, pixa, 1, 1, SPACE, 0);
00094     pixc = pixMedianCutQuantGeneral(pixs, 1, 0, 16, 5, 1, 1);
00095     PixSave32(pixa, pixc, rp);
00096     pixc = pixMedianCutQuantGeneral(pixs, 1, 0, 128, 5, 1, 1);
00097     PixSave32(pixa, pixc, rp);
00098     pixc = pixMedianCutQuantGeneral(pixs, 1, 0, 256, 5, 1, 1);
00099     PixSave32(pixa, pixc, rp);
00100 
00101         /* Median cut quantizer (no dither; 6 sigbits) */
00102     pixSaveTiled(pixs, pixa, 1, 1, SPACE, 32);
00103     pixc = pixMedianCutQuantGeneral(pixs, 0, 0, 16, 6, 1, 1);
00104     PixSave32(pixa, pixc, rp);
00105     pixc = pixMedianCutQuantGeneral(pixs, 0, 0, 128, 6, 1, 1);
00106     PixSave32(pixa, pixc, rp);
00107     pixc = pixMedianCutQuantGeneral(pixs, 0, 0, 256, 6, 1, 1);
00108     PixSave32(pixa, pixc, rp);
00109 
00110         /* Median cut quantizer (with dither; 6 sigbits) */
00111     pixSaveTiled(pixs, pixa, 1, 1, SPACE, 0);
00112     pixc = pixMedianCutQuantGeneral(pixs, 1, 0, 16, 6, 1, 1);
00113     PixSave32(pixa, pixc, rp);
00114     pixc = pixMedianCutQuantGeneral(pixs, 1, 0, 128, 6, 1, 1);
00115     PixSave32(pixa, pixc, rp);
00116     pixc = pixMedianCutQuantGeneral(pixs, 1, 0, 256, 6, 10, 1);
00117     PixSave32(pixa, pixc, rp);
00118 
00119         /* Median cut quantizer (mixed color/gray) */
00120     pixSaveTiled(pixs, pixa, 1, 1, SPACE, 0);
00121     pixc = pixMedianCutQuantMixed(pixs, 20, 10, 0, 0, 0);
00122     PixSave32(pixa, pixc, rp);
00123     pixc = pixMedianCutQuantMixed(pixs, 60, 20, 0, 0, 0);
00124     PixSave32(pixa, pixc, rp);
00125     pixc = pixMedianCutQuantMixed(pixs, 180, 40, 0, 0, 0);
00126     PixSave32(pixa, pixc, rp);
00127 
00128         /* Simple 256 cube octcube quantizer */
00129     pixSaveTiled(pixs, pixa, 1, 1, SPACE, 0);
00130     pixc = pixFixedOctcubeQuant256(pixs, 0);  /* no dither */
00131     PixSave32(pixa, pixc, rp);
00132     pixc = pixFixedOctcubeQuant256(pixs, 1);  /* dither */
00133     PixSave32(pixa, pixc, rp);
00134 
00135         /* 2-pass octree quantizer */
00136     pixSaveTiled(pixs, pixa, 1, 1, SPACE, 0);
00137     pixc = pixOctreeColorQuant(pixs, 128, 0);  /* no dither */
00138     PixSave32(pixa, pixc, rp);
00139     pixc = pixOctreeColorQuant(pixs, 240, 0);  /* no dither */
00140     PixSave32(pixa, pixc, rp);
00141     pixc = pixOctreeColorQuant(pixs, 128, 1);  /* dither */
00142     PixSave32(pixa, pixc, rp);
00143     pixc = pixOctreeColorQuant(pixs, 240, 1);  /* dither */
00144     PixSave32(pixa, pixc, rp);
00145 
00146         /* Simple adaptive quantization to 4 or 8 bpp, specifying ncolors */
00147     pixSaveTiled(pixs, pixa, 1, 1, SPACE, 0);
00148     pixc = pixOctreeQuantNumColors(pixs, 8, 0);    /* fixed: 8 colors */
00149     PixSave32(pixa, pixc, rp);
00150     pixc = pixOctreeQuantNumColors(pixs, 16, 0);   /* fixed: 16 colors */
00151     PixSave32(pixa, pixc, rp);
00152     pixc = pixOctreeQuantNumColors(pixs, 64, 0);   /* fixed: 64 colors */
00153     PixSave32(pixa, pixc, rp);
00154     pixc = pixOctreeQuantNumColors(pixs, 256, 0);   /* fixed: 256 colors */
00155     PixSave32(pixa, pixc, rp);
00156 
00157         /* Quantize to fully populated octree (RGB) at given level */
00158     pixSaveTiled(pixs, pixa, 1, 1, SPACE, 0);
00159     pixc = pixFixedOctcubeQuantGenRGB(pixs, 2);  /* level 2 */
00160     PixSave32(pixa, pixc, rp);
00161     pixc = pixFixedOctcubeQuantGenRGB(pixs, 3);  /* level 3 */
00162     PixSave32(pixa, pixc, rp);
00163     pixc = pixFixedOctcubeQuantGenRGB(pixs, 4);  /* level 4 */
00164     PixSave32(pixa, pixc, rp);
00165     pixc = pixFixedOctcubeQuantGenRGB(pixs, 5);  /* level 5 */
00166     PixSave32(pixa, pixc, rp);
00167 
00168         /* Generate 32 bpp RGB image with num colors <= 256 */
00169     pixt = pixOctreeQuantNumColors(pixs, 256, 0);   /* cmapped version */
00170     pix32 = pixRemoveColormap(pixt, REMOVE_CMAP_BASED_ON_SRC);
00171 
00172         /* Quantize image with few colors at fixed octree leaf level */
00173     pixSaveTiled(pixt, pixa, 1, 1, SPACE, 0);
00174     pixc = pixFewColorsOctcubeQuant1(pix32, 2);   /* level 2 */
00175     PixSave32(pixa, pixc, rp);
00176     pixc = pixFewColorsOctcubeQuant1(pix32, 3);   /* level 3 */
00177     PixSave32(pixa, pixc, rp);
00178     pixc = pixFewColorsOctcubeQuant1(pix32, 4);   /* level 4 */
00179     PixSave32(pixa, pixc, rp);
00180     pixc = pixFewColorsOctcubeQuant1(pix32, 5);   /* level 5 */
00181     PixSave32(pixa, pixc, rp);
00182 
00183         /* Quantize image by population */
00184     pixSaveTiled(pixt, pixa, 1, 1, SPACE, 0);
00185     pixc = pixOctreeQuantByPopulation(pixs, 3, 0);  /* level 3, no dither */
00186     PixSave32(pixa, pixc, rp);
00187     pixc = pixOctreeQuantByPopulation(pixs, 3, 1);  /* level 3, dither */
00188     PixSave32(pixa, pixc, rp);
00189     pixc = pixOctreeQuantByPopulation(pixs, 4, 0);  /* level 4, no dither */
00190     PixSave32(pixa, pixc, rp);
00191     pixc = pixOctreeQuantByPopulation(pixs, 4, 1);  /* level 4, dither */
00192     PixSave32(pixa, pixc, rp);
00193 
00194         /* Mixed color/gray octree quantizer */
00195     pixSaveTiled(pixt, pixa, 1, 1, SPACE, 0);
00196     pixc = pixOctcubeQuantMixedWithGray(pix32, 8, 64, 10);  /* max delta = 10 */
00197     PixSave32(pixa, pixc, rp);
00198     pixc = pixOctcubeQuantMixedWithGray(pix32, 8, 64, 30);  /* max delta = 30 */
00199     PixSave32(pixa, pixc, rp);
00200     pixc = pixOctcubeQuantMixedWithGray(pix32, 8, 64, 50);  /* max delta = 50 */
00201     PixSave32(pixa, pixc, rp);
00202     
00203         /* Run the high-level converter */
00204     pixSaveTiled(pixt, pixa, 1, 1, SPACE, 0);
00205     pixc = pixConvertRGBToColormap(pix32, 1);
00206     PixSave32(pixa, pixc, rp);
00207 
00208     pixDestroy(&pix32);
00209     pixDestroy(&pixt);
00210 
00211     pixd = pixaDisplay(pixa, 0, 0);
00212     pixDisplayWithTitle(pixd, 100, 100, NULL, rp->display);
00213     sprintf(buf, "/tmp/disp.%d.jpg", i);
00214     pixWrite(buf, pixd, IFF_JFIF_JPEG);
00215 
00216     pixDestroy(&pixs);
00217     pixDestroy(&pixd);
00218     pixaDestroy(&pixa);
00219 
00220     return 0;
00221 }
00222 
00223 
00224 static void
00225 PixSave32(PIXA *pixa, PIX *pixc, L_REGPARAMS *rp)
00226 {
00227 PIX  *pix32;
00228 
00229     pix32 = pixConvertTo32(pixc);
00230     pixSaveTiled(pix32, pixa, 1, 0, SPACE, 0);
00231     regTestWritePixAndCheck(rp, pix32, IFF_JFIF_JPEG);
00232     pixDestroy(&pixc);
00233     pixDestroy(&pix32);
00234 }
00235 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines