diff --git a/src/ImageSharp.Formats.Jpeg/Components/Decoder/Bits.cs b/src/ImageSharp.Formats.Jpeg/Components/Decoder/Bits.cs index 21d4f8c43..02f585be0 100644 --- a/src/ImageSharp.Formats.Jpeg/Components/Decoder/Bits.cs +++ b/src/ImageSharp.Formats.Jpeg/Components/Decoder/Bits.cs @@ -36,11 +36,11 @@ namespace ImageSharp.Formats.Jpg /// the caller is the one responsible for first checking that bits.UnreadBits < n. /// /// The number of bits to ensure. - /// The + /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void EnsureNBits(int n, ref BufferProcessor bufferProcessor) + public void EnsureNBits(int n, ref InputProcessor inputProcessor) { - DecoderErrorCode errorCode = this.EnsureNBitsUnsafe(n, ref bufferProcessor); + DecoderErrorCode errorCode = this.EnsureNBitsUnsafe(n, ref inputProcessor); errorCode.EnsureNoError(); } @@ -51,13 +51,13 @@ namespace ImageSharp.Formats.Jpg /// This method does not throw. Returns instead. /// /// The number of bits to ensure. - /// The + /// The /// Error code - public DecoderErrorCode EnsureNBitsUnsafe(int n, ref BufferProcessor bufferProcessor) + public DecoderErrorCode EnsureNBitsUnsafe(int n, ref InputProcessor inputProcessor) { while (true) { - DecoderErrorCode errorCode = this.EnsureBitsStepImpl(ref bufferProcessor); + DecoderErrorCode errorCode = this.EnsureBitsStepImpl(ref inputProcessor); if (errorCode != DecoderErrorCode.NoError || this.UnreadBits >= n) { return errorCode; @@ -68,34 +68,34 @@ namespace ImageSharp.Formats.Jpg /// /// Unrolled version of for n==8 /// - /// The + /// The /// A - public DecoderErrorCode Ensure8BitsUnsafe(ref BufferProcessor bufferProcessor) + public DecoderErrorCode Ensure8BitsUnsafe(ref InputProcessor inputProcessor) { - return this.EnsureBitsStepImpl(ref bufferProcessor); + return this.EnsureBitsStepImpl(ref inputProcessor); } /// /// Unrolled version of for n==1 /// - /// The + /// The /// A - public DecoderErrorCode Ensure1BitUnsafe(ref BufferProcessor bufferProcessor) + public DecoderErrorCode Ensure1BitUnsafe(ref InputProcessor inputProcessor) { - return this.EnsureBitsStepImpl(ref bufferProcessor); + return this.EnsureBitsStepImpl(ref inputProcessor); } /// /// Receive extend /// /// Byte - /// The + /// The /// Read bits value [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int ReceiveExtend(int t, ref BufferProcessor bufferProcessor) + public int ReceiveExtend(int t, ref InputProcessor inputProcessor) { int x; - DecoderErrorCode errorCode = this.ReceiveExtendUnsafe(t, ref bufferProcessor, out x); + DecoderErrorCode errorCode = this.ReceiveExtendUnsafe(t, ref inputProcessor, out x); errorCode.EnsureNoError(); return x; } @@ -104,14 +104,14 @@ namespace ImageSharp.Formats.Jpg /// Receive extend /// /// Byte - /// The + /// The /// Read bits value /// The - public DecoderErrorCode ReceiveExtendUnsafe(int t, ref BufferProcessor bufferProcessor, out int x) + public DecoderErrorCode ReceiveExtendUnsafe(int t, ref InputProcessor inputProcessor, out int x) { if (this.UnreadBits < t) { - DecoderErrorCode errorCode = this.EnsureNBitsUnsafe(t, ref bufferProcessor); + DecoderErrorCode errorCode = this.EnsureNBitsUnsafe(t, ref inputProcessor); if (errorCode != DecoderErrorCode.NoError) { x = int.MaxValue; @@ -132,10 +132,10 @@ namespace ImageSharp.Formats.Jpg return DecoderErrorCode.NoError; } - private DecoderErrorCode EnsureBitsStepImpl(ref BufferProcessor bufferProcessor) + private DecoderErrorCode EnsureBitsStepImpl(ref InputProcessor inputProcessor) { int c; - DecoderErrorCode errorCode = bufferProcessor.Bytes.ReadByteStuffedByteUnsafe(bufferProcessor.InputStream, out c); + DecoderErrorCode errorCode = inputProcessor.Bytes.ReadByteStuffedByteUnsafe(inputProcessor.InputStream, out c); if (errorCode != DecoderErrorCode.NoError) { diff --git a/src/ImageSharp.Formats.Jpeg/Components/Decoder/HuffmanTree.cs b/src/ImageSharp.Formats.Jpeg/Components/Decoder/HuffmanTree.cs index cd8781a5f..abe7b1106 100644 --- a/src/ImageSharp.Formats.Jpeg/Components/Decoder/HuffmanTree.cs +++ b/src/ImageSharp.Formats.Jpeg/Components/Decoder/HuffmanTree.cs @@ -127,11 +127,11 @@ namespace ImageSharp.Formats.Jpg /// /// Internal part of the DHT processor, whatever does it mean /// - /// The decoder instance + /// The decoder instance /// The temporal buffer that holds the data that has been read from the Jpeg stream /// Remaining bits public void ProcessDefineHuffmanTablesMarkerLoop( - ref BufferProcessor bufferProcessor, + ref InputProcessor inputProcessor, byte[] defineHuffmanTablesData, ref int remaining) { @@ -163,7 +163,7 @@ namespace ImageSharp.Formats.Jpg throw new ImageFormatException("DHT has wrong length"); } - bufferProcessor.ReadFull(this.Values, 0, this.Length); + inputProcessor.ReadFull(this.Values, 0, this.Length); for (int i = 0; i < this.Values.Length; i++) { diff --git a/src/ImageSharp.Formats.Jpeg/Components/Decoder/BufferProcessor.cs b/src/ImageSharp.Formats.Jpeg/Components/Decoder/InputProcessor.cs similarity index 97% rename from src/ImageSharp.Formats.Jpeg/Components/Decoder/BufferProcessor.cs rename to src/ImageSharp.Formats.Jpeg/Components/Decoder/InputProcessor.cs index 6dc3c012c..60042d36f 100644 --- a/src/ImageSharp.Formats.Jpeg/Components/Decoder/BufferProcessor.cs +++ b/src/ImageSharp.Formats.Jpeg/Components/Decoder/InputProcessor.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // @@ -13,7 +13,7 @@ namespace ImageSharp.Formats.Jpg /// Encapsulates stream reading and processing data and operations for . /// It's a value type for imporved data locality, and reduced number of CALLVIRT-s /// - internal struct BufferProcessor : IDisposable + internal struct InputProcessor : IDisposable { /// /// Holds the unprocessed bits that have been taken from the byte-stream. @@ -26,11 +26,11 @@ namespace ImageSharp.Formats.Jpg public Bytes Bytes; /// - /// Initializes a new instance of the struct. + /// Initializes a new instance of the struct. /// /// The input /// Temporal buffer, same as - public BufferProcessor(Stream inputStream, byte[] temp) + public InputProcessor(Stream inputStream, byte[] temp) { this.Bits = default(Bits); this.Bytes = Bytes.Create(); diff --git a/src/ImageSharp.Formats.Jpeg/Components/Decoder/JpegScanDecoder.cs b/src/ImageSharp.Formats.Jpeg/Components/Decoder/JpegScanDecoder.cs index 472720f8f..2d292ddf9 100644 --- a/src/ImageSharp.Formats.Jpeg/Components/Decoder/JpegScanDecoder.cs +++ b/src/ImageSharp.Formats.Jpeg/Components/Decoder/JpegScanDecoder.cs @@ -198,7 +198,7 @@ namespace ImageSharp.Formats.Jpg int blockIndex = this.GetBlockIndex(decoder); this.data.Block = decoder.DecodedBlocks[this.ComponentIndex][blockIndex].Block; - if (!decoder.BufferProcessor.UnexpectedEndOfStreamReached) + if (!decoder.InputProcessor.UnexpectedEndOfStreamReached) { this.DecodeBlock(decoder, scanIndex); } @@ -218,10 +218,10 @@ namespace ImageSharp.Formats.Jpg { // A more sophisticated decoder could use RST[0-7] markers to resynchronize from corrupt input, // but this one assumes well-formed input, and hence the restart marker follows immediately. - if (!decoder.BufferProcessor.UnexpectedEndOfStreamReached) + if (!decoder.InputProcessor.UnexpectedEndOfStreamReached) { - DecoderErrorCode errorCode = decoder.BufferProcessor.ReadFullUnsafe(decoder.Temp, 0, 2); - if (decoder.BufferProcessor.CheckEOFEnsureNoError(errorCode)) + DecoderErrorCode errorCode = decoder.InputProcessor.ReadFullUnsafe(decoder.Temp, 0, 2); + if (decoder.InputProcessor.CheckEOFEnsureNoError(errorCode)) { if (decoder.Temp[0] != 0xff || decoder.Temp[1] != expectedRst) { @@ -237,7 +237,7 @@ namespace ImageSharp.Formats.Jpg } // Reset the Huffman decoder. - decoder.BufferProcessor.Bits = default(Bits); + decoder.InputProcessor.Bits = default(Bits); // Reset the DC components, as per section F.2.1.3.1. this.ResetDc(); @@ -293,7 +293,7 @@ namespace ImageSharp.Formats.Jpg throw new ImageFormatException("SOS has wrong length"); } - decoder.BufferProcessor.ReadFull(decoder.Temp, 0, remaining); + decoder.InputProcessor.ReadFull(decoder.Temp, 0, remaining); this.componentScanCount = decoder.Temp[0]; int scanComponentCountX2 = 2 * this.componentScanCount; @@ -355,7 +355,7 @@ namespace ImageSharp.Formats.Jpg int huffmannIdx = (AcTableIndex * HuffmanTree.ThRowSize) + this.pointers.ComponentScan[scanIndex].AcTableSelector; if (this.ah != 0) { - this.Refine(ref decoder.BufferProcessor, ref decoder.HuffmanTrees[huffmannIdx], 1 << this.al); + this.Refine(ref decoder.InputProcessor, ref decoder.HuffmanTrees[huffmannIdx], 1 << this.al); } else { @@ -367,10 +367,10 @@ namespace ImageSharp.Formats.Jpg // Decode the DC coefficient, as specified in section F.2.2.1. int value; int huffmanIndex = (DcTableIndex * HuffmanTree.ThRowSize) + this.pointers.ComponentScan[scanIndex].DcTableSelector; - errorCode = decoder.BufferProcessor.DecodeHuffmanUnsafe( + errorCode = decoder.InputProcessor.DecodeHuffmanUnsafe( ref decoder.HuffmanTrees[huffmanIndex], out value); - if (!decoder.BufferProcessor.CheckEOF(errorCode)) + if (!decoder.InputProcessor.CheckEOF(errorCode)) { return; } @@ -381,8 +381,8 @@ namespace ImageSharp.Formats.Jpg } int deltaDC; - errorCode = decoder.BufferProcessor.ReceiveExtendUnsafe(value, out deltaDC); - if (!decoder.BufferProcessor.CheckEOFEnsureNoError(errorCode)) + errorCode = decoder.InputProcessor.ReceiveExtendUnsafe(value, out deltaDC); + if (!decoder.InputProcessor.CheckEOFEnsureNoError(errorCode)) { return; } @@ -403,8 +403,8 @@ namespace ImageSharp.Formats.Jpg for (; zig <= this.zigEnd; zig++) { int value; - errorCode = decoder.BufferProcessor.DecodeHuffmanUnsafe(ref decoder.HuffmanTrees[huffmannIdx], out value); - if (!decoder.BufferProcessor.CheckEOF(errorCode)) + errorCode = decoder.InputProcessor.DecodeHuffmanUnsafe(ref decoder.HuffmanTrees[huffmannIdx], out value); + if (!decoder.InputProcessor.CheckEOF(errorCode)) { return; } @@ -420,8 +420,8 @@ namespace ImageSharp.Formats.Jpg } int ac; - errorCode = decoder.BufferProcessor.ReceiveExtendUnsafe(val1, out ac); - if (!decoder.BufferProcessor.CheckEOFEnsureNoError(errorCode)) + errorCode = decoder.InputProcessor.ReceiveExtendUnsafe(val1, out ac); + if (!decoder.InputProcessor.CheckEOFEnsureNoError(errorCode)) { return; } @@ -436,8 +436,8 @@ namespace ImageSharp.Formats.Jpg this.eobRun = (ushort)(1 << val0); if (val0 != 0) { - errorCode = this.DecodeEobRun(val0, ref decoder.BufferProcessor); - if (!decoder.BufferProcessor.CheckEOFEnsureNoError(errorCode)) + errorCode = this.DecodeEobRun(val0, ref decoder.InputProcessor); + if (!decoder.InputProcessor.CheckEOFEnsureNoError(errorCode)) { return; } @@ -454,7 +454,7 @@ namespace ImageSharp.Formats.Jpg } } - private DecoderErrorCode DecodeEobRun(int count, ref BufferProcessor decoder) + private DecoderErrorCode DecodeEobRun(int count, ref InputProcessor decoder) { int bitsResult; DecoderErrorCode errorCode = decoder.DecodeBitsUnsafe(count, out bitsResult); @@ -539,10 +539,10 @@ namespace ImageSharp.Formats.Jpg /// /// Decodes a successive approximation refinement block, as specified in section G.1.2. /// - /// The instance + /// The instance /// The Huffman tree /// The low transform offset - private void Refine(ref BufferProcessor bp, ref HuffmanTree h, int delta) + private void Refine(ref InputProcessor bp, ref HuffmanTree h, int delta) { Block8x8F* b = this.pointers.Block; @@ -668,12 +668,12 @@ namespace ImageSharp.Formats.Jpg /// Refines non-zero entries of b in zig-zag order. /// If >= 0, the first zero entries are skipped over. /// - /// The + /// The /// The zig-zag start index /// The non-zero entry /// The low transform offset /// The - private int RefineNonZeroes(ref BufferProcessor bp, int zig, int nz, int delta) + private int RefineNonZeroes(ref InputProcessor bp, int zig, int nz, int delta) { var b = this.pointers.Block; for (; zig <= this.zigEnd; zig++) diff --git a/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs b/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs index 609eb5f5f..127b399ab 100644 --- a/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs @@ -35,7 +35,7 @@ namespace ImageSharp.Formats /// Encapsulates stream reading and processing data and operations for . /// It's a value type for imporved data locality, and reduced number of CALLVIRT-s /// - public BufferProcessor BufferProcessor; + public InputProcessor InputProcessor; #pragma warning restore SA401 /// @@ -200,7 +200,7 @@ namespace ImageSharp.Formats } this.ycbcrImage?.Dispose(); - this.BufferProcessor.Dispose(); + this.InputProcessor.Dispose(); this.grayImage.ReturnPooled(); this.blackImage.ReturnPooled(); } @@ -274,10 +274,10 @@ namespace ImageSharp.Formats where TColor : struct, IPackedPixel, IEquatable { this.InputStream = stream; - this.BufferProcessor = new BufferProcessor(stream, this.Temp); + this.InputProcessor = new InputProcessor(stream, this.Temp); // Check for the Start Of Image marker. - this.BufferProcessor.ReadFull(this.Temp, 0, 2); + this.InputProcessor.ReadFull(this.Temp, 0, 2); if (this.Temp[0] != JpegConstants.Markers.XFF || this.Temp[1] != JpegConstants.Markers.SOI) { throw new ImageFormatException("Missing SOI marker."); @@ -289,7 +289,7 @@ namespace ImageSharp.Formats // we can't currently short circute progressive images so don't try. while (processBytes) { - this.BufferProcessor.ReadFull(this.Temp, 0, 2); + this.InputProcessor.ReadFull(this.Temp, 0, 2); while (this.Temp[0] != 0xff) { // Strictly speaking, this is a format error. However, libjpeg is @@ -310,7 +310,7 @@ namespace ImageSharp.Formats // Note that extraneous 0xff bytes in e.g. SOS data are escaped as // "\xff\x00", and so are detected a little further down below. this.Temp[0] = this.Temp[1]; - this.Temp[1] = this.BufferProcessor.ReadByte(); + this.Temp[1] = this.InputProcessor.ReadByte(); } byte marker = this.Temp[1]; @@ -324,7 +324,7 @@ namespace ImageSharp.Formats { // Section B.1.1.2 says, "Any marker may optionally be preceded by any // number of fill bytes, which are bytes assigned code X'FF'". - marker = this.BufferProcessor.ReadByte(); + marker = this.InputProcessor.ReadByte(); } // End Of Image. @@ -346,7 +346,7 @@ namespace ImageSharp.Formats // Read the 16-bit length of the segment. The value includes the 2 bytes for the // length itself, so we subtract 2 to get the number of remaining bytes. - this.BufferProcessor.ReadFull(this.Temp, 0, 2); + this.InputProcessor.ReadFull(this.Temp, 0, 2); int remaining = (this.Temp[0] << 8) + this.Temp[1] - 2; if (remaining < 0) { @@ -369,7 +369,7 @@ namespace ImageSharp.Formats case JpegConstants.Markers.DHT: if (metadataOnly) { - this.BufferProcessor.Skip(remaining); + this.InputProcessor.Skip(remaining); } else { @@ -380,7 +380,7 @@ namespace ImageSharp.Formats case JpegConstants.Markers.DQT: if (metadataOnly) { - this.BufferProcessor.Skip(remaining); + this.InputProcessor.Skip(remaining); } else { @@ -397,7 +397,7 @@ namespace ImageSharp.Formats // when this is a progressive image this gets called a number of times // need to know how many times this should be called in total. this.ProcessStartOfScan(remaining); - if (this.BufferProcessor.UnexpectedEndOfStreamReached || !this.IsProgressive) + if (this.InputProcessor.UnexpectedEndOfStreamReached || !this.IsProgressive) { // if unexpeced EOF reached or this is not a progressive image we can stop processing bytes as we now have the image data. processBytes = false; @@ -407,7 +407,7 @@ namespace ImageSharp.Formats case JpegConstants.Markers.DRI: if (metadataOnly) { - this.BufferProcessor.Skip(remaining); + this.InputProcessor.Skip(remaining); } else { @@ -428,7 +428,7 @@ namespace ImageSharp.Formats if ((marker >= JpegConstants.Markers.APP0 && marker <= JpegConstants.Markers.APP15) || marker == JpegConstants.Markers.COM) { - this.BufferProcessor.Skip(remaining); + this.InputProcessor.Skip(remaining); } else if (marker < JpegConstants.Markers.SOF0) { @@ -457,7 +457,7 @@ namespace ImageSharp.Formats { JpegScanDecoder scan = default(JpegScanDecoder); JpegScanDecoder.InitStreamReading(&scan, this, remaining); - this.BufferProcessor.Bits = default(Bits); + this.InputProcessor.Bits = default(Bits); this.MakeImage(); scan.DecodeBlocks(this); } @@ -918,11 +918,11 @@ namespace ImageSharp.Formats { if (remaining < 12) { - this.BufferProcessor.Skip(remaining); + this.InputProcessor.Skip(remaining); return; } - this.BufferProcessor.ReadFull(this.Temp, 0, 12); + this.InputProcessor.ReadFull(this.Temp, 0, 12); remaining -= 12; if (this.Temp[0] == 'A' && this.Temp[1] == 'd' && this.Temp[2] == 'o' && this.Temp[3] == 'b' @@ -934,7 +934,7 @@ namespace ImageSharp.Formats if (remaining > 0) { - this.BufferProcessor.Skip(remaining); + this.InputProcessor.Skip(remaining); } } @@ -949,12 +949,12 @@ namespace ImageSharp.Formats { if (remaining < 6) { - this.BufferProcessor.Skip(remaining); + this.InputProcessor.Skip(remaining); return; } byte[] profile = new byte[remaining]; - this.BufferProcessor.ReadFull(profile, 0, remaining); + this.InputProcessor.ReadFull(profile, 0, remaining); if (profile[0] == 'E' && profile[1] == 'x' && profile[2] == 'i' && profile[3] == 'f' && profile[4] == '\0' && profile[5] == '\0') @@ -971,11 +971,11 @@ namespace ImageSharp.Formats { if (remaining < 5) { - this.BufferProcessor.Skip(remaining); + this.InputProcessor.Skip(remaining); return; } - this.BufferProcessor.ReadFull(this.Temp, 0, 13); + this.InputProcessor.ReadFull(this.Temp, 0, 13); remaining -= 13; // TODO: We should be using constants for this. @@ -990,7 +990,7 @@ namespace ImageSharp.Formats if (remaining > 0) { - this.BufferProcessor.Skip(remaining); + this.InputProcessor.Skip(remaining); } } @@ -1008,7 +1008,7 @@ namespace ImageSharp.Formats throw new ImageFormatException("DHT has wrong length"); } - this.BufferProcessor.ReadFull(this.Temp, 0, 17); + this.InputProcessor.ReadFull(this.Temp, 0, 17); int tc = this.Temp[0] >> 4; if (tc > HuffmanTree.MaxTc) @@ -1024,7 +1024,7 @@ namespace ImageSharp.Formats int huffTreeIndex = (tc * HuffmanTree.ThRowSize) + th; this.HuffmanTrees[huffTreeIndex].ProcessDefineHuffmanTablesMarkerLoop( - ref this.BufferProcessor, + ref this.InputProcessor, this.Temp, ref remaining); } @@ -1042,7 +1042,7 @@ namespace ImageSharp.Formats throw new ImageFormatException("DRI has wrong length"); } - this.BufferProcessor.ReadFull(this.Temp, 0, remaining); + this.InputProcessor.ReadFull(this.Temp, 0, remaining); this.RestartInterval = ((int)this.Temp[0] << 8) + (int)this.Temp[1]; } @@ -1060,7 +1060,7 @@ namespace ImageSharp.Formats bool done = false; remaining--; - byte x = this.BufferProcessor.ReadByte(); + byte x = this.InputProcessor.ReadByte(); int tq = x & 0x0F; if (tq > MaxTq) { @@ -1077,7 +1077,7 @@ namespace ImageSharp.Formats } remaining -= Block8x8F.ScalarCount; - this.BufferProcessor.ReadFull(this.Temp, 0, Block8x8F.ScalarCount); + this.InputProcessor.ReadFull(this.Temp, 0, Block8x8F.ScalarCount); for (int i = 0; i < Block8x8F.ScalarCount; i++) { @@ -1093,7 +1093,7 @@ namespace ImageSharp.Formats } remaining -= 2 * Block8x8F.ScalarCount; - this.BufferProcessor.ReadFull(this.Temp, 0, 2 * Block8x8F.ScalarCount); + this.InputProcessor.ReadFull(this.Temp, 0, 2 * Block8x8F.ScalarCount); for (int i = 0; i < Block8x8F.ScalarCount; i++) { @@ -1143,7 +1143,7 @@ namespace ImageSharp.Formats throw new ImageFormatException("Incorrect number of components"); } - this.BufferProcessor.ReadFull(this.Temp, 0, remaining); + this.InputProcessor.ReadFull(this.Temp, 0, remaining); // We only support 8-bit precision. if (this.Temp[0] != 8)