From 3bc12e43ca82818d12f132418dad22646f675626 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 17 Oct 2023 23:29:28 +1000 Subject: [PATCH] Fix failing tests --- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 2 - .../Formats/Png/PngScanlineProcessor.cs | 67 +++++++++---------- 2 files changed, 30 insertions(+), 39 deletions(-) diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index deb01289ea..f84d936c81 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -935,7 +935,6 @@ internal sealed class PngDecoderCore : IImageDecoderInternals case PngColorType.RgbWithAlpha: PngScanlineProcessor.ProcessRgbaScanline( - this.configuration, this.header.BitDepth, in frameControl, scanlineSpan, @@ -1035,7 +1034,6 @@ internal sealed class PngDecoderCore : IImageDecoderInternals case PngColorType.RgbWithAlpha: PngScanlineProcessor.ProcessInterlacedRgbaScanline( - this.configuration, this.header.BitDepth, in frameControl, scanlineSpan, diff --git a/src/ImageSharp/Formats/Png/PngScanlineProcessor.cs b/src/ImageSharp/Formats/Png/PngScanlineProcessor.cs index 31a59188e3..82faef3fe2 100644 --- a/src/ImageSharp/Formats/Png/PngScanlineProcessor.cs +++ b/src/ImageSharp/Formats/Png/PngScanlineProcessor.cs @@ -41,7 +41,7 @@ internal static class PngScanlineProcessor Color? transparentColor) where TPixel : unmanaged, IPixel { - uint offset = pixelOffset + (uint)frameControl.XOffset; + uint offset = pixelOffset + frameControl.XOffset; TPixel pixel = default; ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan); ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan); @@ -132,7 +132,7 @@ internal static class PngScanlineProcessor uint bytesPerSample) where TPixel : unmanaged, IPixel { - uint offset = pixelOffset + (uint)frameControl.XOffset; + uint offset = pixelOffset + frameControl.XOffset; TPixel pixel = default; ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan); ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan); @@ -242,7 +242,7 @@ internal static class PngScanlineProcessor Color? transparentColor) where TPixel : unmanaged, IPixel { - uint offset = pixelOffset + (uint)frameControl.XOffset; + uint offset = pixelOffset + frameControl.XOffset; TPixel pixel = default; ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan); @@ -266,20 +266,18 @@ internal static class PngScanlineProcessor } else { - // Rgb24 rgb = default; - // int o = 0; - // for (nuint x = offset; x < frameControl.XLimit; x += increment, o += bytesPerPixel) - // { - // rgb.R = Unsafe.Add(ref scanlineSpanRef, (uint)o); - // rgb.G = Unsafe.Add(ref scanlineSpanRef, (uint)(o + bytesPerSample)); - // rgb.B = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (2 * bytesPerSample))); - - // pixel.FromRgb24(rgb); - // Unsafe.Add(ref rowSpanRef, x) = pixel; - // } - - // PixelOperations.Instance.FromRgb24Bytes(configuration, scanlineSpan, rowSpan, header.Width); - PixelOperations.Instance.FromRgb24Bytes(configuration, scanlineSpan, rowSpan[(int)offset..], (int)frameControl.XMax); + // TODO: Investigate reintroducing bulk operations optimization here. + Rgb24 rgb = default; + int o = 0; + for (nuint x = offset; x < frameControl.XMax; x += increment, o += bytesPerPixel) + { + rgb.R = Unsafe.Add(ref scanlineSpanRef, (uint)o); + rgb.G = Unsafe.Add(ref scanlineSpanRef, (uint)(o + bytesPerSample)); + rgb.B = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (2 * bytesPerSample))); + + pixel.FromRgb24(rgb); + Unsafe.Add(ref rowSpanRef, x) = pixel; + } } return; @@ -325,7 +323,6 @@ internal static class PngScanlineProcessor } public static void ProcessRgbaScanline( - Configuration configuration, int bitDepth, in FrameControl frameControl, ReadOnlySpan scanlineSpan, @@ -334,7 +331,6 @@ internal static class PngScanlineProcessor int bytesPerSample) where TPixel : unmanaged, IPixel => ProcessInterlacedRgbaScanline( - configuration, bitDepth, frameControl, scanlineSpan, @@ -345,7 +341,6 @@ internal static class PngScanlineProcessor bytesPerSample); public static void ProcessInterlacedRgbaScanline( - Configuration configuration, int bitDepth, in FrameControl frameControl, ReadOnlySpan scanlineSpan, @@ -356,7 +351,7 @@ internal static class PngScanlineProcessor int bytesPerSample) where TPixel : unmanaged, IPixel { - uint offset = pixelOffset + (uint)frameControl.XOffset; + uint offset = pixelOffset + frameControl.XOffset; TPixel pixel = default; ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan); @@ -377,22 +372,20 @@ internal static class PngScanlineProcessor } else { - // ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan); - // Rgba32 rgba = default; - // int o = 0; - // for (nuint x = offset; x < frameControl.XLimit; x += increment, o += bytesPerPixel) - // { - // rgba.R = Unsafe.Add(ref scanlineSpanRef, (uint)o); - // rgba.G = Unsafe.Add(ref scanlineSpanRef, (uint)(o + bytesPerSample)); - // rgba.B = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (2 * bytesPerSample))); - // rgba.A = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (3 * bytesPerSample))); - - // pixel.FromRgba32(rgba); - // Unsafe.Add(ref rowSpanRef, x) = pixel; - // } - - // PixelOperations.Instance.FromRgba32Bytes(configuration, scanlineSpan, rowSpan, header.Width); - PixelOperations.Instance.FromRgba32Bytes(configuration, scanlineSpan, rowSpan[(int)offset..], (int)frameControl.XMax); + // TODO: Investigate reintroducing bulk operations optimization here. + ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan); + Rgba32 rgba = default; + int o = 0; + for (nuint x = offset; x < frameControl.XMax; x += increment, o += bytesPerPixel) + { + rgba.R = Unsafe.Add(ref scanlineSpanRef, (uint)o); + rgba.G = Unsafe.Add(ref scanlineSpanRef, (uint)(o + bytesPerSample)); + rgba.B = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (2 * bytesPerSample))); + rgba.A = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (3 * bytesPerSample))); + + pixel.FromRgba32(rgba); + Unsafe.Add(ref rowSpanRef, x) = pixel; + } } } }