From 519c6b227a6f148d79e4d7b57458b2331630c3fd Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Mon, 12 Jul 2021 18:24:07 +0300 Subject: [PATCH] Baseline jpegs now clear allocated buffers in the decoding loop --- .../Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs | 8 +++----- .../Formats/Jpeg/Components/Decoder/JpegComponent.cs | 4 +++- .../Formats/Jpeg/Components/Decoder/JpegFrame.cs | 8 ++++++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs index 89120813b0..f2f926d4ca 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs @@ -96,6 +96,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder this.scanBuffer = new HuffmanScanBuffer(this.stream); + bool fullScan = this.frame.Progressive || this.frame.MultiScan; + this.frame.AllocateComponents(fullScan); + if (!this.frame.Progressive) { this.ParseBaselineData(); @@ -123,14 +126,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder { if (this.ComponentsLength == this.frame.ComponentCount) { - // interleaved - we can convert spectral data stride by stride - this.frame.AllocateComponents(fullScan: false); this.ParseBaselineDataInterleaved(); } else { - // non-interleaved - each scan contains - this.frame.AllocateComponents(fullScan: true); this.ParseBaselineDataNonInterleaved(); } } @@ -303,7 +302,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder { this.CheckProgressiveData(); - this.frame.AllocateComponents(fullScan: true); if (this.ComponentsLength == 1) { this.ParseProgressiveDataNonInterleaved(); diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs index 614e96e54a..58c34ecf02 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs @@ -138,7 +138,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder int spectralAllocWidth = this.SizeInBlocks.Width; int spectralAllocHeight = fullScan ? this.SizeInBlocks.Height : this.VerticalSamplingFactor; - this.SpectralBlocks = this.memoryAllocator.Allocate2D(spectralAllocWidth, spectralAllocHeight, AllocationOptions.Clean); + // We don't need to clear buffer for stride-by-stride approach + AllocationOptions allocOptions = fullScan ? AllocationOptions.Clean : AllocationOptions.None; + this.SpectralBlocks = this.memoryAllocator.Allocate2D(spectralAllocWidth, spectralAllocHeight, allocOptions); } } } diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs index 595f39dbd3..9f89fd085f 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs @@ -20,6 +20,14 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder /// public bool Progressive { get; set; } + /// + /// Gets or sets a value indicating whether the frame is encoded using multiple scans (SOS markers). + /// + /// + /// This is true for progressive and baseline non-interleaved images. + /// + public bool MultiScan { get; set; } + /// /// Gets or sets the precision. ///