Browse Source

DrawPolygonTests, DrawLinesTests

pull/910/head
Anton Firszov 7 years ago
parent
commit
680156ad0c
  1. 100
      tests/ImageSharp.Tests/Drawing/DrawLinesTests.cs
  2. 43
      tests/ImageSharp.Tests/Drawing/DrawPolygonTests.cs
  3. 193
      tests/ImageSharp.Tests/Drawing/LineTests.cs
  4. 102
      tests/ImageSharp.Tests/Drawing/PolygonTests.cs
  5. 2
      tests/Images/External

100
tests/ImageSharp.Tests/Drawing/DrawLinesTests.cs

@ -0,0 +1,100 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Numerics;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using Xunit;
namespace SixLabors.ImageSharp.Tests.Drawing
{
[GroupOutput("Drawing")]
public class DrawLinesTests
{
[Theory]
[WithBasicTestPatternImages(250, 350, PixelTypes.Rgba32, "White", 1f, 2.5, true)]
[WithBasicTestPatternImages(250, 350, PixelTypes.Rgba32, "White", 0.6f, 10, true)]
[WithBasicTestPatternImages(250, 350, PixelTypes.Rgba32, "White", 1f, 5, false)]
[WithBasicTestPatternImages(250, 350, PixelTypes.Bgr24, "Yellow", 1f, 10, true)]
public void DrawLines_Simple<TPixel>(TestImageProvider<TPixel> provider, string colorName, float alpha, float thickness, bool antialias)
where TPixel : struct, IPixel<TPixel>
{
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
Pen<TPixel> pen = new Pen<TPixel>(color.ToPixel<TPixel>(), thickness);
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
}
[Theory]
[WithBasicTestPatternImages(250, 350, PixelTypes.Rgba32, "White", 1f, 5, false)]
public void DrawLines_Dash<TPixel>(TestImageProvider<TPixel> provider, string colorName, float alpha, float thickness, bool antialias)
where TPixel : struct, IPixel<TPixel>
{
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
Pen<TPixel> pen = Pens.Dash(color.ToPixel<TPixel>(), thickness);
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
}
[Theory]
[WithBasicTestPatternImages(250, 350, PixelTypes.Rgba32, "LightGreen", 1f, 5, false)]
public void DrawLines_Dot<TPixel>(TestImageProvider<TPixel> provider, string colorName, float alpha, float thickness, bool antialias)
where TPixel : struct, IPixel<TPixel>
{
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
Pen<TPixel> pen = Pens.Dot(color.ToPixel<TPixel>(), thickness);
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
}
[Theory]
[WithBasicTestPatternImages(250, 350, PixelTypes.Rgba32, "Yellow", 1f, 5, false)]
public void DrawLines_DashDot<TPixel>(TestImageProvider<TPixel> provider, string colorName, float alpha, float thickness, bool antialias)
where TPixel : struct, IPixel<TPixel>
{
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
Pen<TPixel> pen = Pens.DashDot(color.ToPixel<TPixel>(), thickness);
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
}
[Theory]
[WithBasicTestPatternImages(250, 350, PixelTypes.Rgba32, "Black", 1f, 5, false)]
public void DrawLines_DashDotDot<TPixel>(TestImageProvider<TPixel> provider, string colorName, float alpha, float thickness, bool antialias)
where TPixel : struct, IPixel<TPixel>
{
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
Pen<TPixel> pen = Pens.DashDotDot(color.ToPixel<TPixel>(), thickness);
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
}
private static void DrawLinesImpl<TPixel>(
TestImageProvider<TPixel> provider,
string colorName,
float alpha,
float thickness,
bool antialias,
Pen<TPixel> pen)
where TPixel : struct, IPixel<TPixel>
{
SixLabors.Primitives.PointF[] simplePath = { new Vector2(10, 10), new Vector2(200, 150), new Vector2(50, 300) };
GraphicsOptions options = new GraphicsOptions(antialias);
string aa = antialias ? "" : "_NoAntialias";
FormattableString outputDetails = $"{colorName}_A({alpha})_T({thickness}){aa}";
provider.RunValidatingProcessorTest(
c => c.DrawLines(options, pen, simplePath),
outputDetails,
appendSourceFileOrDescription: false);
}
}
}

43
tests/ImageSharp.Tests/Drawing/DrawPolygonTests.cs

@ -0,0 +1,43 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Numerics;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Primitives;
using SixLabors.ImageSharp.Processing;
using Xunit;
namespace SixLabors.ImageSharp.Tests.Drawing
{
[GroupOutput("Drawing")]
public class DrawPolygonTests
{
[Theory]
[WithBasicTestPatternImages(250, 350, PixelTypes.Rgba32, "White", 1f, 2.5, true)]
[WithBasicTestPatternImages(250, 350, PixelTypes.Rgba32, "White", 0.6f, 10, true)]
[WithBasicTestPatternImages(250, 350, PixelTypes.Rgba32, "White", 1f, 5, false)]
[WithBasicTestPatternImages(250, 350, PixelTypes.Bgr24, "Yellow", 1f, 10, true)]
public void DrawPolygon<TPixel>(TestImageProvider<TPixel> provider, string colorName, float alpha, float thickness, bool antialias)
where TPixel : struct, IPixel<TPixel>
{
SixLabors.Primitives.PointF[] simplePath =
{
new Vector2(10, 10), new Vector2(200, 150), new Vector2(50, 300)
};
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
GraphicsOptions options = new GraphicsOptions(antialias);
string aa = antialias ? "" : "_NoAntialias";
FormattableString outputDetails = $"{colorName}_A({alpha})_T({thickness}){aa}";
provider.RunValidatingProcessorTest(
c => c.DrawPolygon(options, color.ToPixel<TPixel>(), thickness, simplePath),
outputDetails,
appendSourceFileOrDescription: false);
}
}
}

193
tests/ImageSharp.Tests/Drawing/LineTests.cs

@ -1,193 +0,0 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System.Numerics;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using Xunit;
namespace SixLabors.ImageSharp.Tests.Drawing
{
public class LineTests : FileTestBase
{
[Fact]
public void ImageShouldBeOverlayedByPath()
{
string path = TestEnvironment.CreateOutputDirectory("Drawing", "Lines");
using (var image = new Image<Rgba32>(500, 500))
{
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
image.Mutate(
x => x.DrawLines(
Rgba32.HotPink,
5,
new Vector2(10, 10),
new Vector2(200, 150),
new Vector2(50, 300)));
image.Save($"{path}/Simple.png");
Buffer2D<Rgba32> sourcePixels = image.GetRootFramePixelBuffer();
Assert.Equal(Rgba32.HotPink, sourcePixels[11, 11]);
Assert.Equal(Rgba32.HotPink, sourcePixels[199, 149]);
Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]);
}
}
[Fact]
public void ImageShouldBeOverlayedByPath_NoAntialias()
{
string path = TestEnvironment.CreateOutputDirectory("Drawing", "Lines");
using (var image = new Image<Rgba32>(500, 500))
{
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
image.Mutate(
x => x.DrawLines(
new GraphicsOptions(false),
Rgba32.HotPink,
5,
new Vector2(10, 10),
new Vector2(200, 150),
new Vector2(50, 300)));
image.Save($"{path}/Simple_noantialias.png");
Buffer2D<Rgba32> sourcePixels = image.GetRootFramePixelBuffer();
Assert.Equal(Rgba32.HotPink, sourcePixels[11, 11]);
Assert.Equal(Rgba32.HotPink, sourcePixels[199, 149]);
Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]);
}
}
[Fact]
public void ImageShouldBeOverlayedByPathDashed()
{
string path = TestEnvironment.CreateOutputDirectory("Drawing", "Lines");
using (var image = new Image<Rgba32>(500, 500))
{
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
image.Mutate(x => x.DrawLines(Pens.Dash(Rgba32.HotPink, 5),
new SixLabors.Primitives.PointF[] {
new Vector2(10, 10),
new Vector2(200, 150),
new Vector2(50, 300)
}));
image.Save($"{path}/Dashed.png");
}
}
[Fact]
public void ImageShouldBeOverlayedByPathDotted()
{
string path = TestEnvironment.CreateOutputDirectory("Drawing", "Lines");
using (var image = new Image<Rgba32>(500, 500))
{
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
image.Mutate(x => x.DrawLines(Pens.Dot(Rgba32.HotPink, 5),
new SixLabors.Primitives.PointF[] {
new Vector2(10, 10),
new Vector2(200, 150),
new Vector2(50, 300)
}));
image.Save($"{path}/Dot.png");
}
}
[Fact]
public void ImageShouldBeOverlayedByPathDashDot()
{
string path = TestEnvironment.CreateOutputDirectory("Drawing", "Lines");
using (var image = new Image<Rgba32>(500, 500))
{
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
image.Mutate(x => x.DrawLines(Pens.DashDot(Rgba32.HotPink, 5),
new SixLabors.Primitives.PointF[] {
new Vector2(10, 10),
new Vector2(200, 150),
new Vector2(50, 300)
}));
image.Save($"{path}/DashDot.png");
}
}
[Fact]
public void ImageShouldBeOverlayedByPathDashDotDot()
{
string path = TestEnvironment.CreateOutputDirectory("Drawing", "Lines");
var image = new Image<Rgba32>(500, 500);
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
image.Mutate(x => x.DrawLines(Pens.DashDotDot(Rgba32.HotPink, 5),
new SixLabors.Primitives.PointF[] {
new Vector2(10, 10),
new Vector2(200, 150),
new Vector2(50, 300)
}));
image.Save($"{path}/DashDotDot.png");
}
[Fact]
public void ImageShouldBeOverlayedPathWithOpacity()
{
string path = TestEnvironment.CreateOutputDirectory("Drawing", "Lines");
var color = new Rgba32(Rgba32.HotPink.R, Rgba32.HotPink.G, Rgba32.HotPink.B, 150);
var image = new Image<Rgba32>(500, 500);
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
image.Mutate(
x => x.DrawLines(
color,
10,
new Vector2(10, 10),
new Vector2(200, 150),
new Vector2(50, 300)));
image.Save($"{path}/Opacity.png");
//shift background color towards forground color by the opacity amount
var mergedColor =
new Rgba32(Vector4.Lerp(Rgba32.Blue.ToVector4(), Rgba32.HotPink.ToVector4(), 150f / 255f));
Buffer2D<Rgba32> sourcePixels = image.GetRootFramePixelBuffer();
Assert.Equal(mergedColor, sourcePixels[11, 11]);
Assert.Equal(mergedColor, sourcePixels[199, 149]);
Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]);
}
[Fact]
public void ImageShouldBeOverlayedByPathOutline()
{
string path = TestEnvironment.CreateOutputDirectory("Drawing", "Lines");
var image = new Image<Rgba32>(500, 500);
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
image.Mutate(
x => x.DrawLines(
Rgba32.HotPink,
10,
new Vector2(10, 10),
new Vector2(200, 10),
new Vector2(200, 150),
new Vector2(10, 150)));
image.Save($"{path}/Rectangle.png");
Buffer2D<Rgba32> sourcePixels = image.GetRootFramePixelBuffer();
Assert.Equal(Rgba32.HotPink, sourcePixels[11, 11]);
Assert.Equal(Rgba32.HotPink, sourcePixels[198, 10]);
Assert.Equal(Rgba32.Blue, sourcePixels[10, 50]);
Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]);
}
}
}

102
tests/ImageSharp.Tests/Drawing/PolygonTests.cs

@ -1,102 +0,0 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System.Numerics;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Primitives;
using SixLabors.ImageSharp.Processing;
using Xunit;
namespace SixLabors.ImageSharp.Tests.Drawing
{
public class PolygonTests : FileTestBase
{
[Fact]
public void ImageShouldBeOverlayedByPolygonOutline()
{
string path = TestEnvironment.CreateOutputDirectory("Drawing", "Polygons");
using (Image<Rgba32> image = new Image<Rgba32>(500, 500))
{
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
image.Mutate(
x => x.DrawPolygon(
Rgba32.HotPink,
5,
new Vector2(10, 10),
new Vector2(200, 150),
new Vector2(50, 300)));
image.Save($"{path}/Simple.png");
Buffer2D<Rgba32> sourcePixels = image.GetRootFramePixelBuffer();
Assert.Equal(Rgba32.HotPink, sourcePixels[9, 9]);
Assert.Equal(Rgba32.HotPink, sourcePixels[199, 149]);
Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]);
Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]);
}
}
[Fact]
public void ImageShouldBeOverlayedPolygonOutlineWithOpacity()
{
string path = TestEnvironment.CreateOutputDirectory("Drawing", "Polygons");
PointF[] simplePath = {
new Vector2(10, 10),
new Vector2(200, 150),
new Vector2(50, 300)
};
Rgba32 color = new Rgba32(Rgba32.HotPink.R, Rgba32.HotPink.G, Rgba32.HotPink.B, 150);
using (Image<Rgba32> image = new Image<Rgba32>(500, 500))
{
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
image.Mutate(x => x.DrawPolygon(color, 10, simplePath));
image.Save($"{path}/Opacity.png");
//shift background color towards forground color by the opacity amount
Rgba32 mergedColor = new Rgba32(
Vector4.Lerp(Rgba32.Blue.ToVector4(), Rgba32.HotPink.ToVector4(), 150f / 255f));
Buffer2D<Rgba32> sourcePixels = image.GetRootFramePixelBuffer();
Assert.Equal(mergedColor, sourcePixels[9, 9]);
Assert.Equal(mergedColor, sourcePixels[199, 149]);
Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]);
Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]);
}
}
[Fact]
public void ImageShouldBeOverlayedByRectangleOutline()
{
string path = TestEnvironment.CreateOutputDirectory("Drawing", "Polygons");
using (Image<Rgba32> image = new Image<Rgba32>(500, 500))
{
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
image.Mutate(
x => x.Draw(Rgba32.HotPink, 10, new Rectangle(10, 10, 190, 140)));
image.Save($"{path}/Rectangle.png");
Buffer2D<Rgba32> sourcePixels = image.GetRootFramePixelBuffer();
Assert.Equal(Rgba32.HotPink, sourcePixels[8, 8]);
Assert.Equal(Rgba32.HotPink, sourcePixels[198, 10]);
Assert.Equal(Rgba32.HotPink, sourcePixels[10, 50]);
Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]);
Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]);
}
}
}
}

2
tests/Images/External

@ -1 +1 @@
Subproject commit 0edeb078b9d9f9c8be016ca514a3a625c4768bd6
Subproject commit 4fe9334aa8898f67b6e3df94bc00867645c3e709
Loading…
Cancel
Save