From bd826f170329abfbc91fa9bb8074fb9dbb621099 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sat, 9 May 2020 14:03:16 +0100 Subject: [PATCH] Ensure min dimension of 1px following rounding. Fix #1195 --- .../Transforms/Resize/ResizeHelper.cs | 19 ++++++++++++------- .../Processors/Transforms/ResizeTests.cs | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeHelper.cs b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeHelper.cs index 6066efd1c..6fd87b3f8 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeHelper.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeHelper.cs @@ -72,7 +72,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms // case ResizeMode.Stretch: default: - return (new Size(width, height), new Rectangle(0, 0, width, height)); + return (new Size(Sanitize(width), Sanitize(height)), new Rectangle(0, 0, width, height)); } } @@ -143,7 +143,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms } // Target image width and height can be different to the rectangle width and height. - return (new Size(width, height), new Rectangle(targetX, targetY, targetWidth, targetHeight)); + return (new Size(Sanitize(width), Sanitize(height)), new Rectangle(targetX, targetY, Sanitize(targetWidth), Sanitize(targetHeight))); } // Switch to pad mode to downscale and calculate from there. @@ -253,7 +253,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms } // Target image width and height can be different to the rectangle width and height. - return (new Size(width, height), new Rectangle(targetX, targetY, targetWidth, targetHeight)); + return (new Size(Sanitize(width), Sanitize(height)), new Rectangle(targetX, targetY, Sanitize(targetWidth), Sanitize(targetHeight))); } private static (Size, Rectangle) CalculateMaxRectangle( @@ -282,7 +282,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms } // Replace the size to match the rectangle. - return (new Size(targetWidth, targetHeight), new Rectangle(0, 0, targetWidth, targetHeight)); + return (new Size(Sanitize(targetWidth), Sanitize(targetHeight)), new Rectangle(0, 0, Sanitize(targetWidth), Sanitize(targetHeight))); } private static (Size, Rectangle) CalculateMinRectangle( @@ -330,7 +330,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms } // Replace the size to match the rectangle. - return (new Size(targetWidth, targetHeight), new Rectangle(0, 0, targetWidth, targetHeight)); + return (new Size(Sanitize(targetWidth), Sanitize(targetHeight)), new Rectangle(0, 0, Sanitize(targetWidth), Sanitize(targetHeight))); } private static (Size, Rectangle) CalculatePadRectangle( @@ -398,7 +398,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms } // Target image width and height can be different to the rectangle width and height. - return (new Size(width, height), new Rectangle(targetX, targetY, targetWidth, targetHeight)); + return (new Size(Sanitize(width), Sanitize(height)), new Rectangle(targetX, targetY, Sanitize(targetWidth), Sanitize(targetHeight))); } private static (Size, Rectangle) CalculateManualRectangle( @@ -419,9 +419,14 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms int targetHeight = targetRectangle.Height > 0 ? targetRectangle.Height : height; // Target image width and height can be different to the rectangle width and height. - return (new Size(width, height), new Rectangle(targetX, targetY, targetWidth, targetHeight)); + return (new Size(Sanitize(width), Sanitize(height)), new Rectangle(targetX, targetY, Sanitize(targetWidth), Sanitize(targetHeight))); } private static void ThrowInvalid(string message) => throw new InvalidOperationException(message); + + private static int Sanitize(int input) + { + return Math.Max(1, input); + } } } diff --git a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs index aec0d2a1f..5feb8e9f0 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs @@ -605,5 +605,21 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2)); } } + + [Fact] + public void Issue1195() + { + using (var image = new Image(2, 300)) + { + var size = new Size(50, 50); + image.Mutate(x => x + .Resize( + new ResizeOptions + { + Size = size, + Mode = ResizeMode.Max + })); + } + } } }