// // 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.Processing; using Pens; /// /// Draws a path using the processor pipeline /// /// The type of the color. /// internal class DrawPathProcessor : ImageProcessor where TColor : 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)) { 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; } Parallel.For( minY, maxY, this.ParallelOptions, y => { int offsetY = y - polyStartY; 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); ColoredPointInfo color = applicator.GetColor(offsetX, offsetY, info); float opacity = this.Opacity(color.DistanceFromElement); if (opacity > Constants.Epsilon) { Vector4 backgroundVector = sourcePixels[offsetX, offsetY].ToVector4(); Vector4 sourceVector = color.Color.ToVector4(); Vector4 finalColor = Vector4BlendTransforms.PremultipliedLerp(backgroundVector, sourceVector, opacity); 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; } if (this.Options.Antialias && distance < AntialiasFactor) { return 1 - (distance / AntialiasFactor); } return 0; } } }