annotate src/gifalloc.c @ 203:850242ba4a81 r20-3b28

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