From e39adf85f6c7add528325f1d557411e9751e7bcb Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 00:01:37 +0300 Subject: [PATCH] Fixed invalid baseline jpeg decoding --- .../Components/Decoder/HuffmanScanDecoder.cs | 5 +++-- .../Decoder/JpegComponentPostProcessor.cs | 16 ++++++++++++++++ .../Decoder/SpectralConverter{TPixel}.cs | 10 ++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs index 914562831d..0e13f75e44 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs @@ -185,7 +185,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder for (int y = 0; y < v; y++) { int blockRow = (mcuRow * v) + y; - Span blockSpan = component.SpectralBlocks.GetRowSpan(blockRow); + Span blockSpan = component.SpectralBlocks.GetRowSpan(y); ref Block8x8 blockRef = ref MemoryMarshal.GetReference(blockSpan); for (int x = 0; x < h; x++) @@ -213,7 +213,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder } // convert from spectral to actual pixels via given converter - this.spectralConverter.ConvertStride(j, j); + this.spectralConverter.ConvertStride(j, 0); + this.spectralConverter.ClearStride(0); } } diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs index 9eafaea0ae..6c25601c28 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs @@ -108,6 +108,22 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder } } + public void ClearSpectralStride(int step) + { + int yBlockStart = step * this.BlockRowsPerStep; + for (int y = 0; y < this.BlockRowsPerStep; y++) + { + int yBlock = yBlockStart + y; + + if (yBlock >= this.SizeInBlocks.Height) + { + break; + } + + this.Component.SpectralBlocks.GetRowSpan(yBlock).Clear(); + } + } + public void CopyBlocksToColorBuffer() { this.CopyBlocksToColorBuffer(this.currentComponentRowInBlocks); diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index 580adb3ab7..3499704aed 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -21,6 +21,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder public abstract void ConvertStride(int step, int spectralStep); + public abstract void ClearStride(int spectralStep); + public abstract void Dispose(); } @@ -134,6 +136,14 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder } } + public override void ClearStride(int spectralStep) + { + foreach (JpegComponentPostProcessor cpp in this.componentProcessors) + { + cpp.ClearSpectralStride(spectralStep); + } + } + public override void Dispose() { foreach (JpegComponentPostProcessor cpp in this.componentProcessors)