From 61a9f099b7da38722f758db38d9c89d1b39f3a87 Mon Sep 17 00:00:00 2001 From: Carl Eugen Hoyos Date: Tue, 9 Oct 2012 10:13:14 +0200 Subject: [PATCH] Write 32bit palette to Targa files. Current ImageMagick fails to read such files, therefore only write the 32bit palette if the palette actually contains any transparency information. --- libavcodec/targaenc.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/libavcodec/targaenc.c b/libavcodec/targaenc.c index 35e3ea5bdd..4a71f27522 100644 --- a/libavcodec/targaenc.c +++ b/libavcodec/targaenc.c @@ -103,16 +103,27 @@ static int targa_encode_frame(AVCodecContext *avctx, AVPacket *pkt, avctx->bits_per_coded_sample = av_get_bits_per_pixel(&av_pix_fmt_descriptors[avctx->pix_fmt]); switch(avctx->pix_fmt) { - case AV_PIX_FMT_PAL8: + case AV_PIX_FMT_PAL8: { + int pal_bpp = 24; /* Only write 32bit palette if there is transparency information */ + for (i = 0; i < 256; i++) + if (AV_RN32(p->data[1] + 4 * i) >> 24 != 0xFF) { + pal_bpp = 32; + break; + } pkt->data[1] = 1; /* palette present */ pkt->data[2] = TGA_PAL; /* uncompressed palettised image */ pkt->data[6] = 1; /* palette contains 256 entries */ - pkt->data[7] = 24; /* palette contains 24 bit entries */ + pkt->data[7] = pal_bpp; /* palette contains pal_bpp bit entries */ pkt->data[16] = 8; /* bpp */ for (i = 0; i < 256; i++) + if (pal_bpp == 32) { + AV_WL32(pkt->data + 18 + 4 * i, *(uint32_t *)(p->data[1] + i * 4)); + } else { AV_WL24(pkt->data + 18 + 3 * i, *(uint32_t *)(p->data[1] + i * 4)); - out += 256 * 3; /* skip past the palette we just output */ + } + out += 32 * pal_bpp; /* skip past the palette we just output */ break; + } case AV_PIX_FMT_GRAY8: pkt->data[2] = TGA_BW; /* uncompressed grayscale image */ avctx->bits_per_coded_sample = 0x28;