From f66fe45ac1d21eefd4e77282d8ac927e807c9398 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 23 Nov 2020 00:59:46 +0000 Subject: [PATCH] Utils FTW --- .../Common/Helpers/DenseMatrixUtils.cs | 12 +++++----- .../{Vector4Utilities.cs => Vector4Utils.cs} | 2 +- .../PixelFormats/Utils/Vector4Converters.cs | 4 ++-- .../Filters/FilterProcessor{TPixel}.cs | 2 +- .../AffineTransformProcessor{TPixel}.cs | 6 ++--- ...rmUtilities.cs => LinearTransformUtils.cs} | 6 ++--- .../ProjectiveTransformProcessor{TPixel}.cs | 6 ++--- .../Color/Bulk/PremultiplyVector4.cs | 2 +- .../Color/Bulk/UnPremultiplyVector4.cs | 2 +- .../Helpers/Vector4UtilsTests.cs | 8 +++---- .../PixelOperations/PixelOperationsTests.cs | 24 +++++++++---------- 11 files changed, 37 insertions(+), 37 deletions(-) rename src/ImageSharp/Common/Helpers/{Vector4Utilities.cs => Vector4Utils.cs} (99%) rename src/ImageSharp/Processing/Processors/Transforms/Linear/{LinearTransformUtilities.cs => LinearTransformUtils.cs} (95%) diff --git a/src/ImageSharp/Common/Helpers/DenseMatrixUtils.cs b/src/ImageSharp/Common/Helpers/DenseMatrixUtils.cs index 69c50573f..7da2528fa 100644 --- a/src/ImageSharp/Common/Helpers/DenseMatrixUtils.cs +++ b/src/ImageSharp/Common/Helpers/DenseMatrixUtils.cs @@ -59,7 +59,7 @@ namespace SixLabors.ImageSharp ref Vector4 target = ref Unsafe.Add(ref targetRowRef, column); vector.W = target.W; - Vector4Utilities.UnPremultiply(ref vector); + Vector4Utils.UnPremultiply(ref vector); target = vector; } @@ -105,7 +105,7 @@ namespace SixLabors.ImageSharp out Vector4 vector); ref Vector4 target = ref Unsafe.Add(ref targetRowRef, column); - Vector4Utilities.UnPremultiply(ref vector); + Vector4Utils.UnPremultiply(ref vector); target = vector; } @@ -140,7 +140,7 @@ namespace SixLabors.ImageSharp { int offsetX = Numerics.Clamp(sourceOffsetColumnBase + x - radiusX, minColumn, maxColumn); var currentColor = sourceRowSpan[offsetX].ToVector4(); - Vector4Utilities.Premultiply(ref currentColor); + Vector4Utils.Premultiply(ref currentColor); vectorX += matrixX[y, x] * currentColor; vectorY += matrixY[y, x] * currentColor; @@ -193,7 +193,7 @@ namespace SixLabors.ImageSharp ref Vector4 target = ref Unsafe.Add(ref targetRowRef, column); vector.W = target.W; - Vector4Utilities.UnPremultiply(ref vector); + Vector4Utils.UnPremultiply(ref vector); target = vector; } @@ -238,7 +238,7 @@ namespace SixLabors.ImageSharp ref vector); ref Vector4 target = ref Unsafe.Add(ref targetRowRef, column); - Vector4Utilities.UnPremultiply(ref vector); + Vector4Utils.UnPremultiply(ref vector); target = vector; } @@ -270,7 +270,7 @@ namespace SixLabors.ImageSharp { int offsetX = Numerics.Clamp(sourceOffsetColumnBase + x - radiusX, minColumn, maxColumn); var currentColor = sourceRowSpan[offsetX].ToVector4(); - Vector4Utilities.Premultiply(ref currentColor); + Vector4Utils.Premultiply(ref currentColor); vector += matrix[y, x] * currentColor; } } diff --git a/src/ImageSharp/Common/Helpers/Vector4Utilities.cs b/src/ImageSharp/Common/Helpers/Vector4Utils.cs similarity index 99% rename from src/ImageSharp/Common/Helpers/Vector4Utilities.cs rename to src/ImageSharp/Common/Helpers/Vector4Utils.cs index cab5d5675..05661991a 100644 --- a/src/ImageSharp/Common/Helpers/Vector4Utilities.cs +++ b/src/ImageSharp/Common/Helpers/Vector4Utils.cs @@ -15,7 +15,7 @@ namespace SixLabors.ImageSharp /// /// Utility methods for the struct. /// - internal static class Vector4Utilities + internal static class Vector4Utils { private const int BlendAlphaControl = 0b_10_00_10_00; private const int ShuffleAlphaControl = 0b_11_11_11_11; diff --git a/src/ImageSharp/PixelFormats/Utils/Vector4Converters.cs b/src/ImageSharp/PixelFormats/Utils/Vector4Converters.cs index 2dca8f2c1..977722b81 100644 --- a/src/ImageSharp/PixelFormats/Utils/Vector4Converters.cs +++ b/src/ImageSharp/PixelFormats/Utils/Vector4Converters.cs @@ -24,7 +24,7 @@ namespace SixLabors.ImageSharp.PixelFormats.Utils if (modifiers.IsDefined(PixelConversionModifiers.Premultiply)) { - Vector4Utilities.Premultiply(vectors); + Vector4Utils.Premultiply(vectors); } } @@ -36,7 +36,7 @@ namespace SixLabors.ImageSharp.PixelFormats.Utils { if (modifiers.IsDefined(PixelConversionModifiers.Premultiply)) { - Vector4Utilities.UnPremultiply(vectors); + Vector4Utils.UnPremultiply(vectors); } if (modifiers.IsDefined(PixelConversionModifiers.SRgbCompand)) diff --git a/src/ImageSharp/Processing/Processors/Filters/FilterProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Filters/FilterProcessor{TPixel}.cs index 584ba072c..e28d4add7 100644 --- a/src/ImageSharp/Processing/Processors/Filters/FilterProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Filters/FilterProcessor{TPixel}.cs @@ -74,7 +74,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Filters Span rowSpan = this.source.GetPixelRowSpan(y).Slice(this.startX, span.Length); PixelOperations.Instance.ToVector4(this.configuration, rowSpan, span); - Vector4Utilities.Transform(span, ref Unsafe.AsRef(this.matrix)); + Vector4Utils.Transform(span, ref Unsafe.AsRef(this.matrix)); PixelOperations.Instance.FromVector4Destructive(this.configuration, span, rowSpan); } diff --git a/src/ImageSharp/Processing/Processors/Transforms/Linear/AffineTransformProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Transforms/Linear/AffineTransformProcessor{TPixel}.cs index cd7f46d92..c08f5d3d3 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/Linear/AffineTransformProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/Linear/AffineTransformProcessor{TPixel}.cs @@ -80,8 +80,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms return; } - int yRadius = LinearTransformUtilities.GetSamplingRadius(in sampler, source.Height, destination.Height); - int xRadius = LinearTransformUtilities.GetSamplingRadius(in sampler, source.Width, destination.Width); + int yRadius = LinearTransformUtils.GetSamplingRadius(in sampler, source.Height, destination.Height); + int xRadius = LinearTransformUtils.GetSamplingRadius(in sampler, source.Width, destination.Width); var radialExtents = new Vector2(xRadius, yRadius); int yLength = (yRadius * 2) + 1; int xLength = (xRadius * 2) + 1; @@ -207,7 +207,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms // Use the single precision position to calculate correct bounding pixels // otherwise we get rogue pixels outside of the bounds. var point = Vector2.Transform(new Vector2(x, y), this.matrix); - LinearTransformUtilities.Convolve( + LinearTransformUtils.Convolve( in this.sampler, point, sourceBuffer, diff --git a/src/ImageSharp/Processing/Processors/Transforms/Linear/LinearTransformUtilities.cs b/src/ImageSharp/Processing/Processors/Transforms/Linear/LinearTransformUtils.cs similarity index 95% rename from src/ImageSharp/Processing/Processors/Transforms/Linear/LinearTransformUtilities.cs rename to src/ImageSharp/Processing/Processors/Transforms/Linear/LinearTransformUtils.cs index 5c2f66a94..07e784ec5 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/Linear/LinearTransformUtilities.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/Linear/LinearTransformUtils.cs @@ -12,7 +12,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms /// /// Utility methods for affine and projective transforms. /// - internal static class LinearTransformUtilities + internal static class LinearTransformUtils { [MethodImpl(InliningOptions.ShortMethod)] internal static int GetSamplingRadius(in TResampler sampler, int sourceSize, int destinationSize) @@ -78,13 +78,13 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms // Values are first premultiplied to prevent darkening of edge pixels. var current = sourcePixels[x, y].ToVector4(); - Vector4Utilities.Premultiply(ref current); + Vector4Utils.Premultiply(ref current); sum += current * xWeight * yWeight; } } // Reverse the premultiplication - Vector4Utilities.UnPremultiply(ref sum); + Vector4Utils.UnPremultiply(ref sum); targetRow[column] = sum; } diff --git a/src/ImageSharp/Processing/Processors/Transforms/Linear/ProjectiveTransformProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Transforms/Linear/ProjectiveTransformProcessor{TPixel}.cs index 4f7537796..811bb1a88 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/Linear/ProjectiveTransformProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/Linear/ProjectiveTransformProcessor{TPixel}.cs @@ -80,8 +80,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms return; } - int yRadius = LinearTransformUtilities.GetSamplingRadius(in sampler, source.Height, destination.Height); - int xRadius = LinearTransformUtilities.GetSamplingRadius(in sampler, source.Width, destination.Width); + int yRadius = LinearTransformUtils.GetSamplingRadius(in sampler, source.Height, destination.Height); + int xRadius = LinearTransformUtils.GetSamplingRadius(in sampler, source.Width, destination.Width); var radialExtents = new Vector2(xRadius, yRadius); int yLength = (yRadius * 2) + 1; int xLength = (xRadius * 2) + 1; @@ -207,7 +207,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms // Use the single precision position to calculate correct bounding pixels // otherwise we get rogue pixels outside of the bounds. Vector2 point = TransformUtilities.ProjectiveTransform2D(x, y, this.matrix); - LinearTransformUtilities.Convolve( + LinearTransformUtils.Convolve( in this.sampler, point, sourceBuffer, diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/PremultiplyVector4.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/PremultiplyVector4.cs index 2a886c687..d441b5e0b 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/PremultiplyVector4.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/PremultiplyVector4.cs @@ -29,7 +29,7 @@ namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk [Benchmark] public void Premultiply() { - Vector4Utilities.Premultiply(Vectors); + Vector4Utils.Premultiply(Vectors); } [MethodImpl(InliningOptions.ShortMethod)] diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/UnPremultiplyVector4.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/UnPremultiplyVector4.cs index 1312c767b..819f34b92 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/UnPremultiplyVector4.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/UnPremultiplyVector4.cs @@ -29,7 +29,7 @@ namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk [Benchmark] public void UnPremultiply() { - Vector4Utilities.UnPremultiply(Vectors); + Vector4Utils.UnPremultiply(Vectors); } [MethodImpl(InliningOptions.ShortMethod)] diff --git a/tests/ImageSharp.Tests/Helpers/Vector4UtilsTests.cs b/tests/ImageSharp.Tests/Helpers/Vector4UtilsTests.cs index 2bb43c440..605aa7d5a 100644 --- a/tests/ImageSharp.Tests/Helpers/Vector4UtilsTests.cs +++ b/tests/ImageSharp.Tests/Helpers/Vector4UtilsTests.cs @@ -24,11 +24,11 @@ namespace SixLabors.ImageSharp.Tests.Helpers Vector4[] source = rnd.GenerateRandomVectorArray(length, 0, 1); Vector4[] expected = source.Select(v => { - Vector4Utilities.Premultiply(ref v); + Vector4Utils.Premultiply(ref v); return v; }).ToArray(); - Vector4Utilities.Premultiply(source); + Vector4Utils.Premultiply(source); Assert.Equal(expected, source, this.approximateFloatComparer); } @@ -44,11 +44,11 @@ namespace SixLabors.ImageSharp.Tests.Helpers Vector4[] source = rnd.GenerateRandomVectorArray(length, 0, 1); Vector4[] expected = source.Select(v => { - Vector4Utilities.UnPremultiply(ref v); + Vector4Utils.UnPremultiply(ref v); return v; }).ToArray(); - Vector4Utilities.UnPremultiply(source); + Vector4Utils.UnPremultiply(source); Assert.Equal(expected, source, this.approximateFloatComparer); } diff --git a/tests/ImageSharp.Tests/PixelFormats/PixelOperations/PixelOperationsTests.cs b/tests/ImageSharp.Tests/PixelFormats/PixelOperations/PixelOperationsTests.cs index 059a21803..e56cfe226 100644 --- a/tests/ImageSharp.Tests/PixelFormats/PixelOperations/PixelOperationsTests.cs +++ b/tests/ImageSharp.Tests/PixelFormats/PixelOperations/PixelOperationsTests.cs @@ -197,7 +197,7 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats.PixelOperations { if (this.HasUnassociatedAlpha) { - Vector4Utilities.Premultiply(ref v); + Vector4Utils.Premultiply(ref v); } } @@ -205,7 +205,7 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats.PixelOperations { if (this.HasUnassociatedAlpha) { - Vector4Utilities.UnPremultiply(ref v); + Vector4Utils.UnPremultiply(ref v); } } @@ -232,7 +232,7 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats.PixelOperations { if (this.HasUnassociatedAlpha) { - Vector4Utilities.Premultiply(ref v); + Vector4Utils.Premultiply(ref v); } } @@ -240,7 +240,7 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats.PixelOperations { if (this.HasUnassociatedAlpha) { - Vector4Utilities.UnPremultiply(ref v); + Vector4Utils.UnPremultiply(ref v); } } @@ -273,7 +273,7 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats.PixelOperations if (this.HasUnassociatedAlpha) { - Vector4Utilities.Premultiply(ref v); + Vector4Utils.Premultiply(ref v); } } @@ -281,7 +281,7 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats.PixelOperations { if (this.HasUnassociatedAlpha) { - Vector4Utilities.UnPremultiply(ref v); + Vector4Utils.UnPremultiply(ref v); } SRgbCompanding.Compress(ref v); @@ -394,12 +394,12 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats.PixelOperations { void SourceAction(ref Vector4 v) { - Vector4Utilities.UnPremultiply(ref v); + Vector4Utils.UnPremultiply(ref v); } void ExpectedAction(ref Vector4 v) { - Vector4Utilities.Premultiply(ref v); + Vector4Utils.Premultiply(ref v); } TPixel[] source = CreatePixelTestData(count, (ref Vector4 v) => SourceAction(ref v)); @@ -417,12 +417,12 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats.PixelOperations { void SourceAction(ref Vector4 v) { - Vector4Utilities.UnPremultiply(ref v); + Vector4Utils.UnPremultiply(ref v); } void ExpectedAction(ref Vector4 v) { - Vector4Utilities.Premultiply(ref v); + Vector4Utils.Premultiply(ref v); } TPixel[] source = CreateScaledPixelTestData(count, (ref Vector4 v) => SourceAction(ref v)); @@ -444,14 +444,14 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats.PixelOperations { void SourceAction(ref Vector4 v) { - Vector4Utilities.UnPremultiply(ref v); + Vector4Utils.UnPremultiply(ref v); SRgbCompanding.Compress(ref v); } void ExpectedAction(ref Vector4 v) { SRgbCompanding.Expand(ref v); - Vector4Utilities.Premultiply(ref v); + Vector4Utils.Premultiply(ref v); } TPixel[] source = CreateScaledPixelTestData(count, (ref Vector4 v) => SourceAction(ref v));