Browse Source

Ensure min dimension of 1px following rounding. Fix #1195

pull/1198/head
James Jackson-South 6 years ago
parent
commit
bd826f1703
  1. 19
      src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeHelper.cs
  2. 16
      tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs

19
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);
}
}
}

16
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<Rgba32>(2, 300))
{
var size = new Size(50, 50);
image.Mutate(x => x
.Resize(
new ResizeOptions
{
Size = size,
Mode = ResizeMode.Max
}));
}
}
}
}

Loading…
Cancel
Save