From 27269683b9ca3ad95f8e1eb3b3a341cbc152ed40 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 3 Jul 2018 01:14:33 +0200 Subject: [PATCH] simplify + uniformize blockDataRef retrieval --- .../Components/PdfJsFrameComponent.cs | 8 +++ .../Jpeg/PdfJsPort/Components/ScanDecoder.cs | 49 ++++++++++++------- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsFrameComponent.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsFrameComponent.cs index 1a10adf883..7501b0d83c 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsFrameComponent.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsFrameComponent.cs @@ -144,5 +144,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { return 64 * (((this.WidthInBlocks + 1) * row) + col); } + + // TODO: we need consistence in (row, col) VS (col, row) ordering + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ref short GetBlockDataReference(int row, int col) + { + ref Block8x8 blockRef = ref this.GetBlockReference(col, row); + return ref Unsafe.As(ref blockRef); + } } } \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs index 6f88b6e1f2..18cfc6c3fe 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs @@ -179,8 +179,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components for (int k = 0; k < this.componentsLength; k++) { PdfJsFrameComponent component = this.components[k]; - ref short blockDataRef = ref MemoryMarshal.GetReference( - MemoryMarshal.Cast(component.SpectralBlocks.Span)); + ref PdfJsHuffmanTable dcHuffmanTable = ref dcHuffmanTables[component.DCHuffmanTableId]; ref PdfJsHuffmanTable acHuffmanTable = ref acHuffmanTables[component.ACHuffmanTableId]; ref short fastACRef = @@ -203,10 +202,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components int mcuCol = mcu % mcusPerLine; int blockRow = (mcuRow * v) + y; int blockCol = (mcuCol * h) + x; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); + this.DecodeBlockBaseline( component, - ref Unsafe.Add(ref blockDataRef, offset), + blockRow, + blockCol, ref dcHuffmanTable, ref acHuffmanTable, ref fastACRef); @@ -240,8 +240,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components int w = component.WidthInBlocks; int h = component.HeightInBlocks; - ref short blockDataRef = - ref MemoryMarshal.GetReference(MemoryMarshal.Cast(component.SpectralBlocks.Span)); + ref PdfJsHuffmanTable dcHuffmanTable = ref dcHuffmanTables[component.DCHuffmanTableId]; ref PdfJsHuffmanTable acHuffmanTable = ref acHuffmanTables[component.ACHuffmanTableId]; ref short fastACRef = ref MemoryMarshal.GetReference(fastACTables.GetTableSpan(component.ACHuffmanTableId)); @@ -258,10 +257,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components int blockRow = mcu / w; int blockCol = mcu % w; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); + this.DecodeBlockBaseline( component, - ref Unsafe.Add(ref blockDataRef, offset), + blockRow, + blockCol, ref dcHuffmanTable, ref acHuffmanTable, ref fastACRef); @@ -330,7 +330,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components int offset = component.GetBlockBufferOffset(blockRow, blockCol); this.DecodeBlockProgressiveDC( component, - ref Unsafe.Add(ref blockDataRef, offset), + blockRow, + blockCol, ref dcHuffmanTable); } } @@ -362,8 +363,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components int w = component.WidthInBlocks; int h = component.HeightInBlocks; - ref short blockDataRef = - ref MemoryMarshal.GetReference(MemoryMarshal.Cast(component.SpectralBlocks.Span)); + ref PdfJsHuffmanTable dcHuffmanTable = ref dcHuffmanTables[component.DCHuffmanTableId]; ref PdfJsHuffmanTable acHuffmanTable = ref acHuffmanTables[component.ACHuffmanTableId]; ref short fastACRef = ref MemoryMarshal.GetReference(fastACTables.GetTableSpan(component.ACHuffmanTableId)); @@ -380,16 +380,21 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components int blockRow = mcu / w; int blockCol = mcu % w; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); if (this.spectralStart == 0) { - this.DecodeBlockProgressiveDC(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable); + this.DecodeBlockProgressiveDC( + component, + blockRow, + blockCol, + ref dcHuffmanTable); } else { this.DecodeBlockProgressiveAC( - ref Unsafe.Add(ref blockDataRef, offset), + component, + blockRow, + blockCol, ref acHuffmanTable, ref fastACRef); } @@ -406,7 +411,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components private void DecodeBlockBaseline( PdfJsFrameComponent component, - ref short blockDataRef, + int row, + int col, ref PdfJsHuffmanTable dcTable, ref PdfJsHuffmanTable acTable, ref short fastACRef) @@ -419,6 +425,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components JpegThrowHelper.ThrowBadHuffmanCode(); } + ref short blockDataRef = ref component.GetBlockDataReference(row, col); + int diff = t != 0 ? this.ExtendReceive(t) : 0; int dc = component.DcPredictor + diff; component.DcPredictor = dc; @@ -482,7 +490,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components private void DecodeBlockProgressiveDC( PdfJsFrameComponent component, - ref short blockDataRef, + int row, + int col, ref PdfJsHuffmanTable dcTable) { if (this.spectralEnd != 0) @@ -492,6 +501,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components this.CheckBits(); + ref short blockDataRef = ref component.GetBlockDataReference(row, col); + if (this.successiveHigh == 0) { // First scan for DC coefficient, must be first @@ -514,7 +525,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } private void DecodeBlockProgressiveAC( - ref short blockDataRef, + PdfJsFrameComponent component, + int row, + int col, ref PdfJsHuffmanTable acTable, ref short fastACRef) { @@ -523,6 +536,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components JpegThrowHelper.ThrowImageFormatException("Can't merge DC and AC."); } + ref short blockDataRef = ref component.GetBlockDataReference(row, col); + if (this.successiveHigh == 0) { // MCU decoding for AC initial scan (either spectral selection,