Line 0
Link Here
|
|
|
1 |
/***************************************************************************** |
2 |
* "Gif-Lib" - Yet another gif library. |
3 |
* |
4 |
* Written by: Gershon Elber IBM PC Ver 0.1, Jun. 1989 |
5 |
****************************************************************************** |
6 |
* Module to quatize high resolution image into lower one. You may want to |
7 |
* peek into the following article this code is based on: |
8 |
* "Color Image Quantization for frame buffer Display", by Paul Heckbert |
9 |
* SIGGRAPH 1982 page 297-307. |
10 |
****************************************************************************** |
11 |
* History: |
12 |
* 5 Jan 90 - Version 1.0 by Gershon Elber. |
13 |
*****************************************************************************/ |
14 |
|
15 |
#ifdef HAVE_CONFIG_H |
16 |
#include <config.h> |
17 |
#endif |
18 |
|
19 |
#ifdef __MSDOS__ |
20 |
#include <dos.h> |
21 |
#include <alloc.h> |
22 |
#include <graphics.h> |
23 |
#endif /* __MSDOS__ */ |
24 |
|
25 |
#include <stdlib.h> |
26 |
#include <stdio.h> |
27 |
#include "gif_lib.h" |
28 |
#include "gif_lib_private.h" |
29 |
|
30 |
#define ABS(x) ((x) > 0 ? (x) : (-(x))) |
31 |
|
32 |
#define PROGRAM_NAME "giflib" |
33 |
|
34 |
/* The colors are stripped to 5 bits per primary color if non MSDOS system |
35 |
* or to 4 (not enough memory...) if MSDOS as first step. |
36 |
*/ |
37 |
#ifdef __MSDOS__ |
38 |
#define COLOR_ARRAY_SIZE 4096 |
39 |
#define BITS_PER_PRIM_COLOR 4 |
40 |
#define MAX_PRIM_COLOR 0x0f |
41 |
#else |
42 |
#define COLOR_ARRAY_SIZE 32768 |
43 |
#define BITS_PER_PRIM_COLOR 5 |
44 |
#define MAX_PRIM_COLOR 0x1f |
45 |
#endif /* __MSDOS__ */ |
46 |
|
47 |
static int SortRGBAxis; |
48 |
|
49 |
typedef struct QuantizedColorType { |
50 |
GifByteType RGB[3]; |
51 |
GifByteType NewColorIndex; |
52 |
long Count; |
53 |
struct QuantizedColorType *Pnext; |
54 |
} QuantizedColorType; |
55 |
|
56 |
typedef struct NewColorMapType { |
57 |
GifByteType RGBMin[3], RGBWidth[3]; |
58 |
unsigned int NumEntries; /* # of QuantizedColorType in linked list below */ |
59 |
unsigned long Count; /* Total number of pixels in all the entries */ |
60 |
QuantizedColorType *QuantizedColors; |
61 |
} NewColorMapType; |
62 |
|
63 |
static int SubdivColorMap(NewColorMapType * NewColorSubdiv, |
64 |
unsigned int ColorMapSize, |
65 |
unsigned int *NewColorMapSize); |
66 |
static int SortCmpRtn(const VoidPtr Entry1, const VoidPtr Entry2); |
67 |
|
68 |
/****************************************************************************** |
69 |
* Quantize high resolution image into lower one. Input image consists of a |
70 |
* 2D array for each of the RGB colors with size Width by Height. There is no |
71 |
* Color map for the input. Output is a quantized image with 2D array of |
72 |
* indexes into the output color map. |
73 |
* Note input image can be 24 bits at the most (8 for red/green/blue) and |
74 |
* the output has 256 colors at the most (256 entries in the color map.). |
75 |
* ColorMapSize specifies size of color map up to 256 and will be updated to |
76 |
* real size before returning. |
77 |
* Also non of the parameter are allocated by this routine. |
78 |
* This function returns GIF_OK if succesfull, GIF_ERROR otherwise. |
79 |
******************************************************************************/ |
80 |
int |
81 |
QuantizeBuffer(unsigned int Width, |
82 |
unsigned int Height, |
83 |
int *ColorMapSize, |
84 |
GifByteType * RedInput, |
85 |
GifByteType * GreenInput, |
86 |
GifByteType * BlueInput, |
87 |
GifByteType * OutputBuffer, |
88 |
GifColorType * OutputColorMap) { |
89 |
|
90 |
unsigned int Index, NumOfEntries; |
91 |
int i, j, MaxRGBError[3]; |
92 |
unsigned int NewColorMapSize; |
93 |
long Red, Green, Blue; |
94 |
NewColorMapType NewColorSubdiv[256]; |
95 |
QuantizedColorType *ColorArrayEntries, *QuantizedColor; |
96 |
|
97 |
ColorArrayEntries = (QuantizedColorType *)malloc( |
98 |
sizeof(QuantizedColorType) * COLOR_ARRAY_SIZE); |
99 |
if (ColorArrayEntries == NULL) { |
100 |
_GifError = E_GIF_ERR_NOT_ENOUGH_MEM; |
101 |
return GIF_ERROR; |
102 |
} |
103 |
|
104 |
for (i = 0; i < COLOR_ARRAY_SIZE; i++) { |
105 |
ColorArrayEntries[i].RGB[0] = i >> (2 * BITS_PER_PRIM_COLOR); |
106 |
ColorArrayEntries[i].RGB[1] = (i >> BITS_PER_PRIM_COLOR) & |
107 |
MAX_PRIM_COLOR; |
108 |
ColorArrayEntries[i].RGB[2] = i & MAX_PRIM_COLOR; |
109 |
ColorArrayEntries[i].Count = 0; |
110 |
} |
111 |
|
112 |
/* Sample the colors and their distribution: */ |
113 |
for (i = 0; i < (int)(Width * Height); i++) { |
114 |
Index = ((RedInput[i] >> (8 - BITS_PER_PRIM_COLOR)) << |
115 |
(2 * BITS_PER_PRIM_COLOR)) + |
116 |
((GreenInput[i] >> (8 - BITS_PER_PRIM_COLOR)) << |
117 |
BITS_PER_PRIM_COLOR) + |
118 |
(BlueInput[i] >> (8 - BITS_PER_PRIM_COLOR)); |
119 |
ColorArrayEntries[Index].Count++; |
120 |
} |
121 |
|
122 |
/* Put all the colors in the first entry of the color map, and call the |
123 |
* recursive subdivision process. */ |
124 |
for (i = 0; i < 256; i++) { |
125 |
NewColorSubdiv[i].QuantizedColors = NULL; |
126 |
NewColorSubdiv[i].Count = NewColorSubdiv[i].NumEntries = 0; |
127 |
for (j = 0; j < 3; j++) { |
128 |
NewColorSubdiv[i].RGBMin[j] = 0; |
129 |
NewColorSubdiv[i].RGBWidth[j] = 255; |
130 |
} |
131 |
} |
132 |
|
133 |
/* Find the non empty entries in the color table and chain them: */ |
134 |
for (i = 0; i < COLOR_ARRAY_SIZE; i++) |
135 |
if (ColorArrayEntries[i].Count > 0) |
136 |
break; |
137 |
QuantizedColor = NewColorSubdiv[0].QuantizedColors = &ColorArrayEntries[i]; |
138 |
NumOfEntries = 1; |
139 |
while (++i < COLOR_ARRAY_SIZE) |
140 |
if (ColorArrayEntries[i].Count > 0) { |
141 |
QuantizedColor->Pnext = &ColorArrayEntries[i]; |
142 |
QuantizedColor = &ColorArrayEntries[i]; |
143 |
NumOfEntries++; |
144 |
} |
145 |
QuantizedColor->Pnext = NULL; |
146 |
|
147 |
NewColorSubdiv[0].NumEntries = NumOfEntries; /* Different sampled colors */ |
148 |
NewColorSubdiv[0].Count = ((long)Width) * Height; /* Pixels */ |
149 |
NewColorMapSize = 1; |
150 |
if (SubdivColorMap(NewColorSubdiv, *ColorMapSize, &NewColorMapSize) != |
151 |
GIF_OK) { |
152 |
free((char *)ColorArrayEntries); |
153 |
return GIF_ERROR; |
154 |
} |
155 |
if (NewColorMapSize < *ColorMapSize) { |
156 |
/* And clear rest of color map: */ |
157 |
for (i = NewColorMapSize; i < *ColorMapSize; i++) |
158 |
OutputColorMap[i].Red = OutputColorMap[i].Green = |
159 |
OutputColorMap[i].Blue = 0; |
160 |
} |
161 |
|
162 |
/* Average the colors in each entry to be the color to be used in the |
163 |
* output color map, and plug it into the output color map itself. */ |
164 |
for (i = 0; i < NewColorMapSize; i++) { |
165 |
if ((j = NewColorSubdiv[i].NumEntries) > 0) { |
166 |
QuantizedColor = NewColorSubdiv[i].QuantizedColors; |
167 |
Red = Green = Blue = 0; |
168 |
while (QuantizedColor) { |
169 |
QuantizedColor->NewColorIndex = i; |
170 |
Red += QuantizedColor->RGB[0]; |
171 |
Green += QuantizedColor->RGB[1]; |
172 |
Blue += QuantizedColor->RGB[2]; |
173 |
QuantizedColor = QuantizedColor->Pnext; |
174 |
} |
175 |
OutputColorMap[i].Red = (Red << (8 - BITS_PER_PRIM_COLOR)) / j; |
176 |
OutputColorMap[i].Green = (Green << (8 - BITS_PER_PRIM_COLOR)) / j; |
177 |
OutputColorMap[i].Blue = (Blue << (8 - BITS_PER_PRIM_COLOR)) / j; |
178 |
} else |
179 |
fprintf(stderr, |
180 |
"\n%s: Null entry in quantized color map - that's weird.\n", |
181 |
PROGRAM_NAME); |
182 |
} |
183 |
|
184 |
/* Finally scan the input buffer again and put the mapped index in the |
185 |
* output buffer. */ |
186 |
MaxRGBError[0] = MaxRGBError[1] = MaxRGBError[2] = 0; |
187 |
for (i = 0; i < (int)(Width * Height); i++) { |
188 |
Index = ((RedInput[i] >> (8 - BITS_PER_PRIM_COLOR)) << |
189 |
(2 * BITS_PER_PRIM_COLOR)) + |
190 |
((GreenInput[i] >> (8 - BITS_PER_PRIM_COLOR)) << |
191 |
BITS_PER_PRIM_COLOR) + |
192 |
(BlueInput[i] >> (8 - BITS_PER_PRIM_COLOR)); |
193 |
Index = ColorArrayEntries[Index].NewColorIndex; |
194 |
OutputBuffer[i] = Index; |
195 |
if (MaxRGBError[0] < ABS(OutputColorMap[Index].Red - RedInput[i])) |
196 |
MaxRGBError[0] = ABS(OutputColorMap[Index].Red - RedInput[i]); |
197 |
if (MaxRGBError[1] < ABS(OutputColorMap[Index].Green - GreenInput[i])) |
198 |
MaxRGBError[1] = ABS(OutputColorMap[Index].Green - GreenInput[i]); |
199 |
if (MaxRGBError[2] < ABS(OutputColorMap[Index].Blue - BlueInput[i])) |
200 |
MaxRGBError[2] = ABS(OutputColorMap[Index].Blue - BlueInput[i]); |
201 |
} |
202 |
|
203 |
#ifdef DEBUG |
204 |
fprintf(stderr, |
205 |
"Quantization L(0) errors: Red = %d, Green = %d, Blue = %d.\n", |
206 |
MaxRGBError[0], MaxRGBError[1], MaxRGBError[2]); |
207 |
#endif /* DEBUG */ |
208 |
|
209 |
free((char *)ColorArrayEntries); |
210 |
|
211 |
*ColorMapSize = NewColorMapSize; |
212 |
|
213 |
return GIF_OK; |
214 |
} |
215 |
|
216 |
/****************************************************************************** |
217 |
* Routine to subdivide the RGB space recursively using median cut in each |
218 |
* axes alternatingly until ColorMapSize different cubes exists. |
219 |
* The biggest cube in one dimension is subdivide unless it has only one entry. |
220 |
* Returns GIF_ERROR if failed, otherwise GIF_OK. |
221 |
******************************************************************************/ |
222 |
static int |
223 |
SubdivColorMap(NewColorMapType * NewColorSubdiv, |
224 |
unsigned int ColorMapSize, |
225 |
unsigned int *NewColorMapSize) { |
226 |
|
227 |
int MaxSize; |
228 |
unsigned int i, j, Index = 0, NumEntries, MinColor, MaxColor; |
229 |
long Sum, Count; |
230 |
QuantizedColorType *QuantizedColor, **SortArray; |
231 |
|
232 |
while (ColorMapSize > *NewColorMapSize) { |
233 |
/* Find candidate for subdivision: */ |
234 |
MaxSize = -1; |
235 |
for (i = 0; i < *NewColorMapSize; i++) { |
236 |
for (j = 0; j < 3; j++) { |
237 |
if ((((int)NewColorSubdiv[i].RGBWidth[j]) > MaxSize) && |
238 |
(NewColorSubdiv[i].NumEntries > 1)) { |
239 |
MaxSize = NewColorSubdiv[i].RGBWidth[j]; |
240 |
Index = i; |
241 |
SortRGBAxis = j; |
242 |
} |
243 |
} |
244 |
} |
245 |
|
246 |
if (MaxSize == -1) |
247 |
return GIF_OK; |
248 |
|
249 |
/* Split the entry Index into two along the axis SortRGBAxis: */ |
250 |
|
251 |
/* Sort all elements in that entry along the given axis and split at |
252 |
* the median. */ |
253 |
SortArray = (QuantizedColorType **)malloc( |
254 |
sizeof(QuantizedColorType *) * |
255 |
NewColorSubdiv[Index].NumEntries); |
256 |
if (SortArray == NULL) |
257 |
return GIF_ERROR; |
258 |
for (j = 0, QuantizedColor = NewColorSubdiv[Index].QuantizedColors; |
259 |
j < NewColorSubdiv[Index].NumEntries && QuantizedColor != NULL; |
260 |
j++, QuantizedColor = QuantizedColor->Pnext) |
261 |
SortArray[j] = QuantizedColor; |
262 |
|
263 |
qsort(SortArray, NewColorSubdiv[Index].NumEntries, |
264 |
sizeof(QuantizedColorType *), SortCmpRtn); |
265 |
|
266 |
/* Relink the sorted list into one: */ |
267 |
for (j = 0; j < NewColorSubdiv[Index].NumEntries - 1; j++) |
268 |
SortArray[j]->Pnext = SortArray[j + 1]; |
269 |
SortArray[NewColorSubdiv[Index].NumEntries - 1]->Pnext = NULL; |
270 |
NewColorSubdiv[Index].QuantizedColors = QuantizedColor = SortArray[0]; |
271 |
free((char *)SortArray); |
272 |
|
273 |
/* Now simply add the Counts until we have half of the Count: */ |
274 |
Sum = NewColorSubdiv[Index].Count / 2 - QuantizedColor->Count; |
275 |
NumEntries = 1; |
276 |
Count = QuantizedColor->Count; |
277 |
while ((Sum -= QuantizedColor->Pnext->Count) >= 0 && |
278 |
QuantizedColor->Pnext != NULL && |
279 |
QuantizedColor->Pnext->Pnext != NULL) { |
280 |
QuantizedColor = QuantizedColor->Pnext; |
281 |
NumEntries++; |
282 |
Count += QuantizedColor->Count; |
283 |
} |
284 |
/* Save the values of the last color of the first half, and first |
285 |
* of the second half so we can update the Bounding Boxes later. |
286 |
* Also as the colors are quantized and the BBoxes are full 0..255, |
287 |
* they need to be rescaled. |
288 |
*/ |
289 |
MaxColor = QuantizedColor->RGB[SortRGBAxis]; /* Max. of first half */ |
290 |
MinColor = QuantizedColor->Pnext->RGB[SortRGBAxis]; /* of second */ |
291 |
MaxColor <<= (8 - BITS_PER_PRIM_COLOR); |
292 |
MinColor <<= (8 - BITS_PER_PRIM_COLOR); |
293 |
|
294 |
/* Partition right here: */ |
295 |
NewColorSubdiv[*NewColorMapSize].QuantizedColors = |
296 |
QuantizedColor->Pnext; |
297 |
QuantizedColor->Pnext = NULL; |
298 |
NewColorSubdiv[*NewColorMapSize].Count = Count; |
299 |
NewColorSubdiv[Index].Count -= Count; |
300 |
NewColorSubdiv[*NewColorMapSize].NumEntries = |
301 |
NewColorSubdiv[Index].NumEntries - NumEntries; |
302 |
NewColorSubdiv[Index].NumEntries = NumEntries; |
303 |
for (j = 0; j < 3; j++) { |
304 |
NewColorSubdiv[*NewColorMapSize].RGBMin[j] = |
305 |
NewColorSubdiv[Index].RGBMin[j]; |
306 |
NewColorSubdiv[*NewColorMapSize].RGBWidth[j] = |
307 |
NewColorSubdiv[Index].RGBWidth[j]; |
308 |
} |
309 |
NewColorSubdiv[*NewColorMapSize].RGBWidth[SortRGBAxis] = |
310 |
NewColorSubdiv[*NewColorMapSize].RGBMin[SortRGBAxis] + |
311 |
NewColorSubdiv[*NewColorMapSize].RGBWidth[SortRGBAxis] - MinColor; |
312 |
NewColorSubdiv[*NewColorMapSize].RGBMin[SortRGBAxis] = MinColor; |
313 |
|
314 |
NewColorSubdiv[Index].RGBWidth[SortRGBAxis] = |
315 |
MaxColor - NewColorSubdiv[Index].RGBMin[SortRGBAxis]; |
316 |
|
317 |
(*NewColorMapSize)++; |
318 |
} |
319 |
|
320 |
return GIF_OK; |
321 |
} |
322 |
|
323 |
/**************************************************************************** |
324 |
* Routine called by qsort to compare to entries. |
325 |
****************************************************************************/ |
326 |
static int |
327 |
SortCmpRtn(const VoidPtr Entry1, |
328 |
const VoidPtr Entry2) { |
329 |
|
330 |
return (*((QuantizedColorType **) Entry1))->RGB[SortRGBAxis] - |
331 |
(*((QuantizedColorType **) Entry2))->RGB[SortRGBAxis]; |
332 |
} |