geotiffjs / geotiff.js

geotiff.js is a small library to parse TIFF files for visualization or analysis. It is written in pure JavaScript, and is usable in both the browser and node.js applications.
https://geotiffjs.github.io/
MIT License
857 stars 178 forks source link

Unable to decode tiled GeoTIFF #432

Open kristoffer-dyrkorn opened 1 month ago

kristoffer-dyrkorn commented 1 month ago

It seems geotiff.js 2.1.3 is not able to decode a tiled GeoTIFF file I wanted to parse using node.js (v 22.3.0).

It seems the reason is that the TileByteCounts and TileOffsets arrays in the File Directory object somehow end up having only zero values. And then the decoding of the image tiles fails.

The error occurs when calling the image.readRasters() method - but I suspect the root cause lies in the parsing of the File Directory information.

See attachment for a minimal code example - and data file - that can be used to reproduce the issue. testcase.zip

kristoffer-dyrkorn commented 1 month ago

Small update: It seems the two arrays start with zero values but do not have only zero values. I tried using exiftool to verify/read out the File Directory info from the test tiff, and geotiff.js and exiftool produce the same result. So I assume that this is correct. This means the File Directory parsing in tiff.getImage() is correct - and that the error happens in image.readRasters() when using the array values to decode the tiles.

kristoffer-dyrkorn commented 1 month ago

Have found out more about what happens when the code crashes:

In geotiffimage.js, line 485:

const buffer = tile.data;
const dataView = new DataView(buffer);

...the code creates a dataview that stores 0 bytes (the tile.data array has zero length).

In geotiffimage.js, line 500:

const value = reader.call(dataView, pixelOffset + srcSampleOffsets[si], littleEndian);

...the code tries to read out values from a zero-sized dataview, and this makes it fail.

Could it be that the code should just skip tiles where the TileByteCounts value is zero? Or is the data in the example tiff file inherently wrong? (Both GDAL and QGIS can successfully open the file.)