// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using System; using System.Runtime.CompilerServices; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Primitives; using SixLabors.ImageSharp.Processing.Drawing.Brushes; using SixLabors.ImageSharp.Processing.Processors; using SixLabors.Memory; using SixLabors.Primitives; namespace SixLabors.ImageSharp.Processing.Drawing.Processors { /// /// Using a brush and a shape fills shape with contents of brush the /// /// The type of the color. /// internal class FillRegionProcessor : ImageProcessor where TPixel : struct, IPixel { private const float AntialiasFactor = 1f; private const int DrawPadding = 1; /// /// Initializes a new instance of the class. /// /// The details how to fill the region of interest. /// The region of interest to be filled. /// The configuration options. public FillRegionProcessor(IBrush brush, Region region, GraphicsOptions options) { this.Region = region; this.Brush = brush; this.Options = options; } /// /// Gets the brush. /// public IBrush Brush { get; } /// /// Gets the region that this processor applies to. /// public Region Region { get; } /// /// Gets the options. /// /// /// The options. /// public GraphicsOptions Options { get; } /// protected override void OnFrameApply(ImageFrame source, Rectangle sourceRectangle, Configuration configuration) { Region region = this.Region; Rectangle rect = region.Bounds; // Align start/end positions. int minX = Math.Max(0, rect.Left); int maxX = Math.Min(source.Width, rect.Right); int minY = Math.Max(0, rect.Top); int maxY = Math.Min(source.Height, rect.Bottom); if (minX >= maxX) { return; // no effect inside image; } if (minY >= maxY) { return; // no effect inside image; } int maxIntersections = region.MaxIntersections; float subpixelCount = 4; // we need to offset the pixel grid to account for when we outline a path. // basically if the line is [1,2] => [3,2] then when outlining at 1 we end up with a region of [0.5,1.5],[1.5, 1.5],[3.5,2.5],[2.5,2.5] // and this can cause missed fills when not using antialiasing.so we offset the pixel grid by 0.5 in the x & y direction thus causing the# // region to alline with the pixel grid. float offset = 0.5f; if (this.Options.Antialias) { offset = 0f; // we are antialising skip offsetting as real antalising should take care of offset. subpixelCount = this.Options.AntialiasSubpixelDepth; if (subpixelCount < 4) { subpixelCount = 4; } } using (BrushApplicator applicator = this.Brush.CreateApplicator(source, rect, this.Options)) { int scanlineWidth = maxX - minX; using (IBuffer bBuffer = source.MemoryAllocator.Allocate(maxIntersections)) using (IBuffer bScanline = source.MemoryAllocator.Allocate(scanlineWidth)) { bool scanlineDirty = true; float subpixelFraction = 1f / subpixelCount; float subpixelFractionPoint = subpixelFraction / subpixelCount; Span buffer = bBuffer.GetSpan(); Span scanline = bScanline.GetSpan(); for (int y = minY; y < maxY; y++) { if (scanlineDirty) { scanline.Clear(); scanlineDirty = false; } float yPlusOne = y + 1; for (float subPixel = (float)y; subPixel < yPlusOne; subPixel += subpixelFraction) { int pointsFound = region.Scan(subPixel + offset, buffer, configuration); if (pointsFound == 0) { // nothing on this line skip continue; } QuickSort(buffer.Slice(0, pointsFound)); for (int point = 0; point < pointsFound; point += 2) { // points will be paired up float scanStart = buffer[point] - minX; float scanEnd = buffer[point + 1] - minX; int startX = (int)MathF.Floor(scanStart + offset); int endX = (int)MathF.Floor(scanEnd + offset); if (startX >= 0 && startX < scanline.Length) { for (float x = scanStart; x < startX + 1; x += subpixelFraction) { scanline[startX] += subpixelFractionPoint; scanlineDirty = true; } } if (endX >= 0 && endX < scanline.Length) { for (float x = endX; x < scanEnd; x += subpixelFraction) { scanline[endX] += subpixelFractionPoint; scanlineDirty = true; } } int nextX = startX + 1; endX = Math.Min(endX, scanline.Length); // reduce to end to the right edge nextX = Math.Max(nextX, 0); for (int x = nextX; x < endX; x++) { scanline[x] += subpixelFraction; scanlineDirty = true; } } } if (scanlineDirty) { if (!this.Options.Antialias) { for (int x = 0; x < scanlineWidth; x++) { if (scanline[x] >= 0.5) { scanline[x] = 1; } else { scanline[x] = 0; } } } applicator.Apply(scanline, minX, y); } } } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void Swap(ref float left, ref float right) { float tmp = left; left = right; right = tmp; } private static void QuickSort(Span data) { if (data.Length < 2) { return; } else if (data.Length == 2) { if (data[0] > data[1]) { Swap(ref data[0], ref data[1]); } return; } QuickSort(ref data[0], 0, data.Length - 1); } private static void QuickSort(ref float data0, int lo, int hi) { if (lo < hi) { int p = Partition(ref data0, lo, hi); QuickSort(ref data0, lo, p); QuickSort(ref data0, p + 1, hi); } } private static int Partition(ref float data0, int lo, int hi) { float pivot = Unsafe.Add(ref data0, lo); int i = lo - 1; int j = hi + 1; while (true) { do { i = i + 1; } while (Unsafe.Add(ref data0, i) < pivot && i < hi); do { j = j - 1; } while (Unsafe.Add(ref data0, j) > pivot && j > lo); if (i >= j) { return j; } Swap(ref Unsafe.Add(ref data0, i), ref Unsafe.Add(ref data0, j)); } } } }