summaryrefslogtreecommitdiff
path: root/extra/giflib/giflib-4.2.1-quantize.patch
blob: f6745d2c0bc3c14ac0beb08bc2d64ef9f4bbffc9 (plain)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
From 7b01ed05d6c16172bd11a62d9b334f0eb7c5fc1e Mon Sep 17 00:00:00 2001
From: "Eric S. Raymond" <esr@thyrsus.com>
Date: Sun, 3 Jun 2012 07:59:30 -0400
Subject: [PATCH] Return QuantizeBuffer() to the core library.

Fulfils SourceForge FR 3531618.
---
 NEWS            |   2 +
 lib/Makefile.am |   3 +-
 lib/gif_lib.h   |   9 ++
 lib/quantize.c  | 309 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 util/rgb2gif.c  | 302 ------------------------------------------------------
 5 files changed, 322 insertions(+), 303 deletions(-)
 create mode 100644 lib/quantize.c

index 813a2f8..ae6d5fa 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -13,7 +13,8 @@ libgif_la_SOURCES = dgif_lib.c  \
 					gif_hash.h	\
                     gifalloc.c  \
                     gif_err.c   \
-					gif_lib_private.h
+					gif_lib_private.h \
+		    quantize.c
 
 libgif_la_LDFLAGS = -version-info @SHLIBVERSION@ $(LT_LDFLAGS)
 libgif_la_LIBADD = @DEVS@
diff --git a/lib/gif_lib.h b/lib/gif_lib.h
index e942089..73bc437 100644
--- a/lib/gif_lib.h
+++ b/lib/gif_lib.h
@@ -207,6 +207,15 @@ int DGifGetLZCodes(GifFileType *GifFile, int *GifCode);
 
 
 /******************************************************************************
+ * O.K., here are the routines from GIF_LIB file QUANTIZE.C.              
+******************************************************************************/
+int QuantizeBuffer(unsigned int Width, unsigned int Height,
+                   int *ColorMapSize, GifByteType * RedInput,
+                   GifByteType * GreenInput, GifByteType * BlueInput,
+                   GifByteType * OutputBuffer,
+                   GifColorType * OutputColorMap);
+
+/******************************************************************************
  * O.K., here are the routines from GIF_LIB file GIF_ERR.C.              
 ******************************************************************************/
 extern int GifError(void);             /* new in 2012 - ESR */
diff --git a/lib/quantize.c b/lib/quantize.c
new file mode 100644
index 0000000..feb1dc7
--- /dev/null
+++ b/lib/quantize.c
@@ -0,0 +1,309 @@
+/*****************************************************************************
+ *
+ * quantize.c - quantize a high resolution image into lower one
+ *
+ * Based on: "Color Image Quantization for frame buffer Display", by
+ * Paul Heckbert SIGGRAPH 1982 page 297-307.
+ *
+ * This doesn't really belong in the core library, was undocumented,
+ * and was removed in 4.2.  Then it turned out some client apps were
+ * actually using it, so it was restored in 5.0.
+ *
+ *****************************************************************************/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include "gif_lib.h"
+#include "gif_lib_private.h"
+
+#define ABS(x)    ((x) > 0 ? (x) : (-(x)))
+
+#define COLOR_ARRAY_SIZE 32768
+#define BITS_PER_PRIM_COLOR 5
+#define MAX_PRIM_COLOR      0x1f
+
+static int SortRGBAxis;
+
+typedef struct QuantizedColorType {
+    GifByteType RGB[3];
+    GifByteType NewColorIndex;
+    long Count;
+    struct QuantizedColorType *Pnext;
+} QuantizedColorType;
+
+typedef struct NewColorMapType {
+    GifByteType RGBMin[3], RGBWidth[3];
+    unsigned int NumEntries; /* # of QuantizedColorType in linked list below */
+    unsigned long Count; /* Total number of pixels in all the entries */
+    QuantizedColorType *QuantizedColors;
+} NewColorMapType;
+
+static int SubdivColorMap(NewColorMapType * NewColorSubdiv,
+                          unsigned int ColorMapSize,
+                          unsigned int *NewColorMapSize);
+static int SortCmpRtn(const void *Entry1, const void *Entry2);
+
+/******************************************************************************
+ * Quantize high resolution image into lower one. Input image consists of a
+ * 2D array for each of the RGB colors with size Width by Height. There is no
+ * Color map for the input. Output is a quantized image with 2D array of
+ * indexes into the output color map.
+ *   Note input image can be 24 bits at the most (8 for red/green/blue) and
+ * the output has 256 colors at the most (256 entries in the color map.).
+ * ColorMapSize specifies size of color map up to 256 and will be updated to
+ * real size before returning.
+ *   Also non of the parameter are allocated by this routine.
+ *   This function returns GIF_OK if succesfull, GIF_ERROR otherwise.
+ ******************************************************************************/
+int
+QuantizeBuffer(unsigned int Width,
+               unsigned int Height,
+               int *ColorMapSize,
+               GifByteType * RedInput,
+               GifByteType * GreenInput,
+               GifByteType * BlueInput,
+               GifByteType * OutputBuffer,
+               GifColorType * OutputColorMap) {
+
+    unsigned int Index, NumOfEntries;
+    int i, j, MaxRGBError[3];
+    unsigned int NewColorMapSize;
+    long Red, Green, Blue;
+    NewColorMapType NewColorSubdiv[256];
+    QuantizedColorType *ColorArrayEntries, *QuantizedColor;
+
+    ColorArrayEntries = (QuantizedColorType *)malloc(
+                           sizeof(QuantizedColorType) * COLOR_ARRAY_SIZE);
+    if (ColorArrayEntries == NULL) {
+        _GifError = E_GIF_ERR_NOT_ENOUGH_MEM;
+        return GIF_ERROR;
+    }
+
+    for (i = 0; i < COLOR_ARRAY_SIZE; i++) {
+        ColorArrayEntries[i].RGB[0] = i >> (2 * BITS_PER_PRIM_COLOR);
+        ColorArrayEntries[i].RGB[1] = (i >> BITS_PER_PRIM_COLOR) &
+           MAX_PRIM_COLOR;
+        ColorArrayEntries[i].RGB[2] = i & MAX_PRIM_COLOR;
+        ColorArrayEntries[i].Count = 0;
+    }
+
+    /* Sample the colors and their distribution: */
+    for (i = 0; i < (int)(Width * Height); i++) {
+        Index = ((RedInput[i] >> (8 - BITS_PER_PRIM_COLOR)) <<
+                  (2 * BITS_PER_PRIM_COLOR)) +
+                ((GreenInput[i] >> (8 - BITS_PER_PRIM_COLOR)) <<
+                  BITS_PER_PRIM_COLOR) +
+                (BlueInput[i] >> (8 - BITS_PER_PRIM_COLOR));
+        ColorArrayEntries[Index].Count++;
+    }
+
+    /* Put all the colors in the first entry of the color map, and call the
+     * recursive subdivision process.  */
+    for (i = 0; i < 256; i++) {
+        NewColorSubdiv[i].QuantizedColors = NULL;
+        NewColorSubdiv[i].Count = NewColorSubdiv[i].NumEntries = 0;
+        for (j = 0; j < 3; j++) {
+            NewColorSubdiv[i].RGBMin[j] = 0;
+            NewColorSubdiv[i].RGBWidth[j] = 255;
+        }
+    }
+
+    /* Find the non empty entries in the color table and chain them: */
+    for (i = 0; i < COLOR_ARRAY_SIZE; i++)
+        if (ColorArrayEntries[i].Count > 0)
+            break;
+    QuantizedColor = NewColorSubdiv[0].QuantizedColors = &ColorArrayEntries[i];
+    NumOfEntries = 1;
+    while (++i < COLOR_ARRAY_SIZE)
+        if (ColorArrayEntries[i].Count > 0) {
+            QuantizedColor->Pnext = &ColorArrayEntries[i];
+            QuantizedColor = &ColorArrayEntries[i];
+            NumOfEntries++;
+        }
+    QuantizedColor->Pnext = NULL;
+
+    NewColorSubdiv[0].NumEntries = NumOfEntries; /* Different sampled colors */
+    NewColorSubdiv[0].Count = ((long)Width) * Height; /* Pixels */
+    NewColorMapSize = 1;
+    if (SubdivColorMap(NewColorSubdiv, *ColorMapSize, &NewColorMapSize) !=
+       GIF_OK) {
+        free((char *)ColorArrayEntries);
+        return GIF_ERROR;
+    }
+    if (NewColorMapSize < *ColorMapSize) {
+        /* And clear rest of color map: */
+        for (i = NewColorMapSize; i < *ColorMapSize; i++)
+            OutputColorMap[i].Red = OutputColorMap[i].Green =
+                OutputColorMap[i].Blue = 0;
+    }
+
+    /* Average the colors in each entry to be the color to be used in the
+     * output color map, and plug it into the output color map itself. */
+    for (i = 0; i < NewColorMapSize; i++) {
+        if ((j = NewColorSubdiv[i].NumEntries) > 0) {
+            QuantizedColor = NewColorSubdiv[i].QuantizedColors;
+            Red = Green = Blue = 0;
+            while (QuantizedColor) {
+                QuantizedColor->NewColorIndex = i;
+                Red += QuantizedColor->RGB[0];
+                Green += QuantizedColor->RGB[1];
+                Blue += QuantizedColor->RGB[2];
+                QuantizedColor = QuantizedColor->Pnext;
+            }
+            OutputColorMap[i].Red = (Red << (8 - BITS_PER_PRIM_COLOR)) / j;
+            OutputColorMap[i].Green = (Green << (8 - BITS_PER_PRIM_COLOR)) / j;
+            OutputColorMap[i].Blue = (Blue << (8 - BITS_PER_PRIM_COLOR)) / j;
+        }
+    }
+
+    /* Finally scan the input buffer again and put the mapped index in the
+     * output buffer.  */
+    MaxRGBError[0] = MaxRGBError[1] = MaxRGBError[2] = 0;
+    for (i = 0; i < (int)(Width * Height); i++) {
+        Index = ((RedInput[i] >> (8 - BITS_PER_PRIM_COLOR)) <<
+                 (2 * BITS_PER_PRIM_COLOR)) +
+                ((GreenInput[i] >> (8 - BITS_PER_PRIM_COLOR)) <<
+                 BITS_PER_PRIM_COLOR) +
+                (BlueInput[i] >> (8 - BITS_PER_PRIM_COLOR));
+        Index = ColorArrayEntries[Index].NewColorIndex;
+        OutputBuffer[i] = Index;
+        if (MaxRGBError[0] < ABS(OutputColorMap[Index].Red - RedInput[i]))
+            MaxRGBError[0] = ABS(OutputColorMap[Index].Red - RedInput[i]);
+        if (MaxRGBError[1] < ABS(OutputColorMap[Index].Green - GreenInput[i]))
+            MaxRGBError[1] = ABS(OutputColorMap[Index].Green - GreenInput[i]);
+        if (MaxRGBError[2] < ABS(OutputColorMap[Index].Blue - BlueInput[i]))
+            MaxRGBError[2] = ABS(OutputColorMap[Index].Blue - BlueInput[i]);
+    }
+
+#ifdef DEBUG
+    fprintf(stderr,
+            "Quantization L(0) errors: Red = %d, Green = %d, Blue = %d.\n",
+            MaxRGBError[0], MaxRGBError[1], MaxRGBError[2]);
+#endif /* DEBUG */
+
+    free((char *)ColorArrayEntries);
+
+    *ColorMapSize = NewColorMapSize;
+
+    return GIF_OK;
+}
+
+/******************************************************************************
+ * Routine to subdivide the RGB space recursively using median cut in each
+ * axes alternatingly until ColorMapSize different cubes exists.
+ * The biggest cube in one dimension is subdivide unless it has only one entry.
+ * Returns GIF_ERROR if failed, otherwise GIF_OK.
+ ******************************************************************************/
+static int
+SubdivColorMap(NewColorMapType * NewColorSubdiv,
+               unsigned int ColorMapSize,
+               unsigned int *NewColorMapSize) {
+
+    int MaxSize;
+    unsigned int i, j, Index = 0, NumEntries, MinColor, MaxColor;
+    long Sum, Count;
+    QuantizedColorType *QuantizedColor, **SortArray;
+
+    while (ColorMapSize > *NewColorMapSize) {
+        /* Find candidate for subdivision: */
+        MaxSize = -1;
+        for (i = 0; i < *NewColorMapSize; i++) {
+            for (j = 0; j < 3; j++) {
+                if ((((int)NewColorSubdiv[i].RGBWidth[j]) > MaxSize) &&
+                      (NewColorSubdiv[i].NumEntries > 1)) {
+                    MaxSize = NewColorSubdiv[i].RGBWidth[j];
+                    Index = i;
+                    SortRGBAxis = j;
+                }
+            }
+        }
+
+        if (MaxSize == -1)
+            return GIF_OK;
+
+        /* Split the entry Index into two along the axis SortRGBAxis: */
+
+        /* Sort all elements in that entry along the given axis and split at
+         * the median.  */
+        SortArray = (QuantizedColorType **)malloc(
+                      sizeof(QuantizedColorType *) * 
+                      NewColorSubdiv[Index].NumEntries);
+        if (SortArray == NULL)
+            return GIF_ERROR;
+        for (j = 0, QuantizedColor = NewColorSubdiv[Index].QuantizedColors;
+             j < NewColorSubdiv[Index].NumEntries && QuantizedColor != NULL;
+             j++, QuantizedColor = QuantizedColor->Pnext)
+            SortArray[j] = QuantizedColor;
+
+        qsort(SortArray, NewColorSubdiv[Index].NumEntries,
+              sizeof(QuantizedColorType *), SortCmpRtn);
+
+        /* Relink the sorted list into one: */
+        for (j = 0; j < NewColorSubdiv[Index].NumEntries - 1; j++)
+            SortArray[j]->Pnext = SortArray[j + 1];
+        SortArray[NewColorSubdiv[Index].NumEntries - 1]->Pnext = NULL;
+        NewColorSubdiv[Index].QuantizedColors = QuantizedColor = SortArray[0];
+        free((char *)SortArray);
+
+        /* Now simply add the Counts until we have half of the Count: */
+        Sum = NewColorSubdiv[Index].Count / 2 - QuantizedColor->Count;
+        NumEntries = 1;
+        Count = QuantizedColor->Count;
+        while ((Sum -= QuantizedColor->Pnext->Count) >= 0 &&
+               QuantizedColor->Pnext != NULL &&
+               QuantizedColor->Pnext->Pnext != NULL) {
+            QuantizedColor = QuantizedColor->Pnext;
+            NumEntries++;
+            Count += QuantizedColor->Count;
+        }
+        /* Save the values of the last color of the first half, and first
+         * of the second half so we can update the Bounding Boxes later.
+         * Also as the colors are quantized and the BBoxes are full 0..255,
+         * they need to be rescaled.
+         */
+        MaxColor = QuantizedColor->RGB[SortRGBAxis]; /* Max. of first half */
+        MinColor = QuantizedColor->Pnext->RGB[SortRGBAxis]; /* of second */
+        MaxColor <<= (8 - BITS_PER_PRIM_COLOR);
+        MinColor <<= (8 - BITS_PER_PRIM_COLOR);
+
+        /* Partition right here: */
+        NewColorSubdiv[*NewColorMapSize].QuantizedColors =
+           QuantizedColor->Pnext;
+        QuantizedColor->Pnext = NULL;
+        NewColorSubdiv[*NewColorMapSize].Count = Count;
+        NewColorSubdiv[Index].Count -= Count;
+        NewColorSubdiv[*NewColorMapSize].NumEntries =
+           NewColorSubdiv[Index].NumEntries - NumEntries;
+        NewColorSubdiv[Index].NumEntries = NumEntries;
+        for (j = 0; j < 3; j++) {
+            NewColorSubdiv[*NewColorMapSize].RGBMin[j] =
+               NewColorSubdiv[Index].RGBMin[j];
+            NewColorSubdiv[*NewColorMapSize].RGBWidth[j] =
+               NewColorSubdiv[Index].RGBWidth[j];
+        }
+        NewColorSubdiv[*NewColorMapSize].RGBWidth[SortRGBAxis] =
+           NewColorSubdiv[*NewColorMapSize].RGBMin[SortRGBAxis] +
+           NewColorSubdiv[*NewColorMapSize].RGBWidth[SortRGBAxis] - MinColor;
+        NewColorSubdiv[*NewColorMapSize].RGBMin[SortRGBAxis] = MinColor;
+
+        NewColorSubdiv[Index].RGBWidth[SortRGBAxis] =
+           MaxColor - NewColorSubdiv[Index].RGBMin[SortRGBAxis];
+
+        (*NewColorMapSize)++;
+    }
+
+    return GIF_OK;
+}
+
+/****************************************************************************
+ * Routine called by qsort to compare to entries.
+ ****************************************************************************/
+static int
+SortCmpRtn(const void *Entry1,
+           const void *Entry2) {
+
+    return (*((QuantizedColorType **) Entry1))->RGB[SortRGBAxis] -
+       (*((QuantizedColorType **) Entry2))->RGB[SortRGBAxis];
+}
+
+/* end */
diff --git a/util/rgb2gif.c b/util/rgb2gif.c
index 56386c7..46d9654 100644
--- a/util/rgb2gif.c
+++ b/util/rgb2gif.c
@@ -69,308 +69,6 @@ static void SaveGif(GifByteType *OutputBuffer,
 static void QuitGifError(GifFileType *GifFile);
 
 /******************************************************************************
- * Code to quantize high resolution image into lower one. You may want to
- * peek into the following article this code is based on:
- * "Color Image Quantization for frame buffer Display", by Paul Heckbert
- * SIGGRAPH 1982 page 297-307.
- ******************************************************************************
- * History:
- * 5 Jan 90 - Version 1.0 by Gershon Elber.
- *****************************************************************************/
-
-#define ABS(x)    ((x) > 0 ? (x) : (-(x)))
-
-#define COLOR_ARRAY_SIZE 32768
-#define BITS_PER_PRIM_COLOR 5
-#define MAX_PRIM_COLOR      0x1f
-
-static int SortRGBAxis;
-
-typedef struct QuantizedColorType {
-    GifByteType RGB[3];
-    GifByteType NewColorIndex;
-    long Count;
-    struct QuantizedColorType *Pnext;
-} QuantizedColorType;
-
-typedef struct NewColorMapType {
-    GifByteType RGBMin[3], RGBWidth[3];
-    unsigned int NumEntries; /* # of QuantizedColorType in linked list below */
-    unsigned long Count; /* Total number of pixels in all the entries */
-    QuantizedColorType *QuantizedColors;
-} NewColorMapType;
-
-static int SubdivColorMap(NewColorMapType * NewColorSubdiv,
-                          unsigned int ColorMapSize,
-                          unsigned int *NewColorMapSize);
-static int SortCmpRtn(const void *Entry1, const void *Entry2);
-
-/******************************************************************************
- * Quantize high resolution image into lower one. Input image consists of a
- * 2D array for each of the RGB colors with size Width by Height. There is no
- * Color map for the input. Output is a quantized image with 2D array of
- * indexes into the output color map.
- *   Note input image can be 24 bits at the most (8 for red/green/blue) and
- * the output has 256 colors at the most (256 entries in the color map.).
- * ColorMapSize specifies size of color map up to 256 and will be updated to
- * real size before returning.
- *   Also non of the parameter are allocated by this routine.
- *   This function returns GIF_OK if succesfull, GIF_ERROR otherwise.
- ******************************************************************************/
-static int
-QuantizeBuffer(unsigned int Width,
-               unsigned int Height,
-               int *ColorMapSize,
-               GifByteType * RedInput,
-               GifByteType * GreenInput,
-               GifByteType * BlueInput,
-               GifByteType * OutputBuffer,
-               GifColorType * OutputColorMap) {
-
-    unsigned int Index, NumOfEntries;
-    int i, j, MaxRGBError[3];
-    unsigned int NewColorMapSize;
-    long Red, Green, Blue;
-    NewColorMapType NewColorSubdiv[256];
-    QuantizedColorType *ColorArrayEntries, *QuantizedColor;
-
-    ColorArrayEntries = (QuantizedColorType *)malloc(
-                           sizeof(QuantizedColorType) * COLOR_ARRAY_SIZE);
-    if (ColorArrayEntries == NULL) {
-        return GIF_ERROR;
-    }
-
-    for (i = 0; i < COLOR_ARRAY_SIZE; i++) {
-        ColorArrayEntries[i].RGB[0] = i >> (2 * BITS_PER_PRIM_COLOR);
-        ColorArrayEntries[i].RGB[1] = (i >> BITS_PER_PRIM_COLOR) &
-           MAX_PRIM_COLOR;
-        ColorArrayEntries[i].RGB[2] = i & MAX_PRIM_COLOR;
-        ColorArrayEntries[i].Count = 0;
-    }
-
-    /* Sample the colors and their distribution: */
-    for (i = 0; i < (int)(Width * Height); i++) {
-        Index = ((RedInput[i] >> (8 - BITS_PER_PRIM_COLOR)) <<
-                  (2 * BITS_PER_PRIM_COLOR)) +
-                ((GreenInput[i] >> (8 - BITS_PER_PRIM_COLOR)) <<
-                  BITS_PER_PRIM_COLOR) +
-                (BlueInput[i] >> (8 - BITS_PER_PRIM_COLOR));
-        ColorArrayEntries[Index].Count++;
-    }
-
-    /* Put all the colors in the first entry of the color map, and call the
-     * recursive subdivision process.  */
-    for (i = 0; i < 256; i++) {
-        NewColorSubdiv[i].QuantizedColors = NULL;
-        NewColorSubdiv[i].Count = NewColorSubdiv[i].NumEntries = 0;
-        for (j = 0; j < 3; j++) {
-            NewColorSubdiv[i].RGBMin[j] = 0;
-            NewColorSubdiv[i].RGBWidth[j] = 255;
-        }
-    }
-
-    /* Find the non empty entries in the color table and chain them: */
-    for (i = 0; i < COLOR_ARRAY_SIZE; i++)
-        if (ColorArrayEntries[i].Count > 0)
-            break;
-    QuantizedColor = NewColorSubdiv[0].QuantizedColors = &ColorArrayEntries[i];
-    NumOfEntries = 1;
-    while (++i < COLOR_ARRAY_SIZE)
-        if (ColorArrayEntries[i].Count > 0) {
-            QuantizedColor->Pnext = &ColorArrayEntries[i];
-            QuantizedColor = &ColorArrayEntries[i];
-            NumOfEntries++;
-        }
-    QuantizedColor->Pnext = NULL;
-
-    NewColorSubdiv[0].NumEntries = NumOfEntries; /* Different sampled colors */
-    NewColorSubdiv[0].Count = ((long)Width) * Height; /* Pixels */
-    NewColorMapSize = 1;
-    if (SubdivColorMap(NewColorSubdiv, *ColorMapSize, &NewColorMapSize) !=
-       GIF_OK) {
-        free((char *)ColorArrayEntries);
-        return GIF_ERROR;
-    }
-    if (NewColorMapSize < *ColorMapSize) {
-        /* And clear rest of color map: */
-        for (i = NewColorMapSize; i < *ColorMapSize; i++)
-            OutputColorMap[i].Red = OutputColorMap[i].Green =
-                OutputColorMap[i].Blue = 0;
-    }
-
-    /* Average the colors in each entry to be the color to be used in the
-     * output color map, and plug it into the output color map itself. */
-    for (i = 0; i < NewColorMapSize; i++) {
-        if ((j = NewColorSubdiv[i].NumEntries) > 0) {
-            QuantizedColor = NewColorSubdiv[i].QuantizedColors;
-            Red = Green = Blue = 0;
-            while (QuantizedColor) {
-                QuantizedColor->NewColorIndex = i;
-                Red += QuantizedColor->RGB[0];
-                Green += QuantizedColor->RGB[1];
-                Blue += QuantizedColor->RGB[2];
-                QuantizedColor = QuantizedColor->Pnext;
-            }
-            OutputColorMap[i].Red = (Red << (8 - BITS_PER_PRIM_COLOR)) / j;
-            OutputColorMap[i].Green = (Green << (8 - BITS_PER_PRIM_COLOR)) / j;
-            OutputColorMap[i].Blue = (Blue << (8 - BITS_PER_PRIM_COLOR)) / j;
-        } else
-            fprintf(stderr,
-                    "\n%s: Null entry in quantized color map - that's weird.\n",
-                    PROGRAM_NAME);
-    }
-
-    /* Finally scan the input buffer again and put the mapped index in the
-     * output buffer.  */
-    MaxRGBError[0] = MaxRGBError[1] = MaxRGBError[2] = 0;
-    for (i = 0; i < (int)(Width * Height); i++) {
-        Index = ((RedInput[i] >> (8 - BITS_PER_PRIM_COLOR)) <<
-                 (2 * BITS_PER_PRIM_COLOR)) +
-                ((GreenInput[i] >> (8 - BITS_PER_PRIM_COLOR)) <<
-                 BITS_PER_PRIM_COLOR) +
-                (BlueInput[i] >> (8 - BITS_PER_PRIM_COLOR));
-        Index = ColorArrayEntries[Index].NewColorIndex;
-        OutputBuffer[i] = Index;
-        if (MaxRGBError[0] < ABS(OutputColorMap[Index].Red - RedInput[i]))
-            MaxRGBError[0] = ABS(OutputColorMap[Index].Red - RedInput[i]);
-        if (MaxRGBError[1] < ABS(OutputColorMap[Index].Green - GreenInput[i]))
-            MaxRGBError[1] = ABS(OutputColorMap[Index].Green - GreenInput[i]);
-        if (MaxRGBError[2] < ABS(OutputColorMap[Index].Blue - BlueInput[i]))
-            MaxRGBError[2] = ABS(OutputColorMap[Index].Blue - BlueInput[i]);
-    }
-
-#ifdef DEBUG
-    fprintf(stderr,
-            "Quantization L(0) errors: Red = %d, Green = %d, Blue = %d.\n",
-            MaxRGBError[0], MaxRGBError[1], MaxRGBError[2]);
-#endif /* DEBUG */
-
-    free((char *)ColorArrayEntries);
-
-    *ColorMapSize = NewColorMapSize;
-
-    return GIF_OK;
-}
-
-/******************************************************************************
- * Routine to subdivide the RGB space recursively using median cut in each
- * axes alternatingly until ColorMapSize different cubes exists.
- * The biggest cube in one dimension is subdivide unless it has only one entry.
- * Returns GIF_ERROR if failed, otherwise GIF_OK.
- ******************************************************************************/
-static int
-SubdivColorMap(NewColorMapType * NewColorSubdiv,
-               unsigned int ColorMapSize,
-               unsigned int *NewColorMapSize) {
-
-    int MaxSize;
-    unsigned int i, j, Index = 0, NumEntries, MinColor, MaxColor;
-    long Sum, Count;
-    QuantizedColorType *QuantizedColor, **SortArray;
-
-    while (ColorMapSize > *NewColorMapSize) {
-        /* Find candidate for subdivision: */
-        MaxSize = -1;
-        for (i = 0; i < *NewColorMapSize; i++) {
-            for (j = 0; j < 3; j++) {
-                if ((((int)NewColorSubdiv[i].RGBWidth[j]) > MaxSize) &&
-                      (NewColorSubdiv[i].NumEntries > 1)) {
-                    MaxSize = NewColorSubdiv[i].RGBWidth[j];
-                    Index = i;
-                    SortRGBAxis = j;
-                }
-            }
-        }
-
-        if (MaxSize == -1)
-            return GIF_OK;
-
-        /* Split the entry Index into two along the axis SortRGBAxis: */
-
-        /* Sort all elements in that entry along the given axis and split at
-         * the median.  */
-        SortArray = (QuantizedColorType **)malloc(
-                      sizeof(QuantizedColorType *) * 
-                      NewColorSubdiv[Index].NumEntries);
-        if (SortArray == NULL)
-            return GIF_ERROR;
-        for (j = 0, QuantizedColor = NewColorSubdiv[Index].QuantizedColors;
-             j < NewColorSubdiv[Index].NumEntries && QuantizedColor != NULL;
-             j++, QuantizedColor = QuantizedColor->Pnext)
-            SortArray[j] = QuantizedColor;
-
-        qsort(SortArray, NewColorSubdiv[Index].NumEntries,
-              sizeof(QuantizedColorType *), SortCmpRtn);
-
-        /* Relink the sorted list into one: */
-        for (j = 0; j < NewColorSubdiv[Index].NumEntries - 1; j++)
-            SortArray[j]->Pnext = SortArray[j + 1];
-        SortArray[NewColorSubdiv[Index].NumEntries - 1]->Pnext = NULL;
-        NewColorSubdiv[Index].QuantizedColors = QuantizedColor = SortArray[0];
-        free((char *)SortArray);
-
-        /* Now simply add the Counts until we have half of the Count: */
-        Sum = NewColorSubdiv[Index].Count / 2 - QuantizedColor->Count;
-        NumEntries = 1;
-        Count = QuantizedColor->Count;
-        while (QuantizedColor->Pnext != NULL &&
-	       (Sum -= QuantizedColor->Pnext->Count) >= 0 &&
-               QuantizedColor->Pnext->Pnext != NULL) {
-            QuantizedColor = QuantizedColor->Pnext;
-            NumEntries++;
-            Count += QuantizedColor->Count;
-        }
-        /* Save the values of the last color of the first half, and first
-         * of the second half so we can update the Bounding Boxes later.
-         * Also as the colors are quantized and the BBoxes are full 0..255,
-         * they need to be rescaled.
-         */
-        MaxColor = QuantizedColor->RGB[SortRGBAxis]; /* Max. of first half */
-	/* coverity[var_deref_op] */
-        MinColor = QuantizedColor->Pnext->RGB[SortRGBAxis]; /* of second */
-        MaxColor <<= (8 - BITS_PER_PRIM_COLOR);
-        MinColor <<= (8 - BITS_PER_PRIM_COLOR);
-
-        /* Partition right here: */
-        NewColorSubdiv[*NewColorMapSize].QuantizedColors =
-           QuantizedColor->Pnext;
-        QuantizedColor->Pnext = NULL;
-        NewColorSubdiv[*NewColorMapSize].Count = Count;
-        NewColorSubdiv[Index].Count -= Count;
-        NewColorSubdiv[*NewColorMapSize].NumEntries =
-           NewColorSubdiv[Index].NumEntries - NumEntries;
-        NewColorSubdiv[Index].NumEntries = NumEntries;
-        for (j = 0; j < 3; j++) {
-            NewColorSubdiv[*NewColorMapSize].RGBMin[j] =
-               NewColorSubdiv[Index].RGBMin[j];
-            NewColorSubdiv[*NewColorMapSize].RGBWidth[j] =
-               NewColorSubdiv[Index].RGBWidth[j];
-        }
-        NewColorSubdiv[*NewColorMapSize].RGBWidth[SortRGBAxis] =
-           NewColorSubdiv[*NewColorMapSize].RGBMin[SortRGBAxis] +
-           NewColorSubdiv[*NewColorMapSize].RGBWidth[SortRGBAxis] - MinColor;
-        NewColorSubdiv[*NewColorMapSize].RGBMin[SortRGBAxis] = MinColor;
-
-        NewColorSubdiv[Index].RGBWidth[SortRGBAxis] =
-           MaxColor - NewColorSubdiv[Index].RGBMin[SortRGBAxis];
-
-        (*NewColorMapSize)++;
-    }
-
-    return GIF_OK;
-}
-
-/****************************************************************************
- * Routine called by qsort to compare two entries.
- ****************************************************************************/
-static int
-SortCmpRtn(const void *Entry1,
-           const void *Entry2) {
-
-    return (*((QuantizedColorType **) Entry1))->RGB[SortRGBAxis] -
-       (*((QuantizedColorType **) Entry2))->RGB[SortRGBAxis];
-}
-/******************************************************************************
 * Interpret the command line and scan the given GIF file.
 ******************************************************************************/
 int main(int argc, char **argv)
-- 
1.8.3.2