Hi!

Attached patch allows to decode gray float pbm images, sample by ami_stuff.

Please comment, Carl Eugen
From 71283c6de2eb5ef45382390d695845abb500b316 Mon Sep 17 00:00:00 2001
From: Carl Eugen Hoyos <ceffm...@gmail.com>
Date: Thu, 11 Feb 2021 23:37:06 +0100
Subject: [PATCH] lavc/pnm: Allow decoding of gray float pbm images.

---
 libavcodec/pnm.c    |  5 ++++-
 libavcodec/pnmdec.c | 24 ++++++++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/libavcodec/pnm.c b/libavcodec/pnm.c
index aad23c7ae2..5af04965e7 100644
--- a/libavcodec/pnm.c
+++ b/libavcodec/pnm.c
@@ -72,6 +72,7 @@ int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
         s->bytestream[0] != 'P' ||
         (s->bytestream[1] < '1' ||
          s->bytestream[1] > '7' &&
+         s->bytestream[1] != 'f' &&
          s->bytestream[1] != 'F')) {
         s->bytestream += s->bytestream_end > s->bytestream;
         s->bytestream += s->bytestream_end > s->bytestream;
@@ -82,6 +83,8 @@ int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
 
     if (buf1[1] == 'F') {
         avctx->pix_fmt = AV_PIX_FMT_GBRPF32;
+    } else if (buf1[1] == 'f') {
+        avctx->pix_fmt = AV_PIX_FMT_GRAYF32;
     } else if (s->type==1 || s->type==4) {
         avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
     } else if (s->type==2 || s->type==5) {
@@ -177,7 +180,7 @@ int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
     if (ret < 0)
         return ret;
 
-    if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32) {
+    if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32 || avctx->pix_fmt == AV_PIX_FMT_GRAYF32) {
         pnm_get(s, buf1, sizeof(buf1));
         if (av_sscanf(buf1, "%f", &s->scale) != 1 || s->scale == 0.0 || !isfinite(s->scale)) {
             av_log(avctx, AV_LOG_ERROR, "Invalid scale.\n");
diff --git a/libavcodec/pnmdec.c b/libavcodec/pnmdec.c
index 9add5cfc84..4d5ce0bcb5 100644
--- a/libavcodec/pnmdec.c
+++ b/libavcodec/pnmdec.c
@@ -297,6 +297,30 @@ static int pnm_decode_frame(AVCodecContext *avctx, void *data,
             }
         }
         break;
+    case AV_PIX_FMT_GRAYF32:
+        if (avctx->width * avctx->height * 4 > s->bytestream_end - s->bytestream)
+            return AVERROR_INVALIDDATA;
+        scale = 1.f / s->scale;
+        if (s->endian) {
+            float *g = (float *)p->data[0];
+            for (int i = 0; i < avctx->height; i++) {
+                for (int j = 0; j < avctx->width; j++) {
+                    g[j] = av_int2float(AV_RL32(s->bytestream)) * scale;
+                    s->bytestream += 4;
+                }
+                g += p->linesize[0] / 4;
+            }
+        } else {
+            float *g = (float *)p->data[0];
+            for (int i = 0; i < avctx->height; i++) {
+                for (int j = 0; j < avctx->width; j++) {
+                    g[j] = av_int2float(AV_RB32(s->bytestream)) * scale;
+                    s->bytestream += 4;
+                }
+                g += p->linesize[0] / 4;
+            }
+        }
+        break;
     }
     *got_frame = 1;
 
-- 
2.29.2

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to