13 typedef unsigned char byte;
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);
48 static int SortCmpRtn(
const void *Entry1,
const void *Entry2);
63 int GIFquantize(
unsigned int Width,
unsigned int Height,
int *ColorMapSize,
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];
113 while (++i < COLOR_ARRAY_SIZE)
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 (QuantizedColor -> Pnext !=
NULL &&
249 QuantizedColor -> Pnext -> Pnext !=
NULL &&
250 (Sum -= QuantizedColor -> Pnext -> Count) >= 0) {
251 QuantizedColor = QuantizedColor -> Pnext;
253 Count += QuantizedColor -> Count;
260 if (QuantizedColor -> Pnext) MinColor = QuantizedColor -> Pnext -> RGB[
SortRGBAxis];
266 NewColorSubdiv[*NewColorMapSize].QuantizedColors =
267 QuantizedColor -> Pnext;
268 QuantizedColor -> Pnext =
NULL;
269 NewColorSubdiv[*NewColorMapSize].Count = Count;
270 NewColorSubdiv[
Index].Count -= Count;
271 NewColorSubdiv[*NewColorMapSize].NumEntries =
272 NewColorSubdiv[
Index].NumEntries - NumEntries;
273 NewColorSubdiv[
Index].NumEntries = NumEntries;
274 for (j = 0; j < 3; j++) {
275 NewColorSubdiv[*NewColorMapSize].RGBMin[j] =
276 NewColorSubdiv[
Index].RGBMin[j];
277 NewColorSubdiv[*NewColorMapSize].RGBWidth[j] =
278 NewColorSubdiv[
Index].RGBWidth[j];
280 NewColorSubdiv[*NewColorMapSize].RGBWidth[
SortRGBAxis] =
281 NewColorSubdiv[*NewColorMapSize].RGBMin[
SortRGBAxis] +
282 NewColorSubdiv[*NewColorMapSize].RGBWidth[
SortRGBAxis] -
284 NewColorSubdiv[*NewColorMapSize].RGBMin[
SortRGBAxis] = MinColor;
289 (*NewColorMapSize)++;
298 static int SortCmpRtn(
const void *Entry1,
const void *Entry2)
static int SubdivColorMap(NewColorMapType *NewColorSubdiv, unsigned int ColorMapSize, unsigned int *NewColorMapSize)
Vc_ALWAYS_INLINE void free(T *p)
Frees memory that was allocated with Vc::malloc.
struct QuantizedColorType QuantizedColorType
Double_t Sum(const double *x, const double *p)
RooCmdArg Index(RooCategory &icat)
static int SortCmpRtn(const void *Entry1, const void *Entry2)
struct NewColorMapType NewColorMapType
int GIFquantize(unsigned int Width, unsigned int Height, int *ColorMapSize, byte *RedInput, byte *GreenInput, byte *BlueInput, byte *OutputBuffer, GifColorType *OutputColorMap)
#define BITS_PER_PRIM_COLOR
struct GifColorType GifColorType
Vc_ALWAYS_INLINE_L T *Vc_ALWAYS_INLINE_R malloc(size_t n)
Allocates memory on the Heap with alignment and padding suitable for vectorized access.