-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgif_write.c
More file actions
303 lines (250 loc) · 7.46 KB
/
Copy pathgif_write.c
File metadata and controls
303 lines (250 loc) · 7.46 KB
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
#include "impy.h"
#include "private.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <gif_lib.h>
typedef struct gif_writer {
im_write base;
// gif-specific stuff
GifFileType *gif;
ColorMapObject* global_cm;
ColorMapObject* local_cm;
} gif_writer;
static void pre_img(im_write* wr);
static void emit_header(im_write* wr);
static void emit_rows(im_write *wr, unsigned int num_rows, const void *data, int stride);
static void finish(im_write* wr);
static int calc_colour_res(int ncolours);
static bool colormaps_equal(ColorMapObject const* a, ColorMapObject const* b);
static ColorMapObject* cvt_palette(unsigned int num_colours, const uint8_t *src, int* trans);
static ImErr translate_err(int gif_err_code);
static int output_fn(GifFileType *gif, const GifByteType *buf, int size);
static bool write_loops(GifFileType*gif, int loops);
static struct write_handler gif_write_handler = {
IM_FILETYPE_GIF,
pre_img,
emit_header,
emit_rows,
NULL, // post_img()
finish
};
im_write* igif_new_writer(im_out* out, ImErr* err)
{
gif_writer* gw = imalloc(sizeof(gif_writer));
if (!gw) {
*err = IM_ERR_NOMEM;
return NULL;
}
i_write_init(&gw->base);
gw->base.handler = &gif_write_handler;
gw->base.out = out;
gw->gif = NULL;
gw->global_cm = NULL;
gw->local_cm = NULL;
return (im_write*)gw;
}
// Optional hook, called at the last stage of im_begin_img(), but before
// im_set_palette() et al...
static void pre_img(im_write* writer)
{
// Only accept indexed data. We won't do quantisation on-the-fly, so this
// will fail if the user is planning to send us RGB or whatever.
i_write_set_internal_fmt(writer, IM_FMT_INDEX8);
if (writer->err != IM_ERR_NONE) {
return;
}
}
// Write out everything up to the start of the row data itself.
static void emit_header(im_write* wr)
{
gif_writer* gw = (gif_writer*)wr;
// First frame?
if (wr->num_frames==0) {
int giferr;
assert(!gw->gif);
gw->gif = EGifOpen((void*)wr->out, output_fn, &giferr);
if (!gw->gif) {
wr->err = translate_err(giferr);
return;
}
// Set up the global colormap (and transparent index)
int global_trans = NO_TRANSPARENT_COLOR;
gw->global_cm = cvt_palette(wr->pal_num_colours, wr->pal_data, &global_trans);
if( gw->global_cm == NULL) {
wr->err = IM_ERR_NO_PALETTE;
return;
}
// Emit preamble and screen description
// GIF89 - TODO: only if needed...
EGifSetGifVersion(gw->gif, true);
if (GIF_OK != EGifPutScreenDesc(gw->gif,
wr->w, // TODO: option to specify screen size in advance?
wr->h,
calc_colour_res(gw->global_cm->ColorCount),
0, // Background colour.
gw->global_cm))
{
// failed
wr->err = translate_err(gw->gif->Error);
return;
}
// Assume endlessly-looping gif.
write_loops(gw->gif, 0);
}
// Now, write out the frame!
// Has the palette changed?
if (gw->local_cm) {
GifFreeMapObject(gw->local_cm);
}
int trans = NO_TRANSPARENT_COLOR;
gw->local_cm = cvt_palette(wr->pal_num_colours, wr->pal_data, &trans);
if( gw->local_cm == NULL) {
wr->err = IM_ERR_NO_PALETTE;
return;
}
if (colormaps_equal(gw->global_cm, gw->local_cm)) {
// No change. throw away the local cm.
GifFreeMapObject(gw->local_cm);
gw->local_cm = NULL;
}
// output a GCB block
{
uint8_t gcb_buf[4];
GraphicsControlBlock gcb = {0};
gcb.DisposalMode = DISPOSAL_UNSPECIFIED;
gcb.UserInputFlag = false;
gcb.DelayTime = 10; // in 0.01sec units
gcb.TransparentColor = NO_TRANSPARENT_COLOR; //trans;
EGifGCBToExtension(&gcb, (GifByteType*)gcb_buf);
if( EGifPutExtension(gw->gif, GRAPHICS_EXT_FUNC_CODE, 4, gcb_buf) != GIF_OK) {
wr->err = translate_err(gw->gif->Error);
return;
}
}
if (GIF_OK != EGifPutImageDesc(
gw->gif,
wr->x_offset, wr->y_offset,
wr->w, wr->h,
false, // interlace
gw->local_cm)) // can be null.
{
wr->err = translate_err(gw->gif->Error);
return;
}
}
static void emit_rows(im_write *wr, unsigned int num_rows, const void *data, int stride)
{
gif_writer* gw = (gif_writer*)wr;
unsigned int i;
uint8_t const* src = data;
assert(wr->internal_fmt == IM_FMT_INDEX8);
for (i = 0; i < num_rows; ++i) {
if( GIF_OK !=EGifPutLine(gw->gif, (GifPixelType *)src, wr->w)) {
wr->err = translate_err(gw->gif->Error);
return;
}
src += stride;
}
}
// Return true if a and b hold the same colour values.
static bool colormaps_equal(ColorMapObject const* a, ColorMapObject const* b)
{
int n = a->ColorCount;
if (n != b->ColorCount) {
return false;
}
return (0 == memcmp(a->Colors, b->Colors, n * sizeof(GifColorType)));
}
static int output_fn( GifFileType *gif, const GifByteType *buf, int size)
{
im_out* out = (im_out*)gif->UserData;
return (int)im_out_write(out, (void*)buf, (size_t)size);
}
// convert raw RGB to gif colormap
static ColorMapObject* cvt_palette(unsigned int num_colours, const uint8_t *src, int* trans)
{
ColorMapObject* cm;
GifColorType* dest;
int i;
if (num_colours == 0) {
return NULL;
}
cm = GifMakeMapObject(num_colours, NULL);
if (!cm) {
return NULL;
}
*trans = NO_TRANSPARENT_COLOR; // -1
dest = cm->Colors;
for (i=0; i<num_colours; ++i) {
uint8_t alpha;
dest->Red = *src++;
dest->Green = *src++;
dest->Blue = *src++;
++dest;
alpha = *src++;
if (*trans == NO_TRANSPARENT_COLOR && alpha == 0) {
*trans = i;
}
}
return cm;
}
static ImErr translate_err(int gif_err_code)
{
switch(gif_err_code) {
case D_GIF_ERR_NOT_GIF_FILE: return IM_ERR_MALFORMED;
case D_GIF_ERR_NOT_ENOUGH_MEM: return IM_ERR_NOMEM;
//TODO:
default: return IM_ERR_MALFORMED;
}
}
static int calc_colour_res(int ncolours)
{
int i;
for( i=1; i<=8; ++i) {
if( ncolours <= (1<<i)) {
return i;
}
}
return 8;
}
static void finish(im_write* wr)
{
gif_writer* gw = (gif_writer*)wr;
if (gw->global_cm) {
GifFreeMapObject(gw->global_cm);
gw->global_cm = NULL;
}
if (gw->local_cm) {
GifFreeMapObject(gw->local_cm);
gw->local_cm = NULL;
}
if (gw->gif) {
int giferr;
if( GIF_OK != EGifCloseFile(gw->gif, &giferr) ) {
if (wr->err == IM_ERR_NONE) {
wr->err = translate_err(giferr);
}
}
gw->gif = NULL;
}
}
static bool write_loops(GifFileType*gif, int loops)
{
uint8_t blk1[11] = { 'N','E','T','S','C','A','P','E','2','.','0' };
uint8_t blk2[3] = { 0x01, (loops&0xff), ((loops>>8) & 0xff)};
if(EGifPutExtensionLeader(gif, APPLICATION_EXT_FUNC_CODE) != GIF_OK) {
return false;
}
if(EGifPutExtensionBlock(gif, sizeof(blk1), blk1) != GIF_OK) {
return false;
}
if(EGifPutExtensionBlock(gif, sizeof(blk2), blk2) != GIF_OK) {
return false;
}
if(EGifPutExtensionTrailer(gif) != GIF_OK) {
return false;
}
return true;
}