From baa8c7b8e76a627352d50a23e1b1a41682c7fb23 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 10 Mar 2022 02:06:47 +1100 Subject: [PATCH] Fix pad memory access error --- .../Processing/Extensions/Transforms/PadExtensions.cs | 6 +++++- .../Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs | 5 +---- .../Processing/Processors/Transforms/Resize/ResizeWorker.cs | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/Processing/Extensions/Transforms/PadExtensions.cs b/src/ImageSharp/Processing/Extensions/Transforms/PadExtensions.cs index ff374a9ac4..5e9e420321 100644 --- a/src/ImageSharp/Processing/Extensions/Transforms/PadExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Transforms/PadExtensions.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. +using System; + namespace SixLabors.ImageSharp.Processing { /// @@ -29,9 +31,11 @@ namespace SixLabors.ImageSharp.Processing /// The to allow chaining of operations. public static IImageProcessingContext Pad(this IImageProcessingContext source, int width, int height, Color color) { + Size size = source.GetCurrentSize(); var options = new ResizeOptions { - Size = new Size(width, height), + // Prevent downsizing. + Size = new Size(Math.Max(width, size.Width), Math.Max(height, size.Height)), Mode = ResizeMode.BoxPad, Sampler = KnownResamplers.NearestNeighbor, PadColor = color diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs index e0ccb487f7..af8adc2d5e 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs @@ -61,12 +61,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms Rectangle destinationRectangle = this.destinationRectangle; bool compand = this.options.Compand; bool premultiplyAlpha = this.options.PremultiplyAlpha; - - this.options.PadColor = Color.GreenYellow; - + TPixel fillColor = this.options.PadColor.ToPixel(); bool shouldFill = (this.options.Mode == ResizeMode.BoxPad || this.options.Mode == ResizeMode.Pad) && this.options.PadColor != default; - TPixel fillColor = this.options.PadColor.ToPixel(); // Handle resize dimensions identical to the original if (source.Width == destination.Width diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeWorker.cs b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeWorker.cs index 5f3c609898..a4a504317e 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeWorker.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeWorker.cs @@ -80,14 +80,14 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms int numberOfWindowBands = ResizeHelper.CalculateResizeWorkerHeightInWindowBands( this.windowBandHeight, - targetWorkingRect.Width, + destWidth, workingBufferLimitHintInBytes); this.workerHeight = Math.Min(this.sourceRectangle.Height, numberOfWindowBands * this.windowBandHeight); this.transposedFirstPassBuffer = configuration.MemoryAllocator.Allocate2D( this.workerHeight, - targetWorkingRect.Width, + destWidth, preferContiguosImageBuffers: true, options: AllocationOptions.Clean);