diff --git a/src/ImageSharp/Drawing/Brushes/PatternBrush.cs b/src/ImageSharp/Drawing/Brushes/PatternBrush.cs
index 535dd2eb1..31a04fbf9 100644
--- a/src/ImageSharp/Drawing/Brushes/PatternBrush.cs
+++ b/src/ImageSharp/Drawing/Brushes/PatternBrush.cs
@@ -14,7 +14,7 @@ namespace ImageSharp.Drawing.Brushes
///
/// Provides an implementaion of a pattern brush for painting patterns.
///
- public partial class PatternBrush : PatternBrush
+ public class PatternBrush : PatternBrush
{
///
/// Initializes a new instance of the class.
@@ -48,7 +48,8 @@ namespace ImageSharp.Drawing.Brushes
{
private readonly TColor foreColor;
private readonly TColor backColor;
- private readonly bool[,] pattern;
+ private readonly bool[][] pattern;
+ private readonly int stride;
///
/// Initializes a new instance of the class.
@@ -60,7 +61,20 @@ namespace ImageSharp.Drawing.Brushes
{
this.foreColor = foreColor;
this.backColor = backColor;
- this.pattern = pattern;
+
+ this.stride = pattern.GetLength(1);
+
+ // convert the multidimension array into a jagged one.
+ var height = pattern.GetLength(0);
+ this.pattern = new bool[height][];
+ for (var x = 0; x < height; x++)
+ {
+ this.pattern[x] = new bool[stride];
+ for (var y = 0; y < stride; y++)
+ {
+ this.pattern[x][y] = pattern[x, y];
+ }
+ }
}
///
@@ -68,8 +82,11 @@ namespace ImageSharp.Drawing.Brushes
///
/// The brush.
internal PatternBrush(PatternBrush brush)
- : this(brush.foreColor, brush.backColor, brush.pattern)
{
+ this.foreColor = brush.foreColor;
+ this.backColor = brush.backColor;
+ this.pattern = brush.pattern;
+ this.stride = brush.stride;
}
///
@@ -85,31 +102,31 @@ namespace ImageSharp.Drawing.Brushes
///
public IBrushApplicator CreateApplicator(RectangleF region)
{
- return new PatternBrushApplicator(this.foreColor, this.backColor, this.pattern);
+ return new PatternBrushApplicator(this.foreColor, this.backColor, this.pattern, this.stride);
}
private class PatternBrushApplicator : IBrushApplicator
{
private readonly int xLength;
- private readonly int yLength;
- private readonly bool[,] pattern;
+ private readonly int stride;
+ private readonly bool[][] pattern;
private readonly TColor backColor = default(TColor);
private readonly TColor foreColor = default(TColor);
///
- /// Initializes a new instance of the class.
+ /// Initializes a new instance of the class.
///
/// Color of the fore.
/// Color of the back.
/// The pattern.
- public PatternBrushApplicator(TColor foreColor, TColor backColor, bool[,] pattern)
+ /// The stride.
+ public PatternBrushApplicator(TColor foreColor, TColor backColor, bool[][] pattern, int stride)
{
this.foreColor = foreColor;
this.backColor = backColor;
this.pattern = pattern;
-
- this.xLength = this.pattern.GetLength(0);
- this.yLength = this.pattern.GetLength(1);
+ this.xLength = pattern.Length;
+ this.stride = stride;
}
///
@@ -122,9 +139,9 @@ namespace ImageSharp.Drawing.Brushes
public TColor GetColor(Vector2 point)
{
var x = (int)point.X % this.xLength;
- var y = (int)point.Y % this.yLength;
+ var y = (int)point.Y % this.stride;
- if (this.pattern[x, y])
+ if (this.pattern[x][y])
{
return this.foreColor;
}
diff --git a/tests/ImageSharp.Benchmarks/Drawing/FillWithPattern.cs b/tests/ImageSharp.Benchmarks/Drawing/FillWithPattern.cs
new file mode 100644
index 000000000..6952fcd0f
--- /dev/null
+++ b/tests/ImageSharp.Benchmarks/Drawing/FillWithPattern.cs
@@ -0,0 +1,51 @@
+//
+// Copyright (c) James Jackson-South and contributors.
+// Licensed under the Apache License, Version 2.0.
+//
+
+namespace ImageSharp.Benchmarks
+{
+ using System.Drawing;
+ using System.Drawing.Drawing2D;
+
+ using BenchmarkDotNet.Attributes;
+ using CoreImage = ImageSharp.Image;
+ using CorePoint = ImageSharp.Point;
+ using CoreColor = ImageSharp.Color;
+ using CoreBrushes = ImageSharp.Drawing.Brushes.Brushes;
+ using CorePatternBrush = ImageSharp.Drawing.Brushes.PatternBrush;
+ using System.IO;
+
+ public class FillWithPattern
+ {
+ [Benchmark(Baseline = true, Description = "System.Drawing Fill with Pattern")]
+ public void DrawPatternPolygonSystemDrawing()
+ {
+ using (Bitmap destination = new Bitmap(800, 800))
+ {
+ using (Graphics graphics = Graphics.FromImage(destination))
+ {
+ graphics.SmoothingMode = SmoothingMode.AntiAlias;
+ var brush = new HatchBrush(HatchStyle.BackwardDiagonal, Color.HotPink);
+ graphics.FillRectangle(brush, new Rectangle(0,0, 800,800)); // can't find a way to flood fill with a brush
+ }
+ using (MemoryStream ms = new MemoryStream())
+ {
+ destination.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
+ }
+ }
+ }
+
+ [Benchmark(Description = "ImageSharp Fill with Pattern")]
+ public void DrawPatternPolygon3Core()
+ {
+ CoreImage image = new CoreImage(800, 800);
+ image.Fill(CoreBrushes.BackwardDiagonal(CoreColor.HotPink));
+
+ using (MemoryStream ms = new MemoryStream())
+ {
+ image.SaveAsBmp(ms);
+ }
+ }
+ }
+}
\ No newline at end of file