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

Carl Eugen Hoyos cehoyos at ag.or.at
Wed Dec 7 11:57:02 CET 2011


On Wednesday 07 December 2011 03:40:31 am Michael Niedermayer wrote:
> On Wed, Dec 07, 2011 at 02:22:38AM +0100, Carl Eugen Hoyos wrote:
> > 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
> >
> >  gif.c |   16 +++++++++++++++-
> >  1 file changed, 15 insertions(+), 1 deletion(-)
> > 20cc35d190a3b3e94fda434ac5aef2f5111e8ed7  patchgifalpha.diff
> > 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);
> 
> id write
> smallest_alpha = 0xFF;
> 
> if(smallest_alpha > v >> 24){
>     smallest_alpha = v >> 24
>     ...
> }
> 
> if(smallest_alpha< ?){

Thank you, new patch attached.

Please comment, Carl Eugen
-------------- next part --------------
diff --git a/libavcodec/gif.c b/libavcodec/gif.c
index 8736f0f..9d0519d 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, smallest_alpha = 0xFF, alpha_component = 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 < smallest_alpha) {
+            smallest_alpha = v >> 24;
+            alpha_component = i;
+        }
     }
 
+    if (smallest_alpha < 128) {
+        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_component);
+        bytestream_put_byte(bytestream, 0x00);
+    }
+        
     return 0;
 }
 


More information about the ffmpeg-devel mailing list