Leptonica 1.68
C Image Processing Library

cmapquant_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  * cmapquant_reg.c
00018  *
00019  *   Tests quantization of rgb image to a specific colormap.
00020  *   Does this by starting with a grayscale image, doing a grayscale
00021  *   quantization with a colormap in the dest, then adding new
00022  *   colors, scaling (which removes the colormap), and finally
00023  *   re-quantizing back to the original colormap.
00024  */
00025 
00026 #include "allheaders.h"
00027 
00028 #define  LEVEL       3
00029 #define  MIN_DEPTH   4
00030 
00031 main(int    argc,
00032      char **argv)
00033 {
00034 l_int32      same;
00035 l_uint32    *rtab, *gtab, *btab;
00036 l_int32     *cmaptab;
00037 BOX         *box;
00038 PIX         *pixs, *pixt1, *pixt2, *pixt3, *pixt4;
00039 PIXCMAP     *cmap;
00040 static char  mainName[] = "cmapquant_reg";
00041 
00042     pixs = pixRead("lucasta-frag.jpg");
00043     if (argc != 1)
00044         exit(ERROR_INT("syntax: cmapquant_req", mainName, 1));
00045 
00046         /* Convert to 4 bpp with 6 levels and a colormap */
00047     pixt1 = pixThresholdTo4bpp(pixs, 6, 1);
00048 
00049         /* Color some non-white pixels, preserving antialiasing, and
00050          * adding these colors to the colormap */
00051     box = boxCreate(120, 30, 200, 200);
00052     pixColorGray(pixt1, box, L_PAINT_DARK, 220, 0, 0, 255);
00053     pixDisplayWrite(pixt1, 1);
00054     boxDestroy(&box);
00055 
00056         /* Scale up by 1.5; losing the colormap */
00057     startTimer();
00058     pixt2 = pixScale(pixt1, 1.5, 1.5);
00059     fprintf(stderr, "Time to scale by 1.5x = %7.3f sec\n", stopTimer());
00060     pixDisplayWrite(pixt2, 1);
00061 
00062         /* Re-quantize using the same colormap */
00063     startTimer();
00064     cmap = pixGetColormap(pixt1);
00065     pixt3 = pixOctcubeQuantFromCmap(pixt2, cmap, MIN_DEPTH,
00066                                     LEVEL, L_EUCLIDEAN_DISTANCE);
00067     fprintf(stderr, "Time to requantize to cmap = %7.3f sec\n", stopTimer());
00068     pixDisplayWrite(pixt3, 1);
00069 
00070         /* Re-quantize first making the tables and then 
00071          * using the lower-level function */
00072     startTimer();
00073     makeRGBToIndexTables(&rtab, &gtab, &btab, LEVEL);
00074     cmaptab = pixcmapToOctcubeLUT(cmap, LEVEL, L_EUCLIDEAN_DISTANCE);
00075     fprintf(stderr, "Time to make tables = %7.3f sec\n", stopTimer());
00076     startTimer();
00077     pixt4 = pixOctcubeQuantFromCmapLUT(pixt2, cmap, MIN_DEPTH,
00078                                        cmaptab, rtab, gtab, btab);
00079     fprintf(stderr, "Time for lowlevel re-quant = %7.3f sec\n", stopTimer());
00080     pixDisplayWrite(pixt4, 1);
00081 
00082     pixEqual(pixt3, pixt4, &same);
00083     if (same)
00084         fprintf(stderr, "Correct: images are the same\n");
00085     else
00086         fprintf(stderr, "Error: images differ\n");
00087     lept_free(cmaptab);
00088     lept_free(rtab);
00089     lept_free(gtab);
00090     lept_free(btab);
00091 
00092     pixDisplayMultiple("/tmp/junk_write_display*");
00093 
00094     pixDestroy(&pixs);
00095     pixDestroy(&pixt1);
00096     pixDestroy(&pixt2);
00097     pixDestroy(&pixt3);
00098     pixDestroy(&pixt4);
00099     return 0;
00100 }
00101 
00102 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines