File: colorquant_reg.c

package info (click to toggle)
leptonlib 1.57-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 9,724 kB
  • ctags: 3,900
  • sloc: ansic: 89,715; sh: 3,516; makefile: 718
file content (215 lines) | stat: -rw-r--r-- 7,624 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*====================================================================*
 -  Copyright (C) 2001 Leptonica.  All rights reserved.
 -  This software is distributed in the hope that it will be
 -  useful, but with NO WARRANTY OF ANY KIND.
 -  No author or distributor accepts responsibility to anyone for the
 -  consequences of using this software, or for whether it serves any
 -  particular purpose or works at all, unless he or she says so in
 -  writing.  Everyone is granted permission to copy, modify and
 -  redistribute this source code, for commercial or non-commercial
 -  purposes, with the following restrictions: (1) the origin of this
 -  source code must not be misrepresented; (2) modified versions must
 -  be plainly marked as such; and (3) this notice may not be removed
 -  or altered from any source or modified source distribution.
 *====================================================================*/

/*
 * colorquant_reg.c
 *
 *    Regression test for various color quantizers
 */

#include <stdio.h>
#include <stdlib.h>
#include "allheaders.h"

static const l_int32 SPACE = 30;
static const l_int32 MAX_WIDTH = 350;
static const char *image[4] = {"marge.jpg",
                               "test24.jpg",
                               "juditharismax.jpg",
                               "hardlight2_2.jpg"};

static l_int32 TestImage(const char *filename, l_int32 i);
static void PixSave32(PIXA *pixa, PIX *pixc);

main(int    argc,
     char **argv)
{
l_int32  i;
static char  mainName[] = "colorquant_reg";

    if (argc != 1)
	exit(ERROR_INT(" Syntax: colorquant_reg", mainName, 1));

    for (i = 0; i < 4; i++) {
/*        if (i != 2) continue; */
        TestImage(image[i], i);
    }
    return 0;
}


static l_int32
TestImage(const char *filename,
          l_int32     i)
{
char       buf[256];
l_int32    w, h, nerrors;
l_float32  factor;
PIX       *pix, *pixs, *pixc, *pix32, *pixt, *pixd;
PIXA      *pixa;
char      *fileout;

    PROCNAME("TestImage");

    if ((pix = pixRead(filename)) == NULL)
        return ERROR_INT("pix not made", procName, 1);
    pixGetDimensions(pix, &w, &h, NULL);
    if (w > MAX_WIDTH) {
        factor = (l_float32)MAX_WIDTH / (l_float32)w;
        pixs = pixScale(pix, factor, factor);
    }
    else
        pixs = pixClone(pix);
    pixDestroy(&pix);

    pixa = pixaCreate(0);

        /* Median cut quantizer (no dither; 5 sigbits) */
    pixSaveTiled(pixs, pixa, 1, 1, SPACE, 32);
    pixc = pixMedianCutQuantGeneral(pixs, 0, 0, 16, 5, 1);
    PixSave32(pixa, pixc);
    pixc = pixMedianCutQuantGeneral(pixs, 0, 0, 128, 5, 1);
    PixSave32(pixa, pixc);
    pixc = pixMedianCutQuantGeneral(pixs, 0, 0, 256, 5, 1);
    PixSave32(pixa, pixc);

        /* Median cut quantizer (with dither; 5 sigbits) */
    pixSaveTiled(pixs, pixa, 1, 1, SPACE, 0);
    pixc = pixMedianCutQuantGeneral(pixs, 1, 0, 16, 5, 1);
    PixSave32(pixa, pixc);
    pixc = pixMedianCutQuantGeneral(pixs, 1, 0, 128, 5, 1);
    PixSave32(pixa, pixc);
    pixc = pixMedianCutQuantGeneral(pixs, 1, 0, 256, 5, 1);
    PixSave32(pixa, pixc);

        /* Median cut quantizer (no dither; 6 sigbits) */
    pixSaveTiled(pixs, pixa, 1, 1, SPACE, 32);
    pixc = pixMedianCutQuantGeneral(pixs, 0, 0, 16, 6, 1);
    PixSave32(pixa, pixc);
    pixc = pixMedianCutQuantGeneral(pixs, 0, 0, 128, 6, 1);
    PixSave32(pixa, pixc);
    pixc = pixMedianCutQuantGeneral(pixs, 0, 0, 256, 6, 1);
    PixSave32(pixa, pixc);

        /* Median cut quantizer (with dither; 6 sigbits) */
    pixSaveTiled(pixs, pixa, 1, 1, SPACE, 0);
    pixc = pixMedianCutQuantGeneral(pixs, 1, 0, 16, 6, 1);
    PixSave32(pixa, pixc);
    pixc = pixMedianCutQuantGeneral(pixs, 1, 0, 128, 6, 1);
    PixSave32(pixa, pixc);
    pixc = pixMedianCutQuantGeneral(pixs, 1, 0, 256, 6, 10);
    PixSave32(pixa, pixc);

        /* Simple 1-pass octree quantizer */
    pixSaveTiled(pixs, pixa, 1, 1, SPACE, 0);
    pixc = pixColorQuant1Pass(pixs, 0);  /* no dither */
    PixSave32(pixa, pixc);
    pixc = pixColorQuant1Pass(pixs, 1);  /* dither */
    PixSave32(pixa, pixc);

        /* 2-pass octree quantizer */
    pixSaveTiled(pixs, pixa, 1, 1, SPACE, 0);
    pixc = pixOctreeColorQuant(pixs, 128, 0);  /* no dither */
    PixSave32(pixa, pixc);
    pixc = pixOctreeColorQuant(pixs, 256, 0);  /* no dither */
    PixSave32(pixa, pixc);
    pixc = pixOctreeColorQuant(pixs, 128, 1);  /* dither */
    PixSave32(pixa, pixc);
    pixc = pixOctreeColorQuant(pixs, 256, 1);  /* dither */
    PixSave32(pixa, pixc);

        /* Simple adaptive quantization to 4 or 8 bpp */
    pixSaveTiled(pixs, pixa, 1, 1, SPACE, 0);
    pixc = pixOctreeQuant(pixs, 8, 0);    /* fixed: 8 colors */
    PixSave32(pixa, pixc);
    pixc = pixOctreeQuant(pixs, 16, 0);   /* fixed: 16 colors */
    PixSave32(pixa, pixc);
    pixc = pixOctreeQuant(pixs, 64, 0);   /* fixed: 64 colors */
    PixSave32(pixa, pixc);
    pixc = pixOctreeQuant(pixs, 256, 0);   /* fixed: 256 colors */
    PixSave32(pixa, pixc);

        /* Quantize to fully populated octree (RGB) at given level */
    pixSaveTiled(pixs, pixa, 1, 1, SPACE, 0);
    pixc = pixFixedOctcubeQuantRGB(pixs, 2);  /* level 2 */
    PixSave32(pixa, pixc);
    pixc = pixFixedOctcubeQuantRGB(pixs, 3);  /* level 3 */
    PixSave32(pixa, pixc);
    pixc = pixFixedOctcubeQuantRGB(pixs, 4);  /* level 4 */
    PixSave32(pixa, pixc);
    pixc = pixFixedOctcubeQuantRGB(pixs, 5);  /* level 5 */
    PixSave32(pixa, pixc);

        /* Generate 32 bpp RGB image with num colors <= 256 */
    pixt = pixOctreeQuant(pixs, 256, 0);   /* cmapped version */
    pix32 = pixRemoveColormap(pixt, REMOVE_CMAP_BASED_ON_SRC);

        /* Quantize image with few colors at fixed octree leaf level */
    pixSaveTiled(pixt, pixa, 1, 1, SPACE, 0);
    pixc = pixFixedOctcubeQuantCmap(pix32, 2);   /* level 2 */
    PixSave32(pixa, pixc);
    pixc = pixFixedOctcubeQuantCmap(pix32, 3);   /* level 3 */
    PixSave32(pixa, pixc);
    pixc = pixFixedOctcubeQuantCmap(pix32, 4);   /* level 4 */
    PixSave32(pixa, pixc);
    pixc = pixFixedOctcubeQuantCmap(pix32, 5);   /* level 5 */
    PixSave32(pixa, pixc);

        /* Mixed color/gray octree quantizer */
    pixSaveTiled(pixt, pixa, 1, 1, SPACE, 0);
    pixc = pixOctcubeQuantMixed(pix32, 8, 64, 10);   /* max delta = 10 */
    PixSave32(pixa, pixc);
    pixc = pixOctcubeQuantMixed(pix32, 8, 64, 30);   /* max delta = 30 */
    PixSave32(pixa, pixc);
    pixc = pixOctcubeQuantMixed(pix32, 8, 64, 50);   /* max delta = 50 */
    PixSave32(pixa, pixc);
    
        /* Run the high-level converter */
    pixSaveTiled(pixt, pixa, 1, 1, SPACE, 0);
    pixc = pixConvertRGBToColormap(pix32, 4, &nerrors);    /* level 4 */
    if (nerrors) fprintf(stderr, "Number errors at level 4: %d\n", nerrors);
    PixSave32(pixa, pixc);
    pixc = pixConvertRGBToColormap(pix32, 5, &nerrors);    /* level 5 */
    if (nerrors) fprintf(stderr, "Number errors at level 5: %d\n", nerrors);
    PixSave32(pixa, pixc);
    pixc = pixConvertRGBToColormap(pix32, 6, &nerrors);    /* level 6 */
    if (nerrors) fprintf(stderr, "Number errors at level 6: %d\n", nerrors);
    PixSave32(pixa, pixc);

    pixDestroy(&pix32);
    pixDestroy(&pixt);

    pixd = pixaDisplay(pixa, 0, 0);
    pixDisplay(pixd, 100, 100);
    sprintf(buf, "junkdisp.%d.jpg", i);
    pixWrite(buf, pixd, IFF_JFIF_JPEG);

    pixDestroy(&pixs);
    pixDestroy(&pixd);
    pixaDestroy(&pixa);

    return 0;
}

static void
PixSave32(PIXA *pixa, PIX *pixc)
{
PIX  *pix32;
    pix32 = pixConvertTo32(pixc);
    pixSaveTiled(pix32, pixa, 1, 0, SPACE, 0);
    pixDestroy(&pixc);
    pixDestroy(&pix32);
}