From d0c0dee58229aac1211e6afe237e041319e5bbff Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sat, 18 Mar 2017 11:46:36 +0000 Subject: [PATCH] Improve test coverage - fixed bug in pattern brush and added test coverage - change DebugGuard -> Guard - Update SixLabors.Shapes to version not doing array allocation on hot path. --- .../Brushes/ImageBrush{TColor}.cs | 2 +- .../Brushes/PatternBrush{TColor}.cs | 7 ++--- .../Brushes/RecolorBrush{TColor}.cs | 2 +- .../Brushes/SolidBrush{TColor}.cs | 2 +- .../ImageSharp.Drawing.csproj | 2 +- .../Drawing/FillPolygon.cs | 26 +++++++++++++++++++ .../Drawing/SolidPolygonTests.cs | 26 +++++++++++++++++++ 7 files changed, 60 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp.Drawing/Brushes/ImageBrush{TColor}.cs b/src/ImageSharp.Drawing/Brushes/ImageBrush{TColor}.cs index 636f3a5a4..080111f61 100644 --- a/src/ImageSharp.Drawing/Brushes/ImageBrush{TColor}.cs +++ b/src/ImageSharp.Drawing/Brushes/ImageBrush{TColor}.cs @@ -115,7 +115,7 @@ namespace ImageSharp.Drawing.Brushes /// internal override void Apply(float[] scanlineBuffer, int scanlineWidth, int offset, int x, int y) { - DebugGuard.MustBeGreaterThanOrEqualTo(scanlineBuffer.Length, offset + scanlineWidth, nameof(scanlineWidth)); + Guard.MustBeGreaterThanOrEqualTo(scanlineBuffer.Length, offset + scanlineWidth, nameof(scanlineWidth)); using (PinnedBuffer buffer = new PinnedBuffer(scanlineBuffer)) { diff --git a/src/ImageSharp.Drawing/Brushes/PatternBrush{TColor}.cs b/src/ImageSharp.Drawing/Brushes/PatternBrush{TColor}.cs index c718ce1f4..2b4d3ec73 100644 --- a/src/ImageSharp.Drawing/Brushes/PatternBrush{TColor}.cs +++ b/src/ImageSharp.Drawing/Brushes/PatternBrush{TColor}.cs @@ -86,6 +86,7 @@ namespace ImageSharp.Drawing.Brushes internal PatternBrush(PatternBrush brush) { this.pattern = brush.pattern; + this.patternVector = brush.patternVector; } /// @@ -112,7 +113,7 @@ namespace ImageSharp.Drawing.Brushes /// The pattern. /// The patternVector. public PatternBrushApplicator(PixelAccessor sourcePixels, Fast2DArray pattern, Fast2DArray patternVector) - : base(sourcePixels) + : base(sourcePixels) { this.pattern = pattern; this.patternVector = patternVector; @@ -147,7 +148,7 @@ namespace ImageSharp.Drawing.Brushes /// internal override void Apply(float[] scanlineBuffer, int scanlineWidth, int offset, int x, int y) { - DebugGuard.MustBeGreaterThanOrEqualTo(scanlineBuffer.Length, offset + scanlineWidth, nameof(scanlineWidth)); + Guard.MustBeGreaterThanOrEqualTo(scanlineBuffer.Length, offset + scanlineWidth, nameof(scanlineWidth)); using (PinnedBuffer buffer = new PinnedBuffer(scanlineBuffer)) { @@ -164,7 +165,7 @@ namespace ImageSharp.Drawing.Brushes Vector4 backgroundVector = this.Target[targetX, targetY].ToVector4(); // 2d array index at row/column - Vector4 sourceVector = this.patternVector[targetY % this.pattern.Height, targetX % this.pattern.Width]; + Vector4 sourceVector = this.patternVector[targetY % this.patternVector.Height, targetX % this.patternVector.Width]; Vector4 finalColor = Vector4BlendTransforms.PremultipliedLerp(backgroundVector, sourceVector, opacity); diff --git a/src/ImageSharp.Drawing/Brushes/RecolorBrush{TColor}.cs b/src/ImageSharp.Drawing/Brushes/RecolorBrush{TColor}.cs index cdfc23b91..0c6e86643 100644 --- a/src/ImageSharp.Drawing/Brushes/RecolorBrush{TColor}.cs +++ b/src/ImageSharp.Drawing/Brushes/RecolorBrush{TColor}.cs @@ -139,7 +139,7 @@ namespace ImageSharp.Drawing.Brushes /// internal override void Apply(float[] scanlineBuffer, int scanlineWidth, int offset, int x, int y) { - DebugGuard.MustBeGreaterThanOrEqualTo(scanlineBuffer.Length, offset + scanlineWidth, nameof(scanlineWidth)); + Guard.MustBeGreaterThanOrEqualTo(scanlineBuffer.Length, offset + scanlineWidth, nameof(scanlineWidth)); using (PinnedBuffer buffer = new PinnedBuffer(scanlineBuffer)) { diff --git a/src/ImageSharp.Drawing/Brushes/SolidBrush{TColor}.cs b/src/ImageSharp.Drawing/Brushes/SolidBrush{TColor}.cs index e3413328e..e001829b0 100644 --- a/src/ImageSharp.Drawing/Brushes/SolidBrush{TColor}.cs +++ b/src/ImageSharp.Drawing/Brushes/SolidBrush{TColor}.cs @@ -87,7 +87,7 @@ namespace ImageSharp.Drawing.Brushes /// internal override void Apply(float[] scanlineBuffer, int scanlineWidth, int offset, int x, int y) { - DebugGuard.MustBeGreaterThanOrEqualTo(scanlineBuffer.Length, offset + scanlineWidth, nameof(scanlineWidth)); + Guard.MustBeGreaterThanOrEqualTo(scanlineBuffer.Length, offset + scanlineWidth, nameof(scanlineWidth)); using (PinnedBuffer buffer = new PinnedBuffer(scanlineBuffer)) { diff --git a/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj b/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj index f690dac8d..e15bfe74b 100644 --- a/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj +++ b/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj @@ -40,7 +40,7 @@ All - + ..\..\ImageSharp.ruleset diff --git a/tests/ImageSharp.Benchmarks/Drawing/FillPolygon.cs b/tests/ImageSharp.Benchmarks/Drawing/FillPolygon.cs index 1eafbe077..782306deb 100644 --- a/tests/ImageSharp.Benchmarks/Drawing/FillPolygon.cs +++ b/tests/ImageSharp.Benchmarks/Drawing/FillPolygon.cs @@ -9,6 +9,7 @@ namespace ImageSharp.Benchmarks using System.Drawing.Drawing2D; using System.IO; using System.Numerics; + using SixLabors.Shapes; using BenchmarkDotNet.Attributes; @@ -17,6 +18,15 @@ namespace ImageSharp.Benchmarks public class FillPolygon : BenchmarkBase { + private readonly Polygon shape; + + public FillPolygon() + { + this.shape = new SixLabors.Shapes.Polygon(new LinearLineSegment(new Vector2(10, 10), + new Vector2(550, 50), + new Vector2(200, 400))); + } + [Benchmark(Baseline = true, Description = "System.Drawing Fill Polygon")] public void DrawSolidPolygonSystemDrawing() { @@ -60,5 +70,21 @@ namespace ImageSharp.Benchmarks } } } + + [Benchmark(Description = "ImageSharp Fill Polygon - cached shape")] + public void DrawSolidPolygonCoreCahced() + { + using (CoreImage image = new CoreImage(800, 800)) + { + image.Fill( + CoreColor.HotPink, + this.shape); + + using (MemoryStream ms = new MemoryStream()) + { + image.SaveAsBmp(ms); + } + } + } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs b/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs index 6f9b31e36..79363480f 100644 --- a/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs +++ b/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs @@ -43,6 +43,32 @@ namespace ImageSharp.Tests.Drawing } } + [Fact] + public void ImageShouldBeOverlayedByFilledPolygonWithPattern() + { + string path = this.CreateOutputDirectory("Drawing", "FilledPolygons"); + Vector2[] simplePath = new[] { + new Vector2(10, 10), + new Vector2(200, 150), + new Vector2(50, 300) + }; + + using (Image image = new Image(500, 500)) + { + using (FileStream output = File.OpenWrite($"{path}/Pattern.png")) + { + image + .FillPolygon(Brushes.Horizontal(Color.HotPink), simplePath, new GraphicsOptions(true)) + .Save(output); + } + + using (PixelAccessor sourcePixels = image.Lock()) + { + Assert.Equal(Color.HotPink, sourcePixels[81, 145]); + } + } + } + [Fact] public void ImageShouldBeOverlayedByFilledPolygonNoAntialias() {