From 8d85a75ced991d21b1f86eca9c8f8256b27f6451 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Thu, 15 Dec 2016 13:03:01 +0000 Subject: [PATCH] provide graphics options for drawing methods this allows enabling/disabling the antailising --- src/ImageSharp/Drawing/Draw.cs | 299 +++++++++++++++++- src/ImageSharp/Drawing/Fill.cs | 77 ++++- src/ImageSharp/Drawing/GraphicsOptions.cs | 32 ++ src/ImageSharp/Drawing/Paths/IPath.cs | 9 +- src/ImageSharp/Drawing/Paths/Path.cs | 4 +- .../Drawing/Processors/DrawPathProcessor.cs | 39 ++- .../Drawing/Processors/FillShapeProcessor.cs | 12 +- .../Drawing/Shapes/BezierPolygon.cs | 5 +- .../Drawing/Shapes/ComplexPolygon.cs | 9 +- src/ImageSharp/Drawing/Shapes/IShape.cs | 11 +- .../Drawing/Shapes/LinearPolygon.cs | 5 +- src/ImageSharp/Drawing/Shapes/Polygon.cs | 11 +- .../ImageSharp.Tests/Drawing/DrawPathTests.cs | 2 - tests/ImageSharp.Tests/Drawing/LineTests.cs | 30 +- .../Drawing/SolidPolygonTests.cs | 31 ++ 15 files changed, 522 insertions(+), 54 deletions(-) create mode 100644 src/ImageSharp/Drawing/GraphicsOptions.cs diff --git a/src/ImageSharp/Drawing/Draw.cs b/src/ImageSharp/Drawing/Draw.cs index aa2b91de60..5c2f208a38 100644 --- a/src/ImageSharp/Drawing/Draw.cs +++ b/src/ImageSharp/Drawing/Draw.cs @@ -19,6 +19,25 @@ namespace ImageSharp /// public static partial class ImageExtensions { + /// + /// Draws the outline of the polygon with the provided pen. + /// + /// The type of the color. + /// The type of the packed. + /// The source. + /// The pen. + /// The shape. + /// The options. + /// + /// The Image + /// + public static Image DrawPolygon(this Image source, IPen pen, IShape shape, GraphicsOptions options) + where TColor : struct, IPackedPixel + where TPacked : struct + { + return source.Process(new DrawPathProcessor(pen, shape, options)); + } + /// /// Draws the outline of the polygon with the provided pen. /// @@ -32,7 +51,27 @@ namespace ImageSharp where TColor : struct, IPackedPixel where TPacked : struct { - return source.Process(new DrawPathProcessor(pen, shape)); + return source.DrawPolygon(pen, shape, GraphicsOptions.Default); + } + + /// + /// Draws the outline of the polygon with the provided brush at the provided thickness. + /// + /// The type of the color. + /// The type of the packed. + /// The source. + /// The brush. + /// The thickness. + /// The shape. + /// The options. + /// + /// The Image + /// + public static Image DrawPolygon(this Image source, IBrush brush, float thickness, IShape shape, GraphicsOptions options) + where TColor : struct, IPackedPixel + where TPacked : struct + { + return source.DrawPolygon(new Pen(brush, thickness), shape, options); } /// @@ -52,6 +91,26 @@ namespace ImageSharp return source.DrawPolygon(new Pen(brush, thickness), shape); } + /// + /// Draws the outline of the polygon with the provided brush at the provided thickness. + /// + /// The type of the color. + /// The type of the packed. + /// The source. + /// The color. + /// The thickness. + /// The shape. + /// The options. + /// + /// The Image + /// + public static Image DrawPolygon(this Image source, TColor color, float thickness, IShape shape, GraphicsOptions options) + where TColor : struct, IPackedPixel + where TPacked : struct + { + return source.DrawPolygon(new SolidBrush(color), thickness, shape, options); + } + /// /// Draws the outline of the polygon with the provided brush at the provided thickness. /// @@ -69,6 +128,26 @@ namespace ImageSharp return source.DrawPolygon(new SolidBrush(color), thickness, shape); } + /// + /// Draws the provided Points as a closed Linear Polygon with the provided brush at the provided thickness. + /// + /// The type of the color. + /// The type of the packed. + /// The source. + /// The brush. + /// The thickness. + /// The points. + /// The options. + /// + /// The Image + /// + public static Image DrawPolygon(this Image source, IBrush brush, float thickness, Vector2[] points, GraphicsOptions options) + where TColor : struct, IPackedPixel + where TPacked : struct + { + return source.DrawPolygon(new Pen(brush, thickness), new Polygon(new LinearLineSegment(points)), options); + } + /// /// Draws the provided Points as a closed Linear Polygon with the provided brush at the provided thickness. /// @@ -103,6 +182,44 @@ namespace ImageSharp return source.DrawPolygon(new SolidBrush(color), thickness, points); } + /// + /// Draws the provided Points as a closed Linear Polygon with the provided brush at the provided thickness. + /// + /// The type of the color. + /// The type of the packed. + /// The source. + /// The color. + /// The thickness. + /// The points. + /// The options. + /// + /// The Image + /// + public static Image DrawPolygon(this Image source, TColor color, float thickness, Vector2[] points, GraphicsOptions options) + where TColor : struct, IPackedPixel + where TPacked : struct + { + return source.DrawPolygon(new SolidBrush(color), thickness, points, options); + } + + /// + /// Draws the provided Points as a closed Linear Polygon with the provided Pen. + /// + /// The type of the color. + /// The type of the packed. + /// The source. + /// The pen. + /// The points. + /// The options. + /// + /// The Image + /// + public static Image DrawPolygon(this Image source, IPen pen, Vector2[] points, GraphicsOptions options) + where TColor : struct, IPackedPixel + where TPacked : struct + { + return source.DrawPolygon(pen, new Polygon(new LinearLineSegment(points)), options); + } /// /// Draws the provided Points as a closed Linear Polygon with the provided Pen. /// @@ -119,6 +236,25 @@ namespace ImageSharp return source.DrawPolygon(pen, new Polygon(new LinearLineSegment(points))); } + /// + /// Draws the path with the provided pen. + /// + /// The type of the color. + /// The type of the packed. + /// The source. + /// The pen. + /// The path. + /// The options. + /// + /// The Image + /// + public static Image DrawPath(this Image source, IPen pen, IPath path, GraphicsOptions options) + where TColor : struct, IPackedPixel + where TPacked : struct + { + return source.Process(new DrawPathProcessor(pen, path, options)); + } + /// /// Draws the path with the provided pen. /// @@ -132,7 +268,27 @@ namespace ImageSharp where TColor : struct, IPackedPixel where TPacked : struct { - return source.Process(new DrawPathProcessor(pen, path)); + return source.Process(new DrawPathProcessor(pen, path, GraphicsOptions.Default)); + } + + /// + /// Draws the path with the bursh at the privdied thickness. + /// + /// The type of the color. + /// The type of the packed. + /// The source. + /// The brush. + /// The thickness. + /// The path. + /// The options. + /// + /// The Image + /// + public static Image DrawPath(this Image source, IBrush brush, float thickness, IPath path, GraphicsOptions options) + where TColor : struct, IPackedPixel + where TPacked : struct + { + return source.DrawPath(new Pen(brush, thickness), path, options); } /// @@ -152,6 +308,26 @@ namespace ImageSharp return source.DrawPath(new Pen(brush, thickness), path); } + /// + /// Draws the path with the bursh at the privdied thickness. + /// + /// The type of the color. + /// The type of the packed. + /// The source. + /// The color. + /// The thickness. + /// The path. + /// The options. + /// + /// The Image + /// + public static Image DrawPath(this Image source, TColor color, float thickness, IPath path, GraphicsOptions options) + where TColor : struct, IPackedPixel + where TPacked : struct + { + return source.DrawPath(new SolidBrush(color), thickness, path, options); + } + /// /// Draws the path with the bursh at the privdied thickness. /// @@ -169,6 +345,26 @@ namespace ImageSharp return source.DrawPath(new SolidBrush(color), thickness, path); } + /// + /// Draws the provided Points as an open Linear path at the provided thickness with the supplied brush + /// + /// The type of the color. + /// The type of the packed. + /// The source. + /// The brush. + /// The thickness. + /// The points. + /// The options. + /// + /// The Image + /// + public static Image DrawLines(this Image source, IBrush brush, float thickness, Vector2[] points, GraphicsOptions options) + where TColor : struct, IPackedPixel + where TPacked : struct + { + return source.DrawPath(new Pen(brush, thickness), new Path(new LinearLineSegment(points)), options); + } + /// /// Draws the provided Points as an open Linear path at the provided thickness with the supplied brush /// @@ -203,6 +399,45 @@ namespace ImageSharp return source.DrawLines(new SolidBrush(color), thickness, points); } + /// + /// Draws the provided Points as an open Linear path at the provided thickness with the supplied brush + /// + /// The type of the color. + /// The type of the packed. + /// The source. + /// The color. + /// The thickness. + /// The points. + /// The options. + /// + /// The Image + /// + public static Image DrawLines(this Image source, TColor color, float thickness, Vector2[] points, GraphicsOptions options) + where TColor : struct, IPackedPixel + where TPacked : struct + { + return source.DrawLines(new SolidBrush(color), thickness, points, options); + } + + /// + /// Draws the provided Points as an open Linear path with the supplied pen + /// + /// The type of the color. + /// The type of the packed. + /// The source. + /// The pen. + /// The points. + /// The options. + /// + /// The Image + /// + public static Image DrawLines(this Image source, IPen pen, Vector2[] points, GraphicsOptions options) + where TColor : struct, IPackedPixel + where TPacked : struct + { + return source.DrawPath(pen, new Path(new LinearLineSegment(points)), options); + } + /// /// Draws the provided Points as an open Linear path with the supplied pen /// @@ -218,7 +453,26 @@ namespace ImageSharp { return source.DrawPath(pen, new Path(new LinearLineSegment(points))); } - + + /// + /// Draws the provided Points as an open Bezier path at the provided thickness with the supplied brush + /// + /// The type of the color. + /// The type of the packed. + /// The source. + /// The brush. + /// The thickness. + /// The points. + /// The options. + /// + /// The Image + /// + public static Image DrawBeziers(this Image source, IBrush brush, float thickness, Vector2[] points, GraphicsOptions options) + where TColor : struct, IPackedPixel + where TPacked : struct + { + return source.DrawPath(new Pen(brush, thickness), new Path(new BezierLineSegment(points)), options); + } /// /// Draws the provided Points as an open Bezier path at the provided thickness with the supplied brush @@ -254,6 +508,45 @@ namespace ImageSharp return source.DrawBeziers(new SolidBrush(color), thickness, points); } + /// + /// Draws the provided Points as an open Bezier path at the provided thickness with the supplied brush + /// + /// The type of the color. + /// The type of the packed. + /// The source. + /// The color. + /// The thickness. + /// The points. + /// The options. + /// + /// The Image + /// + public static Image DrawBeziers(this Image source, TColor color, float thickness, Vector2[] points, GraphicsOptions options) + where TColor : struct, IPackedPixel + where TPacked : struct + { + return source.DrawBeziers(new SolidBrush(color), thickness, points, options); + } + + /// + /// Draws the provided Points as an open Bezier path with the supplied pen + /// + /// The type of the color. + /// The type of the packed. + /// The source. + /// The pen. + /// The points. + /// The options. + /// + /// The Image + /// + public static Image DrawBeziers(this Image source, IPen pen, Vector2[] points, GraphicsOptions options) + where TColor : struct, IPackedPixel + where TPacked : struct + { + return source.DrawPath(pen, new Path(new BezierLineSegment(points)), options); + } + /// /// Draws the provided Points as an open Bezier path with the supplied pen /// diff --git a/src/ImageSharp/Drawing/Fill.cs b/src/ImageSharp/Drawing/Fill.cs index 02c9a7a3f8..ebd15a614b 100644 --- a/src/ImageSharp/Drawing/Fill.cs +++ b/src/ImageSharp/Drawing/Fill.cs @@ -48,6 +48,22 @@ namespace ImageSharp return source.Fill(new SolidBrush(color)); } + /// + /// Flood fills the image in the shape o fhte provided polygon with the specified brush.. + /// + /// The type of the color. + /// The type of the packed. + /// The source. + /// The brush. + /// The shape. + /// The Image + public static Image Fill(this Image source, IBrush brush, IShape shape, GraphicsOptions options) + where TColor : struct, IPackedPixel + where TPacked : struct + { + return source.Process(new FillShapeProcessor(brush, shape, options)); + } + /// /// Flood fills the image in the shape o fhte provided polygon with the specified brush.. /// @@ -61,7 +77,26 @@ namespace ImageSharp where TColor : struct, IPackedPixel where TPacked : struct { - return source.Process(new FillShapeProcessor(brush, shape)); + return source.Process(new FillShapeProcessor(brush, shape, GraphicsOptions.Default)); + } + + /// + /// Flood fills the image in the shape o fhte provided polygon with the specified brush.. + /// + /// The type of the color. + /// The type of the packed. + /// The source. + /// The color. + /// The shape. + /// The options. + /// + /// The Image + /// + public static Image Fill(this Image source, TColor color, IShape shape, GraphicsOptions options) + where TColor : struct, IPackedPixel + where TPacked : struct + { + return source.Fill(new SolidBrush(color), shape, options); } /// @@ -80,6 +115,26 @@ namespace ImageSharp return source.Fill(new SolidBrush(color), shape); } + /// + /// Flood fills the image in the shape of a Linear polygon described by the points + /// + /// The type of the color. + /// The type of the packed. + /// The source. + /// The brush. + /// The points. + /// The options. + /// + /// The Image + /// + public static Image FillPolygon(this Image source, IBrush brush, Vector2[] points, GraphicsOptions options) + where TColor : struct, IPackedPixel + where TPacked : struct + { + // using Polygon directly instead of LinearPolygon as its will have less indirection + return source.Fill(brush, new Polygon(new LinearLineSegment(points)), options); + } + /// /// Flood fills the image in the shape of a Linear polygon described by the points /// @@ -97,6 +152,26 @@ namespace ImageSharp return source.Fill(brush, new Polygon(new LinearLineSegment(points))); } + /// + /// Flood fills the image in the shape of a Linear polygon described by the points + /// + /// The type of the color. + /// The type of the packed. + /// The source. + /// The color. + /// The points. + /// The options. + /// + /// The Image + /// + public static Image FillPolygon(this Image source, TColor color, Vector2[] points, GraphicsOptions options) + where TColor : struct, IPackedPixel + where TPacked : struct + { + // using Polygon directly instead of LinearPolygon as its will have less indirection + return source.Fill(new SolidBrush(color), new Polygon(new LinearLineSegment(points)), options); + } + /// /// Flood fills the image in the shape of a Linear polygon described by the points /// diff --git a/src/ImageSharp/Drawing/GraphicsOptions.cs b/src/ImageSharp/Drawing/GraphicsOptions.cs new file mode 100644 index 0000000000..774f84520c --- /dev/null +++ b/src/ImageSharp/Drawing/GraphicsOptions.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace ImageSharp.Drawing +{ + /// + /// Options for influancing the drawing functions. + /// + public struct GraphicsOptions + { + /// + /// Represents the default . + /// + public static readonly GraphicsOptions Default = new GraphicsOptions(true); + + /// + /// Initializes a new instance of the struct. + /// + /// if set to true [enable antialiasing]. + public GraphicsOptions(bool enableAntialiasing) + { + Antialias = enableAntialiasing; + } + + /// + /// Should antialias be applied. + /// + public bool Antialias; + } +} \ No newline at end of file diff --git a/src/ImageSharp/Drawing/Paths/IPath.cs b/src/ImageSharp/Drawing/Paths/IPath.cs index 3eaeb5c200..eaa6b84d23 100644 --- a/src/ImageSharp/Drawing/Paths/IPath.cs +++ b/src/ImageSharp/Drawing/Paths/IPath.cs @@ -43,9 +43,10 @@ namespace ImageSharp.Drawing.Paths /// /// Calcualtes the distance along and away from the path for a specified point. /// - /// The x. - /// The y. - /// Returns details about the point and its distance away from the path. - PointInfo Distance(int x, int y); + /// The point. + /// + /// Returns details about the point and its distance away from the path. + /// + PointInfo Distance(Vector2 point); } } diff --git a/src/ImageSharp/Drawing/Paths/Path.cs b/src/ImageSharp/Drawing/Paths/Path.cs index eb95535743..ce7cb57e7b 100644 --- a/src/ImageSharp/Drawing/Paths/Path.cs +++ b/src/ImageSharp/Drawing/Paths/Path.cs @@ -73,9 +73,9 @@ namespace ImageSharp.Drawing.Paths /// /// Returns details about the point and its distance away from the path. /// - public PointInfo Distance(int x, int y) + public PointInfo Distance(Vector2 point) { - return this.innerPath.DistanceFromPath(new Vector2(x, y)); + return this.innerPath.DistanceFromPath(point); } } } \ No newline at end of file diff --git a/src/ImageSharp/Drawing/Processors/DrawPathProcessor.cs b/src/ImageSharp/Drawing/Processors/DrawPathProcessor.cs index 33a10c625a..2fba6eb4f4 100644 --- a/src/ImageSharp/Drawing/Processors/DrawPathProcessor.cs +++ b/src/ImageSharp/Drawing/Processors/DrawPathProcessor.cs @@ -33,14 +33,16 @@ namespace ImageSharp.Drawing.Processors private readonly IPen pen; private readonly IPath[] paths; private readonly RectangleF region; - + private readonly GraphicsOptions options; + /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The pen. /// The shape. - public DrawPathProcessor(IPen pen, IShape shape) - : this(pen, shape.ToArray()) + /// The options. + public DrawPathProcessor(IPen pen, IShape shape, GraphicsOptions options) + : this(pen, shape.ToArray(), options) { } @@ -48,11 +50,24 @@ namespace ImageSharp.Drawing.Processors /// Initializes a new instance of the class. /// /// The pen. + /// The path. + /// The options. + public DrawPathProcessor(IPen pen, IPath path, GraphicsOptions options) + : this(pen, new[] { path }, options) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The pen. /// The paths. - public DrawPathProcessor(IPen pen, params IPath[] paths) + /// The options. + public DrawPathProcessor(IPen pen, IPath[] paths, GraphicsOptions options) { this.paths = paths; this.pen = pen; + this.options = options; if (paths.Length != 1) { @@ -112,15 +127,17 @@ namespace ImageSharp.Drawing.Processors y => { int offsetY = y - polyStartY; - + var currentPoint = default(Vector2); for (int x = minX; x < maxX; x++) { int offsetX = x - startX; + currentPoint.X = offsetX; + currentPoint.Y = offsetY; - var dist = Closest(offsetX, offsetY); + var dist = Closest(currentPoint); var color = applicator.GetColor(dist); - + var opacity = this.Opacity(color.DistanceFromElement); if (opacity > Epsilon) @@ -143,14 +160,14 @@ namespace ImageSharp.Drawing.Processors } } - private PointInfo Closest(int x, int y) + private PointInfo Closest(Vector2 point) { PointInfo result = default(PointInfo); float distance = float.MaxValue; for (int i = 0; i < this.paths.Length; i++) { - var p = this.paths[i].Distance(x, y); + var p = this.paths[i].Distance(point); if (p.DistanceFromPath < distance) { distance = p.DistanceFromPath; @@ -167,7 +184,7 @@ namespace ImageSharp.Drawing.Processors { return 1; } - else if (distance < AntialiasFactor) + else if (this.options.Antialias && distance < AntialiasFactor) { return 1 - (distance / AntialiasFactor); } diff --git a/src/ImageSharp/Drawing/Processors/FillShapeProcessor.cs b/src/ImageSharp/Drawing/Processors/FillShapeProcessor.cs index 8bdec33d60..69cc451dbb 100644 --- a/src/ImageSharp/Drawing/Processors/FillShapeProcessor.cs +++ b/src/ImageSharp/Drawing/Processors/FillShapeProcessor.cs @@ -28,16 +28,18 @@ namespace ImageSharp.Drawing.Processors private const int DrawPadding = 1; private readonly IBrush fillColor; private readonly IShape poly; + private readonly GraphicsOptions options; /// /// Initializes a new instance of the class. /// /// The brush. /// The shape. - public FillShapeProcessor(IBrush brush, IShape shape) + public FillShapeProcessor(IBrush brush, IShape shape, GraphicsOptions options) { this.poly = shape; this.fillColor = brush; + this.options = options; } /// @@ -71,7 +73,7 @@ namespace ImageSharp.Drawing.Processors { polyStartY = 0; } - + using (PixelAccessor sourcePixels = source.Lock()) using (IBrushApplicator applicator = this.fillColor.CreateApplicator(rect)) { @@ -84,13 +86,13 @@ namespace ImageSharp.Drawing.Processors int offsetY = y - polyStartY; Vector2 currentPoint = default(Vector2); + Vector2 currentPointOffset = default(Vector2); for (int x = minX; x < maxX; x++) { int offsetX = x - startX; currentPoint.X = offsetX; currentPoint.Y = offsetY; - - var dist = this.poly.Distance(offsetX, offsetY); + var dist = this.poly.Distance(currentPoint); var opacity = this.Opacity(dist); if (opacity > Epsilon) @@ -118,7 +120,7 @@ namespace ImageSharp.Drawing.Processors { return 1; } - else if (distance < AntialiasFactor) + else if (this.options.Antialias && distance < AntialiasFactor) { return 1 - (distance / AntialiasFactor); } diff --git a/src/ImageSharp/Drawing/Shapes/BezierPolygon.cs b/src/ImageSharp/Drawing/Shapes/BezierPolygon.cs index 2e77ff77b8..0365588238 100644 --- a/src/ImageSharp/Drawing/Shapes/BezierPolygon.cs +++ b/src/ImageSharp/Drawing/Shapes/BezierPolygon.cs @@ -37,12 +37,11 @@ namespace ImageSharp.Drawing.Shapes /// /// the distance of the point from the outline of the shape, if the value is negative it is inside the polygon bounds /// - /// The x. - /// The y. + /// The point. /// /// The distance from the shape. /// - public float Distance(int x, int y) => this.innerPolygon.Distance(x, y); + public float Distance(Vector2 point) => this.innerPolygon.Distance(point); /// /// Returns an enumerator that iterates through the collection. diff --git a/src/ImageSharp/Drawing/Shapes/ComplexPolygon.cs b/src/ImageSharp/Drawing/Shapes/ComplexPolygon.cs index 5f77c55b94..d0edb76aa5 100644 --- a/src/ImageSharp/Drawing/Shapes/ComplexPolygon.cs +++ b/src/ImageSharp/Drawing/Shapes/ComplexPolygon.cs @@ -65,24 +65,23 @@ namespace ImageSharp.Drawing.Shapes /// /// the distance of the point from the outline of the shape, if the value is negative it is inside the polygon bounds /// - /// The x. - /// The y. + /// The point. /// /// Returns the distance from thr shape to the point /// - float IShape.Distance(int x, int y) + float IShape.Distance(Vector2 point) { // get the outline we are closest to the center of // by rights we should only be inside 1 outline // othersie we will start returning the distanct to the nearest shape - var dist = this.outlines.Select(o => o.Distance(x, y)).OrderBy(p => p).First(); + var dist = this.outlines.Select(o => o.Distance(point)).OrderBy(p => p).First(); if (dist <= 0) { // inside poly foreach (var hole in this.holes) { - var distFromHole = hole.Distance(x, y); + var distFromHole = hole.Distance(point); // less than zero we are inside shape if (distFromHole <= 0) diff --git a/src/ImageSharp/Drawing/Shapes/IShape.cs b/src/ImageSharp/Drawing/Shapes/IShape.cs index 74eb7243a7..2640b33aa4 100644 --- a/src/ImageSharp/Drawing/Shapes/IShape.cs +++ b/src/ImageSharp/Drawing/Shapes/IShape.cs @@ -6,7 +6,7 @@ namespace ImageSharp.Drawing.Shapes { using System.Collections.Generic; - + using System.Numerics; using Paths; /// @@ -25,9 +25,10 @@ namespace ImageSharp.Drawing.Shapes /// /// the distance of the point from the outline of the shape, if the value is negative it is inside the polygon bounds /// - /// The x. - /// The y. - /// Returns the distance from the shape to the point - float Distance(int x, int y); + /// The point. + /// + /// Returns the distance from the shape to the point + /// + float Distance(Vector2 point); } } diff --git a/src/ImageSharp/Drawing/Shapes/LinearPolygon.cs b/src/ImageSharp/Drawing/Shapes/LinearPolygon.cs index fcd0b65453..2b7a0292a1 100644 --- a/src/ImageSharp/Drawing/Shapes/LinearPolygon.cs +++ b/src/ImageSharp/Drawing/Shapes/LinearPolygon.cs @@ -37,12 +37,11 @@ namespace ImageSharp.Drawing.Shapes /// /// the distance of the point from the outline of the shape, if the value is negative it is inside the polygon bounds /// - /// The x. - /// The y. + /// The point. /// /// Returns the distance from the shape to the point /// - public float Distance(int x, int y) => this.innerPolygon.Distance(x, y); + public float Distance(Vector2 point) => this.innerPolygon.Distance(point); /// /// Returns an enumerator that iterates through the collection. diff --git a/src/ImageSharp/Drawing/Shapes/Polygon.cs b/src/ImageSharp/Drawing/Shapes/Polygon.cs index b13bf4f2f1..9d9626d4ee 100644 --- a/src/ImageSharp/Drawing/Shapes/Polygon.cs +++ b/src/ImageSharp/Drawing/Shapes/Polygon.cs @@ -56,15 +56,12 @@ namespace ImageSharp.Drawing.Shapes /// /// the distance of the point from the outline of the shape, if the value is negative it is inside the polygon bounds /// - /// The x. - /// The y. + /// The point. /// /// The distance of the point away from the shape /// - public float Distance(int x, int y) + public float Distance(Vector2 point) { - var point = new Vector2(x, y); - bool isInside = this.innerPath.PointInPolygon(point); var distance = this.innerPath.DistanceFromPath(point).DistanceFromPath; @@ -106,9 +103,9 @@ namespace ImageSharp.Drawing.Shapes /// /// distance metadata about the point. /// - PointInfo IPath.Distance(int x, int y) + PointInfo IPath.Distance(Vector2 point) { - return this.innerPath.DistanceFromPath(new Vector2(x, y)); + return this.innerPath.DistanceFromPath(point); } /// diff --git a/tests/ImageSharp.Tests/Drawing/DrawPathTests.cs b/tests/ImageSharp.Tests/Drawing/DrawPathTests.cs index 3404694967..61080b815b 100644 --- a/tests/ImageSharp.Tests/Drawing/DrawPathTests.cs +++ b/tests/ImageSharp.Tests/Drawing/DrawPathTests.cs @@ -17,8 +17,6 @@ namespace ImageSharp.Tests.Drawing public class DrawPathTests : FileTestBase { - - [Fact] public void ImageShouldBeOverlayedByPath() { diff --git a/tests/ImageSharp.Tests/Drawing/LineTests.cs b/tests/ImageSharp.Tests/Drawing/LineTests.cs index 7c1a826896..46b315164e 100644 --- a/tests/ImageSharp.Tests/Drawing/LineTests.cs +++ b/tests/ImageSharp.Tests/Drawing/LineTests.cs @@ -16,8 +16,6 @@ namespace ImageSharp.Tests.Drawing public class LineTests : FileTestBase { - - [Fact] public void ImageShouldBeOverlayedByPath() { @@ -44,9 +42,35 @@ namespace ImageSharp.Tests.Drawing Assert.Equal(Color.Blue, sourcePixels[50, 50]); } - } + [Fact] + public void ImageShouldBeOverlayedByPath_NoAntialias() + { + string path = CreateOutputDirectory("Drawing", "Lines"); + var image = new Image(500, 500); + + using (FileStream output = File.OpenWrite($"{path}/Simple_noantialias.png")) + { + image + .BackgroundColor(Color.Blue) + .DrawLines(Color.HotPink, 5, new[] { + new Vector2(10, 10), + new Vector2(200, 150), + new Vector2(50, 300) + }, new GraphicsOptions(false)) + .Save(output); + } + + using (var sourcePixels = image.Lock()) + { + Assert.Equal(Color.HotPink, sourcePixels[9, 9]); + + Assert.Equal(Color.HotPink, sourcePixels[199, 149]); + + Assert.Equal(Color.Blue, sourcePixels[50, 50]); + } + } [Fact] public void ImageShouldBeOverlayedByPathDashed() diff --git a/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs b/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs index 4f0f4ac9c0..9903e699af 100644 --- a/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs +++ b/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs @@ -46,6 +46,37 @@ namespace ImageSharp.Tests.Drawing } } + [Fact] + public void ImageShouldBeOverlayedByFilledPolygon_NoAntialias() + { + string path = CreateOutputDirectory("Drawing", "FilledPolygons"); + var simplePath = new[] { + new Vector2(10, 10), + new Vector2(200, 150), + new Vector2(50, 300) + }; + var image = new Image(500, 500); + + using (FileStream output = File.OpenWrite($"{path}/Simple_NoAntialias.png")) + { + image + .BackgroundColor(Color.Blue) + .FillPolygon(Color.HotPink, simplePath, new GraphicsOptions(false)) + .Save(output); + } + + using (var sourcePixels = image.Lock()) + { + Assert.Equal(Color.HotPink, sourcePixels[11, 11]); + + Assert.Equal(Color.HotPink, sourcePixels[200, 150]); + + Assert.Equal(Color.HotPink, sourcePixels[50, 50]); + + Assert.Equal(Color.Blue, sourcePixels[2, 2]); + } + } + [Fact] public void ImageShouldBeOverlayedByFilledPolygonOpacity() {