-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpgif.c
474 lines (427 loc) · 14.2 KB
/
pgif.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#pragma pack(push, 1)
#define ANIMATION_OPTIMIZATION_CROP
// don't use transparency, it's not saving space with 1 bit graphics
// #define ANIMATION_OPTIMIZATION_TRANSPARENCY
struct header_block {
char signature[3];
char version[3];
};
struct logical_screen_descriptor {
uint16_t canvas_width;
uint16_t canvas_height;
uint8_t size_of_global_color_table: 3;
uint8_t sort_flag: 1;
uint8_t color_resolution: 3;
uint8_t global_color_table_flag: 1;
uint8_t background_color_index;
uint8_t pixel_aspect_ratio;
};
struct graphics_control_extension {
uint8_t extension_introducer;
uint8_t graphics_control_label;
uint8_t byte_size;
uint8_t transparent_color_flag: 1;
uint8_t user_input_flag: 1;
uint8_t disposal_method: 3;
uint8_t reserved: 3;
uint16_t delay_time;
uint8_t transparent_color_index;
uint8_t block_terminator;
};
struct image_descriptor {
uint8_t image_separator;
uint16_t image_left;
uint16_t image_top;
uint16_t image_width;
uint16_t image_height;
uint8_t size_of_local_color_table: 3;
uint8_t reserved: 2;
uint8_t sort_flag: 1;
uint8_t interlace_flag: 1;
uint8_t local_color_table_flag: 1;
};
struct application_extension {
uint8_t gif_extension_code;
uint8_t application_extension_label;
uint8_t length_of_application_block;
char label[11];
uint8_t length_of_data_sub_block;
uint8_t one;
uint16_t loop_count;
uint8_t terminator;
};
#pragma pack(pop)
void put8(uint8_t i)
{
fputc(i, stdout);
}
void put_struct(void* p, size_t size)
{
for (int i = 0; i < size; i++)
put8(*(unsigned char*)(p + i));
}
struct lzw_emitter {
uint8_t byte_buffer[255];
uint8_t byte_buffer_size;
uint32_t buffer;
int8_t offset;
uint8_t code_size;
};
void flush_bytes(struct lzw_emitter* emitter)
{
put8(emitter->byte_buffer_size);
put_struct(emitter->byte_buffer, emitter->byte_buffer_size);
emitter->byte_buffer_size = 0;
}
void emit_byte(struct lzw_emitter* emitter)
{
emitter->byte_buffer[emitter->byte_buffer_size++] = emitter->buffer & 0xff;
emitter->buffer >>= 8;
emitter->offset -= 8;
if (emitter->byte_buffer_size == 0xff)
flush_bytes(emitter);
}
void emit_code(struct lzw_emitter* emitter, uint32_t code)
{
emitter->buffer |= code << emitter->offset;
emitter->offset += emitter->code_size;
while (emitter->offset > 7)
emit_byte(emitter);
}
void flush_emitter(struct lzw_emitter* emitter)
{
while (emitter->offset > 0)
emit_byte(emitter);
if (emitter->byte_buffer_size > 0)
flush_bytes(emitter);
}
void encode_image(uint8_t* pixels, uint8_t* previous_pixels,
uint16_t width, uint16_t height,
uint8_t colors_used, uint16_t frame_delay)
{
uint8_t color_depth = 1;
while (colors_used > (1 << color_depth))
color_depth++;
if (color_depth < 2)
color_depth = 2;
uint16_t cropped_left = 0;
uint16_t cropped_top = 0;
uint16_t cropped_width = width;
uint16_t cropped_height = height;
int16_t transparent_color = -1;
if (previous_pixels)
{
// if previous_pixels is not null, we can do some differential encoding!
#ifdef ANIMATION_OPTIMIZATION_CROP
// crop left and right
for (int dir = 0; dir < 2; dir++)
{
while (cropped_width > 0)
{
uint8_t same_pixels = 1;
int x, y;
x = (dir == 0) ? cropped_left : cropped_left + cropped_width - 1;
for (int y = cropped_top; same_pixels && (y < cropped_top + cropped_height); y++)
{
int offset = y * width + x;
if (pixels[offset] != previous_pixels[offset])
same_pixels = 0;
}
if (same_pixels)
{
if (dir == 0)
cropped_left++;
cropped_width--;
}
else
break;
}
}
// crop top and bottom
for (int dir = 0; dir < 2; dir++)
{
while (cropped_height > 0)
{
uint8_t same_pixels = 1;
int x, y;
y = (dir == 0) ? cropped_top : cropped_top + cropped_height - 1;
for (int x = cropped_left; same_pixels && (x < cropped_left + cropped_width); x++)
{
int offset = y * width + x;
if (pixels[offset] != previous_pixels[offset])
same_pixels = 0;
}
if (same_pixels)
{
if (dir == 0)
cropped_top++;
cropped_height--;
}
else
break;
}
}
#endif
#ifdef ANIMATION_OPTIMIZATION_TRANSPARENCY
// replace unchanged pixels with tranparency
if (colors_used < 255)
{
transparent_color = colors_used;
colors_used++;
while (colors_used >= (1 << color_depth))
color_depth++;
for (int y = 0; y < cropped_height; y++)
{
uint32_t offset = (y + cropped_top) * width + cropped_left;
uint8_t* p = pixels + offset;
uint8_t* pp = previous_pixels + offset;
for (int x = 0; x < cropped_width; x++)
{
if (*p == *pp)
{
*p = (uint8_t)transparent_color;
}
p++;
pp++;
}
}
}
#endif
}
// write graphics control extension
struct graphics_control_extension gce;
memset(&gce, 0, sizeof(gce));
gce.extension_introducer = 0x21;
gce.graphics_control_label = 0xf9;
gce.byte_size = 4;
gce.disposal_method = 1; // draw on top
gce.delay_time = frame_delay; // in 1/100 seconds
#ifdef ANIMATION_OPTIMIZATION_TRANSPARENCY
if (transparent_color >= 0)
{
gce.transparent_color_flag = 1;
gce.transparent_color_index = transparent_color;
}
#endif
put_struct(&gce, sizeof(gce));
// write image descriptor
struct image_descriptor id;
memset(&id, 0, sizeof(id));
id.image_separator = 0x2c;
id.image_left = cropped_left;
id.image_top = cropped_top;
id.image_width = cropped_width;
id.image_height = cropped_height;
put_struct(&id, sizeof(id));
uint8_t lzw_minimum_code_size = color_depth;
// if (lzw_minimum_code_size < 2)
// lzw_minimum_code_size = 2;
put8(lzw_minimum_code_size);
struct lzw_emitter emitter;
emitter.byte_buffer_size = 0;
emitter.buffer = 0;
emitter.offset = 0;
emitter.code_size = lzw_minimum_code_size + 1;
// set up LZW encoder
uint16_t clear_code = (1 << color_depth);
uint16_t end_of_information_code = clear_code + 1;
uint16_t* prefix_table = malloc(sizeof(uint16_t) * (2048 - 1 - end_of_information_code));
uint8_t* suffix_table = malloc(sizeof(uint8_t) * (2048 - 1 - end_of_information_code));
uint16_t table_length = 0;
emit_code(&emitter, clear_code);
uint8_t* p = pixels;
uint16_t index_buffer = 0;
for (int y = 0; y < cropped_height; y++)
{
uint8_t* p = pixels + (y + cropped_top) * width + cropped_left;
for (int x = 0; x < cropped_width; x++)
{
if (x == 0 && y == 0)
{
index_buffer = (uint16_t)(*(p++));
continue;
}
uint8_t k = *(p++);
uint16_t found_table_entry = 0xffff;
for (int i = 0; i < table_length; i++)
if (index_buffer == prefix_table[i] && k == suffix_table[i])
found_table_entry = i + end_of_information_code + 1;
if (found_table_entry < 0xffff)
index_buffer = found_table_entry;
else
{
prefix_table[table_length] = index_buffer;
suffix_table[table_length] = k;
table_length += 1;
if (table_length + end_of_information_code > (1 << emitter.code_size))
emitter.code_size++;
emit_code(&emitter, index_buffer);
index_buffer = k;
if (table_length >= 2048 - 1 - end_of_information_code)
{
emit_code(&emitter, clear_code);
table_length = 0;
emitter.code_size = lzw_minimum_code_size + 1;
}
}
}
}
emit_code(&emitter, index_buffer);
emit_code(&emitter, end_of_information_code);
flush_emitter(&emitter);
free(suffix_table);
free(prefix_table);
put8(0);
}
int main(int argc, char** argv)
{
if (argc < 4)
{
fprintf(stderr, "This program creates an animated GIF from a series of frames\n");
fprintf(stderr, "passed via stdin.\n");
fprintf(stderr, "\n");
fprintf(stderr, "Usage: ./pgif <width> <height> <number of colors>\n");
fprintf(stderr, " <width> 1 to 65535\n");
fprintf(stderr, " <height> 1 to 65535\n");
fprintf(stderr, " <number of colors> 1 to 255\n");
fprintf(stderr, "\n");
fprintf(stderr, "After the program has started, write to its stdin:\n");
fprintf(stderr, "- the palette (one color per line, HTML notation without #)\n");
fprintf(stderr, " example: '000000\\nffffff\\n' if you specified two colors\n");
fprintf(stderr, "- as many frames as you wish, one per line, formatted as\n");
fprintf(stderr, " one big hex string and starting with f\n");
fprintf(stderr, " example: 'f 000100000100\\n' if you specified a 3x2 image\n");
fprintf(stderr, "\n");
fprintf(stderr, "The default frame delay is 100 ms. You may change the frame delay\n");
fprintf(stderr, "for all following frames by specifying 'd <number>\\n' where\n");
fprintf(stderr, "<number> is a decimal number and specifies the delay in 1/100 seconds.\n");
fprintf(stderr, "\n");
fprintf(stderr, "To finalize the GIF, close the stdin stream. Output is written to stdout.\n");
exit(1);
}
char* temp = 0;
uint16_t width = strtol(argv[1], &temp, 0);
uint16_t height = strtol(argv[2], &temp, 0);
uint16_t colors_used = strtol(argv[3], &temp, 0);
uint8_t color_depth = 1;
while (colors_used > (1 << color_depth))
color_depth++;
if (color_depth < 2)
color_depth = 2;
// write header
struct header_block header;
strncpy(header.signature, "GIF", 3);
strncpy(header.version, "89a", 3);
put_struct(&header, sizeof(header));
// write logical screen descriptor
struct logical_screen_descriptor lsd;
memset(&lsd, 0, sizeof(lsd));
lsd.canvas_width = width;
lsd.canvas_height = height;
lsd.size_of_global_color_table = color_depth - 1;
lsd.color_resolution = color_depth - 1;
lsd.global_color_table_flag = 1;
put_struct(&lsd, sizeof(lsd));
size_t max_line_size = width + 1024;
char* line = malloc(max_line_size);
if (!line)
{
fprintf(stderr, "Error allocating line!\n");
exit(1);
}
char* line_p;
char hex[4];
memset(hex, 0, 4);
// write global color table, transfer palette from stdin to GIF
for (int i = 0; i < colors_used; i++)
{
fgets(line, max_line_size, stdin);
line_p = line;
for (int k = 0; k < 3; k++)
{
strncpy(hex, line_p, 2);
put8(strtol(hex, &temp, 16));
line_p += 2;
}
}
// fill remaining colors, if any
for (int i = colors_used; i < (1 << color_depth); i++)
{
put8(0); put8(0); put8(0);
}
// write application extension NETSCAPE2.0 to loop the animation
// (otherwise it just plays once, duh...)
struct application_extension ae;
memset(&ae, 0, sizeof(ae));
ae.gif_extension_code = 0x21;
ae.application_extension_label = 0xff;
ae.length_of_application_block = 0x0b;
strncpy(ae.label, "NETSCAPE2.0", 0x0b);
ae.length_of_data_sub_block = 3;
ae.one = 1;
put_struct(&ae, sizeof(ae));
uint8_t *previous_pixels = 0;
uint8_t* pixels = malloc(width * height);
if (!pixels)
{
fprintf(stderr, "Error allocating buffer for image.\n");
exit(1);
}
uint16_t frame_delay = 10;
while (fgets(line, max_line_size, stdin))
{
if (line[0] == 'f' || line[0] == 'l')
{
uint8_t* p = pixels;
if (line[0] == 'f')
{
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
fgets(hex, 4, stdin);
*(p++) = strtol(hex, &temp, 16);
}
}
}
else if (line[0] == 'l')
{
for (int y = 0; y < height; y++)
{
fgets(line, max_line_size, stdin);
line_p = line;
hex[1] = 0;
for (int x = 0; x < width; x++)
{
hex[0] = *(line_p++);
*(p++) = strtol(hex, &temp, 16);
}
}
}
encode_image(pixels, previous_pixels, width, height, colors_used, frame_delay);
if (!previous_pixels)
{
previous_pixels = malloc(width * height);
if (!previous_pixels)
{
fprintf(stderr, "Error allocating buffer for image.\n");
exit(1);
}
}
memcpy(previous_pixels, pixels, width * height);
}
else if (line[0] == 'd')
frame_delay = strtol(line + 2, &temp, 0);
}
if (previous_pixels)
free(previous_pixels);
free(pixels);
free(line);
// write trailer
put8(0x3b);
return 0;
}