diff --git a/samples/AvatarWithRoundedCorner/Program.cs b/samples/AvatarWithRoundedCorner/Program.cs index caed6687f4..4fa2912c1b 100644 --- a/samples/AvatarWithRoundedCorner/Program.cs +++ b/samples/AvatarWithRoundedCorner/Program.cs @@ -42,13 +42,13 @@ namespace AvatarWithRoundedCorner { Size = size, Mode = ImageSharp.Processing.ResizeMode.Crop - }).Run(i => ApplyRoundedCourners(i, cornerRadius)); + }).Apply(i => ApplyRoundedCorners(i, cornerRadius)); } // the combination of `IImageOperations.Run()` + this could be replaced with an `IImageProcessor` - public static void ApplyRoundedCourners(Image img, float cornerRadius) + public static void ApplyRoundedCorners(Image img, float cornerRadius) { - var corners = BuildCorners(img.Width, img.Height, cornerRadius); + IPathCollection corners = BuildCorners(img.Width, img.Height, cornerRadius); // mutating in here as we already have a cloned original img.Mutate(x => x.Fill(Rgba32.Transparent, corners, new GraphicsOptions(true) @@ -60,22 +60,22 @@ namespace AvatarWithRoundedCorner public static IPathCollection BuildCorners(int imageWidth, int imageHeight, float cornerRadius) { // first create a square - var rect = new SixLabors.Shapes.RectangularePolygon(-0.5f, -0.5f, cornerRadius, cornerRadius); + var rect = new RectangularePolygon(-0.5f, -0.5f, cornerRadius, cornerRadius); // then cut out of the square a circle so we are left with a corner - var cornerToptLeft = rect.Clip(new SixLabors.Shapes.EllipsePolygon(cornerRadius - 0.5f, cornerRadius - 0.5f, cornerRadius)); + IPath cornerToptLeft = rect.Clip(new EllipsePolygon(cornerRadius - 0.5f, cornerRadius - 0.5f, cornerRadius)); // corner is now a corner shape positions top left - //lets make 3 more positioned correctly, we cando that by translating the orgional artound the center of the image - var center = new Vector2(imageWidth / 2, imageHeight / 2); + //lets make 3 more positioned correctly, we can do that by translating the orgional artound the center of the image + var center = new Vector2(imageWidth / 2F, imageHeight / 2F); float rightPos = imageWidth - cornerToptLeft.Bounds.Width + 1; float bottomPos = imageHeight - cornerToptLeft.Bounds.Height + 1; // move it across the widthof the image - the width of the shape - var cornerTopRight = cornerToptLeft.RotateDegree(90).Translate(rightPos, 0); - var cornerBottomLeft = cornerToptLeft.RotateDegree(-90).Translate(0, bottomPos); - var cornerBottomRight = cornerToptLeft.RotateDegree(180).Translate(rightPos, bottomPos); + IPath cornerTopRight = cornerToptLeft.RotateDegree(90).Translate(rightPos, 0); + IPath cornerBottomLeft = cornerToptLeft.RotateDegree(-90).Translate(0, bottomPos); + IPath cornerBottomRight = cornerToptLeft.RotateDegree(180).Translate(rightPos, bottomPos); return new PathCollection(cornerToptLeft, cornerBottomLeft, cornerTopRight, cornerBottomRight); } diff --git a/src/ImageSharp/ApplyProcessors.cs b/src/ImageSharp/ApplyProcessors.cs index aa44dfc215..e60cff2e08 100644 --- a/src/ImageSharp/ApplyProcessors.cs +++ b/src/ImageSharp/ApplyProcessors.cs @@ -92,7 +92,7 @@ namespace ImageSharp /// The pixel format. /// The image to rotate, flip, or both. /// The operations to perform on the source. - /// returns the current optinoatins class to allow chaining of oprations. + /// returns the current operations class to allow chaining of operations. public static IImageProcessingContext ApplyProcessors(this IImageProcessingContext source, params IImageProcessor[] operations) where TPixel : struct, IPixel { @@ -104,4 +104,4 @@ namespace ImageSharp return source; } } -} +} \ No newline at end of file diff --git a/src/ImageSharp/DefaultInternalImageProcessorContext.cs b/src/ImageSharp/DefaultInternalImageProcessorContext.cs index 0562f90ce4..6165fbf004 100644 --- a/src/ImageSharp/DefaultInternalImageProcessorContext.cs +++ b/src/ImageSharp/DefaultInternalImageProcessorContext.cs @@ -5,14 +5,12 @@ namespace ImageSharp { - using System; - using System.Collections.Generic; using ImageSharp.PixelFormats; using ImageSharp.Processing; using SixLabors.Primitives; /// - /// The static collection of all the default image formats + /// Performs processor application operations on the source image /// /// The pixel format internal class DefaultInternalImageProcessorContext : IInternalImageProcessingContext @@ -42,7 +40,7 @@ namespace ImageSharp { if (!this.mutate && this.destination == null) { - // ensure we have cloned it if we are not mutating as we might have failed to register any Processors + // Ensure we have cloned it if we are not mutating as we might have failed to register any Processors this.destination = this.source.Clone(); } @@ -54,19 +52,17 @@ namespace ImageSharp { if (!this.mutate && this.destination == null) { - // this will only work if the first processor applied is the cloning one thus + // This will only work if the first processor applied is the cloning one thus // realistically for this optermissation to work the resize must the first processor // applied any only up processors will take the douple data path. - if (processor is ICloningImageProcessor) + var cloningImageProcessor = processor as ICloningImageProcessor; + if (cloningImageProcessor != null) { - var cloningProcessor = (ICloningImageProcessor)processor; - this.destination = cloningProcessor.CloneAndApply(this.source, rectangle); + this.destination = cloningImageProcessor.CloneAndApply(this.source, rectangle); return this; } - else - { - this.destination = this.source.Clone(); - } + + this.destination = this.source.Clone(); } processor.Apply(this.destination, rectangle); diff --git a/src/ImageSharp/IImageProcessingContextFactory.cs b/src/ImageSharp/IImageProcessingContextFactory.cs index a9cd23905a..394f198bf9 100644 --- a/src/ImageSharp/IImageProcessingContextFactory.cs +++ b/src/ImageSharp/IImageProcessingContextFactory.cs @@ -5,16 +5,15 @@ namespace ImageSharp { - using System; using ImageSharp.PixelFormats; /// - /// Represents an interface that will create IImageOperations + /// Represents an interface that will create IInternalImageProcessingContext instances /// internal interface IImageProcessingContextFactory { /// - /// Called during Mutate operations to generate the imageoperations provider. + /// Called during Mutate operations to generate the image operations provider. /// /// The pixel format /// The source image. diff --git a/src/ImageSharp/IImageProcessingContext{TPixel}.cs b/src/ImageSharp/IImageProcessingContext{TPixel}.cs index e79b20ba6b..a3f24186b6 100644 --- a/src/ImageSharp/IImageProcessingContext{TPixel}.cs +++ b/src/ImageSharp/IImageProcessingContext{TPixel}.cs @@ -5,8 +5,6 @@ namespace ImageSharp { - using System; - using ImageSharp.Formats; using ImageSharp.PixelFormats; using ImageSharp.Processing; using SixLabors.Primitives; @@ -19,18 +17,18 @@ namespace ImageSharp where TPixel : struct, IPixel { /// - /// Adds the processor to the current setr of image operations to be applied. + /// Adds the processor to the current set of image operations to be applied. /// /// The processor to apply /// The area to apply it to - /// returns the current optinoatins class to allow chaining of oprations. + /// The current operations class to allow chaining of operations. IImageProcessingContext ApplyProcessor(IImageProcessor processor, Rectangle rectangle); /// - /// Adds the processor to the current setr of image operations to be applied. + /// Adds the processor to the current set of image operations to be applied. /// /// The processor to apply - /// returns the current optinoatins class to allow chaining of oprations. + /// The current operations class to allow chaining of operations. IImageProcessingContext ApplyProcessor(IImageProcessor processor); } @@ -44,7 +42,7 @@ namespace ImageSharp /// /// Adds the processors to the current image /// - /// returns the current image or a new image depending on withere this is alloed to mutate the source image. + /// The current image or a new image depending on withere this is alloed to mutate the source image. Image Apply(); } } \ No newline at end of file diff --git a/src/ImageSharp/Processing/Delegate.cs b/src/ImageSharp/Processing/Delegate.cs index 733c4d090e..76f968dec6 100644 --- a/src/ImageSharp/Processing/Delegate.cs +++ b/src/ImageSharp/Processing/Delegate.cs @@ -17,13 +17,13 @@ namespace ImageSharp public static partial class ImageExtensions { /// - /// Queues up a simple operation that provides access to the mutatable image. + /// Queues up an operation that provides access to the mutatable image. /// /// The pixel format. /// The image to rotate, flip, or both. /// The operations to perform on the source. - /// returns the current optinoatins class to allow chaining of oprations. - public static IImageProcessingContext Run(this IImageProcessingContext source, Action> operation) + /// returns the current operations class to allow chaining of operations. + public static IImageProcessingContext Apply(this IImageProcessingContext source, Action> operation) where TPixel : struct, IPixel => source.ApplyProcessor(new DelegateProcessor(operation)); } diff --git a/src/ImageSharp/Processing/Processors/DelegateProcessor.cs b/src/ImageSharp/Processing/Processors/DelegateProcessor.cs index 756c3d8e4e..d594bf23ee 100644 --- a/src/ImageSharp/Processing/Processors/DelegateProcessor.cs +++ b/src/ImageSharp/Processing/Processors/DelegateProcessor.cs @@ -6,7 +6,6 @@ namespace ImageSharp.Processing { using System; - using System.Threading.Tasks; using ImageSharp.PixelFormats; using SixLabors.Primitives; @@ -37,13 +36,13 @@ namespace ImageSharp.Processing /// protected override void BeforeImageApply(Image source, Rectangle sourceRectangle) { - this.action?.Invoke((Image)source); + this.action?.Invoke(source); } /// protected override void OnApply(ImageBase source, Rectangle sourceRectangle) { - // no op, we did all we wanted to do inside BeforeImageApply + // NOP, we did all we wanted to do inside BeforeImageApply } } } \ No newline at end of file diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs index 65fbb65fb0..2853867460 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs @@ -48,14 +48,13 @@ namespace ImageSharp.Processing.Processors /// protected override Image CreateDestination(Image source, Rectangle sourceRectangle) { - // we will always be creating the clone even for mutate because thatsa the way this base processor works + // We will always be creating the clone even for mutate because thats the way this base processor works // ------------ - // for resize we know we are going to populate every pixel with fresh data and we want a different target size so - // lets manually clone an empty set of images at the correct target and then have the base class processs them in. - // turn. + // For resize we know we are going to populate every pixel with fresh data and we want a different target size so + // let's manually clone an empty set of images at the correct target and then have the base class processs them in turn. var image = new Image(source.Configuration, this.Width, this.Height, source.MetaData.Clone()); - // now 'clone' the ImageFrames + // Now 'clone' the ImageFrames foreach (ImageFrame sourceFrame in source.Frames) { var targetFrame = new ImageFrame(sourceFrame.Configuration, this.Width, this.Height, sourceFrame.MetaData.Clone()); @@ -120,7 +119,6 @@ namespace ImageSharp.Processing.Processors // A 2-pass 1D algorithm appears to be faster than splitting a 1-pass 2D algorithm // First process the columns. Since we are not using multiple threads startY and endY // are the upper and lower bounds of the source rectangle. - // TODO: Using a transposed variant of 'firstPassPixels' could eliminate the need for the WeightsWindow.ComputeWeightedColumnSum() method, and improve speed! using (var firstPassPixels = new Buffer2D(width, source.Height)) { diff --git a/src/ImageSharp/Processing/Transforms/Resize.cs b/src/ImageSharp/Processing/Transforms/Resize.cs index c73581a93a..38662317bf 100644 --- a/src/ImageSharp/Processing/Transforms/Resize.cs +++ b/src/ImageSharp/Processing/Transforms/Resize.cs @@ -27,10 +27,9 @@ namespace ImageSharp public static IImageProcessingContext Resize(this IImageProcessingContext source, ResizeOptions options) where TPixel : struct, IPixel { - return source.Run(img => + return source.Apply(img => { - // cheat and bound through a run, inside here we should just be mutating, this reallt needs moving over to a processor - + // Cheat and bound through a run, inside here we should just be mutating, this really needs moving over to a processor // Ensure size is populated across both dimensions. if (options.Size.Width == 0 && options.Size.Height > 0) { @@ -162,26 +161,26 @@ namespace ImageSharp public static IImageProcessingContext Resize(this IImageProcessingContext source, int width, int height, IResampler sampler, Rectangle sourceRectangle, Rectangle targetRectangle, bool compand) where TPixel : struct, IPixel { - return source.Run(img => - { - // todo : stop cheeting here and move this stuff into the processors itself - if (width == 0 && height > 0) - { - width = (int)MathF.Round(img.Width * height / (float)img.Height); - targetRectangle.Width = width; - } - - if (height == 0 && width > 0) - { - height = (int)MathF.Round(img.Height * width / (float)img.Width); - targetRectangle.Height = height; - } - - Guard.MustBeGreaterThan(width, 0, nameof(width)); - Guard.MustBeGreaterThan(height, 0, nameof(height)); - - img.Mutate(x => x.ApplyProcessor(new ResizeProcessor(sampler, width, height, targetRectangle) { Compand = compand }, sourceRectangle)); - }); + return source.Apply(img => + { + // TODO : Stop cheating here and move this stuff into the processors itself + if (width == 0 && height > 0) + { + width = (int)MathF.Round(img.Width * height / (float)img.Height); + targetRectangle.Width = width; + } + + if (height == 0 && width > 0) + { + height = (int)MathF.Round(img.Height * width / (float)img.Width); + targetRectangle.Height = height; + } + + Guard.MustBeGreaterThan(width, 0, nameof(width)); + Guard.MustBeGreaterThan(height, 0, nameof(height)); + + img.Mutate(x => x.ApplyProcessor(new ResizeProcessor(sampler, width, height, targetRectangle) { Compand = compand }, sourceRectangle)); + }); } /// @@ -202,9 +201,9 @@ namespace ImageSharp public static IImageProcessingContext Resize(this IImageProcessingContext source, int width, int height, IResampler sampler, Rectangle targetRectangle, bool compand) where TPixel : struct, IPixel { - return source.Run(img => + return source.Apply(img => { - // todo : stop cheeting here and move this stuff into the processors itself + // TODO : stop cheating here and move this stuff into the processors itself if (width == 0 && height > 0) { width = (int)MathF.Round(img.Width * height / (float)img.Height); @@ -224,4 +223,4 @@ namespace ImageSharp }); } } -} +} \ No newline at end of file diff --git a/src/ImageSharp/Quantizers/Quantize.cs b/src/ImageSharp/Quantizers/Quantize.cs index 0edc1557a5..612816eef2 100644 --- a/src/ImageSharp/Quantizers/Quantize.cs +++ b/src/ImageSharp/Quantizers/Quantize.cs @@ -57,13 +57,13 @@ namespace ImageSharp public static IImageProcessingContext Quantize(this IImageProcessingContext source, IQuantizer quantizer, int maxColors) where TPixel : struct, IPixel { - return source.Run(img => + return source.Apply(img => { // TODO : move helper logic into the processor QuantizedImage quantized = quantizer.Quantize(img, maxColors); int palleteCount = quantized.Palette.Length - 1; - using (PixelAccessor pixels = new PixelAccessor(quantized.Width, quantized.Height)) + using (var pixels = new PixelAccessor(quantized.Width, quantized.Height)) { Parallel.For( 0, diff --git a/tests/ImageSharp.Tests/Processing/DelegateTest.cs b/tests/ImageSharp.Tests/Processing/DelegateTest.cs index 73011bee1f..d5846b1589 100644 --- a/tests/ImageSharp.Tests/Processing/DelegateTest.cs +++ b/tests/ImageSharp.Tests/Processing/DelegateTest.cs @@ -16,7 +16,7 @@ namespace ImageSharp.Tests.Processing public void Run_CreatedDelegateProcessor() { Action> action = (i) => { }; - this.operations.Run(action); + this.operations.Apply(action); DelegateProcessor processor = this.Verify>(); Assert.Equal(action, processor.Action);