diff --git a/src/ImageSharp/Formats/Jpeg/Components/Encoder/YCbCrForwardConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Encoder/YCbCrForwardConverter{TPixel}.cs index a059f978d1..963e6dd9ea 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Encoder/YCbCrForwardConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Encoder/YCbCrForwardConverter{TPixel}.cs @@ -76,7 +76,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder public void Convert(ImageFrame frame, int x, int y, ref RowOctet currentRows) { Memory.Buffer2D buffer = frame.PixelBuffer; - LoadAndStretchEdges(currentRows, this.pixelSpan, x, buffer.Width - x, buffer.Height - y, new Size(8)); + LoadAndStretchEdges(currentRows, this.pixelSpan, x, y, new Size(8), new Size(buffer.Width, buffer.Height)); PixelOperations.Instance.ToRgb24(frame.GetConfiguration(), this.pixelSpan, this.rgbSpan); @@ -100,7 +100,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder public void Convert420(ImageFrame frame, int x, int y, ref RowOctet currentRows, int idx) { Memory.Buffer2D buffer = frame.PixelBuffer; - LoadAndStretchEdges(currentRows, this.pixelSpan, x, Math.Min(16, buffer.Width - x), Math.Min(8, buffer.Height - y), new Size(16, 8)); + LoadAndStretchEdges(currentRows, this.pixelSpan, x, y, new Size(16, 8), new Size(buffer.Width, buffer.Height)); PixelOperations.Instance.ToRgb24(frame.GetConfiguration(), this.pixelSpan, this.rgbSpan); @@ -116,10 +116,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder } - private static void LoadAndStretchEdges(RowOctet source, Span dest, int startX, int width, int height, Size areaSize) + private static void LoadAndStretchEdges(RowOctet source, Span dest, int startX, int startY, Size areaSize, Size borders) { - DebugGuard.MustBeBetweenOrEqualTo(width, 1, areaSize.Width, nameof(width)); - DebugGuard.MustBeBetweenOrEqualTo(height, 1, areaSize.Height, nameof(height)); + int width = Math.Min(areaSize.Width, borders.Width - startX); + int height = Math.Min(areaSize.Height, borders.Height - startY); uint byteWidth = (uint)(width * Unsafe.SizeOf()); int remainderXCount = areaSize.Width - width;