-
Notifications
You must be signed in to change notification settings - Fork 35
/
gif.c
347 lines (319 loc) · 9.71 KB
/
gif.c
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
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <magick/api.h>
// This is required because both GM and
// giflib define a function named
// DrawRectangle
#define DrawRectangle _DrawRectangle
#include <gif_lib.h>
#undef DrawRectangle
#include "macros.h"
#define NCOLORS 256
#define GIF_APP "NETSCAPE2.0"
#define INLINE static inline
#if !defined(GIFLIB_MAJOR) || GIFLIB_MAJOR < 5
#define EGifOpen(x, y, z) EGifOpen(x, y)
#define EGifCloseFile(x, y) EGifCloseFile(x)
#define GifMakeMapObject MakeMapObject
#define GifFreeMapObject FreeMapObject
#define GifQuantizeBuffer QuantizeBuffer
#define GIF_BEGIN_APP_EXTENSION(f) EGifPutExtensionFirst(f, APPLICATION_EXT_FUNC_CODE, strlen(GIF_APP), GIF_APP)
#define GIF_END_APP_EXTENSION(f, m) EGifPutExtensionLast(f, APPLICATION_EXT_FUNC_CODE, sizeof(m), m)
#else
#define GIF_BEGIN_APP_EXTENSION(f) ({ \
int ret = EGifPutExtensionLeader(f, APPLICATION_EXT_FUNC_CODE); \
if (ret != GIF_ERROR) { \
ret = EGifPutExtensionBlock(f, strlen(GIF_APP), GIF_APP); \
} \
ret; \
})
#define GIF_END_APP_EXTENSION(f, m) ({ \
int ret = EGifPutExtensionBlock(f, sizeof(m), m); \
if (ret != GIF_ERROR) { \
ret = EGifPutExtensionTrailer(f); \
} \
ret; \
})
#endif
typedef struct {
int width;
int height;
int duration;
GifPixelType data[0];
} Frame;
struct _bucket {
uint32_t key;
GifByteType value;
struct _bucket *next;
};
#define PIXEL_CACHE_BUCKETS 17
typedef struct {
struct _bucket *buckets[PIXEL_CACHE_BUCKETS];
} PixelCache;
INLINE PixelCache *
pixel_cache_new(void)
{
return calloc(1, sizeof(PixelCache));
}
INLINE void
pixel_cache_set(PixelCache *p, uint32_t key, GifByteType value)
{
int offset = key % PIXEL_CACHE_BUCKETS;
struct _bucket *b = malloc(sizeof(*b));
b->key = key;
b->value = value;
b->next = p->buckets[offset];
p->buckets[offset] = b;
}
INLINE int
pixel_cache_get(PixelCache *p, uint32_t key, GifByteType *value)
{
struct _bucket *b = p->buckets[key % PIXEL_CACHE_BUCKETS];
for (; b; b = b->next) {
if (b->key == key) {
*value = b->value;
return 1;
}
}
return 0;
}
INLINE void
pixel_cache_free(PixelCache *p)
{
int ii;
for (ii = 0; ii < PIXEL_CACHE_BUCKETS; ii++) {
struct _bucket *b = p->buckets[ii];
struct _bucket *next;
for (; b; b = next) {
next = b->next;
free(b);
}
}
free(p);
}
typedef struct {
GifByteType *data;
int size;
int alloc;
int pos;
} GifBuffer;
int
gif_buffer_write(GifFileType *ft, const GifByteType *bytes, int len)
{
GifBuffer *buf = ft->UserData;
if (buf->size + len > buf->alloc) {
int new_size = buf->alloc * 1.25;
if (new_size < buf->size + len) {
new_size = buf->size + len;
}
buf->data = realloc(buf->data, new_size);
buf->alloc = new_size;
}
memcpy(buf->data + buf->size, bytes, len);
buf->size += len;
return len;
}
int
gif_buffer_read(GifFileType *ft, GifByteType *bytes, int len)
{
GifBuffer *buf = ft->UserData;
int rem = buf->size - buf->pos;
if (rem < len) {
len = rem;
}
memcpy(bytes, buf->data + buf->pos, len);
buf->pos += len;
return len;
}
INLINE void *
gif_save(const Image *image, const ColorMapObject *color_map, Frame *frames, int count, int frame_size, int *size)
{
GifBuffer buf = {0,};
int estimated = count * (image->columns * image->rows);
buf.alloc = estimated;
buf.data = malloc(estimated);
GifFileType *gif_file = EGifOpen(&buf, gif_buffer_write, NULL);
if (!gif_file) {
return NULL;
}
if (EGifPutScreenDesc(gif_file, image->columns, image->rows, NCOLORS, 0, color_map) == GIF_ERROR) {
EGifCloseFile(gif_file, NULL);
return NULL;
}
if (GIF_BEGIN_APP_EXTENSION(gif_file) == GIF_ERROR) {
EGifCloseFile(gif_file, NULL);
return NULL;
}
unsigned char meta[] = {
0x01, // data sub-block index (always 1)
0xFF, 0xFF // 65535 repetitions - unsigned
};
if (GIF_END_APP_EXTENSION(gif_file, meta) == GIF_ERROR) {
EGifCloseFile(gif_file, NULL);
return NULL;
}
int ii;
unsigned char *p = (unsigned char*)frames;
for (ii = 0; ii < count; ii++, p += frame_size) {
Frame *frame = (Frame*)p;
// GCE
unsigned char gce[] = {
0x08, // no transparency
frame->duration % 256, // LSB of delay
frame->duration / 256, // MSB of delay in millisecs
0x00, // no transparent color
};
if (EGifPutExtension(gif_file, GRAPHICS_EXT_FUNC_CODE, sizeof(gce), gce) == GIF_ERROR) {
EGifCloseFile(gif_file, NULL);
return NULL;
}
if (EGifPutImageDesc(gif_file, 0, 0, frame->width, frame->height, 0, NULL) == GIF_ERROR) {
EGifCloseFile(gif_file, NULL);
return NULL;
}
int yy;
GifPixelType *p = frame->data;
for (yy = 0; yy < frame->height; yy++, p += frame->width) {
if (EGifPutLine(gif_file, p, frame->width) == GIF_ERROR) {
EGifCloseFile(gif_file, NULL);
return NULL;
}
}
}
EGifCloseFile(gif_file, NULL);
*size = buf.size;
return buf.data;
}
INLINE int
acquire_image_pixels(const Image *image, GifByteType *red, GifByteType *green, GifByteType *blue)
{
register long y;
register long x;
register const PixelPacket *p;
ExceptionInfo ex;
int width = image->columns;
int height = image->rows;
int ii = 0;
for(y = 0; y < height; ++y) {
p = ACQUIRE_IMAGE_PIXELS(image, 0, y, width, 1, &ex);
if (!p) {
return 0;
}
for (x = 0; x < width; x++, ii++, p++) {
red[ii] = ScaleQuantumToChar(p->red);
green[ii] = ScaleQuantumToChar(p->green);
blue[ii] = ScaleQuantumToChar(p->blue);
}
}
return 1;
}
INLINE int
aprox_image_pixels(const Image *image, GifColorType *colors, int color_count, PixelCache *cache, GifPixelType *out)
{
int width = image->columns;
int height = image->rows;
ExceptionInfo ex;
int ii;
int jj;
register const PixelPacket *p = ACQUIRE_IMAGE_PIXELS(image, 0, 0, width, height, &ex);
if (!p) {
return 0;
}
int end = width * height;
for (ii = 0; ii < end; ii++, p++) {
GifByteType r = ScaleQuantumToChar(p->red);
GifByteType g = ScaleQuantumToChar(p->green);
GifByteType b = ScaleQuantumToChar(p->blue);
uint32_t key = (r << 16) + (g << 8) + b;
if (!pixel_cache_get(cache, key, &out[ii])) {
int min_delta = 3 * (NCOLORS * NCOLORS);
int min_pos = 0;
GifColorType *c = colors;
for (jj = 0; jj < color_count; jj++, c++) {
int rd = c->Red - r;
int gd = c->Green - g;
int bd = c->Blue - b;
int delta = (rd * rd) + (gd * gd) + (bd * bd);
if (delta < min_delta) {
min_delta = delta;
min_pos = jj;
if (min_delta == 0) {
break;
}
}
}
out[ii] = min_pos;
pixel_cache_set(cache, key, min_pos);
}
}
return 1;
}
void *
gif_encode(Image *image, int single, int *size)
{
int width = image->columns;
int height = image->rows;
int total = width * height;
GifByteType red[total];
GifByteType green[total];
GifByteType blue[total];
// Quantize the images using IM/GM first, to reduce
// their number of colors to 256.
int count = GetImageListLength(image);
QuantizeInfo info;
GetQuantizeInfo(&info);
info.dither = 0;
info.number_colors = NCOLORS;
QuantizeImage(&info, image);
if (count > 1) {
#ifdef _MAGICK_USES_IM
RemapImages(&info, image->next, image);
#else
MapImages(image->next, image, 0);
#endif
}
if (!acquire_image_pixels(image, red, green, blue)) {
return NULL;
}
size_t frame_size = sizeof(Frame) + total;
Frame *frames = malloc(frame_size * count);
ColorMapObject *palette = GifMakeMapObject(NCOLORS, NULL);
int palette_size = NCOLORS;
// Quantize again using giflib, since it yields a palette which produces
// better compression, reducing the file size by 20%. Note that this second
// quantization is very fast, because the image already has 256 colors, so
// its effect on performance is negligible.
if (GifQuantizeBuffer(width, height, &palette_size, red, green, blue, frames->data, palette->Colors) == GIF_ERROR) {
GifFreeMapObject(palette);
free(frames);
return NULL;
}
frames->width = width;
frames->height = height;
frames->duration = image->delay;
GifColorType *colors = palette->Colors;
Image *cur = image->next;
PixelCache *cache = pixel_cache_new();
unsigned char *p = (unsigned char*)frames;
int ii;
for (ii = 1; ii < count; ii++, cur = cur->next) {
p += frame_size;
Frame *frame = (Frame*)p;
frame->width = width;
frame->height = height;
frame->duration = cur->delay;
GifPixelType *data = frame->data;
if (!aprox_image_pixels(cur, colors, palette_size, cache, data)) {
GifFreeMapObject(palette);
free(frames);
pixel_cache_free(cache);
return NULL;
}
}
pixel_cache_free(cache);
void *ret = gif_save(image, palette, frames, count, frame_size, size);
GifFreeMapObject(palette);
free(frames);
return ret;
}