changeset 11725:e9537b2d70ce libavcodec

Grayscale support. Patch by Sebastian Vater <cdgs basty googlemail com>.
author rbultje
date Thu, 13 May 2010 19:16:03 +0000
parents 5b64cfa35e9c
children 7c35c611faa4
files iff.c
diffstat 1 files changed, 21 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/iff.c	Thu May 13 18:41:18 2010 +0000
+++ b/iff.c	Thu May 13 19:16:03 2010 +0000
@@ -110,6 +110,11 @@
     LUT32(28), LUT32(29), LUT32(30), LUT32(31),
 };
 
+// Gray to RGB, required for palette table of grayscale images with bpp < 8
+static av_always_inline uint32_t gray2rgb(const uint32_t x) {
+    return x << 16 | x << 8 | x;
+}
+
 /**
  * Convert CMAP buffer (stored in extradata) to lavc palette format
  */
@@ -125,9 +130,17 @@
     count = 1 << avctx->bits_per_coded_sample;
     // If extradata is smaller than actually needed, fill the remaining with black.
     count = FFMIN(avctx->extradata_size / 3, count);
+    if (count) {
     for (i=0; i < count; i++) {
         pal[i] = 0xFF000000 | AV_RB24( avctx->extradata + i*3 );
     }
+    } else { // Create gray-scale color palette for bps < 8
+        count = 1 << avctx->bits_per_coded_sample;
+
+        for (i=0; i < count; i++) {
+            pal[i] = 0xFF000000 | gray2rgb((i * 255) >> avctx->bits_per_coded_sample);
+        }
+    }
     return 0;
 }
 
@@ -137,7 +150,9 @@
     int err;
 
     if (avctx->bits_per_coded_sample <= 8) {
-        avctx->pix_fmt = PIX_FMT_PAL8;
+        avctx->pix_fmt = (avctx->bits_per_coded_sample < 8 ||
+                          avctx->extradata_size) ? PIX_FMT_PAL8
+                                                 : PIX_FMT_GRAY8;
     } else if (avctx->bits_per_coded_sample <= 32) {
         avctx->pix_fmt = PIX_FMT_BGR32;
     } else {
@@ -157,7 +172,8 @@
         return err;
     }
 
-    return avctx->bits_per_coded_sample <= 8 ?
+    return (avctx->bits_per_coded_sample <= 8 &&
+            avctx->pix_fmt != PIX_FMT_GRAY8) ?
        ff_cmap_read_palette(avctx, (uint32_t*)s->frame.data[1]) : 0;
 }
 
@@ -219,7 +235,7 @@
     }
 
     if (avctx->codec_tag == MKTAG('I','L','B','M')) { // interleaved
-        if (avctx->pix_fmt == PIX_FMT_PAL8) {
+        if (avctx->pix_fmt == PIX_FMT_PAL8 || avctx->pix_fmt == PIX_FMT_GRAY8) {
             for(y = 0; y < avctx->height; y++ ) {
                 uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
                 memset(row, 0, avctx->width);
@@ -238,7 +254,7 @@
                 }
             }
         }
-    } else if (avctx->pix_fmt == PIX_FMT_PAL8) { // IFF-PBM
+    } else if (avctx->pix_fmt == PIX_FMT_PAL8 || avctx->pix_fmt == PIX_FMT_GRAY8) { // IFF-PBM
         for(y = 0; y < avctx->height; y++ ) {
             uint8_t *row = &s->frame.data[0][y * s->frame.linesize[0]];
             memcpy(row, buf, FFMIN(avctx->width, buf_end - buf));
@@ -267,7 +283,7 @@
     }
 
     if (avctx->codec_tag == MKTAG('I','L','B','M')) { //interleaved
-        if (avctx->pix_fmt == PIX_FMT_PAL8) {
+        if (avctx->pix_fmt == PIX_FMT_PAL8 || avctx->pix_fmt == PIX_FMT_GRAY8) {
             for(y = 0; y < avctx->height ; y++ ) {
                 uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
                 memset(row, 0, avctx->width);