ngageoint / tiff-java

Tagged Image File Format Java Library
https://ngageoint.github.io/tiff-java
MIT License
72 stars 33 forks source link

Tiff file lzw compression bit size needs to be capped at 12 #58

Open dave-price-parsons opened 3 weeks ago

dave-price-parsons commented 3 weeks ago

Parsing a geotiff from the nrlssc geoint tileserver would often throw exceptions, sometimes about the byte stream ending early or other problems with the bytes. The happens on most geotiff downloads from that site.

mil.nga.tiff.util.TiffException: No more remaining bytes to read. Total Bytes: 5707, Byte offset: 5704, Attempted to read: 4 at mil.nga.tiff.io.ByteReader.verifyRemainingBytes(ByteReader.java:415) at mil.nga.tiff.io.ByteReader.readFloat(ByteReader.java:364) at mil.nga.tiff.io.ByteReader.readFloat(ByteReader.java:351) at mil.nga.tiff.FileDirectory.readValue(FileDirectory.java:1353) at mil.nga.tiff.FileDirectory.readRaster(FileDirectory.java:1294) at mil.nga.tiff.FileDirectory.readRasters(FileDirectory.java:1220) at mil.nga.tiff.FileDirectory.readRasters(FileDirectory.java:1067) at mil.nga.tiff.FileDirectory.readRasters(FileDirectory.java:1019) at mil.nga.tiff.FileDirectory.readRasters(FileDirectory.java:998) at mil.nga.tiff.TiffReadTest.testLzw12BitMax(TiffReadTest.java:396)

Steps to Reproduce:

Download a geotiff from the nrlssc geoint tileserver. Example: https://geoint.nrlssc.org/nrltileserver/wcs/tlorigin?REQUEST=GetCoverage&VERSION=1.0.0&SERVICE=WCS&COVERAGE=USGS_NED_10m&FORMAT=GeoTIFF&BoundingBox=-117.55,33.41,-117.5,33.46&WIDTH=555&HEIGHT=555

TiffReader.readTiff( file ).getFileDirectory().readRasters();

An exception will probably be thrown about parsing the bytes.

This file can successfully be read using Java's native tiff reading as well as with GIMP.

Comparing this implementation to java's native implementation, I notice the bit size is capped at 12 in java's native implementation. Adding that cap to this code allows the successful parsing of the tiff file.