diff --git a/src/ImageProcessor/Colors/Color.cs b/src/ImageProcessor/Colors/Color.cs index 62d4aef7bf..af1383e0a5 100644 --- a/src/ImageProcessor/Colors/Color.cs +++ b/src/ImageProcessor/Colors/Color.cs @@ -8,6 +8,7 @@ namespace ImageProcessor using System; using System.ComponentModel; using System.Numerics; + using System.Runtime.CompilerServices; /// /// Represents a four-component color using red, green, blue, and alpha data. diff --git a/src/ImageProcessor/Common/Helpers/PixelOperations.cs b/src/ImageProcessor/Common/Helpers/PixelOperations.cs deleted file mode 100644 index 40a0fb399c..0000000000 --- a/src/ImageProcessor/Common/Helpers/PixelOperations.cs +++ /dev/null @@ -1,92 +0,0 @@ -// -// Copyright (c) James South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageProcessor -{ - using System; - using System.Numerics; - - /// - /// Performs per-pixel operations. - /// - public static class PixelOperations - { - /// - /// Converts an pixel from an sRGB color-space to the equivalent linear color-space. - /// - /// - /// The to convert. - /// - /// - /// The . - /// - public static Color ToLinear(Color composite) - { - // TODO: Figure out a way to either cache these values quickly or perform the calcuations together. - composite.R = SrgbToLinear(composite.R); - composite.G = SrgbToLinear(composite.G); - composite.B = SrgbToLinear(composite.B); - - return composite; - } - - /// - /// Converts a pixel from a linear color-space to the equivalent sRGB color-space. - /// - /// - /// The to convert. - /// - /// - /// The . - /// - public static Color ToSrgb(Color linear) - { - // TODO: Figure out a way to either cache these values quickly or perform the calcuations together. - linear.R = LinearToSrgb(linear.R); - linear.G = LinearToSrgb(linear.G); - linear.B = LinearToSrgb(linear.B); - - return linear; - } - - /// - /// Gets the correct linear value from an sRGB signal. - /// - /// - /// - /// The signal value to convert. - /// - /// The . - /// - private static float SrgbToLinear(float signal) - { - if (signal <= 0.04045f) - { - return signal / 12.92f; - } - - return (float)Math.Pow((signal + 0.055f) / 1.055f, 2.4f); - } - - /// - /// Gets the correct sRGB value from an linear signal. - /// - /// - /// - /// The signal value to convert. - /// - /// The . - /// - private static float LinearToSrgb(float signal) - { - if (signal <= 0.0031308f) - { - return signal * 12.92f; - } - - return (1.055f * (float)Math.Pow(signal, 0.41666666f)) - 0.055f; - } - } -} \ No newline at end of file diff --git a/src/ImageProcessor/Filters/Brightness.cs b/src/ImageProcessor/Filters/Brightness.cs index 5edd4963fa..e278ddaa40 100644 --- a/src/ImageProcessor/Filters/Brightness.cs +++ b/src/ImageProcessor/Filters/Brightness.cs @@ -66,12 +66,12 @@ namespace ImageProcessor.Filters /// private static Color AdjustBrightness(Color color, float brightness) { - color = PixelOperations.ToLinear(color); + color = Color.InverseCompand(color); Vector3 vector3 = color.ToVector3(); vector3 += new Vector3(brightness, brightness, brightness); - return PixelOperations.ToSrgb(new Color(vector3, color.A)); + return Color.Compand(new Color(vector3, color.A)); } } } diff --git a/src/ImageProcessor/Filters/ColorMatrix/ColorMatrixFilter.cs b/src/ImageProcessor/Filters/ColorMatrix/ColorMatrixFilter.cs index a8382500de..26cf66da74 100644 --- a/src/ImageProcessor/Filters/ColorMatrix/ColorMatrixFilter.cs +++ b/src/ImageProcessor/Filters/ColorMatrix/ColorMatrixFilter.cs @@ -50,7 +50,7 @@ namespace ImageProcessor.Filters /// private static Color ApplyMatrix(Color color, Matrix4x4 matrix) { - color = PixelOperations.ToLinear(color); + color = Color.InverseCompand(color); float sr = color.R; float sg = color.G; @@ -60,7 +60,7 @@ namespace ImageProcessor.Filters color.G = (sr * matrix.M12) + (sg * matrix.M22) + (sb * matrix.M32) + matrix.M42; color.B = (sr * matrix.M13) + (sg * matrix.M23) + (sb * matrix.M33) + matrix.M43; - return PixelOperations.ToSrgb(color); + return Color.Compand(color); } } } diff --git a/src/ImageProcessor/Filters/Contrast.cs b/src/ImageProcessor/Filters/Contrast.cs index 50d45d397b..e1bd3a7664 100644 --- a/src/ImageProcessor/Filters/Contrast.cs +++ b/src/ImageProcessor/Filters/Contrast.cs @@ -65,7 +65,7 @@ namespace ImageProcessor.Filters /// private static Color AdjustContrast(Color color, float contrast) { - color = PixelOperations.ToLinear(color); + color = Color.InverseCompand(color); // Seems to be faster than Vector3. color.R -= 0.5f; @@ -80,7 +80,7 @@ namespace ImageProcessor.Filters color.B *= contrast; color.B += 0.5f; - return PixelOperations.ToSrgb(color); + return Color.Compand(color); } } } diff --git a/src/ImageProcessor/ImageProcessor.csproj b/src/ImageProcessor/ImageProcessor.csproj index 14ec5b965a..5f541ab6a1 100644 --- a/src/ImageProcessor/ImageProcessor.csproj +++ b/src/ImageProcessor/ImageProcessor.csproj @@ -47,7 +47,6 @@ - diff --git a/src/ImageProcessor/Samplers/Resize.cs b/src/ImageProcessor/Samplers/Resize.cs index f43463efce..f6e1759679 100644 --- a/src/ImageProcessor/Samplers/Resize.cs +++ b/src/ImageProcessor/Samplers/Resize.cs @@ -97,8 +97,7 @@ namespace ImageProcessor.Samplers } int originX = xw.Index; - Color sourceColor = PixelOperations.ToLinear(source[originX, originY]); - + Color sourceColor = Color.InverseCompand(source[originX, originY]); if (Math.Abs(sourceColor.A) < Epsilon) { continue; @@ -113,8 +112,7 @@ namespace ImageProcessor.Samplers } } - destination = PixelOperations.ToSrgb(destination); - target[x, y] = destination; + target[x, y] = Color.Compand(destination); } } }); diff --git a/src/ImageProcessor/project.lock.json.REMOVED.git-id b/src/ImageProcessor/project.lock.json.REMOVED.git-id index dba2656f57..24339fed21 100644 --- a/src/ImageProcessor/project.lock.json.REMOVED.git-id +++ b/src/ImageProcessor/project.lock.json.REMOVED.git-id @@ -1 +1 @@ -eb00c54ee74016c2b70f81963e7e8f83cb2dd54b \ No newline at end of file +3f05708641eb3ed085d4689aae4a960eb067fd16 \ No newline at end of file