Skip to content

Commit b4ff1e4

Browse files
Fix issues
1 parent 41cfa9b commit b4ff1e4

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

src/ImageSharp/PixelFormats/PixelBlenders/PorterDuffFunctions.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ public static Vector256<float> Over(Vector256<float> destination, Vector256<floa
293293
// calculate weights
294294
Vector256<float> sW = Avx.Permute(source, ShuffleAlphaControl);
295295
Vector256<float> dW = Avx.Permute(destination, ShuffleAlphaControl);
296-
Vector256<float> blendW = Avx.Multiply(sW, dW);
297296

297+
Vector256<float> blendW = Avx.Multiply(sW, dW);
298298
Vector256<float> dstW = Avx.Subtract(dW, blendW);
299299
Vector256<float> srcW = Avx.Subtract(sW, blendW);
300300

@@ -303,8 +303,8 @@ public static Vector256<float> Over(Vector256<float> destination, Vector256<floa
303303

304304
// calculate final color
305305
Vector256<float> color = Avx.Multiply(destination, dstW);
306-
color = SimdUtils.HwIntrinsics.MultiplyAdd(source, srcW, color);
307-
color = SimdUtils.HwIntrinsics.MultiplyAdd(blend, blendW, color);
306+
color = SimdUtils.HwIntrinsics.MultiplyAdd(color, source, srcW);
307+
color = SimdUtils.HwIntrinsics.MultiplyAdd(color, blend, blendW);
308308

309309
// unpremultiply
310310
color = Avx.Divide(color, Avx.Max(alpha, Vector256.Create(Constants.Epsilon)));
@@ -349,15 +349,15 @@ public static Vector4 Atop(Vector4 destination, Vector4 source, Vector4 blend)
349349
public static Vector256<float> Atop(Vector256<float> destination, Vector256<float> source, Vector256<float> blend)
350350
{
351351
// calculate final alpha
352-
Vector256<float> alpha = Avx.Shuffle(destination, destination, ShuffleAlphaControl);
352+
Vector256<float> alpha = Avx.Permute(destination, ShuffleAlphaControl);
353353

354354
// calculate weights
355-
Vector256<float> sW = Avx.Shuffle(source, source, ShuffleAlphaControl);
355+
Vector256<float> sW = Avx.Permute(source, ShuffleAlphaControl);
356356
Vector256<float> blendW = Avx.Multiply(sW, alpha);
357357
Vector256<float> dstW = Avx.Subtract(alpha, blendW);
358358

359359
// calculate final color
360-
Vector256<float> color = SimdUtils.HwIntrinsics.MultiplyAdd(destination, dstW, Avx.Multiply(blend, blendW));
360+
Vector256<float> color = SimdUtils.HwIntrinsics.MultiplyAdd(Avx.Multiply(blend, blendW), destination, dstW);
361361

362362
// unpremultiply
363363
color = Avx.Divide(color, Avx.Max(alpha, Vector256.Create(Constants.Epsilon)));
@@ -482,8 +482,8 @@ public static Vector256<float> Xor(Vector256<float> destination, Vector256<float
482482
Vector256<float> dstW = Avx.Subtract(vOne, sW);
483483

484484
// calculate alpha
485-
Vector256<float> alpha = SimdUtils.HwIntrinsics.MultiplyAdd(sW, srcW, Avx.Multiply(dW, dstW));
486-
Vector256<float> color = SimdUtils.HwIntrinsics.MultiplyAdd(Avx.Multiply(sW, source), srcW, Avx.Multiply(Avx.Multiply(dW, destination), dstW));
485+
Vector256<float> alpha = SimdUtils.HwIntrinsics.MultiplyAdd(Avx.Multiply(dW, dstW), sW, srcW);
486+
Vector256<float> color = SimdUtils.HwIntrinsics.MultiplyAdd(Avx.Multiply(Avx.Multiply(dW, destination), dstW), Avx.Multiply(sW, source), srcW);
487487

488488
// unpremultiply
489489
color = Avx.Divide(color, Avx.Max(alpha, Vector256.Create(Constants.Epsilon)));

tests/ImageSharp.Tests/PixelFormats/PixelBlenders/PorterDuffFunctionsTests.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
// Licensed under the Six Labors Split License.
33

44
using System.Numerics;
5+
using System.Runtime.Intrinsics;
56
using SixLabors.ImageSharp.PixelFormats.PixelBlenders;
67
using SixLabors.ImageSharp.Tests.TestUtilities;
78

89
namespace SixLabors.ImageSharp.Tests.PixelFormats.PixelBlenders;
910

1011
public class PorterDuffFunctionsTests
1112
{
12-
public static TheoryData<TestVector4, TestVector4, float, TestVector4> NormalBlendFunctionData = new TheoryData<TestVector4, TestVector4, float, TestVector4>
13+
public static TheoryData<TestVector4, TestVector4, float, TestVector4> NormalBlendFunctionData { get; } = new()
1314
{
1415
{ new TestVector4(1, 1, 1, 1), new TestVector4(1, 1, 1, 1), 1, new TestVector4(1, 1, 1, 1) },
1516
{ new TestVector4(1, 1, 1, 1), new TestVector4(0, 0, 0, .8f), .5f, new TestVector4(0.6f, 0.6f, 0.6f, 1) }
@@ -23,7 +24,19 @@ public void NormalBlendFunction(TestVector4 back, TestVector4 source, float amou
2324
Assert.Equal(expected, actual);
2425
}
2526

26-
public static TheoryData<TestVector4, TestVector4, float, TestVector4> MultiplyFunctionData = new TheoryData<TestVector4, TestVector4, float, TestVector4>
27+
[Theory]
28+
[MemberData(nameof(NormalBlendFunctionData))]
29+
public void NormalBlendFunction256(TestVector4 back, TestVector4 source, float amount, TestVector4 expected)
30+
{
31+
Vector256<float> back256 = Vector256.Create(back.X, back.Y, back.Z, back.W, back.X, back.Y, back.Z, back.W);
32+
Vector256<float> source256 = Vector256.Create(source.X, source.Y, source.Z, source.W, source.X, source.Y, source.Z, source.W);
33+
34+
Vector256<float> expected256 = Vector256.Create(expected.X, expected.Y, expected.Z, expected.W, expected.X, expected.Y, expected.Z, expected.W);
35+
Vector256<float> actual = PorterDuffFunctions.NormalSrcOver(back256, source256, Vector256.Create(amount));
36+
Assert.Equal(expected256, actual);
37+
}
38+
39+
public static TheoryData<TestVector4, TestVector4, float, TestVector4> MultiplyFunctionData { get; } = new()
2740
{
2841
{ new TestVector4(1, 1, 1, 1), new TestVector4(1, 1, 1, 1), 1, new TestVector4(1, 1, 1, 1) },
2942
{ new TestVector4(1, 1, 1, 1), new TestVector4(0, 0, 0, .8f), .5f, new TestVector4(0.6f, 0.6f, 0.6f, 1) },
@@ -38,7 +51,7 @@ public void MultiplyFunction(TestVector4 back, TestVector4 source, float amount,
3851
VectorAssert.Equal(expected, actual, 5);
3952
}
4053

41-
public static TheoryData<TestVector4, TestVector4, float, TestVector4> AddFunctionData = new TheoryData<TestVector4, TestVector4, float, TestVector4>
54+
public static TheoryData<TestVector4, TestVector4, float, TestVector4> AddFunctionData { get; } = new()
4255
{
4356
{ new TestVector4(1, 1, 1, 1), new TestVector4(1, 1, 1, 1), 1, new TestVector4(1, 1, 1, 1) },
4457
{ new TestVector4(1, 1, 1, 1), new TestVector4(0, 0, 0, .8f), .5f, new TestVector4(.6f, .6f, .6f, 1f) },
@@ -53,7 +66,7 @@ public void AddFunction(TestVector4 back, TestVector4 source, float amount, Test
5366
VectorAssert.Equal(expected, actual, 5);
5467
}
5568

56-
public static TheoryData<TestVector4, TestVector4, float, TestVector4> SubtractFunctionData = new TheoryData<TestVector4, TestVector4, float, TestVector4>
69+
public static TheoryData<TestVector4, TestVector4, float, TestVector4> SubtractFunctionData { get; } = new()
5770
{
5871
{ new TestVector4(1, 1, 1, 1), new TestVector4(1, 1, 1, 1), 1, new TestVector4(0, 0, 0, 1) },
5972
{ new TestVector4(1, 1, 1, 1), new TestVector4(0, 0, 0, .8f), .5f, new TestVector4(1, 1, 1, 1f) },
@@ -68,7 +81,7 @@ public void SubtractFunction(TestVector4 back, TestVector4 source, float amount,
6881
VectorAssert.Equal(expected, actual, 5);
6982
}
7083

71-
public static TheoryData<TestVector4, TestVector4, float, TestVector4> ScreenFunctionData = new TheoryData<TestVector4, TestVector4, float, TestVector4>
84+
public static TheoryData<TestVector4, TestVector4, float, TestVector4> ScreenFunctionData { get; } = new()
7285
{
7386
{ new TestVector4(1, 1, 1, 1), new TestVector4(1, 1, 1, 1), 1, new TestVector4(1, 1, 1, 1) },
7487
{ new TestVector4(1, 1, 1, 1), new TestVector4(0, 0, 0, .8f), .5f, new TestVector4(1, 1, 1, 1f) },
@@ -83,7 +96,7 @@ public void ScreenFunction(TestVector4 back, TestVector4 source, float amount, T
8396
VectorAssert.Equal(expected, actual, 5);
8497
}
8598

86-
public static TheoryData<TestVector4, TestVector4, float, TestVector4> DarkenFunctionData = new TheoryData<TestVector4, TestVector4, float, TestVector4>
99+
public static TheoryData<TestVector4, TestVector4, float, TestVector4> DarkenFunctionData { get; } = new()
87100
{
88101
{ new TestVector4(1, 1, 1, 1), new TestVector4(1, 1, 1, 1), 1, new TestVector4(1, 1, 1, 1) },
89102
{ new TestVector4(1, 1, 1, 1), new TestVector4(0, 0, 0, .8f), .5f, new TestVector4(.6f, .6f, .6f, 1f) },
@@ -98,7 +111,7 @@ public void DarkenFunction(TestVector4 back, TestVector4 source, float amount, T
98111
VectorAssert.Equal(expected, actual, 5);
99112
}
100113

101-
public static TheoryData<TestVector4, TestVector4, float, TestVector4> LightenFunctionData = new TheoryData<TestVector4, TestVector4, float, TestVector4>
114+
public static TheoryData<TestVector4, TestVector4, float, TestVector4> LightenFunctionData { get; } = new()
102115
{
103116
{ new TestVector4(1, 1, 1, 1), new TestVector4(1, 1, 1, 1), 1, new TestVector4(1, 1, 1, 1) },
104117
{ new TestVector4(1, 1, 1, 1), new TestVector4(0, 0, 0, .8f), .5f, new TestVector4(1, 1, 1, 1f) },
@@ -113,7 +126,7 @@ public void LightenFunction(TestVector4 back, TestVector4 source, float amount,
113126
VectorAssert.Equal(expected, actual, 5);
114127
}
115128

116-
public static TheoryData<TestVector4, TestVector4, float, TestVector4> OverlayFunctionData = new TheoryData<TestVector4, TestVector4, float, TestVector4>
129+
public static TheoryData<TestVector4, TestVector4, float, TestVector4> OverlayFunctionData { get; } = new()
117130
{
118131
{ new TestVector4(1, 1, 1, 1), new TestVector4(1, 1, 1, 1), 1, new TestVector4(1, 1, 1, 1) },
119132
{ new TestVector4(1, 1, 1, 1), new TestVector4(0, 0, 0, .8f), .5f, new TestVector4(1, 1, 1, 1f) },
@@ -128,7 +141,7 @@ public void OverlayFunction(TestVector4 back, TestVector4 source, float amount,
128141
VectorAssert.Equal(expected, actual, 5);
129142
}
130143

131-
public static TheoryData<TestVector4, TestVector4, float, TestVector4> HardLightFunctionData = new TheoryData<TestVector4, TestVector4, float, TestVector4>
144+
public static TheoryData<TestVector4, TestVector4, float, TestVector4> HardLightFunctionData { get; } = new()
132145
{
133146
{ new TestVector4(1, 1, 1, 1), new TestVector4(1, 1, 1, 1), 1, new TestVector4(1, 1, 1, 1) },
134147
{ new TestVector4(1, 1, 1, 1), new TestVector4(0, 0, 0, .8f), .5f, new TestVector4(0.6f, 0.6f, 0.6f, 1f) },

0 commit comments

Comments
 (0)