Skip to content

Image.Load throws IndexOutOfRangeException (Tiff) #2435

Description

@joaovsoares

Prerequisites

  • I have bought a Commercial License
  • 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

3.0.1

Other ImageSharp packages and versions

No

Environment (Operating system, version and so on)

Linux

.NET Framework version

NET 6

Description

When loading some Tiffs, we received this exception.
I found a similar closed issue, but it seemed different to me.
The closed issue related a encode problem. Here it's a decode problem.

System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at SixLabors.ImageSharp.Formats.Tiff.TiffDecoderCore.DecodeTilesChunky[TPixel](ImageFrame`1 frame, Int32 tileWidth, Int32 tileLength, Int32 tilesAcross, Int32 tilesDown, Span`1 tileOffsets, Span`1 tileByteCounts, CancellationToken cancellationToken)
   at SixLabors.ImageSharp.Formats.Tiff.TiffDecoderCore.DecodeImageWithTiles[TPixel](ExifProfile tags, ImageFrame`1 frame, CancellationToken cancellationToken)
   at SixLabors.ImageSharp.Formats.Tiff.TiffDecoderCore.DecodeFrame[TPixel](ExifProfile tags, CancellationToken cancellationToken)
   at SixLabors.ImageSharp.Formats.Tiff.TiffDecoderCore.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.Tiff.TiffDecoder.Decode[TPixel](DecoderOptions options, Stream stream, CancellationToken cancellationToken)
   at SixLabors.ImageSharp.Formats.Tiff.TiffDecoder.Decode(DecoderOptions options, Stream stream, CancellationToken cancellationToken)
   at SixLabors.ImageSharp.Formats.ImageDecoder.<>c__DisplayClass1_0.<Decode>b__0(Stream s)
   at SixLabors.ImageSharp.Formats.ImageDecoder.<WithSeekableStream>g__PeformActionAndResetPosition|11_0[T](Stream s, Int64 position, <>c__DisplayClass11_0`1& )
   at SixLabors.ImageSharp.Formats.ImageDecoder.WithSeekableStream[T](DecoderOptions options, Stream stream, Func`2 action)
   at SixLabors.ImageSharp.Formats.ImageDecoder.Decode(DecoderOptions options, Stream stream)
   at SixLabors.ImageSharp.Image.Decode(DecoderOptions options, Stream stream)
   at SixLabors.ImageSharp.Image.<>c__DisplayClass80_0.<Load>b__0(Stream s)
   at SixLabors.ImageSharp.Image.WithSeekableStream[T](DecoderOptions options, Stream stream, Func`2 action)
   at SixLabors.ImageSharp.Image.Load(DecoderOptions options, Stream stream)
   at SixLabors.ImageSharp.Image.Load(Stream stream)

Steps to Reproduce

Load the provided images from a stream.
The error throws at Image.Load command.

public static MemoryStream ConverterPara(MemoryStream imagemStream)
{
    var pngEncoder = new PngEncoder()
    {
        FilterMethod = PngFilterMethod.Adaptive,
        CompressionLevel = PngCompressionLevel.BestCompression,
        TransparentColorMode = PngTransparentColorMode.Clear,
        ColorType = PngColorType.Grayscale,
        BitDepth = PngBitDepth.Bit4,
        SkipMetadata = true
    };

    imagemStream.Position = 0;
    var resultadoStream = new MemoryStream();

    using (var imagem = Image.Load(imagemStream))
    {
        using (var clone = imagem.CloneAs<L8>())
            clone.Save(resultadoStream, pngEncoder);
    }

    return resultadoStream;
}

Images

Lote.zip

Activity

  1. JimBobSquarePants commented on Apr 12, 2023

    @JimBobSquarePants
    Member

    Thanks for the images. Will have a look asap.

  2. brianpopow commented on Apr 12, 2023

    @brianpopow
    Collaborator

    Both images have none standard tile width according to tiffinfo

    $ tiffinfo 22212589.tiff
    22212589.tiff: Warning, Nonstandard tile width 2481, convert file.
    22212589.tiff: Warning, Nonstandard tile length 3542, convert file.
    === TIFF directory 0 ===
    TIFF Directory at offset 0x8 (8)
      Image Width: 2481 Image Length: 3542
      Tile Width: 2481 Tile Length: 3542
      Resolution: 300, 300 pixels/inch
      Bits/Sample: 1
      Compression Scheme: CCITT Group 4
      Photometric Interpretation: min-is-black
      FillOrder: msb-to-lsb
      Samples/Pixel: 1
      Planar Configuration: single image plane
      Group 4 Options: (0 = 0x0)
    
    $ tiffinfo 22217160.tiff
    22217160.tiff: Warning, Nonstandard tile width 2481, convert file.
    22217160.tiff: Warning, Nonstandard tile length 3542, convert file.
    === TIFF directory 0 ===
    TIFF Directory at offset 0x8 (8)
      Image Width: 2481 Image Length: 3542
      Tile Width: 2481 Tile Length: 3542
      Resolution: 300, 300 pixels/inch
      Bits/Sample: 1
      Compression Scheme: CCITT Group 4
      Photometric Interpretation: min-is-black
      FillOrder: msb-to-lsb
      Samples/Pixel: 1
      Planar Configuration: single image plane
      Group 4 Options: (0 = 0x0)
    
  3. joaovsoares commented on Apr 12, 2023

    @joaovsoares
    Author

    Both images have none standard tile width

    I don't really know what it means. Those images are invalid?
    I mean, both could be opened by other softwares.
    What I need to do?

    BTW: thanks for the answer.

  4. brianpopow commented on Apr 12, 2023

    @brianpopow
    Collaborator

    I still try to figure this out. My comment was more addressed towards @JimBobSquarePants who also wanted to figure this out (sorry for not being explicit here). I thought this may help debugging the issue. If other software/viewers can open it, we should also try to be able to decode it.

  5. brianpopow commented on Apr 13, 2023

    @brianpopow
    Collaborator

    I think that I found the issue.

    @joaovsoares would it be ok for you, if I use the provided test images in a unit test?
    I was able to create a similar test image.

    PR for the fix is open.

  6. added 2 commits that reference this issue on Apr 13, 2023
    8756c32
    93790ba
  7. joaovsoares commented on Apr 13, 2023

    @joaovsoares
    Author

    I think that I found the issue.

    @joaovsoares would it be ok for you, if I use the provided test images in a unit test? I was able to create a similar test image.

    PR for the fix is open.

    @brianpopow sure!
    Sorry for the delayed answer.

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