From 6f6ee7337dcdf1a099a95fdd0a481fc582442c3d Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Wed, 7 Jul 2021 23:38:48 +0300 Subject: [PATCH 01/76] Renamed pixel dimensions for JpegFrame --- .../Formats/Jpeg/Components/Decoder/JpegComponent.cs | 4 ++-- .../Formats/Jpeg/Components/Decoder/JpegFrame.cs | 8 ++++---- src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs | 10 +++++----- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs index 5c3ee6e28e..dd43baa233 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs @@ -109,10 +109,10 @@ public void Dispose() public void Init() { this.WidthInBlocks = (int)MathF.Ceiling( - MathF.Ceiling(this.Frame.SamplesPerLine / 8F) * this.HorizontalSamplingFactor / this.Frame.MaxHorizontalFactor); + MathF.Ceiling(this.Frame.PixelWidth / 8F) * this.HorizontalSamplingFactor / this.Frame.MaxHorizontalFactor); this.HeightInBlocks = (int)MathF.Ceiling( - MathF.Ceiling(this.Frame.Scanlines / 8F) * this.VerticalSamplingFactor / this.Frame.MaxVerticalFactor); + MathF.Ceiling(this.Frame.PixelHeight / 8F) * this.VerticalSamplingFactor / this.Frame.MaxVerticalFactor); int blocksPerLineForMcu = this.Frame.McusPerLine * this.HorizontalSamplingFactor; int blocksPerColumnForMcu = this.Frame.McusPerColumn * this.VerticalSamplingFactor; diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs index 827afe38da..13d6bc35ec 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs @@ -28,12 +28,12 @@ internal sealed class JpegFrame : IDisposable /// /// Gets or sets the number of scanlines within the frame. /// - public int Scanlines { get; set; } + public int PixelHeight { get; set; } /// /// Gets or sets the number of samples per scanline. /// - public int SamplesPerLine { get; set; } + public int PixelWidth { get; set; } /// /// Gets or sets the number of components within a frame. In progressive frames this value can range from only 1 to 4. @@ -95,8 +95,8 @@ public void Dispose() /// public void InitComponents() { - this.McusPerLine = (int)MathF.Ceiling(this.SamplesPerLine / 8F / this.MaxHorizontalFactor); - this.McusPerColumn = (int)MathF.Ceiling(this.Scanlines / 8F / this.MaxVerticalFactor); + this.McusPerLine = (int)MathF.Ceiling(this.PixelWidth / 8F / this.MaxHorizontalFactor); + this.McusPerColumn = (int)MathF.Ceiling(this.PixelHeight / 8F / this.MaxVerticalFactor); for (int i = 0; i < this.ComponentCount; i++) { diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 9f3966de29..ad47f386d2 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -852,17 +852,17 @@ private void ProcessStartOfFrameMarker(BufferedReadStream stream, int remaining, Extended = frameMarker.Marker == JpegConstants.Markers.SOF1, Progressive = frameMarker.Marker == JpegConstants.Markers.SOF2, Precision = this.temp[0], - Scanlines = (this.temp[1] << 8) | this.temp[2], - SamplesPerLine = (this.temp[3] << 8) | this.temp[4], + PixelHeight = (this.temp[1] << 8) | this.temp[2], + PixelWidth = (this.temp[3] << 8) | this.temp[4], ComponentCount = this.temp[5] }; - if (this.Frame.SamplesPerLine == 0 || this.Frame.Scanlines == 0) + if (this.Frame.PixelWidth == 0 || this.Frame.PixelHeight == 0) { - JpegThrowHelper.ThrowInvalidImageDimensions(this.Frame.SamplesPerLine, this.Frame.Scanlines); + JpegThrowHelper.ThrowInvalidImageDimensions(this.Frame.PixelWidth, this.Frame.PixelHeight); } - this.ImageSizeInPixels = new Size(this.Frame.SamplesPerLine, this.Frame.Scanlines); + this.ImageSizeInPixels = new Size(this.Frame.PixelWidth, this.Frame.PixelHeight); this.ComponentCount = this.Frame.ComponentCount; if (!metadataOnly) From 925b3ad1389bf10c19fa867149ed5d1ce3201dcb Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Wed, 7 Jul 2021 23:39:08 +0300 Subject: [PATCH 02/76] Added debug code to the sandbox --- .../Program.cs | 67 ++++++++++++++++++- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs b/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs index 9dd7e4c820..1a956dc9c6 100644 --- a/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs +++ b/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs @@ -3,6 +3,7 @@ using System; using System.Diagnostics; +using System.IO; using SixLabors.ImageSharp.Tests.Formats.Jpg; using SixLabors.ImageSharp.Tests.PixelFormats.PixelOperations; using SixLabors.ImageSharp.Tests.ProfilingBenchmarks; @@ -32,14 +33,76 @@ private class ConsoleOutput : ITestOutputHelper /// public static void Main(string[] args) { - LoadResizeSaveParallelMemoryStress.Run(); // RunJpegEncoderProfilingTests(); // RunJpegColorProfilingTests(); // RunDecodeJpegProfilingTests(); // RunToVector4ProfilingTest(); // RunResizeProfilingTest(); - // Console.ReadLine(); + //Test_Performance(20); + + //Test_DebugRun("chroma_444_16x16", true); + //Console.WriteLine(); + //Test_DebugRun("chroma_420_16x16", true); + //Console.WriteLine(); + //Test_DebugRun("444_14x14"); + //Console.WriteLine(); + //Test_DebugRun("baseline_4k_444", false); + //Console.WriteLine(); + //Test_DebugRun("progressive_4k_444", true); + //Console.WriteLine(); + //Test_DebugRun("baseline_4k_420", false); + //Console.WriteLine(); + //Test_DebugRun("cmyk_jpeg"); + //Console.WriteLine(); + //Test_DebugRun("Channel_digital_image_CMYK_color"); + //Console.WriteLine(); + + + //Test_DebugRun("test_baseline_4k_444", false); + //Console.WriteLine(); + //Test_DebugRun("test_progressive_4k_444", false); + //Console.WriteLine(); + //Test_DebugRun("test_baseline_4k_420", false); + //Console.WriteLine(); + + // Binary size of this must be ~2096kb + //Test_DebugRun("422", true); + + Test_DebugRun("baseline_s444_q100", true); + Test_DebugRun("progressive_s444_q100", true); + + Console.ReadLine(); + } + + public static void Test_Performance(int iterations) + { + using var stream = new MemoryStream(File.ReadAllBytes("C:\\Users\\pl4nu\\Downloads\\progressive_4k_444.jpg")); + //using var stream = new MemoryStream(File.ReadAllBytes("C:\\Users\\pl4nu\\Downloads\\baseline_4k_444.jpg")); + var sw = new Stopwatch(); + sw.Start(); + for (int i = 0; i < iterations; i++) + { + using var img = Image.Load(stream); + stream.Position = 0; + } + + sw.Stop(); + Console.WriteLine($"Elapsed: {sw.ElapsedMilliseconds}ms\nPer invocation: {sw.ElapsedMilliseconds / iterations}ms"); + } + + public static void Test_DebugRun(string name, bool save = false) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"img: {name}"); + Console.ResetColor(); + using var img = Image.Load($"C:\\Users\\pl4nu\\Downloads\\{name}.jpg"); + + if (save) + { + img.SaveAsJpeg($"C:\\Users\\pl4nu\\Downloads\\test_{name}.jpg", + new ImageSharp.Formats.Jpeg.JpegEncoder { Subsample = ImageSharp.Formats.Jpeg.JpegSubsample.Ratio444, Quality = 100 }); + } } private static void RunJpegEncoderProfilingTests() From 2f8d3c933bf0bf3c5b1b44c44a5f512f2ca6a52e Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Thu, 8 Jul 2021 00:03:11 +0300 Subject: [PATCH 03/76] Injected progressive scan parameters --- .../Components/Decoder/HuffmanScanDecoder.cs | 16 ++++------------ src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs | 12 +++++++----- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs index 6424ee23ac..0ff10e2700 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs @@ -29,16 +29,16 @@ internal class HuffmanScanDecoder private readonly int componentsLength; // The spectral selection start. - private readonly int spectralStart; + public int spectralStart; // The spectral selection end. - private readonly int spectralEnd; + public int spectralEnd; // The successive approximation high bit end. - private readonly int successiveHigh; + public int successiveHigh; // The successive approximation low bit end. - private readonly int successiveLow; + public int successiveLow; // How many mcu's are left to do. private int todo; @@ -74,10 +74,6 @@ public HuffmanScanDecoder( HuffmanTable[] acHuffmanTables, int componentsLength, int restartInterval, - int spectralStart, - int spectralEnd, - int successiveHigh, - int successiveLow, CancellationToken cancellationToken) { this.dctZigZag = ZigZag.CreateUnzigTable(); @@ -90,10 +86,6 @@ public HuffmanScanDecoder( this.componentsLength = componentsLength; this.restartInterval = restartInterval; this.todo = restartInterval; - this.spectralStart = spectralStart; - this.spectralEnd = spectralEnd; - this.successiveHigh = successiveHigh; - this.successiveLow = successiveLow; this.cancellationToken = cancellationToken; } diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index ad47f386d2..b2eb18941b 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -1056,11 +1056,13 @@ private void ProcessStartOfScanMarker(BufferedReadStream stream, CancellationTok this.acHuffmanTables, selectorsCount, this.resetInterval, - spectralStart, - spectralEnd, - successiveApproximation >> 4, - successiveApproximation & 15, - cancellationToken); + cancellationToken) + { + spectralStart = spectralStart, + spectralEnd = spectralEnd, + successiveHigh = successiveApproximation >> 4, + successiveLow = successiveApproximation & 15 + }; sd.ParseEntropyCodedData(); } From 336c64aab675b5b3baec1f82b7031882412c207e Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Thu, 8 Jul 2021 00:04:34 +0300 Subject: [PATCH 04/76] Injected scan selectors count --- .../Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs | 4 +--- src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs | 3 ++- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs index 0ff10e2700..34eaf1500b 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs @@ -26,7 +26,7 @@ internal class HuffmanScanDecoder private readonly int restartInterval; // The number of interleaved components. - private readonly int componentsLength; + public int componentsLength; // The spectral selection start. public int spectralStart; @@ -72,7 +72,6 @@ public HuffmanScanDecoder( JpegFrame frame, HuffmanTable[] dcHuffmanTables, HuffmanTable[] acHuffmanTables, - int componentsLength, int restartInterval, CancellationToken cancellationToken) { @@ -83,7 +82,6 @@ public HuffmanScanDecoder( this.dcHuffmanTables = dcHuffmanTables; this.acHuffmanTables = acHuffmanTables; this.components = frame.Components; - this.componentsLength = componentsLength; this.restartInterval = restartInterval; this.todo = restartInterval; this.cancellationToken = cancellationToken; diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index b2eb18941b..9cae029fe0 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -1054,10 +1054,11 @@ private void ProcessStartOfScanMarker(BufferedReadStream stream, CancellationTok this.Frame, this.dcHuffmanTables, this.acHuffmanTables, - selectorsCount, this.resetInterval, cancellationToken) { + componentsLength = selectorsCount, + spectralStart = spectralStart, spectralEnd = spectralEnd, successiveHigh = successiveApproximation >> 4, From 3b2d2d8c1de984af69bcac1e2f5bc7435b0f83bc Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Thu, 8 Jul 2021 00:14:19 +0300 Subject: [PATCH 05/76] Injected frame & reset interval --- .../Components/Decoder/HuffmanScanDecoder.cs | 37 +++++++++++++------ .../Formats/Jpeg/JpegDecoderCore.cs | 6 ++- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs index 34eaf1500b..c189e4e285 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs @@ -16,14 +16,36 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder /// internal class HuffmanScanDecoder { - private readonly JpegFrame frame; private readonly HuffmanTable[] dcHuffmanTables; private readonly HuffmanTable[] acHuffmanTables; private readonly BufferedReadStream stream; - private readonly JpegComponent[] components; + + // Frame related + private JpegFrame frame; + private JpegComponent[] components; + + public JpegFrame Frame + { + set + { + frame = value; + components = value.Components; + } + } // The restart interval. - private readonly int restartInterval; + private int restartInterval; + // How many mcu's are left to do. + private int todo; + + public int ResetInterval + { + set + { + restartInterval = value; + todo = value; + } + } // The number of interleaved components. public int componentsLength; @@ -40,9 +62,6 @@ internal class HuffmanScanDecoder // The successive approximation low bit end. public int successiveLow; - // How many mcu's are left to do. - private int todo; - // The End-Of-Block countdown for ending the sequence prematurely when the remaining coefficients are zero. private int eobrun; @@ -69,21 +88,15 @@ internal class HuffmanScanDecoder /// The token to monitor cancellation. public HuffmanScanDecoder( BufferedReadStream stream, - JpegFrame frame, HuffmanTable[] dcHuffmanTables, HuffmanTable[] acHuffmanTables, - int restartInterval, CancellationToken cancellationToken) { this.dctZigZag = ZigZag.CreateUnzigTable(); this.stream = stream; this.scanBuffer = new HuffmanScanBuffer(stream); - this.frame = frame; this.dcHuffmanTables = dcHuffmanTables; this.acHuffmanTables = acHuffmanTables; - this.components = frame.Components; - this.restartInterval = restartInterval; - this.todo = restartInterval; this.cancellationToken = cancellationToken; } diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 9cae029fe0..5d7e12618c 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -1051,12 +1051,14 @@ private void ProcessStartOfScanMarker(BufferedReadStream stream, CancellationTok var sd = new HuffmanScanDecoder( stream, - this.Frame, this.dcHuffmanTables, this.acHuffmanTables, - this.resetInterval, cancellationToken) { + Frame = this.Frame, + + ResetInterval = this.resetInterval, + componentsLength = selectorsCount, spectralStart = spectralStart, From 5d4450346c36f5fcb15f9504a9e16cd070345042 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Thu, 8 Jul 2021 00:24:18 +0300 Subject: [PATCH 06/76] Injected huffman tables --- .../Jpeg/Components/Decoder/HuffmanScanDecoder.cs | 10 ++++------ src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs | 5 +++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs index c189e4e285..d212340c10 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs @@ -16,10 +16,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder /// internal class HuffmanScanDecoder { - private readonly HuffmanTable[] dcHuffmanTables; - private readonly HuffmanTable[] acHuffmanTables; private readonly BufferedReadStream stream; + // huffman tables + public HuffmanTable[] dcHuffmanTables; + public HuffmanTable[] acHuffmanTables; + // Frame related private JpegFrame frame; private JpegComponent[] components; @@ -88,15 +90,11 @@ public int ResetInterval /// The token to monitor cancellation. public HuffmanScanDecoder( BufferedReadStream stream, - HuffmanTable[] dcHuffmanTables, - HuffmanTable[] acHuffmanTables, CancellationToken cancellationToken) { this.dctZigZag = ZigZag.CreateUnzigTable(); this.stream = stream; this.scanBuffer = new HuffmanScanBuffer(stream); - this.dcHuffmanTables = dcHuffmanTables; - this.acHuffmanTables = acHuffmanTables; this.cancellationToken = cancellationToken; } diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 5d7e12618c..dda4e96ea3 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -1051,12 +1051,13 @@ private void ProcessStartOfScanMarker(BufferedReadStream stream, CancellationTok var sd = new HuffmanScanDecoder( stream, - this.dcHuffmanTables, - this.acHuffmanTables, cancellationToken) { Frame = this.Frame, + dcHuffmanTables = this.dcHuffmanTables, + acHuffmanTables = this.acHuffmanTables, + ResetInterval = this.resetInterval, componentsLength = selectorsCount, From 442af2c5be2a4c552b627a4c0fc79d74cb7d3b63 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Thu, 8 Jul 2021 00:32:31 +0300 Subject: [PATCH 07/76] Scan decoder is not a persistent state of the decoder core --- .../Components/Decoder/HuffmanScanDecoder.cs | 3 +- .../Formats/Jpeg/JpegDecoderCore.cs | 35 ++++++++----------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs index d212340c10..b1f371e0f4 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs @@ -94,7 +94,6 @@ public HuffmanScanDecoder( { this.dctZigZag = ZigZag.CreateUnzigTable(); this.stream = stream; - this.scanBuffer = new HuffmanScanBuffer(stream); this.cancellationToken = cancellationToken; } @@ -105,6 +104,8 @@ public void ParseEntropyCodedData() { this.cancellationToken.ThrowIfCancellationRequested(); + this.scanBuffer = new HuffmanScanBuffer(this.stream); + if (!this.frame.Progressive) { this.ParseBaselineData(); diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index dda4e96ea3..a52ce3f9c4 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -172,6 +172,8 @@ public JpegDecoderCore(Configuration configuration, IJpegDecoderOptions options) /// public Block8x8F[] QuantizationTables { get; private set; } + private HuffmanScanDecoder scanDecoder; + /// /// Finds the next file marker within the byte stream. /// @@ -213,6 +215,8 @@ public static JpegFileMarker FindNextFileMarker(byte[] marker, BufferedReadStrea public Image Decode(BufferedReadStream stream, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { + this.scanDecoder = new HuffmanScanDecoder(stream, cancellationToken); + this.ParseStream(stream, cancellationToken: cancellationToken); this.InitExifProfile(); this.InitIccProfile(); @@ -1049,26 +1053,17 @@ private void ProcessStartOfScanMarker(BufferedReadStream stream, CancellationTok int spectralEnd = this.temp[1]; int successiveApproximation = this.temp[2]; - var sd = new HuffmanScanDecoder( - stream, - cancellationToken) - { - Frame = this.Frame, - - dcHuffmanTables = this.dcHuffmanTables, - acHuffmanTables = this.acHuffmanTables, - - ResetInterval = this.resetInterval, - - componentsLength = selectorsCount, - - spectralStart = spectralStart, - spectralEnd = spectralEnd, - successiveHigh = successiveApproximation >> 4, - successiveLow = successiveApproximation & 15 - }; - - sd.ParseEntropyCodedData(); + this.scanDecoder.Frame = this.Frame; + this.scanDecoder.dcHuffmanTables = this.dcHuffmanTables; + this.scanDecoder.acHuffmanTables = this.acHuffmanTables; + this.scanDecoder.ResetInterval = this.resetInterval; + this.scanDecoder.componentsLength = selectorsCount; + this.scanDecoder.spectralStart = spectralStart; + this.scanDecoder.spectralEnd = spectralEnd; + this.scanDecoder.successiveHigh = successiveApproximation >> 4; + this.scanDecoder.successiveLow = successiveApproximation & 15; + + this.scanDecoder.ParseEntropyCodedData(); } /// From b7d54b10c825659b3d2cb4ef027c3aba185d82d0 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Thu, 8 Jul 2021 00:36:01 +0300 Subject: [PATCH 08/76] Added comments for future refactoring --- src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index a52ce3f9c4..54c3d0a5b5 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -1053,11 +1053,20 @@ private void ProcessStartOfScanMarker(BufferedReadStream stream, CancellationTok int spectralEnd = this.temp[1]; int successiveApproximation = this.temp[2]; + // This can be injected in SOF marker callback this.scanDecoder.Frame = this.Frame; + + // Huffman tables can be calculated directly in the scan decoder class this.scanDecoder.dcHuffmanTables = this.dcHuffmanTables; this.scanDecoder.acHuffmanTables = this.acHuffmanTables; + + // This can be injectd in DRI marker callback this.scanDecoder.ResetInterval = this.resetInterval; + + // This can be passed as ParseEntropyCodedData() parameter as it is used only there this.scanDecoder.componentsLength = selectorsCount; + + // This is okay to inject here, might be good to wrap it in a separate struct but not really necessary this.scanDecoder.spectralStart = spectralStart; this.scanDecoder.spectralEnd = spectralEnd; this.scanDecoder.successiveHigh = successiveApproximation >> 4; From 7044741601300960929b9b1de0d27d2b2cef5599 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Thu, 8 Jul 2021 00:41:49 +0300 Subject: [PATCH 09/76] Added extra comment --- src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 54c3d0a5b5..7455d2a603 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -1053,6 +1053,9 @@ private void ProcessStartOfScanMarker(BufferedReadStream stream, CancellationTok int spectralEnd = this.temp[1]; int successiveApproximation = this.temp[2]; + // All the comments below are for separate refactoring PR + // Main reason it's not fixed here is to make this commit less intrusive + // This can be injected in SOF marker callback this.scanDecoder.Frame = this.Frame; From 9b8172473b2c2c9afdc8e5574606bd87c8768268 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Thu, 8 Jul 2021 06:48:39 +0300 Subject: [PATCH 10/76] Jpeg frame is now injected to the scan decoder at the SOF marker --- src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 7455d2a603..44bd05145b 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -920,6 +920,9 @@ private void ProcessStartOfFrameMarker(BufferedReadStream stream, int remaining, this.Metadata.GetJpegMetadata().ColorType = this.ColorSpace == JpegColorSpace.Grayscale ? JpegColorType.Luminance : JpegColorType.YCbCr; this.Frame.InitComponents(); this.ImageSizeInMCU = new Size(this.Frame.McusPerLine, this.Frame.McusPerColumn); + + // This can be injected in SOF marker callback + this.scanDecoder.Frame = this.Frame; } } @@ -1056,9 +1059,6 @@ private void ProcessStartOfScanMarker(BufferedReadStream stream, CancellationTok // All the comments below are for separate refactoring PR // Main reason it's not fixed here is to make this commit less intrusive - // This can be injected in SOF marker callback - this.scanDecoder.Frame = this.Frame; - // Huffman tables can be calculated directly in the scan decoder class this.scanDecoder.dcHuffmanTables = this.dcHuffmanTables; this.scanDecoder.acHuffmanTables = this.acHuffmanTables; From 7e1bd5906834ed45cc7a5b81e327ff27ebe998cb Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Thu, 8 Jul 2021 15:10:07 +0300 Subject: [PATCH 11/76] Slight change to image post processor for better understanding --- .../Decoder/JpegImagePostProcessor.cs | 22 ++++--------------- 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs index 5b0331c85c..fd3b9b4312 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs @@ -132,28 +132,11 @@ public void PostProcess(ImageFrame destination, CancellationToke /// /// Execute one step processing pixel rows into 'destination'. - /// - /// The pixel type - /// The destination image. - public void DoPostProcessorStep(ImageFrame destination) - where TPixel : unmanaged, IPixel - { - foreach (JpegComponentPostProcessor cpp in this.ComponentProcessors) - { - cpp.CopyBlocksToColorBuffer(); - } - - this.ConvertColorsInto(destination); - - this.PixelRowCounter += PixelRowsPerStep; - } - - /// /// Convert and copy row of colors into 'destination' starting at row . /// /// The pixel type /// The destination image - private void ConvertColorsInto(ImageFrame destination) + public void DoPostProcessorStep(ImageFrame destination) where TPixel : unmanaged, IPixel { int maxY = Math.Min(destination.Height, this.PixelRowCounter + PixelRowsPerStep); @@ -161,6 +144,7 @@ private void ConvertColorsInto(ImageFrame destination) var buffers = new Buffer2D[this.ComponentProcessors.Length]; for (int i = 0; i < this.ComponentProcessors.Length; i++) { + this.ComponentProcessors[i].CopyBlocksToColorBuffer(); buffers[i] = this.ComponentProcessors[i].ColorBuffer; } @@ -176,6 +160,8 @@ private void ConvertColorsInto(ImageFrame destination) // TODO: Investigate if slicing is actually necessary PixelOperations.Instance.FromVector4Destructive(this.configuration, this.rgbaBuffer.GetSpan().Slice(0, destRow.Length), destRow); } + + this.PixelRowCounter += PixelRowsPerStep; } } } From 5ba8763ade0ef953eb4d2ec6accffd8ba4a030c7 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Thu, 8 Jul 2021 15:58:41 +0300 Subject: [PATCH 12/76] Replaced hardcoded values with actual calculated ones in postprocessor --- .../Decoder/JpegComponentPostProcessor.cs | 2 +- .../Components/Decoder/JpegImagePostProcessor.cs | 13 +++++++++---- tests/ImageSharp.Tests.ProfilingSandbox/Program.cs | 5 ++++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs index fc1ebaf921..0a0a2cd9e8 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs @@ -37,7 +37,7 @@ public JpegComponentPostProcessor(MemoryAllocator memoryAllocator, JpegImagePost imagePostProcessor.PostProcessorBufferSize.Height, this.blockAreaSize.Height); - this.BlockRowsPerStep = JpegImagePostProcessor.BlockRowsPerStep / this.Component.SubSamplingDivisors.Height; + this.BlockRowsPerStep = imagePostProcessor.BlockRowsPerStep / this.Component.SubSamplingDivisors.Height; } /// diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs index fd3b9b4312..5f3389e17b 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs @@ -29,12 +29,12 @@ internal class JpegImagePostProcessor : IDisposable /// /// The number of block rows to be processed in one Step. /// - public const int BlockRowsPerStep = 4; + public int BlockRowsPerStep; /// /// The number of image pixel rows to be processed in one step. /// - public const int PixelRowsPerStep = 4 * 8; + public int PixelRowsPerStep; /// /// Temporal buffer to store a row of colors. @@ -56,8 +56,13 @@ public JpegImagePostProcessor(Configuration configuration, IRawJpegData rawJpeg) this.configuration = configuration; this.RawJpeg = rawJpeg; IJpegComponent c0 = rawJpeg.Components[0]; - this.NumberOfPostProcessorSteps = c0.SizeInBlocks.Height / BlockRowsPerStep; - this.PostProcessorBufferSize = new Size(c0.SizeInBlocks.Width * 8, PixelRowsPerStep); + + this.BlockRowsPerStep = c0.SamplingFactors.Height; + this.PixelRowsPerStep = this.BlockRowsPerStep * 8; + + this.NumberOfPostProcessorSteps = c0.SizeInBlocks.Height / this.BlockRowsPerStep; + + this.PostProcessorBufferSize = new Size(c0.SizeInBlocks.Width * 8, this.PixelRowsPerStep); MemoryAllocator memoryAllocator = configuration.MemoryAllocator; diff --git a/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs b/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs index 1a956dc9c6..e0b7f31a77 100644 --- a/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs +++ b/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs @@ -58,7 +58,6 @@ public static void Main(string[] args) //Test_DebugRun("Channel_digital_image_CMYK_color"); //Console.WriteLine(); - //Test_DebugRun("test_baseline_4k_444", false); //Console.WriteLine(); //Test_DebugRun("test_progressive_4k_444", false); @@ -69,6 +68,10 @@ public static void Main(string[] args) // Binary size of this must be ~2096kb //Test_DebugRun("422", true); + //Test_DebugRun("baseline_4k_420", false); + //Test_DebugRun("baseline_s444_q100", false); + //Test_DebugRun("progressive_s444_q100", false); + Test_DebugRun("baseline_4k_420", true); Test_DebugRun("baseline_s444_q100", true); Test_DebugRun("progressive_s444_q100", true); From 22af24128c9054afca7cf0c996aa2762c4ed6444 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Fri, 9 Jul 2021 18:03:33 +0300 Subject: [PATCH 13/76] WIP spectral converter --- .../Decoder/SpectralConverter{TPixel}.cs | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs new file mode 100644 index 0000000000..578a05e326 --- /dev/null +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -0,0 +1,91 @@ +// Copyright (c) Six Labors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.Buffers; +using System.Collections.Generic; +using System.Numerics; +using System.Text; +using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder.ColorConverters; +using SixLabors.ImageSharp.Memory; +using SixLabors.ImageSharp.PixelFormats; + +namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder +{ + internal abstract class SpectralConverter + { + public abstract void ConvertStride(); + } + + internal class SpectralConverter : SpectralConverter + where TPixel : unmanaged, IPixel + { + private Configuration configuration; + + private JpegComponentPostProcessor[] componentProcessors; + + private JpegColorConverter colorConverter; + + private IMemoryOwner rgbaBuffer; + + private Buffer2D pixelBuffer; + + public JpegFrame Frame + { + set => this.InjectFrame(value); + } + + public SpectralConverter(Configuration configuration) + { + this.configuration = configuration; + } + + private void InjectFrame(JpegFrame frame) + { + MemoryAllocator allocator = this.configuration.MemoryAllocator; + + // pixel buffer for resulting image + this.pixelBuffer = allocator.Allocate2D(frame.PixelWidth, frame.PixelHeight, AllocationOptions.Clean); + + // component processors from spectral to Rgba32 + this.componentProcessors = new JpegComponentPostProcessor[frame.Components.Length]; + for (int i = 0; i < this.componentProcessors.Length; i++) + { + this.componentProcessors[i] = new JpegComponentPostProcessor(allocator, this, frame.Components[i]); + } + + // single 'stride' rgba32 buffer for conversion between spectral and TPixel + this.rgbaBuffer = allocator.Allocate(frame.PixelWidth); + + // color converter from Rgba32 to TPixel + this.colorConverter = JpegColorConverter.GetConverter(rawJpeg.ColorSpace, frame.Precision); + } + + public override void ConvertStride() + { + int maxY = Math.Min(this.pixelBuffer.Height, this.PixelRowCounter + PixelRowsPerStep); + + var buffers = new Buffer2D[this.componentProcessors.Length]; + for (int i = 0; i < this.componentProcessors.Length; i++) + { + this.componentProcessors[i].CopyBlocksToColorBuffer(); + buffers[i] = this.componentProcessors[i].ColorBuffer; + } + + for (int yy = this.PixelRowCounter; yy < maxY; yy++) + { + int y = yy - this.PixelRowCounter; + + var values = new JpegColorConverter.ComponentValues(buffers, y); + this.colorConverter.ConvertToRgba(values, this.rgbaBuffer.GetSpan()); + + Span destRow = destination.GetPixelRowSpan(yy); + + // TODO: Investigate if slicing is actually necessary + PixelOperations.Instance.FromVector4Destructive(this.configuration, this.rgbaBuffer.GetSpan().Slice(0, destRow.Length), destRow); + } + + this.PixelRowCounter += PixelRowsPerStep; + } + } +} From dbe4c4e870f645d5706001eaa8eb368458a633ca Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Fri, 9 Jul 2021 18:11:36 +0300 Subject: [PATCH 14/76] Fixed iteration variables --- .../Decoder/SpectralConverter{TPixel}.cs | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index 578a05e326..3db47aa662 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -30,6 +30,14 @@ internal class SpectralConverter : SpectralConverter private Buffer2D pixelBuffer; + + public int BlockRowsPerStep; + + private int PixelRowsPerStep; + + private int PixelRowCounter; + + public JpegFrame Frame { set => this.InjectFrame(value); @@ -59,11 +67,18 @@ private void InjectFrame(JpegFrame frame) // color converter from Rgba32 to TPixel this.colorConverter = JpegColorConverter.GetConverter(rawJpeg.ColorSpace, frame.Precision); + + // iteration data + IJpegComponent c0 = frame.Components[0]; + + const int blockPixelHeight = 8; + this.BlockRowsPerStep = c0.SamplingFactors.Height; + this.PixelRowsPerStep = this.BlockRowsPerStep * blockPixelHeight; } public override void ConvertStride() { - int maxY = Math.Min(this.pixelBuffer.Height, this.PixelRowCounter + PixelRowsPerStep); + int maxY = Math.Min(this.pixelBuffer.Height, this.PixelRowCounter + this.PixelRowsPerStep); var buffers = new Buffer2D[this.componentProcessors.Length]; for (int i = 0; i < this.componentProcessors.Length; i++) @@ -79,13 +94,13 @@ public override void ConvertStride() var values = new JpegColorConverter.ComponentValues(buffers, y); this.colorConverter.ConvertToRgba(values, this.rgbaBuffer.GetSpan()); - Span destRow = destination.GetPixelRowSpan(yy); + Span destRow = this.pixelBuffer.GetRowSpan(yy); // TODO: Investigate if slicing is actually necessary PixelOperations.Instance.FromVector4Destructive(this.configuration, this.rgbaBuffer.GetSpan().Slice(0, destRow.Length), destRow); } - this.PixelRowCounter += PixelRowsPerStep; + this.PixelRowCounter += this.PixelRowsPerStep; } } } From 887c0ba4b9601e141ea8eb5907b88e062b2bdf61 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Fri, 9 Jul 2021 18:15:14 +0300 Subject: [PATCH 15/76] Added todo(s) --- .../Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index 3db47aa662..8bab9f18a3 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -17,6 +17,8 @@ internal abstract class SpectralConverter public abstract void ConvertStride(); } + // TODO: componentProcessors must be disposed!!! + // TODO: rgbaBuffer must be disposed!!! internal class SpectralConverter : SpectralConverter where TPixel : unmanaged, IPixel { From 6b2f18952c0a802cc69065ec095157588dcc97bc Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Fri, 9 Jul 2021 18:52:33 +0300 Subject: [PATCH 16/76] Decoupled image processor from component processor --- .../Decoder/JpegComponentPostProcessor.cs | 19 ++++++++----------- .../Decoder/JpegImagePostProcessor.cs | 11 ++--------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs index 0a0a2cd9e8..83406e0d67 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs @@ -27,23 +27,20 @@ internal class JpegComponentPostProcessor : IDisposable /// /// Initializes a new instance of the class. /// - public JpegComponentPostProcessor(MemoryAllocator memoryAllocator, JpegImagePostProcessor imagePostProcessor, IJpegComponent component) + public JpegComponentPostProcessor(MemoryAllocator memoryAllocator, IRawJpegData rawJpeg, Size postProcessorBufferSize, IJpegComponent component) { this.Component = component; - this.ImagePostProcessor = imagePostProcessor; + this.RawJpeg = rawJpeg; this.blockAreaSize = this.Component.SubSamplingDivisors * 8; this.ColorBuffer = memoryAllocator.Allocate2DOveraligned( - imagePostProcessor.PostProcessorBufferSize.Width, - imagePostProcessor.PostProcessorBufferSize.Height, + postProcessorBufferSize.Width, + postProcessorBufferSize.Height, this.blockAreaSize.Height); - this.BlockRowsPerStep = imagePostProcessor.BlockRowsPerStep / this.Component.SubSamplingDivisors.Height; + this.BlockRowsPerStep = postProcessorBufferSize.Height / 8 / this.Component.SubSamplingDivisors.Height; } - /// - /// Gets the - /// - public JpegImagePostProcessor ImagePostProcessor { get; } + public IRawJpegData RawJpeg { get; } /// /// Gets the @@ -76,8 +73,8 @@ public void Dispose() /// public void CopyBlocksToColorBuffer() { - var blockPp = new JpegBlockPostProcessor(this.ImagePostProcessor.RawJpeg, this.Component); - float maximumValue = MathF.Pow(2, this.ImagePostProcessor.RawJpeg.Precision) - 1; + var blockPp = new JpegBlockPostProcessor(this.RawJpeg, this.Component); + float maximumValue = MathF.Pow(2, this.RawJpeg.Precision) - 1; int destAreaStride = this.ColorBuffer.Width; diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs index 5f3389e17b..18b2bc6e84 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs @@ -62,14 +62,12 @@ public JpegImagePostProcessor(Configuration configuration, IRawJpegData rawJpeg) this.NumberOfPostProcessorSteps = c0.SizeInBlocks.Height / this.BlockRowsPerStep; - this.PostProcessorBufferSize = new Size(c0.SizeInBlocks.Width * 8, this.PixelRowsPerStep); - + var postProcessorBufferSize = new Size(c0.SizeInBlocks.Width * 8, this.PixelRowsPerStep); MemoryAllocator memoryAllocator = configuration.MemoryAllocator; - this.ComponentProcessors = new JpegComponentPostProcessor[rawJpeg.Components.Length]; for (int i = 0; i < rawJpeg.Components.Length; i++) { - this.ComponentProcessors[i] = new JpegComponentPostProcessor(memoryAllocator, this, rawJpeg.Components[i]); + this.ComponentProcessors[i] = new JpegComponentPostProcessor(memoryAllocator, this.RawJpeg, postProcessorBufferSize, rawJpeg.Components[i]); } this.rgbaBuffer = memoryAllocator.Allocate(rawJpeg.ImageSizeInPixels.Width); @@ -91,11 +89,6 @@ public JpegImagePostProcessor(Configuration configuration, IRawJpegData rawJpeg) /// public int NumberOfPostProcessorSteps { get; } - /// - /// Gets the size of the temporary buffers we need to allocate into . - /// - public Size PostProcessorBufferSize { get; } - /// /// Gets the value of the counter that grows by each step by . /// From dc4bc6e03f91f87bc2464ac5154f258a2ab7175a Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Fri, 9 Jul 2021 18:58:07 +0300 Subject: [PATCH 17/76] Fixed converter frame injection --- .../Decoder/SpectralConverter{TPixel}.cs | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index 8bab9f18a3..6e6ba7ab8f 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -33,6 +33,7 @@ internal class SpectralConverter : SpectralConverter private Buffer2D pixelBuffer; + public int BlockRowsPerStep; private int PixelRowsPerStep; @@ -40,42 +41,39 @@ internal class SpectralConverter : SpectralConverter private int PixelRowCounter; - public JpegFrame Frame - { - set => this.InjectFrame(value); - } public SpectralConverter(Configuration configuration) { this.configuration = configuration; } - private void InjectFrame(JpegFrame frame) + public void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) { MemoryAllocator allocator = this.configuration.MemoryAllocator; + // iteration data + IJpegComponent c0 = frame.Components[0]; + + const int blockPixelHeight = 8; + this.BlockRowsPerStep = c0.SamplingFactors.Height; + this.PixelRowsPerStep = this.BlockRowsPerStep * blockPixelHeight; + // pixel buffer for resulting image this.pixelBuffer = allocator.Allocate2D(frame.PixelWidth, frame.PixelHeight, AllocationOptions.Clean); // component processors from spectral to Rgba32 + var postProcessorBufferSize = new Size(c0.SizeInBlocks.Width * 8, this.PixelRowsPerStep); this.componentProcessors = new JpegComponentPostProcessor[frame.Components.Length]; for (int i = 0; i < this.componentProcessors.Length; i++) { - this.componentProcessors[i] = new JpegComponentPostProcessor(allocator, this, frame.Components[i]); + this.componentProcessors[i] = new JpegComponentPostProcessor(allocator, jpegData, postProcessorBufferSize, frame.Components[i]); } // single 'stride' rgba32 buffer for conversion between spectral and TPixel this.rgbaBuffer = allocator.Allocate(frame.PixelWidth); // color converter from Rgba32 to TPixel - this.colorConverter = JpegColorConverter.GetConverter(rawJpeg.ColorSpace, frame.Precision); - - // iteration data - IJpegComponent c0 = frame.Components[0]; - - const int blockPixelHeight = 8; - this.BlockRowsPerStep = c0.SamplingFactors.Height; - this.PixelRowsPerStep = this.BlockRowsPerStep * blockPixelHeight; + this.colorConverter = JpegColorConverter.GetConverter(jpegData.ColorSpace, frame.Precision); } public override void ConvertStride() From d178c8c6725cb68bda2d2acdd820e3ea37ff23fe Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Fri, 9 Jul 2021 19:02:44 +0300 Subject: [PATCH 18/76] Added getter which converts pixels to PixelBuffer property --- .../Decoder/SpectralConverter{TPixel}.cs | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index 6e6ba7ab8f..1befbbf827 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Numerics; using System.Text; +using System.Threading; using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder.ColorConverters; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; @@ -24,6 +25,9 @@ internal class SpectralConverter : SpectralConverter { private Configuration configuration; + private CancellationToken cancellationToken; + + private JpegComponentPostProcessor[] componentProcessors; private JpegColorConverter colorConverter; @@ -41,10 +45,31 @@ internal class SpectralConverter : SpectralConverter private int PixelRowCounter; + private bool converted; + + public Buffer2D PixelBuffer + { + get + { + if (!this.converted) + { + while (this.PixelRowCounter < this.pixelBuffer.Height) + { + this.cancellationToken.ThrowIfCancellationRequested(); + this.ConvertStride(); + } + + this.converted = true; + } + + return this.pixelBuffer; + } + } - public SpectralConverter(Configuration configuration) + public SpectralConverter(Configuration configuration, CancellationToken ct) { this.configuration = configuration; + this.cancellationToken = ct; } public void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) From c86d02901cf91867cedf38db3875c8eecd9c3e80 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Fri, 9 Jul 2021 19:04:05 +0300 Subject: [PATCH 19/76] Fixed sandbox code --- tests/ImageSharp.Tests.ProfilingSandbox/Program.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs b/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs index e0b7f31a77..b51592b6d9 100644 --- a/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs +++ b/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs @@ -70,12 +70,12 @@ public static void Main(string[] args) //Test_DebugRun("baseline_4k_420", false); //Test_DebugRun("baseline_s444_q100", false); - //Test_DebugRun("progressive_s444_q100", false); + //Test_DebugRun("progressive_s420_q100", false); Test_DebugRun("baseline_4k_420", true); Test_DebugRun("baseline_s444_q100", true); - Test_DebugRun("progressive_s444_q100", true); + Test_DebugRun("progressive_s420_q100", true); - Console.ReadLine(); + //Console.ReadLine(); } public static void Test_Performance(int iterations) From 1dbb16a7fdebf38c87be8efa453245a2157cdbb2 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Fri, 9 Jul 2021 20:39:22 +0300 Subject: [PATCH 20/76] Wired up converter & scan decoder --- .../Components/Decoder/HuffmanScanDecoder.cs | 21 +++++++++++-------- .../Decoder/SpectralConverter{TPixel}.cs | 4 +++- .../Formats/Jpeg/JpegDecoderCore.cs | 5 ++++- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs index b1f371e0f4..3980739e1f 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs @@ -26,15 +26,6 @@ internal class HuffmanScanDecoder private JpegFrame frame; private JpegComponent[] components; - public JpegFrame Frame - { - set - { - frame = value; - components = value.Components; - } - } - // The restart interval. private int restartInterval; // How many mcu's are left to do. @@ -72,6 +63,8 @@ public int ResetInterval private HuffmanScanBuffer scanBuffer; + private SpectralConverter spectralConverter; + private CancellationToken cancellationToken; /// @@ -90,10 +83,12 @@ public int ResetInterval /// The token to monitor cancellation. public HuffmanScanDecoder( BufferedReadStream stream, + SpectralConverter converter, CancellationToken cancellationToken) { this.dctZigZag = ZigZag.CreateUnzigTable(); this.stream = stream; + this.spectralConverter = converter; this.cancellationToken = cancellationToken; } @@ -121,6 +116,14 @@ public void ParseEntropyCodedData() } } + public void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) + { + this.frame = frame; + this.components = frame.Components; + + this.spectralConverter.InjectFrameData(frame, jpegData); + } + private void ParseBaselineData() { if (this.componentsLength == 1) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index 1befbbf827..ae4a90337a 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -15,6 +15,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder { internal abstract class SpectralConverter { + public abstract void InjectFrameData(JpegFrame frame, IRawJpegData jpegData); + public abstract void ConvertStride(); } @@ -72,7 +74,7 @@ public SpectralConverter(Configuration configuration, CancellationToken ct) this.cancellationToken = ct; } - public void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) + public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) { MemoryAllocator allocator = this.configuration.MemoryAllocator; diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 44bd05145b..1682860718 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -215,13 +215,16 @@ public static JpegFileMarker FindNextFileMarker(byte[] marker, BufferedReadStrea public Image Decode(BufferedReadStream stream, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { - this.scanDecoder = new HuffmanScanDecoder(stream, cancellationToken); + SpectralConverter spectralConverter = new SpectralConverter(this.Configuration, cancellationToken); + + this.scanDecoder = new HuffmanScanDecoder(stream, spectralConverter, cancellationToken); this.ParseStream(stream, cancellationToken: cancellationToken); this.InitExifProfile(); this.InitIccProfile(); this.InitIptcProfile(); this.InitDerivedMetadataProperties(); + return this.PostProcessIntoImage(cancellationToken); } From 1c10ec6d05fb7d75cc83a5a55f096f219c823a81 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Fri, 9 Jul 2021 20:47:02 +0300 Subject: [PATCH 21/76] Added Buffer2D Image ctor, wired new post processor with decoder core --- src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs | 6 +++--- src/ImageSharp/Image{TPixel}.cs | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 1682860718..eb4ed5b959 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -215,7 +215,7 @@ public static JpegFileMarker FindNextFileMarker(byte[] marker, BufferedReadStrea public Image Decode(BufferedReadStream stream, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { - SpectralConverter spectralConverter = new SpectralConverter(this.Configuration, cancellationToken); + var spectralConverter = new SpectralConverter(this.Configuration, cancellationToken); this.scanDecoder = new HuffmanScanDecoder(stream, spectralConverter, cancellationToken); @@ -225,7 +225,7 @@ public Image Decode(BufferedReadStream stream, CancellationToken this.InitIptcProfile(); this.InitDerivedMetadataProperties(); - return this.PostProcessIntoImage(cancellationToken); + return new Image(this.Configuration, spectralConverter.PixelBuffer, this.Metadata); } /// @@ -925,7 +925,7 @@ private void ProcessStartOfFrameMarker(BufferedReadStream stream, int remaining, this.ImageSizeInMCU = new Size(this.Frame.McusPerLine, this.Frame.McusPerColumn); // This can be injected in SOF marker callback - this.scanDecoder.Frame = this.Frame; + this.scanDecoder.InjectFrameData(this.Frame, this); } } diff --git a/src/ImageSharp/Image{TPixel}.cs b/src/ImageSharp/Image{TPixel}.cs index b43ff0422b..669db2a97b 100644 --- a/src/ImageSharp/Image{TPixel}.cs +++ b/src/ImageSharp/Image{TPixel}.cs @@ -87,6 +87,14 @@ internal Image(Configuration configuration, int width, int height, ImageMetadata this.frames = new ImageFrameCollection(this, width, height, default(TPixel)); } + internal Image( + Configuration configuration, + Buffer2D pixelBuffer, + ImageMetadata metadata) + : this(configuration, pixelBuffer.FastMemoryGroup, pixelBuffer.Width, pixelBuffer.Height, metadata) + { + } + /// /// Initializes a new instance of the class /// wrapping an external . From 1348ecfeaa2e3fdf5584f9afa7490b94f71cfde1 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Fri, 9 Jul 2021 20:53:44 +0300 Subject: [PATCH 22/76] Implemented disposable pattern for spectral converter --- .../Decoder/SpectralConverter{TPixel}.cs | 16 +++++++++++++--- src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index ae4a90337a..ec1c057b27 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -13,15 +13,15 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder { - internal abstract class SpectralConverter + internal abstract class SpectralConverter : IDisposable { public abstract void InjectFrameData(JpegFrame frame, IRawJpegData jpegData); public abstract void ConvertStride(); + + public abstract void Dispose(); } - // TODO: componentProcessors must be disposed!!! - // TODO: rgbaBuffer must be disposed!!! internal class SpectralConverter : SpectralConverter where TPixel : unmanaged, IPixel { @@ -129,5 +129,15 @@ public override void ConvertStride() this.PixelRowCounter += this.PixelRowsPerStep; } + + public override void Dispose() + { + foreach (JpegComponentPostProcessor cpp in this.componentProcessors) + { + cpp.Dispose(); + } + + this.rgbaBuffer.Dispose(); + } } } diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index eb4ed5b959..30024af95f 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -215,7 +215,7 @@ public static JpegFileMarker FindNextFileMarker(byte[] marker, BufferedReadStrea public Image Decode(BufferedReadStream stream, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { - var spectralConverter = new SpectralConverter(this.Configuration, cancellationToken); + using var spectralConverter = new SpectralConverter(this.Configuration, cancellationToken); this.scanDecoder = new HuffmanScanDecoder(stream, spectralConverter, cancellationToken); From 1d4dd088283f57dcc256ceb944c1aebfb558e5c1 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Fri, 9 Jul 2021 23:17:01 +0300 Subject: [PATCH 23/76] Implemented step-based iteration for spectralconverter --- .../Decoder/JpegComponentPostProcessor.cs | 10 ++++++-- .../Decoder/SpectralConverter{TPixel}.cs | 24 +++++++++---------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs index 83406e0d67..9eafaea0ae 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs @@ -71,16 +71,18 @@ public void Dispose() /// /// Invoke for block rows, copy the result into . /// - public void CopyBlocksToColorBuffer() + public void CopyBlocksToColorBuffer(int step) { var blockPp = new JpegBlockPostProcessor(this.RawJpeg, this.Component); float maximumValue = MathF.Pow(2, this.RawJpeg.Precision) - 1; int destAreaStride = this.ColorBuffer.Width; + int yBlockStart = step * this.BlockRowsPerStep; + for (int y = 0; y < this.BlockRowsPerStep; y++) { - int yBlock = this.currentComponentRowInBlocks + y; + int yBlock = yBlockStart + y; if (yBlock >= this.SizeInBlocks.Height) { @@ -104,7 +106,11 @@ public void CopyBlocksToColorBuffer() blockPp.ProcessBlockColorsInto(ref block, ref destAreaOrigin, destAreaStride, maximumValue); } } + } + public void CopyBlocksToColorBuffer() + { + this.CopyBlocksToColorBuffer(this.currentComponentRowInBlocks); this.currentComponentRowInBlocks += this.BlockRowsPerStep; } } diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index ec1c057b27..16a55e95eb 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -17,7 +17,7 @@ internal abstract class SpectralConverter : IDisposable { public abstract void InjectFrameData(JpegFrame frame, IRawJpegData jpegData); - public abstract void ConvertStride(); + public abstract void ConvertStride(int step); public abstract void Dispose(); } @@ -44,8 +44,6 @@ internal class SpectralConverter : SpectralConverter private int PixelRowsPerStep; - private int PixelRowCounter; - private bool converted; @@ -55,10 +53,12 @@ public Buffer2D PixelBuffer { if (!this.converted) { - while (this.PixelRowCounter < this.pixelBuffer.Height) + int steps = (int)Math.Ceiling(this.pixelBuffer.Height / (float)this.PixelRowsPerStep); + + for (int i = 0; i < steps; i++) { this.cancellationToken.ThrowIfCancellationRequested(); - this.ConvertStride(); + this.ConvertStride(i); } this.converted = true; @@ -103,20 +103,22 @@ public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) this.colorConverter = JpegColorConverter.GetConverter(jpegData.ColorSpace, frame.Precision); } - public override void ConvertStride() + public override void ConvertStride(int step) { - int maxY = Math.Min(this.pixelBuffer.Height, this.PixelRowCounter + this.PixelRowsPerStep); + int pixelRowStart = this.PixelRowsPerStep * step; + + int maxY = Math.Min(this.pixelBuffer.Height, pixelRowStart + this.PixelRowsPerStep); var buffers = new Buffer2D[this.componentProcessors.Length]; for (int i = 0; i < this.componentProcessors.Length; i++) { - this.componentProcessors[i].CopyBlocksToColorBuffer(); + this.componentProcessors[i].CopyBlocksToColorBuffer(step); buffers[i] = this.componentProcessors[i].ColorBuffer; } - for (int yy = this.PixelRowCounter; yy < maxY; yy++) + for (int yy = pixelRowStart; yy < maxY; yy++) { - int y = yy - this.PixelRowCounter; + int y = yy - pixelRowStart; var values = new JpegColorConverter.ComponentValues(buffers, y); this.colorConverter.ConvertToRgba(values, this.rgbaBuffer.GetSpan()); @@ -126,8 +128,6 @@ public override void ConvertStride() // TODO: Investigate if slicing is actually necessary PixelOperations.Instance.FromVector4Destructive(this.configuration, this.rgbaBuffer.GetSpan().Slice(0, destRow.Length), destRow); } - - this.PixelRowCounter += this.PixelRowsPerStep; } public override void Dispose() From fa0aaec88e8792f073f53dc691f59965f17905f7 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sat, 10 Jul 2021 15:05:31 +0300 Subject: [PATCH 24/76] Added separate step parameter for spectral data enumeration --- .../Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index 16a55e95eb..11feaa189b 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -17,7 +17,7 @@ internal abstract class SpectralConverter : IDisposable { public abstract void InjectFrameData(JpegFrame frame, IRawJpegData jpegData); - public abstract void ConvertStride(int step); + public abstract void ConvertStride(int step, int spectralStep); public abstract void Dispose(); } @@ -58,7 +58,7 @@ public Buffer2D PixelBuffer for (int i = 0; i < steps; i++) { this.cancellationToken.ThrowIfCancellationRequested(); - this.ConvertStride(i); + this.ConvertStride(i, i); } this.converted = true; @@ -103,7 +103,7 @@ public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) this.colorConverter = JpegColorConverter.GetConverter(jpegData.ColorSpace, frame.Precision); } - public override void ConvertStride(int step) + public override void ConvertStride(int step, int spectralStep) { int pixelRowStart = this.PixelRowsPerStep * step; @@ -112,7 +112,7 @@ public override void ConvertStride(int step) var buffers = new Buffer2D[this.componentProcessors.Length]; for (int i = 0; i < this.componentProcessors.Length; i++) { - this.componentProcessors[i].CopyBlocksToColorBuffer(step); + this.componentProcessors[i].CopyBlocksToColorBuffer(spectralStep); buffers[i] = this.componentProcessors[i].ColorBuffer; } From c0173571d31d5702fb8806cb81064eb69a3e51e7 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sat, 10 Jul 2021 15:23:44 +0300 Subject: [PATCH 25/76] Added external way to mark convesion finished --- .../Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index 11feaa189b..580adb3ab7 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -15,6 +15,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder { internal abstract class SpectralConverter : IDisposable { + public abstract void CommitConversion(); + public abstract void InjectFrameData(JpegFrame frame, IRawJpegData jpegData); public abstract void ConvertStride(int step, int spectralStep); @@ -74,6 +76,8 @@ public SpectralConverter(Configuration configuration, CancellationToken ct) this.cancellationToken = ct; } + public override void CommitConversion() => this.converted = true; + public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) { MemoryAllocator allocator = this.configuration.MemoryAllocator; From 3c59cd9c51c560c1465543b74145ea3522857a82 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sat, 10 Jul 2021 15:41:33 +0300 Subject: [PATCH 26/76] Added initial support for the baseline interleaved stride conversion --- .../Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs index 3980739e1f..914562831d 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs @@ -133,6 +133,9 @@ private void ParseBaselineData() else { this.ParseBaselineDataInterleaved(); + + // this is the only path where conversion is done right after the scan + this.spectralConverter.CommitConversion(); } } @@ -160,6 +163,7 @@ private void ParseBaselineDataInterleaved() { this.cancellationToken.ThrowIfCancellationRequested(); + // decode from binary to spectral for (int i = 0; i < mcusPerLine; i++) { // Scan an interleaved mcu... process components in order @@ -207,6 +211,9 @@ ref Unsafe.Add(ref blockRef, blockCol), mcu++; this.HandleRestart(); } + + // convert from spectral to actual pixels via given converter + this.spectralConverter.ConvertStride(j, j); } } From 460b02c776600ba5696d2d5457f17ecb03f82f93 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sat, 10 Jul 2021 18:31:06 +0300 Subject: [PATCH 27/76] Sandbox changes --- tests/ImageSharp.Tests.ProfilingSandbox/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs b/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs index b51592b6d9..7876c7dc60 100644 --- a/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs +++ b/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs @@ -71,9 +71,9 @@ public static void Main(string[] args) //Test_DebugRun("baseline_4k_420", false); //Test_DebugRun("baseline_s444_q100", false); //Test_DebugRun("progressive_s420_q100", false); - Test_DebugRun("baseline_4k_420", true); + //Test_DebugRun("baseline_4k_420", true); Test_DebugRun("baseline_s444_q100", true); - Test_DebugRun("progressive_s420_q100", true); + //Test_DebugRun("progressive_s420_q100", true); //Console.ReadLine(); } From e39adf85f6c7add528325f1d557411e9751e7bcb Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 00:01:37 +0300 Subject: [PATCH 28/76] 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 @@ private void ParseBaselineDataInterleaved() 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 @@ ref Unsafe.Add(ref blockRef, blockCol), } // 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 @@ public void CopyBlocksToColorBuffer(int step) } } + 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 @@ internal abstract class SpectralConverter : IDisposable public abstract void ConvertStride(int step, int spectralStep); + public abstract void ClearStride(int spectralStep); + public abstract void Dispose(); } @@ -134,6 +136,14 @@ public override void ConvertStride(int step, int spectralStep) } } + 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) From 7afca199b769236cb12982f0412770ea98cfbb0e Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 10:01:12 +0300 Subject: [PATCH 29/76] Rolled back to counter enumeration for spectral converter --- .../Decoder/SpectralConverter{TPixel}.cs | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index 3499704aed..706e4c0b7b 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -19,14 +19,14 @@ internal abstract class SpectralConverter : IDisposable public abstract void InjectFrameData(JpegFrame frame, IRawJpegData jpegData); - public abstract void ConvertStride(int step, int spectralStep); + public abstract void ConvertStride(int spectralStep); public abstract void ClearStride(int spectralStep); public abstract void Dispose(); } - internal class SpectralConverter : SpectralConverter + internal sealed class SpectralConverter : SpectralConverter where TPixel : unmanaged, IPixel { private Configuration configuration; @@ -48,6 +48,8 @@ internal class SpectralConverter : SpectralConverter private int PixelRowsPerStep; + private int PixelRowCounter; + private bool converted; @@ -62,7 +64,7 @@ public Buffer2D PixelBuffer for (int i = 0; i < steps; i++) { this.cancellationToken.ThrowIfCancellationRequested(); - this.ConvertStride(i, i); + this.ConvertStride(i); } this.converted = true; @@ -109,11 +111,9 @@ public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) this.colorConverter = JpegColorConverter.GetConverter(jpegData.ColorSpace, frame.Precision); } - public override void ConvertStride(int step, int spectralStep) + public override void ConvertStride(int spectralStep) { - int pixelRowStart = this.PixelRowsPerStep * step; - - int maxY = Math.Min(this.pixelBuffer.Height, pixelRowStart + this.PixelRowsPerStep); + int maxY = Math.Min(this.pixelBuffer.Height, this.PixelRowCounter + this.PixelRowsPerStep); var buffers = new Buffer2D[this.componentProcessors.Length]; for (int i = 0; i < this.componentProcessors.Length; i++) @@ -122,9 +122,9 @@ public override void ConvertStride(int step, int spectralStep) buffers[i] = this.componentProcessors[i].ColorBuffer; } - for (int yy = pixelRowStart; yy < maxY; yy++) + for (int yy = this.PixelRowCounter; yy < maxY; yy++) { - int y = yy - pixelRowStart; + int y = yy - this.PixelRowCounter; var values = new JpegColorConverter.ComponentValues(buffers, y); this.colorConverter.ConvertToRgba(values, this.rgbaBuffer.GetSpan()); @@ -134,13 +134,15 @@ public override void ConvertStride(int step, int spectralStep) // TODO: Investigate if slicing is actually necessary PixelOperations.Instance.FromVector4Destructive(this.configuration, this.rgbaBuffer.GetSpan().Slice(0, destRow.Length), destRow); } + + this.PixelRowCounter += this.PixelRowsPerStep; } public override void ClearStride(int spectralStep) { foreach (JpegComponentPostProcessor cpp in this.componentProcessors) { - cpp.ClearSpectralStride(spectralStep); + cpp.ClearSpectralBuffers(spectralStep); } } From 4b5f0f69aad44bb5f5a36d84aa0489f194fe0d75 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 10:12:43 +0300 Subject: [PATCH 30/76] Refactored scan converter --- .../Components/Decoder/HuffmanScanDecoder.cs | 3 +- .../Decoder/SpectralConverter{TPixel}.cs | 32 ++++++++++++------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs index 0e13f75e44..6096722717 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs @@ -213,8 +213,7 @@ ref Unsafe.Add(ref blockRef, blockCol), } // convert from spectral to actual pixels via given converter - this.spectralConverter.ConvertStride(j, 0); - this.spectralConverter.ClearStride(0); + this.spectralConverter.ConvertStrideBaseline(); } } diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index 706e4c0b7b..9264039c4b 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -19,9 +19,9 @@ internal abstract class SpectralConverter : IDisposable public abstract void InjectFrameData(JpegFrame frame, IRawJpegData jpegData); - public abstract void ConvertStride(int spectralStep); + public abstract void ConvertStrideIncremental(); - public abstract void ClearStride(int spectralStep); + public abstract void ConvertStrideBaseline(); public abstract void Dispose(); } @@ -111,7 +111,25 @@ public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) this.colorConverter = JpegColorConverter.GetConverter(jpegData.ColorSpace, frame.Precision); } - public override void ConvertStride(int spectralStep) + public override void ConvertStrideIncremental() => this.ConvertNextStride(this.PixelRowCounter / 8); + + public override void ConvertStrideBaseline() + { + // Convert next pixel stride using single spectral `stride' + // Note that zero passing eliminates the need of virtual call from JpegComponentPostProcessor + this.ConvertNextStride(spectralStep: 0); + + // Clear spectral stride - this is VERY important as jpeg possibly won't fill entire buffer each stride + // Which leads to decoding artifacts + // Note that this code clears all buffers of the post processors, it's their responsibility to allocate only single stride + foreach (JpegComponentPostProcessor cpp in this.componentProcessors) + { + cpp.ClearSpectralBuffers(); + } + } + + + private void ConvertNextStride(int spectralStep) { int maxY = Math.Min(this.pixelBuffer.Height, this.PixelRowCounter + this.PixelRowsPerStep); @@ -138,14 +156,6 @@ public override void ConvertStride(int spectralStep) this.PixelRowCounter += this.PixelRowsPerStep; } - public override void ClearStride(int spectralStep) - { - foreach (JpegComponentPostProcessor cpp in this.componentProcessors) - { - cpp.ClearSpectralBuffers(spectralStep); - } - } - public override void Dispose() { foreach (JpegComponentPostProcessor cpp in this.componentProcessors) From 74a7e90cf5e75eee5087d10b4f7fa4a1bab32add Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 10:29:10 +0300 Subject: [PATCH 31/76] Refactores post processor buffer clear --- .../Decoder/JpegComponentPostProcessor.cs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs index 6c25601c28..067eb47bef 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs @@ -108,19 +108,13 @@ public void CopyBlocksToColorBuffer(int step) } } - public void ClearSpectralStride(int step) + // TODO: refactor this + public void ClearSpectralBuffers() { - int yBlockStart = step * this.BlockRowsPerStep; - for (int y = 0; y < this.BlockRowsPerStep; y++) + Buffer2D spectralBlocks = this.Component.SpectralBlocks; + for (int i = 0; i < spectralBlocks.Height; i++) { - int yBlock = yBlockStart + y; - - if (yBlock >= this.SizeInBlocks.Height) - { - break; - } - - this.Component.SpectralBlocks.GetRowSpan(yBlock).Clear(); + spectralBlocks.GetRowSpan(i).Clear(); } } From 4af7fd1dc76307a6feb77f4c56759634bf2d8990 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 10:29:19 +0300 Subject: [PATCH 32/76] Refactored spectral converter --- .../Decoder/SpectralConverter{TPixel}.cs | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index 9264039c4b..c0e6151373 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -19,8 +19,6 @@ internal abstract class SpectralConverter : IDisposable public abstract void InjectFrameData(JpegFrame frame, IRawJpegData jpegData); - public abstract void ConvertStrideIncremental(); - public abstract void ConvertStrideBaseline(); public abstract void Dispose(); @@ -61,10 +59,10 @@ public Buffer2D PixelBuffer { int steps = (int)Math.Ceiling(this.pixelBuffer.Height / (float)this.PixelRowsPerStep); - for (int i = 0; i < steps; i++) + for (int step = 0; step < steps; step++) { this.cancellationToken.ThrowIfCancellationRequested(); - this.ConvertStride(i); + this.ConvertNextStride(step); } this.converted = true; @@ -111,8 +109,6 @@ public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) this.colorConverter = JpegColorConverter.GetConverter(jpegData.ColorSpace, frame.Precision); } - public override void ConvertStrideIncremental() => this.ConvertNextStride(this.PixelRowCounter / 8); - public override void ConvertStrideBaseline() { // Convert next pixel stride using single spectral `stride' @@ -128,6 +124,15 @@ public override void ConvertStrideBaseline() } } + public override void Dispose() + { + foreach (JpegComponentPostProcessor cpp in this.componentProcessors) + { + cpp.Dispose(); + } + + this.rgbaBuffer.Dispose(); + } private void ConvertNextStride(int spectralStep) { @@ -155,15 +160,5 @@ private void ConvertNextStride(int spectralStep) this.PixelRowCounter += this.PixelRowsPerStep; } - - public override void Dispose() - { - foreach (JpegComponentPostProcessor cpp in this.componentProcessors) - { - cpp.Dispose(); - } - - this.rgbaBuffer.Dispose(); - } } } From fae763edd3701de39953f4b31e66719e8a65c490 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 11:07:59 +0300 Subject: [PATCH 33/76] Final refactor of the converter --- .../Components/Decoder/HuffmanScanDecoder.cs | 3 --- .../Decoder/SpectralConverter{TPixel}.cs | 25 ++++++------------- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs index 6096722717..34afa72b43 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs @@ -133,9 +133,6 @@ private void ParseBaselineData() else { this.ParseBaselineDataInterleaved(); - - // this is the only path where conversion is done right after the scan - this.spectralConverter.CommitConversion(); } } diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index c0e6151373..71e37ba8a9 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -14,9 +14,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder { internal abstract class SpectralConverter : IDisposable - { - public abstract void CommitConversion(); - + { public abstract void InjectFrameData(JpegFrame frame, IRawJpegData jpegData); public abstract void ConvertStrideBaseline(); @@ -40,22 +38,25 @@ internal sealed class SpectralConverter : SpectralConverter private Buffer2D pixelBuffer; - - public int BlockRowsPerStep; private int PixelRowsPerStep; private int PixelRowCounter; + public SpectralConverter(Configuration configuration, CancellationToken ct) + { + this.configuration = configuration; + this.cancellationToken = ct; + } - private bool converted; + private bool Converted => this.PixelRowCounter >= this.pixelBuffer.Height; public Buffer2D PixelBuffer { get { - if (!this.converted) + if (!this.Converted) { int steps = (int)Math.Ceiling(this.pixelBuffer.Height / (float)this.PixelRowsPerStep); @@ -64,22 +65,12 @@ public Buffer2D PixelBuffer this.cancellationToken.ThrowIfCancellationRequested(); this.ConvertNextStride(step); } - - this.converted = true; } return this.pixelBuffer; } } - public SpectralConverter(Configuration configuration, CancellationToken ct) - { - this.configuration = configuration; - this.cancellationToken = ct; - } - - public override void CommitConversion() => this.converted = true; - public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) { MemoryAllocator allocator = this.configuration.MemoryAllocator; From 243e2bd463ceabee2157bd2b69639feb4956e5de Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 11:27:58 +0300 Subject: [PATCH 34/76] Removed todo --- .../Jpeg/Components/Decoder/JpegComponentPostProcessor.cs | 1 - .../Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs index 067eb47bef..7ad0e87d21 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs @@ -108,7 +108,6 @@ public void CopyBlocksToColorBuffer(int step) } } - // TODO: refactor this public void ClearSpectralBuffers() { Buffer2D spectralBlocks = this.Component.SpectralBlocks; diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index 71e37ba8a9..441745e535 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -14,7 +14,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder { internal abstract class SpectralConverter : IDisposable - { + { public abstract void InjectFrameData(JpegFrame frame, IRawJpegData jpegData); public abstract void ConvertStrideBaseline(); From 639ed629a8594dbba1dc3e11f02985899ad0a9ec Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 12:21:03 +0300 Subject: [PATCH 35/76] Implemented new spectral buffers allocation --- .../Jpeg/Components/Decoder/HuffmanScanDecoder.cs | 4 ++++ .../Formats/Jpeg/Components/Decoder/JpegComponent.cs | 12 ++++++++++++ .../Formats/Jpeg/Components/Decoder/JpegFrame.cs | 9 +++++++++ src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs | 3 ++- 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs index 34afa72b43..447f2c6435 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs @@ -128,10 +128,13 @@ private void ParseBaselineData() { if (this.componentsLength == 1) { + this.frame.AllocateComponents(fullScan: true); this.ParseBaselineDataNonInterleaved(); } else { + // interleaved baseline is the only place where we can optimize memory footprint via reusing single spectral stride + this.frame.AllocateComponents(fullScan: false); this.ParseBaselineDataInterleaved(); } } @@ -305,6 +308,7 @@ private void ParseProgressiveData() { 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 dd43baa233..05f46aabaa 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs @@ -125,6 +125,18 @@ public void Init() { JpegThrowHelper.ThrowBadSampling(); } + } + + public void AllocateSpectral(bool fullScan) + { + if (this.SpectralBlocks != null) + { + // this method will be called each scan marker so we need to allocate only once + return; + } + + int blocksPerLineForMcu = this.Frame.McusPerLine * this.HorizontalSamplingFactor; + int blocksPerColumnForMcu = fullScan ? this.Frame.McusPerColumn * this.VerticalSamplingFactor : this.VerticalSamplingFactor; int totalNumberOfBlocks = blocksPerColumnForMcu * (blocksPerLineForMcu + 1); int width = this.WidthInBlocks + 1; diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs index 13d6bc35ec..595f39dbd3 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs @@ -104,5 +104,14 @@ public void InitComponents() component.Init(); } } + + public void AllocateComponents(bool fullScan) + { + for (int i = 0; i < this.ComponentCount; i++) + { + JpegComponent component = this.Components[i]; + component.AllocateSpectral(fullScan); + } + } } } diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 30024af95f..1289ff3bff 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -921,9 +921,10 @@ private void ProcessStartOfFrameMarker(BufferedReadStream stream, int remaining, this.Frame.MaxVerticalFactor = maxV; this.ColorSpace = this.DeduceJpegColorSpace(); this.Metadata.GetJpegMetadata().ColorType = this.ColorSpace == JpegColorSpace.Grayscale ? JpegColorType.Luminance : JpegColorType.YCbCr; - this.Frame.InitComponents(); this.ImageSizeInMCU = new Size(this.Frame.McusPerLine, this.Frame.McusPerColumn); + this.Frame.InitComponents(); + // This can be injected in SOF marker callback this.scanDecoder.InjectFrameData(this.Frame, this); } From 27d7c3a7695a47bbe0785bb6234e44181fb2ba73 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 12:44:47 +0300 Subject: [PATCH 36/76] Rolled back to initial sandbox code --- .../Program.cs | 70 +------------------ 1 file changed, 1 insertion(+), 69 deletions(-) diff --git a/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs b/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs index 7876c7dc60..9aa983ac5f 100644 --- a/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs +++ b/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs @@ -2,8 +2,6 @@ // Licensed under the Apache License, Version 2.0. using System; -using System.Diagnostics; -using System.IO; using SixLabors.ImageSharp.Tests.Formats.Jpg; using SixLabors.ImageSharp.Tests.PixelFormats.PixelOperations; using SixLabors.ImageSharp.Tests.ProfilingBenchmarks; @@ -39,73 +37,7 @@ public static void Main(string[] args) // RunToVector4ProfilingTest(); // RunResizeProfilingTest(); - //Test_Performance(20); - - //Test_DebugRun("chroma_444_16x16", true); - //Console.WriteLine(); - //Test_DebugRun("chroma_420_16x16", true); - //Console.WriteLine(); - //Test_DebugRun("444_14x14"); - //Console.WriteLine(); - //Test_DebugRun("baseline_4k_444", false); - //Console.WriteLine(); - //Test_DebugRun("progressive_4k_444", true); - //Console.WriteLine(); - //Test_DebugRun("baseline_4k_420", false); - //Console.WriteLine(); - //Test_DebugRun("cmyk_jpeg"); - //Console.WriteLine(); - //Test_DebugRun("Channel_digital_image_CMYK_color"); - //Console.WriteLine(); - - //Test_DebugRun("test_baseline_4k_444", false); - //Console.WriteLine(); - //Test_DebugRun("test_progressive_4k_444", false); - //Console.WriteLine(); - //Test_DebugRun("test_baseline_4k_420", false); - //Console.WriteLine(); - - // Binary size of this must be ~2096kb - //Test_DebugRun("422", true); - - //Test_DebugRun("baseline_4k_420", false); - //Test_DebugRun("baseline_s444_q100", false); - //Test_DebugRun("progressive_s420_q100", false); - //Test_DebugRun("baseline_4k_420", true); - Test_DebugRun("baseline_s444_q100", true); - //Test_DebugRun("progressive_s420_q100", true); - - //Console.ReadLine(); - } - - public static void Test_Performance(int iterations) - { - using var stream = new MemoryStream(File.ReadAllBytes("C:\\Users\\pl4nu\\Downloads\\progressive_4k_444.jpg")); - //using var stream = new MemoryStream(File.ReadAllBytes("C:\\Users\\pl4nu\\Downloads\\baseline_4k_444.jpg")); - var sw = new Stopwatch(); - sw.Start(); - for (int i = 0; i < iterations; i++) - { - using var img = Image.Load(stream); - stream.Position = 0; - } - - sw.Stop(); - Console.WriteLine($"Elapsed: {sw.ElapsedMilliseconds}ms\nPer invocation: {sw.ElapsedMilliseconds / iterations}ms"); - } - - public static void Test_DebugRun(string name, bool save = false) - { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine($"img: {name}"); - Console.ResetColor(); - using var img = Image.Load($"C:\\Users\\pl4nu\\Downloads\\{name}.jpg"); - - if (save) - { - img.SaveAsJpeg($"C:\\Users\\pl4nu\\Downloads\\test_{name}.jpg", - new ImageSharp.Formats.Jpeg.JpegEncoder { Subsample = ImageSharp.Formats.Jpeg.JpegSubsample.Ratio444, Quality = 100 }); - } + Console.ReadLine(); } private static void RunJpegEncoderProfilingTests() From da7bca3786d56b54bd9ca526883a958dca56e35f Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 13:07:08 +0300 Subject: [PATCH 37/76] Moved SpectralConverter to the separate file --- .../Jpeg/Components/Decoder/SpectralConverter.cs | 16 ++++++++++++++++ .../Decoder/SpectralConverter{TPixel}.cs | 9 --------- 2 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter.cs diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter.cs new file mode 100644 index 0000000000..35a8790824 --- /dev/null +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter.cs @@ -0,0 +1,16 @@ +// Copyright (c) Six Labors. +// Licensed under the Apache License, Version 2.0. + +using System; + +namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder +{ + internal abstract class SpectralConverter : IDisposable + { + public abstract void InjectFrameData(JpegFrame frame, IRawJpegData jpegData); + + public abstract void ConvertStrideBaseline(); + + public abstract void Dispose(); + } +} diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index 441745e535..a25e756c73 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -13,15 +13,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder { - internal abstract class SpectralConverter : IDisposable - { - public abstract void InjectFrameData(JpegFrame frame, IRawJpegData jpegData); - - public abstract void ConvertStrideBaseline(); - - public abstract void Dispose(); - } - internal sealed class SpectralConverter : SpectralConverter where TPixel : unmanaged, IPixel { From 86a7b462c4205302fab8443642651e27c63c548d Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 13:07:46 +0300 Subject: [PATCH 38/76] Added docs --- src/ImageSharp/Image{TPixel}.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/ImageSharp/Image{TPixel}.cs b/src/ImageSharp/Image{TPixel}.cs index 669db2a97b..fb1e6d92f3 100644 --- a/src/ImageSharp/Image{TPixel}.cs +++ b/src/ImageSharp/Image{TPixel}.cs @@ -87,6 +87,13 @@ internal Image(Configuration configuration, int width, int height, ImageMetadata this.frames = new ImageFrameCollection(this, width, height, default(TPixel)); } + /// + /// Initializes a new instance of the class + /// wrapping an external + /// The configuration providing initialization code which allows extending the library. + /// Pixel buffer. + /// The images metadata. internal Image( Configuration configuration, Buffer2D pixelBuffer, From d325d06b5fa00806b0336e8427383f3d852c8b0e Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 13:14:42 +0300 Subject: [PATCH 39/76] Fixed styling issues --- .../Components/Decoder/HuffmanScanDecoder.cs | 143 +++++++++--------- .../Decoder/JpegComponentPostProcessor.cs | 8 +- .../Decoder/JpegImagePostProcessor.cs | 27 ++-- .../Decoder/SpectralConverter{TPixel}.cs | 29 ++-- .../Formats/Jpeg/JpegDecoderCore.cs | 21 +-- 5 files changed, 108 insertions(+), 120 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs index 447f2c6435..7a63ef7c66 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs @@ -18,43 +18,16 @@ internal class HuffmanScanDecoder { private readonly BufferedReadStream stream; - // huffman tables - public HuffmanTable[] dcHuffmanTables; - public HuffmanTable[] acHuffmanTables; - // Frame related private JpegFrame frame; private JpegComponent[] components; // The restart interval. private int restartInterval; + // How many mcu's are left to do. private int todo; - public int ResetInterval - { - set - { - restartInterval = value; - todo = value; - } - } - - // The number of interleaved components. - public int componentsLength; - - // The spectral selection start. - public int spectralStart; - - // The spectral selection end. - public int spectralEnd; - - // The successive approximation high bit end. - public int successiveHigh; - - // The successive approximation low bit end. - public int successiveLow; - // The End-Of-Block countdown for ending the sequence prematurely when the remaining coefficients are zero. private int eobrun; @@ -63,7 +36,7 @@ public int ResetInterval private HuffmanScanBuffer scanBuffer; - private SpectralConverter spectralConverter; + private readonly SpectralConverter spectralConverter; private CancellationToken cancellationToken; @@ -71,15 +44,7 @@ public int ResetInterval /// Initializes a new instance of the class. /// /// The input stream. - /// The image frame. - /// The DC Huffman tables. - /// The AC Huffman tables. - /// The length of the components. Different to the array length. - /// The reset interval. - /// The spectral selection start. - /// The spectral selection end. - /// The successive approximation bit high end. - /// The successive approximation bit low end. + /// Spectral to pixel converter. /// The token to monitor cancellation. public HuffmanScanDecoder( BufferedReadStream stream, @@ -92,6 +57,36 @@ public HuffmanScanDecoder( this.cancellationToken = cancellationToken; } + // huffman tables + public HuffmanTable[] DcHuffmanTables { get; set; } + + public HuffmanTable[] AcHuffmanTables { get; set; } + + // Reset interval + public int ResetInterval + { + set + { + this.restartInterval = value; + this.todo = value; + } + } + + // The number of interleaved components. + public int ComponentsLength { get; set; } + + // The spectral selection start. + public int SpectralStart { get; set; } + + // The spectral selection end. + public int SpectralEnd { get; set; } + + // The successive approximation high bit end. + public int SuccessiveHigh { get; set; } + + // The successive approximation low bit end. + public int SuccessiveLow { get; set; } + /// /// Decodes the entropy coded data. /// @@ -126,7 +121,7 @@ public void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) private void ParseBaselineData() { - if (this.componentsLength == 1) + if (this.ComponentsLength == 1) { this.frame.AllocateComponents(fullScan: true); this.ParseBaselineDataNonInterleaved(); @@ -148,13 +143,13 @@ private void ParseBaselineDataInterleaved() ref HuffmanScanBuffer buffer = ref this.scanBuffer; // Pre-derive the huffman table to avoid in-loop checks. - for (int i = 0; i < this.componentsLength; i++) + for (int i = 0; i < this.ComponentsLength; i++) { int order = this.frame.ComponentOrder[i]; JpegComponent component = this.components[order]; - ref HuffmanTable dcHuffmanTable = ref this.dcHuffmanTables[component.DCHuffmanTableId]; - ref HuffmanTable acHuffmanTable = ref this.acHuffmanTables[component.ACHuffmanTableId]; + ref HuffmanTable dcHuffmanTable = ref this.DcHuffmanTables[component.DCHuffmanTableId]; + ref HuffmanTable acHuffmanTable = ref this.AcHuffmanTables[component.ACHuffmanTableId]; dcHuffmanTable.Configure(); acHuffmanTable.Configure(); } @@ -169,13 +164,13 @@ private void ParseBaselineDataInterleaved() // Scan an interleaved mcu... process components in order int mcuRow = mcu / mcusPerLine; int mcuCol = mcu % mcusPerLine; - for (int k = 0; k < this.componentsLength; k++) + for (int k = 0; k < this.ComponentsLength; k++) { int order = this.frame.ComponentOrder[k]; JpegComponent component = this.components[order]; - ref HuffmanTable dcHuffmanTable = ref this.dcHuffmanTables[component.DCHuffmanTableId]; - ref HuffmanTable acHuffmanTable = ref this.acHuffmanTables[component.ACHuffmanTableId]; + ref HuffmanTable dcHuffmanTable = ref this.DcHuffmanTables[component.DCHuffmanTableId]; + ref HuffmanTable acHuffmanTable = ref this.AcHuffmanTables[component.ACHuffmanTableId]; int h = component.HorizontalSamplingFactor; int v = component.VerticalSamplingFactor; @@ -225,8 +220,8 @@ private void ParseBaselineDataNonInterleaved() int w = component.WidthInBlocks; int h = component.HeightInBlocks; - ref HuffmanTable dcHuffmanTable = ref this.dcHuffmanTables[component.DCHuffmanTableId]; - ref HuffmanTable acHuffmanTable = ref this.acHuffmanTables[component.ACHuffmanTableId]; + ref HuffmanTable dcHuffmanTable = ref this.DcHuffmanTables[component.DCHuffmanTableId]; + ref HuffmanTable acHuffmanTable = ref this.AcHuffmanTables[component.ACHuffmanTableId]; dcHuffmanTable.Configure(); acHuffmanTable.Configure(); @@ -260,9 +255,9 @@ private void CheckProgressiveData() // Logic has been adapted from libjpeg. // See Table B.3 – Scan header parameter size and values. itu-t81.pdf bool invalid = false; - if (this.spectralStart == 0) + if (this.SpectralStart == 0) { - if (this.spectralEnd != 0) + if (this.SpectralEnd != 0) { invalid = true; } @@ -270,22 +265,22 @@ private void CheckProgressiveData() else { // Need not check Ss/Se < 0 since they came from unsigned bytes. - if (this.spectralEnd < this.spectralStart || this.spectralEnd > 63) + if (this.SpectralEnd < this.SpectralStart || this.SpectralEnd > 63) { invalid = true; } // AC scans may have only one component. - if (this.componentsLength != 1) + if (this.ComponentsLength != 1) { invalid = true; } } - if (this.successiveHigh != 0) + if (this.SuccessiveHigh != 0) { // Successive approximation refinement scan: must have Al = Ah-1. - if (this.successiveHigh - 1 != this.successiveLow) + if (this.SuccessiveHigh - 1 != this.SuccessiveLow) { invalid = true; } @@ -293,14 +288,14 @@ private void CheckProgressiveData() // TODO: How does this affect 12bit jpegs. // According to libjpeg the range covers 8bit only? - if (this.successiveLow > 13) + if (this.SuccessiveLow > 13) { invalid = true; } if (invalid) { - JpegThrowHelper.ThrowBadProgressiveScan(this.spectralStart, this.spectralEnd, this.successiveHigh, this.successiveLow); + JpegThrowHelper.ThrowBadProgressiveScan(this.SpectralStart, this.SpectralEnd, this.SuccessiveHigh, this.SuccessiveLow); } } @@ -309,7 +304,7 @@ private void ParseProgressiveData() this.CheckProgressiveData(); this.frame.AllocateComponents(fullScan: true); - if (this.componentsLength == 1) + if (this.ComponentsLength == 1) { this.ParseProgressiveDataNonInterleaved(); } @@ -328,11 +323,11 @@ private void ParseProgressiveDataInterleaved() ref HuffmanScanBuffer buffer = ref this.scanBuffer; // Pre-derive the huffman table to avoid in-loop checks. - for (int k = 0; k < this.componentsLength; k++) + for (int k = 0; k < this.ComponentsLength; k++) { int order = this.frame.ComponentOrder[k]; JpegComponent component = this.components[order]; - ref HuffmanTable dcHuffmanTable = ref this.dcHuffmanTables[component.DCHuffmanTableId]; + ref HuffmanTable dcHuffmanTable = ref this.DcHuffmanTables[component.DCHuffmanTableId]; dcHuffmanTable.Configure(); } @@ -343,11 +338,11 @@ private void ParseProgressiveDataInterleaved() // Scan an interleaved mcu... process components in order int mcuRow = mcu / mcusPerLine; int mcuCol = mcu % mcusPerLine; - for (int k = 0; k < this.componentsLength; k++) + for (int k = 0; k < this.ComponentsLength; k++) { int order = this.frame.ComponentOrder[k]; JpegComponent component = this.components[order]; - ref HuffmanTable dcHuffmanTable = ref this.dcHuffmanTables[component.DCHuffmanTableId]; + ref HuffmanTable dcHuffmanTable = ref this.DcHuffmanTables[component.DCHuffmanTableId]; int h = component.HorizontalSamplingFactor; int v = component.VerticalSamplingFactor; @@ -393,9 +388,9 @@ private void ParseProgressiveDataNonInterleaved() int w = component.WidthInBlocks; int h = component.HeightInBlocks; - if (this.spectralStart == 0) + if (this.SpectralStart == 0) { - ref HuffmanTable dcHuffmanTable = ref this.dcHuffmanTables[component.DCHuffmanTableId]; + ref HuffmanTable dcHuffmanTable = ref this.DcHuffmanTables[component.DCHuffmanTableId]; dcHuffmanTable.Configure(); for (int j = 0; j < h; j++) @@ -423,7 +418,7 @@ ref Unsafe.Add(ref blockRef, i), } else { - ref HuffmanTable acHuffmanTable = ref this.acHuffmanTables[component.ACHuffmanTableId]; + ref HuffmanTable acHuffmanTable = ref this.AcHuffmanTables[component.ACHuffmanTableId]; acHuffmanTable.Configure(); for (int j = 0; j < h; j++) @@ -502,7 +497,7 @@ private void DecodeBlockProgressiveDC(JpegComponent component, ref Block8x8 bloc ref short blockDataRef = ref Unsafe.As(ref block); ref HuffmanScanBuffer buffer = ref this.scanBuffer; - if (this.successiveHigh == 0) + if (this.SuccessiveHigh == 0) { // First scan for DC coefficient, must be first int s = buffer.DecodeHuffman(ref dcTable); @@ -513,20 +508,20 @@ private void DecodeBlockProgressiveDC(JpegComponent component, ref Block8x8 bloc s += component.DcPredictor; component.DcPredictor = s; - blockDataRef = (short)(s << this.successiveLow); + blockDataRef = (short)(s << this.SuccessiveLow); } else { // Refinement scan for DC coefficient buffer.CheckBits(); - blockDataRef |= (short)(buffer.GetBits(1) << this.successiveLow); + blockDataRef |= (short)(buffer.GetBits(1) << this.SuccessiveLow); } } private void DecodeBlockProgressiveAC(ref Block8x8 block, ref HuffmanTable acTable) { ref short blockDataRef = ref Unsafe.As(ref block); - if (this.successiveHigh == 0) + if (this.SuccessiveHigh == 0) { // MCU decoding for AC initial scan (either spectral selection, // or first pass of successive approximation). @@ -538,9 +533,9 @@ private void DecodeBlockProgressiveAC(ref Block8x8 block, ref HuffmanTable acTab ref HuffmanScanBuffer buffer = ref this.scanBuffer; ref ZigZag zigzag = ref this.dctZigZag; - int start = this.spectralStart; - int end = this.spectralEnd; - int low = this.successiveLow; + int start = this.SpectralStart; + int end = this.SpectralEnd; + int low = this.SuccessiveLow; for (int i = start; i <= end; ++i) { @@ -584,11 +579,11 @@ private void DecodeBlockProgressiveACRefined(ref short blockDataRef, ref Huffman // Refinement scan for these AC coefficients ref HuffmanScanBuffer buffer = ref this.scanBuffer; ref ZigZag zigzag = ref this.dctZigZag; - int start = this.spectralStart; - int end = this.spectralEnd; + int start = this.SpectralStart; + int end = this.SpectralEnd; - int p1 = 1 << this.successiveLow; - int m1 = (-1) << this.successiveLow; + int p1 = 1 << this.SuccessiveLow; + int m1 = (-1) << this.SuccessiveLow; int k = start; diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs index 7ad0e87d21..a853d06fd2 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs @@ -2,9 +2,6 @@ // Licensed under the Apache License, Version 2.0. using System; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - using SixLabors.ImageSharp.Memory; namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder @@ -63,10 +60,7 @@ public JpegComponentPostProcessor(MemoryAllocator memoryAllocator, IRawJpegData public int BlockRowsPerStep { get; } /// - public void Dispose() - { - this.ColorBuffer.Dispose(); - } + public void Dispose() => this.ColorBuffer.Dispose(); /// /// Invoke for block rows, copy the result into . diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs index 18b2bc6e84..26a0635244 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs @@ -5,7 +5,6 @@ using System.Buffers; using System.Numerics; using System.Threading; -using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; using JpegColorConverter = SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder.ColorConverters.JpegColorConverter; @@ -19,7 +18,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder /// (3) Color conversion form one of the -s into a buffer of RGBA values
/// (4) Packing pixels from the buffer.
/// These operations are executed in steps. - /// image rows are converted in one step, + /// image rows are converted in one step, /// which means that size of the allocated memory is limited (does not depend on ). ///
internal class JpegImagePostProcessor : IDisposable @@ -29,12 +28,12 @@ internal class JpegImagePostProcessor : IDisposable /// /// The number of block rows to be processed in one Step. /// - public int BlockRowsPerStep; + private readonly int blockRowsPerStep; /// /// The number of image pixel rows to be processed in one step. /// - public int PixelRowsPerStep; + private readonly int pixelRowsPerStep; /// /// Temporal buffer to store a row of colors. @@ -57,12 +56,12 @@ public JpegImagePostProcessor(Configuration configuration, IRawJpegData rawJpeg) this.RawJpeg = rawJpeg; IJpegComponent c0 = rawJpeg.Components[0]; - this.BlockRowsPerStep = c0.SamplingFactors.Height; - this.PixelRowsPerStep = this.BlockRowsPerStep * 8; + this.blockRowsPerStep = c0.SamplingFactors.Height; + this.pixelRowsPerStep = this.blockRowsPerStep * 8; - this.NumberOfPostProcessorSteps = c0.SizeInBlocks.Height / this.BlockRowsPerStep; + this.NumberOfPostProcessorSteps = c0.SizeInBlocks.Height / this.blockRowsPerStep; - var postProcessorBufferSize = new Size(c0.SizeInBlocks.Width * 8, this.PixelRowsPerStep); + var postProcessorBufferSize = new Size(c0.SizeInBlocks.Width * 8, this.pixelRowsPerStep); MemoryAllocator memoryAllocator = configuration.MemoryAllocator; this.ComponentProcessors = new JpegComponentPostProcessor[rawJpeg.Components.Length]; for (int i = 0; i < rawJpeg.Components.Length; i++) @@ -85,12 +84,12 @@ public JpegImagePostProcessor(Configuration configuration, IRawJpegData rawJpeg) public IRawJpegData RawJpeg { get; } /// - /// Gets the total number of post processor steps deduced from the height of the image and . + /// Gets the total number of post processor steps deduced from the height of the image and . /// public int NumberOfPostProcessorSteps { get; } /// - /// Gets the value of the counter that grows by each step by . + /// Gets the value of the counter that grows by each step by . /// public int PixelRowCounter { get; private set; } @@ -129,15 +128,15 @@ public void PostProcess(ImageFrame destination, CancellationToke } /// - /// Execute one step processing pixel rows into 'destination'. - /// Convert and copy row of colors into 'destination' starting at row . + /// Execute one step processing pixel rows into 'destination'. + /// Convert and copy row of colors into 'destination' starting at row . /// /// The pixel type /// The destination image public void DoPostProcessorStep(ImageFrame destination) where TPixel : unmanaged, IPixel { - int maxY = Math.Min(destination.Height, this.PixelRowCounter + PixelRowsPerStep); + int maxY = Math.Min(destination.Height, this.PixelRowCounter + this.pixelRowsPerStep); var buffers = new Buffer2D[this.ComponentProcessors.Length]; for (int i = 0; i < this.ComponentProcessors.Length; i++) @@ -159,7 +158,7 @@ public void DoPostProcessorStep(ImageFrame destination) PixelOperations.Instance.FromVector4Destructive(this.configuration, this.rgbaBuffer.GetSpan().Slice(0, destRow.Length), destRow); } - this.PixelRowCounter += PixelRowsPerStep; + this.PixelRowCounter += this.pixelRowsPerStep; } } } diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index a25e756c73..1d1770aa77 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -3,9 +3,7 @@ using System; using System.Buffers; -using System.Collections.Generic; using System.Numerics; -using System.Text; using System.Threading; using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder.ColorConverters; using SixLabors.ImageSharp.Memory; @@ -16,11 +14,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder internal sealed class SpectralConverter : SpectralConverter where TPixel : unmanaged, IPixel { - private Configuration configuration; + private readonly Configuration configuration; private CancellationToken cancellationToken; - private JpegComponentPostProcessor[] componentProcessors; private JpegColorConverter colorConverter; @@ -29,11 +26,11 @@ internal sealed class SpectralConverter : SpectralConverter private Buffer2D pixelBuffer; - public int BlockRowsPerStep; + private int blockRowsPerStep; - private int PixelRowsPerStep; + private int pixelRowsPerStep; - private int PixelRowCounter; + private int pixelRowCounter; public SpectralConverter(Configuration configuration, CancellationToken ct) { @@ -41,7 +38,7 @@ public SpectralConverter(Configuration configuration, CancellationToken ct) this.cancellationToken = ct; } - private bool Converted => this.PixelRowCounter >= this.pixelBuffer.Height; + private bool Converted => this.pixelRowCounter >= this.pixelBuffer.Height; public Buffer2D PixelBuffer { @@ -49,7 +46,7 @@ public Buffer2D PixelBuffer { if (!this.Converted) { - int steps = (int)Math.Ceiling(this.pixelBuffer.Height / (float)this.PixelRowsPerStep); + int steps = (int)Math.Ceiling(this.pixelBuffer.Height / (float)this.pixelRowsPerStep); for (int step = 0; step < steps; step++) { @@ -70,14 +67,14 @@ public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) IJpegComponent c0 = frame.Components[0]; const int blockPixelHeight = 8; - this.BlockRowsPerStep = c0.SamplingFactors.Height; - this.PixelRowsPerStep = this.BlockRowsPerStep * blockPixelHeight; + this.blockRowsPerStep = c0.SamplingFactors.Height; + this.pixelRowsPerStep = this.blockRowsPerStep * blockPixelHeight; // pixel buffer for resulting image this.pixelBuffer = allocator.Allocate2D(frame.PixelWidth, frame.PixelHeight, AllocationOptions.Clean); // component processors from spectral to Rgba32 - var postProcessorBufferSize = new Size(c0.SizeInBlocks.Width * 8, this.PixelRowsPerStep); + var postProcessorBufferSize = new Size(c0.SizeInBlocks.Width * 8, this.pixelRowsPerStep); this.componentProcessors = new JpegComponentPostProcessor[frame.Components.Length]; for (int i = 0; i < this.componentProcessors.Length; i++) { @@ -118,7 +115,7 @@ public override void Dispose() private void ConvertNextStride(int spectralStep) { - int maxY = Math.Min(this.pixelBuffer.Height, this.PixelRowCounter + this.PixelRowsPerStep); + int maxY = Math.Min(this.pixelBuffer.Height, this.pixelRowCounter + this.pixelRowsPerStep); var buffers = new Buffer2D[this.componentProcessors.Length]; for (int i = 0; i < this.componentProcessors.Length; i++) @@ -127,9 +124,9 @@ private void ConvertNextStride(int spectralStep) buffers[i] = this.componentProcessors[i].ColorBuffer; } - for (int yy = this.PixelRowCounter; yy < maxY; yy++) + for (int yy = this.pixelRowCounter; yy < maxY; yy++) { - int y = yy - this.PixelRowCounter; + int y = yy - this.pixelRowCounter; var values = new JpegColorConverter.ComponentValues(buffers, y); this.colorConverter.ConvertToRgba(values, this.rgbaBuffer.GetSpan()); @@ -140,7 +137,7 @@ private void ConvertNextStride(int spectralStep) PixelOperations.Instance.FromVector4Destructive(this.configuration, this.rgbaBuffer.GetSpan().Slice(0, destRow.Length), destRow); } - this.PixelRowCounter += this.PixelRowsPerStep; + this.pixelRowCounter += this.pixelRowsPerStep; } } } diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 1289ff3bff..08e6ae021f 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -97,6 +97,11 @@ internal sealed class JpegDecoderCore : IRawJpegData, IImageDecoderInternals /// private AdobeMarker adobe; + /// + /// Scan decoder. + /// + private HuffmanScanDecoder scanDecoder; + /// /// Initializes a new instance of the class. /// @@ -172,8 +177,6 @@ public JpegDecoderCore(Configuration configuration, IJpegDecoderOptions options) /// public Block8x8F[] QuantizationTables { get; private set; } - private HuffmanScanDecoder scanDecoder; - /// /// Finds the next file marker within the byte stream. /// @@ -1064,20 +1067,20 @@ private void ProcessStartOfScanMarker(BufferedReadStream stream, CancellationTok // Main reason it's not fixed here is to make this commit less intrusive // Huffman tables can be calculated directly in the scan decoder class - this.scanDecoder.dcHuffmanTables = this.dcHuffmanTables; - this.scanDecoder.acHuffmanTables = this.acHuffmanTables; + this.scanDecoder.DcHuffmanTables = this.dcHuffmanTables; + this.scanDecoder.AcHuffmanTables = this.acHuffmanTables; // This can be injectd in DRI marker callback this.scanDecoder.ResetInterval = this.resetInterval; // This can be passed as ParseEntropyCodedData() parameter as it is used only there - this.scanDecoder.componentsLength = selectorsCount; + this.scanDecoder.ComponentsLength = selectorsCount; // This is okay to inject here, might be good to wrap it in a separate struct but not really necessary - this.scanDecoder.spectralStart = spectralStart; - this.scanDecoder.spectralEnd = spectralEnd; - this.scanDecoder.successiveHigh = successiveApproximation >> 4; - this.scanDecoder.successiveLow = successiveApproximation & 15; + this.scanDecoder.SpectralStart = spectralStart; + this.scanDecoder.SpectralEnd = spectralEnd; + this.scanDecoder.SuccessiveHigh = successiveApproximation >> 4; + this.scanDecoder.SuccessiveLow = successiveApproximation & 15; this.scanDecoder.ParseEntropyCodedData(); } From 3fb7105f86b00a6c7872b940537afa9505de8144 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 14:17:23 +0300 Subject: [PATCH 40/76] Fixed docs --- src/ImageSharp/Image{TPixel}.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Image{TPixel}.cs b/src/ImageSharp/Image{TPixel}.cs index fb1e6d92f3..2aa9c53945 100644 --- a/src/ImageSharp/Image{TPixel}.cs +++ b/src/ImageSharp/Image{TPixel}.cs @@ -89,7 +89,7 @@ internal Image(Configuration configuration, int width, int height, ImageMetadata /// /// Initializes a new instance of the class - /// wrapping an external pixel bufferx. /// /// The configuration providing initialization code which allows extending the library. /// Pixel buffer. From 73d35b779c9e113c66c17e359c9e020e4b3f4141 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 14:35:14 +0300 Subject: [PATCH 41/76] Fixed no color deduction for metadata only pass --- src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 08e6ae021f..a45f51db05 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -875,6 +875,9 @@ private void ProcessStartOfFrameMarker(BufferedReadStream stream, int remaining, this.ImageSizeInPixels = new Size(this.Frame.PixelWidth, this.Frame.PixelHeight); this.ComponentCount = this.Frame.ComponentCount; + this.ColorSpace = this.DeduceJpegColorSpace(); + this.Metadata.GetJpegMetadata().ColorType = this.ColorSpace == JpegColorSpace.Grayscale ? JpegColorType.Luminance : JpegColorType.YCbCr; + if (!metadataOnly) { remaining -= length; @@ -891,7 +894,6 @@ private void ProcessStartOfFrameMarker(BufferedReadStream stream, int remaining, this.Frame.ComponentIds = new byte[this.ComponentCount]; this.Frame.ComponentOrder = new byte[this.ComponentCount]; this.Frame.Components = new JpegComponent[this.ComponentCount]; - this.ColorSpace = this.DeduceJpegColorSpace(); int maxH = 0; int maxV = 0; @@ -922,8 +924,6 @@ private void ProcessStartOfFrameMarker(BufferedReadStream stream, int remaining, this.Frame.MaxHorizontalFactor = maxH; this.Frame.MaxVerticalFactor = maxV; - this.ColorSpace = this.DeduceJpegColorSpace(); - this.Metadata.GetJpegMetadata().ColorType = this.ColorSpace == JpegColorSpace.Grayscale ? JpegColorType.Luminance : JpegColorType.YCbCr; this.ImageSizeInMCU = new Size(this.Frame.McusPerLine, this.Frame.McusPerColumn); this.Frame.InitComponents(); From ccd660115828b0d874915d1c5dac4447c7a403e3 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 15:51:35 +0300 Subject: [PATCH 42/76] Marked ParseStream private as it now can't be called outside of Decode/Identify methods --- src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index a45f51db05..6dd88a00cd 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -249,7 +249,7 @@ public IImageInfo Identify(BufferedReadStream stream, CancellationToken cancella /// The input stream /// Whether to decode metadata only. /// The token to monitor cancellation. - public void ParseStream(BufferedReadStream stream, bool metadataOnly = false, CancellationToken cancellationToken = default) + private void ParseStream(BufferedReadStream stream, bool metadataOnly = false, CancellationToken cancellationToken = default) { this.Metadata = new ImageMetadata(); From b9f12a6a127a5eb92ab382f9d38e73f46c854b3d Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 15:55:51 +0300 Subject: [PATCH 43/76] Removed unsupported benchmark --- .../Codecs/Jpeg/DecodeJpegParseStreamOnly.cs | 118 +++++++++--------- 1 file changed, 59 insertions(+), 59 deletions(-) diff --git a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs index 68a102e3ce..6796faa6d0 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs @@ -1,59 +1,59 @@ -// Copyright (c) Six Labors. -// Licensed under the Apache License, Version 2.0. - -using System.IO; -using BenchmarkDotNet.Attributes; -using SixLabors.ImageSharp.Formats.Jpeg; -using SixLabors.ImageSharp.IO; -using SixLabors.ImageSharp.Tests; -using SDSize = System.Drawing.Size; - -namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jpeg -{ - [Config(typeof(Config.ShortMultiFramework))] - public class DecodeJpegParseStreamOnly - { - [Params(TestImages.Jpeg.BenchmarkSuite.Lake_Small444YCbCr)] - public string TestImage { get; set; } - - private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage); - - private byte[] jpegBytes; - - [GlobalSetup] - public void Setup() - => this.jpegBytes = File.ReadAllBytes(this.TestImageFullPath); - - [Benchmark(Baseline = true, Description = "System.Drawing FULL")] - public SDSize JpegSystemDrawing() - { - using var memoryStream = new MemoryStream(this.jpegBytes); - using var image = System.Drawing.Image.FromStream(memoryStream); - return image.Size; - } - - [Benchmark(Description = "JpegDecoderCore.ParseStream")] - public void ParseStream() - { - using var memoryStream = new MemoryStream(this.jpegBytes); - using var bufferedStream = new BufferedReadStream(Configuration.Default, memoryStream); - - var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder { IgnoreMetadata = true }); - decoder.ParseStream(bufferedStream); - decoder.Dispose(); - } - } - - /* - | Method | Job | Runtime | TestImage | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated | - |---------------------------- |----------- |-------------- |--------------------- |---------:|----------:|----------:|------:|--------:|------:|------:|----------:| - | 'System.Drawing FULL' | Job-HITJFX | .NET 4.7.2 | Jpg/b(...)e.jpg [21] | 5.828 ms | 0.9885 ms | 0.0542 ms | 1.00 | 46.8750 | - | - | 211566 B | - | JpegDecoderCore.ParseStream | Job-HITJFX | .NET 4.7.2 | Jpg/b(...)e.jpg [21] | 5.833 ms | 0.2923 ms | 0.0160 ms | 1.00 | - | - | - | 12416 B | - | | | | | | | | | | | | | - | 'System.Drawing FULL' | Job-WPSKZD | .NET Core 2.1 | Jpg/b(...)e.jpg [21] | 6.018 ms | 2.1374 ms | 0.1172 ms | 1.00 | 46.8750 | - | - | 210768 B | - | JpegDecoderCore.ParseStream | Job-WPSKZD | .NET Core 2.1 | Jpg/b(...)e.jpg [21] | 4.382 ms | 0.9009 ms | 0.0494 ms | 0.73 | - | - | - | 12360 B | - | | | | | | | | | | | | | - | 'System.Drawing FULL' | Job-ZLSNRP | .NET Core 3.1 | Jpg/b(...)e.jpg [21] | 5.714 ms | 0.4078 ms | 0.0224 ms | 1.00 | - | - | - | 176 B | - | JpegDecoderCore.ParseStream | Job-ZLSNRP | .NET Core 3.1 | Jpg/b(...)e.jpg [21] | 4.239 ms | 1.0943 ms | 0.0600 ms | 0.74 | - | - | - | 12406 B | - */ -} +//// Copyright (c) Six Labors. +//// Licensed under the Apache License, Version 2.0. + +//using System.IO; +//using BenchmarkDotNet.Attributes; +//using SixLabors.ImageSharp.Formats.Jpeg; +//using SixLabors.ImageSharp.IO; +//using SixLabors.ImageSharp.Tests; +//using SDSize = System.Drawing.Size; + +//namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jpeg +//{ +// [Config(typeof(Config.ShortMultiFramework))] +// public class DecodeJpegParseStreamOnly +// { +// [Params(TestImages.Jpeg.BenchmarkSuite.Lake_Small444YCbCr)] +// public string TestImage { get; set; } + +// private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage); + +// private byte[] jpegBytes; + +// [GlobalSetup] +// public void Setup() +// => this.jpegBytes = File.ReadAllBytes(this.TestImageFullPath); + +// [Benchmark(Baseline = true, Description = "System.Drawing FULL")] +// public SDSize JpegSystemDrawing() +// { +// using var memoryStream = new MemoryStream(this.jpegBytes); +// using var image = System.Drawing.Image.FromStream(memoryStream); +// return image.Size; +// } + +// [Benchmark(Description = "JpegDecoderCore.ParseStream")] +// public void ParseStream() +// { +// using var memoryStream = new MemoryStream(this.jpegBytes); +// using var bufferedStream = new BufferedReadStream(Configuration.Default, memoryStream); + +// var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder { IgnoreMetadata = true }); +// decoder.ParseStream(bufferedStream); +// decoder.Dispose(); +// } +// } + +// /* +// | Method | Job | Runtime | TestImage | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated | +// |---------------------------- |----------- |-------------- |--------------------- |---------:|----------:|----------:|------:|--------:|------:|------:|----------:| +// | 'System.Drawing FULL' | Job-HITJFX | .NET 4.7.2 | Jpg/b(...)e.jpg [21] | 5.828 ms | 0.9885 ms | 0.0542 ms | 1.00 | 46.8750 | - | - | 211566 B | +// | JpegDecoderCore.ParseStream | Job-HITJFX | .NET 4.7.2 | Jpg/b(...)e.jpg [21] | 5.833 ms | 0.2923 ms | 0.0160 ms | 1.00 | - | - | - | 12416 B | +// | | | | | | | | | | | | | +// | 'System.Drawing FULL' | Job-WPSKZD | .NET Core 2.1 | Jpg/b(...)e.jpg [21] | 6.018 ms | 2.1374 ms | 0.1172 ms | 1.00 | 46.8750 | - | - | 210768 B | +// | JpegDecoderCore.ParseStream | Job-WPSKZD | .NET Core 2.1 | Jpg/b(...)e.jpg [21] | 4.382 ms | 0.9009 ms | 0.0494 ms | 0.73 | - | - | - | 12360 B | +// | | | | | | | | | | | | | +// | 'System.Drawing FULL' | Job-ZLSNRP | .NET Core 3.1 | Jpg/b(...)e.jpg [21] | 5.714 ms | 0.4078 ms | 0.0224 ms | 1.00 | - | - | - | 176 B | +// | JpegDecoderCore.ParseStream | Job-ZLSNRP | .NET Core 3.1 | Jpg/b(...)e.jpg [21] | 4.239 ms | 1.0943 ms | 0.0600 ms | 0.74 | - | - | - | 12406 B | +// */ +//} From ef80d98ee29a3000d501b0eec0e3bfe6402ce7d7 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 16:08:35 +0300 Subject: [PATCH 44/76] Tests no longer use ParseStream method --- tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs | 3 +-- .../ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs | 4 ++-- .../ImageSharp.Tests/Formats/Jpg/Utils/JpegFixture.cs | 10 +++++++++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs index d13a9696c3..a2f7583a16 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs @@ -6,7 +6,6 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; - using SixLabors.ImageSharp.Formats.Jpeg; using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.Memory; @@ -79,7 +78,7 @@ public void ParseStream_BasicPropertiesAreCorrect() using var ms = new MemoryStream(bytes); using var bufferedStream = new BufferedReadStream(Configuration.Default, ms); var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder()); - decoder.ParseStream(bufferedStream); + using Image image = decoder.Decode(bufferedStream, cancellationToken: default); // I don't know why these numbers are different. All I know is that the decoder works // and spectral data is exactly correct also. diff --git a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs index 91b1b9cd78..fe31c5118c 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs @@ -54,7 +54,7 @@ public void Decoder_ParseStream_SaveSpectralResult(TestImageProvider image = decoder.Decode(bufferedStream, cancellationToken: default); var data = LibJpegTools.SpectralData.LoadFromImageSharpDecoder(decoder); VerifyJpeg.SaveSpectralImage(provider, data); @@ -76,7 +76,7 @@ public void VerifySpectralCorrectness(TestImageProvider provider using var ms = new MemoryStream(sourceBytes); using var bufferedStream = new BufferedReadStream(Configuration.Default, ms); - decoder.ParseStream(bufferedStream); + using Image image = decoder.Decode(bufferedStream, cancellationToken: default); var imageSharpData = LibJpegTools.SpectralData.LoadFromImageSharpDecoder(decoder); this.VerifySpectralCorrectnessImpl(provider, imageSharpData); diff --git a/tests/ImageSharp.Tests/Formats/Jpg/Utils/JpegFixture.cs b/tests/ImageSharp.Tests/Formats/Jpg/Utils/JpegFixture.cs index c6f4704f05..ccb7f6f1eb 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/Utils/JpegFixture.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/Utils/JpegFixture.cs @@ -9,6 +9,7 @@ using SixLabors.ImageSharp.Formats.Jpeg; using SixLabors.ImageSharp.Formats.Jpeg.Components; using SixLabors.ImageSharp.IO; +using SixLabors.ImageSharp.PixelFormats; using Xunit; using Xunit.Abstractions; @@ -196,7 +197,14 @@ internal static JpegDecoderCore ParseJpegStream(string testFileName, bool metaDa using var bufferedStream = new BufferedReadStream(Configuration.Default, ms); var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder()); - decoder.ParseStream(bufferedStream, metaDataOnly); + if (metaDataOnly) + { + decoder.Identify(bufferedStream, cancellationToken: default); + } + else + { + using Image image = decoder.Decode(bufferedStream, cancellationToken: default); + } return decoder; } From 8078688d6eba8b5ff80f98443943dface307011d Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 16:08:45 +0300 Subject: [PATCH 45/76] Fixed null reference in spectral converter --- .../Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index 1d1770aa77..6d38bde06e 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -105,12 +105,15 @@ public override void ConvertStrideBaseline() public override void Dispose() { - foreach (JpegComponentPostProcessor cpp in this.componentProcessors) + if (this.componentProcessors != null) { - cpp.Dispose(); + foreach (JpegComponentPostProcessor cpp in this.componentProcessors) + { + cpp.Dispose(); + } } - this.rgbaBuffer.Dispose(); + this.rgbaBuffer?.Dispose(); } private void ConvertNextStride(int spectralStep) From 7c63fb4a1cb5c5e2e463497e06c6d9a9ab7f3198 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 16:48:53 +0300 Subject: [PATCH 46/76] Fixed out of range exception at component postprocessor --- .../Formats/Jpeg/Components/Decoder/JpegComponent.cs | 10 +++------- .../Components/Decoder/JpegComponentPostProcessor.cs | 8 +++++--- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs index 05f46aabaa..614e96e54a 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs @@ -135,14 +135,10 @@ public void AllocateSpectral(bool fullScan) return; } - int blocksPerLineForMcu = this.Frame.McusPerLine * this.HorizontalSamplingFactor; - int blocksPerColumnForMcu = fullScan ? this.Frame.McusPerColumn * this.VerticalSamplingFactor : this.VerticalSamplingFactor; - - int totalNumberOfBlocks = blocksPerColumnForMcu * (blocksPerLineForMcu + 1); - int width = this.WidthInBlocks + 1; - int height = totalNumberOfBlocks / width; + int spectralAllocWidth = this.SizeInBlocks.Width; + int spectralAllocHeight = fullScan ? this.SizeInBlocks.Height : this.VerticalSamplingFactor; - this.SpectralBlocks = this.memoryAllocator.Allocate2D(width, height, AllocationOptions.Clean); + this.SpectralBlocks = this.memoryAllocator.Allocate2D(spectralAllocWidth, spectralAllocHeight, AllocationOptions.Clean); } } } diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs index a853d06fd2..2d38b417cd 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs @@ -67,6 +67,8 @@ public JpegComponentPostProcessor(MemoryAllocator memoryAllocator, IRawJpegData ///
public void CopyBlocksToColorBuffer(int step) { + Buffer2D spectralBuffer = this.Component.SpectralBlocks; + var blockPp = new JpegBlockPostProcessor(this.RawJpeg, this.Component); float maximumValue = MathF.Pow(2, this.RawJpeg.Precision) - 1; @@ -78,7 +80,7 @@ public void CopyBlocksToColorBuffer(int step) { int yBlock = yBlockStart + y; - if (yBlock >= this.SizeInBlocks.Height) + if (yBlock >= spectralBuffer.Height) { break; } @@ -86,10 +88,10 @@ public void CopyBlocksToColorBuffer(int step) int yBuffer = y * this.blockAreaSize.Height; Span colorBufferRow = this.ColorBuffer.GetRowSpan(yBuffer); - Span blockRow = this.Component.SpectralBlocks.GetRowSpan(yBlock); + Span blockRow = spectralBuffer.GetRowSpan(yBlock); // see: https://github.com/SixLabors/ImageSharp/issues/824 - int widthInBlocks = Math.Min(this.Component.SpectralBlocks.Width, this.SizeInBlocks.Width); + int widthInBlocks = Math.Min(spectralBuffer.Width, this.SizeInBlocks.Width); for (int xBlock = 0; xBlock < widthInBlocks; xBlock++) { From ae1b40dee887718ee5b054937bb8b4335bc6f40b Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 17:04:50 +0300 Subject: [PATCH 47/76] Skipped old post processing pipeline tests --- .../Formats/Jpg/JpegImagePostProcessorTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegImagePostProcessorTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegImagePostProcessorTests.cs index 93d9aee923..1a969060cd 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegImagePostProcessorTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegImagePostProcessorTests.cs @@ -41,7 +41,7 @@ private static void SaveBuffer(JpegComponentPostProcessor cp, TestImageP } } - [Theory] + [Theory(Skip = "Decoding core had breaking internal change and no longer supports post processing after stream parsing.")] [WithFile(TestImages.Jpeg.Baseline.Calliphora, PixelTypes.Rgba32)] [WithFile(TestImages.Jpeg.Baseline.Testorig420, PixelTypes.Rgba32)] public void DoProcessorStep(TestImageProvider provider) @@ -62,7 +62,7 @@ public void DoProcessorStep(TestImageProvider provider) } } - [Theory] + [Theory(Skip = "Decoding core had breaking internal change and no longer supports post processing after stream parsing.")] [WithFileCollection(nameof(BaselineTestJpegs), PixelTypes.Rgba32)] public void PostProcess(TestImageProvider provider) where TPixel : unmanaged, IPixel From 3c4d0fefd3cd1a41ec5a725e93177ccb42c908a4 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 17:05:04 +0300 Subject: [PATCH 48/76] Fixed invalid frame mcu size calculation --- src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 6dd88a00cd..37628e88d7 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -924,10 +924,10 @@ private void ProcessStartOfFrameMarker(BufferedReadStream stream, int remaining, this.Frame.MaxHorizontalFactor = maxH; this.Frame.MaxVerticalFactor = maxV; - this.ImageSizeInMCU = new Size(this.Frame.McusPerLine, this.Frame.McusPerColumn); - this.Frame.InitComponents(); + this.ImageSizeInMCU = new Size(this.Frame.McusPerLine, this.Frame.McusPerColumn); + // This can be injected in SOF marker callback this.scanDecoder.InjectFrameData(this.Frame, this); } From 7fbc33c1d18bf9e750b315a46288669cdb386b11 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 23:54:17 +0300 Subject: [PATCH 49/76] Fixed invalid internal deconding mode selection for grayscale jpegs --- .../Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs index 7a63ef7c66..29de8059c5 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs @@ -121,7 +121,7 @@ public void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) private void ParseBaselineData() { - if (this.ComponentsLength == 1) + if (this.ComponentsLength != this.frame.ComponentCount) { this.frame.AllocateComponents(fullScan: true); this.ParseBaselineDataNonInterleaved(); From e4787956448cb54f4ace99f212f2c7f45794fa77 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sun, 11 Jul 2021 23:59:30 +0300 Subject: [PATCH 50/76] Cosmetic fixes --- .../Jpeg/Components/Decoder/HuffmanScanDecoder.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs index 29de8059c5..89120813b0 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs @@ -121,16 +121,17 @@ public void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) private void ParseBaselineData() { - if (this.ComponentsLength != this.frame.ComponentCount) + if (this.ComponentsLength == this.frame.ComponentCount) { - this.frame.AllocateComponents(fullScan: true); - this.ParseBaselineDataNonInterleaved(); + // interleaved - we can convert spectral data stride by stride + this.frame.AllocateComponents(fullScan: false); + this.ParseBaselineDataInterleaved(); } else { - // interleaved baseline is the only place where we can optimize memory footprint via reusing single spectral stride - this.frame.AllocateComponents(fullScan: false); - this.ParseBaselineDataInterleaved(); + // non-interleaved - each scan contains + this.frame.AllocateComponents(fullScan: true); + this.ParseBaselineDataNonInterleaved(); } } @@ -179,7 +180,6 @@ private void ParseBaselineDataInterleaved() // by the basic H and V specified for the component for (int y = 0; y < v; y++) { - int blockRow = (mcuRow * v) + y; Span blockSpan = component.SpectralBlocks.GetRowSpan(y); ref Block8x8 blockRef = ref MemoryMarshal.GetReference(blockSpan); From b8e13e7eb52df0603cc25e1256b3c35d8e100405 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Mon, 12 Jul 2021 02:42:49 +0300 Subject: [PATCH 51/76] Disabled spectral tests due to new architecture incompatibility --- tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs index fe31c5118c..8e787e7255 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs @@ -60,7 +60,7 @@ public void Decoder_ParseStream_SaveSpectralResult(TestImageProvider(TestImageProvider provider) where TPixel : unmanaged, IPixel From 519c6b227a6f148d79e4d7b57458b2331630c3fd Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Mon, 12 Jul 2021 18:24:07 +0300 Subject: [PATCH 52/76] 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 @@ public void ParseEntropyCodedData() 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 @@ private void ParseBaselineData() { 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 @@ private void ParseProgressiveData() { 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 @@ public void AllocateSpectral(bool fullScan) 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 @@ internal sealed class JpegFrame : IDisposable ///
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. /// From daccbfbccce21314d6d5d54e82888afcea9e59e7 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Mon, 12 Jul 2021 18:26:21 +0300 Subject: [PATCH 53/76] Basement for spectral tests --- .../Decoder/SpectralConverter{TPixel}.cs | 12 +++---- .../Formats/Jpeg/JpegDecoderCore.cs | 25 +++++++++------ .../Formats/Jpg/SpectralJpegTests.cs | 31 +++++++++++++++++++ 3 files changed, 52 insertions(+), 16 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index 6d38bde06e..6ad2bf00c1 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -32,10 +32,10 @@ internal sealed class SpectralConverter : SpectralConverter private int pixelRowCounter; - public SpectralConverter(Configuration configuration, CancellationToken ct) + public SpectralConverter(Configuration configuration, CancellationToken cancellationToken) { this.configuration = configuration; - this.cancellationToken = ct; + this.cancellationToken = cancellationToken; } private bool Converted => this.pixelRowCounter >= this.pixelBuffer.Height; @@ -90,10 +90,6 @@ public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) public override void ConvertStrideBaseline() { - // Convert next pixel stride using single spectral `stride' - // Note that zero passing eliminates the need of virtual call from JpegComponentPostProcessor - this.ConvertNextStride(spectralStep: 0); - // Clear spectral stride - this is VERY important as jpeg possibly won't fill entire buffer each stride // Which leads to decoding artifacts // Note that this code clears all buffers of the post processors, it's their responsibility to allocate only single stride @@ -101,6 +97,10 @@ public override void ConvertStrideBaseline() { cpp.ClearSpectralBuffers(); } + + // Convert next pixel stride using single spectral `stride' + // Note that zero passing eliminates the need of virtual call from JpegComponentPostProcessor + this.ConvertNextStride(spectralStep: 0); } public override void Dispose() diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 37628e88d7..c7a5bc42c7 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -220,9 +220,9 @@ public Image Decode(BufferedReadStream stream, CancellationToken { using var spectralConverter = new SpectralConverter(this.Configuration, cancellationToken); - this.scanDecoder = new HuffmanScanDecoder(stream, spectralConverter, cancellationToken); + var scanDecoder = new HuffmanScanDecoder(stream, spectralConverter, cancellationToken); - this.ParseStream(stream, cancellationToken: cancellationToken); + this.ParseStream(stream, scanDecoder, cancellationToken); this.InitExifProfile(); this.InitIccProfile(); this.InitIptcProfile(); @@ -234,7 +234,7 @@ public Image Decode(BufferedReadStream stream, CancellationToken /// public IImageInfo Identify(BufferedReadStream stream, CancellationToken cancellationToken) { - this.ParseStream(stream, true, cancellationToken); + this.ParseStream(stream, scanDecoder: null, cancellationToken); this.InitExifProfile(); this.InitIccProfile(); this.InitIptcProfile(); @@ -244,13 +244,17 @@ public IImageInfo Identify(BufferedReadStream stream, CancellationToken cancella } /// - /// Parses the input stream for file markers + /// Parses the input stream for file markers. /// - /// The input stream - /// Whether to decode metadata only. - /// The token to monitor cancellation. - private void ParseStream(BufferedReadStream stream, bool metadataOnly = false, CancellationToken cancellationToken = default) + /// The input stream. + /// Scan decoder used exclusively to decode SOS marker. + /// The token to monitor cancellation. + internal void ParseStream(BufferedReadStream stream, HuffmanScanDecoder scanDecoder, CancellationToken ct) { + bool metadataOnly = scanDecoder == null; + + this.scanDecoder = scanDecoder; + this.Metadata = new ImageMetadata(); // Check for the Start Of Image marker. @@ -279,7 +283,7 @@ private void ParseStream(BufferedReadStream stream, bool metadataOnly = false, C while (fileMarker.Marker != JpegConstants.Markers.EOI || (fileMarker.Marker == JpegConstants.Markers.EOI && fileMarker.Invalid)) { - cancellationToken.ThrowIfCancellationRequested(); + ct.ThrowIfCancellationRequested(); if (!fileMarker.Invalid) { @@ -297,7 +301,7 @@ private void ParseStream(BufferedReadStream stream, bool metadataOnly = false, C case JpegConstants.Markers.SOS: if (!metadataOnly) { - this.ProcessStartOfScanMarker(stream, cancellationToken); + this.ProcessStartOfScanMarker(stream, ct); break; } else @@ -1030,6 +1034,7 @@ private void ProcessStartOfScanMarker(BufferedReadStream stream, CancellationTok } int selectorsCount = stream.ReadByte(); + this.Frame.MultiScan = this.Frame.ComponentCount != selectorsCount; for (int i = 0; i < selectorsCount; i++) { int componentIndex = -1; diff --git a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs index 8e787e7255..3c7855367f 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs @@ -6,6 +6,7 @@ using System.Linq; using SixLabors.ImageSharp.Formats.Jpeg; +using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder; using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Tests.Formats.Jpg.Utils; @@ -76,6 +77,10 @@ public void VerifySpectralCorrectness(TestImageProvider provider using var ms = new MemoryStream(sourceBytes); using var bufferedStream = new BufferedReadStream(Configuration.Default, ms); + using var spectralConverter = new SpectralConverter(Configuration.Default, cancellationToken: default); + + var scanDecoder = new HuffmanScanDecoder(bufferedStream, spectralConverter, cancellationToken: default); + using Image image = decoder.Decode(bufferedStream, cancellationToken: default); var imageSharpData = LibJpegTools.SpectralData.LoadFromImageSharpDecoder(decoder); @@ -126,5 +131,31 @@ private void VerifySpectralCorrectnessImpl( Assert.True(totalDifference < tolerance); } + + private class DebugSpectralConverter : SpectralConverter + where TPixel : unmanaged, IPixel + { + private readonly SpectralConverter converter; + + public DebugSpectralConverter(SpectralConverter converter) + { + this.converter = converter; + } + + public override void ConvertStrideBaseline() + { + this.converter.ConvertStrideBaseline(); + } + + public override void Dispose() + { + this.converter?.Dispose(); + } + + public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) + { + this.converter.InjectFrameData(frame, jpegData); + } + } } } From bbbfb50f509fb016a9e951f9ebc54ae2d9bb94b3 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Mon, 12 Jul 2021 18:44:10 +0300 Subject: [PATCH 54/76] Additional fixes for spectral tests --- .../Formats/Jpg/SpectralJpegTests.cs | 44 +++++++++++-------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs index 3c7855367f..0a457985f1 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs @@ -4,7 +4,7 @@ using System; using System.IO; using System.Linq; - +using System.Threading; using SixLabors.ImageSharp.Formats.Jpeg; using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder; using SixLabors.ImageSharp.IO; @@ -71,29 +71,33 @@ public void VerifySpectralCorrectness(TestImageProvider provider return; } - var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder()); + // Expected data from libjpeg + LibJpegTools.SpectralData libJpegData = LibJpegTools.ExtractSpectralData(provider.SourceFileOrDescription); + // Calculating data from ImageSharp byte[] sourceBytes = TestFile.Create(provider.SourceFileOrDescription).Bytes; + var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder()); using var ms = new MemoryStream(sourceBytes); using var bufferedStream = new BufferedReadStream(Configuration.Default, ms); - using var spectralConverter = new SpectralConverter(Configuration.Default, cancellationToken: default); - var scanDecoder = new HuffmanScanDecoder(bufferedStream, spectralConverter, cancellationToken: default); + // internal scan decoder which we substitute to assert spectral correctness + using var debugConverter = new DebugSpectralConverter(Configuration.Default, cancellationToken: default); + var scanDecoder = new HuffmanScanDecoder(bufferedStream, debugConverter, cancellationToken: default); - using Image image = decoder.Decode(bufferedStream, cancellationToken: default); + // This would parse entire image + // Due to underlying architecture, baseline interleaved jpegs would be tested inside the parsing loop + // Everything else must be checked manually after this method + decoder.ParseStream(bufferedStream, scanDecoder, ct: default); var imageSharpData = LibJpegTools.SpectralData.LoadFromImageSharpDecoder(decoder); - this.VerifySpectralCorrectnessImpl(provider, imageSharpData); + this.VerifySpectralCorrectnessImpl(libJpegData, imageSharpData); } - private void VerifySpectralCorrectnessImpl( - TestImageProvider provider, + private void VerifySpectralCorrectnessImpl( + LibJpegTools.SpectralData libJpegData, LibJpegTools.SpectralData imageSharpData) - where TPixel : unmanaged, IPixel { - LibJpegTools.SpectralData libJpegData = LibJpegTools.ExtractSpectralData(provider.SourceFileOrDescription); - bool equality = libJpegData.Equals(imageSharpData); this.Output.WriteLine("Spectral data equality: " + equality); @@ -137,25 +141,27 @@ private class DebugSpectralConverter : SpectralConverter { private readonly SpectralConverter converter; - public DebugSpectralConverter(SpectralConverter converter) - { - this.converter = converter; - } + public DebugSpectralConverter(Configuration configuration, CancellationToken cancellationToken) + => this.converter = new SpectralConverter(configuration, cancellationToken); public override void ConvertStrideBaseline() { this.converter.ConvertStrideBaseline(); + + // This would be called only for baseline non-interleaved images + // We must test spectral strides here } public override void Dispose() { this.converter?.Dispose(); - } - public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) - { - this.converter.InjectFrameData(frame, jpegData); + // As we are only testing spectral data we don't care about pixels + // But we need to dispose allocated pixel buffer + this.converter.PixelBuffer.Dispose(); } + + public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) => this.converter.InjectFrameData(frame, jpegData); } } } From 19e2e3d40df77f77a46f4468deb59515ffc45572 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Mon, 12 Jul 2021 20:01:09 +0300 Subject: [PATCH 55/76] Fixed new spectral tests for progressive and multi-scan images --- .../Formats/Jpg/SpectralJpegTests.cs | 62 ++++++++++++++++--- .../Jpg/Utils/LibJpegTools.ComponentData.cs | 31 ++++++++++ 2 files changed, 85 insertions(+), 8 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs index 0a457985f1..0235ebb382 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs @@ -61,7 +61,8 @@ public void Decoder_ParseStream_SaveSpectralResult(TestImageProvider(TestImageProvider provider) where TPixel : unmanaged, IPixel @@ -86,12 +87,10 @@ public void VerifySpectralCorrectness(TestImageProvider provider var scanDecoder = new HuffmanScanDecoder(bufferedStream, debugConverter, cancellationToken: default); // This would parse entire image - // Due to underlying architecture, baseline interleaved jpegs would be tested inside the parsing loop - // Everything else must be checked manually after this method decoder.ParseStream(bufferedStream, scanDecoder, ct: default); - var imageSharpData = LibJpegTools.SpectralData.LoadFromImageSharpDecoder(decoder); - this.VerifySpectralCorrectnessImpl(libJpegData, imageSharpData); + // Actual verification + this.VerifySpectralCorrectnessImpl(libJpegData, debugConverter.SpectralData); } private void VerifySpectralCorrectnessImpl( @@ -141,27 +140,74 @@ private class DebugSpectralConverter : SpectralConverter { private readonly SpectralConverter converter; + private JpegFrame frame; + + private LibJpegTools.SpectralData spectralData; + + private int baselineScanRowCounter; + public DebugSpectralConverter(Configuration configuration, CancellationToken cancellationToken) => this.converter = new SpectralConverter(configuration, cancellationToken); + public LibJpegTools.SpectralData SpectralData + { + get + { + // Due to underlying architecture, baseline interleaved jpegs would inject spectral data during parsing + // Progressive and multi-scan images must be loaded manually + if (this.frame.Progressive || this.frame.MultiScan) + { + LibJpegTools.ComponentData[] components = this.spectralData.Components; + for (int i = 0; i < components.Length; i++) + { + components[i].LoadSpectral(this.frame.Components[i]); + } + } + + return this.spectralData; + } + } + public override void ConvertStrideBaseline() { this.converter.ConvertStrideBaseline(); // This would be called only for baseline non-interleaved images // We must test spectral strides here + LibJpegTools.ComponentData[] components = this.spectralData.Components; + for (int i = 0; i < components.Length; i++) + { + components[i].LoadSpectralStride(this.frame.Components[i].SpectralBlocks, this.baselineScanRowCounter); + } + + this.baselineScanRowCounter++; } public override void Dispose() { - this.converter?.Dispose(); - // As we are only testing spectral data we don't care about pixels // But we need to dispose allocated pixel buffer this.converter.PixelBuffer.Dispose(); + + // Converter Dispose must be called after pixel buffer disposal because pixel buffer getter can do a full scan conversion + this.converter?.Dispose(); } - public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) => this.converter.InjectFrameData(frame, jpegData); + public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) + { + this.converter.InjectFrameData(frame, jpegData); + + this.frame = frame; + + var spectralComponents = new LibJpegTools.ComponentData[frame.ComponentCount]; + for (int i = 0; i < spectralComponents.Length; i++) + { + JpegComponent component = frame.Components[i]; + spectralComponents[i] = new LibJpegTools.ComponentData(component.WidthInBlocks, component.HeightInBlocks, component.Index); + } + + this.spectralData = new LibJpegTools.SpectralData(spectralComponents); + } } } } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs b/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs index 6f6032ee2e..4ec9b8d69c 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs @@ -56,6 +56,37 @@ internal void MakeBlock(short[] data, int y, int x) this.SpectralBlocks[x, y] = new Block8x8(data); } + public void LoadSpectralStride(Buffer2D data, int strideIndex) + { + for (int y = 0; y < data.Height; y++) + { + Span blockRow = data.GetRowSpan(y); + for (int x = 0; x < data.Width; x++) + { + short[] block = blockRow[x].ToArray(); + + // x coordinate stays the same - we load entire stride + // y coordinate is tricky as we load single stride to full buffer - offset is needed + int yOffset = strideIndex * data.Height; + this.MakeBlock(block, y + yOffset, x); + } + } + } + + public void LoadSpectral(JpegComponent c) + { + Buffer2D data = c.SpectralBlocks; + for (int y = 0; y < c.HeightInBlocks; y++) + { + Span blockRow = data.GetRowSpan(y); + for (int x = 0; x < c.WidthInBlocks; x++) + { + short[] block = blockRow[x].ToArray(); + this.MakeBlock(block, y, x); + } + } + } + public static ComponentData Load(JpegComponent c, int index) { var result = new ComponentData( From 39dd5bc5364805899078cbcc71cc0c7cb2cc24c3 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Mon, 12 Jul 2021 21:00:06 +0300 Subject: [PATCH 56/76] Fixed out of range exception for baseline tests --- .../Jpg/Utils/LibJpegTools.ComponentData.cs | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs b/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs index 4ec9b8d69c..edb8d457b7 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs @@ -58,17 +58,20 @@ internal void MakeBlock(short[] data, int y, int x) public void LoadSpectralStride(Buffer2D data, int strideIndex) { - for (int y = 0; y < data.Height; y++) + int startIndex = strideIndex * data.Height; + + int endIndex = Math.Min(this.HeightInBlocks, startIndex + data.Height); + + for (int y = startIndex; y < endIndex; y++) { - Span blockRow = data.GetRowSpan(y); - for (int x = 0; x < data.Width; x++) + Span blockRow = data.GetRowSpan(y - startIndex); + for (int x = 0; x < this.WidthInBlocks; x++) { short[] block = blockRow[x].ToArray(); // x coordinate stays the same - we load entire stride // y coordinate is tricky as we load single stride to full buffer - offset is needed - int yOffset = strideIndex * data.Height; - this.MakeBlock(block, y + yOffset, x); + this.MakeBlock(block, y, x); } } } @@ -76,10 +79,10 @@ public void LoadSpectralStride(Buffer2D data, int strideIndex) public void LoadSpectral(JpegComponent c) { Buffer2D data = c.SpectralBlocks; - for (int y = 0; y < c.HeightInBlocks; y++) + for (int y = 0; y < this.HeightInBlocks; y++) { Span blockRow = data.GetRowSpan(y); - for (int x = 0; x < c.WidthInBlocks; x++) + for (int x = 0; x < this.WidthInBlocks; x++) { short[] block = blockRow[x].ToArray(); this.MakeBlock(block, y, x); @@ -94,16 +97,7 @@ public static ComponentData Load(JpegComponent c, int index) c.HeightInBlocks, index); - for (int y = 0; y < result.HeightInBlocks; y++) - { - Span blockRow = c.SpectralBlocks.GetRowSpan(y); - for (int x = 0; x < result.WidthInBlocks; x++) - { - short[] data = blockRow[x].ToArray(); - result.MakeBlock(data, y, x); - } - } - + result.LoadSpectral(c); return result; } From 0c78c676278122a1936c48146317b654f96b01bd Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Mon, 12 Jul 2021 21:11:16 +0300 Subject: [PATCH 57/76] Clarified diff logs --- .../Formats/Jpg/SpectralJpegTests.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs index 0235ebb382..197d18940f 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs @@ -57,11 +57,12 @@ public void Decoder_ParseStream_SaveSpectralResult(TestImageProvider image = decoder.Decode(bufferedStream, cancellationToken: default); + + // TODO: Fix this var data = LibJpegTools.SpectralData.LoadFromImageSharpDecoder(decoder); VerifyJpeg.SaveSpectralImage(provider, data); } - //[Theory(Skip = "Temporary skipped due to new decoder core architecture")] [Theory] [WithFileCollection(nameof(AllTestJpegs), PixelTypes.Rgba32)] public void VerifySpectralCorrectness(TestImageProvider provider) @@ -116,11 +117,11 @@ private void VerifySpectralCorrectnessImpl( LibJpegTools.ComponentData libJpegComponent = libJpegData.Components[i]; LibJpegTools.ComponentData imageSharpComponent = imageSharpData.Components[i]; - (double total, double average) diff = LibJpegTools.CalculateDifference(libJpegComponent, imageSharpComponent); + (double total, double average) = LibJpegTools.CalculateDifference(libJpegComponent, imageSharpComponent); - this.Output.WriteLine($"Component{i}: {diff}"); - averageDifference += diff.average; - totalDifference += diff.total; + this.Output.WriteLine($"Component{i}: [total: {total} | average: {average}]"); + averageDifference += average; + totalDifference += total; tolerance += libJpegComponent.SpectralBlocks.DangerousGetSingleSpan().Length; } @@ -173,13 +174,12 @@ public override void ConvertStrideBaseline() this.converter.ConvertStrideBaseline(); // This would be called only for baseline non-interleaved images - // We must test spectral strides here + // We must copy spectral strides here LibJpegTools.ComponentData[] components = this.spectralData.Components; for (int i = 0; i < components.Length; i++) { components[i].LoadSpectralStride(this.frame.Components[i].SpectralBlocks, this.baselineScanRowCounter); } - this.baselineScanRowCounter++; } From 4c97fcc79379ad76d4405a72c2ad9b5c4f3f9cf1 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Mon, 12 Jul 2021 21:17:58 +0300 Subject: [PATCH 58/76] Rolled back spectral buffer cleaning logic --- .../Formats/Jpeg/Components/Decoder/JpegComponent.cs | 4 +--- .../Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs | 8 ++++---- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs index 58c34ecf02..614e96e54a 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs @@ -138,9 +138,7 @@ public void AllocateSpectral(bool fullScan) int spectralAllocWidth = this.SizeInBlocks.Width; int spectralAllocHeight = fullScan ? this.SizeInBlocks.Height : this.VerticalSamplingFactor; - // 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); + this.SpectralBlocks = this.memoryAllocator.Allocate2D(spectralAllocWidth, spectralAllocHeight, AllocationOptions.Clean); } } } diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index 6ad2bf00c1..61f521c2f4 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -90,6 +90,10 @@ public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) public override void ConvertStrideBaseline() { + // Convert next pixel stride using single spectral `stride' + // Note that zero passing eliminates the need of virtual call from JpegComponentPostProcessor + this.ConvertNextStride(spectralStep: 0); + // Clear spectral stride - this is VERY important as jpeg possibly won't fill entire buffer each stride // Which leads to decoding artifacts // Note that this code clears all buffers of the post processors, it's their responsibility to allocate only single stride @@ -97,10 +101,6 @@ public override void ConvertStrideBaseline() { cpp.ClearSpectralBuffers(); } - - // Convert next pixel stride using single spectral `stride' - // Note that zero passing eliminates the need of virtual call from JpegComponentPostProcessor - this.ConvertNextStride(spectralStep: 0); } public override void Dispose() From 024be3b2a2544290f3d64b01a7c7fc6c4b296b9a Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Mon, 12 Jul 2021 21:24:47 +0300 Subject: [PATCH 59/76] Spectral converter base class no longer implements IDisposable interface --- .../Formats/Jpeg/Components/Decoder/SpectralConverter.cs | 8 ++------ .../Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs | 4 ++-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter.cs index 35a8790824..1d0ac200f2 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter.cs @@ -1,16 +1,12 @@ -// Copyright (c) Six Labors. +// Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. -using System; - namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder { - internal abstract class SpectralConverter : IDisposable + internal abstract class SpectralConverter { public abstract void InjectFrameData(JpegFrame frame, IRawJpegData jpegData); public abstract void ConvertStrideBaseline(); - - public abstract void Dispose(); } } diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index 61f521c2f4..9f3d4195cc 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -11,7 +11,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder { - internal sealed class SpectralConverter : SpectralConverter + internal sealed class SpectralConverter : SpectralConverter, IDisposable where TPixel : unmanaged, IPixel { private readonly Configuration configuration; @@ -103,7 +103,7 @@ public override void ConvertStrideBaseline() } } - public override void Dispose() + public void Dispose() { if (this.componentProcessors != null) { From 194f6e022a49a8a5316bc8d86918c625c7792894 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Mon, 12 Jul 2021 21:31:44 +0300 Subject: [PATCH 60/76] Debug converter no longer use actual converter --- .../Formats/Jpg/SpectralJpegTests.cs | 20 ++----------------- 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs index 197d18940f..b694808b76 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs @@ -84,7 +84,7 @@ public void VerifySpectralCorrectness(TestImageProvider provider using var bufferedStream = new BufferedReadStream(Configuration.Default, ms); // internal scan decoder which we substitute to assert spectral correctness - using var debugConverter = new DebugSpectralConverter(Configuration.Default, cancellationToken: default); + var debugConverter = new DebugSpectralConverter(); var scanDecoder = new HuffmanScanDecoder(bufferedStream, debugConverter, cancellationToken: default); // This would parse entire image @@ -147,9 +147,6 @@ private class DebugSpectralConverter : SpectralConverter private int baselineScanRowCounter; - public DebugSpectralConverter(Configuration configuration, CancellationToken cancellationToken) - => this.converter = new SpectralConverter(configuration, cancellationToken); - public LibJpegTools.SpectralData SpectralData { get @@ -171,8 +168,6 @@ public LibJpegTools.SpectralData SpectralData public override void ConvertStrideBaseline() { - this.converter.ConvertStrideBaseline(); - // This would be called only for baseline non-interleaved images // We must copy spectral strides here LibJpegTools.ComponentData[] components = this.spectralData.Components; @@ -180,23 +175,12 @@ public override void ConvertStrideBaseline() { components[i].LoadSpectralStride(this.frame.Components[i].SpectralBlocks, this.baselineScanRowCounter); } - this.baselineScanRowCounter++; - } - public override void Dispose() - { - // As we are only testing spectral data we don't care about pixels - // But we need to dispose allocated pixel buffer - this.converter.PixelBuffer.Dispose(); - - // Converter Dispose must be called after pixel buffer disposal because pixel buffer getter can do a full scan conversion - this.converter?.Dispose(); + this.baselineScanRowCounter++; } public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) { - this.converter.InjectFrameData(frame, jpegData); - this.frame = frame; var spectralComponents = new LibJpegTools.ComponentData[frame.ComponentCount]; From 7540fd9018aae10e351d738ad626204fd8ed0348 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Mon, 12 Jul 2021 23:36:08 +0300 Subject: [PATCH 61/76] Fixed baseline images tsting code --- .../Formats/Jpg/SpectralJpegTests.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs index b694808b76..09548e2761 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs @@ -6,8 +6,10 @@ using System.Linq; using System.Threading; using SixLabors.ImageSharp.Formats.Jpeg; +using SixLabors.ImageSharp.Formats.Jpeg.Components; using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder; using SixLabors.ImageSharp.IO; +using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Tests.Formats.Jpg.Utils; @@ -139,8 +141,6 @@ private void VerifySpectralCorrectnessImpl( private class DebugSpectralConverter : SpectralConverter where TPixel : unmanaged, IPixel { - private readonly SpectralConverter converter; - private JpegFrame frame; private LibJpegTools.SpectralData spectralData; @@ -177,6 +177,16 @@ public override void ConvertStrideBaseline() } this.baselineScanRowCounter++; + + // As spectral buffers are reused for each stride decoding - we need to manually clear it like it's done in SpectralConverter + foreach (JpegComponent component in this.frame.Components) + { + Buffer2D spectralBlocks = component.SpectralBlocks; + for (int i = 0; i < spectralBlocks.Height; i++) + { + spectralBlocks.GetRowSpan(i).Clear(); + } + } } public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) From 0261ea9350ae725d5ae98e662823293e0fb0aa24 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Mon, 12 Jul 2021 23:38:54 +0300 Subject: [PATCH 62/76] Fixed bad EOI image --- .../Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs index f2f926d4ca..a09c7ada3e 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs @@ -162,7 +162,6 @@ private void ParseBaselineDataInterleaved() for (int i = 0; i < mcusPerLine; i++) { // Scan an interleaved mcu... process components in order - int mcuRow = mcu / mcusPerLine; int mcuCol = mcu % mcusPerLine; for (int k = 0; k < this.ComponentsLength; k++) { @@ -186,6 +185,9 @@ private void ParseBaselineDataInterleaved() { if (buffer.NoData) { + // It is very likely that some spectral data was decoded before we encountered EOI marker + // so we need to decode what's left and return (or maybe throw?) + this.spectralConverter.ConvertStrideBaseline(); return; } From 79eb6c401839b54efa6288475f607e9065b0aa42 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Tue, 13 Jul 2021 00:00:50 +0300 Subject: [PATCH 63/76] Fixed metadata only pass for a test --- tests/ImageSharp.Tests/Formats/Jpg/ParseStreamTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/ParseStreamTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/ParseStreamTests.cs index de8103d639..a124ec1918 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/ParseStreamTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/ParseStreamTests.cs @@ -32,7 +32,7 @@ public void ColorSpace_IsDeducedCorrectly(string imageFile, object expectedColor { var expectedColorSpace = (JpegColorSpace)expectedColorSpaceValue; - using (JpegDecoderCore decoder = JpegFixture.ParseJpegStream(imageFile)) + using (JpegDecoderCore decoder = JpegFixture.ParseJpegStream(imageFile, metaDataOnly: true)) { Assert.Equal(expectedColorSpace, decoder.ColorSpace); } From d7084eb686b06cd4e397a5c432c82e19954fc822 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Tue, 13 Jul 2021 12:49:36 +0300 Subject: [PATCH 64/76] Style fixes --- tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs index a2f7583a16..a052ee88a5 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs @@ -131,10 +131,10 @@ public async Task DecodeAsync_DegenerateMemoryRequest_ShouldTranslateTo_ImageFor [InlineData(0)] [InlineData(0.5)] [InlineData(0.9)] - public async Task Decode_IsCancellable(int percentageOfStreamReadToCancel) + public async Task DecodeAsync_IsCancellable(int percentageOfStreamReadToCancel) { var cts = new CancellationTokenSource(); - var file = Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, TestImages.Jpeg.Baseline.Jpeg420Small); + string file = Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, TestImages.Jpeg.Baseline.Jpeg420Small); using var pausedStream = new PausedStream(file); pausedStream.OnWaiting(s => { From 2e5b0ad74da6b476f812266b193be81c63ba17f5 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Tue, 13 Jul 2021 14:03:34 +0300 Subject: [PATCH 65/76] Fixed baseline image invalid reference output png image --- .../Jpg/SpectralToPixelConversionTests.cs | 69 +++++++++++++++++++ .../DecodeBaselineJpeg_jpeg420small.png | 4 +- 2 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 tests/ImageSharp.Tests/Formats/Jpg/SpectralToPixelConversionTests.cs diff --git a/tests/ImageSharp.Tests/Formats/Jpg/SpectralToPixelConversionTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/SpectralToPixelConversionTests.cs new file mode 100644 index 0000000000..b0e5a3db6b --- /dev/null +++ b/tests/ImageSharp.Tests/Formats/Jpg/SpectralToPixelConversionTests.cs @@ -0,0 +1,69 @@ +// Copyright (c) Six Labors. +// Licensed under the Apache License, Version 2.0. + +using System.IO; +using System.Linq; +using SixLabors.ImageSharp.Formats.Jpeg; +using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder; +using SixLabors.ImageSharp.IO; +using SixLabors.ImageSharp.Metadata; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; +using Xunit; +using Xunit.Abstractions; + +namespace SixLabors.ImageSharp.Tests.Formats.Jpg +{ + [Trait("Format", "Jpg")] + public class SpectralToPixelConversionTests + { + public static readonly string[] BaselineTestJpegs = + { + TestImages.Jpeg.Baseline.Calliphora, TestImages.Jpeg.Baseline.Cmyk, TestImages.Jpeg.Baseline.Jpeg400, + TestImages.Jpeg.Baseline.Jpeg444, TestImages.Jpeg.Baseline.Testorig420, + TestImages.Jpeg.Baseline.Jpeg420Small, TestImages.Jpeg.Baseline.Bad.BadEOF, + TestImages.Jpeg.Baseline.MultiScanBaselineCMYK + }; + + public SpectralToPixelConversionTests(ITestOutputHelper output) + { + this.Output = output; + } + + private ITestOutputHelper Output { get; } + + [Theory] + [WithFileCollection(nameof(BaselineTestJpegs), PixelTypes.Rgba32)] + public void Decoder_PixelBufferComparison(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + // Stream + byte[] sourceBytes = TestFile.Create(provider.SourceFileOrDescription).Bytes; + using var ms = new MemoryStream(sourceBytes); + using var bufferedStream = new BufferedReadStream(Configuration.Default, ms); + + // Decoding + using var converter = new SpectralConverter(Configuration.Default, cancellationToken: default); + var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder()); + var scanDecoder = new HuffmanScanDecoder(bufferedStream, converter, cancellationToken: default); + decoder.ParseStream(bufferedStream, scanDecoder, ct: default); + + // Test metadata + provider.Utility.TestGroupName = nameof(JpegDecoderTests); + provider.Utility.TestName = JpegDecoderTests.DecodeBaselineJpegOutputName; + + // Comparison + using (Image image = new Image(Configuration.Default, converter.PixelBuffer, new ImageMetadata())) + using (Image referenceImage = provider.GetReferenceOutputImage(appendPixelTypeToFileName: false)) + { + ImageSimilarityReport report = ImageComparer.Exact.CompareImagesOrFrames(referenceImage, image); + + this.Output.WriteLine($"*** {provider.SourceFileOrDescription} ***"); + this.Output.WriteLine($"Difference: {report.DifferencePercentageString}"); + + // ReSharper disable once PossibleInvalidOperationException + Assert.True(report.TotalNormalizedDifference.Value < 0.005f); + } + } + } +} diff --git a/tests/Images/External/ReferenceOutput/JpegDecoderTests/DecodeBaselineJpeg_jpeg420small.png b/tests/Images/External/ReferenceOutput/JpegDecoderTests/DecodeBaselineJpeg_jpeg420small.png index c57b00d0e3..4032a32afb 100644 --- a/tests/Images/External/ReferenceOutput/JpegDecoderTests/DecodeBaselineJpeg_jpeg420small.png +++ b/tests/Images/External/ReferenceOutput/JpegDecoderTests/DecodeBaselineJpeg_jpeg420small.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:a76832570111a868ea6cb6e8287aae1976c575c94c63880c74346a4b5db5d305 -size 27007 +oid sha256:2b5e1d91fb6dc1ddb696fbee63331ba9c6ef3548b619c005887e60c5b01f4981 +size 27303 From 005fff7fb3bc37ff31104c5cdcc84dd95c9d5bbb Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Tue, 13 Jul 2021 14:11:35 +0300 Subject: [PATCH 66/76] Removed post processor tests --- .../Jpg/JpegImagePostProcessorTests.cs | 97 ------------------- 1 file changed, 97 deletions(-) delete mode 100644 tests/ImageSharp.Tests/Formats/Jpg/JpegImagePostProcessorTests.cs diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegImagePostProcessorTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegImagePostProcessorTests.cs deleted file mode 100644 index 1a969060cd..0000000000 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegImagePostProcessorTests.cs +++ /dev/null @@ -1,97 +0,0 @@ -// Copyright (c) Six Labors. -// Licensed under the Apache License, Version 2.0. - -using SixLabors.ImageSharp.Formats.Jpeg; -using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder; -using SixLabors.ImageSharp.PixelFormats; -using SixLabors.ImageSharp.Tests.Formats.Jpg.Utils; -using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; - -using Xunit; -using Xunit.Abstractions; - -namespace SixLabors.ImageSharp.Tests.Formats.Jpg -{ - [Trait("Format", "Jpg")] - public class JpegImagePostProcessorTests - { - public static string[] BaselineTestJpegs = - { - TestImages.Jpeg.Baseline.Calliphora, - TestImages.Jpeg.Baseline.Cmyk, - TestImages.Jpeg.Baseline.Ycck, - TestImages.Jpeg.Baseline.Jpeg400, - TestImages.Jpeg.Baseline.Testorig420, - TestImages.Jpeg.Baseline.Jpeg444, - }; - - public JpegImagePostProcessorTests(ITestOutputHelper output) - { - this.Output = output; - } - - private ITestOutputHelper Output { get; } - - private static void SaveBuffer(JpegComponentPostProcessor cp, TestImageProvider provider) - where TPixel : unmanaged, IPixel - { - using (Image image = cp.ColorBuffer.ToGrayscaleImage(1f / 255f)) - { - image.DebugSave(provider, $"-C{cp.Component.Index}-"); - } - } - - [Theory(Skip = "Decoding core had breaking internal change and no longer supports post processing after stream parsing.")] - [WithFile(TestImages.Jpeg.Baseline.Calliphora, PixelTypes.Rgba32)] - [WithFile(TestImages.Jpeg.Baseline.Testorig420, PixelTypes.Rgba32)] - public void DoProcessorStep(TestImageProvider provider) - where TPixel : unmanaged, IPixel - { - string imageFile = provider.SourceFileOrDescription; - using (JpegDecoderCore decoder = JpegFixture.ParseJpegStream(imageFile)) - using (var pp = new JpegImagePostProcessor(Configuration.Default, decoder)) - using (var imageFrame = new ImageFrame(Configuration.Default, decoder.ImageWidth, decoder.ImageHeight)) - { - pp.DoPostProcessorStep(imageFrame); - - JpegComponentPostProcessor[] cp = pp.ComponentProcessors; - - SaveBuffer(cp[0], provider); - SaveBuffer(cp[1], provider); - SaveBuffer(cp[2], provider); - } - } - - [Theory(Skip = "Decoding core had breaking internal change and no longer supports post processing after stream parsing.")] - [WithFileCollection(nameof(BaselineTestJpegs), PixelTypes.Rgba32)] - public void PostProcess(TestImageProvider provider) - where TPixel : unmanaged, IPixel - { - string imageFile = provider.SourceFileOrDescription; - using (JpegDecoderCore decoder = JpegFixture.ParseJpegStream(imageFile)) - using (var pp = new JpegImagePostProcessor(Configuration.Default, decoder)) - using (var image = new Image(decoder.ImageWidth, decoder.ImageHeight)) - { - pp.PostProcess(image.Frames.RootFrame, default); - - image.DebugSave(provider); - - ImagingTestCaseUtility testUtil = provider.Utility; - testUtil.TestGroupName = nameof(JpegDecoderTests); - testUtil.TestName = JpegDecoderTests.DecodeBaselineJpegOutputName; - - using (Image referenceImage = - provider.GetReferenceOutputImage(appendPixelTypeToFileName: false)) - { - ImageSimilarityReport report = ImageComparer.Exact.CompareImagesOrFrames(referenceImage, image); - - this.Output.WriteLine($"*** {imageFile} ***"); - this.Output.WriteLine($"Difference: {report.DifferencePercentageString}"); - - // ReSharper disable once PossibleInvalidOperationException - Assert.True(report.TotalNormalizedDifference.Value < 0.005f); - } - } - } - } -} From c6a2c6b8f84dbdcee5d3f8dff4b8b0289d2f351d Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Tue, 13 Jul 2021 14:12:22 +0300 Subject: [PATCH 67/76] Removed post processor from jpeg decoder --- .../Decoder/JpegImagePostProcessor.cs | 164 ------------------ .../Formats/Jpeg/JpegDecoderCore.cs | 27 --- 2 files changed, 191 deletions(-) delete mode 100644 src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs deleted file mode 100644 index 26a0635244..0000000000 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs +++ /dev/null @@ -1,164 +0,0 @@ -// Copyright (c) Six Labors. -// Licensed under the Apache License, Version 2.0. - -using System; -using System.Buffers; -using System.Numerics; -using System.Threading; -using SixLabors.ImageSharp.Memory; -using SixLabors.ImageSharp.PixelFormats; -using JpegColorConverter = SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder.ColorConverters.JpegColorConverter; - -namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder -{ - /// - /// Encapsulates the execution od post-processing algorithms to be applied on a to produce a valid :
- /// (1) Dequantization
- /// (2) IDCT
- /// (3) Color conversion form one of the -s into a buffer of RGBA values
- /// (4) Packing pixels from the buffer.
- /// These operations are executed in steps. - /// image rows are converted in one step, - /// which means that size of the allocated memory is limited (does not depend on ). - ///
- internal class JpegImagePostProcessor : IDisposable - { - private readonly Configuration configuration; - - /// - /// The number of block rows to be processed in one Step. - /// - private readonly int blockRowsPerStep; - - /// - /// The number of image pixel rows to be processed in one step. - /// - private readonly int pixelRowsPerStep; - - /// - /// Temporal buffer to store a row of colors. - /// - private readonly IMemoryOwner rgbaBuffer; - - /// - /// The corresponding to the current determined by . - /// - private readonly JpegColorConverter colorConverter; - - /// - /// Initializes a new instance of the class. - /// - /// The to configure internal operations. - /// The representing the uncompressed spectral Jpeg data - public JpegImagePostProcessor(Configuration configuration, IRawJpegData rawJpeg) - { - this.configuration = configuration; - this.RawJpeg = rawJpeg; - IJpegComponent c0 = rawJpeg.Components[0]; - - this.blockRowsPerStep = c0.SamplingFactors.Height; - this.pixelRowsPerStep = this.blockRowsPerStep * 8; - - this.NumberOfPostProcessorSteps = c0.SizeInBlocks.Height / this.blockRowsPerStep; - - var postProcessorBufferSize = new Size(c0.SizeInBlocks.Width * 8, this.pixelRowsPerStep); - MemoryAllocator memoryAllocator = configuration.MemoryAllocator; - this.ComponentProcessors = new JpegComponentPostProcessor[rawJpeg.Components.Length]; - for (int i = 0; i < rawJpeg.Components.Length; i++) - { - this.ComponentProcessors[i] = new JpegComponentPostProcessor(memoryAllocator, this.RawJpeg, postProcessorBufferSize, rawJpeg.Components[i]); - } - - this.rgbaBuffer = memoryAllocator.Allocate(rawJpeg.ImageSizeInPixels.Width); - this.colorConverter = JpegColorConverter.GetConverter(rawJpeg.ColorSpace, rawJpeg.Precision); - } - - /// - /// Gets the instances. - /// - public JpegComponentPostProcessor[] ComponentProcessors { get; } - - /// - /// Gets the to be processed. - /// - public IRawJpegData RawJpeg { get; } - - /// - /// Gets the total number of post processor steps deduced from the height of the image and . - /// - public int NumberOfPostProcessorSteps { get; } - - /// - /// Gets the value of the counter that grows by each step by . - /// - public int PixelRowCounter { get; private set; } - - /// - public void Dispose() - { - foreach (JpegComponentPostProcessor cpp in this.ComponentProcessors) - { - cpp.Dispose(); - } - - this.rgbaBuffer.Dispose(); - } - - /// - /// Process all pixels into 'destination'. The image dimensions should match . - /// - /// The pixel type - /// The destination image - /// The token to request cancellation. - public void PostProcess(ImageFrame destination, CancellationToken cancellationToken) - where TPixel : unmanaged, IPixel - { - this.PixelRowCounter = 0; - - if (this.RawJpeg.ImageSizeInPixels != destination.Size()) - { - throw new ArgumentException("Input image is not of the size of the processed one!"); - } - - while (this.PixelRowCounter < this.RawJpeg.ImageSizeInPixels.Height) - { - cancellationToken.ThrowIfCancellationRequested(); - this.DoPostProcessorStep(destination); - } - } - - /// - /// Execute one step processing pixel rows into 'destination'. - /// Convert and copy row of colors into 'destination' starting at row . - /// - /// The pixel type - /// The destination image - public void DoPostProcessorStep(ImageFrame destination) - where TPixel : unmanaged, IPixel - { - int maxY = Math.Min(destination.Height, this.PixelRowCounter + this.pixelRowsPerStep); - - var buffers = new Buffer2D[this.ComponentProcessors.Length]; - for (int i = 0; i < this.ComponentProcessors.Length; i++) - { - this.ComponentProcessors[i].CopyBlocksToColorBuffer(); - buffers[i] = this.ComponentProcessors[i].ColorBuffer; - } - - for (int yy = this.PixelRowCounter; yy < maxY; yy++) - { - int y = yy - this.PixelRowCounter; - - var values = new JpegColorConverter.ComponentValues(buffers, y); - this.colorConverter.ConvertToRgba(values, this.rgbaBuffer.GetSpan()); - - Span destRow = destination.GetPixelRowSpan(yy); - - // TODO: Investigate if slicing is actually necessary - PixelOperations.Instance.FromVector4Destructive(this.configuration, this.rgbaBuffer.GetSpan().Slice(0, destRow.Length), destRow); - } - - this.PixelRowCounter += this.pixelRowsPerStep; - } - } -} diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index c7a5bc42c7..c4f8a1281f 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -1112,32 +1112,5 @@ private ushort ReadUint16(BufferedReadStream stream) stream.Read(this.markerBuffer, 0, 2); return BinaryPrimitives.ReadUInt16BigEndian(this.markerBuffer); } - - /// - /// Post processes the pixels into the destination image. - /// - /// The pixel format. - /// The . - private Image PostProcessIntoImage(CancellationToken cancellationToken) - where TPixel : unmanaged, IPixel - { - if (this.ImageWidth == 0 || this.ImageHeight == 0) - { - JpegThrowHelper.ThrowInvalidImageDimensions(this.ImageWidth, this.ImageHeight); - } - - var image = Image.CreateUninitialized( - this.Configuration, - this.ImageWidth, - this.ImageHeight, - this.Metadata); - - using (var postProcessor = new JpegImagePostProcessor(this.Configuration, this)) - { - postProcessor.PostProcess(image.Frames.RootFrame, cancellationToken); - } - - return image; - } } } From 865c7060a38e9164980426b8cc0284488c629b2c Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Tue, 13 Jul 2021 14:14:52 +0300 Subject: [PATCH 68/76] Added new tolerance to the Jpeg420Small test image --- tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs index 2faea2611e..304dd93a63 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs @@ -17,8 +17,6 @@ public partial class JpegDecoderTests TestImages.Jpeg.Baseline.Jpeg400, TestImages.Jpeg.Baseline.Turtle420, TestImages.Jpeg.Baseline.Testorig420, - - // BUG: The following image has a high difference compared to the expected output: 1.0096% TestImages.Jpeg.Baseline.Jpeg420Small, TestImages.Jpeg.Issues.Fuzz.AccessViolationException922, TestImages.Jpeg.Baseline.Jpeg444, @@ -101,7 +99,7 @@ public partial class JpegDecoderTests [TestImages.Jpeg.Baseline.Bad.BadRST] = 0.0589f / 100, [TestImages.Jpeg.Baseline.Testorig420] = 0.38f / 100, - [TestImages.Jpeg.Baseline.Jpeg420Small] = 1.1f / 100, + [TestImages.Jpeg.Baseline.Jpeg420Small] = 0.287f / 100, [TestImages.Jpeg.Baseline.Turtle420] = 1.0f / 100, // Progressive: From 8b6ad9ce8a9e333b6c47d6592efa49ce78d62934 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Tue, 13 Jul 2021 14:24:33 +0300 Subject: [PATCH 69/76] Fixed docs --- .../Jpeg/Components/Decoder/JpegComponentPostProcessor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs index 2d38b417cd..79965a3f0c 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs @@ -7,7 +7,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder { /// - /// Encapsulates postprocessing data for one component for . + /// Encapsulates spectral data to rgba32 processing for one component. /// internal class JpegComponentPostProcessor : IDisposable { From 84900dcd2954d93c66169396dfa965df24824376 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Tue, 13 Jul 2021 15:58:36 +0300 Subject: [PATCH 70/76] Restored memory stress test to the sandbox --- tests/ImageSharp.Tests.ProfilingSandbox/Program.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs b/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs index 9aa983ac5f..e6e82b9810 100644 --- a/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs +++ b/tests/ImageSharp.Tests.ProfilingSandbox/Program.cs @@ -31,13 +31,14 @@ private class ConsoleOutput : ITestOutputHelper /// public static void Main(string[] args) { + LoadResizeSaveParallelMemoryStress.Run(); // RunJpegEncoderProfilingTests(); // RunJpegColorProfilingTests(); // RunDecodeJpegProfilingTests(); // RunToVector4ProfilingTest(); // RunResizeProfilingTest(); - Console.ReadLine(); + // Console.ReadLine(); } private static void RunJpegEncoderProfilingTests() From 82e22c30b4f01c90f0f6c4b36b6b64902abfb981 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Tue, 13 Jul 2021 16:11:05 +0300 Subject: [PATCH 71/76] Restored decoder parse stream only benchmark --- .../Formats/Jpeg/JpegDecoderCore.cs | 8 +- .../Codecs/Jpeg/DecodeJpegParseStreamOnly.cs | 135 ++++++++++-------- .../Formats/Jpg/SpectralJpegTests.cs | 2 +- .../Jpg/SpectralToPixelConversionTests.cs | 2 +- 4 files changed, 82 insertions(+), 65 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index c4f8a1281f..922e9797cb 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -248,8 +248,8 @@ public IImageInfo Identify(BufferedReadStream stream, CancellationToken cancella ///
/// The input stream. /// Scan decoder used exclusively to decode SOS marker. - /// The token to monitor cancellation. - internal void ParseStream(BufferedReadStream stream, HuffmanScanDecoder scanDecoder, CancellationToken ct) + /// The token to monitor cancellation. + internal void ParseStream(BufferedReadStream stream, HuffmanScanDecoder scanDecoder, CancellationToken cancellationToken) { bool metadataOnly = scanDecoder == null; @@ -283,7 +283,7 @@ internal void ParseStream(BufferedReadStream stream, HuffmanScanDecoder scanDeco while (fileMarker.Marker != JpegConstants.Markers.EOI || (fileMarker.Marker == JpegConstants.Markers.EOI && fileMarker.Invalid)) { - ct.ThrowIfCancellationRequested(); + cancellationToken.ThrowIfCancellationRequested(); if (!fileMarker.Invalid) { @@ -301,7 +301,7 @@ internal void ParseStream(BufferedReadStream stream, HuffmanScanDecoder scanDeco case JpegConstants.Markers.SOS: if (!metadataOnly) { - this.ProcessStartOfScanMarker(stream, ct); + this.ProcessStartOfScanMarker(stream, cancellationToken); break; } else diff --git a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs index 6796faa6d0..8659aee634 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs @@ -1,59 +1,76 @@ -//// Copyright (c) Six Labors. -//// Licensed under the Apache License, Version 2.0. - -//using System.IO; -//using BenchmarkDotNet.Attributes; -//using SixLabors.ImageSharp.Formats.Jpeg; -//using SixLabors.ImageSharp.IO; -//using SixLabors.ImageSharp.Tests; -//using SDSize = System.Drawing.Size; - -//namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jpeg -//{ -// [Config(typeof(Config.ShortMultiFramework))] -// public class DecodeJpegParseStreamOnly -// { -// [Params(TestImages.Jpeg.BenchmarkSuite.Lake_Small444YCbCr)] -// public string TestImage { get; set; } - -// private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage); - -// private byte[] jpegBytes; - -// [GlobalSetup] -// public void Setup() -// => this.jpegBytes = File.ReadAllBytes(this.TestImageFullPath); - -// [Benchmark(Baseline = true, Description = "System.Drawing FULL")] -// public SDSize JpegSystemDrawing() -// { -// using var memoryStream = new MemoryStream(this.jpegBytes); -// using var image = System.Drawing.Image.FromStream(memoryStream); -// return image.Size; -// } - -// [Benchmark(Description = "JpegDecoderCore.ParseStream")] -// public void ParseStream() -// { -// using var memoryStream = new MemoryStream(this.jpegBytes); -// using var bufferedStream = new BufferedReadStream(Configuration.Default, memoryStream); - -// var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder { IgnoreMetadata = true }); -// decoder.ParseStream(bufferedStream); -// decoder.Dispose(); -// } -// } - -// /* -// | Method | Job | Runtime | TestImage | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated | -// |---------------------------- |----------- |-------------- |--------------------- |---------:|----------:|----------:|------:|--------:|------:|------:|----------:| -// | 'System.Drawing FULL' | Job-HITJFX | .NET 4.7.2 | Jpg/b(...)e.jpg [21] | 5.828 ms | 0.9885 ms | 0.0542 ms | 1.00 | 46.8750 | - | - | 211566 B | -// | JpegDecoderCore.ParseStream | Job-HITJFX | .NET 4.7.2 | Jpg/b(...)e.jpg [21] | 5.833 ms | 0.2923 ms | 0.0160 ms | 1.00 | - | - | - | 12416 B | -// | | | | | | | | | | | | | -// | 'System.Drawing FULL' | Job-WPSKZD | .NET Core 2.1 | Jpg/b(...)e.jpg [21] | 6.018 ms | 2.1374 ms | 0.1172 ms | 1.00 | 46.8750 | - | - | 210768 B | -// | JpegDecoderCore.ParseStream | Job-WPSKZD | .NET Core 2.1 | Jpg/b(...)e.jpg [21] | 4.382 ms | 0.9009 ms | 0.0494 ms | 0.73 | - | - | - | 12360 B | -// | | | | | | | | | | | | | -// | 'System.Drawing FULL' | Job-ZLSNRP | .NET Core 3.1 | Jpg/b(...)e.jpg [21] | 5.714 ms | 0.4078 ms | 0.0224 ms | 1.00 | - | - | - | 176 B | -// | JpegDecoderCore.ParseStream | Job-ZLSNRP | .NET Core 3.1 | Jpg/b(...)e.jpg [21] | 4.239 ms | 1.0943 ms | 0.0600 ms | 0.74 | - | - | - | 12406 B | -// */ -//} +// Copyright (c) Six Labors. +// Licensed under the Apache License, Version 2.0. + +using System.IO; +using BenchmarkDotNet.Attributes; +using SixLabors.ImageSharp.Formats.Jpeg; +using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder; +using SixLabors.ImageSharp.IO; +using SixLabors.ImageSharp.Tests; +using SDSize = System.Drawing.Size; + +namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jpeg +{ + //[Config(typeof(Config.ShortMultiFramework))] + public class DecodeJpegParseStreamOnly + { + [Params(TestImages.Jpeg.BenchmarkSuite.Lake_Small444YCbCr)] + public string TestImage { get; set; } + + private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage); + + private byte[] jpegBytes; + + [GlobalSetup] + public void Setup() + => this.jpegBytes = File.ReadAllBytes(this.TestImageFullPath); + + //[Benchmark(Baseline = true, Description = "System.Drawing FULL")] + //public SDSize JpegSystemDrawing() + //{ + // using var memoryStream = new MemoryStream(this.jpegBytes); + // using var image = System.Drawing.Image.FromStream(memoryStream); + // return image.Size; + //} + + [Benchmark(Description = "JpegDecoderCore.ParseStream")] + public void ParseStream() + { + using var memoryStream = new MemoryStream(this.jpegBytes); + using var bufferedStream = new BufferedReadStream(Configuration.Default, memoryStream); + + var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder { IgnoreMetadata = true }); + var scanDecoder = new HuffmanScanDecoder(bufferedStream, new NoopSpectralConverter(), cancellationToken: default); + decoder.ParseStream(bufferedStream, scanDecoder, cancellationToken: default); + decoder.Dispose(); + } + + // We want to test only stream parsing and scan decoding, we don't need to convert spectral data to actual pixels + // Nor we need to allocate final pixel buffer + // Note: this still introduces virtual method call overhead for baseline interleaved images + // There's no way to eliminate it as spectral conversion is built into the scan decoding loop for memory footprint reduction + private class NoopSpectralConverter : SpectralConverter + { + public override void ConvertStrideBaseline() + { + } + + public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) + { + } + } + } + + /* + | Method | Job | Runtime | TestImage | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated | + |---------------------------- |----------- |-------------- |--------------------- |---------:|----------:|----------:|------:|--------:|------:|------:|----------:| + | 'System.Drawing FULL' | Job-HITJFX | .NET 4.7.2 | Jpg/b(...)e.jpg [21] | 5.828 ms | 0.9885 ms | 0.0542 ms | 1.00 | 46.8750 | - | - | 211566 B | + | JpegDecoderCore.ParseStream | Job-HITJFX | .NET 4.7.2 | Jpg/b(...)e.jpg [21] | 5.833 ms | 0.2923 ms | 0.0160 ms | 1.00 | - | - | - | 12416 B | + | | | | | | | | | | | | | + | 'System.Drawing FULL' | Job-WPSKZD | .NET Core 2.1 | Jpg/b(...)e.jpg [21] | 6.018 ms | 2.1374 ms | 0.1172 ms | 1.00 | 46.8750 | - | - | 210768 B | + | JpegDecoderCore.ParseStream | Job-WPSKZD | .NET Core 2.1 | Jpg/b(...)e.jpg [21] | 4.382 ms | 0.9009 ms | 0.0494 ms | 0.73 | - | - | - | 12360 B | + | | | | | | | | | | | | | + | 'System.Drawing FULL' | Job-ZLSNRP | .NET Core 3.1 | Jpg/b(...)e.jpg [21] | 5.714 ms | 0.4078 ms | 0.0224 ms | 1.00 | - | - | - | 176 B | + | JpegDecoderCore.ParseStream | Job-ZLSNRP | .NET Core 3.1 | Jpg/b(...)e.jpg [21] | 4.239 ms | 1.0943 ms | 0.0600 ms | 0.74 | - | - | - | 12406 B | + */ +} diff --git a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs index 09548e2761..0d4881adab 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs @@ -90,7 +90,7 @@ public void VerifySpectralCorrectness(TestImageProvider provider var scanDecoder = new HuffmanScanDecoder(bufferedStream, debugConverter, cancellationToken: default); // This would parse entire image - decoder.ParseStream(bufferedStream, scanDecoder, ct: default); + decoder.ParseStream(bufferedStream, scanDecoder, cancellationToken: default); // Actual verification this.VerifySpectralCorrectnessImpl(libJpegData, debugConverter.SpectralData); diff --git a/tests/ImageSharp.Tests/Formats/Jpg/SpectralToPixelConversionTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/SpectralToPixelConversionTests.cs index b0e5a3db6b..353ae39f0f 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/SpectralToPixelConversionTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/SpectralToPixelConversionTests.cs @@ -46,7 +46,7 @@ public void Decoder_PixelBufferComparison(TestImageProvider prov using var converter = new SpectralConverter(Configuration.Default, cancellationToken: default); var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder()); var scanDecoder = new HuffmanScanDecoder(bufferedStream, converter, cancellationToken: default); - decoder.ParseStream(bufferedStream, scanDecoder, ct: default); + decoder.ParseStream(bufferedStream, scanDecoder, cancellationToken: default); // Test metadata provider.Utility.TestGroupName = nameof(JpegDecoderTests); From 0c27adc96fd2f2848ed9a5c64f4e9b280086de00 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Tue, 13 Jul 2021 16:26:13 +0300 Subject: [PATCH 72/76] Updated StreamParseOnly benchmark --- .../Codecs/Jpeg/DecodeJpegParseStreamOnly.cs | 51 +++++++++++-------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs index 8659aee634..9db666c374 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs @@ -11,7 +11,7 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jpeg { - //[Config(typeof(Config.ShortMultiFramework))] + [Config(typeof(Config.ShortMultiFramework))] public class DecodeJpegParseStreamOnly { [Params(TestImages.Jpeg.BenchmarkSuite.Lake_Small444YCbCr)] @@ -25,13 +25,13 @@ public class DecodeJpegParseStreamOnly public void Setup() => this.jpegBytes = File.ReadAllBytes(this.TestImageFullPath); - //[Benchmark(Baseline = true, Description = "System.Drawing FULL")] - //public SDSize JpegSystemDrawing() - //{ - // using var memoryStream = new MemoryStream(this.jpegBytes); - // using var image = System.Drawing.Image.FromStream(memoryStream); - // return image.Size; - //} + [Benchmark(Baseline = true, Description = "System.Drawing FULL")] + public SDSize JpegSystemDrawing() + { + using var memoryStream = new MemoryStream(this.jpegBytes); + using var image = System.Drawing.Image.FromStream(memoryStream); + return image.Size; + } [Benchmark(Description = "JpegDecoderCore.ParseStream")] public void ParseStream() @@ -60,17 +60,26 @@ public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData) } } } - - /* - | Method | Job | Runtime | TestImage | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated | - |---------------------------- |----------- |-------------- |--------------------- |---------:|----------:|----------:|------:|--------:|------:|------:|----------:| - | 'System.Drawing FULL' | Job-HITJFX | .NET 4.7.2 | Jpg/b(...)e.jpg [21] | 5.828 ms | 0.9885 ms | 0.0542 ms | 1.00 | 46.8750 | - | - | 211566 B | - | JpegDecoderCore.ParseStream | Job-HITJFX | .NET 4.7.2 | Jpg/b(...)e.jpg [21] | 5.833 ms | 0.2923 ms | 0.0160 ms | 1.00 | - | - | - | 12416 B | - | | | | | | | | | | | | | - | 'System.Drawing FULL' | Job-WPSKZD | .NET Core 2.1 | Jpg/b(...)e.jpg [21] | 6.018 ms | 2.1374 ms | 0.1172 ms | 1.00 | 46.8750 | - | - | 210768 B | - | JpegDecoderCore.ParseStream | Job-WPSKZD | .NET Core 2.1 | Jpg/b(...)e.jpg [21] | 4.382 ms | 0.9009 ms | 0.0494 ms | 0.73 | - | - | - | 12360 B | - | | | | | | | | | | | | | - | 'System.Drawing FULL' | Job-ZLSNRP | .NET Core 3.1 | Jpg/b(...)e.jpg [21] | 5.714 ms | 0.4078 ms | 0.0224 ms | 1.00 | - | - | - | 176 B | - | JpegDecoderCore.ParseStream | Job-ZLSNRP | .NET Core 3.1 | Jpg/b(...)e.jpg [21] | 4.239 ms | 1.0943 ms | 0.0600 ms | 0.74 | - | - | - | 12406 B | - */ } + +/* +BenchmarkDotNet=v0.13.0, OS=Windows 10.0.19042.1083 (20H2/October2020Update) +Intel Core i7-6700K CPU 4.00GHz (Skylake), 1 CPU, 8 logical and 4 physical cores +.NET SDK=6.0.100-preview.3.21202.5 + [Host] : .NET Core 3.1.13 (CoreCLR 4.700.21.11102, CoreFX 4.700.21.11602), X64 RyuJIT + Job-VAJCIU : .NET Core 2.1.26 (CoreCLR 4.6.29812.02, CoreFX 4.6.29812.01), X64 RyuJIT + Job-INPXCR : .NET Core 3.1.13 (CoreCLR 4.700.21.11102, CoreFX 4.700.21.11602), X64 RyuJIT + Job-JRCLOJ : .NET Framework 4.8 (4.8.4390.0), X64 RyuJIT + +IterationCount=3 LaunchCount=1 WarmupCount=3 +| Method | Job | Runtime | TestImage | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated | +|---------------------------- |----------- |--------------------- |---------------------- |---------:|----------:|----------:|------:|--------:|------:|------:|----------:| +| 'System.Drawing FULL' | Job-VAJCIU | .NET Core 2.1 | Jpg/baseline/Lake.jpg | 5.196 ms | 0.7520 ms | 0.0412 ms | 1.00 | 46.8750 | - | - | 210,768 B | +| JpegDecoderCore.ParseStream | Job-VAJCIU | .NET Core 2.1 | Jpg/baseline/Lake.jpg | 3.467 ms | 0.0784 ms | 0.0043 ms | 0.67 | - | - | - | 12,416 B | +| | | | | | | | | | | | | +| 'System.Drawing FULL' | Job-INPXCR | .NET Core 3.1 | Jpg/baseline/Lake.jpg | 5.201 ms | 0.4105 ms | 0.0225 ms | 1.00 | - | - | - | 183 B | +| JpegDecoderCore.ParseStream | Job-INPXCR | .NET Core 3.1 | Jpg/baseline/Lake.jpg | 3.349 ms | 0.0468 ms | 0.0026 ms | 0.64 | - | - | - | 12,408 B | +| | | | | | | | | | | | | +| 'System.Drawing FULL' | Job-JRCLOJ | .NET Framework 4.7.2 | Jpg/baseline/Lake.jpg | 5.164 ms | 0.6524 ms | 0.0358 ms | 1.00 | 46.8750 | - | - | 211,571 B | +| JpegDecoderCore.ParseStream | Job-JRCLOJ | .NET Framework 4.7.2 | Jpg/baseline/Lake.jpg | 4.548 ms | 0.3357 ms | 0.0184 ms | 0.88 | - | - | - | 12,480 B | +*/ From 2eaa2d54e366777472e17c05d5958f03ea9f0e44 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Tue, 13 Jul 2021 17:23:24 +0300 Subject: [PATCH 73/76] Added docs --- .../Components/Decoder/SpectralConverter.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter.cs index 1d0ac200f2..e84d13ff16 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter.cs @@ -3,10 +3,32 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder { + /// + /// Converter used to convert jpeg spectral data. + /// + /// + /// This is tightly coupled with and . + /// internal abstract class SpectralConverter { + /// + /// Injects jpeg image decoding metadata. + /// + /// + /// This is guaranteed to be called only once at SOF marker by . + /// + /// instance containing decoder-specific parameters. + /// instance containing decoder-specific parameters. public abstract void InjectFrameData(JpegFrame frame, IRawJpegData jpegData); + /// + /// Called once per spectral stride for each component in . + /// This is called only for baseline interleaved jpegs. + /// + /// + /// Spectral 'stride' doesn't particularly mean 'single stride'. + /// Actual stride height depends on the subsampling factor of the given component. + /// public abstract void ConvertStrideBaseline(); } } From 13c3a45a9832188783e0857b700b2f4d5e3d6235 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Tue, 13 Jul 2021 17:53:17 +0300 Subject: [PATCH 74/76] Added DivideCeil --- src/ImageSharp/Common/Helpers/Numerics.cs | 8 +++++ .../Jpeg/Components/Decoder/JpegFrame.cs | 4 +-- .../ImageSharp.Tests/Common/NumericsTests.cs | 34 +++++++++++++++++-- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/ImageSharp/Common/Helpers/Numerics.cs b/src/ImageSharp/Common/Helpers/Numerics.cs index db65b84cca..ba5c588ca5 100644 --- a/src/ImageSharp/Common/Helpers/Numerics.cs +++ b/src/ImageSharp/Common/Helpers/Numerics.cs @@ -879,5 +879,13 @@ ref MemoryMarshal.GetReference(Log2DeBruijn), (IntPtr)(int)((value * 0x07C4ACDDu) >> 27)); // uint|long -> IntPtr cast on 32-bit platforms does expensive overflow checks not needed here } #endif + + /// + /// Fast division with ceiling for numbers. + /// + /// Divident value. + /// Divisor value. + /// Ceiled division result. + public static uint DivideCeil(uint value, uint divisor) => (value + divisor - 1) / divisor; } } diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs index 9f89fd085f..3a136b4103 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs @@ -103,8 +103,8 @@ public void Dispose() ///
public void InitComponents() { - this.McusPerLine = (int)MathF.Ceiling(this.PixelWidth / 8F / this.MaxHorizontalFactor); - this.McusPerColumn = (int)MathF.Ceiling(this.PixelHeight / 8F / this.MaxVerticalFactor); + this.McusPerLine = (int)Numerics.DivideCeil((uint)this.PixelWidth, (uint)this.MaxHorizontalFactor * 8); + this.McusPerColumn = (int)Numerics.DivideCeil((uint)this.PixelHeight, (uint)this.MaxVerticalFactor * 8); for (int i = 0; i < this.ComponentCount; i++) { diff --git a/tests/ImageSharp.Tests/Common/NumericsTests.cs b/tests/ImageSharp.Tests/Common/NumericsTests.cs index 29eae6d488..62819af493 100644 --- a/tests/ImageSharp.Tests/Common/NumericsTests.cs +++ b/tests/ImageSharp.Tests/Common/NumericsTests.cs @@ -34,7 +34,7 @@ public void Log2_ZeroConvention() int expected = 0; int actual = Numerics.Log2(value); - Assert.True(expected == actual, $"Expected: {expected}, Actual: {actual}"); + Assert.Equal(expected, actual); } [Fact] @@ -47,7 +47,7 @@ public void Log2_PowersOfTwo() int expected = i; int actual = Numerics.Log2(value); - Assert.True(expected == actual, $"Expected: {expected}, Actual: {actual}"); + Assert.Equal(expected, actual); } } @@ -66,7 +66,35 @@ public void Log2_RandomValues(int seed, int count) int expected = Log2_ReferenceImplementation(value); int actual = Numerics.Log2(value); - Assert.True(expected == actual, $"Expected: {expected}, Actual: {actual}"); + Assert.Equal(expected, actual); + } + } + + private static uint DivideCeil_ReferenceImplementation(uint value, uint divisor) => (uint)MathF.Ceiling((float)value / divisor); + + [Fact] + public void DivideCeil_DivideZero() + { + uint expected = 0; + uint actual = Numerics.DivideCeil(0, 100); + + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(1, 100)] + public void DivideCeil_RandomValues(int seed, int count) + { + var rng = new Random(seed); + for (int i = 0; i < count; i++) + { + uint value = (uint)rng.Next(); + uint divisor = (uint)rng.Next(); + + uint expected = DivideCeil_ReferenceImplementation(value, divisor); + uint actual = Numerics.DivideCeil(value, divisor); + + Assert.True(expected == actual, $"Expected: {expected}\nActual: {actual}\n{value} / {divisor} = {expected}"); } } } From 269c0735200815b777377f7a5a25d5e1584bff89 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Tue, 13 Jul 2021 18:27:10 +0300 Subject: [PATCH 75/76] Fixed spectral data as image saving test --- .../Formats/Jpg/SpectralJpegTests.cs | 17 ++++++++++------- .../Jpg/Utils/LibJpegTools.SpectralData.cs | 8 -------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs index 0d4881adab..805e19d973 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs @@ -46,23 +46,26 @@ public SpectralJpegTests(ITestOutputHelper output) public static readonly string[] AllTestJpegs = BaselineTestJpegs.Concat(ProgressiveTestJpegs).ToArray(); - [Theory(Skip = "Debug only, enable manually!")] + //[Theory(Skip = "Debug only, enable manually!")] + [Theory] [WithFileCollection(nameof(AllTestJpegs), PixelTypes.Rgba32)] public void Decoder_ParseStream_SaveSpectralResult(TestImageProvider provider) where TPixel : unmanaged, IPixel { - var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder()); - + // Calculating data from ImageSharp byte[] sourceBytes = TestFile.Create(provider.SourceFileOrDescription).Bytes; + var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder()); using var ms = new MemoryStream(sourceBytes); using var bufferedStream = new BufferedReadStream(Configuration.Default, ms); - using Image image = decoder.Decode(bufferedStream, cancellationToken: default); + // internal scan decoder which we substitute to assert spectral correctness + var debugConverter = new DebugSpectralConverter(); + var scanDecoder = new HuffmanScanDecoder(bufferedStream, debugConverter, cancellationToken: default); - // TODO: Fix this - var data = LibJpegTools.SpectralData.LoadFromImageSharpDecoder(decoder); - VerifyJpeg.SaveSpectralImage(provider, data); + // This would parse entire image + decoder.ParseStream(bufferedStream, scanDecoder, cancellationToken: default); + VerifyJpeg.SaveSpectralImage(provider, debugConverter.SpectralData); } [Theory] diff --git a/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.SpectralData.cs b/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.SpectralData.cs index 6ed7c15aed..2d0672f172 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.SpectralData.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.SpectralData.cs @@ -29,14 +29,6 @@ internal SpectralData(LibJpegTools.ComponentData[] components) this.Components = components; } - public static SpectralData LoadFromImageSharpDecoder(JpegDecoderCore decoder) - { - JpegComponent[] srcComponents = decoder.Frame.Components; - LibJpegTools.ComponentData[] destComponents = srcComponents.Select(LibJpegTools.ComponentData.Load).ToArray(); - - return new SpectralData(destComponents); - } - public Image TryCreateRGBSpectralImage() { if (this.ComponentCount != 3) From 190964c9bafa9e04cbe36e2eafeafcfae6b5a6c2 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Tue, 13 Jul 2021 18:31:38 +0300 Subject: [PATCH 76/76] Disabled image saving test --- tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs index 805e19d973..0b819bf13c 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs @@ -46,8 +46,8 @@ public SpectralJpegTests(ITestOutputHelper output) public static readonly string[] AllTestJpegs = BaselineTestJpegs.Concat(ProgressiveTestJpegs).ToArray(); - //[Theory(Skip = "Debug only, enable manually!")] - [Theory] + [Theory(Skip = "Debug only, enable manually!")] + //[Theory] [WithFileCollection(nameof(AllTestJpegs), PixelTypes.Rgba32)] public void Decoder_ParseStream_SaveSpectralResult(TestImageProvider provider) where TPixel : unmanaged, IPixel