From d4fc8a03be221fdab018bf9592ea356f11aeca60 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 2 Jul 2018 22:45:19 +1000 Subject: [PATCH] Move method where it belongs. --- .../Jpeg/PdfJsPort/Components/FastACTables.cs | 45 ++++++++++++++++++- .../Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs | 44 +----------------- 2 files changed, 45 insertions(+), 44 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FastACTables.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FastACTables.cs index 6a11f2805..3e170a92c 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FastACTables.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FastACTables.cs @@ -29,11 +29,54 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components /// The table index. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Span GetTableSpan(int index) + public ReadOnlySpan GetTableSpan(int index) { return this.tables.GetRowSpan(index); } + /// + /// Builds a lookup table for fast AC entropy scan decoding. + /// + /// The table index. + /// The collection of AC Huffman tables. + public void BuildACTableLut(int index, PdfJsHuffmanTables acHuffmanTables) + { + const int FastBits = ScanDecoder.FastBits; + Span fastAC = this.tables.GetRowSpan(index); + ref PdfJsHuffmanTable huffman = ref acHuffmanTables[index]; + + int i; + for (i = 0; i < (1 << FastBits); i++) + { + byte fast = huffman.Lookahead[i]; + fastAC[i] = 0; + if (fast < byte.MaxValue) + { + int rs = huffman.Values[fast]; + int run = (rs >> 4) & 15; + int magbits = rs & 15; + int len = huffman.Sizes[fast]; + + if (magbits > 0 && len + magbits <= FastBits) + { + // Magnitude code followed by receive_extend code + int k = ((i << len) & ((1 << FastBits) - 1)) >> (FastBits - magbits); + int m = 1 << (magbits - 1); + if (k < m) + { + k += (int)((~0U << magbits) + 1); + } + + // if the result is small enough, we can fit it in fastAC table + if (k >= -128 && k <= 127) + { + fastAC[i] = (short)((k * 256) + (run * 16) + (len + magbits)); + } + } + } + } + } + /// public void Dispose() { diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs index fda98e437..cb52fb84b 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs @@ -743,7 +743,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort if (huffmanTableSpec >> 4 != 0) { // Build a table that decodes both magnitude and value of small ACs in one go. - this.BuildFastACTable(huffmanTableSpec & 15); + this.fastACTables.BuildACTableLut(huffmanTableSpec & 15, this.acHuffmanTables); } } } @@ -857,47 +857,5 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort return image; } } - - /// - /// Builds a lookup table for fast AC entropy scan decoding. - /// - /// The table index. - private void BuildFastACTable(int index) - { - const int FastBits = ScanDecoder.FastBits; - Span fastAC = this.fastACTables.GetTableSpan(index); - ref PdfJsHuffmanTable huffman = ref this.acHuffmanTables[index]; - - int i; - for (i = 0; i < (1 << FastBits); i++) - { - byte fast = huffman.Lookahead[i]; - fastAC[i] = 0; - if (fast < byte.MaxValue) - { - int rs = huffman.Values[fast]; - int run = (rs >> 4) & 15; - int magbits = rs & 15; - int len = huffman.Sizes[fast]; - - if (magbits > 0 && len + magbits <= FastBits) - { - // Magnitude code followed by receive_extend code - int k = ((i << len) & ((1 << FastBits) - 1)) >> (FastBits - magbits); - int m = 1 << (magbits - 1); - if (k < m) - { - k += (int)((~0U << magbits) + 1); - } - - // if the result is small enough, we can fit it in fastAC table - if (k >= -128 && k <= 127) - { - fastAC[i] = (short)((k * 256) + (run * 16) + (len + magbits)); - } - } - } - } - } } } \ No newline at end of file