1 /*====================================================================*
2  -  Copyright (C) 2001 Leptonica.  All rights reserved.
3  -
4  -  Redistribution and use in source and binary forms, with or without
5  -  modification, are permitted provided that the following conditions
6  -  are met:
7  -  1. Redistributions of source code must retain the above copyright
8  -     notice, this list of conditions and the following disclaimer.
9  -  2. Redistributions in binary form must reproduce the above
10  -     copyright notice, this list of conditions and the following
11  -     disclaimer in the documentation and/or other materials
12  -     provided with the distribution.
13  -
14  -  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  -  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  -  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  -  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ANY
18  -  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  -  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  -  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  -  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  -  OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  -  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  -  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *====================================================================*/
26 
27 /*
28  * cmapquant_reg.c
29  *
30  *   Tests quantization of rgb image to a specific colormap.
31  *   Does this by starting with a grayscale image, doing a grayscale
32  *   quantization with a colormap in the dest, then adding new
33  *   colors, scaling (which removes the colormap), and finally
34  *   re-quantizing back to the original colormap.
35  */
36 
37 #include "allheaders.h"
38 
39 #define  LEVEL       3
40 #define  MIN_DEPTH   4
41 
main(int argc,char ** argv)42 int main(int    argc,
43          char **argv)
44 {
45 BOX          *box;
46 PIX          *pixs, *pix1, *pix2, *pix3, *pix4, *pix5, *pix6, *pix7;
47 PIX          *pix8, *pix9;
48 PIXCMAP      *cmap;
49 L_REGPARAMS  *rp;
50 
51    if (regTestSetup(argc, argv, &rp))
52         return 1;
53 
54     pixs = pixRead("lucasta-frag.jpg");
55 
56         /* Convert to 4 bpp with 6 levels and a colormap */
57     pix1 = pixThresholdTo4bpp(pixs, 6, 1);
58 
59         /* Color some non-white pixels, preserving antialiasing, and
60          * adding these colors to the colormap */
61     box = boxCreate(120, 30, 200, 200);
62     pixColorGray(pix1, box, L_PAINT_DARK, 220, 0, 0, 255);
63     regTestWritePixAndCheck(rp, pix1, IFF_PNG);  /* 0 */
64     pixDisplayWithTitle(pix1, 0, 0, NULL, rp->display);
65     boxDestroy(&box);
66 
67         /* Scale up by 1.5; losing the colormap */
68     pix2 = pixScale(pix1, 1.5, 1.5);
69     regTestWritePixAndCheck(rp, pix2, IFF_JFIF_JPEG);  /* 1 */
70     pixDisplayWithTitle(pix2, 0, 0, NULL, rp->display);
71 
72         /* Octcube quantize using the same colormap */
73     startTimer();
74     cmap = pixGetColormap(pix1);
75     pix3 = pixOctcubeQuantFromCmap(pix2, cmap, MIN_DEPTH,
76                                    LEVEL, L_EUCLIDEAN_DISTANCE);
77     fprintf(stderr, "Time to re-quantize to cmap = %7.3f sec\n", stopTimer());
78     regTestWritePixAndCheck(rp, pix3, IFF_PNG);  /* 2 */
79     pixDisplayWithTitle(pix3, 0, 0, NULL, rp->display);
80 
81         /* Convert the quantized image to rgb */
82     pix4 = pixConvertTo32(pix3);
83 
84         /* Re-quantize using median cut */
85     pix5 = pixMedianCutQuant(pix4, 0);
86     regTestWritePixAndCheck(rp, pix5, IFF_PNG);  /* 3 */
87     pixDisplayWithTitle(pix5, 0, 0, NULL, rp->display);
88 
89         /* Re-quantize to few colors using median cut */
90     pix6 = pixFewColorsMedianCutQuantMixed(pix4, 30, 30, 100, 0, 0, 0);
91     regTestWritePixAndCheck(rp, pix6, IFF_PNG);  /* 4 */
92     pixDisplayWithTitle(pix6, 0, 0, NULL, rp->display);
93 
94         /* Octcube quantize mixed with gray */
95     startTimer();
96     pix7 = pixOctcubeQuantMixedWithGray(pix2, 4, 5, 5);
97     fprintf(stderr, "Time to re-quantize mixed = %7.3f sec\n", stopTimer());
98     regTestWritePixAndCheck(rp, pix7, IFF_PNG);  /* 5 */
99     pixDisplayWithTitle(pix7, 0, 0, NULL, rp->display);
100 
101         /* Fixed octcube quantization */
102     startTimer();
103     pix8 = pixFixedOctcubeQuant256(pix2, 0);
104     fprintf(stderr, "Time to re-quantize 256 = %7.3f sec\n", stopTimer());
105     regTestWritePixAndCheck(rp, pix8, IFF_PNG);  /* 6 */
106     pixDisplayWithTitle(pix8, 0, 0, NULL, rp->display);
107 
108         /* Remove unused colors */
109     startTimer();
110     pix9 = pixCopy(NULL, pix8);
111     pixRemoveUnusedColors(pix9);
112     fprintf(stderr, "Time to remove unused colors = %7.3f sec\n", stopTimer());
113     regTestWritePixAndCheck(rp, pix9, IFF_PNG);  /* 7 */
114     pixDisplayWithTitle(pix8, 0, 0, NULL, rp->display);
115 
116          /* Compare before and after colors removed */
117     regTestComparePix(rp, pix8, pix9);  /* 8 */
118 
119     pixDestroy(&pixs);
120     pixDestroy(&pix1);
121     pixDestroy(&pix2);
122     pixDestroy(&pix3);
123     pixDestroy(&pix4);
124     pixDestroy(&pix5);
125     pixDestroy(&pix6);
126     pixDestroy(&pix7);
127     pixDestroy(&pix8);
128     pixDestroy(&pix9);
129     return regTestCleanup(rp);
130 }
131 
132 
133