/// <inheritdoc/> protected override void OnApply(ImageBase <TPixel> source, Rectangle sourceRectangle) { int startX = sourceRectangle.X; int endX = sourceRectangle.Right; int startY = sourceRectangle.Y; int endY = sourceRectangle.Bottom; // Align start/end positions. int minX = Math.Max(0, startX); int maxX = Math.Min(source.Width, endX); int minY = Math.Max(0, startY); int maxY = Math.Min(source.Height, endY); // Reset offset if necessary. if (minX > 0) { startX = 0; } if (minY > 0) { startY = 0; } int width = maxX - minX; // we could possibly do some optermising by having knowledge about the individual brushes operate // for example If brush is SolidBrush<TPixel> then we could just get the color upfront // and skip using the IBrushApplicator<TPixel>?. using (PixelAccessor <TPixel> sourcePixels = source.Lock()) using (Buffer <float> amount = new Buffer <float>(width)) using (BrushApplicator <TPixel> applicator = this.brush.CreateApplicator(sourcePixels, sourceRectangle, this.options)) { for (int i = 0; i < width; i++) { amount[i] = this.options.BlendPercentage; } Parallel.For( minY, maxY, this.ParallelOptions, y => { int offsetY = y - startY; int offsetX = minX - startX; applicator.Apply(amount, offsetX, offsetY); }); } }
/// <inheritdoc/> protected override void OnApply(ImageBase <TPixel> source, Rectangle sourceRectangle) { 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; } ArrayPool <float> arrayPool = ArrayPool <float> .Shared; int maxIntersections = region.MaxIntersections; float subpixelCount = 4; if (this.Options.Antialias) { subpixelCount = this.Options.AntialiasSubpixelDepth; if (subpixelCount < 4) { subpixelCount = 4; } } using (PixelAccessor <TPixel> sourcePixels = source.Lock()) using (BrushApplicator <TPixel> applicator = this.Brush.CreateApplicator(sourcePixels, rect)) { float[] buffer = arrayPool.Rent(maxIntersections); int scanlineWidth = maxX - minX; float[] scanline = ArrayPool <float> .Shared.Rent(scanlineWidth); try { bool scanlineDirty = true; for (int y = minY; y < maxY; y++) { if (scanlineDirty) { // clear the buffer for (int x = 0; x < scanlineWidth; x++) { scanline[x] = 0; } scanlineDirty = false; } float subpixelFraction = 1f / subpixelCount; float subpixelFractionPoint = subpixelFraction / subpixelCount; for (float subPixel = (float)y; subPixel < y + 1; subPixel += subpixelFraction) { int pointsFound = region.Scan(subPixel, buffer, maxIntersections, 0); if (pointsFound == 0) { // nothing on this line skip continue; } QuickSort(buffer, 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); int endX = (int)MathF.Floor(scanEnd); 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 if (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, scanlineWidth, 0, minX, y); } } } finally { arrayPool.Return(buffer); ArrayPool <float> .Shared.Return(scanline); } } }