haraldk / TwelveMonkeys

TwelveMonkeys ImageIO: Additional plug-ins and extensions for Java's ImageIO
https://haraldk.github.io/TwelveMonkeys/
BSD 3-Clause "New" or "Revised" License
1.83k stars 308 forks source link

ArrayIndexOutOfBoundsException in HorizontalDeDifferencingFloatingPointStream.decodeDeltaBytes #972

Open willivolt opened 1 week ago

willivolt commented 1 week ago

Describe the bug Reading the shapes_lzw_predictor3.tif from https://github.com/tlnagy/exampletiffs produces an ArrayIndexOutOfBoundsException.

Version information

  1. The version of the TwelveMonkeys ImageIO library in use. 3.11.0

  2. The exact output of java --version (or java -version for older Java releases). openjdk version "11.0.19" 2023-04-18 OpenJDK Runtime Environment Temurin-11.0.19+7 (build 11.0.19+7) OpenJDK 64-Bit Server VM Temurin-11.0.19+7 (build 11.0.19+7, mixed mode)

Example code BufferedImage inImage = ImageIO.read(new File("shapes_lzw_predictor3.tif"));

Sample file(s) shapes_lzw_predictor3.zip

Stak trace

java.lang.ArrayIndexOutOfBoundsException: Index 1536 out of bounds for length 1536
    at com.twelvemonkeys.imageio.plugins.tiff.HorizontalDeDifferencingFloatingPointStream.decodeDeltaBytes(HorizontalDeDifferencingFloatingPointStream.java:117)
    at com.twelvemonkeys.imageio.plugins.tiff.HorizontalDeDifferencingFloatingPointStream.decodeFloatingPointDelta(HorizontalDeDifferencingFloatingPointStream.java:124)
    at com.twelvemonkeys.imageio.plugins.tiff.HorizontalDeDifferencingFloatingPointStream.decodeRow(HorizontalDeDifferencingFloatingPointStream.java:140)
    at com.twelvemonkeys.imageio.plugins.tiff.HorizontalDeDifferencingFloatingPointStream.fetch(HorizontalDeDifferencingFloatingPointStream.java:102)
    at com.twelvemonkeys.imageio.plugins.tiff.HorizontalDeDifferencingFloatingPointStream.read(HorizontalDeDifferencingFloatingPointStream.java:158)
    at com.twelvemonkeys.imageio.stream.DirectImageInputStream.read(DirectImageInputStream.java:73)
    at java.desktop/javax.imageio.stream.ImageInputStreamImpl.readFully(ImageInputStreamImpl.java:351)
    at java.desktop/javax.imageio.stream.ImageInputStreamImpl.readFully(ImageInputStreamImpl.java:437)
    at com.twelvemonkeys.imageio.plugins.tiff.TIFFImageReader.readStripTileData(TIFFImageReader.java:2059)
    at com.twelvemonkeys.imageio.plugins.tiff.TIFFImageReader.read(TIFFImageReader.java:1154)
    at com.twelvemonkeys.imageio.plugins.tiff.TIFFImageReader.read(TIFFImageReader.java:936)
    at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1468)
    at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1315)
haraldk commented 6 days ago

Hi William,

Thanks for reporting!

There is indeed a bug in the predictor stream implementation for FP.... It seems it was only tested for single channel (gray) data, where the bug didn't matter.

If you don't mind, I'll add the image to the test cases. Will issue a fix as soon as possible.

willivolt commented 6 days ago

Hello Harald,

Thanks for checking. This not my file. The tlnagy/exampletiffs repo it is from says it is under BSD 3-clause license.

For what it's worth, I am also having some issue with the house.tif from that repo, but I have not yet determined if it is in the decoding, the scaling or the re-encoding to jpeg.

haraldk commented 5 days ago

Understood, BSD is fine.

house.tif is has an interesting combination of BlacIsZero, 2 samples per pixel and an ExtraSamples value [999] (legal values are 0, 1 or 2)... But there seems to be a bug decoding it, not taking the extra samples into account. I'll try to fix this as well.