Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 486320
Collapse All | Expand All

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

Return to bug 486320