diff --git a/src/ImageSharp.Drawing/Processing/FillRectangleExtensions.cs b/src/ImageSharp.Drawing/Processing/FillRectangleExtensions.cs index 26bf214f71..f2aafaefee 100644 --- a/src/ImageSharp.Drawing/Processing/FillRectangleExtensions.cs +++ b/src/ImageSharp.Drawing/Processing/FillRectangleExtensions.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing.Processors.Drawing; using SixLabors.Primitives; using SixLabors.Shapes; @@ -38,7 +39,7 @@ public static IImageProcessingContext Fill(this IImageProcessing => source.Fill(brush, new RectangularPolygon(shape.X, shape.Y, shape.Width, shape.Height)); /// - /// Flood fills the image in the shape of the provided rectangle with the specified brush. + /// Flood fills the image in the shape of the provided rectangle with the specified color. /// /// The type of the color. /// The image this method extends. @@ -51,7 +52,7 @@ public static IImageProcessingContext Fill(this IImageProcessing => source.Fill(options, new SolidBrush(color), shape); /// - /// Flood fills the image in the shape of the provided rectangle with the specified brush. + /// Flood fills the image in the shape of the provided rectangle with the specified color. /// /// The type of the color. /// The image this method extends. @@ -61,5 +62,55 @@ public static IImageProcessingContext Fill(this IImageProcessing public static IImageProcessingContext Fill(this IImageProcessingContext source, TPixel color, RectangleF shape) where TPixel : struct, IPixel => source.Fill(new SolidBrush(color), shape); + + /// + /// Fills the image in the shape of the provided rectangle with the specified color. + /// + /// The type of the color. + /// The image this method extends. + /// The color. + /// The rectangle shape. + /// The . + public static IImageProcessingContext Fill(this IImageProcessingContext source, TPixel color, Rectangle rectangle) + where TPixel : struct, IPixel + => source.Fill(GraphicsOptions.Default, new SolidBrush(color), rectangle); + + /// + /// Fills the image in the shape of the provided rectangle with the specified brush. + /// + /// The type of the color. + /// The image this method extends. + /// The details how to fill the region of interest. + /// The rectangle shape. + /// The . + public static IImageProcessingContext Fill(this IImageProcessingContext source, IBrush brush, Rectangle rectangle) + where TPixel : struct, IPixel + => source.Fill(GraphicsOptions.Default, brush, rectangle); + + /// + /// Fills the image in the shape of the provided rectangle with the specified color. + /// + /// The type of the color. + /// The image this method extends. + /// The graphics options. + /// The color. + /// The rectangle shape. + /// The . + public static IImageProcessingContext Fill(this IImageProcessingContext source, GraphicsOptions options, TPixel color, Rectangle rectangle) + where TPixel : struct, IPixel + => source.Fill(options, new SolidBrush(color), rectangle); + + /// + /// Fills the image in the shape of the provided rectangle with the specified brush. + /// + /// The type of the color. + /// The image this method extends. + /// The graphics options. + /// The details how to fill the region of interest. + /// The rectangle shape. + /// The . + public static IImageProcessingContext Fill(this IImageProcessingContext source, GraphicsOptions options, IBrush brush, Rectangle rectangle) + where TPixel : struct, IPixel + => source.ApplyProcessor(new FillProcessor(brush, options), rectangle); } } \ No newline at end of file diff --git a/src/ImageSharp.Drawing/Processing/Processors/Drawing/FillProcessor.cs b/src/ImageSharp.Drawing/Processing/Processors/Drawing/FillProcessor.cs index ed6c869511..9708dcf2f9 100644 --- a/src/ImageSharp.Drawing/Processing/Processors/Drawing/FillProcessor.cs +++ b/src/ImageSharp.Drawing/Processing/Processors/Drawing/FillProcessor.cs @@ -3,12 +3,10 @@ using System; using System.Buffers; -using System.Threading.Tasks; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.ParallelUtils; using SixLabors.ImageSharp.PixelFormats; -using SixLabors.Memory; using SixLabors.Primitives; namespace SixLabors.ImageSharp.Processing.Processors.Drawing @@ -20,23 +18,27 @@ namespace SixLabors.ImageSharp.Processing.Processors.Drawing internal class FillProcessor : ImageProcessor where TPixel : struct, IPixel { - /// - /// The brush. - /// - private readonly IBrush brush; - private readonly GraphicsOptions options; - - /// + /// /// Initializes a new instance of the class. /// /// The brush to source pixel colors from. /// The options public FillProcessor(IBrush brush, GraphicsOptions options) { - this.brush = brush; - this.options = options; + this.Brush = brush; + this.Options = options; } + /// + /// Gets the brush. + /// + public IBrush Brush { get; } + + /// + /// Gets the options. + /// + public GraphicsOptions Options { get; } + /// protected override void OnFrameApply(ImageFrame source, Rectangle sourceRectangle, Configuration configuration) { @@ -85,10 +87,10 @@ protected override void OnFrameApply(ImageFrame source, Rectangle source } using (IMemoryOwner amount = source.MemoryAllocator.Allocate(width)) - using (BrushApplicator applicator = this.brush.CreateApplicator( + using (BrushApplicator applicator = this.Brush.CreateApplicator( source, sourceRectangle, - this.options)) + this.Options)) { amount.GetSpan().Fill(1f); @@ -111,14 +113,14 @@ protected override void OnFrameApply(ImageFrame source, Rectangle source private bool IsSolidBrushWithoutBlending(out SolidBrush solidBrush) { - solidBrush = this.brush as SolidBrush; + solidBrush = this.Brush as SolidBrush; if (solidBrush == null) { return false; } - return this.options.IsOpaqueColorWithoutBlending(solidBrush.Color); + return this.Options.IsOpaqueColorWithoutBlending(solidBrush.Color); } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Drawing/Paths/FillRectangle.cs b/tests/ImageSharp.Tests/Drawing/Paths/FillRectangle.cs index 8f648e425f..0b05cbd459 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/FillRectangle.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/FillRectangle.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. using SixLabors.ImageSharp.PixelFormats; -using SixLabors.ImageSharp.Primitives; using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing.Processors.Drawing; using Xunit; @@ -11,83 +10,47 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Paths { public class FillRectangle : BaseImageOperationsExtensionTest { - GraphicsOptions noneDefault = new GraphicsOptions(); - Rgba32 color = Rgba32.HotPink; - SolidBrush brush = Brushes.Solid(Rgba32.HotPink); - SixLabors.Primitives.Rectangle rectangle = new SixLabors.Primitives.Rectangle(10, 10, 77, 76); + private readonly GraphicsOptions noneDefault = new GraphicsOptions(); + private readonly Rgba32 colorForTesting = Rgba32.HotPink; + private readonly SolidBrush brushForTesting = Brushes.Solid(Rgba32.HotPink); + private readonly SixLabors.Primitives.Rectangle rectangleForTesting = new SixLabors.Primitives.Rectangle(10, 10, 77, 76); [Fact] public void CorrectlySetsBrushAndRectangle() { - this.operations.Fill(this.brush, this.rectangle); - FillRegionProcessor processor = this.Verify>(); - + this.operations.Fill(this.brushForTesting, this.rectangleForTesting); + FillProcessor processor = this.Verify>(); Assert.Equal(GraphicsOptions.Default, processor.Options); - - ShapeRegion region = Assert.IsType(processor.Region); - Shapes.RectangularPolygon rect = Assert.IsType(region.Shape); - Assert.Equal(rect.Location.X, this.rectangle.X); - Assert.Equal(rect.Location.Y, this.rectangle.Y); - Assert.Equal(rect.Size.Width, this.rectangle.Width); - Assert.Equal(rect.Size.Height, this.rectangle.Height); - - Assert.Equal(this.brush, processor.Brush); + Assert.Equal(this.brushForTesting, processor.Brush); } [Fact] public void CorrectlySetsBrushRectangleAndOptions() { - this.operations.Fill(this.noneDefault, this.brush, this.rectangle); - FillRegionProcessor processor = this.Verify>(); - + this.operations.Fill(this.noneDefault, this.brushForTesting, this.rectangleForTesting); + FillProcessor processor = this.Verify>(); Assert.Equal(this.noneDefault, processor.Options); - - ShapeRegion region = Assert.IsType(processor.Region); - Shapes.RectangularPolygon rect = Assert.IsType(region.Shape); - Assert.Equal(rect.Location.X, this.rectangle.X); - Assert.Equal(rect.Location.Y, this.rectangle.Y); - Assert.Equal(rect.Size.Width, this.rectangle.Width); - Assert.Equal(rect.Size.Height, this.rectangle.Height); - - Assert.Equal(this.brush, processor.Brush); + Assert.Equal(this.brushForTesting, processor.Brush); } [Fact] public void CorrectlySetsColorAndRectangle() { - this.operations.Fill(this.color, this.rectangle); - FillRegionProcessor processor = this.Verify>(); - + this.operations.Fill(this.colorForTesting, this.rectangleForTesting); + FillProcessor processor = this.Verify>(); Assert.Equal(GraphicsOptions.Default, processor.Options); - - ShapeRegion region = Assert.IsType(processor.Region); - Shapes.RectangularPolygon rect = Assert.IsType(region.Shape); - Assert.Equal(rect.Location.X, this.rectangle.X); - Assert.Equal(rect.Location.Y, this.rectangle.Y); - Assert.Equal(rect.Size.Width, this.rectangle.Width); - Assert.Equal(rect.Size.Height, this.rectangle.Height); - SolidBrush brush = Assert.IsType>(processor.Brush); - Assert.Equal(this.color, brush.Color); + Assert.Equal(this.colorForTesting, brush.Color); } [Fact] public void CorrectlySetsColorRectangleAndOptions() { - this.operations.Fill(this.noneDefault, this.color, this.rectangle); - FillRegionProcessor processor = this.Verify>(); - + this.operations.Fill(this.noneDefault, this.colorForTesting, this.rectangleForTesting); + FillProcessor processor = this.Verify>(); Assert.Equal(this.noneDefault, processor.Options); - - ShapeRegion region = Assert.IsType(processor.Region); - Shapes.RectangularPolygon rect = Assert.IsType(region.Shape); - Assert.Equal(rect.Location.X, this.rectangle.X); - Assert.Equal(rect.Location.Y, this.rectangle.Y); - Assert.Equal(rect.Size.Width, this.rectangle.Width); - Assert.Equal(rect.Size.Height, this.rectangle.Height); - SolidBrush brush = Assert.IsType>(processor.Brush); - Assert.Equal(this.color, brush.Color); + Assert.Equal(this.colorForTesting, brush.Color); } } }