diff --git a/src/ImageSharp.Formats.Jpeg/Components/Decoder/Bits.cs b/src/ImageSharp.Formats.Jpeg/Components/Decoder/Bits.cs index 36344b75a..1eea1417f 100644 --- a/src/ImageSharp.Formats.Jpeg/Components/Decoder/Bits.cs +++ b/src/ImageSharp.Formats.Jpeg/Components/Decoder/Bits.cs @@ -5,6 +5,7 @@ namespace ImageSharp.Formats.Jpg { + using System; using System.Runtime.CompilerServices; /// @@ -53,39 +54,60 @@ namespace ImageSharp.Formats.Jpg /// The number of bits to ensure. /// Jpeg decoder /// Error code - [MethodImpl(MethodImplOptions.AggressiveInlining)] + //[MethodImpl(MethodImplOptions.AggressiveInlining)] public DecoderErrorCode EnsureNBitsUnsafe(int n, JpegDecoderCore decoder) { while (true) { - // Grab the decode bytes, use them and then set them - // back on the decoder. - Bytes decoderBytes = decoder.Bytes; - byte c; - DecoderErrorCode errorCode = decoderBytes.ReadByteStuffedByteUnsafe(decoder.InputStream, out c); - decoder.Bytes = decoderBytes; - - if (errorCode != DecoderErrorCode.NoError) + DecoderErrorCode errorCode = this.EnsureBitsStepImpl(decoder); + if (errorCode != DecoderErrorCode.NoError || this.UnreadBits >= n) { return errorCode; } + } + } - this.Accumulator = (this.Accumulator << 8) | c; - this.UnreadBits += 8; - if (this.Mask == 0) - { - this.Mask = 1 << 7; - } - else - { - this.Mask <<= 8; - } + /// + /// Unrolled version of for n==8 + /// + /// + /// + public DecoderErrorCode Ensure8BitsUnsafe(JpegDecoderCore decoder) + { + return this.EnsureBitsStepImpl(decoder); + } - if (this.UnreadBits >= n) - { - return DecoderErrorCode.NoError; - } + /// + /// Unrolled version of for n==1 + /// + /// + /// + public DecoderErrorCode Ensure1BitUnsafe(JpegDecoderCore decoder) + { + return this.EnsureBitsStepImpl(decoder); + } + + private DecoderErrorCode EnsureBitsStepImpl(JpegDecoderCore decoder) + { + byte c; + DecoderErrorCode errorCode = decoder.Bytes.ReadByteStuffedByteUnsafe(decoder.InputStream, out c); + + if (errorCode != DecoderErrorCode.NoError) + { + return errorCode; + } + + this.Accumulator = (this.Accumulator << 8) | c; + this.UnreadBits += 8; + if (this.Mask == 0) + { + this.Mask = 1 << 7; + } + else + { + this.Mask <<= 8; } + return errorCode; } /// diff --git a/src/ImageSharp.Formats.Jpeg/Components/Decoder/Bytes.cs b/src/ImageSharp.Formats.Jpeg/Components/Decoder/Bytes.cs index 6ca017f4e..27f902aa1 100644 --- a/src/ImageSharp.Formats.Jpeg/Components/Decoder/Bytes.cs +++ b/src/ImageSharp.Formats.Jpeg/Components/Decoder/Bytes.cs @@ -144,7 +144,7 @@ namespace ImageSharp.Formats.Jpg /// Input stream /// The result as out parameter /// The - [MethodImpl(MethodImplOptions.AggressiveInlining)] + //[MethodImpl(MethodImplOptions.AggressiveInlining)] public DecoderErrorCode ReadByteUnsafe(Stream inputStream, out byte result) { DecoderErrorCode errorCode = DecoderErrorCode.NoError; @@ -184,7 +184,7 @@ namespace ImageSharp.Formats.Jpg /// /// Input stream /// The - [MethodImpl(MethodImplOptions.AggressiveInlining)] + //[MethodImpl(MethodImplOptions.AggressiveInlining)] public DecoderErrorCode FillUnsafe(Stream inputStream) { if (this.I != this.J) diff --git a/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs b/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs index 7d6cfba52..1a039191e 100644 --- a/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs @@ -229,7 +229,8 @@ namespace ImageSharp.Formats { if (this.Bits.UnreadBits == 0) { - DecoderErrorCode errorCode = this.Bits.EnsureNBitsUnsafe(1, this); + //DecoderErrorCode errorCode = this.Bits.EnsureNBitsUnsafe(1, this); + DecoderErrorCode errorCode = this.Bits.Ensure1BitUnsafe(this); if (errorCode != DecoderErrorCode.NoError) { result = false; @@ -338,7 +339,8 @@ namespace ImageSharp.Formats if (this.Bits.UnreadBits < 8) { - DecoderErrorCode errorCode = this.Bits.EnsureNBitsUnsafe(8, this); + //DecoderErrorCode errorCode = this.Bits.EnsureNBitsUnsafe(8, this); + DecoderErrorCode errorCode = this.Bits.Ensure8BitsUnsafe(this); if (errorCode == DecoderErrorCode.NoError) { diff --git a/src/ImageSharp.Formats.Jpeg/project.json b/src/ImageSharp.Formats.Jpeg/project.json index e0fdeeef2..9a704b4b4 100644 --- a/src/ImageSharp.Formats.Jpeg/project.json +++ b/src/ImageSharp.Formats.Jpeg/project.json @@ -42,10 +42,10 @@ "target": "project", "version": "1.0.0-*" }, - "StyleCop.Analyzers": { - "version": "1.1.0-beta001", - "type": "build" - }, + //"StyleCop.Analyzers": { + // "version": "1.1.0-beta001", + // "type": "build" + //}, "System.Buffers": "4.0.0", "System.Runtime.CompilerServices.Unsafe": "4.0.0" }, diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs index 37aff8338..5776bf2e4 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs @@ -40,31 +40,5 @@ namespace ImageSharp.Tests provider.Utility.SaveTestOutputFile(image, "bmp"); } - - unsafe struct Buzisag - { - public int Value; - - public delegate void BlockAction(Buzisag* b); - - public static void Foo(Buzisag* buzisag) - { - Bar(buzisag, b => b->Value++); - } - - public static void Bar(Buzisag* buzisag, BlockAction action) - { - action(buzisag); - } - } - - [Fact] - public unsafe void Kabbe() - { - Buzisag b = default(Buzisag); - Buzisag.Foo(&b); - - Assert.Equal(1, b.Value); - } } } \ No newline at end of file