Leptonica 1.68
C Image Processing Library

grayquant_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  * grayquant_reg.c
00018  *
00019  *     Tests gray thresholding to 1, 2 and 4 bpp, with and without colormaps
00020  */
00021 
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include "allheaders.h"
00025 
00026 static const l_int32  THRESHOLD = 130;
00027     /* nlevels for 4 bpp output; anything between 2 and 16 */
00028 static const l_int32  NLEVELS = 4;
00029 
00030 
00031 main(int    argc,
00032      char **argv)
00033 {
00034 const char  *str;
00035 l_int32      equal, index, w, h;
00036 BOX         *box;
00037 PIX         *pixs, *pixd, *pixt, *pixd1, *pixd2, *pixd3;
00038 PIX         *pixt0, *pixt1, *pixt2, *pixt3, *pixt4;
00039 PIXA        *pixa;
00040 PIXCMAP     *cmap;
00041 static char  mainName[] = "grayquant_reg";
00042 
00043     if ((pixs = pixRead("test8.jpg")) == NULL)
00044         exit(ERROR_INT("pixs not made", mainName, 1));
00045 
00046     pixa = pixaCreate(0);
00047     pixSaveTiled(pixs, pixa, 1, 1, 20, 8);
00048 
00049         /* threshold to 1 bpp */
00050     pixd = pixThresholdToBinary(pixs, THRESHOLD);
00051     pixSaveTiled(pixd, pixa, 1, 1, 20, 0);
00052     pixWrite("/tmp/junkthr0.png", pixd, IFF_PNG);
00053     pixDestroy(&pixd);
00054 
00055         /* dither to 2 bpp, with and without colormap */
00056     pixd = pixDitherTo2bpp(pixs, 1);
00057     pixt = pixDitherTo2bpp(pixs, 0);
00058     pixt2 = pixConvertGrayToColormap(pixt);
00059     pixSaveTiled(pixd, pixa, 1, 1, 20, 0);
00060     pixSaveTiled(pixt, pixa, 1, 0, 20, 0);
00061     pixSaveTiled(pixt2, pixa, 1, 0, 20, 0);
00062     pixWrite("/tmp/junkthr1.png", pixd, IFF_PNG);
00063     pixWrite("/tmp/junkthr2.png", pixt, IFF_PNG);
00064     pixWrite("/tmp/junkthr3.png", pixt2, IFF_PNG);
00065 /*    pixcmapWriteStream(stderr, pixGetColormap(pixd)); */
00066     pixEqual(pixd, pixt2, &equal);
00067     if (!equal)
00068         fprintf(stderr, "Error: thr2 != thr3\n");
00069     pixDestroy(&pixt);
00070     pixDestroy(&pixt2);
00071     pixDestroy(&pixd);
00072 
00073         /* threshold to 2 bpp, with and without colormap */
00074     pixd = pixThresholdTo2bpp(pixs, 4, 1);
00075     pixt = pixThresholdTo2bpp(pixs, 4, 0);
00076     pixt2 = pixConvertGrayToColormap(pixt);
00077     pixSaveTiled(pixd, pixa, 1, 1, 20, 0);
00078     pixSaveTiled(pixt, pixa, 1, 0, 20, 0);
00079     pixSaveTiled(pixt2, pixa, 1, 0, 20, 0);
00080     pixWrite("/tmp/junkthr4.png", pixd, IFF_PNG);
00081     pixWrite("/tmp/junkthr5.png", pixt2, IFF_PNG);
00082     pixEqual(pixd, pixt2, &equal);
00083     if (!equal)
00084         fprintf(stderr, "Error: thr4 != thr5\n");
00085     pixDestroy(&pixt);
00086     pixDestroy(&pixt2);
00087     pixDestroy(&pixd);
00088 
00089     pixd = pixThresholdTo2bpp(pixs, 3, 1);
00090     pixt = pixThresholdTo2bpp(pixs, 3, 0);
00091     pixSaveTiled(pixd, pixa, 1, 1, 20, 0);
00092     pixSaveTiled(pixt, pixa, 1, 0, 20, 0);
00093     pixWrite("/tmp/junkthr6.png", pixd, IFF_PNG);
00094     pixWrite("/tmp/junkthr7.png", pixt, IFF_PNG);
00095     pixDestroy(&pixt);
00096     pixDestroy(&pixd);
00097 
00098         /* threshold to 4 bpp, with and without colormap */
00099     pixd = pixThresholdTo4bpp(pixs, 9, 1);
00100     pixt = pixThresholdTo4bpp(pixs, 9, 0);
00101     pixt2 = pixConvertGrayToColormap(pixt);
00102     pixSaveTiled(pixd, pixa, 1, 1, 20, 0);
00103     pixSaveTiled(pixt, pixa, 1, 0, 20, 0);
00104     pixSaveTiled(pixt2, pixa, 1, 0, 20, 0);
00105     pixWrite("/tmp/junkthr8.png", pixd, IFF_PNG);
00106     pixWrite("/tmp/junkthr9.png", pixt, IFF_PNG);
00107     pixWrite("/tmp/junkthr10.png", pixt2, IFF_PNG);
00108 /*    pixcmapWriteStream(stderr, pixGetColormap(pixd)); */
00109     pixDestroy(&pixt);
00110     pixDestroy(&pixt2);
00111     pixDestroy(&pixd);
00112 
00113         /* threshold on 8 bpp, with and without colormap */
00114     pixd = pixThresholdOn8bpp(pixs, 9, 1);
00115     pixt = pixThresholdOn8bpp(pixs, 9, 0);
00116     pixt2 = pixConvertGrayToColormap(pixt);
00117     pixSaveTiled(pixd, pixa, 1, 1, 20, 0);
00118     pixSaveTiled(pixt, pixa, 1, 0, 20, 0);
00119     pixSaveTiled(pixt2, pixa, 1, 0, 20, 0);
00120     pixWrite("/tmp/junkthr11.png", pixd, IFF_PNG);
00121     pixWrite("/tmp/junkthr12.png", pixt2, IFF_PNG);
00122 /*    pixcmapWriteStream(stderr, pixGetColormap(pixd)); */
00123     pixEqual(pixd, pixt2, &equal);
00124     if (!equal)
00125         fprintf(stderr, "Error: thr11 != thr12\n");
00126     pixDestroy(&pixt);
00127     pixDestroy(&pixt2);
00128     pixDestroy(&pixd);
00129 
00130     pixd1 = pixaDisplay(pixa, 0, 0);
00131     pixDisplay(pixd1, 100, 100);
00132     pixWrite("/tmp/junkpixd1.jpg", pixd1, IFF_JFIF_JPEG);
00133     pixDestroy(&pixd1);
00134     pixaDestroy(&pixa);
00135 
00136     pixa = pixaCreate(0);
00137     pixSaveTiled(pixs, pixa, 1, 1, 20, 32);
00138 
00139         /* highlight 2 bpp with colormap */
00140     pixd = pixThresholdTo2bpp(pixs, 3, 1);
00141     cmap = pixGetColormap(pixd);
00142     pixcmapWriteStream(stderr, cmap);
00143     box = boxCreate(278, 35, 122, 50);
00144     pixSetSelectCmap(pixd, box, 2, 255, 255, 100);
00145     pixcmapWriteStream(stderr, cmap);
00146     pixDisplay(pixd, 0, 0);
00147     pixSaveTiled(pixd, pixa, 1, 1, 20, 0);
00148     pixWrite("/tmp/junkthr13.png", pixd, IFF_PNG);
00149     pixDestroy(&pixd);
00150     boxDestroy(&box);
00151 
00152         /* test pixThreshold8() */
00153     pixd = pixThreshold8(pixs, 1, 2, 1);  /* cmap */
00154     pixSaveTiled(pixd, pixa, 1, 1, 20, 0);
00155     pixWrite("/tmp/junkthr14.png", pixd, IFF_PNG);
00156     pixDisplay(pixd, 100, 0);
00157     pixDestroy(&pixd);
00158     pixd = pixThreshold8(pixs, 1, 2, 0);  /* no cmap */
00159     pixSaveTiled(pixd, pixa, 1, 0, 20, 0);
00160     pixWrite("/tmp/junkthr15.png", pixd, IFF_PNG);
00161     pixDisplay(pixd, 200, 0);
00162     pixDestroy(&pixd);
00163     pixd = pixThreshold8(pixs, 2, 3, 1);  /* highlight one box */
00164     pixSaveTiled(pixd, pixa, 1, 0, 20, 0);
00165     box = boxCreate(278, 35, 122, 50);
00166     pixSetSelectCmap(pixd, box, 2, 255, 255, 100);
00167     pixWrite("/tmp/junkthr16.png", pixd, IFF_PNG);
00168     pixDisplay(pixd, 300, 0);
00169     cmap = pixGetColormap(pixd);
00170     pixcmapWriteStream(stderr, cmap);
00171     boxDestroy(&box);
00172     pixDestroy(&pixd);
00173     pixd = pixThreshold8(pixs, 2, 4, 0);  /* no cmap */
00174     pixSaveTiled(pixd, pixa, 1, 0, 20, 0);
00175     pixWrite("/tmp/junkthr17.png", pixd, IFF_PNG);
00176     pixDisplay(pixd, 400, 0);
00177     pixDestroy(&pixd);
00178     pixd = pixThreshold8(pixs, 4, 6, 1);  /* highlight one box */
00179     box = boxCreate(278, 35, 122, 50);
00180     pixSetSelectCmap(pixd, box, 5, 255, 255, 100);
00181     pixSaveTiled(pixd, pixa, 1, 0, 20, 0);
00182     pixWrite("/tmp/junkthr18.png", pixd, IFF_PNG);
00183     cmap = pixGetColormap(pixd);
00184     pixcmapWriteStream(stderr, cmap);
00185     boxDestroy(&box);
00186     pixDisplay(pixd, 500, 0);
00187     pixDestroy(&pixd);
00188     pixd = pixThreshold8(pixs, 4, 6, 0);  /* no cmap */
00189     pixSaveTiled(pixd, pixa, 1, 0, 20, 0);
00190     pixWrite("/tmp/junkthr19.png", pixd, IFF_PNG);
00191     pixDisplay(pixd, 600, 0);
00192     pixDestroy(&pixd);
00193 
00194         /* highlight 4 bpp with 2 colormap entries */
00195         /* Note: We use 5 levels (0-4) for gray.           */
00196         /*       5 & 6 are used for highlight color.       */
00197     pixd = pixThresholdTo4bpp(pixs, 5, 1);
00198     cmap = pixGetColormap(pixd);
00199     pixcmapGetIndex(cmap, 255, 255, 255, &index);
00200     box = boxCreate(278, 35, 122, 50);
00201     pixSetSelectCmap(pixd, box, index, 255, 255, 100);  /* use 5 */
00202     boxDestroy(&box);
00203     box = boxCreate(4, 6, 157, 33);
00204     pixSetSelectCmap(pixd, box, index, 100, 255, 255);  /* use 6 */
00205     boxDestroy(&box);
00206     pixcmapWriteStream(stderr, cmap);
00207     pixSaveTiled(pixd, pixa, 1, 1, 20, 0);
00208     pixDisplay(pixd, 700, 0);
00209     pixWrite("/tmp/junkthr20.png", pixd, IFF_PNG);
00210     pixDestroy(&pixd);
00211 
00212         /* comparison 8 bpp jpeg with 2 bpp (highlight) */
00213     pixDestroy(&pixs);
00214     pixs = pixRead("feyn.tif");
00215     pixt = pixScaleToGray4(pixs);
00216     pixt2 = pixReduceRankBinaryCascade(pixs, 2, 2, 0, 0);
00217     pixd = pixThresholdTo2bpp(pixt, 3, 1);
00218     box = boxCreate(175, 208, 228, 88);
00219     pixSetSelectCmap(pixd, box, 2, 255, 255, 100);
00220     pixDisplay(pixd, 100, 200);
00221     cmap = pixGetColormap(pixd);
00222     pixcmapWriteStream(stderr, cmap);
00223     pixSaveTiled(pixt, pixa, 1, 1, 20, 0);
00224     pixSaveTiled(pixt2, pixa, 1, 0, 20, 0);
00225     pixSaveTiled(pixd, pixa, 1, 0, 20, 0);
00226     pixWrite("/tmp/junkthr21.jpg", pixt, IFF_JFIF_JPEG);
00227     pixWrite("/tmp/junkthr22.png", pixt2, IFF_PNG);
00228     pixWrite("/tmp/junkthr23.png", pixd, IFF_PNG);
00229     pixDestroy(&pixd);
00230     pixDestroy(&pixt2);
00231     boxDestroy(&box);
00232 
00233         /* thresholding to 4 bpp (highlight); use pixt from above */
00234     pixd = pixThresholdTo4bpp(pixt, NLEVELS, 1);
00235     box = boxCreate(175, 208, 228, 83);
00236     pixSetSelectCmap(pixd, box, NLEVELS - 1, 255, 255, 100);
00237     boxDestroy(&box);
00238     box = boxCreate(232, 298, 110, 25);
00239     pixSetSelectCmap(pixd, box, NLEVELS - 1, 100, 255, 255);
00240     boxDestroy(&box);
00241     box = boxCreate(21, 698, 246, 82);
00242     pixSetSelectCmap(pixd, box, NLEVELS - 1, 225, 100, 255);
00243     boxDestroy(&box);
00244     pixDisplay(pixd, 500, 200);
00245     cmap = pixGetColormap(pixd);
00246     pixcmapWriteStream(stderr, cmap);
00247     pixt2 = pixReduceRankBinaryCascade(pixs, 2, 2, 0, 0);
00248     pixSaveTiled(pixt2, pixa, 1, 1, 20, 0);
00249     pixSaveTiled(pixd, pixa, 1, 0, 20, 0);
00250     pixWrite("/tmp/junkthr24.png", pixt2, IFF_PNG);
00251     pixWrite("/tmp/junkthr25.png", pixd, IFF_PNG);
00252     pixDestroy(&pixt2);
00253     pixDestroy(&pixd);
00254 
00255        /* Thresholding to 4 bpp at 2, 3, 4, 5 and 6 levels */
00256     box = boxCreate(25, 202, 136, 37);
00257     pixt1 = pixClipRectangle(pixt, box, NULL);
00258     pixt2 = pixScale(pixt1, 6., 6.);
00259     pixGetDimensions(pixt2, &w, &h, NULL);
00260     pixSaveTiled(pixt2, pixa, 1, 1, 20, 0);
00261     pixDisplay(pixt2, 0, 0);
00262     pixWrite("/tmp/junk-8.jpg", pixt2, IFF_JFIF_JPEG);
00263     pixd = pixCreate(w, 6 * h, 8);
00264     pixRasterop(pixd, 0, 0, w, h, PIX_SRC, pixt2, 0, 0);
00265 
00266     pixt3 = pixThresholdTo4bpp(pixt2, 6, 1);
00267     pixt4 = pixRemoveColormap(pixt3, REMOVE_CMAP_TO_GRAYSCALE);
00268     pixRasterop(pixd, 0, h, w, h, PIX_SRC, pixt4, 0, 0);
00269     pixSaveTiled(pixt3, pixa, 1, 0, 20, 0);
00270     pixDisplay(pixt3, 0, 100);
00271     pixWrite("/tmp/junk-4-6.png", pixt3, IFF_PNG);
00272     pixDestroy(&pixt3);
00273     pixDestroy(&pixt4);
00274 
00275     pixt3 = pixThresholdTo4bpp(pixt2, 5, 1);
00276     pixt4 = pixRemoveColormap(pixt3, REMOVE_CMAP_TO_GRAYSCALE);
00277     pixRasterop(pixd, 0, 2 * h, w, h, PIX_SRC, pixt4, 0, 0);
00278     pixSaveTiled(pixt3, pixa, 1, 0, 20, 0);
00279     pixDisplay(pixt3, 0, 200);
00280     pixWrite("/tmp/junk-4-5.png", pixt3, IFF_PNG);
00281     pixDestroy(&pixt3);
00282     pixDestroy(&pixt4);
00283 
00284     pixt3 = pixThresholdTo4bpp(pixt2, 4, 1);
00285     pixt4 = pixRemoveColormap(pixt3, REMOVE_CMAP_TO_GRAYSCALE);
00286     pixRasterop(pixd, 0, 3 * h, w, h, PIX_SRC, pixt4, 0, 0);
00287     pixSaveTiled(pixt3, pixa, 1, 0, 20, 0);
00288     pixDisplay(pixt3, 0, 300);
00289     pixWrite("/tmp/junk-4-4.png", pixt3, IFF_PNG);
00290     pixDestroy(&pixt3);
00291     pixDestroy(&pixt4);
00292 
00293     pixt3 = pixThresholdTo4bpp(pixt2, 3, 1);
00294     pixt4 = pixRemoveColormap(pixt3, REMOVE_CMAP_TO_GRAYSCALE);
00295     pixRasterop(pixd, 0, 4 * h, w, h, PIX_SRC, pixt4, 0, 0);
00296     pixSaveTiled(pixt3, pixa, 1, 1, 20, 0);
00297     pixDisplay(pixt3, 0, 400);
00298     pixWrite("/tmp/junk-4-3.png", pixt3, IFF_PNG);
00299     pixDestroy(&pixt3);
00300     pixDestroy(&pixt4);
00301 
00302     pixt3 = pixThresholdTo4bpp(pixt2, 2, 1);
00303     pixt4 = pixRemoveColormap(pixt3, REMOVE_CMAP_TO_GRAYSCALE);
00304     pixRasterop(pixd, 0, 5 * h, w, h, PIX_SRC, pixt4, 0, 0);
00305     pixDisplay(pixt3, 0, 500);
00306     pixSaveTiled(pixt3, pixa, 1, 0, 20, 0);
00307     pixWrite("/tmp/junk-4-2.png", pixt3, IFF_PNG);
00308     pixDestroy(&pixt3);
00309     pixDestroy(&pixt4);
00310     pixWrite("/tmp/junk-all.png", pixd, IFF_PNG);
00311     pixDestroy(&pixd);
00312 
00313     pixd2 = pixaDisplay(pixa, 0, 0);
00314     pixDisplay(pixd2, 100, 100);
00315     pixWrite("/tmp/junkpixd2.jpg", pixd2, IFF_JFIF_JPEG);
00316     pixDestroy(&pixd2);
00317     pixaDestroy(&pixa);
00318 
00319 #if 0   /* upscale 2x and threshold to 1 bpp; e.g., use test8.jpg */
00320     startTimer();
00321     pixd = pixScaleGray2xLIThresh(pixs, THRESHOLD);
00322     fprintf(stderr, " time for scale/dither = %7.3f sec\n", stopTimer());
00323     pixWrite(fileout, pixd, IFF_PNG);
00324     pixDestroy(&pixd);
00325 #endif
00326 
00327 #if 0   /* upscale 4x and threshold to 1 bpp; e.g., use test8.jpg */
00328     startTimer();
00329     pixd = pixScaleGray4xLIThresh(pixs, THRESHOLD);
00330     fprintf(stderr, " time for scale/dither = %7.3f sec\n", stopTimer());
00331     pixWrite(fileout, pixd, IFF_PNG);
00332     pixDestroy(&pixd);
00333 #endif
00334 
00335     boxDestroy(&box);
00336     pixDestroy(&pixt);
00337     pixDestroy(&pixt1);
00338     pixDestroy(&pixt2);
00339     pixDestroy(&pixs);
00340 
00341        /* Thresholding with fixed and arbitrary bin boundaries */
00342     pixa = pixaCreate(0);
00343     pixs = pixRead("stampede2.jpg");
00344     pixSaveTiled(pixs, pixa, 1, 1, 20, 8);
00345     pixt = pixThresholdTo4bpp(pixs, 5, 1);
00346     pixSaveTiled(pixt, pixa, 1, 0, 20, 0);
00347     pixDestroy(&pixt);
00348     pixt = pixThresholdTo4bpp(pixs, 7, 1);
00349     cmap = pixGetColormap(pixt);
00350     pixcmapWriteStream(stderr, cmap);
00351     pixSaveTiled(pixt, pixa, 1, 0, 20, 0);
00352     pixDestroy(&pixt);
00353     pixt = pixThresholdTo4bpp(pixs, 11, 1);
00354     pixSaveTiled(pixt, pixa, 1, 0, 20, 0);
00355     pixDestroy(&pixt);
00356 
00357     pixSaveTiled(pixs, pixa, 1, 1, 20, 8);
00358     str = "45 75 115 185";
00359     pixt = pixThresholdGrayArb(pixs, str, 8, 0, 0, 0);
00360     pixSaveTiled(pixt, pixa, 1, 0, 20, 0);
00361     pixDestroy(&pixt);
00362     str = "38 65 85 115 160 210";
00363     pixt = pixThresholdGrayArb(pixs, str, 8, 0, 1, 1);
00364     cmap = pixGetColormap(pixt);
00365     pixcmapWriteStream(stderr, cmap);
00366     pixSaveTiled(pixt, pixa, 1, 0, 20, 0);
00367     pixDestroy(&pixt);
00368     str = "38 60 75 90 110 130 155 185 208 239";
00369     pixt = pixThresholdGrayArb(pixs, str, 8, 0, 0, 0);
00370     pixSaveTiled(pixt, pixa, 1, 0, 20, 0);
00371     pixDestroy(&pixt);
00372 
00373     pixSaveTiled(pixs, pixa, 1, 1, 20, 8);
00374     str = "45 75 115 185";
00375     pixt = pixThresholdGrayArb(pixs, str, 0, 1, 0, 1);
00376     pixSaveTiled(pixt, pixa, 1, 0, 20, 0);
00377     pixDestroy(&pixt);
00378     str = "38 65 85 115 160 210";
00379     pixt = pixThresholdGrayArb(pixs, str, 0, 1, 0, 1);
00380     cmap = pixGetColormap(pixt);
00381     pixcmapWriteStream(stderr, cmap);
00382     pixSaveTiled(pixt, pixa, 1, 0, 20, 0);
00383     pixDestroy(&pixt);
00384     str = "38 60 75 90 110 130 155 185 208 239";
00385     pixt = pixThresholdGrayArb(pixs, str, 4, 1, 0, 1);
00386     pixSaveTiled(pixt, pixa, 1, 0, 20, 0);
00387     pixDestroy(&pixt);
00388 
00389     pixd3 = pixaDisplay(pixa, 0, 0);
00390     pixDisplay(pixd3, 100, 100);
00391     pixWrite("/tmp/junkpixd3.jpg", pixd3, IFF_JFIF_JPEG);
00392     pixDestroy(&pixd3);
00393     pixaDestroy(&pixa);
00394 
00395     pixDestroy(&pixs);
00396 
00397     return 0;
00398 }
00399 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines