From c040582df6ba3abaf10269decd7ef3f9eeebb18a Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 8 Dec 2016 22:56:49 +1100 Subject: [PATCH] Update Crop method to accept rectangle --- .../Processors/Transforms/CropProcessor.cs | 37 +++++++++---------- src/ImageSharp/Filters/Transforms/Crop.cs | 30 ++++----------- 2 files changed, 24 insertions(+), 43 deletions(-) diff --git a/src/ImageSharp/Filters/Processors/Transforms/CropProcessor.cs b/src/ImageSharp/Filters/Processors/Transforms/CropProcessor.cs index 789538b2c..68c196113 100644 --- a/src/ImageSharp/Filters/Processors/Transforms/CropProcessor.cs +++ b/src/ImageSharp/Filters/Processors/Transforms/CropProcessor.cs @@ -5,6 +5,7 @@ namespace ImageSharp.Processors { + using System; using System.Threading.Tasks; /// @@ -19,18 +20,16 @@ namespace ImageSharp.Processors /// /// Initializes a new instance of the class. /// - /// The target image width. - /// The target image height. - public CropProcessor(int width, int height) + /// The target cropped rectangle. + public CropProcessor(Rectangle cropRectangle) { - this.Width = width; - this.Height = height; + this.CropRectangle = cropRectangle; } /// /// Gets the width. /// - public int Width { get; } + public Rectangle CropRectangle { get; } /// /// Gets the height. @@ -40,22 +39,20 @@ namespace ImageSharp.Processors /// protected override void Apply(ImageBase source, Rectangle sourceRectangle, int startY, int endY) { - int minX = 0; - int maxX = this.Width; - int minY = 0; - int maxY = this.Height; - int sourceX = sourceRectangle.X; - int sourceY = sourceRectangle.Y; + if (this.CropRectangle == sourceRectangle) + { + return; + } - Guard.MustBeGreaterThanOrEqualTo(minX, sourceX, nameof(minX)); - Guard.MustBeGreaterThanOrEqualTo(minY, startY, nameof(startY)); - Guard.MustBeLessThanOrEqualTo(maxX, sourceRectangle.Right, nameof(maxX)); - Guard.MustBeLessThanOrEqualTo(maxY, endY, nameof(maxY)); + int minY = Math.Max(this.CropRectangle.Y, startY); + int maxY = Math.Min(this.CropRectangle.Bottom, endY); + int minX = Math.Max(this.CropRectangle.X, sourceRectangle.X); + int maxX = Math.Min(this.CropRectangle.Right, sourceRectangle.Right); - TColor[] target = new TColor[this.Width * this.Height]; + TColor[] target = new TColor[this.CropRectangle.Width * this.CropRectangle.Height]; using (PixelAccessor sourcePixels = source.Lock()) - using (PixelAccessor targetPixels = target.Lock(this.Width, this.Height)) + using (PixelAccessor targetPixels = target.Lock(this.CropRectangle.Width, this.CropRectangle.Height)) { Parallel.For( minY, @@ -65,12 +62,12 @@ namespace ImageSharp.Processors { for (int x = minX; x < maxX; x++) { - targetPixels[x, y] = sourcePixels[x + sourceX, y + sourceY]; + targetPixels[x - minX, y - minY] = sourcePixels[x, y]; } }); } - source.SetPixels(this.Width, this.Height, target); + source.SetPixels(this.CropRectangle.Width, this.CropRectangle.Height, target); } } } \ No newline at end of file diff --git a/src/ImageSharp/Filters/Transforms/Crop.cs b/src/ImageSharp/Filters/Transforms/Crop.cs index 95091358c..d0c8ab8d8 100644 --- a/src/ImageSharp/Filters/Transforms/Crop.cs +++ b/src/ImageSharp/Filters/Transforms/Crop.cs @@ -25,41 +25,25 @@ namespace ImageSharp where TColor : struct, IPackedPixel where TPacked : struct { - return Crop(source, width, height, source.Bounds); + return Crop(source, new Rectangle(0, 0, width, height)); } /// - /// Crops an image to the given width and height with the given source rectangle. - /// - /// If the source rectangle is smaller than the target dimensions then the - /// area within the source is resized performing a zoomed crop. - /// + /// Crops an image to the given rectangle. /// /// The pixel format. /// The packed format. uint, long, float. /// The image to crop. - /// The target image width. - /// The target image height. - /// - /// The structure that specifies the portion of the image object to draw. + /// + /// The structure that specifies the portion of the image object to retain. /// /// The - public static Image Crop(this Image source, int width, int height, Rectangle sourceRectangle) + public static Image Crop(this Image source, Rectangle cropRectangle) where TColor : struct, IPackedPixel where TPacked : struct { - Guard.MustBeGreaterThan(width, 0, nameof(width)); - Guard.MustBeGreaterThan(height, 0, nameof(height)); - - if (sourceRectangle.Width < width || sourceRectangle.Height < height) - { - // If the source rectangle is smaller than the target perform a - // cropped zoom. - source = source.Resize(sourceRectangle.Width, sourceRectangle.Height); - } - - CropProcessor processor = new CropProcessor(width, height); - return source.Process(sourceRectangle, processor); + CropProcessor processor = new CropProcessor(cropRectangle); + return source.Process(source.Bounds, processor); } } } \ No newline at end of file