ngageoint / tiff-java

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

Crash when reading TIFF file #5

Closed midikiman closed 7 years ago

midikiman commented 7 years ago

Please fill out as much known and relevant information as possible.

Version Information:

Expected Results:

Observed Results:

Output:

-- File Directory --

NewSubfileType (254) LONG (4 bytes) Count: 1 Values: 0

ImageWidth (256) LONG (4 bytes) Count: 1 Values: 256

ImageLength (257) LONG (4 bytes) Count: 1 Values: 256

BitsPerSample (258) SHORT (2 bytes) Count: 4 Values: [16, 16, 16, 16]

Compression (259) SHORT (2 bytes) Count: 1 Values: 1

PhotometricInterpretation (262) SHORT (2 bytes) Count: 1 Values: 2

ImageDescription (270) ASCII (1 bytes) Count: 25 Values: [{"shape": [256, 256, 4]}]

StripOffsets (273) LONG (4 bytes) Count: 1 Values: [304]

SamplesPerPixel (277) SHORT (2 bytes) Count: 1 Values: 4

RowsPerStrip (278) LONG (4 bytes) Count: 1 Values: 256

StripByteCounts (279) LONG (4 bytes) Count: 1 Values: [524288]

PlanarConfiguration (284) SHORT (2 bytes) Count: 1 Values: 1

Software (305) ASCII (1 bytes) Count: 12 Values: [tifffile.py]

DateTime (306) ASCII (1 bytes) Count: 20 Values: [2017:04:26 23:11:27]

ExtraSamples (338) SHORT (2 bytes) Count: 1 Values: [1]

SampleFormat (339) SHORT (2 bytes) Count: 1 Values: [1] Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at mil.nga.tiff.FileDirectory.getFieldTypeForSample(FileDirectory.java:1274) at mil.nga.tiff.FileDirectory.readRaster(FileDirectory.java:1149) at mil.nga.tiff.FileDirectory.readRasters(FileDirectory.java:1109) at mil.nga.tiff.FileDirectory.readRasters(FileDirectory.java:979) at mil.nga.tiff.FileDirectory.readRasters(FileDirectory.java:931) at mil.nga.tiff.FileDirectory.readRasters(FileDirectory.java:910) at mil.nga.tiff.TiffFileTester.main(TiffFileTester.java:60)

BUILD FAILURE

Steps to Reproduce:

  1. Run TiffFileTester on the file (or just try to load it directly)
  2. readRasters() fails.

Relevant Code:

// Code to reproduce the problem?
        File input = new File("d:/kaggle/amazon_planet/train-tif-v2/train_0.tif");
        TIFFImage tiffImage = TiffReader.readTiff(input);
        List<FileDirectory> directories = tiffImage.getFileDirectories();
        FileDirectory directory = directories.get(0);
        Rasters rasters = directory.readRasters();

Or see TiffFileTester

Test Files:

Additional Information:

midikiman commented 7 years ago

On doing some investigation into the tags, it looks like some TIFF processors play fast and loose with the SampleFormat tag. There should be the same number of entries as SamplesPerPixel, but when the samples are all of the same type only one is stored. I made a simple fix to handle it for my application, not sure how robust or general it is.

bosborn commented 7 years ago

I came to the same conclusion as you. Made a fix on the develop branch here: 591433f Fix will go out on the next release.

TIFF Image: train_0.tif

-- File Directory --

NewSubfileType (254)
LONG (4 bytes)
Count: 1
Values: 0

ImageWidth (256)
LONG (4 bytes)
Count: 1
Values: 256

ImageLength (257)
LONG (4 bytes)
Count: 1
Values: 256

BitsPerSample (258)
SHORT (2 bytes)
Count: 4
Values: [16, 16, 16, 16]

Compression (259)
SHORT (2 bytes)
Count: 1
Values: 1

PhotometricInterpretation (262)
SHORT (2 bytes)
Count: 1
Values: 2

ImageDescription (270)
ASCII (1 bytes)
Count: 25
Values: [{"shape": [256, 256, 4]}]

StripOffsets (273)
LONG (4 bytes)
Count: 1
Values: [304]

SamplesPerPixel (277)
SHORT (2 bytes)
Count: 1
Values: 4

RowsPerStrip (278)
LONG (4 bytes)
Count: 1
Values: 256

StripByteCounts (279)
LONG (4 bytes)
Count: 1
Values: [524288]

PlanarConfiguration (284)
SHORT (2 bytes)
Count: 1
Values: 1

Software (305)
ASCII (1 bytes)
Count: 12
Values: [tifffile.py]

DateTime (306)
ASCII (1 bytes)
Count: 20
Values: [2017:04:26 23:11:27]

ExtraSamples (338)
SHORT (2 bytes)
Count: 1
Values: [1]

SampleFormat (339)
SHORT (2 bytes)
Count: 1
Values: [1]

-- Rasters --

Width: 256
Height: 256
Number of Pixels: 65536
Samples Per Pixel: 4
Bits Per Sample: [16, 16, 16, 16]

Pixel x = 0, y = 0: [5264, 4412, 2859, 6621]
Pixel x = 128, y = 128: [5267, 4408, 2837, 6335]
Pixel x = 255, y = 255: [5205, 4340, 2824, 6281]
gdallocationinfo train_0.tif 0 0
Report:
  Location: (0P,0L)
  Band 1:
    Value: 5264
  Band 2:
    Value: 4412
  Band 3:
    Value: 2859
  Band 4:
    Value: 6621

gdallocationinfo train_0.tif 128 128
Report:
  Location: (128P,128L)
  Band 1:
    Value: 5267
  Band 2:
    Value: 4408
  Band 3:
    Value: 2837
  Band 4:
    Value: 6335

gdallocationinfo train_0.tif 255 255
Report:
  Location: (255P,255L)
  Band 1:
    Value: 5205
  Band 2:
    Value: 4340
  Band 3:
    Value: 2824
  Band 4:
    Value: 6281
cgohlke commented 7 years ago

I'll correct this SampleFormat bug in tifffile.py. Thanks.

bosborn commented 7 years ago

Fixed in release 1.0.2 (45aa75e)