From 01a6d8b285e6a01683c9a93191ce87709ec39a9e Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 10 Oct 2018 13:19:16 +0100 Subject: [PATCH] Unify sRGB companding --- .../Common/Extensions/Vector4Extensions.cs | 78 ++++++------------- .../Helpers/Vector4ExtensionsTests.cs | 2 - 2 files changed, 25 insertions(+), 55 deletions(-) diff --git a/src/ImageSharp/Common/Extensions/Vector4Extensions.cs b/src/ImageSharp/Common/Extensions/Vector4Extensions.cs index f9bbdfc040..50afc6a4b4 100644 --- a/src/ImageSharp/Common/Extensions/Vector4Extensions.cs +++ b/src/ImageSharp/Common/Extensions/Vector4Extensions.cs @@ -5,7 +5,7 @@ using System; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; - +using SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation; using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp @@ -55,8 +55,10 @@ namespace SixLabors.ImageSharp for (int i = 0; i < vectors.Length; i++) { ref Vector4 v = ref Unsafe.Add(ref baseRef, i); - var s = new Vector4(v.W); - s.W = 1; + var s = new Vector4(v.W) + { + W = 1 + }; v *= s; } } @@ -73,8 +75,10 @@ namespace SixLabors.ImageSharp for (int i = 0; i < vectors.Length; i++) { ref Vector4 v = ref Unsafe.Add(ref baseRef, i); - var s = new Vector4(1 / v.W); - s.W = 1; + var s = new Vector4(1 / v.W) + { + W = 1 + }; v *= s; } } @@ -90,7 +94,11 @@ namespace SixLabors.ImageSharp public static Vector4 Compress(this Vector4 linear) { // TODO: Is there a faster way to do this? - return new Vector4(Compress(linear.X), Compress(linear.Y), Compress(linear.Z), linear.W); + return new Vector4( + SRgbCompanding.Compress(linear.X), + SRgbCompanding.Compress(linear.Y), + SRgbCompanding.Compress(linear.Z), + linear.W); } /// @@ -104,7 +112,11 @@ namespace SixLabors.ImageSharp public static Vector4 Expand(this Vector4 gamma) { // TODO: Is there a faster way to do this? - return new Vector4(Expand(gamma.X), Expand(gamma.Y), Expand(gamma.Z), gamma.W); + return new Vector4( + SRgbCompanding.Expand(gamma.X), + SRgbCompanding.Expand(gamma.Y), + SRgbCompanding.Expand(gamma.Z), + gamma.W); } /// @@ -118,9 +130,9 @@ namespace SixLabors.ImageSharp for (int i = 0; i < vectors.Length; i++) { ref Vector4 v = ref Unsafe.Add(ref baseRef, i); - v.X = Compress(v.X); - v.Y = Compress(v.Y); - v.Z = Compress(v.Z); + v.X = SRgbCompanding.Compress(v.X); + v.Y = SRgbCompanding.Compress(v.Y); + v.Z = SRgbCompanding.Compress(v.Z); } } @@ -135,50 +147,10 @@ namespace SixLabors.ImageSharp for (int i = 0; i < vectors.Length; i++) { ref Vector4 v = ref Unsafe.Add(ref baseRef, i); - v.X = Expand(v.X); - v.Y = Expand(v.Y); - v.Z = Expand(v.Z); - } - } - - /// - /// Gets the compressed sRGB value from an linear signal. - /// - /// - /// - /// The signal value to compress. - /// - /// The . - /// - [MethodImpl(InliningOptions.ShortMethod)] - private static float Compress(float signal) - { - if (signal <= 0.0031308F) - { - return signal * 12.92F; + v.X = SRgbCompanding.Expand(v.X); + v.Y = SRgbCompanding.Expand(v.Y); + v.Z = SRgbCompanding.Expand(v.Z); } - - return (1.055F * MathF.Pow(signal, 0.41666666F)) - 0.055F; - } - - /// - /// Gets the expanded linear value from an sRGB signal. - /// - /// - /// - /// The signal value to expand. - /// - /// The . - /// - [MethodImpl(InliningOptions.ShortMethod)] - private static float Expand(float signal) - { - if (signal <= 0.04045F) - { - return signal / 12.92F; - } - - return MathF.Pow((signal + 0.055F) / 1.055F, 2.4F); } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Helpers/Vector4ExtensionsTests.cs b/tests/ImageSharp.Tests/Helpers/Vector4ExtensionsTests.cs index 68f71d88f8..2d2a2795d8 100644 --- a/tests/ImageSharp.Tests/Helpers/Vector4ExtensionsTests.cs +++ b/tests/ImageSharp.Tests/Helpers/Vector4ExtensionsTests.cs @@ -70,7 +70,5 @@ namespace SixLabors.ImageSharp.Tests.Helpers Assert.Equal(expected, source, new ApproximateFloatComparer(1e-6f)); } - - } }