Skip to content

Commit 0e3eda9

Browse files
committed
Add tests with and without avx
1 parent 8806d6b commit 0e3eda9

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -587,19 +587,17 @@ private static byte NearLosslessComponent(byte value, byte predict, byte boundar
587587

588588
return (byte)lower;
589589
}
590-
else
591-
{
592-
// upper is closer to residual than lower.
593-
if (residual <= boundaryResidual && upper > boundaryResidual)
594-
{
595-
// Halve quantization step to avoid crossing boundary. This midpoint is
596-
// on the same side of boundary as residual because midpoint <= residual
597-
// (since upper is closer than lower) and residual is below the boundary.
598-
return (byte)(lower + (quantization >> 1));
599-
}
600590

601-
return (byte)(upper & 0xff);
591+
// upper is closer to residual than lower.
592+
if (residual <= boundaryResidual && upper > boundaryResidual)
593+
{
594+
// Halve quantization step to avoid crossing boundary. This midpoint is
595+
// on the same side of boundary as residual because midpoint <= residual
596+
// (since upper is closer than lower) and residual is below the boundary.
597+
return (byte)(lower + (quantization >> 1));
602598
}
599+
600+
return (byte)(upper & 0xff);
603601
}
604602

605603
/// <summary>
@@ -1075,7 +1073,7 @@ private static double GetPredictionCostCrossColorBlue(
10751073
private static void CollectColorRedTransforms(Span<uint> bgra, int stride, int tileWidth, int tileHeight, int greenToRed, Span<int> histo)
10761074
{
10771075
#if SUPPORTS_RUNTIME_INTRINSICS
1078-
if (Avx2.IsSupported && tileWidth > 16)
1076+
if (Avx2.IsSupported && tileWidth >= 16)
10791077
{
10801078
var multsg = Vector256.Create(LosslessUtils.Cst5b(greenToRed));
10811079
const int span = 16;
@@ -1182,7 +1180,7 @@ private static void CollectColorRedTransformsNoneVectorized(Span<uint> bgra, int
11821180
private static void CollectColorBlueTransforms(Span<uint> bgra, int stride, int tileWidth, int tileHeight, int greenToBlue, int redToBlue, Span<int> histo)
11831181
{
11841182
#if SUPPORTS_RUNTIME_INTRINSICS
1185-
if (Avx2.IsSupported && tileWidth > 16)
1183+
if (Avx2.IsSupported && tileWidth >= 16)
11861184
{
11871185
const int span = 16;
11881186
Span<ushort> values = stackalloc ushort[span];
@@ -1219,12 +1217,12 @@ private static void CollectColorBlueTransforms(Span<uint> bgra, int stride, int
12191217
++histo[values[i]];
12201218
}
12211219
}
1220+
}
12221221

1223-
int leftOver = tileWidth & (span - 1);
1224-
if (leftOver > 0)
1225-
{
1226-
CollectColorBlueTransformsNoneVectorized(bgra.Slice(tileWidth - leftOver), stride, leftOver, tileHeight, greenToBlue, redToBlue, histo);
1227-
}
1222+
int leftOver = tileWidth & (span - 1);
1223+
if (leftOver > 0)
1224+
{
1225+
CollectColorBlueTransformsNoneVectorized(bgra.Slice(tileWidth - leftOver), stride, leftOver, tileHeight, greenToBlue, redToBlue, histo);
12281226
}
12291227
}
12301228
else if (Sse41.IsSupported)

tests/ImageSharp.Tests/Formats/WebP/PredictorEncoderTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,13 @@ public void ColorSpaceTransform_WithBikeImage_WithHardwareIntrinsics_Works()
4040
[Fact]
4141
public void ColorSpaceTransform_WithBikeImage_WithoutSSE41_Works()
4242
=> FeatureTestRunner.RunWithHwIntrinsicsFeature(ColorSpaceTransform_WithBikeImage_ProducesExpectedData, HwIntrinsics.DisableSSE41);
43+
44+
[Fact]
45+
public void ColorSpaceTransform_WithBikeImage_WithoutAvx2_Works()
46+
=> FeatureTestRunner.RunWithHwIntrinsicsFeature(ColorSpaceTransform_WithBikeImage_ProducesExpectedData, HwIntrinsics.DisableAVX2);
4347
#endif
4448

49+
// Test image: Input\Webp\peak.png
4550
private static void RunColorSpaceTransformTestWithPeakImage()
4651
{
4752
// arrange
@@ -99,6 +104,7 @@ private static void RunColorSpaceTransformTestWithPeakImage()
99104
Assert.Equal(expectedData, transformData);
100105
}
101106

107+
// Test image: Input\Png\Bike.png
102108
private static void RunColorSpaceTransformTestWithBikeImage()
103109
{
104110
// arrange

0 commit comments

Comments
 (0)