Skip to content

InvalidImageContentException "Input stream does not have enough bytes to parse declared contents of the 00 marker" when loading JPG created by Brother scanner #2136

Description

@w-michal

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

ImageSharp version

2.1.2

Other ImageSharp packages and versions

N/A

Environment (Operating system, version and so on)

Windows 10 x64, 21H2

.NET Framework version

net6.0

Description

Looks like my scanner (Brother 920dw) creates broken JPEGs by default...

Call stack

An unhandled exception of type 'SixLabors.ImageSharp.InvalidImageContentException' occurred in SixLabors.ImageSharp.dll: 'Input stream does not have enough bytes to parse declared contents of the 00 marker.'
   at SixLabors.ImageSharp.Formats.Jpeg.JpegThrowHelper.ThrowNotEnoughBytesForMarker(Byte marker)
   at SixLabors.ImageSharp.Formats.Jpeg.JpegDecoderCore.ParseStream(BufferedReadStream stream, HuffmanScanDecoder scanDecoder, CancellationToken cancellationToken)
   at SixLabors.ImageSharp.Formats.Jpeg.JpegDecoderCore.Decode[TPixel](BufferedReadStream stream, CancellationToken cancellationToken)
   at SixLabors.ImageSharp.Formats.ImageDecoderUtilities.Decode[TPixel](IImageDecoderInternals decoder, Configuration configuration, Stream stream, Func`3 largeImageExceptionFactory, CancellationToken cancellationToken)
   at SixLabors.ImageSharp.Formats.ImageDecoderUtilities.Decode[TPixel](IImageDecoderInternals decoder, Configuration configuration, Stream stream, CancellationToken cancellationToken)
   at SixLabors.ImageSharp.Formats.Jpeg.JpegDecoder.Decode[TPixel](Configuration configuration, Stream stream, CancellationToken cancellationToken)
   at SixLabors.ImageSharp.Formats.Jpeg.JpegDecoder.Decode(Configuration configuration, Stream stream, CancellationToken cancellationToken)
   at SixLabors.ImageSharp.Image.Decode(Stream stream, Configuration config, CancellationToken cancellationToken)
   at SixLabors.ImageSharp.Image.<>c__DisplayClass134_0.<Load>b__0(Stream s)
   at SixLabors.ImageSharp.Image.WithSeekableStream[T](Configuration configuration, Stream stream, Func`2 action)
   at SixLabors.ImageSharp.Image.Load(Configuration configuration, Stream stream, IImageFormat& format)
   at SixLabors.ImageSharp.Image.Load(Configuration configuration, String path, IImageFormat& format)
   at SixLabors.ImageSharp.Image.Load(Configuration configuration, String path)
   at SixLabors.ImageSharp.Image.Load(String path)

On top of that, ImageMagik reports more warnings. Command identify.exe -regard-warnings IMG_0012.JPG output:

IMG_0012.JPG JPEG 2544x3312 2544x3312+0+0 8-bit sRGB 826538B 0.047u 0:00.062
identify.exe: Corrupt JPEG data: 1418 extraneous bytes before marker 0xd7 `IMG_0012.JPG' @ warning/jpeg.c/JPEGWarningHandler/403.
identify.exe: Corrupt JPEG data: 1200 extraneous bytes before marker 0xd0 `IMG_0012.JPG' @ warning/jpeg.c/JPEGWarningHandler/403.
identify.exe: Corrupt JPEG data: 1169 extraneous bytes before marker 0xd1 `IMG_0012.JPG' @ warning/jpeg.c/JPEGWarningHandler/403.
identify.exe: Corrupt JPEG data: 1169 extraneous bytes before marker 0xd2 `IMG_0012.JPG' @ warning/jpeg.c/JPEGWarningHandler/403.
identify.exe: Corrupt JPEG data: 4197 extraneous bytes before marker 0xd3 `IMG_0012.JPG' @ warning/jpeg.c/JPEGWarningHandler/403.
identify.exe: Corrupt JPEG data: 1418 extraneous bytes before marker 0xd4 `IMG_0012.JPG' @ warning/jpeg.c/JPEGWarningHandler/403.
identify.exe: Corrupt JPEG data: 1200 extraneous bytes before marker 0xd5 `IMG_0012.JPG' @ warning/jpeg.c/JPEGWarningHandler/403.
identify.exe: Corrupt JPEG data: 1169 extraneous bytes before marker 0xd6 `IMG_0012.JPG' @ warning/jpeg.c/JPEGWarningHandler/403.
identify.exe: Corrupt JPEG data: 1169 extraneous bytes before marker 0xd9 `IMG_0012.JPG' @ warning/jpeg.c/JPEGWarningHandler/403.

Steps to Reproduce

Call Image.Load on provided image.

Images

IMG_0012.zip

Activity

  1. br3aker commented on May 30, 2022

    @br3aker
    Contributor

    Yes, your file is corrupted but it still can be decoded. For whatever reason scanner appends some data to scan segment - 'N extraneous bytes' in each restart marker ('0xd0-0xd7). But it's not that bad, the real problem lies in the last bytes of the image:

    FF 00 7D 0A F9 2B F6 B6 91 64 F8 8F A7 14 60 C0 69 31 8E 3F EB B4 D5 A4 66 A5 B3 22 4A C7 89 D1 56 48 51 40 05 14 00 51 40 1F FF D9 69
    

    According to the specs, any FFXX hex sequence out of scan segment is considered a marker which is not true in this specific situation. Most likely it's part of the 'extraneous bytes'.

    I'll take a look at our decoder RST markers behaviour, most likely it can be solved there.

  2. br3aker commented on Jun 5, 2022

    @br3aker
    Contributor

    Found a problem, scan segment has a lot of unused bytes starting from this position:

    Found forbidden marker: 0 at 807155
    Found forbidden marker: 0 at 807243
    Found forbidden marker: 0 at 807278
    Found forbidden marker: 0 at 807469
    Found forbidden marker: 0 at 807588
    Found forbidden marker: 0 at 807900
    Found forbidden marker: 0 at 807965
    Found forbidden marker: 215 at 808073
    ...
    
  3. brianpopow commented on Jun 6, 2022

    @brianpopow
    Collaborator

    @w-michal thanks to @br3aker this is fixed now with #2147, but maybe you should report that to the brother scanner support. Those jpeg's are weird.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions