annotate src/gifalloc.c @ 155:43dd3413c7c7 r20-3b4

Import from CVS: tag r20-3b4
author cvs
date Mon, 13 Aug 2007 09:39:39 +0200
parents 376386a54a3c
children 850242ba4a81
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 /* Synched up with: Not in FSF. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 /*****************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 * "Gif-Lib" - Yet another gif library. *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 * *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 * Written by: Gershon Elber Ver 0.1, Jun. 1989 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 * Extensively hacked by: Eric S. Raymond Ver 1.?, Sep 1992 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ******************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 * GIF construction tools *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ******************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 * History: *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 * 15 Sep 92 - Version 1.0 by Eric Raymond. *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 *****************************************************************************/
155
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
14
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
15 #ifdef emacs
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
16 #include <config.h>
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
17
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
18 void *xmalloc (unsigned int size);
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
19 void *xrealloc (void *ptr, unsigned int size);
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
20 #ifdef ERROR_CHECK_MALLOC
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
21 void *xfree_1 (void *);
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
22 #define xfree xfree_1
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
23 #else
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
24 void *xfree (void *);
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
25 #endif
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
26 #endif
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
27
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 #include <string.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 #include <stdlib.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 #ifdef emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 #include <config.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 void *xmalloc (unsigned int size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 void *xrealloc (void *ptr, unsigned int size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 #ifdef ERROR_CHECK_MALLOC
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 void *xfree_1 (void *);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 #define xfree xfree_1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 void *xfree (void *);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 #include "gif_lib.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 #ifndef MAX
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 /******************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 * Miscellaneous utility functions *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 ******************************************************************************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 int BitSize(int n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 /* return smallest bitfield size n will fit in */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 register i;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 for (i = 1; i <= 8; i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 if ((1 << i) >= n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 return(i);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 /******************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 * Color map object functions *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 ******************************************************************************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 ColorMapObject *MakeMapObject(int ColorCount, GifColorType *ColorMap)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 * Allocate a color map of given size; initialize with contents of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 * ColorMap if that pointer is non-NULL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 ColorMapObject *Object;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 if (ColorCount != (1 << BitSize(ColorCount)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 return((ColorMapObject *)NULL);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 Object = (ColorMapObject *)xmalloc(sizeof(ColorMapObject));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 if (Object == (ColorMapObject *)NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 return((ColorMapObject *)NULL);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 Object->Colors = (GifColorType *)calloc(ColorCount, sizeof(GifColorType));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 if (Object->Colors == (GifColorType *)NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 return((ColorMapObject *)NULL);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 Object->ColorCount = ColorCount;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 Object->BitsPerPixel = BitSize(ColorCount);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 if (ColorMap)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 memcpy((char *)Object->Colors,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 (char *)ColorMap, ColorCount * sizeof(GifColorType));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 return(Object);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 void FreeMapObject(ColorMapObject *Object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 * Free a color map object
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 xfree(Object->Colors);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 xfree(Object);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 #ifdef DEBUG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 void DumpColorMap(ColorMapObject *Object, FILE *fp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 if (Object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 int i, j, Len = Object->ColorCount;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 for (i = 0; i < Len; i+=4) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 for (j = 0; j < 4 && j < Len; j++) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 fprintf(fp,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 "%3d: %02x %02x %02x ", i + j,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 Object->Colors[i + j].Red,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 Object->Colors[i + j].Green,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 Object->Colors[i + j].Blue);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 fprintf(fp, "\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 #endif /* DEBUG */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 ColorMapObject *UnionColorMap(
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 ColorMapObject *ColorIn1,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 ColorMapObject *ColorIn2,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 GifPixelType ColorTransIn2[])
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 * Compute the union of two given color maps and return it. If result can't
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 * fit into 256 colors, NULL is returned, the allocated union otherwise.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 * ColorIn1 is copied as it to ColorUnion, while colors from ColorIn2 are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 * copied iff they didn't exist before. ColorTransIn2 maps the old
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 * ColorIn2 into ColorUnion color map table.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 int i, j, CrntSlot, RoundUpTo, NewBitSize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 ColorMapObject *ColorUnion;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 * Allocate table which will hold the result for sure.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 ColorUnion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 = MakeMapObject(MAX(ColorIn1->ColorCount,ColorIn2->ColorCount)*2,NULL);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 if (ColorUnion == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 return(NULL);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 /* Copy ColorIn1 to ColorUnionSize; */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 for (i = 0; i < ColorIn1->ColorCount; i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 ColorUnion->Colors[i] = ColorIn1->Colors[i];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 CrntSlot = ColorIn1->ColorCount;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 * Potentially obnoxious hack:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 * Back CrntSlot down past all contiguous {0, 0, 0} slots at the end
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 * of table 1. This is very useful if your display is limited to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 * 16 colors.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 while (ColorIn1->Colors[CrntSlot-1].Red == 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 && ColorIn1->Colors[CrntSlot-1].Green == 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 && ColorIn1->Colors[CrntSlot-1].Red == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 CrntSlot--;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 /* Copy ColorIn2 to ColorUnionSize (use old colors if they exist): */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 for (i = 0; i < ColorIn2->ColorCount && CrntSlot<=256; i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 /* Let's see if this color already exists: */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 for (j = 0; j < ColorIn1->ColorCount; j++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 if (memcmp(&ColorIn1->Colors[j], &ColorIn2->Colors[i], sizeof(GifColorType)) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 if (j < ColorIn1->ColorCount)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 ColorTransIn2[i] = j; /* color exists in Color1 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 /* Color is new - copy it to a new slot: */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 ColorUnion->Colors[CrntSlot] = ColorIn2->Colors[i];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 ColorTransIn2[i] = CrntSlot++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 if (CrntSlot > 256)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 FreeMapObject(ColorUnion);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 return((ColorMapObject *)NULL);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 NewBitSize = BitSize(CrntSlot);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 RoundUpTo = (1 << NewBitSize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 if (RoundUpTo != ColorUnion->ColorCount)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 register GifColorType *Map = ColorUnion->Colors;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 * Zero out slots up to next power of 2.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 * We know these slots exist because of the way ColorUnion's
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 * start dimension was computed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 for (j = CrntSlot; j < RoundUpTo; j++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 Map[j].Red = Map[j].Green = Map[j].Blue = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 /* perhaps we can shrink the map? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 if (RoundUpTo < ColorUnion->ColorCount)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 ColorUnion->Colors
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 = (GifColorType *)xrealloc(Map, sizeof(GifColorType)*RoundUpTo);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 ColorUnion->ColorCount = RoundUpTo;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 ColorUnion->BitsPerPixel = NewBitSize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 return(ColorUnion);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 void ApplyTranslation(SavedImage *Image, GifPixelType Translation[])
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 * Apply a given color translation to the raster bits of an image
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 register int i;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 register int RasterSize = Image->ImageDesc.Height * Image->ImageDesc.Width;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 for (i = 0; i < RasterSize; i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 Image->RasterBits[i] = Translation[Image->RasterBits[i]];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 /******************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 * Extension record functions *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 ******************************************************************************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 void MakeExtension(SavedImage *New, int Function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 New->Function = Function;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 * Someday we might have to deal with multiple extensions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 int AddExtensionBlock(SavedImage *New, int Len, char ExtData[])
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 ExtensionBlock *ep;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 if (New->ExtensionBlocks == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 New->ExtensionBlocks = (ExtensionBlock *)xmalloc(sizeof(ExtensionBlock));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 New->ExtensionBlocks =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 (ExtensionBlock *)xrealloc(New->ExtensionBlocks,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 sizeof(ExtensionBlock) * (New->ExtensionBlockCount + 1));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 if (New->ExtensionBlocks == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 return(GIF_ERROR);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 ep = &New->ExtensionBlocks[New->ExtensionBlockCount++];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 if ((ep->Bytes = (GifByteType *)xmalloc(ep->ByteCount = Len)) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 return(GIF_ERROR);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 if (ExtData)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 memcpy(ep->Bytes, ExtData, Len);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 return(GIF_OK);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 void FreeExtension(SavedImage *Image)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 ExtensionBlock *ep;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 for (ep = Image->ExtensionBlocks;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 ep < Image->ExtensionBlocks + Image->ExtensionBlockCount;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 ep++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 (void) xfree((char *)ep->Bytes);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 xfree((char *)Image->ExtensionBlocks);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 Image->ExtensionBlocks = NULL;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 /******************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 * Image block allocation functions *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 ******************************************************************************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 SavedImage *MakeSavedImage(GifFileType *GifFile, SavedImage *CopyFrom)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 * Append an image block to the SavedImages array
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 SavedImage *sp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 if (GifFile->SavedImages == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 GifFile->SavedImages = (SavedImage *)xmalloc(sizeof(SavedImage));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 GifFile->SavedImages = (SavedImage *)xrealloc(GifFile->SavedImages,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 sizeof(SavedImage) * (GifFile->ImageCount+1));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 if (GifFile->SavedImages == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 return((SavedImage *)NULL);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 sp = &GifFile->SavedImages[GifFile->ImageCount++];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 memset((char *)sp, '\0', sizeof(SavedImage));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 if (CopyFrom)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 memcpy((char *)sp, CopyFrom, sizeof(SavedImage));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 * Make our own allocated copies of the heap fields in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 * copied record. This guards against potential aliasing
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 * problems.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 /* first, the local color map */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 if (sp->ImageDesc.ColorMap)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 sp->ImageDesc.ColorMap =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 MakeMapObject(CopyFrom->ImageDesc.ColorMap->ColorCount,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 CopyFrom->ImageDesc.ColorMap->Colors);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 /* next, the raster */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 sp->RasterBits = (GifPixelType *)xmalloc(sizeof(GifPixelType)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 * CopyFrom->ImageDesc.Height
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 * CopyFrom->ImageDesc.Width);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 memcpy(sp->RasterBits,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 CopyFrom->RasterBits,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 sizeof(GifPixelType)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 * CopyFrom->ImageDesc.Height
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 * CopyFrom->ImageDesc.Width);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 /* finally, the extension blocks */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 if (sp->ExtensionBlocks)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 sp->ExtensionBlocks
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 = (ExtensionBlock*)xmalloc(sizeof(ExtensionBlock)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 * CopyFrom->ExtensionBlockCount);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 memcpy(sp->ExtensionBlocks,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 CopyFrom->ExtensionBlocks,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 sizeof(ExtensionBlock)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 * CopyFrom->ExtensionBlockCount);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 * For the moment, the actual blocks can take their
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 * chances with free(). We'll fix this later.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 return(sp);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 void FreeSavedImages(GifFileType *GifFile)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 SavedImage *sp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 for (sp = GifFile->SavedImages;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 sp < GifFile->SavedImages + GifFile->ImageCount;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 sp++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 if (sp->ImageDesc.ColorMap)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 FreeMapObject(sp->ImageDesc.ColorMap);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 sp->ImageDesc.ColorMap = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 if (sp->RasterBits)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 xfree((char *)sp->RasterBits);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 sp->RasterBits = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 if (sp->ExtensionBlocks)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 FreeExtension(sp);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 sp->ExtensionBlocks = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 xfree((char *) GifFile->SavedImages);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383