From e233b8136e2be968d60496d75dfe96ed9f06ba30 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sat, 16 Oct 2021 13:43:20 +0200 Subject: [PATCH] undo unsafe optimizations in ErrorDither --- .../Processing/Processors/Dithering/ErrorDither.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Dithering/ErrorDither.cs b/src/ImageSharp/Processing/Processors/Dithering/ErrorDither.cs index 0fe2d4b2c3..2e6cfebe15 100644 --- a/src/ImageSharp/Processing/Processors/Dithering/ErrorDither.cs +++ b/src/ImageSharp/Processing/Processors/Dithering/ErrorDither.cs @@ -108,13 +108,19 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering for (int y = bounds.Top; y < bounds.Bottom; y++) { - ref TPixel sourceRowRef = ref MemoryMarshal.GetReference(source.GetPixelRowSpan(y)); - ref byte destinationRowRef = ref MemoryMarshal.GetReference(destination.GetWritablePixelRowSpanUnsafe(y - offsetY)); + // Unsafe optimizations undone temporarily. + // Sporadic local AccessViolationException indicates possible indexing bug. + // ref TPixel sourceRowRef = ref MemoryMarshal.GetReference(source.GetPixelRowSpan(y)); + // ref byte destinationRowRef = ref MemoryMarshal.GetReference(destination.GetWritablePixelRowSpanUnsafe(y - offsetY)); + Span sourceSpan = source.GetPixelRowSpan(y); + Span destSpan = destination.GetWritablePixelRowSpanUnsafe(y - offsetY); for (int x = bounds.Left; x < bounds.Right; x++) { - TPixel sourcePixel = Unsafe.Add(ref sourceRowRef, x); - Unsafe.Add(ref destinationRowRef, x - offsetX) = quantizer.GetQuantizedColor(sourcePixel, out TPixel transformed); + // TPixel sourcePixel = Unsafe.Add(ref sourceRowRef, x); + // Unsafe.Add(ref destinationRowRef, x - offsetX) = quantizer.GetQuantizedColor(sourcePixel, out TPixel transformed); + TPixel sourcePixel = sourceSpan[x]; + destSpan[x - offsetX] = quantizer.GetQuantizedColor(sourcePixel, out TPixel transformed); this.Dither(source, bounds, sourcePixel, transformed, x, y, scale); } }