From 3b8f49cc669a69566713cd7227c7392574969e57 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 9 May 2018 01:30:11 +1000 Subject: [PATCH 01/33] Begin porting stb_image --- .../Jpeg/PdfJsPort/Components/FastACTables.cs | 34 + .../Components/FixedInt16Buffer257.cs | 24 + .../Components/PdfJsFrameComponent.cs | 4 +- .../PdfJsPort/Components/PdfJsHuffmanTable.cs | 70 +- .../Components/PdfJsHuffmanTables.cs | 2 +- .../PdfJsPort/Components/PdfJsScanDecoder.cs | 17 +- .../Jpeg/PdfJsPort/Components/ScanDecoder.cs | 628 ++++++++++++++++++ .../Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs | 59 +- 8 files changed, 798 insertions(+), 40 deletions(-) create mode 100644 src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FastACTables.cs create mode 100644 src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt16Buffer257.cs create mode 100644 src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FastACTables.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FastACTables.cs new file mode 100644 index 0000000000..8d37c567ed --- /dev/null +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FastACTables.cs @@ -0,0 +1,34 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using SixLabors.ImageSharp.Memory; + +namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components +{ + /// + /// The collection of tables used for fast AC entropy scan decoding. + /// + internal sealed class FastACTables : IDisposable + { + /// + /// Initializes a new instance of the class. + /// + /// The memory manager used to allocate memory for image processing operations. + public FastACTables(MemoryManager memoryManager) + { + this.Tables = memoryManager.AllocateClean2D(512, 4); + } + + /// + /// Gets the collection of tables. + /// + public Buffer2D Tables { get; } + + /// + public void Dispose() + { + this.Tables?.Dispose(); + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt16Buffer257.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt16Buffer257.cs new file mode 100644 index 0000000000..b304dbf8c2 --- /dev/null +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt16Buffer257.cs @@ -0,0 +1,24 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components +{ + [StructLayout(LayoutKind.Sequential)] + internal unsafe struct FixedInt16Buffer257 + { + public fixed short Data[257]; + + public short this[int idx] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + ref short self = ref Unsafe.As(ref this); + return Unsafe.Add(ref self, idx); + } + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsFrameComponent.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsFrameComponent.cs index 7f50a8529c..f063309eac 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsFrameComponent.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsFrameComponent.cs @@ -36,9 +36,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components public byte Id { get; } /// - /// Gets or sets Pred TODO: What does pred stand for? + /// Gets or sets DC coefficient predictor /// - public int Pred { get; set; } + public int DcPredictor { get; set; } /// /// Gets the horizontal sampling factor. diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs index 875a862638..0541de91b0 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs @@ -27,13 +27,18 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components /// /// Gets the huffman value array /// - public FixedByteBuffer256 HuffVal; + public FixedByteBuffer256 Values; /// /// Gets the lookahead array /// public FixedInt16Buffer256 Lookahead; + /// + /// Gets the sizes array + /// + public FixedInt16Buffer257 Sizes; + /// /// Initializes a new instance of the struct. /// @@ -42,20 +47,18 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components /// The huffman values public PdfJsHuffmanTable(MemoryManager memoryManager, ReadOnlySpan lengths, ReadOnlySpan values) { - const int length = 257; - using (IBuffer huffsize = memoryManager.Allocate(length)) - using (IBuffer huffcode = memoryManager.Allocate(length)) + const int Length = 257; + using (IBuffer huffcode = memoryManager.Allocate(Length)) { - ref short huffsizeRef = ref MemoryMarshal.GetReference(huffsize.Span); ref short huffcodeRef = ref MemoryMarshal.GetReference(huffcode.Span); - GenerateSizeTable(lengths, ref huffsizeRef); - GenerateCodeTable(ref huffsizeRef, ref huffcodeRef, length); + this.GenerateSizeTable(lengths); + this.GenerateCodeTable(ref huffcodeRef, Length); this.GenerateDecoderTables(lengths, ref huffcodeRef); this.GenerateLookaheadTables(lengths, values, ref huffcodeRef); } - fixed (byte* huffValRef = this.HuffVal.Data) + fixed (byte* huffValRef = this.Values.Data) { var huffValSpan = new Span(huffValRef, 256); @@ -67,45 +70,49 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components /// Figure C.1: make table of Huffman code length for each symbol /// /// The code lengths - /// The huffman size span ref - private static void GenerateSizeTable(ReadOnlySpan lengths, ref short huffsizeRef) + private void GenerateSizeTable(ReadOnlySpan lengths) { - short index = 0; - for (short l = 1; l <= 16; l++) + fixed (short* sizesRef = this.Sizes.Data) { - byte i = lengths[l]; - for (short j = 0; j < i; j++) + short index = 0; + for (short l = 1; l <= 16; l++) { - Unsafe.Add(ref huffsizeRef, index) = l; - index++; + byte i = lengths[l]; + for (short j = 0; j < i; j++) + { + sizesRef[index] = l; + index++; + } } - } - Unsafe.Add(ref huffsizeRef, index) = 0; + sizesRef[index] = 0; + } } /// /// Figure C.2: generate the codes themselves /// - /// The huffman size span ref /// The huffman code span ref /// The length of the huffsize span - private static void GenerateCodeTable(ref short huffsizeRef, ref short huffcodeRef, int length) + private void GenerateCodeTable(ref short huffcodeRef, int length) { - short k = 0; - short si = huffsizeRef; - short code = 0; - for (short i = 0; i < length; i++) + fixed (short* sizesRef = this.Sizes.Data) { - while (Unsafe.Add(ref huffsizeRef, k) == si) + short k = 0; + short si = sizesRef[0]; + short code = 0; + for (short i = 0; i < length; i++) { - Unsafe.Add(ref huffcodeRef, k) = code; - code++; - k++; - } + while (sizesRef[k] == si) + { + Unsafe.Add(ref huffcodeRef, k) = code; + code++; + k++; + } - code <<= 1; - si++; + code <<= 1; + si++; + } } } @@ -148,6 +155,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components /// The huffman code span ref private void GenerateLookaheadTables(ReadOnlySpan lengths, ReadOnlySpan huffval, ref short huffcodeRef) { + // TODO: Rewrite this to match stb_Image // TODO: This generation code matches the libJpeg code but the lookahead table is not actually used yet. // To use it we need to implement fast lookup path in PdfJsScanDecoder.DecodeHuffman // This should yield much faster scan decoding as usually, more than 95% of the Huffman codes diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTables.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTables.cs index 3a559bb864..5cbde2b88c 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTables.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTables.cs @@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components /// Gets or sets the table at the given index. /// /// The index - /// The + /// The public ref PdfJsHuffmanTable this[int index] { [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs index c6b14d6fb0..62c8f984f0 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs @@ -107,7 +107,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components for (int i = 0; i < components.Length; i++) { PdfJsFrameComponent c = components[i]; - c.Pred = 0; + c.DcPredictor = 0; } this.eobrun = 0; @@ -552,7 +552,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } int j = tree.ValOffset[i]; - value = tree.HuffVal[(j + code) & 0xFF]; + value = tree.Values[(j + code) & 0xFF]; return true; } @@ -618,7 +618,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } } - Unsafe.Add(ref blockDataRef, offset) = (short)(component.Pred += diff); + Unsafe.Add(ref blockDataRef, offset) = (short)(component.DcPredictor += diff); int k = 1; while (k < 64) @@ -673,7 +673,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } } - Unsafe.Add(ref blockDataRef, offset) = (short)(component.Pred += diff << this.successiveState); + Unsafe.Add(ref blockDataRef, offset) = (short)(component.DcPredictor += diff << this.successiveState); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -860,5 +860,14 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } } } + + private void Reset() + { + // Reset + // TODO: I do not understand why these values are reset? We should surely be tracking the bits across mcu's? + this.bitsCount = 0; + this.bitsData = 0; + this.unexpectedMarkerReached = false; + } } } \ 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 new file mode 100644 index 0000000000..af7233bfe8 --- /dev/null +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs @@ -0,0 +1,628 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +using SixLabors.ImageSharp.Formats.Jpeg.Common; +using SixLabors.ImageSharp.Memory; + +namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components +{ + internal class ScanDecoder + { + public const int FastBits = 9; + + // bmask[n] = (1 << n) - 1 + private static readonly uint[] stbi__bmask = { 0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535 }; + + // bias[n] = (-1 << n) + 1 + private static readonly int[] stbi__jbias = { 0, -1, -3, -7, -15, -31, -63, -127, -255, -511, -1023, -2047, -4095, -8191, -16383, -32767 }; + + private readonly DoubleBufferedStreamReader stream; + private readonly PdfJsFrameComponent[] components; + private readonly ZigZag dctZigZag; + private int codeBits; + private uint codeBuffer; + private bool nomore; + private byte marker; + + private int todo; + private int restartInterval; + private int componentIndex; + private int componentsLength; + private int eobrun; + private int spectralStart; + private int spectralEnd; + private int successiveHigh; + private int successiveLow; + + /// + /// Initializes a new instance of the class. + /// + /// The input stream + /// The scan components + /// The component index within the array + /// 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 + public ScanDecoder( + DoubleBufferedStreamReader stream, + PdfJsFrameComponent[] components, + int componentIndex, + int componentsLength, + int restartInterval, + int spectralStart, + int spectralEnd, + int successiveHigh, + int successiveLow) + { + this.dctZigZag = ZigZag.CreateUnzigTable(); + this.stream = stream; + this.components = components; + this.marker = PdfJsJpegConstants.Markers.Prefix; + this.componentIndex = componentIndex; + this.componentsLength = componentsLength; + this.restartInterval = restartInterval; + this.spectralStart = spectralStart; + this.spectralEnd = spectralEnd; + this.successiveHigh = successiveHigh; + this.successiveLow = successiveLow; + } + + /// + /// Decodes the entropy coded data. + /// + /// The image frame. + /// The DC Huffman tables. + /// The AC Huffman tables. + /// The fast AC decoding tables. + /// The + public int ParseEntropyCodedData( + PdfJsFrame frame, + PdfJsHuffmanTables dcHuffmanTables, + PdfJsHuffmanTables acHuffmanTables, + FastACTables fastACTables) + { + this.Reset(); + + if (!frame.Progressive) + { + if (this.componentsLength == 1) + { + int i, j; + int n = this.componentIndex; + PdfJsFrameComponent component = this.components[n]; + + // Non-interleaved data, we just need to process one block at a time, + // in trivial scanline order + // number of blocks to do just depends on how many actual "pixels" this + // component has, independent of interleaved MCU blocking and such + 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]; + Span fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); + + int mcu = 0; + for (j = 0; j < h; j++) + { + for (i = 0; i < w; i++) + { + int blockRow = mcu / w; + int blockCol = mcu % w; + int offset = component.GetBlockBufferOffset(blockRow, blockCol); + this.DecodeBlock(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable, ref acHuffmanTable, fastAC); + mcu++; + } + } + } + } + + return 1; + } + + private int DecodeBlock( + PdfJsFrameComponent component, + ref short blockDataRef, + ref PdfJsHuffmanTable dcTable, + ref PdfJsHuffmanTable acTable, + Span fac) + { + if (this.codeBits < 16) + { + this.GrowBufferUnsafe(); + } + + int t = this.DecodeHuffman(ref dcTable); + + if (t < 0) + { + throw new ImageFormatException("Bad Huffman code"); + } + + int diff = t > 0 ? this.ExtendReceive(t) : 0; + int dc = component.DcPredictor + diff; + component.DcPredictor = dc; + blockDataRef = (short)dc; + + // Decode AC Components, See Jpeg Spec + int k = 1; + do + { + int zig; + int s; + + this.CheckBits(); + int c = this.PeekBits(); + int r = fac[c]; + + if (r > 0) + { + // Fast AC path + k += (r >> 4) & 15; // Run + s = r & 15; // Combined Length + this.codeBuffer <<= s; + this.codeBits -= s; + + // Decode into unzigzag location + zig = this.dctZigZag[k++]; + Unsafe.Add(ref blockDataRef, zig) = (short)(r >> 8); + } + else + { + int rs = this.DecodeHuffman(ref acTable); + + if (rs < 0) + { + throw new ImageFormatException("Bad Huffman code"); + } + + s = rs & 15; + r = rs >> 4; + + if (s == 0) + { + if (rs != 0xF0) + { + break; // End block + } + + k += 16; + } + else + { + k += r; + + // Decode into unzigzag location + zig = this.dctZigZag[k++]; + Unsafe.Add(ref blockDataRef, zig) = (short)this.ExtendReceive(s); + } + } + } while (k < 64); + + return 1; + } + + private int DecodeBlockProgressiveDC( + PdfJsFrameComponent component, + ref short blockDataRef, + ref PdfJsHuffmanTable dcTable) + { + if (this.spectralEnd != 0) + { + throw new ImageFormatException("Can't merge DC and AC."); + } + + this.CheckBits(); + + if (this.successiveHigh == 0) + { + // First scan for DC coefficient, must be first + int t = this.DecodeHuffman(ref dcTable); + int diff = t > 0 ? this.ExtendReceive(t) : 0; + + int dc = component.DcPredictor + diff; + component.DcPredictor = dc; + + blockDataRef = (short)(dc << this.successiveLow); + } + else + { + // Refinement scan for DC coefficient + if (this.GetBit() > 0) + { + blockDataRef += (short)(1 << this.successiveLow); + } + } + + return 1; + } + + private int DecodeBlockProgressiveAC( + PdfJsFrameComponent component, + ref short blockDataRef, + ref PdfJsHuffmanTable acTable, + Span fac) + { + int k; + + if (this.spectralStart == 0) + { + throw new ImageFormatException("Can't merge DC and AC."); + } + + if (this.successiveHigh == 0) + { + int shift = this.successiveLow; + + if (this.eobrun > 0) + { + this.eobrun--; + return 1; + } + + k = this.spectralStart; + do + { + int zig; + int s; + + this.CheckBits(); + int c = this.PeekBits(); + int r = fac[c]; + + if (r > 0) + { + // Fast AC path + k += (r >> 4) & 15; // Run + s = r & 15; // Combined length + this.codeBuffer <<= s; + this.codeBits -= s; + + // Decode into unzigzag location + zig = this.dctZigZag[k++]; + Unsafe.Add(ref blockDataRef, zig) = (short)((r >> 8) << shift); + } + else + { + int rs = this.DecodeHuffman(ref acTable); + + if (rs < 0) + { + throw new ImageFormatException("Bad Huffman code."); + } + + s = rs & 15; + r = rs >> 4; + + if (s == 0) + { + if (r < 15) + { + this.eobrun = 1 << r; + if (r > 0) + { + this.eobrun += this.GetBits(r); + } + + this.eobrun--; + break; + } + + k += 16; + } + else + { + k += r; + zig = this.dctZigZag[k++]; + Unsafe.Add(ref blockDataRef, zig) = (short)(this.ExtendReceive(s) << shift); + } + } + } + while (k <= this.spectralEnd); + } + else + { + // Refinement scan for these AC coefficients + short bit = (short)(1 << this.successiveLow); + + if (this.eobrun > 0) + { + this.eobrun--; + for (k = this.spectralStart; k < this.spectralEnd; k++) + { + ref short p = ref Unsafe.Add(ref blockDataRef, this.dctZigZag[k]); + if (p != 0) + { + if (this.GetBit() > 0) + { + if ((p & bit) == 0) + { + if (p > 0) + { + p += bit; + } + else + { + p -= bit; + } + } + } + } + } + } + else + { + k = this.spectralStart; + do + { + int rs = this.DecodeHuffman(ref acTable); + if (rs < 0) + { + throw new ImageFormatException("Bad Huffman code."); + } + + int s = rs & 15; + int r = rs >> 4; + + if (s == 0) + { + // r=15 s=0 should write 16 0s, so we just do + // a run of 15 0s and then write s (which is 0), + // so we don't have to do anything special here + if (r < 15) + { + this.eobrun = (1 << r) - 1; + + if (r > 0) + { + this.eobrun += this.GetBits(r); + } + + r = 64; // Force end of block + } + } + else + { + if (s != 1) + { + throw new ImageFormatException("Bad Huffman code."); + } + + // Sign bit + if (this.GetBit() > 0) + { + s = bit; + } + else + { + s -= bit; + } + } + + // Advance by r + while (k <= this.spectralEnd) + { + ref short p = ref Unsafe.Add(ref blockDataRef, this.dctZigZag[k++]); + if (p != 0) + { + if (this.GetBit() > 0) + { + if ((p & bit) == 0) + { + if (p > 0) + { + p += bit; + } + else + { + p -= bit; + } + } + } + } + else + { + if (r == 0) + { + p = (short)s; + break; + } + + r--; + } + } + } + while (k <= this.spectralEnd); + } + } + + return 1; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private int GetBits(int n) + { + if (this.codeBits < n) + { + this.GrowBufferUnsafe(); + } + + uint k = this.Lrot(this.codeBuffer, n); + this.codeBuffer = k & ~stbi__bmask[n]; + k &= stbi__bmask[n]; + this.codeBits -= n; + return (int)k; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private int GetBit() + { + if (this.codeBits < 1) + { + this.GrowBufferUnsafe(); + } + + uint k = this.codeBuffer; + this.codeBuffer <<= 1; + this.codeBits--; + + return (int)(k & 0x80000000); + } + + private void GrowBufferUnsafe() + { + do + { + // TODO: EOF + uint b = (uint)(this.nomore ? 0 : this.stream.ReadByte()); + if (b == PdfJsJpegConstants.Markers.Prefix) + { + long position = this.stream.Position - 1; + int c = this.stream.ReadByte(); + while (c == PdfJsJpegConstants.Markers.Prefix) + { + if (c != 0) + { + this.marker = (byte)c; + this.nomore = true; + this.stream.Position = position; + return; + } + } + } + + this.codeBuffer |= b << (24 - this.codeBits); + this.codeBits += 8; + } + while (this.codeBits <= 24); + } + + private int DecodeHuffman(ref PdfJsHuffmanTable table) + { + this.CheckBits(); + + // Look at the top FastBits and determine what symbol ID it is, + // if the code is <= FastBits. + int c = this.PeekBits(); + int k = table.Lookahead[c]; + if (k < byte.MaxValue) + { + int s = table.Sizes[k]; + if (s > this.codeBits) + { + return -1; + } + + this.codeBuffer <<= s; + this.codeBits -= s; + return table.Values[k]; + } + + // Naive test is to shift the code_buffer down so k bits are + // valid, then test against MaxCode. To speed this up, we've + // preshifted maxcode left so that it has (16-k) 0s at the + // end; in other words, regardless of the number of bits, it + // wants to be compared against something shifted to have 16; + // that way we don't need to shift inside the loop. + uint temp = this.codeBuffer >> 16; + for (k = FastBits + 1; ; ++k) + { + if (temp < table.MaxCode[k]) + { + break; + } + } + + if (k == 17) + { + // Error! code not found + this.codeBits -= 16; + return -1; + } + + if (k > this.codeBits) + { + return -1; + } + + // Convert the huffman code to the symbol id + c = (int)((this.codeBuffer >> (32 - k)) & stbi__bmask[k]) + table.ValOffset[k]; + + // Convert the id to a symbol + this.codeBits -= k; + this.codeBuffer <<= k; + return table.Values[c]; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private int ExtendReceive(int n) + { + if (this.codeBits < n) + { + this.GrowBufferUnsafe(); + } + + int sgn = (int)(this.codeBuffer >> 31); + uint k = this.Lrot(this.codeBuffer, n); + this.codeBuffer = k & ~stbi__bmask[n]; + k &= stbi__bmask[n]; + this.codeBits -= n; + return (int)(k + (stbi__jbias[n] & ~sgn)); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void CheckBits() + { + if (this.codeBuffer < 16) + { + this.GrowBufferUnsafe(); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private int PeekBits() + { + return (int)(this.codeBuffer >> ((32 - FastBits) & ((1 << FastBits) - 1))); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private uint Lrot(uint x, int y) + { + return (x << y) | (x >> (32 - y)); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private bool IsRestartMarker(byte x) + { + return x >= PdfJsJpegConstants.Markers.RST0 && x <= PdfJsJpegConstants.Markers.RST7; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void Reset() + { + this.codeBits = 0; + this.codeBuffer = 0; + this.nomore = false; + + for (int i = 0; i < this.components.Length; i++) + { + PdfJsFrameComponent c = this.components[i]; + c.DcPredictor = 0; + } + + this.marker = PdfJsJpegConstants.Markers.Prefix; + this.todo = this.restartInterval > 0 ? this.restartInterval : 0x7FFFFFFF; + this.eobrun = 0; + + // No more than 1<<31 MCUs if no restartInterval? that's plenty safe, + // since we don't even allow 1<<30 pixels + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs index df803a9202..18a444c5bd 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs @@ -57,6 +57,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort /// private PdfJsHuffmanTables acHuffmanTables; + /// + /// The fast AC tables used for entropy decoding + /// + private FastACTables fastACTables; + /// /// The reset interval determined by RST markers /// @@ -228,6 +233,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort this.QuantizationTables = new Block8x8F[4]; this.dcHuffmanTables = new PdfJsHuffmanTables(); this.acHuffmanTables = new PdfJsHuffmanTables(); + this.fastACTables = new FastACTables(this.configuration.MemoryManager); } while (fileMarker.Marker != PdfJsJpegConstants.Markers.EOI) @@ -341,12 +347,14 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort { this.InputStream?.Dispose(); this.Frame?.Dispose(); + this.fastACTables?.Dispose(); // Set large fields to null. this.InputStream = null; this.Frame = null; this.dcHuffmanTables = null; this.acHuffmanTables = null; + this.fastACTables = null; } /// @@ -714,11 +722,20 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort i += 17 + codeLengthSum; + int tableType = huffmanTableSpec >> 4; + int tableIndex = huffmanTableSpec & 15; + this.BuildHuffmanTable( - huffmanTableSpec >> 4 == 0 ? this.dcHuffmanTables : this.acHuffmanTables, - huffmanTableSpec & 15, + tableType == 0 ? this.dcHuffmanTables : this.acHuffmanTables, + tableIndex, codeLengths.Span, huffmanValues.Span); + + if (tableType != 0) + { + // Build a table that decodes both magnitude and value of small ACs in one go. + this.BuildFastACTable(tableIndex); + } } } } @@ -829,5 +846,43 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort return image; } } + + private void BuildFastACTable(int index) + { + const int FastBits = ScanDecoder.FastBits; + Span fastac = this.fastACTables.Tables.GetRowSpan(index); + ref PdfJsHuffmanTable huffman = ref this.acHuffmanTables[index]; + + int i; + for (i = 0; i < (1 << FastBits); i++) + { + short fast = huffman.Lookahead[i]; + fastac[i] = 0; + if (fast < 255) + { + 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 From 36317714a81a7df90366b786e12204ce7d686d19 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 9 May 2018 10:57:43 +1000 Subject: [PATCH 02/33] Minor cleanup --- .../Jpeg/PdfJsPort/Components/ScanDecoder.cs | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs index af7233bfe8..e9f91ef06c 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs @@ -94,9 +94,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { if (this.componentsLength == 1) { - int i, j; - int n = this.componentIndex; - PdfJsFrameComponent component = this.components[n]; + PdfJsFrameComponent component = this.components[this.componentIndex]; // Non-interleaved data, we just need to process one block at a time, // in trivial scanline order @@ -110,18 +108,42 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components Span fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); int mcu = 0; - for (j = 0; j < h; j++) + for (int j = 0; j < h; j++) { - for (i = 0; i < w; i++) + for (int i = 0; i < w; i++) { int blockRow = mcu / w; int blockCol = mcu % w; int offset = component.GetBlockBufferOffset(blockRow, blockCol); this.DecodeBlock(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable, ref acHuffmanTable, fastAC); mcu++; + + // Every data block is an MCU, so countdown the restart interval + if (this.todo-- <= 0) + { + if (this.codeBits < 24) + { + this.GrowBufferUnsafe(); + } + + // If it's NOT a restart, then just bail, so we get corrupt data + // rather than no data + if (!this.IsRestartMarker(this.marker)) + { + return 1; + } + + this.Reset(); + } } } } + else + { + // Interleaved + int i, j, k, x, y; + + } } return 1; From 71f8d6c322059f0b4016cd5a349ad1afe632abb5 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 10 May 2018 00:47:16 +1000 Subject: [PATCH 03/33] Wire up huffman tables. (doesn't work) --- .../Components/FixedByteBuffer512.cs | 24 ++++ .../PdfJsPort/Components/PdfJsHuffmanTable.cs | 126 +++++++++++++----- .../Jpeg/PdfJsPort/Components/ScanDecoder.cs | 87 +++++++++--- .../Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs | 50 ++++--- 4 files changed, 219 insertions(+), 68 deletions(-) create mode 100644 src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedByteBuffer512.cs diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedByteBuffer512.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedByteBuffer512.cs new file mode 100644 index 0000000000..c509903c98 --- /dev/null +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedByteBuffer512.cs @@ -0,0 +1,24 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components +{ + [StructLayout(LayoutKind.Sequential)] + internal unsafe struct FixedByteBuffer512 + { + public fixed byte Data[1 << ScanDecoder.FastBits]; + + public byte this[int idx] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + ref byte self = ref Unsafe.As(ref this); + return Unsafe.Add(ref self, idx); + } + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs index 0541de91b0..1cc342f5ac 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs @@ -32,7 +32,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components /// /// Gets the lookahead array /// - public FixedInt16Buffer256 Lookahead; + public FixedByteBuffer512 Lookahead; /// /// Gets the sizes array @@ -43,19 +43,76 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components /// Initializes a new instance of the struct. /// /// The to use for buffer allocations. - /// The code lengths + /// The code lengths /// The huffman values - public PdfJsHuffmanTable(MemoryManager memoryManager, ReadOnlySpan lengths, ReadOnlySpan values) + public PdfJsHuffmanTable(MemoryManager memoryManager, ReadOnlySpan count, ReadOnlySpan values) { const int Length = 257; using (IBuffer huffcode = memoryManager.Allocate(Length)) { + // Span codes = huffcode.Span; ref short huffcodeRef = ref MemoryMarshal.GetReference(huffcode.Span); - this.GenerateSizeTable(lengths); - this.GenerateCodeTable(ref huffcodeRef, Length); - this.GenerateDecoderTables(lengths, ref huffcodeRef); - this.GenerateLookaheadTables(lengths, values, ref huffcodeRef); + this.GenerateSizeTable(count); + + //int k = 0; + //fixed (short* sizesRef = this.Sizes.Data) + //fixed (short* deltaRef = this.ValOffset.Data) + //fixed (long* maxcodeRef = this.MaxCode.Data) + //{ + // uint code = 0; + // int j; + // for (j = 1; j <= 16; j++) + // { + // // Compute delta to add to code to compute symbol id. + // deltaRef[j] = (short)(k - code); + // if (sizesRef[k] == j) + // { + // while (sizesRef[k] == j) + // { + // codes[k++] = (short)code++; + + // // Unsafe.Add(ref huffcodeRef, k++) = (short)code++; + + // // TODO: Throw if invalid? + // } + // } + + // // Compute largest code + 1 for this size. preshifted as neeed later. + // maxcodeRef[j] = code << (16 - j); + // code <<= 1; + // } + + // maxcodeRef[j] = 0xFFFFFFFF; + //} + + //fixed (short* lookaheadRef = this.Lookahead.Data) + //{ + // const int FastBits = ScanDecoder.FastBits; + // var fast = new Span(lookaheadRef, 1 << FastBits); + // fast.Fill(255); // Flag for non-accelerated + + // fixed (short* sizesRef = this.Sizes.Data) + // { + // for (int i = 0; i < k; i++) + // { + // int s = sizesRef[i]; + // if (s <= ScanDecoder.FastBits) + // { + // int c = codes[i] << (FastBits - s); + // int m = 1 << (FastBits - s); + // for (int j = 0; j < m; j++) + // { + // fast[c + j] = (byte)i; + // } + // } + // } + // } + //} + + this.GenerateCodeTable(ref huffcodeRef, Length, out int k); + this.GenerateDecoderTables(count, ref huffcodeRef); + this.GenerateLookaheadTables(count, values, ref huffcodeRef, k); } fixed (byte* huffValRef = this.Values.Data) @@ -74,18 +131,18 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { fixed (short* sizesRef = this.Sizes.Data) { - short index = 0; - for (short l = 1; l <= 16; l++) + short k = 0; + for (short i = 1; i < 17; i++) { - byte i = lengths[l]; - for (short j = 0; j < i; j++) + byte l = lengths[i]; + for (short j = 0; j < l; j++) { - sizesRef[index] = l; - index++; + sizesRef[k] = i; + k++; } } - sizesRef[index] = 0; + sizesRef[k] = 0; } } @@ -94,11 +151,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components /// /// The huffman code span ref /// The length of the huffsize span - private void GenerateCodeTable(ref short huffcodeRef, int length) + /// The length of any valid codes + private void GenerateCodeTable(ref short huffcodeRef, int length, out int k) { fixed (short* sizesRef = this.Sizes.Data) { - short k = 0; + k = 0; short si = sizesRef[0]; short code = 0; for (short i = 0; i < length; i++) @@ -134,7 +192,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components // valOffsetRef[l] = huffcodeRef[] index of 1st symbol of code length i, minus the minimum code of length i valOffsetRef[i] = (short)(bitcount - Unsafe.Add(ref huffcodeRef, bitcount)); bitcount += lengths[i]; - maxcodeRef[i] = Unsafe.Add(ref huffcodeRef, bitcount - 1); // maximum code of length i + maxcodeRef[i] = Unsafe.Add(ref huffcodeRef, bitcount - 1) << (16 - i); // maximum code of length i preshifted for faster reading later } else { @@ -143,41 +201,43 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } valOffsetRef[17] = 0; - maxcodeRef[17] = 0xFFFFFL; + maxcodeRef[17] = 0xFFFFFFFFL; } } /// - /// Generates lookup tables to speed up decoding + /// Generates non-spec lookup tables to speed up decoding /// /// The code lengths /// The huffman value array /// The huffman code span ref - private void GenerateLookaheadTables(ReadOnlySpan lengths, ReadOnlySpan huffval, ref short huffcodeRef) + /// The lengths of any valid codes + private void GenerateLookaheadTables(ReadOnlySpan lengths, ReadOnlySpan huffval, ref short huffcodeRef, int k) { // TODO: Rewrite this to match stb_Image // TODO: This generation code matches the libJpeg code but the lookahead table is not actually used yet. // To use it we need to implement fast lookup path in PdfJsScanDecoder.DecodeHuffman // This should yield much faster scan decoding as usually, more than 95% of the Huffman codes // will be 8 or fewer bits long and can be handled without looping. - fixed (short* lookaheadRef = this.Lookahead.Data) + fixed (byte* lookaheadRef = this.Lookahead.Data) { - var lookaheadSpan = new Span(lookaheadRef, 256); - - lookaheadSpan.Fill(2034); // 9 << 8; + const int FastBits = ScanDecoder.FastBits; + var lookaheadSpan = new Span(lookaheadRef, 1 << ScanDecoder.FastBits); - int p = 0; - for (int l = 1; l <= 8; l++) + lookaheadSpan.Fill(255); // Flag for non-accelerated + fixed (short* sizesRef = this.Sizes.Data) { - for (int i = 1; i <= lengths[l]; i++, p++) + for (int i = 0; i < k; ++i) { - // l = current code's length, p = its index in huffcode[] & huffval[]. - // Generate left-justified code followed by all possible bit sequences - int lookBits = Unsafe.Add(ref huffcodeRef, p) << (8 - l); - for (int ctr = 1 << (8 - l); ctr > 0; ctr--) + int s = sizesRef[i]; + if (s <= ScanDecoder.FastBits) { - lookaheadRef[lookBits] = (short)((l << 8) | huffval[p]); - lookBits++; + int c = Unsafe.Add(ref huffcodeRef, i) << (FastBits - s); + int m = 1 << (FastBits - s); + for (int j = 0; j < m; ++j) + { + lookaheadRef[c + j] = (byte)i; + } } } } diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs index e9f91ef06c..217b3cb62b 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs @@ -15,10 +15,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components public const int FastBits = 9; // bmask[n] = (1 << n) - 1 - private static readonly uint[] stbi__bmask = { 0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535 }; + private static readonly uint[] Bmask = { 0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535 }; // bias[n] = (-1 << n) + 1 - private static readonly int[] stbi__jbias = { 0, -1, -3, -7, -15, -31, -63, -127, -255, -511, -1023, -2047, -4095, -8191, -16383, -32767 }; + private static readonly int[] Bias = { 0, -1, -3, -7, -15, -31, -63, -127, -255, -511, -1023, -2047, -4095, -8191, -16383, -32767 }; private readonly DoubleBufferedStreamReader stream; private readonly PdfJsFrameComponent[] components; @@ -141,8 +141,61 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components else { // Interleaved - int i, j, k, x, y; + int mcu = 0; + int mcusPerColumn = frame.McusPerColumn; + int mcusPerLine = frame.McusPerLine; + for (int j = 0; j < mcusPerColumn; j++) + { + for (int i = 0; i < mcusPerLine; i++) + { + // Scan an interleaved mcu... process components in order + 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]; + Span fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); + int h = component.HorizontalSamplingFactor; + int v = component.VerticalSamplingFactor; + + // Scan out an mcu's worth of this component; that's just determined + // by the basic H and V specified for the component + for (int y = 0; y < v; y++) + { + for (int x = 0; x < h; x++) + { + int mcuRow = mcu / mcusPerLine; + int mcuCol = mcu % mcusPerLine; + int blockRow = (mcuRow * v) + y; + int blockCol = (mcuCol * h) + x; + int offset = component.GetBlockBufferOffset(blockRow, blockCol); + this.DecodeBlock(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable, ref acHuffmanTable, fastAC); + } + } + } + + // After all interleaved components, that's an interleaved MCU, + // so now count down the restart interval + mcu++; + if (this.todo-- <= 0) + { + if (this.codeBits < 24) + { + this.GrowBufferUnsafe(); + } + + // If it's NOT a restart, then just bail, so we get corrupt data + // rather than no data + if (!this.IsRestartMarker(this.marker)) + { + return 1; + } + this.Reset(); + } + } + } } } @@ -156,11 +209,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components ref PdfJsHuffmanTable acTable, Span fac) { - if (this.codeBits < 16) - { - this.GrowBufferUnsafe(); - } - + this.CheckBits(); int t = this.DecodeHuffman(ref dcTable); if (t < 0) @@ -477,8 +526,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } uint k = this.Lrot(this.codeBuffer, n); - this.codeBuffer = k & ~stbi__bmask[n]; - k &= stbi__bmask[n]; + this.codeBuffer = k & ~Bmask[n]; + k &= Bmask[n]; this.codeBits -= n; return (int)k; } @@ -503,7 +552,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components do { // TODO: EOF - uint b = (uint)(this.nomore ? 0 : this.stream.ReadByte()); + int b = this.nomore ? 0 : this.stream.ReadByte(); if (b == PdfJsJpegConstants.Markers.Prefix) { long position = this.stream.Position - 1; @@ -514,13 +563,17 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { this.marker = (byte)c; this.nomore = true; - this.stream.Position = position; + if (!this.IsRestartMarker(this.marker)) + { + this.stream.Position = position; + } + return; } } } - this.codeBuffer |= b << (24 - this.codeBits); + this.codeBuffer |= (uint)b << (24 - this.codeBits); this.codeBits += 8; } while (this.codeBits <= 24); @@ -575,7 +628,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } // Convert the huffman code to the symbol id - c = (int)((this.codeBuffer >> (32 - k)) & stbi__bmask[k]) + table.ValOffset[k]; + c = (int)((this.codeBuffer >> (32 - k)) & Bmask[k]) + table.ValOffset[k]; // Convert the id to a symbol this.codeBits -= k; @@ -593,10 +646,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components int sgn = (int)(this.codeBuffer >> 31); uint k = this.Lrot(this.codeBuffer, n); - this.codeBuffer = k & ~stbi__bmask[n]; - k &= stbi__bmask[n]; + this.codeBuffer = k & ~Bmask[n]; + k &= Bmask[n]; this.codeBits -= n; - return (int)(k + (stbi__jbias[n] & ~sgn)); + return (int)(k + (Bias[n] & ~sgn)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs index 18a444c5bd..5bf4ab5aa4 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs @@ -731,10 +731,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort codeLengths.Span, huffmanValues.Span); - if (tableType != 0) + if (huffmanTableSpec >> 4 != 0) { // Build a table that decodes both magnitude and value of small ACs in one go. - this.BuildFastACTable(tableIndex); + this.BuildFastACTable(huffmanTableSpec & 15); } } } @@ -794,21 +794,35 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort int spectralStart = this.temp[0]; int spectralEnd = this.temp[1]; int successiveApproximation = this.temp[2]; - var scanDecoder = default(PdfJsScanDecoder); - - scanDecoder.DecodeScan( - this.Frame, - this.InputStream, - this.dcHuffmanTables, - this.acHuffmanTables, - this.Frame.Components, - componentIndex, - selectorsCount, - this.resetInterval, - spectralStart, - spectralEnd, - successiveApproximation >> 4, - successiveApproximation & 15); + + var sd = new ScanDecoder( + this.InputStream, + this.Frame.Components, + componentIndex, + selectorsCount, + this.resetInterval, + spectralStart, + spectralEnd, + successiveApproximation >> 4, + successiveApproximation & 15); + + sd.ParseEntropyCodedData(this.Frame, this.dcHuffmanTables, this.acHuffmanTables, this.fastACTables); + + //var scanDecoder = default(PdfJsScanDecoder); + + //scanDecoder.DecodeScan( + // this.Frame, + // this.InputStream, + // this.dcHuffmanTables, + // this.acHuffmanTables, + // this.Frame.Components, + // componentIndex, + // selectorsCount, + // this.resetInterval, + // spectralStart, + // spectralEnd, + // successiveApproximation >> 4, + // successiveApproximation & 15); } /// @@ -856,7 +870,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort int i; for (i = 0; i < (1 << FastBits); i++) { - short fast = huffman.Lookahead[i]; + byte fast = huffman.Lookahead[i]; fastac[i] = 0; if (fast < 255) { From fcc6d531f2cf2db46e21928f9aef684fc7a01a4a Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 11 May 2018 11:32:01 +1000 Subject: [PATCH 04/33] Meh --- .../Components/FixedByteBuffer257.cs | 24 ++++ .../PdfJsPort/Components/PdfJsHuffmanTable.cs | 120 +++++++++--------- .../PdfJsPort/Components/PdfJsScanDecoder.cs | 9 -- .../Jpeg/PdfJsPort/Components/ScanDecoder.cs | 2 +- 4 files changed, 85 insertions(+), 70 deletions(-) create mode 100644 src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedByteBuffer257.cs diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedByteBuffer257.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedByteBuffer257.cs new file mode 100644 index 0000000000..3015243168 --- /dev/null +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedByteBuffer257.cs @@ -0,0 +1,24 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components +{ + [StructLayout(LayoutKind.Sequential)] + internal unsafe struct FixedByteBuffer257 + { + public fixed byte Data[257]; + + public byte this[int idx] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + ref byte self = ref Unsafe.As(ref this); + return Unsafe.Add(ref self, idx); + } + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs index 1cc342f5ac..a63573030f 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs @@ -50,69 +50,69 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components const int Length = 257; using (IBuffer huffcode = memoryManager.Allocate(Length)) { - // Span codes = huffcode.Span; + Span codes = huffcode.Span; ref short huffcodeRef = ref MemoryMarshal.GetReference(huffcode.Span); this.GenerateSizeTable(count); - //int k = 0; - //fixed (short* sizesRef = this.Sizes.Data) - //fixed (short* deltaRef = this.ValOffset.Data) - //fixed (long* maxcodeRef = this.MaxCode.Data) - //{ - // uint code = 0; - // int j; - // for (j = 1; j <= 16; j++) - // { - // // Compute delta to add to code to compute symbol id. - // deltaRef[j] = (short)(k - code); - // if (sizesRef[k] == j) - // { - // while (sizesRef[k] == j) - // { - // codes[k++] = (short)code++; - - // // Unsafe.Add(ref huffcodeRef, k++) = (short)code++; - - // // TODO: Throw if invalid? - // } - // } - - // // Compute largest code + 1 for this size. preshifted as neeed later. - // maxcodeRef[j] = code << (16 - j); - // code <<= 1; - // } - - // maxcodeRef[j] = 0xFFFFFFFF; - //} - - //fixed (short* lookaheadRef = this.Lookahead.Data) - //{ - // const int FastBits = ScanDecoder.FastBits; - // var fast = new Span(lookaheadRef, 1 << FastBits); - // fast.Fill(255); // Flag for non-accelerated - - // fixed (short* sizesRef = this.Sizes.Data) - // { - // for (int i = 0; i < k; i++) - // { - // int s = sizesRef[i]; - // if (s <= ScanDecoder.FastBits) - // { - // int c = codes[i] << (FastBits - s); - // int m = 1 << (FastBits - s); - // for (int j = 0; j < m; j++) - // { - // fast[c + j] = (byte)i; - // } - // } - // } - // } - //} - - this.GenerateCodeTable(ref huffcodeRef, Length, out int k); - this.GenerateDecoderTables(count, ref huffcodeRef); - this.GenerateLookaheadTables(count, values, ref huffcodeRef, k); + int k = 0; + fixed (short* size = this.Sizes.Data) + fixed (short* delta = this.ValOffset.Data) + fixed (long* maxcode = this.MaxCode.Data) + { + uint code = 0; + int j; + for (j = 1; j <= 16; j++) + { + // Compute delta to add to code to compute symbol id. + delta[j] = (short)(k - code); + if (size[k] == j) + { + while (size[k] == j) + { + codes[k++] = (short)code++; + + // Unsafe.Add(ref huffcodeRef, k++) = (short)code++; + + // TODO: Throw if invalid? + } + } + + // Compute largest code + 1 for this size. preshifted as neeed later. + maxcode[j] = code << (16 - j); + code <<= 1; + } + + maxcode[j] = 0xFFFFFFFF; + } + + fixed (byte* lookaheadRef = this.Lookahead.Data) + { + const int FastBits = ScanDecoder.FastBits; + var fast = new Span(lookaheadRef, 1 << FastBits); + fast.Fill(255); // Flag for non-accelerated + + fixed (short* sizesRef = this.Sizes.Data) + { + for (int i = 0; i < k; i++) + { + int s = sizesRef[i]; + if (s <= ScanDecoder.FastBits) + { + int c = codes[i] << (FastBits - s); + int m = 1 << (FastBits - s); + for (int j = 0; j < m; j++) + { + fast[c + j] = (byte)i; + } + } + } + } + } + + // this.GenerateCodeTable(ref huffcodeRef, Length, out int k); + // this.GenerateDecoderTables(count, ref huffcodeRef); + // this.GenerateLookaheadTables(count, values, ref huffcodeRef, k); } fixed (byte* huffValRef = this.Values.Data) @@ -224,7 +224,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components const int FastBits = ScanDecoder.FastBits; var lookaheadSpan = new Span(lookaheadRef, 1 << ScanDecoder.FastBits); - lookaheadSpan.Fill(255); // Flag for non-accelerated + lookaheadSpan.Fill(byte.MaxValue); // Flag for non-accelerated fixed (short* sizesRef = this.Sizes.Data) { for (int i = 0; i < k; ++i) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs index 62c8f984f0..0736fd342b 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs @@ -860,14 +860,5 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } } } - - private void Reset() - { - // Reset - // TODO: I do not understand why these values are reset? We should surely be tracking the bits across mcu's? - this.bitsCount = 0; - this.bitsData = 0; - this.unexpectedMarkerReached = false; - } } } \ 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 217b3cb62b..8322be2fe3 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs @@ -607,7 +607,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components // wants to be compared against something shifted to have 16; // that way we don't need to shift inside the loop. uint temp = this.codeBuffer >> 16; - for (k = FastBits + 1; ; ++k) + for (k = FastBits + 1; ; k++) { if (temp < table.MaxCode[k]) { From e1a8ebb6440c86781f5dcf6f112e0ed9bbc92709 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 31 May 2018 16:16:03 +1000 Subject: [PATCH 05/33] Baseline decoding seems to work --- .../Components/FixedInt16Buffer18.cs | 6 +- .../Components/FixedInt64Buffer18.cs | 6 +- .../PdfJsPort/Components/PdfJsHuffmanTable.cs | 22 +++--- .../Jpeg/PdfJsPort/Components/ScanDecoder.cs | 70 ++++++++++--------- .../Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs | 6 +- 5 files changed, 58 insertions(+), 52 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt16Buffer18.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt16Buffer18.cs index 20d4b77336..b193bf59e6 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt16Buffer18.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt16Buffer18.cs @@ -9,14 +9,14 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components [StructLayout(LayoutKind.Sequential)] internal unsafe struct FixedInt16Buffer18 { - public fixed short Data[18]; + public fixed int Data[18]; - public short this[int idx] + public int this[int idx] { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - ref short self = ref Unsafe.As(ref this); + ref int self = ref Unsafe.As(ref this); return Unsafe.Add(ref self, idx); } } diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt64Buffer18.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt64Buffer18.cs index 51381cb27a..a9266bd6b1 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt64Buffer18.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt64Buffer18.cs @@ -9,14 +9,14 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components [StructLayout(LayoutKind.Sequential)] internal unsafe struct FixedInt64Buffer18 { - public fixed long Data[18]; + public fixed uint Data[18]; - public long this[int idx] + public uint this[int idx] { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - ref long self = ref Unsafe.As(ref this); + ref uint self = ref Unsafe.As(ref this); return Unsafe.Add(ref self, idx); } } diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs index a63573030f..fb18340adf 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs @@ -57,15 +57,15 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components int k = 0; fixed (short* size = this.Sizes.Data) - fixed (short* delta = this.ValOffset.Data) - fixed (long* maxcode = this.MaxCode.Data) + fixed (int* delta = this.ValOffset.Data) + fixed (uint* maxcode = this.MaxCode.Data) { uint code = 0; int j; for (j = 1; j <= 16; j++) { // Compute delta to add to code to compute symbol id. - delta[j] = (short)(k - code); + delta[j] = (int)(k - code); if (size[k] == j) { while (size[k] == j) @@ -89,8 +89,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components fixed (byte* lookaheadRef = this.Lookahead.Data) { const int FastBits = ScanDecoder.FastBits; - var fast = new Span(lookaheadRef, 1 << FastBits); - fast.Fill(255); // Flag for non-accelerated + var fast = new Span(lookaheadRef, 1 << FastBits); + fast.Fill(0xFF); // Flag for non-accelerated fixed (short* sizesRef = this.Sizes.Data) { @@ -181,8 +181,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components /// The huffman code span ref private void GenerateDecoderTables(ReadOnlySpan lengths, ref short huffcodeRef) { - fixed (short* valOffsetRef = this.ValOffset.Data) - fixed (long* maxcodeRef = this.MaxCode.Data) + fixed (int* valOffsetRef = this.ValOffset.Data) + fixed (uint* maxcodeRef = this.MaxCode.Data) { short bitcount = 0; for (int i = 1; i <= 16; i++) @@ -190,18 +190,18 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components if (lengths[i] != 0) { // valOffsetRef[l] = huffcodeRef[] index of 1st symbol of code length i, minus the minimum code of length i - valOffsetRef[i] = (short)(bitcount - Unsafe.Add(ref huffcodeRef, bitcount)); + valOffsetRef[i] = (int)(bitcount - Unsafe.Add(ref huffcodeRef, bitcount)); bitcount += lengths[i]; - maxcodeRef[i] = Unsafe.Add(ref huffcodeRef, bitcount - 1) << (16 - i); // maximum code of length i preshifted for faster reading later + maxcodeRef[i] = (uint)Unsafe.Add(ref huffcodeRef, bitcount - 1) << (16 - i); // maximum code of length i preshifted for faster reading later } else { - maxcodeRef[i] = -1; // -1 if no codes of this length + // maxcodeRef[i] = -1; // -1 if no codes of this length } } valOffsetRef[17] = 0; - maxcodeRef[17] = 0xFFFFFFFFL; + maxcodeRef[17] = 0xFFFFFFFF; } } diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs index 8322be2fe3..9aec5173b7 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs @@ -148,32 +148,39 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { for (int i = 0; i < mcusPerLine; i++) { - // Scan an interleaved mcu... process components in order - for (int k = 0; k < this.componentsLength; k++) + try { - 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]; - Span fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); - int h = component.HorizontalSamplingFactor; - int v = component.VerticalSamplingFactor; - - // Scan out an mcu's worth of this component; that's just determined - // by the basic H and V specified for the component - for (int y = 0; y < v; y++) + // Scan an interleaved mcu... process components in order + for (int k = 0; k < this.componentsLength; k++) { - for (int x = 0; x < h; x++) + 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]; + Span fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); + int h = component.HorizontalSamplingFactor; + int v = component.VerticalSamplingFactor; + + // Scan out an mcu's worth of this component; that's just determined + // by the basic H and V specified for the component + for (int y = 0; y < v; y++) { - int mcuRow = mcu / mcusPerLine; - int mcuCol = mcu % mcusPerLine; - int blockRow = (mcuRow * v) + y; - int blockCol = (mcuCol * h) + x; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); - this.DecodeBlock(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable, ref acHuffmanTable, fastAC); + for (int x = 0; x < h; x++) + { + int mcuRow = mcu / mcusPerLine; + int mcuCol = mcu % mcusPerLine; + int blockRow = (mcuRow * v) + y; + int blockCol = (mcuCol * h) + x; + int offset = component.GetBlockBufferOffset(blockRow, blockCol); + this.DecodeBlock(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable, ref acHuffmanTable, fastAC); + } } } } + catch + { + break; + } // After all interleaved components, that's an interleaved MCU, // so now count down the restart interval @@ -207,7 +214,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components ref short blockDataRef, ref PdfJsHuffmanTable dcTable, ref PdfJsHuffmanTable acTable, - Span fac) + Span fastAc) { this.CheckBits(); int t = this.DecodeHuffman(ref dcTable); @@ -217,7 +224,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components throw new ImageFormatException("Bad Huffman code"); } - int diff = t > 0 ? this.ExtendReceive(t) : 0; + int diff = t != 0 ? this.ExtendReceive(t) : 0; int dc = component.DcPredictor + diff; component.DcPredictor = dc; blockDataRef = (short)dc; @@ -231,9 +238,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components this.CheckBits(); int c = this.PeekBits(); - int r = fac[c]; + int r = fastAc[c]; - if (r > 0) + if (r != 0) { // Fast AC path k += (r >> 4) & 15; // Run @@ -587,7 +594,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components // if the code is <= FastBits. int c = this.PeekBits(); int k = table.Lookahead[c]; - if (k < byte.MaxValue) + if (k < 0xFF) { int s = table.Sizes[k]; if (s > this.codeBits) @@ -628,7 +635,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } // Convert the huffman code to the symbol id - c = (int)((this.codeBuffer >> (32 - k)) & Bmask[k]) + table.ValOffset[k]; + c = (int)(((this.codeBuffer >> (32 - k)) & Bmask[k]) + table.ValOffset[k]); // Convert the id to a symbol this.codeBits -= k; @@ -644,7 +651,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components this.GrowBufferUnsafe(); } - int sgn = (int)(this.codeBuffer >> 31); + int sgn = (int)((int)this.codeBuffer >> 31); uint k = this.Lrot(this.codeBuffer, n); this.codeBuffer = k & ~Bmask[n]; k &= Bmask[n]; @@ -655,7 +662,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components [MethodImpl(MethodImplOptions.AggressiveInlining)] private void CheckBits() { - if (this.codeBuffer < 16) + if (this.codeBits < 16) { this.GrowBufferUnsafe(); } @@ -664,7 +671,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components [MethodImpl(MethodImplOptions.AggressiveInlining)] private int PeekBits() { - return (int)(this.codeBuffer >> ((32 - FastBits) & ((1 << FastBits) - 1))); + return (int)((this.codeBuffer >> (32 - FastBits)) & ((1 << FastBits) - 1)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -693,11 +700,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } this.marker = PdfJsJpegConstants.Markers.Prefix; - this.todo = this.restartInterval > 0 ? this.restartInterval : 0x7FFFFFFF; this.eobrun = 0; - // No more than 1<<31 MCUs if no restartInterval? that's plenty safe, - // since we don't even allow 1<<30 pixels + // No more than 1<<31 MCUs if no restartInterval? that's plenty safe since we don't even allow 1<<30 pixels + this.todo = this.restartInterval > 0 ? this.restartInterval : 0x7FFFFFFF; } } } \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs index 5bf4ab5aa4..f1d1053881 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs @@ -808,9 +808,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort sd.ParseEntropyCodedData(this.Frame, this.dcHuffmanTables, this.acHuffmanTables, this.fastACTables); - //var scanDecoder = default(PdfJsScanDecoder); - - //scanDecoder.DecodeScan( + // var scanDecoder = default(PdfJsScanDecoder); + // + // scanDecoder.DecodeScan( // this.Frame, // this.InputStream, // this.dcHuffmanTables, From e9a9f33be0ffab926b760e29f1c5790e4831591e Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 31 May 2018 18:37:43 +1000 Subject: [PATCH 06/33] Update reference types --- .../Jpeg/PdfJsPort/Components/ScanDecoder.cs | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs index 9aec5173b7..9a9348f7f3 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs @@ -4,8 +4,7 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; - -using SixLabors.ImageSharp.Formats.Jpeg.Common; +using SixLabors.ImageSharp.Formats.Jpeg.Components; using SixLabors.ImageSharp.Memory; namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components @@ -64,7 +63,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components this.dctZigZag = ZigZag.CreateUnzigTable(); this.stream = stream; this.components = components; - this.marker = PdfJsJpegConstants.Markers.Prefix; + this.marker = JpegConstants.Markers.XFF; this.componentIndex = componentIndex; this.componentsLength = componentsLength; this.restartInterval = restartInterval; @@ -532,7 +531,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components this.GrowBufferUnsafe(); } - uint k = this.Lrot(this.codeBuffer, n); + uint k = this.LRot(this.codeBuffer, n); this.codeBuffer = k & ~Bmask[n]; k &= Bmask[n]; this.codeBits -= n; @@ -554,17 +553,18 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components return (int)(k & 0x80000000); } + [MethodImpl(MethodImplOptions.NoInlining)] private void GrowBufferUnsafe() { do { // TODO: EOF int b = this.nomore ? 0 : this.stream.ReadByte(); - if (b == PdfJsJpegConstants.Markers.Prefix) + if (b == JpegConstants.Markers.XFF) { long position = this.stream.Position - 1; int c = this.stream.ReadByte(); - while (c == PdfJsJpegConstants.Markers.Prefix) + while (c == JpegConstants.Markers.XFF) { if (c != 0) { @@ -586,6 +586,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components while (this.codeBits <= 24); } + // TODO: Split into Fast/Slow and inline Fast private int DecodeHuffman(ref PdfJsHuffmanTable table) { this.CheckBits(); @@ -651,8 +652,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components this.GrowBufferUnsafe(); } - int sgn = (int)((int)this.codeBuffer >> 31); - uint k = this.Lrot(this.codeBuffer, n); + int sgn = (int)this.codeBuffer >> 31; + uint k = this.LRot(this.codeBuffer, n); this.codeBuffer = k & ~Bmask[n]; k &= Bmask[n]; this.codeBits -= n; @@ -675,7 +676,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private uint Lrot(uint x, int y) + private uint LRot(uint x, int y) { return (x << y) | (x >> (32 - y)); } @@ -683,7 +684,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components [MethodImpl(MethodImplOptions.AggressiveInlining)] private bool IsRestartMarker(byte x) { - return x >= PdfJsJpegConstants.Markers.RST0 && x <= PdfJsJpegConstants.Markers.RST7; + return x >= JpegConstants.Markers.RST0 && x <= JpegConstants.Markers.RST7; } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -699,7 +700,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components c.DcPredictor = 0; } - this.marker = PdfJsJpegConstants.Markers.Prefix; + this.marker = JpegConstants.Markers.XFF; this.eobrun = 0; // No more than 1<<31 MCUs if no restartInterval? that's plenty safe since we don't even allow 1<<30 pixels From 3b0184ffc5ff78990a8f580fef49f66931c20928 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sat, 30 Jun 2018 11:16:35 +1000 Subject: [PATCH 07/33] 6/10 baseline images now pass. --- .../Jpeg/PdfJsPort/Components/ScanDecoder.cs | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs index 9a9348f7f3..f1edd55deb 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs @@ -562,21 +562,17 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components int b = this.nomore ? 0 : this.stream.ReadByte(); if (b == JpegConstants.Markers.XFF) { - long position = this.stream.Position - 1; int c = this.stream.ReadByte(); while (c == JpegConstants.Markers.XFF) { - if (c != 0) - { - this.marker = (byte)c; - this.nomore = true; - if (!this.IsRestartMarker(this.marker)) - { - this.stream.Position = position; - } + c = this.stream.ReadByte(); + } - return; - } + if (c != 0) + { + this.marker = (byte)c; + this.nomore = true; + return; } } @@ -586,7 +582,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components while (this.codeBits <= 24); } - // TODO: Split into Fast/Slow and inline Fast + [MethodImpl(MethodImplOptions.AggressiveInlining)] private int DecodeHuffman(ref PdfJsHuffmanTable table) { this.CheckBits(); @@ -608,6 +604,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components return table.Values[k]; } + return this.DecodeHuffmanSlow(ref table); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private int DecodeHuffmanSlow(ref PdfJsHuffmanTable table) + { // Naive test is to shift the code_buffer down so k bits are // valid, then test against MaxCode. To speed this up, we've // preshifted maxcode left so that it has (16-k) 0s at the @@ -615,6 +617,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components // wants to be compared against something shifted to have 16; // that way we don't need to shift inside the loop. uint temp = this.codeBuffer >> 16; + int k; for (k = FastBits + 1; ; k++) { if (temp < table.MaxCode[k]) @@ -636,7 +639,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } // Convert the huffman code to the symbol id - c = (int)(((this.codeBuffer >> (32 - k)) & Bmask[k]) + table.ValOffset[k]); + int c = (int)(((this.codeBuffer >> (32 - k)) & Bmask[k]) + table.ValOffset[k]); // Convert the id to a symbol this.codeBits -= k; From 9edaf542aa28934180f02474c9c22701b924b12c Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sat, 30 Jun 2018 19:08:00 +1000 Subject: [PATCH 08/33] 9/10 baseline now pass! --- .../Jpeg/PdfJsPort/Components/ScanDecoder.cs | 51 ++++++++----------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs index 61482569a8..21763c5229 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs @@ -118,7 +118,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components mcu++; // Every data block is an MCU, so countdown the restart interval - if (this.todo-- <= 0) + if (--this.todo <= 0) { if (this.codeBits < 24) { @@ -147,44 +147,37 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { for (int i = 0; i < mcusPerLine; i++) { - try + // Scan an interleaved mcu... process components in order + for (int k = 0; k < this.componentsLength; k++) { - // Scan an interleaved mcu... process components in order - 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]; + Span fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); + int h = component.HorizontalSamplingFactor; + int v = component.VerticalSamplingFactor; + + // Scan out an mcu's worth of this component; that's just determined + // by the basic H and V specified for the component + for (int y = 0; y < v; y++) { - 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]; - Span fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); - int h = component.HorizontalSamplingFactor; - int v = component.VerticalSamplingFactor; - - // Scan out an mcu's worth of this component; that's just determined - // by the basic H and V specified for the component - for (int y = 0; y < v; y++) + for (int x = 0; x < h; x++) { - for (int x = 0; x < h; x++) - { - int mcuRow = mcu / mcusPerLine; - int mcuCol = mcu % mcusPerLine; - int blockRow = (mcuRow * v) + y; - int blockCol = (mcuCol * h) + x; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); - this.DecodeBlock(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable, ref acHuffmanTable, fastAC); - } + int mcuRow = mcu / mcusPerLine; + int mcuCol = mcu % mcusPerLine; + int blockRow = (mcuRow * v) + y; + int blockCol = (mcuCol * h) + x; + int offset = component.GetBlockBufferOffset(blockRow, blockCol); + this.DecodeBlock(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable, ref acHuffmanTable, fastAC); } } } - catch - { - break; - } // After all interleaved components, that's an interleaved MCU, // so now count down the restart interval mcu++; - if (this.todo-- <= 0) + if (--this.todo <= 0) { if (this.codeBits < 24) { From 4fddef4e1dc085a60815e0468cdf7ce6cfbb016b Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 1 Jul 2018 02:01:29 +1000 Subject: [PATCH 09/33] Can now decode baseline + progressive --- .../Jpeg/PdfJsPort/Components/ScanDecoder.cs | 207 ++++++++++++++++-- 1 file changed, 187 insertions(+), 20 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs index 21763c5229..6c781dc8d4 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs @@ -25,7 +25,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components private int codeBits; private uint codeBuffer; private bool nomore; + private bool eof; private byte marker; + private bool badMarker; + private long markerPosition; private int todo; private int restartInterval; @@ -64,6 +67,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components this.stream = stream; this.components = components; this.marker = JpegConstants.Markers.XFF; + this.markerPosition = 0; this.componentIndex = componentIndex; this.componentsLength = componentsLength; this.restartInterval = restartInterval; @@ -104,13 +108,18 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components 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]; - Span fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); + ReadOnlySpan fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); int mcu = 0; for (int j = 0; j < h; j++) { for (int i = 0; i < w; i++) { + if (this.eof) + { + continue; + } + int blockRow = mcu / w; int blockCol = mcu % w; int offset = component.GetBlockBufferOffset(blockRow, blockCol); @@ -154,7 +163,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components 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]; - Span fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); + ReadOnlySpan fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); int h = component.HorizontalSamplingFactor; int v = component.VerticalSamplingFactor; @@ -164,6 +173,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { for (int x = 0; x < h; x++) { + if (this.eof) + { + continue; + } + int mcuRow = mcu / mcusPerLine; int mcuCol = mcu % mcusPerLine; int blockRow = (mcuRow * v) + y; @@ -197,6 +211,138 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } } } + else + { + if (this.componentsLength == 1) + { + PdfJsFrameComponent component = this.components[this.componentIndex]; + + // Non-interleaved data, we just need to process one block at a time, + // in trivial scanline order + // number of blocks to do just depends on how many actual "pixels" this + // component has, independent of interleaved MCU blocking and such + 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]; + ReadOnlySpan fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); + + int mcu = 0; + for (int j = 0; j < h; j++) + { + for (int i = 0; i < w; i++) + { + if (this.eof) + { + continue; + } + + 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); + } + else + { + this.DecodeBlockProgressiveAC(ref Unsafe.Add(ref blockDataRef, offset), ref acHuffmanTable, fastAC); + } + + mcu++; + + // Every data block is an MCU, so countdown the restart interval + if (--this.todo <= 0) + { + if (this.codeBits < 24) + { + this.GrowBufferUnsafe(); + } + + // If it's NOT a restart, then just bail, so we get corrupt data + // rather than no data + if (!this.IsRestartMarker(this.marker)) + { + return 1; + } + + this.Reset(); + } + } + } + } + else + { + // Interleaved + int mcu = 0; + int mcusPerColumn = frame.McusPerColumn; + int mcusPerLine = frame.McusPerLine; + for (int j = 0; j < mcusPerColumn; j++) + { + for (int i = 0; i < mcusPerLine; i++) + { + // Scan an interleaved mcu... process components in order + 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]; + ReadOnlySpan fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); + int h = component.HorizontalSamplingFactor; + int v = component.VerticalSamplingFactor; + + // Scan out an mcu's worth of this component; that's just determined + // by the basic H and V specified for the component + for (int y = 0; y < v; y++) + { + for (int x = 0; x < h; x++) + { + if (this.eof) + { + continue; + } + + int mcuRow = mcu / mcusPerLine; + int mcuCol = mcu % mcusPerLine; + int blockRow = (mcuRow * v) + y; + int blockCol = (mcuCol * h) + x; + int offset = component.GetBlockBufferOffset(blockRow, blockCol); + this.DecodeBlockProgressiveDC(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable); + } + } + } + + // After all interleaved components, that's an interleaved MCU, + // so now count down the restart interval + mcu++; + if (--this.todo <= 0) + { + if (this.codeBits < 24) + { + this.GrowBufferUnsafe(); + } + + // If it's NOT a restart, then just bail, so we get corrupt data + // rather than no data + if (!this.IsRestartMarker(this.marker)) + { + return 1; + } + + this.Reset(); + } + } + } + } + } + + if (this.badMarker) + { + this.stream.Position = this.markerPosition; + } return 1; } @@ -206,7 +352,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components ref short blockDataRef, ref PdfJsHuffmanTable dcTable, ref PdfJsHuffmanTable acTable, - Span fastAc) + ReadOnlySpan fastAc) { this.CheckBits(); int t = this.DecodeHuffman(ref dcTable); @@ -295,7 +441,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { // First scan for DC coefficient, must be first int t = this.DecodeHuffman(ref dcTable); - int diff = t > 0 ? this.ExtendReceive(t) : 0; + int diff = t != 0 ? this.ExtendReceive(t) : 0; int dc = component.DcPredictor + diff; component.DcPredictor = dc; @@ -305,7 +451,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components else { // Refinement scan for DC coefficient - if (this.GetBit() > 0) + if (this.GetBit() != 0) { blockDataRef += (short)(1 << this.successiveLow); } @@ -315,10 +461,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } private int DecodeBlockProgressiveAC( - PdfJsFrameComponent component, ref short blockDataRef, ref PdfJsHuffmanTable acTable, - Span fac) + ReadOnlySpan fastAc) { int k; @@ -331,7 +476,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { int shift = this.successiveLow; - if (this.eobrun > 0) + if (this.eobrun != 0) { this.eobrun--; return 1; @@ -345,9 +490,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components this.CheckBits(); int c = this.PeekBits(); - int r = fac[c]; + int r = fastAc[c]; - if (r > 0) + if (r != 0) { // Fast AC path k += (r >> 4) & 15; // Run @@ -376,7 +521,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components if (r < 15) { this.eobrun = 1 << r; - if (r > 0) + if (r != 0) { this.eobrun += this.GetBits(r); } @@ -402,15 +547,15 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components // Refinement scan for these AC coefficients short bit = (short)(1 << this.successiveLow); - if (this.eobrun > 0) + if (this.eobrun != 0) { this.eobrun--; - for (k = this.spectralStart; k < this.spectralEnd; k++) + for (k = this.spectralStart; k <= this.spectralEnd; k++) { ref short p = ref Unsafe.Add(ref blockDataRef, this.dctZigZag[k]); if (p != 0) { - if (this.GetBit() > 0) + if (this.GetBit() != 0) { if ((p & bit) == 0) { @@ -450,7 +595,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { this.eobrun = (1 << r) - 1; - if (r > 0) + if (r != 0) { this.eobrun += this.GetBits(r); } @@ -466,13 +611,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } // Sign bit - if (this.GetBit() > 0) + if (this.GetBit() != 0) { s = bit; } else { - s -= bit; + s = -bit; } } @@ -482,7 +627,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components ref short p = ref Unsafe.Add(ref blockDataRef, this.dctZigZag[k++]); if (p != 0) { - if (this.GetBit() > 0) + if (this.GetBit() != 0) { if ((p & bit) == 0) { @@ -553,18 +698,38 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { // TODO: EOF int b = this.nomore ? 0 : this.stream.ReadByte(); + + if (b == -1) + { + this.eof = true; + b = 0; + } + if (b == JpegConstants.Markers.XFF) { + this.markerPosition = this.stream.Position - 1; int c = this.stream.ReadByte(); while (c == JpegConstants.Markers.XFF) { c = this.stream.ReadByte(); + + if (c == -1) + { + this.eof = true; + c = 0; + break; + } } if (c != 0) { this.marker = (byte)c; this.nomore = true; + if (!this.IsRestartMarker(this.marker)) + { + this.badMarker = true; + } + return; } } @@ -688,7 +853,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { this.codeBits = 0; this.codeBuffer = 0; - this.nomore = false; for (int i = 0; i < this.components.Length; i++) { @@ -696,11 +860,14 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components c.DcPredictor = 0; } + this.nomore = false; this.marker = JpegConstants.Markers.XFF; + this.markerPosition = 0; + this.badMarker = false; this.eobrun = 0; // No more than 1<<31 MCUs if no restartInterval? that's plenty safe since we don't even allow 1<<30 pixels - this.todo = this.restartInterval > 0 ? this.restartInterval : 0x7FFFFFFF; + this.todo = this.restartInterval > 0 ? this.restartInterval : int.MaxValue; } } } \ No newline at end of file From 020e2ecd44d9d609ae7e8025e3ad6cf77b3e87d0 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 1 Jul 2018 16:11:31 +1000 Subject: [PATCH 10/33] Split decode method. --- .../Jpeg/PdfJsPort/Components/ScanDecoder.cs | 426 +++++++++--------- 1 file changed, 223 insertions(+), 203 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs index 6c781dc8d4..8b5ed0c053 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs @@ -84,8 +84,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components /// The DC Huffman tables. /// The AC Huffman tables. /// The fast AC decoding tables. - /// The - public int ParseEntropyCodedData( + public void ParseEntropyCodedData( PdfJsFrame frame, PdfJsHuffmanTables dcHuffmanTables, PdfJsHuffmanTables acHuffmanTables, @@ -95,256 +94,272 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components if (!frame.Progressive) { - if (this.componentsLength == 1) + this.ParseBaselineData(frame, dcHuffmanTables, acHuffmanTables, fastACTables); + } + else + { + this.ParseProgressiveData(frame, dcHuffmanTables, acHuffmanTables, fastACTables); + } + + if (this.badMarker) + { + this.stream.Position = this.markerPosition; + } + } + + private void ParseBaselineData( + PdfJsFrame frame, + PdfJsHuffmanTables dcHuffmanTables, + PdfJsHuffmanTables acHuffmanTables, + FastACTables fastACTables) + { + if (this.componentsLength == 1) + { + PdfJsFrameComponent component = this.components[this.componentIndex]; + + // Non-interleaved data, we just need to process one block at a time, + // in trivial scanline order + // number of blocks to do just depends on how many actual "pixels" this + // component has, independent of interleaved MCU blocking and such + 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]; + ReadOnlySpan fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); + + int mcu = 0; + for (int j = 0; j < h; j++) { - PdfJsFrameComponent component = this.components[this.componentIndex]; - - // Non-interleaved data, we just need to process one block at a time, - // in trivial scanline order - // number of blocks to do just depends on how many actual "pixels" this - // component has, independent of interleaved MCU blocking and such - 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]; - ReadOnlySpan fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); - - int mcu = 0; - for (int j = 0; j < h; j++) + for (int i = 0; i < w; i++) { - for (int i = 0; i < w; i++) + if (this.eof) { - if (this.eof) - { - continue; - } + return; + } - int blockRow = mcu / w; - int blockCol = mcu % w; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); - this.DecodeBlock(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable, ref acHuffmanTable, fastAC); - mcu++; + int blockRow = mcu / w; + int blockCol = mcu % w; + int offset = component.GetBlockBufferOffset(blockRow, blockCol); + this.DecodeBlock(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable, ref acHuffmanTable, fastAC); + mcu++; - // Every data block is an MCU, so countdown the restart interval - if (--this.todo <= 0) + // Every data block is an MCU, so countdown the restart interval + if (--this.todo <= 0) + { + if (this.codeBits < 24) { - if (this.codeBits < 24) - { - this.GrowBufferUnsafe(); - } - - // If it's NOT a restart, then just bail, so we get corrupt data - // rather than no data - if (!this.IsRestartMarker(this.marker)) - { - return 1; - } + this.GrowBufferUnsafe(); + } - this.Reset(); + // If it's NOT a restart, then just bail, so we get corrupt data + // rather than no data + if (!this.ContinueOnRestart()) + { + return; } + + this.Reset(); } } } - else + } + else + { + // Interleaved + int mcu = 0; + int mcusPerColumn = frame.McusPerColumn; + int mcusPerLine = frame.McusPerLine; + for (int j = 0; j < mcusPerColumn; j++) { - // Interleaved - int mcu = 0; - int mcusPerColumn = frame.McusPerColumn; - int mcusPerLine = frame.McusPerLine; - for (int j = 0; j < mcusPerColumn; j++) + for (int i = 0; i < mcusPerLine; i++) { - for (int i = 0; i < mcusPerLine; i++) + // Scan an interleaved mcu... process components in order + for (int k = 0; k < this.componentsLength; k++) { - // Scan an interleaved mcu... process components in order - 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]; + ReadOnlySpan fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); + int h = component.HorizontalSamplingFactor; + int v = component.VerticalSamplingFactor; + + // Scan out an mcu's worth of this component; that's just determined + // by the basic H and V specified for the component + for (int y = 0; y < v; y++) { - 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]; - ReadOnlySpan fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); - int h = component.HorizontalSamplingFactor; - int v = component.VerticalSamplingFactor; - - // Scan out an mcu's worth of this component; that's just determined - // by the basic H and V specified for the component - for (int y = 0; y < v; y++) + for (int x = 0; x < h; x++) { - for (int x = 0; x < h; x++) + if (this.eof) { - if (this.eof) - { - continue; - } - - int mcuRow = mcu / mcusPerLine; - int mcuCol = mcu % mcusPerLine; - int blockRow = (mcuRow * v) + y; - int blockCol = (mcuCol * h) + x; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); - this.DecodeBlock(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable, ref acHuffmanTable, fastAC); + return; } + + int mcuRow = mcu / mcusPerLine; + int mcuCol = mcu % mcusPerLine; + int blockRow = (mcuRow * v) + y; + int blockCol = (mcuCol * h) + x; + int offset = component.GetBlockBufferOffset(blockRow, blockCol); + this.DecodeBlock(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable, ref acHuffmanTable, fastAC); } } + } - // After all interleaved components, that's an interleaved MCU, - // so now count down the restart interval - mcu++; - if (--this.todo <= 0) + // After all interleaved components, that's an interleaved MCU, + // so now count down the restart interval + mcu++; + if (--this.todo <= 0) + { + if (this.codeBits < 24) { - if (this.codeBits < 24) - { - this.GrowBufferUnsafe(); - } - - // If it's NOT a restart, then just bail, so we get corrupt data - // rather than no data - if (!this.IsRestartMarker(this.marker)) - { - return 1; - } + this.GrowBufferUnsafe(); + } - this.Reset(); + // If it's NOT a restart, then just bail, so we get corrupt data + // rather than no data + if (!this.ContinueOnRestart()) + { + return; } + + this.Reset(); } } } } - else + } + + private void ParseProgressiveData( + PdfJsFrame frame, + PdfJsHuffmanTables dcHuffmanTables, + PdfJsHuffmanTables acHuffmanTables, + FastACTables fastACTables) + { + if (this.componentsLength == 1) { - if (this.componentsLength == 1) + PdfJsFrameComponent component = this.components[this.componentIndex]; + + // Non-interleaved data, we just need to process one block at a time, + // in trivial scanline order + // number of blocks to do just depends on how many actual "pixels" this + // component has, independent of interleaved MCU blocking and such + 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]; + ReadOnlySpan fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); + + int mcu = 0; + for (int j = 0; j < h; j++) { - PdfJsFrameComponent component = this.components[this.componentIndex]; - - // Non-interleaved data, we just need to process one block at a time, - // in trivial scanline order - // number of blocks to do just depends on how many actual "pixels" this - // component has, independent of interleaved MCU blocking and such - 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]; - ReadOnlySpan fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); - - int mcu = 0; - for (int j = 0; j < h; j++) + for (int i = 0; i < w; i++) { - for (int i = 0; i < w; i++) + if (this.eof) { - if (this.eof) - { - continue; - } + return; + } - int blockRow = mcu / w; - int blockCol = mcu % w; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); + 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); - } - else - { - this.DecodeBlockProgressiveAC(ref Unsafe.Add(ref blockDataRef, offset), ref acHuffmanTable, fastAC); - } + if (this.spectralStart == 0) + { + this.DecodeBlockProgressiveDC(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable); + } + else + { + this.DecodeBlockProgressiveAC(ref Unsafe.Add(ref blockDataRef, offset), ref acHuffmanTable, fastAC); + } - mcu++; + mcu++; - // Every data block is an MCU, so countdown the restart interval - if (--this.todo <= 0) + // Every data block is an MCU, so countdown the restart interval + if (--this.todo <= 0) + { + if (this.codeBits < 24) { - if (this.codeBits < 24) - { - this.GrowBufferUnsafe(); - } - - // If it's NOT a restart, then just bail, so we get corrupt data - // rather than no data - if (!this.IsRestartMarker(this.marker)) - { - return 1; - } + this.GrowBufferUnsafe(); + } - this.Reset(); + // If it's NOT a restart, then just bail, so we get corrupt data + // rather than no data + if (!this.ContinueOnRestart()) + { + return; } + + this.Reset(); } } } - else + } + else + { + // Interleaved + int mcu = 0; + int mcusPerColumn = frame.McusPerColumn; + int mcusPerLine = frame.McusPerLine; + for (int j = 0; j < mcusPerColumn; j++) { - // Interleaved - int mcu = 0; - int mcusPerColumn = frame.McusPerColumn; - int mcusPerLine = frame.McusPerLine; - for (int j = 0; j < mcusPerColumn; j++) + for (int i = 0; i < mcusPerLine; i++) { - for (int i = 0; i < mcusPerLine; i++) + // Scan an interleaved mcu... process components in order + for (int k = 0; k < this.componentsLength; k++) { - // Scan an interleaved mcu... process components in order - 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]; + ReadOnlySpan fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); + int h = component.HorizontalSamplingFactor; + int v = component.VerticalSamplingFactor; + + // Scan out an mcu's worth of this component; that's just determined + // by the basic H and V specified for the component + for (int y = 0; y < v; y++) { - 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]; - ReadOnlySpan fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); - int h = component.HorizontalSamplingFactor; - int v = component.VerticalSamplingFactor; - - // Scan out an mcu's worth of this component; that's just determined - // by the basic H and V specified for the component - for (int y = 0; y < v; y++) + for (int x = 0; x < h; x++) { - for (int x = 0; x < h; x++) + if (this.eof) { - if (this.eof) - { - continue; - } - - int mcuRow = mcu / mcusPerLine; - int mcuCol = mcu % mcusPerLine; - int blockRow = (mcuRow * v) + y; - int blockCol = (mcuCol * h) + x; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); - this.DecodeBlockProgressiveDC(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable); + return; } + + int mcuRow = mcu / mcusPerLine; + int mcuCol = mcu % mcusPerLine; + int blockRow = (mcuRow * v) + y; + int blockCol = (mcuCol * h) + x; + int offset = component.GetBlockBufferOffset(blockRow, blockCol); + this.DecodeBlockProgressiveDC(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable); } } + } - // After all interleaved components, that's an interleaved MCU, - // so now count down the restart interval - mcu++; - if (--this.todo <= 0) + // After all interleaved components, that's an interleaved MCU, + // so now count down the restart interval + mcu++; + if (--this.todo <= 0) + { + if (this.codeBits < 24) { - if (this.codeBits < 24) - { - this.GrowBufferUnsafe(); - } - - // If it's NOT a restart, then just bail, so we get corrupt data - // rather than no data - if (!this.IsRestartMarker(this.marker)) - { - return 1; - } + this.GrowBufferUnsafe(); + } - this.Reset(); + // If it's NOT a restart, then just bail, so we get corrupt data + // rather than no data + if (!this.ContinueOnRestart()) + { + return; } + + this.Reset(); } } } } - - if (this.badMarker) - { - this.stream.Position = this.markerPosition; - } - - return 1; } private int DecodeBlock( @@ -696,7 +711,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { do { - // TODO: EOF int b = this.nomore ? 0 : this.stream.ReadByte(); if (b == -1) @@ -725,7 +739,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { this.marker = (byte)c; this.nomore = true; - if (!this.IsRestartMarker(this.marker)) + if (!this.HasRestart()) { this.badMarker = true; } @@ -831,21 +845,27 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private int PeekBits() - { - return (int)((this.codeBuffer >> (32 - FastBits)) & ((1 << FastBits) - 1)); - } + private int PeekBits() => (int)((this.codeBuffer >> (32 - FastBits)) & ((1 << FastBits) - 1)); [MethodImpl(MethodImplOptions.AggressiveInlining)] - private uint LRot(uint x, int y) + private uint LRot(uint x, int y) => (x << y) | (x >> (32 - y)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private bool ContinueOnRestart() { - return (x << y) | (x >> (32 - y)); + if (this.badMarker) + { + this.stream.Position = this.markerPosition; + } + + return this.HasRestart(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private bool IsRestartMarker(byte x) + private bool HasRestart() { - return x >= JpegConstants.Markers.RST0 && x <= JpegConstants.Markers.RST7; + byte m = this.marker; + return m >= JpegConstants.Markers.RST0 && m <= JpegConstants.Markers.RST7; } [MethodImpl(MethodImplOptions.AggressiveInlining)] From 09d29f59efe12dcd08f21f336ec8b09d8a1144dd Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 1 Jul 2018 16:26:06 +1000 Subject: [PATCH 11/33] void methods --- .../Jpeg/PdfJsPort/Components/ScanDecoder.cs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs index 8b5ed0c053..bdf87bac5d 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs @@ -362,7 +362,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } } - private int DecodeBlock( + private void DecodeBlock( PdfJsFrameComponent component, ref short blockDataRef, ref PdfJsHuffmanTable dcTable, @@ -436,11 +436,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } } } while (k < 64); - - return 1; } - private int DecodeBlockProgressiveDC( + private void DecodeBlockProgressiveDC( PdfJsFrameComponent component, ref short blockDataRef, ref PdfJsHuffmanTable dcTable) @@ -471,11 +469,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components blockDataRef += (short)(1 << this.successiveLow); } } - - return 1; } - private int DecodeBlockProgressiveAC( + private void DecodeBlockProgressiveAC( ref short blockDataRef, ref PdfJsHuffmanTable acTable, ReadOnlySpan fastAc) @@ -494,7 +490,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components if (this.eobrun != 0) { this.eobrun--; - return 1; + return; } k = this.spectralStart; @@ -672,8 +668,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components while (k <= this.spectralEnd); } } - - return 1; } [MethodImpl(MethodImplOptions.AggressiveInlining)] From d6fd103ca1663463100e1cad1f0c2c653fe9200d Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 1 Jul 2018 16:44:35 +1000 Subject: [PATCH 12/33] Minor perf changes. --- .../Jpeg/PdfJsPort/Components/ScanDecoder.cs | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs index bdf87bac5d..8a39dab344 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs @@ -22,6 +22,14 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components private readonly DoubleBufferedStreamReader stream; private readonly PdfJsFrameComponent[] components; private readonly ZigZag dctZigZag; + private readonly int restartInterval; + private readonly int componentIndex; + private readonly int componentsLength; + private readonly int spectralStart; + private readonly int spectralEnd; + private readonly int successiveHigh; + private readonly int successiveLow; + private int codeBits; private uint codeBuffer; private bool nomore; @@ -29,16 +37,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components private byte marker; private bool badMarker; private long markerPosition; - private int todo; - private int restartInterval; - private int componentIndex; - private int componentsLength; private int eobrun; - private int spectralStart; - private int spectralEnd; - private int successiveHigh; - private int successiveLow; /// /// Initializes a new instance of the class. @@ -126,7 +126,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components 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]; - ReadOnlySpan fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); + ref short fastACRef = ref MemoryMarshal.GetReference(fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId)); int mcu = 0; for (int j = 0; j < h; j++) @@ -141,7 +141,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components int blockRow = mcu / w; int blockCol = mcu % w; int offset = component.GetBlockBufferOffset(blockRow, blockCol); - this.DecodeBlock(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable, ref acHuffmanTable, fastAC); + this.DecodeBlock(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable, ref acHuffmanTable, ref fastACRef); mcu++; // Every data block is an MCU, so countdown the restart interval @@ -181,7 +181,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components 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]; - ReadOnlySpan fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); + ref short fastACRef = ref MemoryMarshal.GetReference(fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId)); int h = component.HorizontalSamplingFactor; int v = component.VerticalSamplingFactor; @@ -201,7 +201,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components int blockRow = (mcuRow * v) + y; int blockCol = (mcuCol * h) + x; int offset = component.GetBlockBufferOffset(blockRow, blockCol); - this.DecodeBlock(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable, ref acHuffmanTable, fastAC); + this.DecodeBlock(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable, ref acHuffmanTable, ref fastACRef); } } } @@ -249,7 +249,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components 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]; - ReadOnlySpan fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); + ref short fastACRef = ref MemoryMarshal.GetReference(fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId)); int mcu = 0; for (int j = 0; j < h; j++) @@ -271,7 +271,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } else { - this.DecodeBlockProgressiveAC(ref Unsafe.Add(ref blockDataRef, offset), ref acHuffmanTable, fastAC); + this.DecodeBlockProgressiveAC(ref Unsafe.Add(ref blockDataRef, offset), ref acHuffmanTable, ref fastACRef); } mcu++; @@ -367,7 +367,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components ref short blockDataRef, ref PdfJsHuffmanTable dcTable, ref PdfJsHuffmanTable acTable, - ReadOnlySpan fastAc) + ref short fastACRef) { this.CheckBits(); int t = this.DecodeHuffman(ref dcTable); @@ -391,7 +391,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components this.CheckBits(); int c = this.PeekBits(); - int r = fastAc[c]; + int r = Unsafe.Add(ref fastACRef, c); if (r != 0) { @@ -474,7 +474,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components private void DecodeBlockProgressiveAC( ref short blockDataRef, ref PdfJsHuffmanTable acTable, - ReadOnlySpan fastAc) + ref short fastACRef) { int k; @@ -501,7 +501,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components this.CheckBits(); int c = this.PeekBits(); - int r = fastAc[c]; + int r = Unsafe.Add(ref fastACRef, c); if (r != 0) { From 78e0531c8936b1feb620a91462c283ce7613c736 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 1 Jul 2018 17:39:47 +1000 Subject: [PATCH 13/33] Remove unused huffman table code. --- .../PdfJsPort/Components/PdfJsHuffmanTable.cs | 198 ++++-------------- 1 file changed, 41 insertions(+), 157 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs index 16a60d1874..e843ebbaca 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs @@ -50,55 +50,65 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components const int Length = 257; using (IBuffer huffcode = memoryAllocator.Allocate(Length)) { - Span codes = huffcode.GetSpan(); ref short huffcodeRef = ref MemoryMarshal.GetReference(huffcode.GetSpan()); - this.GenerateSizeTable(count); - int k = 0; - fixed (short* size = this.Sizes.Data) - fixed (int* delta = this.ValOffset.Data) - fixed (uint* maxcode = this.MaxCode.Data) + // Figure C.1: make table of Huffman code length for each symbol + fixed (short* sizesRef = this.Sizes.Data) { - uint code = 0; - int j; - for (j = 1; j <= 16; j++) + short x = 0; + for (short i = 1; i < 17; i++) { - // Compute delta to add to code to compute symbol id. - delta[j] = (int)(k - code); - if (size[k] == j) + byte l = count[i]; + for (short j = 0; j < l; j++) { - while (size[k] == j) - { - codes[k++] = (short)code++; + sizesRef[x] = i; + x++; + } + } - // Unsafe.Add(ref huffcodeRef, k++) = (short)code++; + sizesRef[x] = 0; - // TODO: Throw if invalid? + // Figure C.2: generate the codes themselves + int k = 0; + fixed (int* valOffsetRef = this.ValOffset.Data) + fixed (uint* maxcodeRef = this.MaxCode.Data) + { + uint code = 0; + int j; + for (j = 1; j < 17; j++) + { + // Compute delta to add to code to compute symbol id. + valOffsetRef[j] = (int)(k - code); + if (sizesRef[k] == j) + { + while (sizesRef[k] == j) + { + Unsafe.Add(ref huffcodeRef, k++) = (short)code++; + } } + + // Figure F.15: generate decoding tables for bit-sequential decoding. + // Compute largest code + 1 for this size. preshifted as neeed later. + maxcodeRef[j] = code << (16 - j); + code <<= 1; } - // Compute largest code + 1 for this size. preshifted as neeed later. - maxcode[j] = code << (16 - j); - code <<= 1; + maxcodeRef[j] = 0xFFFFFFFF; } - maxcode[j] = 0xFFFFFFFF; - } - - fixed (byte* lookaheadRef = this.Lookahead.Data) - { - const int FastBits = ScanDecoder.FastBits; - var fast = new Span(lookaheadRef, 1 << FastBits); - fast.Fill(0xFF); // Flag for non-accelerated - - fixed (short* sizesRef = this.Sizes.Data) + // Generate non-spec lookup tables to speed up decoding. + fixed (byte* lookaheadRef = this.Lookahead.Data) { + const int FastBits = ScanDecoder.FastBits; + var fast = new Span(lookaheadRef, 1 << FastBits); + fast.Fill(0xFF); // Flag for non-accelerated + for (int i = 0; i < k; i++) { int s = sizesRef[i]; if (s <= ScanDecoder.FastBits) { - int c = codes[i] << (FastBits - s); + int c = Unsafe.Add(ref huffcodeRef, i) << (FastBits - s); int m = 1 << (FastBits - s); for (int j = 0; j < m; j++) { @@ -108,139 +118,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } } } - - // this.GenerateCodeTable(ref huffcodeRef, Length, out int k); - // this.GenerateDecoderTables(count, ref huffcodeRef); - // this.GenerateLookaheadTables(count, values, ref huffcodeRef, k); } fixed (byte* huffValRef = this.Values.Data) { var huffValSpan = new Span(huffValRef, 256); - values.CopyTo(huffValSpan); } } - - /// - /// Figure C.1: make table of Huffman code length for each symbol - /// - /// The code lengths - private void GenerateSizeTable(ReadOnlySpan lengths) - { - fixed (short* sizesRef = this.Sizes.Data) - { - short k = 0; - for (short i = 1; i < 17; i++) - { - byte l = lengths[i]; - for (short j = 0; j < l; j++) - { - sizesRef[k] = i; - k++; - } - } - - sizesRef[k] = 0; - } - } - - /// - /// Figure C.2: generate the codes themselves - /// - /// The huffman code span ref - /// The length of the huffsize span - /// The length of any valid codes - private void GenerateCodeTable(ref short huffcodeRef, int length, out int k) - { - fixed (short* sizesRef = this.Sizes.Data) - { - k = 0; - short si = sizesRef[0]; - short code = 0; - for (short i = 0; i < length; i++) - { - while (sizesRef[k] == si) - { - Unsafe.Add(ref huffcodeRef, k) = code; - code++; - k++; - } - - code <<= 1; - si++; - } - } - } - - /// - /// Figure F.15: generate decoding tables for bit-sequential decoding - /// - /// The code lengths - /// The huffman code span ref - private void GenerateDecoderTables(ReadOnlySpan lengths, ref short huffcodeRef) - { - fixed (int* valOffsetRef = this.ValOffset.Data) - fixed (uint* maxcodeRef = this.MaxCode.Data) - { - short bitcount = 0; - for (int i = 1; i <= 16; i++) - { - if (lengths[i] != 0) - { - // valOffsetRef[l] = huffcodeRef[] index of 1st symbol of code length i, minus the minimum code of length i - valOffsetRef[i] = (int)(bitcount - Unsafe.Add(ref huffcodeRef, bitcount)); - bitcount += lengths[i]; - maxcodeRef[i] = (uint)Unsafe.Add(ref huffcodeRef, bitcount - 1) << (16 - i); // maximum code of length i preshifted for faster reading later - } - else - { - // maxcodeRef[i] = -1; // -1 if no codes of this length - } - } - - valOffsetRef[17] = 0; - maxcodeRef[17] = 0xFFFFFFFF; - } - } - - /// - /// Generates non-spec lookup tables to speed up decoding - /// - /// The code lengths - /// The huffman value array - /// The huffman code span ref - /// The lengths of any valid codes - private void GenerateLookaheadTables(ReadOnlySpan lengths, ReadOnlySpan huffval, ref short huffcodeRef, int k) - { - // TODO: Rewrite this to match stb_Image - // TODO: This generation code matches the libJpeg code but the lookahead table is not actually used yet. - // To use it we need to implement fast lookup path in PdfJsScanDecoder.DecodeHuffman - // This should yield much faster scan decoding as usually, more than 95% of the Huffman codes - // will be 8 or fewer bits long and can be handled without looping. - fixed (byte* lookaheadRef = this.Lookahead.Data) - { - const int FastBits = ScanDecoder.FastBits; - var lookaheadSpan = new Span(lookaheadRef, 1 << ScanDecoder.FastBits); - - lookaheadSpan.Fill(byte.MaxValue); // Flag for non-accelerated - fixed (short* sizesRef = this.Sizes.Data) - { - for (int i = 0; i < k; ++i) - { - int s = sizesRef[i]; - if (s <= ScanDecoder.FastBits) - { - int c = Unsafe.Add(ref huffcodeRef, i) << (FastBits - s); - int m = 1 << (FastBits - s); - for (int j = 0; j < m; ++j) - { - lookaheadRef[c + j] = (byte)i; - } - } - } - } - } - } } } \ No newline at end of file From 84958a8f167d6229b2bf17c5554494fe0f47b09e Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 1 Jul 2018 19:48:16 +1000 Subject: [PATCH 14/33] Delete unused code. --- .../Jpeg/PdfJsPort/Components/FastACTables.cs | 2 +- .../Components/FixedByteBuffer257.cs | 24 - .../Components/FixedInt16Buffer256.cs | 24 - .../Components/PdfJsFrameComponent.cs | 2 +- .../PdfJsPort/Components/PdfJsHuffmanTable.cs | 2 +- .../PdfJsPort/Components/PdfJsScanDecoder.cs | 866 ------------------ .../Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs | 16 - 7 files changed, 3 insertions(+), 933 deletions(-) delete mode 100644 src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedByteBuffer257.cs delete mode 100644 src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt16Buffer256.cs delete mode 100644 src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FastACTables.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FastACTables.cs index 1e608cf7aa..f936f73426 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FastACTables.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FastACTables.cs @@ -7,7 +7,7 @@ using SixLabors.Memory; namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { /// - /// The collection of tables used for fast AC entropy scan decoding. + /// The collection of lookup tables used for fast AC entropy scan decoding. /// internal sealed class FastACTables : IDisposable { diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedByteBuffer257.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedByteBuffer257.cs deleted file mode 100644 index 3015243168..0000000000 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedByteBuffer257.cs +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components -{ - [StructLayout(LayoutKind.Sequential)] - internal unsafe struct FixedByteBuffer257 - { - public fixed byte Data[257]; - - public byte this[int idx] - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - ref byte self = ref Unsafe.As(ref this); - return Unsafe.Add(ref self, idx); - } - } - } -} \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt16Buffer256.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt16Buffer256.cs deleted file mode 100644 index 2c16a918f4..0000000000 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt16Buffer256.cs +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components -{ - [StructLayout(LayoutKind.Sequential)] - internal unsafe struct FixedInt16Buffer256 - { - public fixed short Data[256]; - - public short this[int idx] - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - ref short self = ref Unsafe.As(ref this); - return Unsafe.Add(ref self, idx); - } - } - } -} \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsFrameComponent.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsFrameComponent.cs index eefe8b97ea..1a10adf883 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsFrameComponent.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsFrameComponent.cs @@ -129,7 +129,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components this.SubSamplingDivisors = c0.SamplingFactors.DivideBy(this.SamplingFactors); } - this.SpectralBlocks = this.memoryAllocator.Allocate2D(blocksPerColumnForMcu, blocksPerLineForMcu + 1, true); + this.SpectralBlocks = this.memoryAllocator.AllocateClean2D(blocksPerColumnForMcu, blocksPerLineForMcu + 1); } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs index e843ebbaca..3babb449a4 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs @@ -88,7 +88,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } // Figure F.15: generate decoding tables for bit-sequential decoding. - // Compute largest code + 1 for this size. preshifted as neeed later. + // Compute largest code + 1 for this size. preshifted as need later. maxcodeRef[j] = code << (16 - j); code <<= 1; } diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs deleted file mode 100644 index d524fa5d84..0000000000 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs +++ /dev/null @@ -1,866 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System; - -#if DEBUG -using System.Diagnostics; -#endif -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using SixLabors.ImageSharp.Formats.Jpeg.Components; -using SixLabors.Memory; - -namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components -{ - /// - /// Provides the means to decode a spectral scan - /// - internal struct PdfJsScanDecoder - { - private ZigZag dctZigZag; - - private byte[] markerBuffer; - - private int mcuToRead; - - private int mcusPerLine; - - private int mcu; - - private int bitsData; - - private int bitsCount; - - private int specStart; - - private int specEnd; - - private int eobrun; - - private int compIndex; - - private int successiveState; - - private int successiveACState; - - private int successiveACNextValue; - - private bool endOfStreamReached; - - private bool unexpectedMarkerReached; - - /// - /// Decodes the spectral scan - /// - /// The image frame - /// The input stream - /// The DC Huffman tables - /// The AC Huffman tables - /// The scan components - /// The component index within the array - /// 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 - public void DecodeScan( - PdfJsFrame frame, - DoubleBufferedStreamReader stream, - PdfJsHuffmanTables dcHuffmanTables, - PdfJsHuffmanTables acHuffmanTables, - PdfJsFrameComponent[] components, - int componentIndex, - int componentsLength, - ushort resetInterval, - int spectralStart, - int spectralEnd, - int successivePrev, - int successive) - { - this.dctZigZag = ZigZag.CreateUnzigTable(); - this.markerBuffer = new byte[2]; - this.compIndex = componentIndex; - this.specStart = spectralStart; - this.specEnd = spectralEnd; - this.successiveState = successive; - this.endOfStreamReached = false; - this.unexpectedMarkerReached = false; - - bool progressive = frame.Progressive; - this.mcusPerLine = frame.McusPerLine; - - this.mcu = 0; - int mcuExpected; - if (componentsLength == 1) - { - mcuExpected = components[this.compIndex].WidthInBlocks * components[this.compIndex].HeightInBlocks; - } - else - { - mcuExpected = this.mcusPerLine * frame.McusPerColumn; - } - - while (this.mcu < mcuExpected) - { - // Reset interval stuff - this.mcuToRead = resetInterval != 0 ? Math.Min(mcuExpected - this.mcu, resetInterval) : mcuExpected; - for (int i = 0; i < components.Length; i++) - { - PdfJsFrameComponent c = components[i]; - c.DcPredictor = 0; - } - - this.eobrun = 0; - - if (!progressive) - { - this.DecodeScanBaseline(dcHuffmanTables, acHuffmanTables, components, componentsLength, stream); - } - else - { - bool isAc = this.specStart != 0; - bool isFirst = successivePrev == 0; - PdfJsHuffmanTables huffmanTables = isAc ? acHuffmanTables : dcHuffmanTables; - this.DecodeScanProgressive(huffmanTables, isAc, isFirst, components, componentsLength, stream); - } - - // Reset - // TODO: I do not understand why these values are reset? We should surely be tracking the bits across mcu's? - this.bitsCount = 0; - this.bitsData = 0; - this.unexpectedMarkerReached = false; - - // Some images include more scan blocks than expected, skip past those and - // attempt to find the next valid marker - PdfJsFileMarker fileMarker = PdfJsJpegDecoderCore.FindNextFileMarker(this.markerBuffer, stream); - byte marker = fileMarker.Marker; - - // RSTn - We've already read the bytes and altered the position so no need to skip - if (marker >= JpegConstants.Markers.RST0 && marker <= JpegConstants.Markers.RST7) - { - continue; - } - - if (!fileMarker.Invalid) - { - // We've found a valid marker. - // Rewind the stream to the position of the marker and break - stream.Position = fileMarker.Position; - break; - } - -#if DEBUG - Debug.WriteLine($"DecodeScan - Unexpected MCU data at {stream.Position}, next marker is: {fileMarker.Marker:X}"); -#endif - } - } - - private void DecodeScanBaseline( - PdfJsHuffmanTables dcHuffmanTables, - PdfJsHuffmanTables acHuffmanTables, - PdfJsFrameComponent[] components, - int componentsLength, - DoubleBufferedStreamReader stream) - { - if (componentsLength == 1) - { - PdfJsFrameComponent component = components[this.compIndex]; - ref short blockDataRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast(component.SpectralBlocks.GetSpan())); - ref PdfJsHuffmanTable dcHuffmanTable = ref dcHuffmanTables[component.DCHuffmanTableId]; - ref PdfJsHuffmanTable acHuffmanTable = ref acHuffmanTables[component.ACHuffmanTableId]; - - for (int n = 0; n < this.mcuToRead; n++) - { - if (this.endOfStreamReached || this.unexpectedMarkerReached) - { - continue; - } - - this.DecodeBlockBaseline(ref dcHuffmanTable, ref acHuffmanTable, component, ref blockDataRef, stream); - this.mcu++; - } - } - else - { - for (int n = 0; n < this.mcuToRead; n++) - { - for (int i = 0; i < componentsLength; i++) - { - PdfJsFrameComponent component = components[i]; - ref short blockDataRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast(component.SpectralBlocks.GetSpan())); - ref PdfJsHuffmanTable dcHuffmanTable = ref dcHuffmanTables[component.DCHuffmanTableId]; - ref PdfJsHuffmanTable acHuffmanTable = ref acHuffmanTables[component.ACHuffmanTableId]; - int h = component.HorizontalSamplingFactor; - int v = component.VerticalSamplingFactor; - - for (int j = 0; j < v; j++) - { - for (int k = 0; k < h; k++) - { - if (this.endOfStreamReached || this.unexpectedMarkerReached) - { - continue; - } - - this.DecodeMcuBaseline(ref dcHuffmanTable, ref acHuffmanTable, component, ref blockDataRef, j, k, stream); - } - } - } - - this.mcu++; - } - } - } - - private void DecodeScanProgressive( - PdfJsHuffmanTables huffmanTables, - bool isAC, - bool isFirst, - PdfJsFrameComponent[] components, - int componentsLength, - DoubleBufferedStreamReader stream) - { - if (componentsLength == 1) - { - PdfJsFrameComponent component = components[this.compIndex]; - ref short blockDataRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast(component.SpectralBlocks.GetSpan())); - ref PdfJsHuffmanTable huffmanTable = ref huffmanTables[isAC ? component.ACHuffmanTableId : component.DCHuffmanTableId]; - - for (int n = 0; n < this.mcuToRead; n++) - { - if (this.endOfStreamReached || this.unexpectedMarkerReached) - { - continue; - } - - if (isAC) - { - if (isFirst) - { - this.DecodeBlockACFirst(ref huffmanTable, component, ref blockDataRef, stream); - } - else - { - this.DecodeBlockACSuccessive(ref huffmanTable, component, ref blockDataRef, stream); - } - } - else - { - if (isFirst) - { - this.DecodeBlockDCFirst(ref huffmanTable, component, ref blockDataRef, stream); - } - else - { - this.DecodeBlockDCSuccessive(component, ref blockDataRef, stream); - } - } - - this.mcu++; - } - } - else - { - for (int n = 0; n < this.mcuToRead; n++) - { - for (int i = 0; i < componentsLength; i++) - { - PdfJsFrameComponent component = components[i]; - ref short blockDataRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast(component.SpectralBlocks.GetSpan())); - ref PdfJsHuffmanTable huffmanTable = ref huffmanTables[isAC ? component.ACHuffmanTableId : component.DCHuffmanTableId]; - int h = component.HorizontalSamplingFactor; - int v = component.VerticalSamplingFactor; - - for (int j = 0; j < v; j++) - { - for (int k = 0; k < h; k++) - { - // No need to continue here. - if (this.endOfStreamReached || this.unexpectedMarkerReached) - { - break; - } - - if (isAC) - { - if (isFirst) - { - this.DecodeMcuACFirst(ref huffmanTable, component, ref blockDataRef, j, k, stream); - } - else - { - this.DecodeMcuACSuccessive(ref huffmanTable, component, ref blockDataRef, j, k, stream); - } - } - else - { - if (isFirst) - { - this.DecodeMcuDCFirst(ref huffmanTable, component, ref blockDataRef, j, k, stream); - } - else - { - this.DecodeMcuDCSuccessive(component, ref blockDataRef, j, k, stream); - } - } - } - } - } - - this.mcu++; - } - } - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DecodeBlockBaseline(ref PdfJsHuffmanTable dcHuffmanTable, ref PdfJsHuffmanTable acHuffmanTable, PdfJsFrameComponent component, ref short blockDataRef, DoubleBufferedStreamReader stream) - { - int blockRow = this.mcu / component.WidthInBlocks; - int blockCol = this.mcu % component.WidthInBlocks; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); - this.DecodeBaseline(component, ref blockDataRef, offset, ref dcHuffmanTable, ref acHuffmanTable, stream); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DecodeMcuBaseline(ref PdfJsHuffmanTable dcHuffmanTable, ref PdfJsHuffmanTable acHuffmanTable, PdfJsFrameComponent component, ref short blockDataRef, int row, int col, DoubleBufferedStreamReader stream) - { - int mcuRow = this.mcu / this.mcusPerLine; - int mcuCol = this.mcu % this.mcusPerLine; - int blockRow = (mcuRow * component.VerticalSamplingFactor) + row; - int blockCol = (mcuCol * component.HorizontalSamplingFactor) + col; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); - this.DecodeBaseline(component, ref blockDataRef, offset, ref dcHuffmanTable, ref acHuffmanTable, stream); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DecodeBlockDCFirst(ref PdfJsHuffmanTable dcHuffmanTable, PdfJsFrameComponent component, ref short blockDataRef, DoubleBufferedStreamReader stream) - { - int blockRow = this.mcu / component.WidthInBlocks; - int blockCol = this.mcu % component.WidthInBlocks; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); - this.DecodeDCFirst(component, ref blockDataRef, offset, ref dcHuffmanTable, stream); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DecodeMcuDCFirst(ref PdfJsHuffmanTable dcHuffmanTable, PdfJsFrameComponent component, ref short blockDataRef, int row, int col, DoubleBufferedStreamReader stream) - { - int mcuRow = this.mcu / this.mcusPerLine; - int mcuCol = this.mcu % this.mcusPerLine; - int blockRow = (mcuRow * component.VerticalSamplingFactor) + row; - int blockCol = (mcuCol * component.HorizontalSamplingFactor) + col; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); - this.DecodeDCFirst(component, ref blockDataRef, offset, ref dcHuffmanTable, stream); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DecodeBlockDCSuccessive(PdfJsFrameComponent component, ref short blockDataRef, DoubleBufferedStreamReader stream) - { - int blockRow = this.mcu / component.WidthInBlocks; - int blockCol = this.mcu % component.WidthInBlocks; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); - this.DecodeDCSuccessive(component, ref blockDataRef, offset, stream); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DecodeMcuDCSuccessive(PdfJsFrameComponent component, ref short blockDataRef, int row, int col, DoubleBufferedStreamReader stream) - { - int mcuRow = this.mcu / this.mcusPerLine; - int mcuCol = this.mcu % this.mcusPerLine; - int blockRow = (mcuRow * component.VerticalSamplingFactor) + row; - int blockCol = (mcuCol * component.HorizontalSamplingFactor) + col; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); - this.DecodeDCSuccessive(component, ref blockDataRef, offset, stream); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DecodeBlockACFirst(ref PdfJsHuffmanTable acHuffmanTable, PdfJsFrameComponent component, ref short blockDataRef, DoubleBufferedStreamReader stream) - { - int blockRow = this.mcu / component.WidthInBlocks; - int blockCol = this.mcu % component.WidthInBlocks; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); - this.DecodeACFirst(ref blockDataRef, offset, ref acHuffmanTable, stream); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DecodeMcuACFirst(ref PdfJsHuffmanTable acHuffmanTable, PdfJsFrameComponent component, ref short blockDataRef, int row, int col, DoubleBufferedStreamReader stream) - { - int mcuRow = this.mcu / this.mcusPerLine; - int mcuCol = this.mcu % this.mcusPerLine; - int blockRow = (mcuRow * component.VerticalSamplingFactor) + row; - int blockCol = (mcuCol * component.HorizontalSamplingFactor) + col; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); - this.DecodeACFirst(ref blockDataRef, offset, ref acHuffmanTable, stream); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DecodeBlockACSuccessive(ref PdfJsHuffmanTable acHuffmanTable, PdfJsFrameComponent component, ref short blockDataRef, DoubleBufferedStreamReader stream) - { - int blockRow = this.mcu / component.WidthInBlocks; - int blockCol = this.mcu % component.WidthInBlocks; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); - this.DecodeACSuccessive(ref blockDataRef, offset, ref acHuffmanTable, stream); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DecodeMcuACSuccessive(ref PdfJsHuffmanTable acHuffmanTable, PdfJsFrameComponent component, ref short blockDataRef, int row, int col, DoubleBufferedStreamReader stream) - { - int mcuRow = this.mcu / this.mcusPerLine; - int mcuCol = this.mcu % this.mcusPerLine; - int blockRow = (mcuRow * component.VerticalSamplingFactor) + row; - int blockCol = (mcuCol * component.HorizontalSamplingFactor) + col; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); - this.DecodeACSuccessive(ref blockDataRef, offset, ref acHuffmanTable, stream); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private bool TryReadBit(DoubleBufferedStreamReader stream, out int bit) - { - if (this.bitsCount == 0) - { - if (!this.TryFillBits(stream)) - { - bit = 0; - return false; - } - } - - this.bitsCount--; - bit = (this.bitsData >> this.bitsCount) & 1; - return true; - } - - [MethodImpl(MethodImplOptions.NoInlining)] - private bool TryFillBits(DoubleBufferedStreamReader stream) - { - // TODO: Read more then 1 byte at a time. - // In LibJpegTurbo this is be 25 bits (32-7) but I cannot get this to work - // for some images, I'm assuming because I am crossing MCU boundaries and not maintining the correct buffer state. - const int MinGetBits = 7; - - if (!this.unexpectedMarkerReached) - { - // Attempt to load to the minimum bit count. - while (this.bitsCount < MinGetBits) - { - int c = stream.ReadByte(); - - switch (c) - { - case -0x1: - - // We've encountered the end of the file stream which means there's no EOI marker in the image. - this.endOfStreamReached = true; - return false; - - case JpegConstants.Markers.XFF: - int nextByte = stream.ReadByte(); - - if (nextByte == -0x1) - { - this.endOfStreamReached = true; - return false; - } - - if (nextByte != 0) - { -#if DEBUG - Debug.WriteLine($"DecodeScan - Unexpected marker {(c << 8) | nextByte:X} at {stream.Position}"); -#endif - - // We've encountered an unexpected marker. Reverse the stream and exit. - this.unexpectedMarkerReached = true; - stream.Position -= 2; - - // TODO: double check we need this. - // Fill buffer with zero bits. - if (this.bitsCount == 0) - { - this.bitsData <<= MinGetBits; - this.bitsCount = MinGetBits; - } - - return true; - } - - break; - } - - // OK, load the next byte into bitsData - this.bitsData = (this.bitsData << 8) | c; - this.bitsCount += 8; - } - } - - return true; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private int PeekBits(int count) - { - return this.bitsData >> (this.bitsCount - count) & ((1 << count) - 1); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DropBits(int count) - { - this.bitsCount -= count; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private bool TryDecodeHuffman(ref PdfJsHuffmanTable tree, DoubleBufferedStreamReader stream, out short value) - { - value = -1; - - // TODO: Implement fast Huffman decoding. - // In LibJpegTurbo a minimum of 25 bits (32-7) is collected from the stream - // Then a LUT is used to avoid the loop when decoding the Huffman value. - // using 3 methods: FillBits, PeekBits, and DropBits. - // The LUT has been ported from LibJpegTurbo as has this code but it doesn't work. - // this.TryFillBits(stream); - // - // const int LookAhead = 8; - // int look = this.PeekBits(LookAhead); - // look = tree.Lookahead[look]; - // int bits = look >> LookAhead; - // - // if (bits <= LookAhead) - // { - // this.DropBits(bits); - // value = (short)(look & ((1 << LookAhead) - 1)); - // return true; - // } - if (!this.TryReadBit(stream, out int bit)) - { - return false; - } - - short code = (short)bit; - - // "DECODE", section F.2.2.3, figure F.16, page 109 of T.81 - int i = 1; - - while (code > tree.MaxCode[i]) - { - if (!this.TryReadBit(stream, out bit)) - { - return false; - } - - code <<= 1; - code |= (short)bit; - i++; - } - - int j = tree.ValOffset[i]; - value = tree.Values[(j + code) & 0xFF]; - return true; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private bool TryReceive(int length, DoubleBufferedStreamReader stream, out int value) - { - value = 0; - while (length > 0) - { - if (!this.TryReadBit(stream, out int bit)) - { - return false; - } - - value = (value << 1) | bit; - length--; - } - - return true; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private bool TryReceiveAndExtend(int length, DoubleBufferedStreamReader stream, out int value) - { - if (length == 1) - { - if (!this.TryReadBit(stream, out value)) - { - return false; - } - - value = value == 1 ? 1 : -1; - } - else - { - if (!this.TryReceive(length, stream, out value)) - { - return false; - } - - if (value < 1 << (length - 1)) - { - value += (-1 << length) + 1; - } - } - - return true; - } - - private void DecodeBaseline(PdfJsFrameComponent component, ref short blockDataRef, int offset, ref PdfJsHuffmanTable dcHuffmanTable, ref PdfJsHuffmanTable acHuffmanTable, DoubleBufferedStreamReader stream) - { - if (!this.TryDecodeHuffman(ref dcHuffmanTable, stream, out short t)) - { - return; - } - - int diff = 0; - if (t != 0) - { - if (!this.TryReceiveAndExtend(t, stream, out diff)) - { - return; - } - } - - Unsafe.Add(ref blockDataRef, offset) = (short)(component.DcPredictor += diff); - - int k = 1; - while (k < 64) - { - if (!this.TryDecodeHuffman(ref acHuffmanTable, stream, out short rs)) - { - return; - } - - int s = rs & 15; - int r = rs >> 4; - - if (s == 0) - { - if (r < 15) - { - break; - } - - k += 16; - continue; - } - - k += r; - - byte z = this.dctZigZag[k]; - - if (!this.TryReceiveAndExtend(s, stream, out int re)) - { - return; - } - - Unsafe.Add(ref blockDataRef, offset + z) = (short)re; - k++; - } - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DecodeDCFirst(PdfJsFrameComponent component, ref short blockDataRef, int offset, ref PdfJsHuffmanTable dcHuffmanTable, DoubleBufferedStreamReader stream) - { - if (!this.TryDecodeHuffman(ref dcHuffmanTable, stream, out short t)) - { - return; - } - - int diff = 0; - if (t != 0) - { - if (!this.TryReceiveAndExtend(t, stream, out diff)) - { - return; - } - } - - Unsafe.Add(ref blockDataRef, offset) = (short)(component.DcPredictor += diff << this.successiveState); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DecodeDCSuccessive(PdfJsFrameComponent component, ref short blockDataRef, int offset, DoubleBufferedStreamReader stream) - { - if (!this.TryReadBit(stream, out int bit)) - { - return; - } - - Unsafe.Add(ref blockDataRef, offset) |= (short)(bit << this.successiveState); - } - - private void DecodeACFirst(ref short blockDataRef, int offset, ref PdfJsHuffmanTable acHuffmanTable, DoubleBufferedStreamReader stream) - { - if (this.eobrun > 0) - { - this.eobrun--; - return; - } - - int k = this.specStart; - int e = this.specEnd; - while (k <= e) - { - if (!this.TryDecodeHuffman(ref acHuffmanTable, stream, out short rs)) - { - return; - } - - int s = rs & 15; - int r = rs >> 4; - - if (s == 0) - { - if (r < 15) - { - if (!this.TryReceive(r, stream, out int eob)) - { - return; - } - - this.eobrun = eob + (1 << r) - 1; - break; - } - - k += 16; - continue; - } - - k += r; - - byte z = this.dctZigZag[k]; - - if (!this.TryReceiveAndExtend(s, stream, out int v)) - { - return; - } - - Unsafe.Add(ref blockDataRef, offset + z) = (short)(v * (1 << this.successiveState)); - k++; - } - } - - private void DecodeACSuccessive(ref short blockDataRef, int offset, ref PdfJsHuffmanTable acHuffmanTable, DoubleBufferedStreamReader stream) - { - int k = this.specStart; - int e = this.specEnd; - int r = 0; - - while (k <= e) - { - int offsetZ = offset + this.dctZigZag[k]; - ref short blockOffsetZRef = ref Unsafe.Add(ref blockDataRef, offsetZ); - int sign = blockOffsetZRef < 0 ? -1 : 1; - - switch (this.successiveACState) - { - case 0: // Initial state - - if (!this.TryDecodeHuffman(ref acHuffmanTable, stream, out short rs)) - { - return; - } - - int s = rs & 15; - r = rs >> 4; - if (s == 0) - { - if (r < 15) - { - if (!this.TryReceive(r, stream, out int eob)) - { - return; - } - - this.eobrun = eob + (1 << r); - this.successiveACState = 4; - } - else - { - r = 16; - this.successiveACState = 1; - } - } - else - { - if (s != 1) - { - throw new ImageFormatException("Invalid ACn encoding"); - } - - if (!this.TryReceiveAndExtend(s, stream, out int v)) - { - return; - } - - this.successiveACNextValue = v; - this.successiveACState = r > 0 ? 2 : 3; - } - - continue; - case 1: // Skipping r zero items - case 2: - if (blockOffsetZRef != 0) - { - if (!this.TryReadBit(stream, out int bit)) - { - return; - } - - blockOffsetZRef += (short)(sign * (bit << this.successiveState)); - } - else - { - r--; - if (r == 0) - { - this.successiveACState = this.successiveACState == 2 ? 3 : 0; - } - } - - break; - case 3: // Set value for a zero item - if (blockOffsetZRef != 0) - { - if (!this.TryReadBit(stream, out int bit)) - { - return; - } - - blockOffsetZRef += (short)(sign * (bit << this.successiveState)); - } - else - { - blockOffsetZRef = (short)(this.successiveACNextValue << this.successiveState); - this.successiveACState = 0; - } - - break; - case 4: // Eob - if (blockOffsetZRef != 0) - { - if (!this.TryReadBit(stream, out int bit)) - { - return; - } - - blockOffsetZRef += (short)(sign * (bit << this.successiveState)); - } - - break; - } - - k++; - } - - if (this.successiveACState == 4) - { - this.eobrun--; - if (this.eobrun == 0) - { - this.successiveACState = 0; - } - } - } - } -} \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs index b71667300a..86ac6b195a 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs @@ -816,22 +816,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort successiveApproximation & 15); sd.ParseEntropyCodedData(this.Frame, this.dcHuffmanTables, this.acHuffmanTables, this.fastACTables); - - // PdfJsScanDecoder scanDecoder = default; - // - // scanDecoder.DecodeScan( - // this.Frame, - // this.InputStream, - // this.dcHuffmanTables, - // this.acHuffmanTables, - // this.Frame.Components, - // componentIndex, - // selectorsCount, - // this.resetInterval, - // spectralStart, - // spectralEnd, - // successiveApproximation >> 4, - // successiveApproximation & 15); } /// From f8d4c2a81282a3af12954b945a9e502a1a788441 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 1 Jul 2018 19:59:14 +1000 Subject: [PATCH 15/33] Update reference images from master --- tests/Images/External | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Images/External b/tests/Images/External index eb40b3c039..d9d93bbdd1 160000 --- a/tests/Images/External +++ b/tests/Images/External @@ -1 +1 @@ -Subproject commit eb40b3c039dd8c8ca448cb8073a59ca178901e9f +Subproject commit d9d93bbdd18dd7b818c0d19cc8f967be98045d3c From 86addf3a9d1cb08fb92ac0a1e1e9dca5408f53eb Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 2 Jul 2018 10:27:26 +1000 Subject: [PATCH 16/33] Add descriptive comments, remove unused, and make method static. --- .../Jpeg/PdfJsPort/Components/ScanDecoder.cs | 43 ++++++++++++++----- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs index 8a39dab344..42cf38f2a8 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs @@ -1,7 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using SixLabors.ImageSharp.Formats.Jpeg.Components; @@ -11,12 +10,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { internal class ScanDecoder { + // The number of bits that can be read via a LUT. public const int FastBits = 9; - // bmask[n] = (1 << n) - 1 + // LUT Bmask[n] = (1 << n) - 1 private static readonly uint[] Bmask = { 0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535 }; - // bias[n] = (-1 << n) + 1 + // LUT Bias[n] = (-1 << n) + 1 private static readonly int[] Bias = { 0, -1, -3, -7, -15, -31, -63, -127, -255, -511, -1023, -2047, -4095, -8191, -16383, -32767 }; private readonly DoubleBufferedStreamReader stream; @@ -25,19 +25,44 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components private readonly int restartInterval; private readonly int componentIndex; private readonly int componentsLength; + + // The spectral selection start. private readonly int spectralStart; + + // The spectral selection end. private readonly int spectralEnd; + + // The successive approximation high bit end. private readonly int successiveHigh; + + // The successive approximation low bit end. private readonly int successiveLow; + // The number of valid bits left to read in the buffer. private int codeBits; + + // The entropy encoded code buffer. private uint codeBuffer; + + // Whether there is more data to pull from the stream for the current mcu. private bool nomore; + + // Whether we have prematurely reached the end of the file. private bool eof; + + // The current, if any, marker in the input stream. private byte marker; + + // Whether we have a bad marker, ie. one that is not between RST0 and RST7 private bool badMarker; + + // The opening position of an identified marker. private long markerPosition; + + // 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; /// @@ -230,6 +255,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint LRot(uint x, int y) => (x << y) | (x >> (32 - y)); + private void ParseProgressiveData( PdfJsFrame frame, PdfJsHuffmanTables dcHuffmanTables, @@ -312,8 +340,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components 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]; - ReadOnlySpan fastAC = fastACTables.Tables.GetRowSpan(component.ACHuffmanTableId); int h = component.HorizontalSamplingFactor; int v = component.VerticalSamplingFactor; @@ -678,7 +704,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components this.GrowBufferUnsafe(); } - uint k = this.LRot(this.codeBuffer, n); + uint k = LRot(this.codeBuffer, n); this.codeBuffer = k & ~Bmask[n]; k &= Bmask[n]; this.codeBits -= n; @@ -822,7 +848,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } int sgn = (int)this.codeBuffer >> 31; - uint k = this.LRot(this.codeBuffer, n); + uint k = LRot(this.codeBuffer, n); this.codeBuffer = k & ~Bmask[n]; k &= Bmask[n]; this.codeBits -= n; @@ -841,9 +867,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components [MethodImpl(MethodImplOptions.AggressiveInlining)] private int PeekBits() => (int)((this.codeBuffer >> (32 - FastBits)) & ((1 << FastBits) - 1)); - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private uint LRot(uint x, int y) => (x << y) | (x >> (32 - y)); - [MethodImpl(MethodImplOptions.AggressiveInlining)] private bool ContinueOnRestart() { From 36081fd5692f116f720cc776243458b5c46399c7 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 2 Jul 2018 11:44:50 +1000 Subject: [PATCH 17/33] Split progressive AC method and rename GrowBufferUnsafe --- .../Jpeg/PdfJsPort/Components/ScanDecoder.cs | 215 ++++++++++-------- 1 file changed, 117 insertions(+), 98 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs index 42cf38f2a8..4fdac53735 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs @@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components // The number of bits that can be read via a LUT. public const int FastBits = 9; - // LUT Bmask[n] = (1 << n) - 1 + // LUT mask for n rightmost bits. Bmask[n] = (1 << n) - 1 private static readonly uint[] Bmask = { 0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535 }; // LUT Bias[n] = (-1 << n) + 1 @@ -22,8 +22,14 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components private readonly DoubleBufferedStreamReader stream; private readonly PdfJsFrameComponent[] components; private readonly ZigZag dctZigZag; + + // The restart interval. private readonly int restartInterval; + + // The current component index. private readonly int componentIndex; + + // The number of interleaved components. private readonly int componentsLength; // The spectral selection start. @@ -53,7 +59,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components // The current, if any, marker in the input stream. private byte marker; - // Whether we have a bad marker, ie. one that is not between RST0 and RST7 + // Whether we have a bad marker, I.E. One that is not between RST0 and RST7 private bool badMarker; // The opening position of an identified marker. @@ -68,15 +74,15 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components /// /// Initializes a new instance of the class. /// - /// The input stream - /// The scan components - /// The component index within the array - /// 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 + /// The input stream. + /// The scan components. + /// The component index within the array. + /// 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. public ScanDecoder( DoubleBufferedStreamReader stream, PdfJsFrameComponent[] components, @@ -174,7 +180,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { if (this.codeBits < 24) { - this.GrowBufferUnsafe(); + this.FillBuffer(); } // If it's NOT a restart, then just bail, so we get corrupt data @@ -238,7 +244,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { if (this.codeBits < 24) { - this.GrowBufferUnsafe(); + this.FillBuffer(); } // If it's NOT a restart, then just bail, so we get corrupt data @@ -309,7 +315,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { if (this.codeBits < 24) { - this.GrowBufferUnsafe(); + this.FillBuffer(); } // If it's NOT a restart, then just bail, so we get corrupt data @@ -371,7 +377,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { if (this.codeBits < 24) { - this.GrowBufferUnsafe(); + this.FillBuffer(); } // If it's NOT a restart, then just bail, so we get corrupt data @@ -502,8 +508,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components ref PdfJsHuffmanTable acTable, ref short fastACRef) { - int k; - if (this.spectralStart == 0) { throw new ImageFormatException("Can't merge DC and AC."); @@ -511,6 +515,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components if (this.successiveHigh == 0) { + // MCU decoding for AC initial scan (either spectral selection, + // or first pass of successive approximation). int shift = this.successiveLow; if (this.eobrun != 0) @@ -519,7 +525,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components return; } - k = this.spectralStart; + int k = this.spectralStart; do { int zig; @@ -582,117 +588,125 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components else { // Refinement scan for these AC coefficients - short bit = (short)(1 << this.successiveLow); + this.DecodeBlockProgressiveACRefined(ref blockDataRef, ref acTable); + } + } - if (this.eobrun != 0) + private void DecodeBlockProgressiveACRefined(ref short blockDataRef, ref PdfJsHuffmanTable acTable) + { + int k; + + // Refinement scan for these AC coefficients + short bit = (short)(1 << this.successiveLow); + + if (this.eobrun != 0) + { + this.eobrun--; + for (k = this.spectralStart; k <= this.spectralEnd; k++) { - this.eobrun--; - for (k = this.spectralStart; k <= this.spectralEnd; k++) + ref short p = ref Unsafe.Add(ref blockDataRef, this.dctZigZag[k]); + if (p != 0) { - ref short p = ref Unsafe.Add(ref blockDataRef, this.dctZigZag[k]); - if (p != 0) + if (this.GetBit() != 0) { - if (this.GetBit() != 0) + if ((p & bit) == 0) { - if ((p & bit) == 0) + if (p > 0) { - if (p > 0) - { - p += bit; - } - else - { - p -= bit; - } + p += bit; + } + else + { + p -= bit; } } } } } - else + } + else + { + k = this.spectralStart; + do { - k = this.spectralStart; - do + int rs = this.DecodeHuffman(ref acTable); + if (rs < 0) { - int rs = this.DecodeHuffman(ref acTable); - if (rs < 0) - { - throw new ImageFormatException("Bad Huffman code."); - } + throw new ImageFormatException("Bad Huffman code."); + } - int s = rs & 15; - int r = rs >> 4; + int s = rs & 15; + int r = rs >> 4; - if (s == 0) + if (s == 0) + { + // r=15 s=0 should write 16 0s, so we just do + // a run of 15 0s and then write s (which is 0), + // so we don't have to do anything special here + if (r < 15) { - // r=15 s=0 should write 16 0s, so we just do - // a run of 15 0s and then write s (which is 0), - // so we don't have to do anything special here - if (r < 15) + this.eobrun = (1 << r) - 1; + + if (r != 0) { - this.eobrun = (1 << r) - 1; + this.eobrun += this.GetBits(r); + } - if (r != 0) - { - this.eobrun += this.GetBits(r); - } + r = 64; // Force end of block + } + } + else + { + if (s != 1) + { + throw new ImageFormatException("Bad Huffman code."); + } - r = 64; // Force end of block - } + // Sign bit + if (this.GetBit() != 0) + { + s = bit; } else { - if (s != 1) - { - throw new ImageFormatException("Bad Huffman code."); - } - - // Sign bit - if (this.GetBit() != 0) - { - s = bit; - } - else - { - s = -bit; - } + s = -bit; } + } - // Advance by r - while (k <= this.spectralEnd) + // Advance by r + while (k <= this.spectralEnd) + { + ref short p = ref Unsafe.Add(ref blockDataRef, this.dctZigZag[k++]); + if (p != 0) { - ref short p = ref Unsafe.Add(ref blockDataRef, this.dctZigZag[k++]); - if (p != 0) + if (this.GetBit() != 0) { - if (this.GetBit() != 0) + if ((p & bit) == 0) { - if ((p & bit) == 0) + if (p > 0) + { + p += bit; + } + else { - if (p > 0) - { - p += bit; - } - else - { - p -= bit; - } + p -= bit; } } } - else + } + else + { + if (r == 0) { - if (r == 0) - { - p = (short)s; - break; - } - - r--; + p = (short)s; + break; } + + r--; } } - while (k <= this.spectralEnd); } + while (k <= this.spectralEnd); } } @@ -701,7 +715,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { if (this.codeBits < n) { - this.GrowBufferUnsafe(); + this.FillBuffer(); } uint k = LRot(this.codeBuffer, n); @@ -716,7 +730,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { if (this.codeBits < 1) { - this.GrowBufferUnsafe(); + this.FillBuffer(); } uint k = this.codeBuffer; @@ -727,18 +741,23 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } [MethodImpl(MethodImplOptions.NoInlining)] - private void GrowBufferUnsafe() + private void FillBuffer() { + // Attempt to load at least the minimum nbumber of required bits into the buffer. + // We fail to do so only if we hit a marker or reach the end of the input stream. do { int b = this.nomore ? 0 : this.stream.ReadByte(); if (b == -1) { + // We've encountered the end of the file stream which means there's no EOI marker in the image + // or the SOS marker has the wrong dimensions set. this.eof = true; b = 0; } + // Found a marker. if (b == JpegConstants.Markers.XFF) { this.markerPosition = this.stream.Position - 1; @@ -844,7 +863,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { if (this.codeBits < n) { - this.GrowBufferUnsafe(); + this.FillBuffer(); } int sgn = (int)this.codeBuffer >> 31; @@ -860,7 +879,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { if (this.codeBits < 16) { - this.GrowBufferUnsafe(); + this.FillBuffer(); } } From a96bcc63aaba2cd0f9997a4eb7aaf7fb9c369c85 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 2 Jul 2018 11:46:54 +1000 Subject: [PATCH 18/33] Fix updated struct name --- .../{FixedInt64Buffer18.cs => FixedUInt32Buffer18.cs} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/{FixedInt64Buffer18.cs => FixedUInt32Buffer18.cs} (80%) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt64Buffer18.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedUInt32Buffer18.cs similarity index 80% rename from src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt64Buffer18.cs rename to src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedUInt32Buffer18.cs index a9266bd6b1..9b076d9daa 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt64Buffer18.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedUInt32Buffer18.cs @@ -7,7 +7,7 @@ using System.Runtime.InteropServices; namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { [StructLayout(LayoutKind.Sequential)] - internal unsafe struct FixedInt64Buffer18 + internal unsafe struct FixedUInt32Buffer18 { public fixed uint Data[18]; @@ -16,7 +16,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - ref uint self = ref Unsafe.As(ref this); + ref uint self = ref Unsafe.As(ref this); return Unsafe.Add(ref self, idx); } } From 4df33e9a5c47e1fb2eedcd82f6dd7e6d4198f3df Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 2 Jul 2018 11:47:18 +1000 Subject: [PATCH 19/33] Update name reference --- .../Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs index 3babb449a4..a895fd0a48 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs @@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components /// /// Gets the max code array /// - public FixedInt64Buffer18 MaxCode; + public FixedUInt32Buffer18 MaxCode; /// /// Gets the value offset array From ade4131d2b5b38ec69847994a1aaf57ad41ddd8a Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 2 Jul 2018 12:32:59 +1000 Subject: [PATCH 20/33] Refactor FastACTables and reduce trivial duplication. --- .../Jpeg/PdfJsPort/Components/FastACTables.cs | 18 ++- .../Jpeg/PdfJsPort/Components/ScanDecoder.cs | 106 +++++++----------- .../Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs | 19 +++- 3 files changed, 68 insertions(+), 75 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FastACTables.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FastACTables.cs index f936f73426..6a11f28056 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FastACTables.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FastACTables.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; +using System.Runtime.CompilerServices; using SixLabors.Memory; namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components @@ -11,24 +12,33 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components /// internal sealed class FastACTables : IDisposable { + private Buffer2D tables; + /// /// Initializes a new instance of the class. /// /// The memory allocator used to allocate memory for image processing operations. public FastACTables(MemoryAllocator memoryAllocator) { - this.Tables = memoryAllocator.AllocateClean2D(512, 4); + this.tables = memoryAllocator.AllocateClean2D(512, 4); } /// - /// Gets the collection of tables. + /// Gets the representing the table at the index in the collection. /// - public Buffer2D Tables { get; } + /// The table index. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Span GetTableSpan(int index) + { + return this.tables.GetRowSpan(index); + } /// public void Dispose() { - this.Tables?.Dispose(); + this.tables?.Dispose(); + this.tables = null; } } } \ 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 4fdac53735..6c01deaa9a 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs @@ -4,10 +4,14 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using SixLabors.ImageSharp.Formats.Jpeg.Components; -using SixLabors.Memory; namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { + /// + /// Decodes the Huffman encoded spectral scan. + /// Originally ported from + /// with additional fixes for both performance and common encoding errors. + /// internal class ScanDecoder { // The number of bits that can be read via a LUT. @@ -157,7 +161,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components 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.Tables.GetRowSpan(component.ACHuffmanTableId)); + ref short fastACRef = ref MemoryMarshal.GetReference(fastACTables.GetTableSpan(component.ACHuffmanTableId)); int mcu = 0; for (int j = 0; j < h; j++) @@ -173,24 +177,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components int blockCol = mcu % w; int offset = component.GetBlockBufferOffset(blockRow, blockCol); this.DecodeBlock(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable, ref acHuffmanTable, ref fastACRef); - mcu++; // Every data block is an MCU, so countdown the restart interval - if (--this.todo <= 0) + mcu++; + if (!this.ContinueOnMcuComplete()) { - if (this.codeBits < 24) - { - this.FillBuffer(); - } - - // If it's NOT a restart, then just bail, so we get corrupt data - // rather than no data - if (!this.ContinueOnRestart()) - { - return; - } - - this.Reset(); + return; } } } @@ -212,7 +204,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components 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.Tables.GetRowSpan(component.ACHuffmanTableId)); + ref short fastACRef = ref MemoryMarshal.GetReference(fastACTables.GetTableSpan(component.ACHuffmanTableId)); int h = component.HorizontalSamplingFactor; int v = component.VerticalSamplingFactor; @@ -240,21 +232,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components // After all interleaved components, that's an interleaved MCU, // so now count down the restart interval mcu++; - if (--this.todo <= 0) + if (!this.ContinueOnMcuComplete()) { - if (this.codeBits < 24) - { - this.FillBuffer(); - } - - // If it's NOT a restart, then just bail, so we get corrupt data - // rather than no data - if (!this.ContinueOnRestart()) - { - return; - } - - this.Reset(); + return; } } } @@ -283,7 +263,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components 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.Tables.GetRowSpan(component.ACHuffmanTableId)); + ref short fastACRef = ref MemoryMarshal.GetReference(fastACTables.GetTableSpan(component.ACHuffmanTableId)); int mcu = 0; for (int j = 0; j < h; j++) @@ -308,24 +288,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components this.DecodeBlockProgressiveAC(ref Unsafe.Add(ref blockDataRef, offset), ref acHuffmanTable, ref fastACRef); } - mcu++; - // Every data block is an MCU, so countdown the restart interval - if (--this.todo <= 0) + mcu++; + if (!this.ContinueOnMcuComplete()) { - if (this.codeBits < 24) - { - this.FillBuffer(); - } - - // If it's NOT a restart, then just bail, so we get corrupt data - // rather than no data - if (!this.ContinueOnRestart()) - { - return; - } - - this.Reset(); + return; } } } @@ -373,21 +340,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components // After all interleaved components, that's an interleaved MCU, // so now count down the restart interval mcu++; - if (--this.todo <= 0) + if (!this.ContinueOnMcuComplete()) { - if (this.codeBits < 24) - { - this.FillBuffer(); - } - - // If it's NOT a restart, then just bail, so we get corrupt data - // rather than no data - if (!this.ContinueOnRestart()) - { - return; - } - - this.Reset(); + return; } } } @@ -887,14 +842,33 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components private int PeekBits() => (int)((this.codeBuffer >> (32 - FastBits)) & ((1 << FastBits) - 1)); [MethodImpl(MethodImplOptions.AggressiveInlining)] - private bool ContinueOnRestart() + private bool ContinueOnMcuComplete() { + if (--this.todo > 0) + { + return true; + } + + if (this.codeBits < 24) + { + this.FillBuffer(); + } + + // If it's NOT a restart, then just bail, so we get corrupt data rather than no data. + // Reset the stream to before any bad markers to ensure we can read sucessive segments. if (this.badMarker) { this.stream.Position = this.markerPosition; } - return this.HasRestart(); + if (!this.HasRestart()) + { + return false; + } + + this.Reset(); + + return true; } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -904,7 +878,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components return m >= JpegConstants.Markers.RST0 && m <= JpegConstants.Markers.RST7; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.NoInlining)] private void Reset() { this.codeBits = 0; diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs index 86ac6b195a..fda98e4371 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs @@ -842,6 +842,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort return BinaryPrimitives.ReadUInt16BigEndian(this.markerBuffer); } + /// + /// Post processes the pixels into the destination image. + /// + /// The pixel format. + /// The . private Image PostProcessIntoImage() where TPixel : struct, IPixel { @@ -853,18 +858,22 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort } } + /// + /// 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.Tables.GetRowSpan(index); + 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 < 255) + fastAC[i] = 0; + if (fast < byte.MaxValue) { int rs = huffman.Values[fast]; int run = (rs >> 4) & 15; @@ -881,10 +890,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort k += (int)((~0U << magbits) + 1); } - // if the result is small enough, we can fit it in fastac table + // 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)); + fastAC[i] = (short)((k * 256) + (run * 16) + (len + magbits)); } } } From 26c0fd070f72dacb577587618c34b6e3bb592921 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 2 Jul 2018 12:38:23 +1000 Subject: [PATCH 21/33] private static ordering. --- .../Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs index 6c01deaa9a..cc9d4d470e 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs @@ -142,6 +142,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint LRot(uint x, int y) => (x << y) | (x >> (32 - y)); + private void ParseBaselineData( PdfJsFrame frame, PdfJsHuffmanTables dcHuffmanTables, @@ -241,9 +244,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static uint LRot(uint x, int y) => (x << y) | (x >> (32 - y)); - private void ParseProgressiveData( PdfJsFrame frame, PdfJsHuffmanTables dcHuffmanTables, From 898c86cfe3da3f35a57252aa2ff5a7de560c1b40 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 2 Jul 2018 22:22:26 +1000 Subject: [PATCH 22/33] Rename struct --- .../{FixedInt16Buffer18.cs => FixedInt32Buffer18.cs} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/{FixedInt16Buffer18.cs => FixedInt32Buffer18.cs} (83%) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt16Buffer18.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt32Buffer18.cs similarity index 83% rename from src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt16Buffer18.cs rename to src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt32Buffer18.cs index b193bf59e6..f8507ec47c 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt16Buffer18.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedInt32Buffer18.cs @@ -7,7 +7,7 @@ using System.Runtime.InteropServices; namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { [StructLayout(LayoutKind.Sequential)] - internal unsafe struct FixedInt16Buffer18 + internal unsafe struct FixedInt32Buffer18 { public fixed int Data[18]; @@ -16,7 +16,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - ref int self = ref Unsafe.As(ref this); + ref int self = ref Unsafe.As(ref this); return Unsafe.Add(ref self, idx); } } From 70f97d01e43aa8870e171e5b836c47df9674f422 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 2 Jul 2018 22:25:58 +1000 Subject: [PATCH 23/33] Update Huffman table property --- .../Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs index a895fd0a48..15ae56331c 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs @@ -22,7 +22,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components /// /// Gets the value offset array /// - public FixedInt16Buffer18 ValOffset; + public FixedInt32Buffer18 ValOffset; /// /// Gets the huffman value array From 6505c9964fb439eabca9b935120259e9b2850ed0 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 2 Jul 2018 22:45:19 +1000 Subject: [PATCH 24/33] 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 6a11f28056..3e170a92c7 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 fda98e4371..cb52fb84b3 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 From 5a9b9b3939115719e67169e91eb8850bf359930e Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 3 Jul 2018 00:25:05 +0200 Subject: [PATCH 25/33] add regression test for #624 --- tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs | 1 + tests/ImageSharp.Tests/TestImages.cs | 1 + tests/Images/External | 2 +- .../Jpg/issues/Issue624-DhtHasWrongLength-Progressive-N.jpg | 3 +++ 4 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 tests/Images/Input/Jpg/issues/Issue624-DhtHasWrongLength-Progressive-N.jpg diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs index 539ab73195..3c98d5be72 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs @@ -38,6 +38,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg TestImages.Jpeg.Issues.NoEoiProgressive517, TestImages.Jpeg.Issues.BadRstProgressive518, TestImages.Jpeg.Issues.MissingFF00ProgressiveBedroom159, + TestImages.Jpeg.Issues.DhtHasWrongLength624, }; /// diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 6d3a76e75f..b0bdad8e5c 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -144,6 +144,7 @@ namespace SixLabors.ImageSharp.Tests public const string NoEoiProgressive517 = "Jpg/issues/Issue517-No-EOI-Progressive.jpg"; public const string BadRstProgressive518 = "Jpg/issues/Issue518-Bad-RST-Progressive.jpg"; public const string InvalidCast520 = "Jpg/issues/Issue520-InvalidCast.jpg"; + public const string DhtHasWrongLength624 = "Jpg/issues/Issue624-DhtHasWrongLength-Progressive-N.jpg"; } public static readonly string[] All = Baseline.All.Concat(Progressive.All).ToArray(); diff --git a/tests/Images/External b/tests/Images/External index d9d93bbdd1..98fb7e2e4d 160000 --- a/tests/Images/External +++ b/tests/Images/External @@ -1 +1 @@ -Subproject commit d9d93bbdd18dd7b818c0d19cc8f967be98045d3c +Subproject commit 98fb7e2e4d5935b1c733bd2b206b6145b71ef378 diff --git a/tests/Images/Input/Jpg/issues/Issue624-DhtHasWrongLength-Progressive-N.jpg b/tests/Images/Input/Jpg/issues/Issue624-DhtHasWrongLength-Progressive-N.jpg new file mode 100644 index 0000000000..20a50fba9b --- /dev/null +++ b/tests/Images/Input/Jpg/issues/Issue624-DhtHasWrongLength-Progressive-N.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:da574d176b964eba84b5decb1e88a35d425e09975e0bc0ca73e485604179868f +size 30441 From 9e504ed7efce7908a6c485fc908fb30cef866d73 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 3 Jul 2018 00:41:14 +0200 Subject: [PATCH 26/33] ParseStream -only benchmark --- .../Codecs/Jpeg/DecodeJpegParseStreamOnly.cs | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs diff --git a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs new file mode 100644 index 0000000000..059f312b3e --- /dev/null +++ b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs @@ -0,0 +1,52 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using BenchmarkDotNet.Attributes; +using System.Drawing; +using System.IO; +using SixLabors.ImageSharp.Formats.Jpeg; +using SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort; +using SixLabors.ImageSharp.Tests; + +namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jpeg +{ + [Config(typeof(Config.ShortClr))] + public class DecodeJpegParseStreamOnly + { + [Params(TestImages.Jpeg.Baseline.Jpeg420Exif)] + 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 Size JpegSystemDrawing() + { + using (var memoryStream = new MemoryStream(this.jpegBytes)) + { + using (var image = System.Drawing.Image.FromStream(memoryStream)) + { + return image.Size; + } + } + } + + [Benchmark(Description = "PdfJsJpegDecoderCore.ParseStream")] + public void ParseStreamPdfJs() + { + using (var memoryStream = new MemoryStream(this.jpegBytes)) + { + var decoder = new PdfJsJpegDecoderCore(Configuration.Default, new JpegDecoder() { IgnoreMetadata = true }); + decoder.ParseStream(memoryStream); + decoder.Dispose(); + } + } + } +} \ No newline at end of file From 836dc643838b1ebec9cfd1ffffe9b66e29e29c21 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 3 Jul 2018 00:52:44 +0200 Subject: [PATCH 27/33] Introduce InliningOptions --- .../Common/Helpers/InliningOptions.cs | 22 ++++++++++++++++ .../Formats/Jpeg/JpegThrowHelper.cs | 26 +++++++++++++++++++ .../Jpeg/PdfJsPort/Components/ScanDecoder.cs | 21 +++++++-------- 3 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 src/ImageSharp/Common/Helpers/InliningOptions.cs create mode 100644 src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs diff --git a/src/ImageSharp/Common/Helpers/InliningOptions.cs b/src/ImageSharp/Common/Helpers/InliningOptions.cs new file mode 100644 index 0000000000..d218acdbaa --- /dev/null +++ b/src/ImageSharp/Common/Helpers/InliningOptions.cs @@ -0,0 +1,22 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +// Uncomment this for verbose profiler results: +// #define PROFILING +using System.Runtime.CompilerServices; + +namespace SixLabors.ImageSharp +{ + /// + /// Global inlining options. Helps temporally disable inling for better profiler output. + /// + internal static class InliningOptions + { +#if PROFILING + public const MethodImplOptions ShortMethod = 0; +#else + public const MethodImplOptions ShortMethod = MethodImplOptions.AggressiveInlining; +#endif + public const MethodImplOptions ColdPath = MethodImplOptions.NoInlining; + } +} \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs b/src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs new file mode 100644 index 0000000000..c7f3666604 --- /dev/null +++ b/src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs @@ -0,0 +1,26 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.Runtime.CompilerServices; + +namespace SixLabors.ImageSharp.Formats.Jpeg +{ + internal static class JpegThrowHelper + { + /// + /// Cold path optimization for throwing -s + /// + /// The error message for the exception + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowImageFormatException(string errorMessage) + { + throw new ImageFormatException(errorMessage); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowBadHuffmanCode() + { + throw new ImageFormatException("Bad Huffman code."); + } + } +} \ 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 cc9d4d470e..74772ec617 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs @@ -665,7 +665,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(InliningOptions.ShortMethod)] private int GetBits(int n) { if (this.codeBits < n) @@ -680,7 +680,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components return (int)k; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(InliningOptions.ShortMethod)] private int GetBit() { if (this.codeBits < 1) @@ -695,7 +695,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components return (int)(k & 0x80000000); } - [MethodImpl(MethodImplOptions.NoInlining)] + [MethodImpl(InliningOptions.ColdPath)] private void FillBuffer() { // Attempt to load at least the minimum nbumber of required bits into the buffer. @@ -748,7 +748,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components while (this.codeBits <= 24); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(InliningOptions.ShortMethod)] private int DecodeHuffman(ref PdfJsHuffmanTable table) { this.CheckBits(); @@ -773,7 +773,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components return this.DecodeHuffmanSlow(ref table); } - [MethodImpl(MethodImplOptions.NoInlining)] + [MethodImpl(InliningOptions.ColdPath)] private int DecodeHuffmanSlow(ref PdfJsHuffmanTable table) { // Naive test is to shift the code_buffer down so k bits are @@ -813,7 +813,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components return table.Values[c]; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(InliningOptions.ShortMethod)] private int ExtendReceive(int n) { if (this.codeBits < n) @@ -829,7 +829,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components return (int)(k + (Bias[n] & ~sgn)); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(InliningOptions.ShortMethod)] private void CheckBits() { if (this.codeBits < 16) @@ -838,10 +838,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(InliningOptions.ShortMethod)] private int PeekBits() => (int)((this.codeBuffer >> (32 - FastBits)) & ((1 << FastBits) - 1)); - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(InliningOptions.ShortMethod)] private bool ContinueOnMcuComplete() { if (--this.todo > 0) @@ -871,14 +871,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components return true; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(InliningOptions.ShortMethod)] private bool HasRestart() { byte m = this.marker; return m >= JpegConstants.Markers.RST0 && m <= JpegConstants.Markers.RST7; } - [MethodImpl(MethodImplOptions.NoInlining)] private void Reset() { this.codeBits = 0; From b30e6dde049dfa53664808fb155c7c92f5173b6f Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 3 Jul 2018 00:55:55 +0200 Subject: [PATCH 28/33] use JpegThrowHelper --- .../Jpeg/PdfJsPort/Components/ScanDecoder.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs index 74772ec617..d0b6cc9095 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs @@ -361,7 +361,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components if (t < 0) { - throw new ImageFormatException("Bad Huffman code"); + JpegThrowHelper.ThrowBadHuffmanCode(); } int diff = t != 0 ? this.ExtendReceive(t) : 0; @@ -398,7 +398,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components if (rs < 0) { - throw new ImageFormatException("Bad Huffman code"); + JpegThrowHelper.ThrowBadHuffmanCode(); } s = rs & 15; @@ -432,7 +432,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { if (this.spectralEnd != 0) { - throw new ImageFormatException("Can't merge DC and AC."); + JpegThrowHelper.ThrowImageFormatException("Can't merge DC and AC."); } this.CheckBits(); @@ -465,7 +465,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { if (this.spectralStart == 0) { - throw new ImageFormatException("Can't merge DC and AC."); + JpegThrowHelper.ThrowImageFormatException("Can't merge DC and AC."); } if (this.successiveHigh == 0) @@ -508,7 +508,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components if (rs < 0) { - throw new ImageFormatException("Bad Huffman code."); + JpegThrowHelper.ThrowBadHuffmanCode(); } s = rs & 15; @@ -587,7 +587,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components int rs = this.DecodeHuffman(ref acTable); if (rs < 0) { - throw new ImageFormatException("Bad Huffman code."); + JpegThrowHelper.ThrowBadHuffmanCode(); } int s = rs & 15; @@ -614,7 +614,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { if (s != 1) { - throw new ImageFormatException("Bad Huffman code."); + JpegThrowHelper.ThrowBadHuffmanCode(); } // Sign bit From 04f57108d8c02f98c28436f9ee9f956d2b2987f4 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 3 Jul 2018 01:03:51 +0200 Subject: [PATCH 29/33] separate Interleaved / Non-Interleaved code path --- .../Jpeg/PdfJsPort/Components/ScanDecoder.cs | 361 ++++++++++-------- 1 file changed, 208 insertions(+), 153 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs index d0b6cc9095..6f88b6e1f2 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs @@ -153,203 +153,258 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { if (this.componentsLength == 1) { - PdfJsFrameComponent component = this.components[this.componentIndex]; - - // Non-interleaved data, we just need to process one block at a time, - // in trivial scanline order - // number of blocks to do just depends on how many actual "pixels" this - // component has, independent of interleaved MCU blocking and such - 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)); - - int mcu = 0; - for (int j = 0; j < h; j++) - { - for (int i = 0; i < w; i++) - { - if (this.eof) - { - return; - } - - int blockRow = mcu / w; - int blockCol = mcu % w; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); - this.DecodeBlock(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable, ref acHuffmanTable, ref fastACRef); - - // Every data block is an MCU, so countdown the restart interval - mcu++; - if (!this.ContinueOnMcuComplete()) - { - return; - } - } - } + this.ParseBaselineDataNonInterleaved(dcHuffmanTables, acHuffmanTables, fastACTables); } else { - // Interleaved - int mcu = 0; - int mcusPerColumn = frame.McusPerColumn; - int mcusPerLine = frame.McusPerLine; - for (int j = 0; j < mcusPerColumn; j++) + this.ParseBaselineDataInterleaved(frame, dcHuffmanTables, acHuffmanTables, fastACTables); + } + } + + private void ParseBaselineDataInterleaved( + PdfJsFrame frame, + PdfJsHuffmanTables dcHuffmanTables, + PdfJsHuffmanTables acHuffmanTables, + FastACTables fastACTables) + { + // Interleaved + int mcu = 0; + int mcusPerColumn = frame.McusPerColumn; + int mcusPerLine = frame.McusPerLine; + for (int j = 0; j < mcusPerColumn; j++) + { + for (int i = 0; i < mcusPerLine; i++) { - for (int i = 0; i < mcusPerLine; i++) + // Scan an interleaved mcu... process components in order + for (int k = 0; k < this.componentsLength; k++) { - // Scan an interleaved mcu... process components in order - 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 = + ref MemoryMarshal.GetReference(fastACTables.GetTableSpan(component.ACHuffmanTableId)); + int h = component.HorizontalSamplingFactor; + int v = component.VerticalSamplingFactor; + + // Scan out an mcu's worth of this component; that's just determined + // by the basic H and V specified for the component + for (int y = 0; y < v; y++) { - 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 = ref MemoryMarshal.GetReference(fastACTables.GetTableSpan(component.ACHuffmanTableId)); - int h = component.HorizontalSamplingFactor; - int v = component.VerticalSamplingFactor; - - // Scan out an mcu's worth of this component; that's just determined - // by the basic H and V specified for the component - for (int y = 0; y < v; y++) + for (int x = 0; x < h; x++) { - for (int x = 0; x < h; x++) + if (this.eof) { - if (this.eof) - { - return; - } - - int mcuRow = mcu / mcusPerLine; - int mcuCol = mcu % mcusPerLine; - int blockRow = (mcuRow * v) + y; - int blockCol = (mcuCol * h) + x; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); - this.DecodeBlock(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable, ref acHuffmanTable, ref fastACRef); + return; } + + int mcuRow = mcu / mcusPerLine; + 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), + ref dcHuffmanTable, + ref acHuffmanTable, + ref fastACRef); } } + } - // After all interleaved components, that's an interleaved MCU, - // so now count down the restart interval - mcu++; - if (!this.ContinueOnMcuComplete()) - { - return; - } + // After all interleaved components, that's an interleaved MCU, + // so now count down the restart interval + mcu++; + if (!this.ContinueOnMcuComplete()) + { + return; } } } } - private void ParseProgressiveData( - PdfJsFrame frame, + /// + /// Non-interleaved data, we just need to process one block at a ti + /// in trivial scanline order + /// number of blocks to do just depends on how many actual "pixels" + /// component has, independent of interleaved MCU blocking and such + /// + private void ParseBaselineDataNonInterleaved( PdfJsHuffmanTables dcHuffmanTables, PdfJsHuffmanTables acHuffmanTables, FastACTables fastACTables) { - if (this.componentsLength == 1) + PdfJsFrameComponent component = this.components[this.componentIndex]; + + 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)); + + int mcu = 0; + for (int j = 0; j < h; j++) { - PdfJsFrameComponent component = this.components[this.componentIndex]; - - // Non-interleaved data, we just need to process one block at a time, - // in trivial scanline order - // number of blocks to do just depends on how many actual "pixels" this - // component has, independent of interleaved MCU blocking and such - 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)); - - int mcu = 0; - for (int j = 0; j < h; j++) + for (int i = 0; i < w; i++) { - for (int i = 0; i < w; i++) + if (this.eof) { - if (this.eof) - { - return; - } - - 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); - } - else - { - this.DecodeBlockProgressiveAC(ref Unsafe.Add(ref blockDataRef, offset), ref acHuffmanTable, ref fastACRef); - } + return; + } - // Every data block is an MCU, so countdown the restart interval - mcu++; - if (!this.ContinueOnMcuComplete()) - { - return; - } + int blockRow = mcu / w; + int blockCol = mcu % w; + int offset = component.GetBlockBufferOffset(blockRow, blockCol); + this.DecodeBlockBaseline( + component, + ref Unsafe.Add(ref blockDataRef, offset), + ref dcHuffmanTable, + ref acHuffmanTable, + ref fastACRef); + + // Every data block is an MCU, so countdown the restart interval + mcu++; + if (!this.ContinueOnMcuComplete()) + { + return; } } } + } + + private void ParseProgressiveData( + PdfJsFrame frame, + PdfJsHuffmanTables dcHuffmanTables, + PdfJsHuffmanTables acHuffmanTables, + FastACTables fastACTables) + { + if (this.componentsLength == 1) + { + this.ParseProgressiveDataNonInterleaved(dcHuffmanTables, acHuffmanTables, fastACTables); + } else { - // Interleaved - int mcu = 0; - int mcusPerColumn = frame.McusPerColumn; - int mcusPerLine = frame.McusPerLine; - for (int j = 0; j < mcusPerColumn; j++) + this.ParseProgressiveDataInterleaved(frame, dcHuffmanTables); + } + } + + private void ParseProgressiveDataInterleaved(PdfJsFrame frame, PdfJsHuffmanTables dcHuffmanTables) + { + // Interleaved + int mcu = 0; + int mcusPerColumn = frame.McusPerColumn; + int mcusPerLine = frame.McusPerLine; + for (int j = 0; j < mcusPerColumn; j++) + { + for (int i = 0; i < mcusPerLine; i++) { - for (int i = 0; i < mcusPerLine; i++) + // Scan an interleaved mcu... process components in order + for (int k = 0; k < this.componentsLength; k++) { - // Scan an interleaved mcu... process components in order - 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]; + int h = component.HorizontalSamplingFactor; + int v = component.VerticalSamplingFactor; + + // Scan out an mcu's worth of this component; that's just determined + // by the basic H and V specified for the component + for (int y = 0; y < v; y++) { - PdfJsFrameComponent component = this.components[k]; - ref short blockDataRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast(component.SpectralBlocks.Span)); - ref PdfJsHuffmanTable dcHuffmanTable = ref dcHuffmanTables[component.DCHuffmanTableId]; - int h = component.HorizontalSamplingFactor; - int v = component.VerticalSamplingFactor; - - // Scan out an mcu's worth of this component; that's just determined - // by the basic H and V specified for the component - for (int y = 0; y < v; y++) + for (int x = 0; x < h; x++) { - for (int x = 0; x < h; x++) + if (this.eof) { - if (this.eof) - { - return; - } - - int mcuRow = mcu / mcusPerLine; - int mcuCol = mcu % mcusPerLine; - int blockRow = (mcuRow * v) + y; - int blockCol = (mcuCol * h) + x; - int offset = component.GetBlockBufferOffset(blockRow, blockCol); - this.DecodeBlockProgressiveDC(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable); + return; } + + int mcuRow = mcu / mcusPerLine; + int mcuCol = mcu % mcusPerLine; + int blockRow = (mcuRow * v) + y; + int blockCol = (mcuCol * h) + x; + int offset = component.GetBlockBufferOffset(blockRow, blockCol); + this.DecodeBlockProgressiveDC( + component, + ref Unsafe.Add(ref blockDataRef, offset), + ref dcHuffmanTable); } } + } - // After all interleaved components, that's an interleaved MCU, - // so now count down the restart interval - mcu++; - if (!this.ContinueOnMcuComplete()) - { - return; - } + // After all interleaved components, that's an interleaved MCU, + // so now count down the restart interval + mcu++; + if (!this.ContinueOnMcuComplete()) + { + return; + } + } + } + } + + /// + /// Non-interleaved data, we just need to process one block at a time, + /// in trivial scanline order + /// number of blocks to do just depends on how many actual "pixels" this + /// component has, independent of interleaved MCU blocking and such + /// + private void ParseProgressiveDataNonInterleaved( + PdfJsHuffmanTables dcHuffmanTables, + PdfJsHuffmanTables acHuffmanTables, + FastACTables fastACTables) + { + PdfJsFrameComponent component = this.components[this.componentIndex]; + + 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)); + + int mcu = 0; + for (int j = 0; j < h; j++) + { + for (int i = 0; i < w; i++) + { + if (this.eof) + { + return; + } + + 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); + } + else + { + this.DecodeBlockProgressiveAC( + ref Unsafe.Add(ref blockDataRef, offset), + ref acHuffmanTable, + ref fastACRef); + } + + // Every data block is an MCU, so countdown the restart interval + mcu++; + if (!this.ContinueOnMcuComplete()) + { + return; } } } } - private void DecodeBlock( + private void DecodeBlockBaseline( PdfJsFrameComponent component, ref short blockDataRef, ref PdfJsHuffmanTable dcTable, From 27269683b9ca3ad95f8e1eb3b3a341cbc152ed40 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 3 Jul 2018 01:14:33 +0200 Subject: [PATCH 30/33] 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, From f790a8cc82dbd536efd12bf066fcab02a06bae6d Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 3 Jul 2018 01:29:04 +0200 Subject: [PATCH 31/33] ScanDecoder: refactor parameters to members --- .../Jpeg/PdfJsPort/Components/FastACTables.cs | 9 ++ .../Jpeg/PdfJsPort/Components/ScanDecoder.cs | 108 ++++++++---------- .../Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs | 7 +- 3 files changed, 61 insertions(+), 63 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FastACTables.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FastACTables.cs index 3e170a92c7..6cb0d6dfe5 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FastACTables.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FastACTables.cs @@ -34,6 +34,15 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components return this.tables.GetRowSpan(index); } + /// + /// Gets a reference to the first element of the AC table indexed by + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ref short GetAcTableReference(PdfJsFrameComponent component) + { + return ref this.tables.GetRowSpan(component.ACHuffmanTableId)[0]; + } + /// /// Builds a lookup table for fast AC entropy scan decoding. /// diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs index 18cfc6c3fe..8575bac69e 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs @@ -23,6 +23,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components // LUT Bias[n] = (-1 << n) + 1 private static readonly int[] Bias = { 0, -1, -3, -7, -15, -31, -63, -127, -255, -511, -1023, -2047, -4095, -8191, -16383, -32767 }; + private readonly PdfJsFrame frame; + private readonly PdfJsHuffmanTables dcHuffmanTables; + private readonly PdfJsHuffmanTables acHuffmanTables; + private readonly FastACTables fastACTables; + private readonly DoubleBufferedStreamReader stream; private readonly PdfJsFrameComponent[] components; private readonly ZigZag dctZigZag; @@ -79,7 +84,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components /// Initializes a new instance of the class. /// /// The input stream. - /// The scan components. + /// The image frame. + /// The DC Huffman tables. + /// The AC Huffman tables. + /// The fast AC decoding tables. /// The component index within the array. /// The length of the components. Different to the array length. /// The reset interval. @@ -89,7 +97,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components /// The successive approximation bit low end. public ScanDecoder( DoubleBufferedStreamReader stream, - PdfJsFrameComponent[] components, + PdfJsFrame frame, + PdfJsHuffmanTables dcHuffmanTables, + PdfJsHuffmanTables acHuffmanTables, + FastACTables fastACTables, int componentIndex, int componentsLength, int restartInterval, @@ -100,7 +111,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { this.dctZigZag = ZigZag.CreateUnzigTable(); this.stream = stream; - this.components = components; + this.frame = frame; + this.dcHuffmanTables = dcHuffmanTables; + this.acHuffmanTables = acHuffmanTables; + this.fastACTables = fastACTables; + this.components = frame.Components; this.marker = JpegConstants.Markers.XFF; this.markerPosition = 0; this.componentIndex = componentIndex; @@ -115,25 +130,17 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components /// /// Decodes the entropy coded data. /// - /// The image frame. - /// The DC Huffman tables. - /// The AC Huffman tables. - /// The fast AC decoding tables. - public void ParseEntropyCodedData( - PdfJsFrame frame, - PdfJsHuffmanTables dcHuffmanTables, - PdfJsHuffmanTables acHuffmanTables, - FastACTables fastACTables) + public void ParseEntropyCodedData() { this.Reset(); - if (!frame.Progressive) + if (!this.frame.Progressive) { - this.ParseBaselineData(frame, dcHuffmanTables, acHuffmanTables, fastACTables); + this.ParseBaselineData(); } else { - this.ParseProgressiveData(frame, dcHuffmanTables, acHuffmanTables, fastACTables); + this.ParseProgressiveData(); } if (this.badMarker) @@ -145,32 +152,24 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint LRot(uint x, int y) => (x << y) | (x >> (32 - y)); - private void ParseBaselineData( - PdfJsFrame frame, - PdfJsHuffmanTables dcHuffmanTables, - PdfJsHuffmanTables acHuffmanTables, - FastACTables fastACTables) + private void ParseBaselineData() { if (this.componentsLength == 1) { - this.ParseBaselineDataNonInterleaved(dcHuffmanTables, acHuffmanTables, fastACTables); + this.ParseBaselineDataNonInterleaved(); } else { - this.ParseBaselineDataInterleaved(frame, dcHuffmanTables, acHuffmanTables, fastACTables); + this.ParseBaselineDataInterleaved(); } } - private void ParseBaselineDataInterleaved( - PdfJsFrame frame, - PdfJsHuffmanTables dcHuffmanTables, - PdfJsHuffmanTables acHuffmanTables, - FastACTables fastACTables) + private void ParseBaselineDataInterleaved() { // Interleaved int mcu = 0; - int mcusPerColumn = frame.McusPerColumn; - int mcusPerLine = frame.McusPerLine; + int mcusPerColumn = this.frame.McusPerColumn; + int mcusPerLine = this.frame.McusPerLine; for (int j = 0; j < mcusPerColumn; j++) { for (int i = 0; i < mcusPerLine; i++) @@ -180,10 +179,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { PdfJsFrameComponent component = this.components[k]; - ref PdfJsHuffmanTable dcHuffmanTable = ref dcHuffmanTables[component.DCHuffmanTableId]; - ref PdfJsHuffmanTable acHuffmanTable = ref acHuffmanTables[component.ACHuffmanTableId]; - ref short fastACRef = - ref MemoryMarshal.GetReference(fastACTables.GetTableSpan(component.ACHuffmanTableId)); + ref PdfJsHuffmanTable dcHuffmanTable = ref this.dcHuffmanTables[component.DCHuffmanTableId]; + ref PdfJsHuffmanTable acHuffmanTable = ref this.acHuffmanTables[component.ACHuffmanTableId]; + ref short fastACRef = ref this.fastACTables.GetAcTableReference(component); int h = component.HorizontalSamplingFactor; int v = component.VerticalSamplingFactor; @@ -231,19 +229,16 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components /// number of blocks to do just depends on how many actual "pixels" /// component has, independent of interleaved MCU blocking and such /// - private void ParseBaselineDataNonInterleaved( - PdfJsHuffmanTables dcHuffmanTables, - PdfJsHuffmanTables acHuffmanTables, - FastACTables fastACTables) + private void ParseBaselineDataNonInterleaved() { PdfJsFrameComponent component = this.components[this.componentIndex]; int w = component.WidthInBlocks; int h = component.HeightInBlocks; - ref PdfJsHuffmanTable dcHuffmanTable = ref dcHuffmanTables[component.DCHuffmanTableId]; - ref PdfJsHuffmanTable acHuffmanTable = ref acHuffmanTables[component.ACHuffmanTableId]; - ref short fastACRef = ref MemoryMarshal.GetReference(fastACTables.GetTableSpan(component.ACHuffmanTableId)); + ref PdfJsHuffmanTable dcHuffmanTable = ref this.dcHuffmanTables[component.DCHuffmanTableId]; + ref PdfJsHuffmanTable acHuffmanTable = ref this.acHuffmanTables[component.ACHuffmanTableId]; + ref short fastACRef = ref this.fastACTables.GetAcTableReference(component); int mcu = 0; for (int j = 0; j < h; j++) @@ -276,28 +271,24 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } } - private void ParseProgressiveData( - PdfJsFrame frame, - PdfJsHuffmanTables dcHuffmanTables, - PdfJsHuffmanTables acHuffmanTables, - FastACTables fastACTables) + private void ParseProgressiveData() { if (this.componentsLength == 1) { - this.ParseProgressiveDataNonInterleaved(dcHuffmanTables, acHuffmanTables, fastACTables); + this.ParseProgressiveDataNonInterleaved(); } else { - this.ParseProgressiveDataInterleaved(frame, dcHuffmanTables); + this.ParseProgressiveDataInterleaved(); } } - private void ParseProgressiveDataInterleaved(PdfJsFrame frame, PdfJsHuffmanTables dcHuffmanTables) + private void ParseProgressiveDataInterleaved() { // Interleaved int mcu = 0; - int mcusPerColumn = frame.McusPerColumn; - int mcusPerLine = frame.McusPerLine; + int mcusPerColumn = this.frame.McusPerColumn; + int mcusPerLine = this.frame.McusPerLine; for (int j = 0; j < mcusPerColumn; j++) { for (int i = 0; i < mcusPerLine; i++) @@ -306,9 +297,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 dcHuffmanTable = ref this.dcHuffmanTables[component.DCHuffmanTableId]; int h = component.HorizontalSamplingFactor; int v = component.VerticalSamplingFactor; @@ -327,7 +316,7 @@ 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.DecodeBlockProgressiveDC( component, blockRow, @@ -354,19 +343,16 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components /// number of blocks to do just depends on how many actual "pixels" this /// component has, independent of interleaved MCU blocking and such /// - private void ParseProgressiveDataNonInterleaved( - PdfJsHuffmanTables dcHuffmanTables, - PdfJsHuffmanTables acHuffmanTables, - FastACTables fastACTables) + private void ParseProgressiveDataNonInterleaved() { PdfJsFrameComponent component = this.components[this.componentIndex]; int w = component.WidthInBlocks; int h = component.HeightInBlocks; - ref PdfJsHuffmanTable dcHuffmanTable = ref dcHuffmanTables[component.DCHuffmanTableId]; - ref PdfJsHuffmanTable acHuffmanTable = ref acHuffmanTables[component.ACHuffmanTableId]; - ref short fastACRef = ref MemoryMarshal.GetReference(fastACTables.GetTableSpan(component.ACHuffmanTableId)); + ref PdfJsHuffmanTable dcHuffmanTable = ref this.dcHuffmanTables[component.DCHuffmanTableId]; + ref PdfJsHuffmanTable acHuffmanTable = ref this.acHuffmanTables[component.ACHuffmanTableId]; + ref short fastACRef = ref this.fastACTables.GetAcTableReference(component); int mcu = 0; for (int j = 0; j < h; j++) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs index cb52fb84b3..a360d54771 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs @@ -806,7 +806,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort var sd = new ScanDecoder( this.InputStream, - this.Frame.Components, + this.Frame, + this.dcHuffmanTables, + this.acHuffmanTables, + this.fastACTables, componentIndex, selectorsCount, this.resetInterval, @@ -815,7 +818,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort successiveApproximation >> 4, successiveApproximation & 15); - sd.ParseEntropyCodedData(this.Frame, this.dcHuffmanTables, this.acHuffmanTables, this.fastACTables); + sd.ParseEntropyCodedData(); } /// From ef7d5e55ec7a4d1d46cf68faede4971a8c1ec3e1 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 3 Jul 2018 01:34:15 +0200 Subject: [PATCH 32/33] temporal vortex attacked again --- src/ImageSharp/Common/Helpers/InliningOptions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Common/Helpers/InliningOptions.cs b/src/ImageSharp/Common/Helpers/InliningOptions.cs index d218acdbaa..e1d51da8d4 100644 --- a/src/ImageSharp/Common/Helpers/InliningOptions.cs +++ b/src/ImageSharp/Common/Helpers/InliningOptions.cs @@ -8,7 +8,7 @@ using System.Runtime.CompilerServices; namespace SixLabors.ImageSharp { /// - /// Global inlining options. Helps temporally disable inling for better profiler output. + /// Global inlining options. Helps temporarily disable inling for better profiler output. /// internal static class InliningOptions { From cb1c9de4289e2e09f467023987b45252cf1dafc4 Mon Sep 17 00:00:00 2001 From: Johannes Bildstein Date: Tue, 3 Jul 2018 13:55:01 +0200 Subject: [PATCH 33/33] improve check for invalid ICC profiles and extend tests --- .../MetaData/Profiles/ICC/IccProfile.cs | 17 ++- .../TestDataIcc/IccTestDataProfiles.cs | 119 ++++++++++++++---- 2 files changed, 108 insertions(+), 28 deletions(-) diff --git a/src/ImageSharp/MetaData/Profiles/ICC/IccProfile.cs b/src/ImageSharp/MetaData/Profiles/ICC/IccProfile.cs index 52b8e43dac..db1d96d7ec 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/IccProfile.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/IccProfile.cs @@ -167,11 +167,22 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// True if the profile is valid; False otherwise public bool CheckIsValid() { - return Enum.IsDefined(typeof(IccColorSpaceType), this.Header.DataColorSpace) && + const int minSize = 128; + const int maxSize = 50_000_000; // it's unlikely there is a profile bigger than 50MB + + bool arrayValid = true; + if (this.data != null) + { + arrayValid = this.data.Length >= minSize && + this.data.Length >= this.Header.Size; + } + + return arrayValid && + Enum.IsDefined(typeof(IccColorSpaceType), this.Header.DataColorSpace) && Enum.IsDefined(typeof(IccColorSpaceType), this.Header.ProfileConnectionSpace) && Enum.IsDefined(typeof(IccRenderingIntent), this.Header.RenderingIntent) && - this.Header.Size >= 128 && - this.Header.Size < 50_000_000; // it's unlikely there is a profile bigger than 50MB + this.Header.Size >= minSize && + this.Header.Size < maxSize; } /// diff --git a/tests/ImageSharp.Tests/TestDataIcc/IccTestDataProfiles.cs b/tests/ImageSharp.Tests/TestDataIcc/IccTestDataProfiles.cs index cf8cffb326..35ffa2bbb6 100644 --- a/tests/ImageSharp.Tests/TestDataIcc/IccTestDataProfiles.cs +++ b/tests/ImageSharp.Tests/TestDataIcc/IccTestDataProfiles.cs @@ -99,12 +99,12 @@ namespace SixLabors.ImageSharp.Tests 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - // Nr of tag table entries (0) + // Nr of tag table entries (byte)(nrOfEntries >> 24), (byte)(nrOfEntries >> 16), (byte)(nrOfEntries >> 8), (byte)nrOfEntries }); } - public static byte[] Profile_Random_Array = ArrayHelper.Concat(CreateHeaderRandomArray(168, 2, Profile_Random_Id_Array), + public static readonly byte[] Profile_Random_Array = ArrayHelper.Concat(CreateHeaderRandomArray(168, 2, Profile_Random_Id_Array), new byte[] { 0x00, 0x00, 0x00, 0x00, // tag signature (Unknown) @@ -119,7 +119,7 @@ namespace SixLabors.ImageSharp.Tests IccTestDataTagDataEntry.Unknown_Arr ); - public static IccProfile Profile_Random_Val = new IccProfile(CreateHeaderRandomValue(168, + public static readonly IccProfile Profile_Random_Val = new IccProfile(CreateHeaderRandomValue(168, #if !NETSTANDARD1_1 Profile_Random_Id_Value, #else @@ -132,41 +132,110 @@ namespace SixLabors.ImageSharp.Tests IccTestDataTagDataEntry.Unknown_Val }); - public static byte[] Header_Corrupt1_Array = + public static readonly byte[] Header_CorruptDataColorSpace_Array = { - 0x81, 0xB1, 0x81, 0xE4, 0x82, 0x16, 0x82, 0x49, 0x82, 0x7B, 0x82, 0xAD, 0x82, 0xDF, 0x83, 0x11, - 0x83, 0x43, 0x83, 0x75, 0x83, 0xA7, 0x83, 0xD8, 0x84, 0x0A, 0x84, 0x3B, 0x84, 0x6C, 0x84, 0x9E, - 0x84, 0xCF, 0x85, 0x00, 0x85, 0x31, 0x85, 0x62, 0x85, 0x93, 0x85, 0xC3, 0x85, 0xF4, 0x86, 0x24, - 0x86, 0x55, 0x86, 0x85, 0x86, 0xB5, 0x86, 0xE6, 0x87, 0x16, 0x87, 0x46, 0x87, 0x76, 0x87, 0xA5, - 0x87, 0xD5, 0x88, 0x05, 0x88, 0x34, 0x88, 0x64, 0x88, 0x93, 0x88, 0xC3, 0x88, 0xF2, 0x89, 0x21, - 0x89, 0x50, 0x89, 0x7F, 0x89, 0xAE, 0x89, 0xDD, 0x8A, 0x0C, 0x8A, 0x3B, 0x8A, 0x69, 0x8A, 0x98, - 0x8A, 0xC6, 0x8A, 0xF5, 0x8B, 0x23, 0x8B, 0x51, 0x8B, 0x7F, 0x8B, 0xAE, 0x8B, 0xDC, 0x8C, 0x09, - 0x8C, 0x37, 0x8C, 0x65, 0x8C, 0x93, 0x8C, 0xC1, 0x8C, 0xEE, 0x8D, 0x1C, 0x8D, 0x49, 0x8D, 0x76, + 0x00, 0x00, 0x00, 0x80, // Size + 0x61, 0x62, 0x63, 0x64, // CmmType + 0x04, 0x30, 0x00, 0x00, // Version + 0x6D, 0x6E, 0x74, 0x72, // Class + 0x68, 0x45, 0x8D, 0x6A, // DataColorSpace + 0x58, 0x59, 0x5A, 0x20, // ProfileConnectionSpace + 0x07, 0xC6, 0x00, 0x0B, 0x00, 0x1A, 0x00, 0x07, 0x00, 0x15, 0x00, 0x2A, // CreationDate + 0x61, 0x63, 0x73, 0x70, // FileSignature + 0x4D, 0x53, 0x46, 0x54, // PrimaryPlatformSignature + 0x00, 0x00, 0x00, 0x01, // Flags + 0x07, 0x5B, 0xCD, 0x15, // DeviceManufacturer + 0x3A, 0xDE, 0x68, 0xB1, // DeviceModel + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, // DeviceAttributes + 0x00, 0x00, 0x00, 0x03, // RenderingIntent + 0x00, 0x04, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, // PcsIlluminant + 0x64, 0x63, 0x62, 0x61, // CreatorSignature + // Profile ID + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + // Padding + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, }; - public static byte[] Header_Corrupt2_Array = + public static readonly byte[] Header_CorruptProfileConnectionSpace_Array = { - 0x23, 0x74, 0x6D, 0x6D, 0xB1, 0xBC, 0x28, 0xB2, 0x6D, 0x0B, 0xA3, 0x9C, 0x2D, 0x60, 0x6C, 0xB4, - 0x96, 0xF2, 0x31, 0x88, 0x6C, 0x67, 0x8B, 0xA9, 0x35, 0x31, 0x6C, 0x24, 0x81, 0xAE, 0x38, 0x64, - 0x6B, 0xE9, 0x78, 0xEC, 0x3B, 0x28, 0x6B, 0xB7, 0x71, 0x4F, 0x3D, 0x87, 0x6B, 0x8C, 0x6A, 0xC3, - 0x3F, 0x87, 0x6B, 0x68, 0x65, 0x33, 0x41, 0x30, 0x6B, 0x4A, 0x60, 0x8C, 0x42, 0x8C, 0x6B, 0x32, - 0x5C, 0xB8, 0x43, 0xA2, 0x6B, 0x1F, 0x59, 0xA4, 0x44, 0x79, 0x6B, 0x10, 0x57, 0x3B, 0x45, 0x1A, - 0x6B, 0x05, 0x55, 0x68, 0x45, 0x8D, 0x6A, 0xFE, 0x54, 0x15, 0x45, 0xDA, 0x6A, 0xF9, 0x53, 0x2A, - 0x46, 0x16, 0x6A, 0xF5, 0x52, 0x74, 0x46, 0x27, 0x6A, 0xF4, 0x52, 0x43, 0x46, 0x27, 0x6A, 0xF4, - 0x52, 0x43, 0x46, 0x27, 0x6A, 0xF4, 0x52, 0x43, 0x46, 0x27, 0x6A, 0xF4, 0x52, 0x43, 0x46, 0x27, + 0x00, 0x00, 0x00, 0x80, // Size + 0x62, 0x63, 0x64, 0x65, // CmmType + 0x04, 0x30, 0x00, 0x00, // Version + 0x6D, 0x6E, 0x74, 0x72, // Class + 0x52, 0x47, 0x42, 0x20, // DataColorSpace + 0x68, 0x45, 0x8D, 0x6A, // ProfileConnectionSpace + 0x07, 0xC6, 0x00, 0x0B, 0x00, 0x1A, 0x00, 0x07, 0x00, 0x15, 0x00, 0x2A, // CreationDate + 0x61, 0x63, 0x73, 0x70, // FileSignature + 0x4D, 0x53, 0x46, 0x54, // PrimaryPlatformSignature + 0x00, 0x00, 0x00, 0x01, // Flags + 0x07, 0x5B, 0xCD, 0x15, // DeviceManufacturer + 0x3A, 0xDE, 0x68, 0xB1, // DeviceModel + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, // DeviceAttributes + 0x00, 0x00, 0x00, 0x03, // RenderingIntent + 0x00, 0x04, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, // PcsIlluminant + 0x64, 0x63, 0x62, 0x61, // CreatorSignature + // Profile ID + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + // Padding + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, }; + public static readonly byte[] Header_CorruptRenderingIntent_Array = + { + 0x00, 0x00, 0x00, 0x80, // Size + 0x63, 0x64, 0x65, 0x66, // CmmType + 0x04, 0x30, 0x00, 0x00, // Version + 0x6D, 0x6E, 0x74, 0x72, // Class + 0x52, 0x47, 0x42, 0x20, // DataColorSpace + 0x58, 0x59, 0x5A, 0x20, // ProfileConnectionSpace + 0x07, 0xC6, 0x00, 0x0B, 0x00, 0x1A, 0x00, 0x07, 0x00, 0x15, 0x00, 0x2A, // CreationDate + 0x61, 0x63, 0x73, 0x70, // FileSignature + 0x4D, 0x53, 0x46, 0x54, // PrimaryPlatformSignature + 0x00, 0x00, 0x00, 0x01, // Flags + 0x07, 0x5B, 0xCD, 0x15, // DeviceManufacturer + 0x3A, 0xDE, 0x68, 0xB1, // DeviceModel + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, // DeviceAttributes + 0x33, 0x41, 0x30, 0x6B, // RenderingIntent + 0x00, 0x04, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, // PcsIlluminant + 0x64, 0x63, 0x62, 0x61, // CreatorSignature + // Profile ID + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + // Padding + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + }; + + public static readonly byte[] Header_DataTooSmall_Array = new byte[127]; + + public static readonly byte[] Header_InvalidSizeSmall_Array = CreateHeaderRandomArray(127, 0, Header_Random_Id_Array); + + public static readonly byte[] Header_InvalidSizeBig_Array = CreateHeaderRandomArray(50_000_000, 0, Header_Random_Id_Array); + + public static readonly byte[] Header_SizeBiggerThanData_Array = CreateHeaderRandomArray(160, 0, Header_Random_Id_Array); - public static object[][] ProfileIdTestData = + public static readonly object[][] ProfileIdTestData = { new object[] { Header_Random_Array, Header_Random_Id_Value }, new object[] { Profile_Random_Array, Profile_Random_Id_Value }, }; - public static object[][] ProfileValidityTestData = + public static readonly object[][] ProfileValidityTestData = { - new object[] { Header_Corrupt1_Array, false }, - new object[] { Header_Corrupt2_Array, false }, + new object[] { Header_CorruptDataColorSpace_Array, false }, + new object[] { Header_CorruptProfileConnectionSpace_Array, false }, + new object[] { Header_CorruptRenderingIntent_Array, false }, + new object[] { Header_DataTooSmall_Array, false }, + new object[] { Header_InvalidSizeSmall_Array, false }, + new object[] { Header_InvalidSizeBig_Array, false }, + new object[] { Header_SizeBiggerThanData_Array, false }, new object[] { Header_Random_Array, true }, }; }