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