[FFmpeg-devel] [PATCH]Write transparent colour to pal8 gif

Carl Eugen Hoyos cehoyos at ag.or.at
Wed Dec 7 02:22:38 CET 2011


Hi!

Attached allows to write transparent gif if the original file had a palette as 
in ticket 715.
Tested with:
ffmpeg -i 11.png -vcodec gif -f image2 out.gif

Please comment, Carl Eugen
-------------- next part --------------
diff --git a/libavcodec/gif.c b/libavcodec/gif.c
index 8736f0f..655f2c7 100644
--- a/libavcodec/gif.c
+++ b/libavcodec/gif.c
@@ -62,7 +62,7 @@ static int gif_image_write_header(AVCodecContext *avctx,
                                   uint8_t **bytestream, uint32_t *palette)
 {
     int i;
-    unsigned int v;
+    unsigned int v, use_transparency = 0, alpha = 0;
 
     bytestream_put_buffer(bytestream, "GIF", 3);
     bytestream_put_buffer(bytestream, "89a", 3);
@@ -77,8 +77,22 @@ static int gif_image_write_header(AVCodecContext *avctx,
     for(i=0;i<256;i++) {
         v = palette[i];
         bytestream_put_be24(bytestream, v);
+        if ((v >> 24) < 0xFF - use_transparency) {
+            use_transparency = 0xFF - (v >> 24);
+            alpha = i;
+        }
     }
 
+    if (use_transparency) {
+        bytestream_put_byte(bytestream, 0x21); /* Extension Introducer */
+        bytestream_put_byte(bytestream, 0xf9); /* Graphic Control Label */
+        bytestream_put_byte(bytestream, 0x04); /* block length */
+        bytestream_put_byte(bytestream, 0x01); /* Transparent Color Flag */
+        bytestream_put_le16(bytestream, 0x00); /* no delay */
+        bytestream_put_byte(bytestream, alpha);
+        bytestream_put_byte(bytestream, 0x00);
+    }
+        
     return 0;
 }
 


More information about the ffmpeg-devel mailing list