From 9b4c0ef4e93672940ac9280606826705bea96ceb Mon Sep 17 00:00:00 2001 From: Moonchild Date: Wed, 30 Nov 2022 11:32:44 +0000 Subject: [PATCH] Issue #2040 - Pre-multiply the alpha values in our JXL decode buffer. Using selective calculation (only if not opaque) and fast integer math here should optimize well in compilers. --- image/decoders/nsJXLDecoder.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/image/decoders/nsJXLDecoder.cpp b/image/decoders/nsJXLDecoder.cpp index fa4f6e77de..64a192877d 100644 --- a/image/decoders/nsJXLDecoder.cpp +++ b/image/decoders/nsJXLDecoder.cpp @@ -145,6 +145,12 @@ nsJXLDecoder::ReadJXLData(const char* aData, size_t aLength) // We currently have a channel ordering mis-match here. for (uint8_t* pixPtr = rowPtr; pixPtr < rowPtr + mInfo.xsize * 4; pixPtr+=4){ std::swap(pixPtr[0], pixPtr[2]); + // Pre-multiply, too + if (pixPtr[3] < 255) { + pixPtr[0]=((uint16_t)pixPtr[0]*(uint16_t)pixPtr[3]) >> 8; + pixPtr[1]=((uint16_t)pixPtr[1]*(uint16_t)pixPtr[3]) >> 8; + pixPtr[2]=((uint16_t)pixPtr[2]*(uint16_t)pixPtr[3]) >> 8; + } } pipe->WriteBuffer(reinterpret_cast(rowPtr)); }