// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // namespace ImageSharp.Drawing.Processors { using System; using System.Numerics; using System.Threading.Tasks; using ImageSharp.Memory; using ImageSharp.PixelFormats; using ImageSharp.Processing; using Pens; /// /// Draws a path using the processor pipeline /// /// The type of the color. /// internal class DrawPathProcessor : ImageProcessor where TPixel : struct, IPixel { private const float AntialiasFactor = 1f; private const int PaddingFactor = 1; // needs to been the same or greater than AntialiasFactor /// /// Initializes a new instance of the class. /// /// The details how to draw the outline/path. /// The details of the paths and outlines to draw. /// The drawing configuration options. public DrawPathProcessor(IPen pen, Drawable drawable, GraphicsOptions options) { this.Path = drawable; this.Pen = pen; this.Options = options; } /// /// Gets the graphics options. /// public GraphicsOptions Options { get; } /// /// Gets the pen. /// public IPen Pen { get; } /// /// Gets the path. /// public Drawable Path { get; } /// protected override void OnApply(ImageBase source, Rectangle sourceRectangle) { using (PixelAccessor sourcePixels = source.Lock()) using (PenApplicator applicator = this.Pen.CreateApplicator(sourcePixels, this.Path.Bounds, this.Options)) { Rectangle rect = RectangleF.Ceiling(applicator.RequiredRegion); int polyStartY = rect.Y - PaddingFactor; int polyEndY = rect.Bottom + PaddingFactor; int startX = rect.X - PaddingFactor; int endX = rect.Right + PaddingFactor; int minX = Math.Max(sourceRectangle.Left, startX); int maxX = Math.Min(sourceRectangle.Right, endX); int minY = Math.Max(sourceRectangle.Top, polyStartY); int maxY = Math.Min(sourceRectangle.Bottom, polyEndY); // Align start/end positions. minX = Math.Max(0, minX); maxX = Math.Min(source.Width, maxX); minY = Math.Max(0, minY); maxY = Math.Min(source.Height, maxY); // Reset offset if necessary. if (minX > 0) { startX = 0; } if (minY > 0) { polyStartY = 0; } int width = maxX - minX; PixelBlender blender = PixelOperations.Instance.GetPixelBlender(this.Options.BlenderMode); Parallel.For( minY, maxY, this.ParallelOptions, y => { int offsetY = y - polyStartY; using (Buffer amount = new Buffer(width)) using (Buffer colors = new Buffer(width)) { for (int i = 0; i < width; i++) { int x = i + minX; int offsetX = x - startX; PointInfo info = this.Path.GetPointInfo(offsetX, offsetY); ColoredPointInfo color = applicator.GetColor(offsetX, offsetY, info); amount[i] = (this.Opacity(color.DistanceFromElement) * this.Options.BlendPercentage).Clamp(0, 1); colors[i] = color.Color; } Span destination = sourcePixels.GetRowSpan(offsetY).Slice(minX - startX, width); blender.Blend(destination, destination, colors, amount); } }); } } /// /// 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; } if (this.Options.Antialias && distance < AntialiasFactor) { return 1 - (distance / AntialiasFactor); } return 0; } } }