15 byte Red, Green, Blue;
18#define ABS(x) ((x) > 0 ? (x) : (-(x)))
24#define COLOR_ARRAY_SIZE 32768
25#define BITS_PER_PRIM_COLOR 5
26#define MAX_PRIM_COLOR 0x1f
39 byte RGBMin[3], RGBWidth[3];
40 unsigned int NumEntries;
46 unsigned int ColorMapSize,
47 unsigned int *NewColorMapSize);
48static int SortCmpRtn(
const void *Entry1,
const void *Entry2);
63int GIFquantize(
unsigned int Width,
unsigned int Height,
int *ColorMapSize,
64 byte *RedInput,
byte *GreenInput,
byte *BlueInput,
67 unsigned int Index, NumOfEntries, newsize;
68 int i, j, MaxRGBError[3];
70 long Red, Green, Blue;
76 fprintf(stderr,
"QuantizeBuffer: not enough memory\n");
84 ColorArrayEntries[i].Count = 0;
88 for (i = 0; i < (int)(Width * Height); i++) {
94 ColorArrayEntries[
Index].Count++;
99 for (i = 0; i < 256; i++) {
100 NewColorSubdiv[i].QuantizedColors = NULL;
101 NewColorSubdiv[i].Count = NewColorSubdiv[i].NumEntries = 0;
102 for (j = 0; j < 3; j++) {
103 NewColorSubdiv[i].RGBMin[j] = 0;
104 NewColorSubdiv[i].RGBWidth[j] = 255;
110 if (ColorArrayEntries[i].Count > 0)
break;
111 QuantizedColor = NewColorSubdiv[0].QuantizedColors = &ColorArrayEntries[i];
114 if (ColorArrayEntries[i].Count > 0) {
115 QuantizedColor -> Pnext = &ColorArrayEntries[i];
116 QuantizedColor = &ColorArrayEntries[i];
119 QuantizedColor -> Pnext = NULL;
121 NewColorSubdiv[0].NumEntries = NumOfEntries;
122 NewColorSubdiv[0].Count = ((long) Width) * Height;
125 free((
char *) ColorArrayEntries);
128 NewColorMapSize = (int)newsize;
129 if (NewColorMapSize < *ColorMapSize) {
131 for (i = NewColorMapSize; i < *ColorMapSize; i++)
132 OutputColorMap[i].Red =
133 OutputColorMap[i].Green =
134 OutputColorMap[i].Blue = 0;
139 for (i = 0; i < NewColorMapSize; i++) {
140 if ((j = NewColorSubdiv[i].NumEntries) > 0) {
141 QuantizedColor = NewColorSubdiv[i].QuantizedColors;
142 Red = Green = Blue = 0;
143 while (QuantizedColor) {
144 QuantizedColor -> NewColorIndex = i;
145 Red += QuantizedColor -> RGB[0];
146 Green += QuantizedColor -> RGB[1];
147 Blue += QuantizedColor -> RGB[2];
148 QuantizedColor = QuantizedColor -> Pnext;
155 fprintf(stderr,
"Null entry in quantized color map - thats weird.");
160 MaxRGBError[0] = MaxRGBError[1] = MaxRGBError[2] = 0;
161 for (i = 0; i < (int)(Width * Height); i++) {
167 Index = ColorArrayEntries[
Index].NewColorIndex;
168 OutputBuffer[i] =
Index;
169 if (MaxRGBError[0] <
ABS(OutputColorMap[
Index].Red - RedInput[i]))
170 MaxRGBError[0] =
ABS(OutputColorMap[
Index].Red - RedInput[i]);
171 if (MaxRGBError[1] <
ABS(OutputColorMap[
Index].Green - GreenInput[i]))
172 MaxRGBError[1] =
ABS(OutputColorMap[
Index].Green - GreenInput[i]);
173 if (MaxRGBError[2] <
ABS(OutputColorMap[
Index].Blue - BlueInput[i]))
174 MaxRGBError[2] =
ABS(OutputColorMap[
Index].Blue - BlueInput[i]);
179 "Quantization L(0) errors: Red = %d, Green = %d, Blue = %d.\n",
180 MaxRGBError[0], MaxRGBError[1], MaxRGBError[2]);
183 free((
char *) ColorArrayEntries);
185 *ColorMapSize = NewColorMapSize;
197 unsigned int ColorMapSize,
198 unsigned int *NewColorMapSize)
201 unsigned int i, j,
Index = 0, NumEntries, MinColor, MaxColor;
205 while (ColorMapSize > *NewColorMapSize) {
208 for (i = 0; i < *NewColorMapSize; i++) {
209 for (j = 0; j < 3; j++) {
210 if (((
int) NewColorSubdiv[i].RGBWidth[j]) > MaxSize &&
211 NewColorSubdiv[i].NumEntries > 1) {
212 MaxSize = NewColorSubdiv[i].RGBWidth[j];
228 NewColorSubdiv[
Index].NumEntries)) == NULL)
230 for (j = 0, QuantizedColor = NewColorSubdiv[
Index].QuantizedColors;
231 j < NewColorSubdiv[
Index].NumEntries && QuantizedColor != NULL;
232 j++, QuantizedColor = QuantizedColor -> Pnext)
233 SortArray[j] = QuantizedColor;
234 qsort(SortArray, NewColorSubdiv[
Index].NumEntries,
238 for (j = 0; j < NewColorSubdiv[
Index].NumEntries - 1; j++)
239 SortArray[j] -> Pnext = SortArray[j + 1];
240 SortArray[NewColorSubdiv[
Index].NumEntries - 1] -> Pnext = NULL;
241 NewColorSubdiv[
Index].QuantizedColors = QuantizedColor = SortArray[0];
242 free((
char *) SortArray);
245 Sum = NewColorSubdiv[
Index].Count / 2 - QuantizedColor -> Count;
247 Count = QuantizedColor -> Count;
248 while ((
Sum -= QuantizedColor -> Pnext -> Count) >= 0 &&
249 QuantizedColor -> Pnext != NULL &&
250 QuantizedColor -> Pnext -> Pnext != NULL) {
251 QuantizedColor = QuantizedColor -> Pnext;
253 Count += QuantizedColor -> Count;
260 MinColor = QuantizedColor -> Pnext -> RGB[
SortRGBAxis];
265 NewColorSubdiv[*NewColorMapSize].QuantizedColors =
266 QuantizedColor -> Pnext;
267 QuantizedColor -> Pnext = NULL;
268 NewColorSubdiv[*NewColorMapSize].Count = Count;
269 NewColorSubdiv[
Index].Count -= Count;
270 NewColorSubdiv[*NewColorMapSize].NumEntries =
271 NewColorSubdiv[
Index].NumEntries - NumEntries;
272 NewColorSubdiv[
Index].NumEntries = NumEntries;
273 for (j = 0; j < 3; j++) {
274 NewColorSubdiv[*NewColorMapSize].RGBMin[j] =
275 NewColorSubdiv[
Index].RGBMin[j];
276 NewColorSubdiv[*NewColorMapSize].RGBWidth[j] =
277 NewColorSubdiv[
Index].RGBWidth[j];
279 NewColorSubdiv[*NewColorMapSize].RGBWidth[
SortRGBAxis] =
280 NewColorSubdiv[*NewColorMapSize].RGBMin[
SortRGBAxis] +
281 NewColorSubdiv[*NewColorMapSize].RGBWidth[
SortRGBAxis] -
283 NewColorSubdiv[*NewColorMapSize].RGBMin[
SortRGBAxis] = MinColor;
288 (*NewColorMapSize)++;
T Sum(const RVec< T > &v)
Sum elements of an RVec.
RooCmdArg Index(RooCategory &icat)
#define BITS_PER_PRIM_COLOR
static int SubdivColorMap(NewColorMapType *NewColorSubdiv, unsigned int ColorMapSize, unsigned int *NewColorMapSize)
struct NewColorMapType NewColorMapType
static int SortCmpRtn(const void *Entry1, const void *Entry2)
struct GifColorType GifColorType
int GIFquantize(unsigned int Width, unsigned int Height, int *ColorMapSize, byte *RedInput, byte *GreenInput, byte *BlueInput, byte *OutputBuffer, GifColorType *OutputColorMap)
struct QuantizedColorType QuantizedColorType