From 8199e0611b0ba09c1f15a751dc579daba0de48d4 Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Sun, 26 Mar 2023 11:17:54 +0200 Subject: [PATCH 01/11] Fix RgbScalar --- .../Jpeg/Components/ColorConverters/JpegColorConverterBase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverterBase.cs b/src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverterBase.cs index 58b0b9b1b2..a0850c304e 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverterBase.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverterBase.cs @@ -229,7 +229,7 @@ private static JpegColorConverterBase GetRgbConverter(int precision) return new RgbScalar(precision); } - return new GrayscaleScalar(precision); + return new RgbScalar(precision); } /// From b9e54a16a3cf7a9bfdcc61f77fbcc84ed77af73d Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Sun, 26 Mar 2023 11:21:31 +0200 Subject: [PATCH 02/11] Return Vector not Scalar --- .../Jpeg/Components/ColorConverters/JpegColorConverterBase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverterBase.cs b/src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverterBase.cs index a0850c304e..5171c4986e 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverterBase.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverterBase.cs @@ -226,7 +226,7 @@ private static JpegColorConverterBase GetRgbConverter(int precision) if (JpegColorConverterVector.IsSupported) { - return new RgbScalar(precision); + return new RgbVector(precision); } return new RgbScalar(precision); From 09b4dd751d9383156ea7e7671e90ab59c3ea618f Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 26 Mar 2023 19:47:27 +0200 Subject: [PATCH 03/11] Add test for GetConverter with rgb color space which uses FeatureTestRunner to disable specific hw features --- .../Formats/Jpg/JpegColorConverterTests.cs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs index 44675aaea2..eb7179b89b 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using System.Runtime.Intrinsics.Arm; +using System.Runtime.Intrinsics.X86; using SixLabors.ImageSharp.ColorSpaces; using SixLabors.ImageSharp.ColorSpaces.Conversion; using SixLabors.ImageSharp.Formats.Jpeg.Components; @@ -69,6 +71,39 @@ internal void GetConverterReturnsValidConverter(JpegColorSpace colorSpace, int p Assert.Equal(precision, converter.Precision); } + [Fact] + public void GetConverterReturnsValidConverterWithRgbColorSpace() + { + FeatureTestRunner.RunWithHwIntrinsicsFeature( + RunTest, + HwIntrinsics.AllowAll | HwIntrinsics.DisableAVX2 | HwIntrinsics.DisableSSE2 | HwIntrinsics.DisableHWIntrinsic); + + static void RunTest(string arg) + { + // arrange + Type expectedType = typeof(JpegColorConverterBase.RgbScalar); + if (Avx.IsSupported) + { + expectedType = typeof(JpegColorConverterBase.RgbAvx); + } + else if (Sse2.IsSupported) + { + expectedType = typeof(JpegColorConverterBase.RgbVector); + } + else if (AdvSimd.IsSupported) + { + expectedType = typeof(JpegColorConverterBase.RgbArm); + } + + // act + JpegColorConverterBase converter = JpegColorConverterBase.GetConverter(JpegColorSpace.RGB, 8); + Type actualType = converter.GetType(); + + // assert + Assert.Equal(expectedType, actualType); + } + } + [Theory] [InlineData(JpegColorSpace.Grayscale, 1)] [InlineData(JpegColorSpace.Ycck, 4)] From e62b62f5be39e631385eb55749112c4cda752480 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 26 Mar 2023 19:50:25 +0200 Subject: [PATCH 04/11] Fix formatting warnings --- .../Formats/Jpg/JpegColorConverterTests.cs | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs index eb7179b89b..93c246e7cf 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs @@ -277,7 +277,8 @@ static void RunTest(string arg) => [Theory] [MemberData(nameof(Seeds))] public void FromYCbCrAvx2(int seed) => - this.TestConversionToRgb(new JpegColorConverterBase.YCbCrAvx(8), + this.TestConversionToRgb( + new JpegColorConverterBase.YCbCrAvx(8), 3, seed, new JpegColorConverterBase.YCbCrScalar(8)); @@ -285,7 +286,8 @@ public void FromYCbCrAvx2(int seed) => [Theory] [MemberData(nameof(Seeds))] public void FromRgbToYCbCrAvx2(int seed) => - this.TestConversionFromRgb(new JpegColorConverterBase.YCbCrAvx(8), + this.TestConversionFromRgb( + new JpegColorConverterBase.YCbCrAvx(8), 3, seed, new JpegColorConverterBase.YCbCrScalar(8), @@ -294,7 +296,8 @@ public void FromRgbToYCbCrAvx2(int seed) => [Theory] [MemberData(nameof(Seeds))] public void FromCmykAvx2(int seed) => - this.TestConversionToRgb(new JpegColorConverterBase.CmykAvx(8), + this.TestConversionToRgb( + new JpegColorConverterBase.CmykAvx(8), 4, seed, new JpegColorConverterBase.CmykScalar(8)); @@ -302,7 +305,8 @@ public void FromCmykAvx2(int seed) => [Theory] [MemberData(nameof(Seeds))] public void FromRgbToCmykAvx2(int seed) => - this.TestConversionFromRgb(new JpegColorConverterBase.CmykAvx(8), + this.TestConversionFromRgb( + new JpegColorConverterBase.CmykAvx(8), 4, seed, new JpegColorConverterBase.CmykScalar(8), @@ -311,7 +315,8 @@ public void FromRgbToCmykAvx2(int seed) => [Theory] [MemberData(nameof(Seeds))] public void FromCmykArm(int seed) => - this.TestConversionToRgb( new JpegColorConverterBase.CmykArm64(8), + this.TestConversionToRgb( + new JpegColorConverterBase.CmykArm64(8), 4, seed, new JpegColorConverterBase.CmykScalar(8)); @@ -319,7 +324,8 @@ public void FromCmykArm(int seed) => [Theory] [MemberData(nameof(Seeds))] public void FromRgbToCmykArm(int seed) => - this.TestConversionFromRgb(new JpegColorConverterBase.CmykArm64(8), + this.TestConversionFromRgb( + new JpegColorConverterBase.CmykArm64(8), 4, seed, new JpegColorConverterBase.CmykScalar(8), @@ -328,7 +334,8 @@ public void FromRgbToCmykArm(int seed) => [Theory] [MemberData(nameof(Seeds))] public void FromGrayscaleAvx2(int seed) => - this.TestConversionToRgb(new JpegColorConverterBase.GrayscaleAvx(8), + this.TestConversionToRgb( + new JpegColorConverterBase.GrayscaleAvx(8), 1, seed, new JpegColorConverterBase.GrayscaleScalar(8)); @@ -336,7 +343,8 @@ public void FromGrayscaleAvx2(int seed) => [Theory] [MemberData(nameof(Seeds))] public void FromRgbToGrayscaleAvx2(int seed) => - this.TestConversionFromRgb(new JpegColorConverterBase.GrayscaleAvx(8), + this.TestConversionFromRgb( + new JpegColorConverterBase.GrayscaleAvx(8), 1, seed, new JpegColorConverterBase.GrayscaleScalar(8), @@ -345,7 +353,8 @@ public void FromRgbToGrayscaleAvx2(int seed) => [Theory] [MemberData(nameof(Seeds))] public void FromRgbAvx2(int seed) => - this.TestConversionToRgb(new JpegColorConverterBase.RgbAvx(8), + this.TestConversionToRgb( + new JpegColorConverterBase.RgbAvx(8), 3, seed, new JpegColorConverterBase.RgbScalar(8)); @@ -353,7 +362,8 @@ public void FromRgbAvx2(int seed) => [Theory] [MemberData(nameof(Seeds))] public void FromRgbArm(int seed) => - this.TestConversionToRgb(new JpegColorConverterBase.RgbArm(8), + this.TestConversionToRgb( + new JpegColorConverterBase.RgbArm(8), 3, seed, new JpegColorConverterBase.RgbScalar(8)); @@ -361,7 +371,8 @@ public void FromRgbArm(int seed) => [Theory] [MemberData(nameof(Seeds))] public void FromYccKAvx2(int seed) => - this.TestConversionToRgb( new JpegColorConverterBase.YccKAvx(8), + this.TestConversionToRgb( + new JpegColorConverterBase.YccKAvx(8), 4, seed, new JpegColorConverterBase.YccKScalar(8)); @@ -369,7 +380,8 @@ public void FromYccKAvx2(int seed) => [Theory] [MemberData(nameof(Seeds))] public void FromRgbToYccKAvx2(int seed) => - this.TestConversionFromRgb(new JpegColorConverterBase.YccKAvx(8), + this.TestConversionFromRgb( + new JpegColorConverterBase.YccKAvx(8), 4, seed, new JpegColorConverterBase.YccKScalar(8), @@ -515,7 +527,7 @@ private static void ValidateConversionFromRgb( JpegColorConverterBase baseLineConverter, int precision = 4) { - // arrange + // arrange JpegColorConverterBase.ComponentValues actual = CreateRandomValues(TestBufferLength, componentCount, seed); JpegColorConverterBase.ComponentValues expected = CreateRandomValues(TestBufferLength, componentCount, seed); Random rnd = new(seed); From f4727d1d3480d2b01d0619051ec5ada0959dbb71 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Mon, 27 Mar 2023 18:13:52 +0200 Subject: [PATCH 05/11] Add test for GetConverter with cmyk color space which uses FeatureTestRunner to disable specific hw features --- .../Formats/Jpg/JpegColorConverterTests.cs | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs index 93c246e7cf..bb6a67403e 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs @@ -72,7 +72,7 @@ internal void GetConverterReturnsValidConverter(JpegColorSpace colorSpace, int p } [Fact] - public void GetConverterReturnsValidConverterWithRgbColorSpace() + public void GetConverterReturnsCorrectConverterWithRgbColorSpace() { FeatureTestRunner.RunWithHwIntrinsicsFeature( RunTest, @@ -104,6 +104,39 @@ static void RunTest(string arg) } } + [Fact] + public void GetConverterReturnsCorrectConverterWithCmykColorSpace() + { + FeatureTestRunner.RunWithHwIntrinsicsFeature( + RunTest, + HwIntrinsics.AllowAll | HwIntrinsics.DisableAVX2 | HwIntrinsics.DisableSSE2 | HwIntrinsics.DisableHWIntrinsic); + + static void RunTest(string arg) + { + // arrange + Type expectedType = typeof(JpegColorConverterBase.CmykScalar); + if (Avx.IsSupported) + { + expectedType = typeof(JpegColorConverterBase.CmykAvx); + } + else if (Sse2.IsSupported) + { + expectedType = typeof(JpegColorConverterBase.CmykVector); + } + else if (AdvSimd.IsSupported) + { + expectedType = typeof(JpegColorConverterBase.CmykArm64); + } + + // act + JpegColorConverterBase converter = JpegColorConverterBase.GetConverter(JpegColorSpace.Cmyk, 8); + Type actualType = converter.GetType(); + + // assert + Assert.Equal(expectedType, actualType); + } + } + [Theory] [InlineData(JpegColorSpace.Grayscale, 1)] [InlineData(JpegColorSpace.Ycck, 4)] From ecb6c0f823d0cd27281c1bf84c63022744123f6a Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Mon, 27 Mar 2023 18:18:19 +0200 Subject: [PATCH 06/11] Add test for GetConverter with ycbcr color space which uses FeatureTestRunner to disable specific hw features --- .../Formats/Jpg/JpegColorConverterTests.cs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs index bb6a67403e..dcb56e5714 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs @@ -137,6 +137,35 @@ static void RunTest(string arg) } } + [Fact] + public void GetConverterReturnsCorrectConverterWithYCbCrColorSpace() + { + FeatureTestRunner.RunWithHwIntrinsicsFeature( + RunTest, + HwIntrinsics.AllowAll | HwIntrinsics.DisableAVX2 | HwIntrinsics.DisableSSE2 | HwIntrinsics.DisableHWIntrinsic); + + static void RunTest(string arg) + { + // arrange + Type expectedType = typeof(JpegColorConverterBase.YCbCrScalar); + if (Avx.IsSupported) + { + expectedType = typeof(JpegColorConverterBase.YCbCrAvx); + } + else if (Sse2.IsSupported) + { + expectedType = typeof(JpegColorConverterBase.YCbCrVector); + } + + // act + JpegColorConverterBase converter = JpegColorConverterBase.GetConverter(JpegColorSpace.YCbCr, 8); + Type actualType = converter.GetType(); + + // assert + Assert.Equal(expectedType, actualType); + } + } + [Theory] [InlineData(JpegColorSpace.Grayscale, 1)] [InlineData(JpegColorSpace.Ycck, 4)] From f0530bf56a404bbe3be6a20b4748793b078d543c Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Mon, 27 Mar 2023 18:22:00 +0200 Subject: [PATCH 07/11] Add test for GetConverter with ycck color space which uses FeatureTestRunner to disable specific hw features --- .../Formats/Jpg/JpegColorConverterTests.cs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs index dcb56e5714..8ede71a5c7 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs @@ -166,6 +166,35 @@ static void RunTest(string arg) } } + [Fact] + public void GetConverterReturnsCorrectConverterWithYcckColorSpace() + { + FeatureTestRunner.RunWithHwIntrinsicsFeature( + RunTest, + HwIntrinsics.AllowAll | HwIntrinsics.DisableAVX2 | HwIntrinsics.DisableSSE2 | HwIntrinsics.DisableHWIntrinsic); + + static void RunTest(string arg) + { + // arrange + Type expectedType = typeof(JpegColorConverterBase.YccKScalar); + if (Avx.IsSupported) + { + expectedType = typeof(JpegColorConverterBase.YccKAvx); + } + else if (Sse2.IsSupported) + { + expectedType = typeof(JpegColorConverterBase.YccKVector); + } + + // act + JpegColorConverterBase converter = JpegColorConverterBase.GetConverter(JpegColorSpace.Ycck, 8); + Type actualType = converter.GetType(); + + // assert + Assert.Equal(expectedType, actualType); + } + } + [Theory] [InlineData(JpegColorSpace.Grayscale, 1)] [InlineData(JpegColorSpace.Ycck, 4)] From 42918ed6158305e75e8cf14646abefed844aa9f8 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Mon, 27 Mar 2023 18:25:27 +0200 Subject: [PATCH 08/11] Add test for GetConverter with grayscale color space which uses FeatureTestRunner to disable specific hw features --- .../Formats/Jpg/JpegColorConverterTests.cs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs index 8ede71a5c7..2eb988e751 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs @@ -104,6 +104,35 @@ static void RunTest(string arg) } } + [Fact] + public void GetConverterReturnsCorrectConverterWithGrayScaleColorSpace() + { + FeatureTestRunner.RunWithHwIntrinsicsFeature( + RunTest, + HwIntrinsics.AllowAll | HwIntrinsics.DisableAVX2 | HwIntrinsics.DisableSSE2 | HwIntrinsics.DisableHWIntrinsic); + + static void RunTest(string arg) + { + // arrange + Type expectedType = typeof(JpegColorConverterBase.GrayscaleScalar); + if (Avx.IsSupported) + { + expectedType = typeof(JpegColorConverterBase.GrayscaleAvx); + } + else if (Sse2.IsSupported) + { + expectedType = typeof(JpegColorConverterBase.GrayScaleVector); + } + + // act + JpegColorConverterBase converter = JpegColorConverterBase.GetConverter(JpegColorSpace.Grayscale, 8); + Type actualType = converter.GetType(); + + // assert + Assert.Equal(expectedType, actualType); + } + } + [Fact] public void GetConverterReturnsCorrectConverterWithCmykColorSpace() { From 2f6edd76fa672a8c259c2da89d74169b94b0e319 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Mon, 27 Mar 2023 18:27:32 +0200 Subject: [PATCH 09/11] Add GrayscaleArm case --- tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs index 72f89394d8..41623ebf64 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs @@ -123,6 +123,10 @@ static void RunTest(string arg) { expectedType = typeof(JpegColorConverterBase.GrayScaleVector); } + else if (AdvSimd.IsSupported) + { + expectedType = typeof(JpegColorConverterBase.GrayscaleArm); + } // act JpegColorConverterBase converter = JpegColorConverterBase.GetConverter(JpegColorSpace.Grayscale, 8); From dae030d8b85a275ee75762493b59194ad1320462 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Mon, 27 Mar 2023 18:49:59 +0200 Subject: [PATCH 10/11] Expect YCbCrVector on ARM --- tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs index 41623ebf64..67602295c0 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs @@ -189,6 +189,10 @@ static void RunTest(string arg) { expectedType = typeof(JpegColorConverterBase.YCbCrVector); } + else if (AdvSimd.IsSupported) + { + expectedType = typeof(JpegColorConverterBase.YCbCrVector); + } // act JpegColorConverterBase converter = JpegColorConverterBase.GetConverter(JpegColorSpace.YCbCr, 8); From 52861d87d0c571652d6e8c008d6d783f1ce94660 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Mon, 27 Mar 2023 19:27:24 +0200 Subject: [PATCH 11/11] Another case for arm where YccKVector should be expected --- tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs index 67602295c0..bdb175f6f1 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs @@ -222,6 +222,10 @@ static void RunTest(string arg) { expectedType = typeof(JpegColorConverterBase.YccKVector); } + else if (AdvSimd.IsSupported) + { + expectedType = typeof(JpegColorConverterBase.YccKVector); + } // act JpegColorConverterBase converter = JpegColorConverterBase.GetConverter(JpegColorSpace.Ycck, 8);