diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs index c7e26e04a..875f16ec2 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs @@ -191,7 +191,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort where TPixel : struct, IPixel { this.ParseStream(stream); - return this.PostProcessIntoImage(); } @@ -202,7 +201,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort public IImageInfo Identify(Stream stream) { this.ParseStream(stream, true); - return new ImageInfo(new PixelTypeInfo(this.BitsPerPixel), this.ImageWidth, this.ImageHeight, this.MetaData); } @@ -361,11 +359,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort break; case OrigJpegConstants.Markers.SOS: - if (metadataOnly) - { - this.InputProcessor.Skip(remaining); - } - else + if (!metadataOnly) { this.ProcessStartOfScanMarker(remaining); if (this.InputProcessor.ReachedEOF) @@ -374,8 +368,15 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort processBytes = false; } } + else + { + // It's highly unlikely that APPn related data will be found after the SOS marker + // We should have gathered everything we need by now. + processBytes = false; + } break; + case OrigJpegConstants.Markers.DRI: if (metadataOnly) { diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs index b43c51e0a..261cd61b1 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs @@ -412,19 +412,19 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private bool TryReadBit(DoubleBufferedStreamReader stream, out int bits) + private bool TryReadBit(DoubleBufferedStreamReader stream, out int bit) { if (this.bitsCount == 0) { if (!this.TryFillBits(stream)) { - bits = 0; + bit = 0; return false; } } this.bitsCount--; - bits = (this.bitsData >> this.bitsCount) & 1; + bit = (this.bitsData >> this.bitsCount) & 1; return true; } diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs index 772d07f33..df803a920 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs @@ -185,6 +185,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort where TPixel : struct, IPixel { this.ParseStream(stream); + this.AssignResolution(); return this.PostProcessIntoImage(); } @@ -195,6 +196,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort public IImageInfo Identify(Stream stream) { this.ParseStream(stream, true); + this.AssignResolution(); return new ImageInfo(new PixelTypeInfo(this.BitsPerPixel), this.ImageWidth, this.ImageHeight, this.MetaData); } @@ -244,17 +246,18 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort break; case PdfJsJpegConstants.Markers.SOS: - if (metadataOnly) + if (!metadataOnly) { - this.InputStream.Skip(remaining); + this.ProcessStartOfScanMarker(); + break; } else { - this.ProcessStartOfScanMarker(); + // It's highly unlikely that APPn related data will be found after the SOS marker + // We should have gathered everything we need by now. + return; } - break; - case PdfJsJpegConstants.Markers.DHT: if (metadataOnly) { @@ -331,11 +334,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort // Read on. fileMarker = FindNextFileMarker(this.markerBuffer, this.InputStream); } - - this.ImageWidth = this.Frame.SamplesPerLine; - this.ImageHeight = this.Frame.Scanlines; - this.ComponentCount = this.Frame.ComponentCount; - this.AssignResolution(); } /// @@ -389,6 +387,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort /// private void AssignResolution() { + this.ImageWidth = this.Frame.SamplesPerLine; + this.ImageHeight = this.Frame.Scanlines; + if (this.jFif.XDensity > 0 && this.jFif.YDensity > 0) { this.MetaData.HorizontalResolution = this.jFif.XDensity; @@ -633,6 +634,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort int maxV = 0; int index = 6; + this.ComponentCount = this.Frame.ComponentCount; if (!metadataOnly) { // No need to pool this. They max out at 4