diff mbox series

[FFmpeg-devel,v2,1/2] lavc/texturedsp: fix premult2straight inversion

Message ID 20240202011051.46786-1-connorbworley@gmail.com
State New
Headers show
Series [FFmpeg-devel,v2,1/2] lavc/texturedsp: fix premult2straight inversion | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Connor Worley Feb. 2, 2024, 1:10 a.m. UTC
This function should convert premultiplied alpha to straight, but does the opposite.

Signed-off-by: Connor Worley <connorbworley@gmail.com>
---
 libavcodec/texturedsp.c | 9 ++++++---
 tests/ref/fate/dds-dxt2 | 2 +-
 tests/ref/fate/dds-dxt4 | 2 +-
 3 files changed, 8 insertions(+), 5 deletions(-)

Comments

Niklas Haas Feb. 2, 2024, 8:41 a.m. UTC | #1
On Thu, 01 Feb 2024 17:10:50 -0800 Connor Worley <connorbworley@gmail.com> wrote:
> +    src[0] = (uint8_t) FFMIN(r * 255 / a, 255);
> +    src[1] = (uint8_t) FFMIN(g * 255 / a, 255);
> +    src[2] = (uint8_t) FFMIN(b * 255 / a, 255);

av_clip_uint8?
Andreas Rheinhardt Feb. 2, 2024, 9:16 a.m. UTC | #2
Niklas Haas:
> On Thu, 01 Feb 2024 17:10:50 -0800 Connor Worley <connorbworley@gmail.com> wrote:
>> +    src[0] = (uint8_t) FFMIN(r * 255 / a, 255);
>> +    src[1] = (uint8_t) FFMIN(g * 255 / a, 255);
>> +    src[2] = (uint8_t) FFMIN(b * 255 / a, 255);
> 
> av_clip_uint8?

That is for int values and therefore has to take care of negative values
as well. At least the C version is more complicated (i.e. the compiler
will have to work harder to optimize it to something like the above).

- Andreas
Connor Worley Feb. 2, 2024, 8:15 p.m. UTC | #3
Linked are some demo files.

I converted a sample to DDS with texconv.exe -f DXT4 -pmalpha sample.png
I converted it back to PNG against master with ffmpeg -i sample.dds
master.png
I converted it back to PNG against this patch with ffmpeg -i sample.dds
patch.png

The color in master.png is dark due to being premultiplied twice.
The color in patch.png is true to sample.png.

https://connorworley.com/sample.png
https://connorworley.com/sample.dds
https://connorworley.com/master.png
https://connorworley.com/patch.png
diff mbox series

Patch

diff --git a/libavcodec/texturedsp.c b/libavcodec/texturedsp.c
index 5fb79937da..721bfc17f5 100644
--- a/libavcodec/texturedsp.c
+++ b/libavcodec/texturedsp.c
@@ -175,9 +175,12 @@  static av_always_inline void premult2straight(uint8_t *src)
     int b = src[2];
     int a = src[3]; /* unchanged */
 
-    src[0] = (uint8_t) r * a / 255;
-    src[1] = (uint8_t) g * a / 255;
-    src[2] = (uint8_t) b * a / 255;
+    if (a == 0)
+        return;
+
+    src[0] = (uint8_t) FFMIN(r * 255 / a, 255);
+    src[1] = (uint8_t) FFMIN(g * 255 / a, 255);
+    src[2] = (uint8_t) FFMIN(b * 255 / a, 255);
 }
 
 /**
diff --git a/tests/ref/fate/dds-dxt2 b/tests/ref/fate/dds-dxt2
index b5bdfbadc3..1744ef41f6 100644
--- a/tests/ref/fate/dds-dxt2
+++ b/tests/ref/fate/dds-dxt2
@@ -3,4 +3,4 @@ 
 #codec_id 0: rawvideo
 #dimensions 0: 64x64
 #sar 0: 0/1
-0,          0,          0,        1,    16384, 0x11cebeb0
+0,          0,          0,        1,    16384, 0xd7f7241b
diff --git a/tests/ref/fate/dds-dxt4 b/tests/ref/fate/dds-dxt4
index 136dfd8006..f22878da56 100644
--- a/tests/ref/fate/dds-dxt4
+++ b/tests/ref/fate/dds-dxt4
@@ -3,4 +3,4 @@ 
 #codec_id 0: rawvideo
 #dimensions 0: 64x64
 #sar 0: 0/1
-0,          0,          0,        1,    16384, 0x31aaacd6
+0,          0,          0,        1,    16384, 0xf18d4216