From ca513fd0701697879e7bf9c6a9714e0297a99cde Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 5 Feb 2017 23:34:47 +1100 Subject: [PATCH] A little cleanup --- src/ImageSharp.Drawing.Paths/DrawBeziers.cs | 4 +- src/ImageSharp.Drawing.Paths/DrawLines.cs | 2 +- src/ImageSharp.Drawing.Paths/DrawPath.cs | 4 +- src/ImageSharp.Drawing.Paths/DrawPolygon.cs | 2 +- src/ImageSharp.Drawing.Paths/DrawRectangle.cs | 3 - src/ImageSharp.Drawing.Paths/DrawShape.cs | 4 +- src/ImageSharp.Drawing.Paths/FillPaths.cs | 3 +- src/ImageSharp.Drawing.Paths/FillPolygon.cs | 1 - src/ImageSharp.Drawing.Paths/FillRectangle.cs | 1 - src/ImageSharp.Drawing.Paths/FillShape.cs | 5 +- .../RectangleExtensions.cs | 9 +-- src/ImageSharp.Drawing.Paths/ShapePath.cs | 24 ++---- src/ImageSharp.Drawing.Paths/ShapeRegion.cs | 19 +---- src/ImageSharp.Drawing/DrawPath.cs | 2 +- src/ImageSharp.Drawing/FillRegion.cs | 2 +- src/ImageSharp.Drawing/PointInfo.cs | 3 - .../Processors/DrawPathProcessor.cs | 73 +++++++++---------- src/ImageSharp.Drawing/Region.cs | 6 +- 18 files changed, 57 insertions(+), 110 deletions(-) diff --git a/src/ImageSharp.Drawing.Paths/DrawBeziers.cs b/src/ImageSharp.Drawing.Paths/DrawBeziers.cs index 1a511d84d7..ef11a3bac5 100644 --- a/src/ImageSharp.Drawing.Paths/DrawBeziers.cs +++ b/src/ImageSharp.Drawing.Paths/DrawBeziers.cs @@ -10,10 +10,8 @@ namespace ImageSharp using Drawing; using Drawing.Brushes; using Drawing.Pens; - using Drawing.Processors; - using SixLabors.Shapes; - using Path = SixLabors.Shapes.Path; + using SixLabors.Shapes; /// /// Extension methods for the type. diff --git a/src/ImageSharp.Drawing.Paths/DrawLines.cs b/src/ImageSharp.Drawing.Paths/DrawLines.cs index f6f8d8f6c9..fc046d6276 100644 --- a/src/ImageSharp.Drawing.Paths/DrawLines.cs +++ b/src/ImageSharp.Drawing.Paths/DrawLines.cs @@ -10,7 +10,7 @@ namespace ImageSharp using Drawing; using Drawing.Brushes; using Drawing.Pens; - using Drawing.Processors; + using SixLabors.Shapes; /// diff --git a/src/ImageSharp.Drawing.Paths/DrawPath.cs b/src/ImageSharp.Drawing.Paths/DrawPath.cs index 4e4275df6e..29b389fa24 100644 --- a/src/ImageSharp.Drawing.Paths/DrawPath.cs +++ b/src/ImageSharp.Drawing.Paths/DrawPath.cs @@ -6,11 +6,11 @@ namespace ImageSharp { using System; - using System.Numerics; + using Drawing; using Drawing.Brushes; using Drawing.Pens; - using Drawing.Processors; + using SixLabors.Shapes; /// diff --git a/src/ImageSharp.Drawing.Paths/DrawPolygon.cs b/src/ImageSharp.Drawing.Paths/DrawPolygon.cs index 28785e5cbf..6b3a6f1f36 100644 --- a/src/ImageSharp.Drawing.Paths/DrawPolygon.cs +++ b/src/ImageSharp.Drawing.Paths/DrawPolygon.cs @@ -10,7 +10,7 @@ namespace ImageSharp using Drawing; using Drawing.Brushes; using Drawing.Pens; - using Drawing.Processors; + using SixLabors.Shapes; /// diff --git a/src/ImageSharp.Drawing.Paths/DrawRectangle.cs b/src/ImageSharp.Drawing.Paths/DrawRectangle.cs index 4e8ec7135b..d42fee5dc1 100644 --- a/src/ImageSharp.Drawing.Paths/DrawRectangle.cs +++ b/src/ImageSharp.Drawing.Paths/DrawRectangle.cs @@ -10,9 +10,6 @@ namespace ImageSharp using Drawing; using Drawing.Brushes; using Drawing.Pens; - using Drawing.Processors; - - using SixLabors.Shapes; /// /// Extension methods for the type. diff --git a/src/ImageSharp.Drawing.Paths/DrawShape.cs b/src/ImageSharp.Drawing.Paths/DrawShape.cs index 6ddce65b98..748bb6a11d 100644 --- a/src/ImageSharp.Drawing.Paths/DrawShape.cs +++ b/src/ImageSharp.Drawing.Paths/DrawShape.cs @@ -6,11 +6,11 @@ namespace ImageSharp { using System; - using System.Numerics; + using Drawing; using Drawing.Brushes; using Drawing.Pens; - using Drawing.Processors; + using SixLabors.Shapes; /// diff --git a/src/ImageSharp.Drawing.Paths/FillPaths.cs b/src/ImageSharp.Drawing.Paths/FillPaths.cs index 09a799794c..1dd0b0a3a8 100644 --- a/src/ImageSharp.Drawing.Paths/FillPaths.cs +++ b/src/ImageSharp.Drawing.Paths/FillPaths.cs @@ -6,10 +6,9 @@ namespace ImageSharp { using System; - using System.Numerics; + using Drawing; using Drawing.Brushes; - using Drawing.Processors; using SixLabors.Shapes; diff --git a/src/ImageSharp.Drawing.Paths/FillPolygon.cs b/src/ImageSharp.Drawing.Paths/FillPolygon.cs index a609ceed24..b41267b9e2 100644 --- a/src/ImageSharp.Drawing.Paths/FillPolygon.cs +++ b/src/ImageSharp.Drawing.Paths/FillPolygon.cs @@ -9,7 +9,6 @@ namespace ImageSharp using System.Numerics; using Drawing; using Drawing.Brushes; - using Drawing.Processors; using SixLabors.Shapes; diff --git a/src/ImageSharp.Drawing.Paths/FillRectangle.cs b/src/ImageSharp.Drawing.Paths/FillRectangle.cs index 579a288a48..3b2cef6131 100644 --- a/src/ImageSharp.Drawing.Paths/FillRectangle.cs +++ b/src/ImageSharp.Drawing.Paths/FillRectangle.cs @@ -9,7 +9,6 @@ namespace ImageSharp using Drawing; using Drawing.Brushes; - using Drawing.Processors; /// /// Extension methods for the type. diff --git a/src/ImageSharp.Drawing.Paths/FillShape.cs b/src/ImageSharp.Drawing.Paths/FillShape.cs index 52a3c60dd5..f143e75b72 100644 --- a/src/ImageSharp.Drawing.Paths/FillShape.cs +++ b/src/ImageSharp.Drawing.Paths/FillShape.cs @@ -6,10 +6,9 @@ namespace ImageSharp { using System; - using System.Numerics; + using Drawing; using Drawing.Brushes; - using Drawing.Processors; using SixLabors.Shapes; @@ -19,7 +18,7 @@ namespace ImageSharp public static partial class ImageExtensions { /// - /// Flood fills the image in the shape of the provided polygon with the specified brush.. + /// Flood fills the image in the shape of the provided polygon with the specified brush. /// /// The type of the color. /// The image this method extends. diff --git a/src/ImageSharp.Drawing.Paths/RectangleExtensions.cs b/src/ImageSharp.Drawing.Paths/RectangleExtensions.cs index 1faa6469ad..2fa5fe43f2 100644 --- a/src/ImageSharp.Drawing.Paths/RectangleExtensions.cs +++ b/src/ImageSharp.Drawing.Paths/RectangleExtensions.cs @@ -3,16 +3,9 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp.Drawing.Processors +namespace ImageSharp.Drawing { using System; - using System.Buffers; - using System.Numerics; - using System.Threading.Tasks; - using Drawing; - using ImageSharp.Processing; - using SixLabors.Shapes; - using Rectangle = ImageSharp.Rectangle; /// /// Extension methods for helping to bridge Shaper2D and ImageSharp primitives. diff --git a/src/ImageSharp.Drawing.Paths/ShapePath.cs b/src/ImageSharp.Drawing.Paths/ShapePath.cs index 34e77b8074..5ea38a365b 100644 --- a/src/ImageSharp.Drawing.Paths/ShapePath.cs +++ b/src/ImageSharp.Drawing.Paths/ShapePath.cs @@ -9,16 +9,14 @@ namespace ImageSharp.Drawing using System.Collections.Immutable; using System.Numerics; - using ImageSharp.Drawing.Processors; - using SixLabors.Shapes; using Rectangle = ImageSharp.Rectangle; /// - /// A drawable mapping between a / and a drawable/fillable region. + /// A drawable mapping between a / and a drawable/fillable region. /// - internal class ShapePath : ImageSharp.Drawing.Drawable + internal class ShapePath : Drawable { /// /// The fillable shape @@ -59,9 +57,6 @@ namespace ImageSharp.Drawing /// /// Gets the drawable paths /// - /// - /// The paths. - /// public ImmutableArray Paths { get; } /// @@ -78,12 +73,7 @@ namespace ImageSharp.Drawing Vector2[] innerbuffer = ArrayPool.Shared.Rent(length); try { - int count = this.shape.FindIntersections( - start, - end, - innerbuffer, - length, - 0); + int count = this.shape.FindIntersections(start, end, innerbuffer, length, 0); for (int i = 0; i < count; i++) { @@ -106,12 +96,7 @@ namespace ImageSharp.Drawing Vector2[] innerbuffer = ArrayPool.Shared.Rent(length); try { - int count = this.shape.FindIntersections( - start, - end, - innerbuffer, - length, - 0); + int count = this.shape.FindIntersections(start, end, innerbuffer, length, 0); for (int i = 0; i < count; i++) { @@ -133,6 +118,7 @@ namespace ImageSharp.Drawing float distanceFromPath = float.MaxValue; float distanceAlongPath = 0; + // ReSharper disable once ForCanBeConvertedToForeach for (int i = 0; i < this.Paths.Length; i++) { SixLabors.Shapes.PointInfo p = this.Paths[i].Distance(point); diff --git a/src/ImageSharp.Drawing.Paths/ShapeRegion.cs b/src/ImageSharp.Drawing.Paths/ShapeRegion.cs index 5adec67908..dc035e3b05 100644 --- a/src/ImageSharp.Drawing.Paths/ShapeRegion.cs +++ b/src/ImageSharp.Drawing.Paths/ShapeRegion.cs @@ -6,17 +6,14 @@ namespace ImageSharp.Drawing { using System.Buffers; - using System.Collections.Immutable; using System.Numerics; - using ImageSharp.Drawing.Processors; - using SixLabors.Shapes; using Rectangle = ImageSharp.Rectangle; /// - /// A drawable mapping between a / and a drawable/fillable region. + /// A drawable mapping between a / and a drawable/fillable region. /// internal class ShapeRegion : Region { @@ -58,12 +55,7 @@ namespace ImageSharp.Drawing Vector2[] innerbuffer = ArrayPool.Shared.Rent(length); try { - int count = this.Shape.FindIntersections( - start, - end, - innerbuffer, - length, - 0); + int count = this.Shape.FindIntersections(start, end, innerbuffer, length, 0); for (int i = 0; i < count; i++) { @@ -86,12 +78,7 @@ namespace ImageSharp.Drawing Vector2[] innerbuffer = ArrayPool.Shared.Rent(length); try { - int count = this.Shape.FindIntersections( - start, - end, - innerbuffer, - length, - 0); + int count = this.Shape.FindIntersections(start, end, innerbuffer, length, 0); for (int i = 0; i < count; i++) { diff --git a/src/ImageSharp.Drawing/DrawPath.cs b/src/ImageSharp.Drawing/DrawPath.cs index d92731270f..75a0d81575 100644 --- a/src/ImageSharp.Drawing/DrawPath.cs +++ b/src/ImageSharp.Drawing/DrawPath.cs @@ -6,7 +6,7 @@ namespace ImageSharp { using System; - using System.Numerics; + using Drawing; using Drawing.Brushes; using Drawing.Pens; diff --git a/src/ImageSharp.Drawing/FillRegion.cs b/src/ImageSharp.Drawing/FillRegion.cs index fbbf659d1c..6faf519af1 100644 --- a/src/ImageSharp.Drawing/FillRegion.cs +++ b/src/ImageSharp.Drawing/FillRegion.cs @@ -6,7 +6,7 @@ namespace ImageSharp { using System; - using System.Numerics; + using Drawing; using Drawing.Brushes; using Drawing.Processors; diff --git a/src/ImageSharp.Drawing/PointInfo.cs b/src/ImageSharp.Drawing/PointInfo.cs index e222a81469..7eff24fac8 100644 --- a/src/ImageSharp.Drawing/PointInfo.cs +++ b/src/ImageSharp.Drawing/PointInfo.cs @@ -5,9 +5,6 @@ namespace ImageSharp.Drawing { - using System; - using System.Numerics; - /// /// Returns details about how far away from the inside of a shape and the color the pixel could be. /// diff --git a/src/ImageSharp.Drawing/Processors/DrawPathProcessor.cs b/src/ImageSharp.Drawing/Processors/DrawPathProcessor.cs index b4aa7fba5a..83ae9521cb 100644 --- a/src/ImageSharp.Drawing/Processors/DrawPathProcessor.cs +++ b/src/ImageSharp.Drawing/Processors/DrawPathProcessor.cs @@ -6,13 +6,11 @@ namespace ImageSharp.Drawing.Processors { using System; - using System.Collections.Generic; - using System.Linq; using System.Numerics; using System.Threading.Tasks; + using ImageSharp.Processing; using Pens; - using Rectangle = ImageSharp.Rectangle; /// /// Draws a path using the processor pipeline @@ -39,27 +37,18 @@ namespace ImageSharp.Drawing.Processors } /// - /// Gets the options. + /// Gets the graphics options. /// - /// - /// The options. - /// public GraphicsOptions Options { get; } /// /// Gets the pen. /// - /// - /// The pen. - /// public IPen Pen { get; } /// /// Gets the path. /// - /// - /// The path. - /// public Drawable Path { get; } /// @@ -68,7 +57,7 @@ namespace ImageSharp.Drawing.Processors using (PixelAccessor sourcePixels = source.Lock()) using (PenApplicator applicator = this.Pen.CreateApplicator(sourcePixels, this.Path.Bounds)) { - var rect = RectangleF.Ceiling(applicator.RequiredRegion); + Rectangle rect = RectangleF.Ceiling(applicator.RequiredRegion); int polyStartY = rect.Y - PaddingFactor; int polyEndY = rect.Bottom + PaddingFactor; @@ -98,49 +87,53 @@ namespace ImageSharp.Drawing.Processors } Parallel.For( - minY, - maxY, - this.ParallelOptions, - (int y) => - { - int offsetY = y - polyStartY; - - for (int x = minX; x < maxX; x++) + minY, + maxY, + this.ParallelOptions, + y => { - // TODO add find intersections code to skip and scan large regions of this. - int offsetX = x - startX; - var info = this.Path.GetPointInfo(offsetX, offsetY); + int offsetY = y - polyStartY; - var color = applicator.GetColor(offsetX, offsetY, info); + for (int x = minX; x < maxX; x++) + { + // TODO add find intersections code to skip and scan large regions of this. + int offsetX = x - startX; + PointInfo info = this.Path.GetPointInfo(offsetX, offsetY); - var opacity = this.Opacity(color.DistanceFromElement); + ColoredPointInfo color = applicator.GetColor(offsetX, offsetY, info); - if (opacity > Constants.Epsilon) - { - int offsetColorX = x - minX; + float opacity = this.Opacity(color.DistanceFromElement); - Vector4 backgroundVector = sourcePixels[offsetX, offsetY].ToVector4(); - Vector4 sourceVector = color.Color.ToVector4(); + if (opacity > Constants.Epsilon) + { + Vector4 backgroundVector = sourcePixels[offsetX, offsetY].ToVector4(); + Vector4 sourceVector = color.Color.ToVector4(); - var finalColor = Vector4BlendTransforms.PremultipliedLerp(backgroundVector, sourceVector, opacity); - finalColor.W = backgroundVector.W; + Vector4 finalColor = Vector4BlendTransforms.PremultipliedLerp(backgroundVector, sourceVector, opacity); + finalColor.W = backgroundVector.W; - TColor packed = default(TColor); - packed.PackFromVector4(finalColor); - sourcePixels[offsetX, offsetY] = packed; + TColor packed = default(TColor); + packed.PackFromVector4(finalColor); + sourcePixels[offsetX, offsetY] = packed; + } } - } - }); + }); } } + /// + /// Returns the correct opacity for the given distance. + /// + /// Thw distance from the central point. + /// The private float Opacity(float distance) { if (distance <= 0) { return 1; } - else if (this.Options.Antialias && distance < AntialiasFactor) + + if (this.Options.Antialias && distance < AntialiasFactor) { return 1 - (distance / AntialiasFactor); } diff --git a/src/ImageSharp.Drawing/Region.cs b/src/ImageSharp.Drawing/Region.cs index 81f3dca5bd..fe1dc52221 100644 --- a/src/ImageSharp.Drawing/Region.cs +++ b/src/ImageSharp.Drawing/Region.cs @@ -16,17 +16,17 @@ namespace ImageSharp.Drawing public abstract int MaxIntersections { get; } /// - /// Gets the bounding box that entirly surrounds this region. + /// Gets the bounding box that entirely surrounds this region. /// /// - /// This should always contains all possible points returned from eather or . + /// This should always contains all possible points returned from either or . /// public abstract Rectangle Bounds { get; } /// /// Scans the X axis for intersections. /// - /// The position along the X axies to find intersections. + /// The position along the X axis to find intersections. /// The buffer. /// The length. /// The offset.