Leptonica 1.68
C Image Processing Library

dithertest.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  * dithertest.c
00018  *
00019  *    Input is 8 bpp grayscale
00020  *    Output file is PostScript, 2 bpp dithered
00021  */
00022 
00023 #include "allheaders.h"
00024 
00025 static const l_float32  FACTOR = 0.95;
00026 static const l_float32  GAMMA = 1.0;
00027 
00028 
00029 main(int    argc,
00030      char **argv)
00031 {
00032 char        *filein, *fileout;
00033 l_int32      w, h;
00034 l_float32    scale;
00035 FILE        *fp;
00036 PIX         *pix, *pixs, *pixd;
00037 PIXCMAP     *cmap;
00038 static char  mainName[] = "dithertest";
00039 
00040     if (argc != 3)
00041         exit(ERROR_INT(" Syntax:  dithertest filein fileout", mainName, 1));
00042 
00043     filein = argv[1];
00044     fileout = argv[2];
00045 
00046     if ((pix = pixRead(filein)) == NULL)
00047         exit(ERROR_INT("pix not made", mainName, 1));
00048     if (pixGetDepth(pix) != 8)
00049         exit(ERROR_INT("pix not 8 bpp", mainName, 1));
00050     pixs = pixGammaTRC(NULL, pix, GAMMA, 0, 255);
00051 
00052     startTimer();
00053     pixd = pixDitherToBinary(pixs);
00054     fprintf(stderr, " time for binarized dither = %7.3f sec\n", stopTimer());
00055     pixDisplayWrite(pixd, 1);
00056     pixDestroy(&pixd);
00057 
00058          /* Dither to 2 bpp, with colormap */
00059     startTimer();
00060     pixd = pixDitherTo2bpp(pixs, 1);
00061     fprintf(stderr, " time for dither = %7.3f sec\n", stopTimer());
00062     pixDisplayWrite(pixd, 1);
00063     cmap = pixGetColormap(pixd);
00064     pixcmapWriteStream(stderr, cmap);
00065     pixDestroy(&pixd);
00066 
00067          /* Dither to 2 bpp, without colormap */
00068     startTimer();
00069     pixd = pixDitherTo2bpp(pixs, 0);
00070     fprintf(stderr, " time for dither = %7.3f sec\n", stopTimer());
00071     pixDisplayWrite(pixd, 1);
00072     pixDestroy(&pixd);
00073 
00074          /* Dither to 2 bpp, without colormap; output in PostScript */
00075     pixd = pixDitherTo2bpp(pixs, 0);
00076     w = pixGetWidth(pixs);
00077     h = pixGetHeight(pixs);
00078     scale = L_MIN(FACTOR * 2550 / w, FACTOR * 3300 / h);
00079     fp = lept_fopen(fileout, "wb+");
00080     pixWriteStreamPS(fp, pixd, NULL, 300, scale);
00081     lept_fclose(fp);
00082     pixDestroy(&pixd);
00083 
00084         /* Dither 2x upscale to 1 bpp */
00085     startTimer();
00086     pixd = pixScaleGray2xLIDither(pixs);
00087     fprintf(stderr, " time for scale/dither = %7.3f sec\n", stopTimer());
00088     pixDisplayWrite(pixd, 1);
00089     pixDestroy(&pixd);
00090 
00091         /* Dither 4x upscale to 1 bpp */
00092     startTimer();
00093     pixd = pixScaleGray4xLIDither(pixs);
00094     fprintf(stderr, " time for scale/dither = %7.3f sec\n", stopTimer());
00095     pixDisplayWrite(pixd, 1);
00096     pixDestroy(&pixd);
00097 
00098     pixDisplayMultiple("/tmp/junk_write_display*");
00099 
00100     pixDestroy(&pix);
00101     pixDestroy(&pixs);
00102     return 0;
00103 }
00104 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines