diff --git a/src/ImageSharp/Formats/Exr/ExrDecoderCore.cs b/src/ImageSharp/Formats/Exr/ExrDecoderCore.cs index 533cff2fc9..b823f452ee 100644 --- a/src/ImageSharp/Formats/Exr/ExrDecoderCore.cs +++ b/src/ImageSharp/Formats/Exr/ExrDecoderCore.cs @@ -4,7 +4,9 @@ using System.Buffers; using System.Buffers.Binary; +using System.Numerics; using System.Runtime.CompilerServices; +using System.Runtime.Intrinsics; using System.Text; using SixLabors.ImageSharp.Formats.Exr.Compression; using SixLabors.ImageSharp.IO; @@ -19,6 +21,9 @@ namespace SixLabors.ImageSharp.Formats.Exr; /// internal sealed class ExrDecoderCore : ImageDecoderCore { + private const float Scale32Bit = 1f / 0xFFFFFFFF; + private static readonly Vector4 Scale32BitVector = Vector128.Create(Scale32Bit, Scale32Bit, Scale32Bit, 1f).AsVector4(); + /// /// Reusable buffer. /// @@ -196,7 +201,6 @@ internal sealed class ExrDecoderCore : ImageDecoderCore using ExrBaseDecompressor decompressor = ExrDecompressorFactory.Create(this.Compression, this.memoryAllocator, bytesPerBlock, width, height, rowsPerBlock, channelCount); - TPixel color = default; for (uint y = 0; y < height; y += rowsPerBlock) { ulong rowOffset = this.ReadUnsignedLong(stream); @@ -222,9 +226,7 @@ internal sealed class ExrDecoderCore : ImageDecoderCore for (int x = 0; x < width; x++) { - Rgba128 pixelValue = new(redPixelData[x], greenPixelData[x], bluePixelData[x], hasAlpha ? alphaPixelData[x] : uint.MaxValue); - TPixel.FromVector4(pixelValue.ToVector4()); - pixelRow[x] = color; + pixelRow[x] = ColorScaleTo32Bit(redPixelData[x], greenPixelData[x], bluePixelData[x], hasAlpha ? alphaPixelData[x] : uint.MaxValue); } } } @@ -799,4 +801,9 @@ internal sealed class ExrDecoderCore : ImageDecoderCore return BinaryPrimitives.ReadInt32LittleEndian(this.buffer); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static TPixel ColorScaleTo32Bit(uint r, uint g, uint b, uint a) + where TPixel : unmanaged, IPixel + => TPixel.FromScaledVector4(new Vector4(r, g, b, a) * Scale32Bit); } diff --git a/tests/Images/Input/Exr/rgb_uint32_uncompressed.exr b/tests/Images/Input/Exr/rgb_uint32_uncompressed.exr index 616b676c3c..9a652749f2 100644 --- a/tests/Images/Input/Exr/rgb_uint32_uncompressed.exr +++ b/tests/Images/Input/Exr/rgb_uint32_uncompressed.exr @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:8de6e6f5b1c5acfc67d84e228c45f47ec16a1635f90fd484dfaeb89c28278f10 -size 101408 +oid sha256:487f4641908b0c5c34e7cc3be439eff31e5e8d2fd03a9d17d39dc3dc5e55874d +size 243558