From 14b39fd6205d7a4f6e7a1ef28dac5d832d29f26a Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sun, 30 Apr 2017 11:43:44 +0100 Subject: [PATCH] bulk blending tests --- .../PorterDuffFunctionsTests_TPixel.cs | 185 +++++++++++++++++- .../TestUtilities/TestPixel.cs | 7 +- 2 files changed, 187 insertions(+), 5 deletions(-) diff --git a/tests/ImageSharp.Tests/PixelFormats/PixelBlenders/PorterDuffFunctionsTests_TPixel.cs b/tests/ImageSharp.Tests/PixelFormats/PixelBlenders/PorterDuffFunctionsTests_TPixel.cs index 168269e514..1f5971424c 100644 --- a/tests/ImageSharp.Tests/PixelFormats/PixelBlenders/PorterDuffFunctionsTests_TPixel.cs +++ b/tests/ImageSharp.Tests/PixelFormats/PixelBlenders/PorterDuffFunctionsTests_TPixel.cs @@ -16,6 +16,12 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public class PorterDuffFunctionsTests_TPixel { + private static BufferSpan AsSpan(T value) + where T : struct + { + return new BufferSpan(new[] { value }); + } + public static TheoryData NormalBlendFunctionData = new TheoryData() { { new TestPixel(1,1,1,1), new TestPixel(1,1,1,1), 1, new TestPixel(1,1,1,1) }, { new TestPixel(1,1,1,1), new TestPixel(0,0,0,.8f), .5f, new TestPixel(0.6f, 0.6f, 0.6f, 1) }, @@ -27,7 +33,26 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders where TPixel : struct, IPixel { TPixel actual = PorterDuffFunctions.NormalBlendFunction(back, source, amount); - VectorAssert.Equal(expected, actual, 3); + VectorAssert.Equal(expected, actual, 2); + } + + [Theory] + [MemberData(nameof(NormalBlendFunctionData))] + public void NormalBlendFunction_Blender(TestPixel back, TestPixel source, float amount, TestPixel expected) + where TPixel : struct, IPixel + { + TPixel actual = new DefaultNormalPixelBlender().Compose(back, source, amount); + VectorAssert.Equal(expected, actual, 2); + } + + [Theory] + [MemberData(nameof(NormalBlendFunctionData))] + public void NormalBlendFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) + where TPixel : struct, IPixel + { + BufferSpan dest = new BufferSpan(new TPixel[1]); + new DefaultNormalPixelBlender().Compose(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); + VectorAssert.Equal(expected, dest[0], 2); } public static TheoryData MultiplyFunctionData = new TheoryData() { @@ -50,14 +75,33 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders VectorAssert.Equal(expected, actual, 2); } + [Theory] + [MemberData(nameof(MultiplyFunctionData))] + public void MultiplyFunction_Blender(TestPixel back, TestPixel source, float amount, TestPixel expected) + where TPixel : struct, IPixel + { + TPixel actual = new DefaultMultiplyPixelBlender().Compose(back, source, amount); + VectorAssert.Equal(expected, actual, 2); + } + + [Theory] + [MemberData(nameof(MultiplyFunctionData))] + public void MultiplyFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) + where TPixel : struct, IPixel + { + BufferSpan dest = new BufferSpan(new TPixel[1]); + new DefaultMultiplyPixelBlender().Compose(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); + VectorAssert.Equal(expected, dest[0], 2); + } + public static TheoryData AddFunctionData = new TheoryData() { { new TestPixel(1,1,1,1), new TestPixel(1,1,1,1), 1, new TestPixel(1,1,1,1) }, - { new TestPixel(1,1,1,1), new TestPixel(0,0,0,.8f), .5f, new TestPixel(.6f, .6f, .6f, 1f) }, + { new TestPixel(1,1,1,1), new TestPixel(0,0,0,.8f), .5f, new TestPixel(1f, 1f, 1f, 1f) }, { new TestPixel(0.2f,0.2f,0.2f,0.3f), new TestPixel(0.3f,0.3f,0.3f,0.2f), .5f, - new TestPixel(.2075676f, .2075676f, .2075676f, .37f) + new TestPixel(.2431373f, .2431373f, .2431373f, .372549f) }, }; @@ -66,10 +110,29 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public void AddFunction(TestPixel back, TestPixel source, float amount, TestPixel expected) where TPixel : struct, IPixel { - TPixel actual = PorterDuffFunctions.MultiplyFunction(back, source, amount); + TPixel actual = PorterDuffFunctions.AddFunction(back, source, amount); VectorAssert.Equal(expected, actual, 2); } + [Theory] + [MemberData(nameof(AddFunctionData))] + public void AddFunction_Blender(TestPixel back, TestPixel source, float amount, TestPixel expected) + where TPixel : struct, IPixel + { + TPixel actual = new DefaultAddPixelBlender().Compose(back, source, amount); + VectorAssert.Equal(expected, actual, 2); + } + + [Theory] + [MemberData(nameof(AddFunctionData))] + public void AddFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) + where TPixel : struct, IPixel + { + BufferSpan dest = new BufferSpan(new TPixel[1]); + new DefaultAddPixelBlender().Compose(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); + VectorAssert.Equal(expected, dest[0], 2); + } + public static TheoryData SubstractFunctionData = new TheoryData() { { new TestPixel(1,1,1,1), new TestPixel(1,1,1,1), 1, new TestPixel(0,0,0,1) }, { new TestPixel(1,1,1,1), new TestPixel(0,0,0,.8f), .5f, new TestPixel(1,1,1, 1f) }, @@ -90,6 +153,25 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders VectorAssert.Equal(expected, actual, 2); } + [Theory] + [MemberData(nameof(SubstractFunctionData))] + public void SubstractFunction_Blender(TestPixel back, TestPixel source, float amount, TestPixel expected) + where TPixel : struct, IPixel + { + TPixel actual = new DefaultSubstractPixelBlender().Compose(back, source, amount); + VectorAssert.Equal(expected, actual, 2); + } + + [Theory] + [MemberData(nameof(SubstractFunctionData))] + public void SubstractFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) + where TPixel : struct, IPixel + { + BufferSpan dest = new BufferSpan(new TPixel[1]); + new DefaultSubstractPixelBlender().Compose(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); + VectorAssert.Equal(expected, dest[0], 2); + } + public static TheoryData ScreenFunctionData = new TheoryData() { { new TestPixel(1,1,1,1), new TestPixel(1,1,1,1), 1, new TestPixel(1,1,1,1) }, { new TestPixel(1,1,1,1), new TestPixel(0,0,0,.8f), .5f, new TestPixel(1,1,1, 1f) }, @@ -110,6 +192,25 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders VectorAssert.Equal(expected, actual, 2); } + [Theory] + [MemberData(nameof(ScreenFunctionData))] + public void ScreenFunction_Blender(TestPixel back, TestPixel source, float amount, TestPixel expected) + where TPixel : struct, IPixel + { + TPixel actual = new DefaultScreenPixelBlender().Compose(back, source, amount); + VectorAssert.Equal(expected, actual, 2); + } + + [Theory] + [MemberData(nameof(ScreenFunctionData))] + public void ScreenFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) + where TPixel : struct, IPixel + { + BufferSpan dest = new BufferSpan(new TPixel[1]); + new DefaultScreenPixelBlender().Compose(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); + VectorAssert.Equal(expected, dest[0], 2); + } + public static TheoryData DarkenFunctionData = new TheoryData() { { new TestPixel(1,1,1,1), new TestPixel(1,1,1,1), 1, new TestPixel(1,1,1,1) }, { new TestPixel(1,1,1,1), new TestPixel(0,0,0,.8f), .5f, new TestPixel(.6f,.6f,.6f, 1f) }, @@ -130,6 +231,25 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders VectorAssert.Equal(expected, actual, 2); } + [Theory] + [MemberData(nameof(DarkenFunctionData))] + public void DarkenFunction_Blender(TestPixel back, TestPixel source, float amount, TestPixel expected) + where TPixel : struct, IPixel + { + TPixel actual = new DefaultDarkenPixelBlender().Compose(back, source, amount); + VectorAssert.Equal(expected, actual, 2); + } + + [Theory] + [MemberData(nameof(DarkenFunctionData))] + public void DarkenFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) + where TPixel : struct, IPixel + { + BufferSpan dest = new BufferSpan(new TPixel[1]); + new DefaultDarkenPixelBlender().Compose(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); + VectorAssert.Equal(expected, dest[0], 2); + } + public static TheoryData LightenFunctionData = new TheoryData() { { new TestPixel(1,1,1,1), new TestPixel(1,1,1,1), 1, new TestPixel(1,1,1,1) }, { new TestPixel(1,1,1,1), new TestPixel(0,0,0,.8f), .5f, new TestPixel(1,1,1,1f) }, @@ -150,6 +270,25 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders VectorAssert.Equal(expected, actual, 2); } + [Theory] + [MemberData(nameof(LightenFunctionData))] + public void LightenFunction_Blender(TestPixel back, TestPixel source, float amount, TestPixel expected) + where TPixel : struct, IPixel + { + TPixel actual = new DefaultLightenPixelBlender().Compose(back, source, amount); + VectorAssert.Equal(expected, actual, 2); + } + + [Theory] + [MemberData(nameof(LightenFunctionData))] + public void LightenFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) + where TPixel : struct, IPixel + { + BufferSpan dest = new BufferSpan(new TPixel[1]); + new DefaultLightenPixelBlender().Compose(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); + VectorAssert.Equal(expected, dest[0], 2); + } + public static TheoryData OverlayFunctionData = new TheoryData() { { new TestPixel(1,1,1,1), new TestPixel(1,1,1,1), 1, new TestPixel(1,1,1,1) }, { new TestPixel(1,1,1,1), new TestPixel(0,0,0,.8f), .5f, new TestPixel(1,1,1,1f) }, @@ -170,6 +309,25 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders VectorAssert.Equal(expected, actual, 2); } + [Theory] + [MemberData(nameof(OverlayFunctionData))] + public void OverlayFunction_Blender(TestPixel back, TestPixel source, float amount, TestPixel expected) + where TPixel : struct, IPixel + { + TPixel actual = new DefaultOverlayPixelBlender().Compose(back, source, amount); + VectorAssert.Equal(expected, actual, 2); + } + + [Theory] + [MemberData(nameof(OverlayFunctionData))] + public void OverlayFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) + where TPixel : struct, IPixel + { + BufferSpan dest = new BufferSpan(new TPixel[1]); + new DefaultOverlayPixelBlender().Compose(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); + VectorAssert.Equal(expected, dest[0], 2); + } + public static TheoryData HardLightFunctionData = new TheoryData() { { new TestPixel(1,1,1,1), new TestPixel(1,1,1,1), 1, new TestPixel(1,1,1,1) }, { new TestPixel(1,1,1,1), new TestPixel(0,0,0,.8f), .5f, new TestPixel(0.6f,0.6f,0.6f,1f) }, @@ -189,5 +347,24 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders TPixel actual = PorterDuffFunctions.HardLightFunction(back, source, amount); VectorAssert.Equal(expected, actual, 2); } + + [Theory] + [MemberData(nameof(HardLightFunctionData))] + public void HardLightFunction_Blender(TestPixel back, TestPixel source, float amount, TestPixel expected) + where TPixel : struct, IPixel + { + TPixel actual = new DefaultHardLightPixelBlender().Compose(back, source, amount); + VectorAssert.Equal(expected, actual, 2); + } + + [Theory] + [MemberData(nameof(HardLightFunctionData))] + public void HardLightFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) + where TPixel : struct, IPixel + { + BufferSpan dest = new BufferSpan(new TPixel[1]); + new DefaultHardLightPixelBlender().Compose(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); + VectorAssert.Equal(expected, dest[0], 2); + } } } diff --git a/tests/ImageSharp.Tests/TestUtilities/TestPixel.cs b/tests/ImageSharp.Tests/TestUtilities/TestPixel.cs index 93ccb0b142..7e3d318c0e 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestPixel.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestPixel.cs @@ -34,10 +34,15 @@ namespace ImageSharp.Tests.TestUtilities public TPixel AsPixel() { TPixel pix = default(TPixel); - pix.PackFromVector4(new System.Numerics.Vector4( this.Red, this.Green, this.Blue, this.Alpha)); + pix.PackFromVector4(new System.Numerics.Vector4(this.Red, this.Green, this.Blue, this.Alpha)); return pix; } + internal BufferSpan AsSpan() + { + return new BufferSpan(new[] { AsPixel() }); + } + public void Deserialize(IXunitSerializationInfo info) { this.Red = info.GetValue("red");