Browse Source

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.
af/merge-core
Scott Williams 9 years ago
parent
commit
d0c0dee582
  1. 2
      src/ImageSharp.Drawing/Brushes/ImageBrush{TColor}.cs
  2. 7
      src/ImageSharp.Drawing/Brushes/PatternBrush{TColor}.cs
  3. 2
      src/ImageSharp.Drawing/Brushes/RecolorBrush{TColor}.cs
  4. 2
      src/ImageSharp.Drawing/Brushes/SolidBrush{TColor}.cs
  5. 2
      src/ImageSharp.Drawing/ImageSharp.Drawing.csproj
  6. 26
      tests/ImageSharp.Benchmarks/Drawing/FillPolygon.cs
  7. 26
      tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs

2
src/ImageSharp.Drawing/Brushes/ImageBrush{TColor}.cs

@ -115,7 +115,7 @@ namespace ImageSharp.Drawing.Brushes
/// <inheritdoc />
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<float> buffer = new PinnedBuffer<float>(scanlineBuffer))
{

7
src/ImageSharp.Drawing/Brushes/PatternBrush{TColor}.cs

@ -86,6 +86,7 @@ namespace ImageSharp.Drawing.Brushes
internal PatternBrush(PatternBrush<TColor> brush)
{
this.pattern = brush.pattern;
this.patternVector = brush.patternVector;
}
/// <inheritdoc />
@ -112,7 +113,7 @@ namespace ImageSharp.Drawing.Brushes
/// <param name="pattern">The pattern.</param>
/// <param name="patternVector">The patternVector.</param>
public PatternBrushApplicator(PixelAccessor<TColor> sourcePixels, Fast2DArray<TColor> pattern, Fast2DArray<Vector4> patternVector)
: base(sourcePixels)
: base(sourcePixels)
{
this.pattern = pattern;
this.patternVector = patternVector;
@ -147,7 +148,7 @@ namespace ImageSharp.Drawing.Brushes
/// <inheritdoc />
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<float> buffer = new PinnedBuffer<float>(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);

2
src/ImageSharp.Drawing/Brushes/RecolorBrush{TColor}.cs

@ -139,7 +139,7 @@ namespace ImageSharp.Drawing.Brushes
/// <inheritdoc />
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<float> buffer = new PinnedBuffer<float>(scanlineBuffer))
{

2
src/ImageSharp.Drawing/Brushes/SolidBrush{TColor}.cs

@ -87,7 +87,7 @@ namespace ImageSharp.Drawing.Brushes
/// <inheritdoc />
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<float> buffer = new PinnedBuffer<float>(scanlineBuffer))
{

2
src/ImageSharp.Drawing/ImageSharp.Drawing.csproj

@ -40,7 +40,7 @@
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="SixLabors.Fonts" Version="0.1.0-alpha0002" />
<PackageReference Include="SixLabors.Shapes" Version="0.1.0-alpha0008" />
<PackageReference Include="SixLabors.Shapes" Version="0.1.0-alpha0009" />
</ItemGroup>
<PropertyGroup>
<CodeAnalysisRuleSet>..\..\ImageSharp.ruleset</CodeAnalysisRuleSet>

26
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);
}
}
}
}
}

26
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<Color> sourcePixels = image.Lock())
{
Assert.Equal(Color.HotPink, sourcePixels[81, 145]);
}
}
}
[Fact]
public void ImageShouldBeOverlayedByFilledPolygonNoAntialias()
{

Loading…
Cancel
Save