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.9k stars 314 forks source link

TIFF: Exception when reading region of a tiled float32 BigTIFF #746

Open rfreeman853 opened 1 year ago

rfreeman853 commented 1 year ago

This code fragment raises an exception with some specific files:


    try {
      ImageInputStream input = ImageIO.createImageInputStream(file);
      ImageReader reader = ImageIO.getImageReaders(input).next();
      System.out.println( "Using reader: " + reader );

      reader.setInput(input);
      int width  = reader.getWidth(reader.getMinIndex());
      int height = reader.getHeight(reader.getMinIndex());
      System.out.println( "width, height: " + width + ", " + height );

      ImageReadParam param = reader.getDefaultReadParam();
      Rectangle rect = new Rectangle(256, 256, 2, 2);
      param.setSourceRegion(rect);
      System.out.println( "sourceRegion:    " + param.getSourceRegion().toString() );
      BufferedImage image = reader.read(0, param);
    }
    catch (Exception e) {
      System.out.println( "Exception:" );
      e.printStackTrace();
    }

When the requested rectangle is inside the initial tile in the image, the result is java.lang.ArrayIndexOutOfBoundsException with the message 'Coordinate out of bounds!'. Outside the first tile (as above) the output is:

Using reader: com.twelvemonkeys.imageio.plugins.tiff.TIFFImageReader@76ccd017
width, height: 58000, 66500
sourceRegion:    java.awt.Rectangle[x=256,y=256,width=2,height=2]
Exception:
java.awt.image.RasterFormatException: (parentX + width) is outside raster
        at java.desktop/java.awt.image.Raster.createChild(Raster.java:1526)
        at com.twelvemonkeys.imageio.plugins.tiff.TIFFImageReader.clipRowToRect(TIFFImageReader.java:1909)
        at com.twelvemonkeys.imageio.plugins.tiff.TIFFImageReader.read(TIFFImageReader.java:1094)
        at smallest_test.main(smallest_test.java:58)

The BigTIFF file is available from this page: https://www.data.gov.uk/dataset/8311f42d-bddd-4cd4-98a3-e543de5be4cb/lidar-composite-dtm-2019-10m Beware: the download is 3.6Gb; the file is 15.5Gb.

On that page, there is a message about a newer version of the BigTIFF. That file has the same effect.

An observation: the number of pixels in the two files is between 2^31 and 2^32. That might cause a signed integer to become negative. No idea if that's useful.

haraldk commented 1 year ago

Thanks!

I'll have a look! Haven't tested with such large rasters, but in theory it should work... I'll see if I can find the issue.

Update: Seems the server responds with 500 Internal Server Error at the moment. I'll come by later...

haraldk commented 1 year ago

Okay, seems there is a bug in the clipRowToRect method in relation to a combination of source region and tiles... I'll find a fix.

rfreeman853 commented 1 year ago

Thanks for your efforts on this!