[FFmpeg-devel] [PATCH]Read alpha value from 32bit bmp files

Carl Eugen Hoyos cehoyos at ag.or.at
Fri Dec 2 00:37:17 CET 2011


Michael Niedermayer <michaelni <at> gmx.at> writes:

> > Attached patch allows to read bmp files with transparency made with GIMP
> > and (newer versions) of ImageMagick (older versions produced a file with
> > inverted transparency).
> > 
> > I will apply if nobody objects, please comment, Carl Eugen
> 
> should be ok if tested with a few bmps

I tested this with files from GIMP, ImageMagick and the (often mentioned)
transparent example bitmap file from West Virginia Network.

Encoding needs a bit more changes than below, I suspect ImageMagick wants a 
BITMAPV4HEADER header (gimp reads the files fine).

Patch applied, Carl Eugen

diff --git a/libavcodec/bmpenc.c b/libavcodec/bmpenc.c
index 63c3b72..fbee101 100644
--- a/libavcodec/bmpenc.c
+++ b/libavcodec/bmpenc.c
@@ -35,6 +35,9 @@ static av_cold int bmp_encode_init(AVCodecContext *avctx){
     avctx->coded_frame = (AVFrame*)&s->picture;

     switch (avctx->pix_fmt) {
+    case PIX_FMT_BGRA:
+        avctx->bits_per_coded_sample = 32;
+        break;
     case PIX_FMT_BGR24:
         avctx->bits_per_coded_sample = 24;
         break;
@@ -157,5 +160,5 @@ AVCodec ff_bmp_encoder = {
     .init           = bmp_encode_init,
     .encode         = bmp_encode_frame,
     .pix_fmts = (const enum PixelFormat[]){
-        PIX_FMT_BGR24,
+        PIX_FMT_BGR24, PIX_FMT_BGRA,
         PIX_FMT_RGB555, PIX_FMT_RGB565,




More information about the ffmpeg-devel mailing list