From 0e3eda9840bae44b07eeb3b22f4e8696f01c3f98 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Fri, 12 Nov 2021 15:33:02 +0100 Subject: [PATCH] Add tests with and without avx --- .../Formats/Webp/Lossless/PredictorEncoder.cs | 34 +++++++++---------- .../Formats/WebP/PredictorEncoderTests.cs | 6 ++++ 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs b/src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs index 48c02f0d3..e40045c46 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs @@ -587,19 +587,17 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless return (byte)lower; } - else - { - // upper is closer to residual than lower. - if (residual <= boundaryResidual && upper > boundaryResidual) - { - // Halve quantization step to avoid crossing boundary. This midpoint is - // on the same side of boundary as residual because midpoint <= residual - // (since upper is closer than lower) and residual is below the boundary. - return (byte)(lower + (quantization >> 1)); - } - return (byte)(upper & 0xff); + // upper is closer to residual than lower. + if (residual <= boundaryResidual && upper > boundaryResidual) + { + // Halve quantization step to avoid crossing boundary. This midpoint is + // on the same side of boundary as residual because midpoint <= residual + // (since upper is closer than lower) and residual is below the boundary. + return (byte)(lower + (quantization >> 1)); } + + return (byte)(upper & 0xff); } /// @@ -1075,7 +1073,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless private static void CollectColorRedTransforms(Span bgra, int stride, int tileWidth, int tileHeight, int greenToRed, Span histo) { #if SUPPORTS_RUNTIME_INTRINSICS - if (Avx2.IsSupported && tileWidth > 16) + if (Avx2.IsSupported && tileWidth >= 16) { var multsg = Vector256.Create(LosslessUtils.Cst5b(greenToRed)); const int span = 16; @@ -1182,7 +1180,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless private static void CollectColorBlueTransforms(Span bgra, int stride, int tileWidth, int tileHeight, int greenToBlue, int redToBlue, Span histo) { #if SUPPORTS_RUNTIME_INTRINSICS - if (Avx2.IsSupported && tileWidth > 16) + if (Avx2.IsSupported && tileWidth >= 16) { const int span = 16; Span values = stackalloc ushort[span]; @@ -1219,12 +1217,12 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless ++histo[values[i]]; } } + } - int leftOver = tileWidth & (span - 1); - if (leftOver > 0) - { - CollectColorBlueTransformsNoneVectorized(bgra.Slice(tileWidth - leftOver), stride, leftOver, tileHeight, greenToBlue, redToBlue, histo); - } + int leftOver = tileWidth & (span - 1); + if (leftOver > 0) + { + CollectColorBlueTransformsNoneVectorized(bgra.Slice(tileWidth - leftOver), stride, leftOver, tileHeight, greenToBlue, redToBlue, histo); } } else if (Sse41.IsSupported) diff --git a/tests/ImageSharp.Tests/Formats/WebP/PredictorEncoderTests.cs b/tests/ImageSharp.Tests/Formats/WebP/PredictorEncoderTests.cs index d78f7e2f2..98c144a90 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/PredictorEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/PredictorEncoderTests.cs @@ -40,8 +40,13 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp [Fact] public void ColorSpaceTransform_WithBikeImage_WithoutSSE41_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(ColorSpaceTransform_WithBikeImage_ProducesExpectedData, HwIntrinsics.DisableSSE41); + + [Fact] + public void ColorSpaceTransform_WithBikeImage_WithoutAvx2_Works() + => FeatureTestRunner.RunWithHwIntrinsicsFeature(ColorSpaceTransform_WithBikeImage_ProducesExpectedData, HwIntrinsics.DisableAVX2); #endif + // Test image: Input\Webp\peak.png private static void RunColorSpaceTransformTestWithPeakImage() { // arrange @@ -99,6 +104,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp Assert.Equal(expectedData, transformData); } + // Test image: Input\Png\Bike.png private static void RunColorSpaceTransformTestWithBikeImage() { // arrange