Debian Codegraph @

gifenc/gifenc.c

raw symbols SCIP
1 /* simple gif encoder
2 * Copyright (C) 2005 Benjamin Otte <[email protected]
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 3 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <glib/gstdio.h>
28 #include "gifenc.h"
29
30
31 /*** UTILITIES ***/
32
33 static guint
34 log2n (guint number)
35 {
36 guint ret = 0;
37 while (number > 0) {
38 number >>= 1;
39 ret++;
40 }
41 return ret;
42 }
43
44 #define RED(x) ((guint8) ((x) >> 16))
45 #define GREEN(x) ((guint8) ((x) >> 8))
46 #define BLUE(x) ((guint8) (x))
47
48 #define COLOR(r, g, b) (((r) << 16) | ((g) << 8) | (b))
49
50 /*** WRITE ROUTINES ***/
51
52 static gboolean
53 gifenc_flush (Gifenc *enc, GError **error)
54 {
55 gboolean result;
56
57 if (enc->buffer->len == 0)
58 return TRUE;
59
60 result = enc->write_func (enc->write_data, enc->buffer->data,
61 enc->buffer->len, error);
62
63 g_byte_array_set_size (enc->buffer, 0);
64 return result;
65 }
66
67 static void
68 gifenc_write_uint16 (Gifenc *enc, guint16 value)
69 {
70 g_return_if_fail (enc->n_bits == 0);
71
72 value = GUINT16_TO_LE (value);
73 g_byte_array_append (enc->buffer, (guint8 *) &value, 2);
74 }
75
76 static void
77 gifenc_write_byte (Gifenc *enc, guint8 value)
78 {
79 g_return_if_fail (enc->n_bits == 0);
80
81 g_byte_array_append (enc->buffer, &value, 1);
82 }
83
84 static void
85 gifenc_write_bits (Gifenc *enc, guint bits, guint nbits)
86 {
87 g_return_if_fail (bits <= 24);
88 g_return_if_fail ((bits & ((1 << nbits) - 1)) == bits);
89
90 enc->bits <<= nbits;
91 enc->bits |= bits;
92 nbits = enc->n_bits + nbits;
93 enc->n_bits = 0;
94 while (nbits >= 8) {
95 nbits -= 8;
96 gifenc_write_byte (enc, enc->bits >> nbits);
97 }
98 enc->n_bits = nbits;
99 enc->bits &= (1 << nbits) - 1;
100 }
101
102 /*** FUNCTIONS TO WRITE BLOCKS ***/
103
104 static void
105 gifenc_write_header (Gifenc *enc)
106 {
107 g_byte_array_append (enc->buffer, (const guchar *) "GIF89a", 6);
108 }
109
110 static void
111 gifenc_write_lsd (Gifenc *enc, GifencPalette *palette)
112 {
113 g_assert (palette == NULL || gifenc_palette_get_num_colors (palette) >= 2);
114
115 gifenc_write_uint16 (enc, enc->width);
116 gifenc_write_uint16 (enc, enc->height);
117 gifenc_write_bits (enc, palette ? 1 : 0, 1); /* global color table flag */
118 gifenc_write_bits (enc, 0x7, 3); /* color resolution */
119 gifenc_write_bits (enc, 0, 1); /* sort flag */
120 gifenc_write_bits (enc, palette ?
121 log2n (gifenc_palette_get_num_colors (palette) - 1) - 1 : 0, 3); /* number of colors */
122 gifenc_write_byte (enc, 0); /* background color */
123 gifenc_write_byte (enc, 0); /* pixel aspect ratio */
124 }
125
126 static void
127 gifenc_write_color_table (Gifenc *enc, GifencPalette *palette)
128 {
129 guint i, table_size;
130
131 if (!palette)
132 return;
133 i = gifenc_palette_get_num_colors (palette);
134 table_size = 1 << log2n (i - 1);
135 for (i = 0; i < palette->num_colors; i++) {
136 gifenc_write_byte (enc, RED (palette->colors[i]));
137 gifenc_write_byte (enc, GREEN (palette->colors[i]));
138 gifenc_write_byte (enc, BLUE (palette->colors[i]));
139 }
140 if (palette->alpha) {
141 g_byte_array_append (enc->buffer, (guint8 *) "\272\219\001", 3);
142 i++;
143 }
144 for (; i < table_size; i++) {
145 g_byte_array_append (enc->buffer, (guint8 *) "\0\0\0", 3);
146 }
147 }
148
149 typedef struct {
150 guint x;
151 guint y;
152 guint width;
153 guint height;
154 GifencPalette *palette;
155 guint8 *data;
156 guint rowstride;
157 } GifencImage;
158
159 static void
160 gifenc_write_image_description (Gifenc *enc, const GifencImage *image)
161 {
162 gifenc_write_byte (enc, 0x2C);
163 gifenc_write_uint16 (enc, image->x);
164 gifenc_write_uint16 (enc, image->y);
165 gifenc_write_uint16 (enc, image->width);
166 gifenc_write_uint16 (enc, image->height);
167 gifenc_write_bits (enc, image->palette ? 1 : 0, 1); /* local color table flag */
168 gifenc_write_bits (enc, 0, 1); /* interlace flag */
169 gifenc_write_bits (enc, 0, 1); /* sort flag */
170 gifenc_write_bits (enc, 0, 2); /* reserved */
171 gifenc_write_bits (enc, image->palette ?
172 log2n (gifenc_palette_get_num_colors (image->palette) - 1) - 1 : 0, 3); /* number of palette */
173 gifenc_write_color_table (enc, image->palette);
174 }
175
176 typedef struct {
177 guint8 data[255];
178 guint bytes;
179 guint current_data;
180 guint bits;
181 } EncodeBuffer;
182
183 static void
184 gifenc_buffer_write (Gifenc *enc, EncodeBuffer *buffer)
185 {
186 if (buffer->bytes == 0)
187 return;
188 gifenc_write_byte (enc, buffer->bytes);
189 g_byte_array_append (enc->buffer, buffer->data, buffer->bytes);
190 buffer->bytes = 0;
191 }
192
193 static void G_GNUC_UNUSED
194 print_bits (const char *text, guint data, int bits)
195 {
196 int i;
197 g_print ("%s %u (", text, data);
198 for (i = bits - 1; i >= 0; i--) {
199 g_print ("%c", data & (1 << i) ? '1' : '0');
200 }
201 g_print (")\n");
202 }
203 #define print_bits(text, data, bits)
204
205 static void
206 gifenc_buffer_append (Gifenc *enc, EncodeBuffer *buffer, guint data, guint bits)
207 {
208 g_assert (buffer->bits + bits < 24);
209
210 //g_print ("got code %u (%u)\n", data, bits);
211 print_bits ("appending", data, bits);
212 buffer->current_data |= (data << buffer->bits);
213 buffer->bits += bits;
214 while (buffer->bits >= 8) {
215 if (buffer->bytes == 255)
216 gifenc_buffer_write (enc, buffer);
217 buffer->data[buffer->bytes] = buffer->current_data;
218 print_bits ("got", buffer->data[buffer->bytes], 8);
219 buffer->bits -= 8;
220 buffer->current_data >>= 8;
221 buffer->bytes++;
222 }
223 }
224
225 static void
226 gifenc_buffer_flush (Gifenc *enc, EncodeBuffer *buffer)
227 {
228 if (buffer->bits)
229 gifenc_buffer_append (enc, buffer, 0, 8 - buffer->bits);
230 gifenc_buffer_write (enc, buffer);
231 gifenc_write_byte (enc, 0);
232 }
233
234 static void
235 gifenc_write_image_data (Gifenc *enc, const GifencImage *image)
236 {
237 guint codesize, wordsize, x, y;
238 guint next = 0, count = 0, clear, eof, hashcode, hashvalue, cur, codeword;
239 guint8 *data;
240 #define HASH_SIZE (5003)
241 struct {
242 guint value;
243 guint code;
244 } hash[HASH_SIZE];
245 EncodeBuffer buffer = { { 0, }, 0, 0, 0 };
246
247 codesize = log2n (gifenc_palette_get_num_colors (image->palette ?
248 image->palette : enc->palette) - 1);
249 codesize = MAX (codesize, 2);
250 gifenc_write_byte (enc, codesize);
251 //g_print ("codesize with %u palette is %u\n", enc->n_palette, codesize);
252 clear = 1 << codesize;
253 eof = clear + 1;
254 codeword = cur = *image->data;
255 //g_print ("read byte %u\n", cur);
256 wordsize = codesize + 1;
257 gifenc_buffer_append (enc, &buffer, clear, wordsize);
258 if (1 == image->width) {
259 y = 1;
260 x = 0;
261 data = image->data + image->rowstride;
262 } else {
263 y = 0;
264 x = 1;
265 data = image->data;
266 }
267
268 while (y < image->height) {
269 count = eof + 1;
270 next = (1 << wordsize);
271 /* clear hash */
272 memset (hash, 0xFF, sizeof (hash));
273 while (y < image->height) {
274 cur = data[x];
275 //g_print ("read byte %u\n", cur);
276 x++;
277 if (x >= image->width) {
278 y++;
279 x = 0;
280 data += image->rowstride;
281 }
282 hashcode = codeword ^ (cur << 4);
283 hashvalue = (codeword << 8) | cur;
284 loop:
285 if (hash[hashcode].value == hashvalue) {
286 codeword = hash[hashcode].code;
287 continue;
288 }
289 if (hash[hashcode].value != (guint) -1) { /* not empty */
290 hashcode = (hashcode + 0xF) % HASH_SIZE;
291 goto loop;
292 }
293 /* found empty slot, put code there */
294 hash[hashcode].value = hashvalue;
295 hash[hashcode].code = count;
296 //g_print ("saving as %u (%X):", count, count);
297 gifenc_buffer_append (enc, &buffer, codeword, wordsize);
298 count++;
299 codeword = cur;
300 if (count > next) {
301 if (wordsize == 12) {
302 gifenc_buffer_append (enc, &buffer, clear, wordsize);
303 wordsize = codesize + 1;
304 break;
305 }
306 next = MIN (next << 1, 0xFFF);
307 wordsize++;
308 }
309 }
310 }
311 gifenc_buffer_append (enc, &buffer, codeword, wordsize);
312 if (count == next) {
313 wordsize++;
314 if (wordsize > 12) {
315 wordsize = codesize + 1;
316 gifenc_buffer_append (enc, &buffer, clear, wordsize);
317 }
318 }
319 gifenc_buffer_append (enc, &buffer, eof, wordsize);
320 gifenc_buffer_flush (enc, &buffer);
321 }
322
323 static void
324 gifenc_write_graphic_control (Gifenc *enc, GifencPalette *palette,
325 guint milliseconds)
326 {
327 gifenc_write_byte (enc, 0x21); /* extension */
328 gifenc_write_byte (enc, 0xF9); /* extension type */
329 gifenc_write_byte (enc, 0x04); /* size */
330 gifenc_write_bits (enc, 0, 3); /* reserved */
331 gifenc_write_bits (enc, 1, 3); /* disposal: do not dispose */
332 gifenc_write_bits (enc, 0, 1); /* no user input required */
333 gifenc_write_bits (enc, palette->alpha ? 1 : 0, 1); /* transparent color? */
334 gifenc_write_uint16 (enc, milliseconds / 10); /* display this long */
335 gifenc_write_byte (enc, palette->alpha ? palette->num_colors : 0); /* transparent color index */
336 gifenc_write_byte (enc, 0); /* terminator */
337 }
338
339 static void
340 gifenc_write_loop (Gifenc *enc)
341 {
342 gifenc_write_byte (enc, 0x21); /* extension */
343 gifenc_write_byte (enc, 0xFF); /* application extension */
344 gifenc_write_byte (enc, 11); /* block size */
345 g_byte_array_append (enc->buffer, (guint8 *) "NETSCAPE2.0", 11);
346 gifenc_write_byte (enc, 3); /* block size */
347 gifenc_write_byte (enc, 1); /* ??? */
348 gifenc_write_byte (enc, 0); /* ??? */
349 gifenc_write_byte (enc, 0); /* ??? */
350 gifenc_write_byte (enc, 0); /* block terminator */
351 }
352
353 /*** PUBLIC API ***/
354
355 Gifenc *
356 gifenc_new (guint width, guint height, GifencWriteFunc write_func,
357 gpointer write_data, GDestroyNotify write_destroy)
358 {
359 Gifenc *enc;
360
361 g_return_val_if_fail (width <= G_MAXUINT16, NULL);
362 g_return_val_if_fail (height <= G_MAXUINT16, NULL);
363 g_return_val_if_fail (write_func, NULL);
364
365 enc = g_slice_new0 (Gifenc);
366 enc->width = width;
367 enc->height = height;
368 enc->buffer = g_byte_array_new ();
369 enc->write_func = write_func;
370 enc->write_data = write_data;
371 enc->write_destroy = write_destroy;
372
373 return enc;
374 }
375
376 gboolean
377 gifenc_initialize (Gifenc *enc, GifencPalette *palette, gboolean loop, GError **error)
378 {
379 g_return_val_if_fail (enc != NULL, FALSE);
380 g_return_val_if_fail (enc->state == GIFENC_STATE_NEW, FALSE);
381 g_return_val_if_fail (palette != NULL, FALSE);
382
383 gifenc_write_header (enc);
384 gifenc_write_lsd (enc, palette);
385 gifenc_write_color_table (enc, palette);
386 if (loop)
387 gifenc_write_loop (enc);
388 if (!gifenc_flush (enc, error))
389 return FALSE;
390
391 enc->palette = palette;
392 enc->state = GIFENC_STATE_INITIALIZED;
393 return TRUE;
394 }
395
396 gboolean
397 gifenc_add_image (Gifenc *enc, guint x, guint y, guint width, guint height,
398 guint display_millis, guint8 *data, guint rowstride, GError **error)
399 {
400 GifencImage image = { x, y, width, height, NULL, data, rowstride };
401
402 g_return_val_if_fail (enc != NULL, FALSE);
403 g_return_val_if_fail (enc->state == GIFENC_STATE_INITIALIZED, FALSE);
404 g_return_val_if_fail (width > 0, FALSE);
405 g_return_val_if_fail (x + width <= enc->width, FALSE);
406 g_return_val_if_fail (height > 0, FALSE);
407 g_return_val_if_fail (y + height <= enc->height, FALSE);
408
409 //g_print ("adding image (display time %u)\n", display_millis);
410 gifenc_write_graphic_control (enc, image.palette ? image.palette : enc->palette,
411 display_millis);
412 gifenc_write_image_description (enc, &image);
413 gifenc_write_image_data (enc, &image);
414 return gifenc_flush (enc, error);
415 }
416
417 gboolean
418 gifenc_close (Gifenc *enc, GError **error)
419 {
420 g_return_val_if_fail (enc != NULL, FALSE);
421 g_return_val_if_fail (enc->state == GIFENC_STATE_INITIALIZED, FALSE);
422
423 gifenc_write_byte (enc, 0x3B);
424 if (!gifenc_flush (enc, error))
425 return FALSE;
426
427 enc->state = GIFENC_STATE_CLOSED;
428 return TRUE;
429 }
430
431 gboolean
432 gifenc_free (Gifenc *enc)
433 {
434 gboolean success;
435
436 g_return_val_if_fail (enc != NULL, FALSE);
437
438 success = enc->state == GIFENC_STATE_CLOSED;
439
440 if (enc->write_destroy)
441 enc->write_destroy (enc->write_data);
442 if (enc->palette)
443 gifenc_palette_free (enc->palette);
444 g_byte_array_unref (enc->buffer);
445 g_slice_free (Gifenc, enc);
446
447 return success;
448 }
449
450 guint
451 gifenc_get_width (Gifenc *gifenc)
452 {
453 g_return_val_if_fail (gifenc != NULL, 0);
454
455 return gifenc->width;
456 }
457
458 guint
459 gifenc_get_height (Gifenc *gifenc)
460 {
461 g_return_val_if_fail (gifenc != NULL, 0);
462
463 return gifenc->height;
464 }
465
466 /* Floyd-Steinman factors */
467 #define FACTOR0 (23)
468 #define FACTOR1 (79)
469 #define FACTOR2 (41)
470 #define FACTOR_FRONT (113)
471
472 void
473 gifenc_dither_rgb (guint8* target, guint target_rowstride,
474 const GifencPalette *palette, const guint8 *data, guint width, guint height,
475 guint rowstride)
476 {
477 guint x, y, c;
478 gint *this_error, *next_error;
479 guint8 this[3];
480 gint err[3] = { 0, 0, 0 };
481 guint32 pixel;
482
483 g_return_if_fail (palette != NULL);
484
485 this_error = g_new0 (gint, (width + 2) * 3);
486 next_error = g_new (gint, (width + 2) * 3);
487 for (y = 0; y < height; y++) {
488 const guint32 *row = (const guint32 *) (void *) data;
489 gint *cur_error = this_error + 3;
490 gint *cur_next_error = next_error;
491 err[0] = err[1] = err[2] = 0;
492 memset (cur_next_error, 0, sizeof (gint) * 6);
493 for (x = 0; x < width; x++) {
494 //g_print ("%dx%d %2X%2X%2X %2d %2d %2d", x, y, row[0], row[1], row[2],
495 // (err[0] + cur_error[0]) >> 8, (err[1] + cur_error[1]) >> 8,
496 // (err[2] + cur_error[2]) >> 8);
497 for (c = 0; c < 3; c++) {
498 err[c] = ((err[c] + cur_error[c]) >> 8) + (guint8) (*row >> 8 * c);
499 this[c] = err[c] = CLAMP (err[c], 0, 0xFF);
500 }
501 pixel = COLOR (err[2], err[1], err[0]);
502 //g_print (" %2X%2X%2X =>", this[0], this[1], this[2]);
503 target[x] = palette->lookup (palette->data, pixel, &pixel);
504 //g_print (" %2X%2X%2X (%u) %p\n", this[0], this[1], this[2], (guint) target[x], target + x);
505 for (c = 0; c < 3; c++) {
506 this[c] = *row >> 8 * c;
507 err[c] -= this[c];
508 cur_next_error[c] += FACTOR0 * err[c];
509 cur_next_error[c + 3] += FACTOR1 * err[c];
510 cur_next_error[c + 6] = FACTOR2 * err[c];
511 err[c] *= FACTOR_FRONT;
512 }
513 row++;
514 cur_error += 3;
515 cur_next_error += 3;
516 }
517 data += rowstride;
518 cur_error = this_error;
519 this_error = next_error;
520 next_error = cur_error;
521 target += target_rowstride;
522 }
523 g_free (this_error);
524 g_free (next_error);
525 }
526
527 gboolean
528 gifenc_dither_rgb_with_full_image (guint8 *target, guint target_rowstride,
529 guint8 *full, guint full_rowstride,
530 const GifencPalette *palette, const guint8 *data, guint width, guint height,
531 guint rowstride, cairo_rectangle_int_t *rect_out)
532 {
533 int x, y, c;
534 gint *this_error, *next_error;
535 guint8 this[3], alpha;
536 gint err[3] = { 0, 0, 0 };
537 guint32 pixel;
538 cairo_rectangle_int_t area = { width, height, 0, 0 };
539
540 g_return_val_if_fail (palette != NULL, FALSE);
541 g_return_val_if_fail (palette->alpha, FALSE);
542 alpha = gifenc_palette_get_alpha_index (palette);
543
544 this_error = g_new0 (gint, (width + 2) * 3);
545 next_error = g_new (gint, (width + 2) * 3);
546 for (y = 0; y < (int) height; y++) {
547 const guint32 *row = (const guint32 *) (void *) data;
548 gint *cur_error = this_error + 3;
549 gint *cur_next_error = next_error;
550 err[0] = err[1] = err[2] = 0;
551 memset (cur_next_error, 0, sizeof (gint) * 6);
552 for (x = 0; x < (int) width; x++) {
553 //g_print ("%dx%d %2X%2X%2X %2d %2d %2d", x, y, row[0], row[1], row[2],
554 // (err[0] + cur_error[0]) >> 8, (err[1] + cur_error[1]) >> 8,
555 // (err[2] + cur_error[2]) >> 8);
556 for (c = 0; c < 3; c++) {
557 err[c] = ((err[c] + cur_error[c]) >> 8) + (guint8) (*row >> 8 * c);
558 this[c] = err[c] = CLAMP (err[c], 0, 0xFF);
559 }
560 //g_print (" %2X%2X%2X =>", this[0], this[1], this[2]);
561 pixel = COLOR (this[2], this[1], this[0]);
562 target[x] = palette->lookup (palette->data, pixel, &pixel);
563 if (target[x] == full[x]) {
564 target[x] = alpha;
565 } else {
566 area.x = MIN (x, area.x);
567 area.y = MIN (y, area.y);
568 area.width = MAX (x, area.width);
569 area.height = MAX (y, area.height);
570 full[x] = target[x];
571 }
572 //g_print (" %2X%2X%2X (%u) %p\n", this[0], this[1], this[2], (guint) target[x], target + x);
573 for (c = 0; c < 3; c++) {
574 this[0] = *row >> 8 * c;
575 err[c] -= this[c];
576 cur_next_error[c] += FACTOR0 * err[c];
577 cur_next_error[c + 3] += FACTOR1 * err[c];
578 cur_next_error[c + 6] = FACTOR2 * err[c];
579 err[c] *= FACTOR_FRONT;
580 }
581 row++;
582 cur_error += 3;
583 cur_next_error += 3;
584 }
585 data += rowstride;
586 cur_error = this_error;
587 this_error = next_error;
588 next_error = cur_error;
589 target += target_rowstride;
590 full += full_rowstride;
591 }
592 g_free (this_error);
593 g_free (next_error);
594
595 if (area.width < area.x || area.height < area.y) {
596 return FALSE;
597 } else {
598 if (rect_out) {
599 area.width = area.width - area.x + 1;
600 area.height = area.height - area.y + 1;
601 //g_print ("image was %d %d, relevant is %d %d %d %d\n", width, height,
602 // area.x, area.y, area.width, area.height);
603 *rect_out = area;
604 }
605 return TRUE;
606 }
607 }
608