ngageoint / tiff-java

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

tiff.util.TiffException: Unsupported field type for sample format: 1, bits per sample: 1 #28

Closed KnIfER closed 5 years ago

KnIfER commented 5 years ago

Could you add support for the tiff image below ? It's black and white, and thus very small.

sample image (rename to *.tif)

Version Information:

Steps to Reproduce:

       TIFFImage tiffImage = TiffReader.readTiff(ByteUtils.fileToBytes(new File("abc.tif")));
       List<FileDirectory> directories = tiffImage.getFileDirectories();
       FileDirectory directory = directories.get(0);
       Rasters rasters = directory.readRasters();  //crash

console log:

Exception in thread "main" tiff.util.TiffException: Unsupported field type for sample format: 1, bits per sample: 1
    at tiff.FieldType.getFieldType(FieldType.java:189)
    at tiff.FileDirectory.getFieldTypeForSample(FileDirectory.java:1292)
    at tiff.FileDirectory.readRasters(FileDirectory.java:1119)
    at tiff.FileDirectory.readRasters(FileDirectory.java:981)
    at tiff.FileDirectory.readRasters(FileDirectory.java:933)
    at tiff.FileDirectory.readRasters(FileDirectory.java:912)
    at tiff.testtiff.main(testtiff.java:19)

Test Files:

more_samples (first 8)

bosborn commented 5 years ago

This implementation only works for bits per samples that are divisible by full bytes. Black and white single bits per sample images are not currently supported.

You could convert the image to color tiffs and then use the library in its current form.

Example: Rename the following file to abc.tiff abc

TIFF Image: abc.tiff

-- File Directory --

ImageWidth (256) SHORT (2 bytes) Count: 1 Values: 2169

ImageLength (257) SHORT (2 bytes) Count: 1 Values: 931

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

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

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

FillOrder (266) SHORT (2 bytes) Count: 1 Values: 1

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

Orientation (274) SHORT (2 bytes) Count: 1 Values: 1

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

RowsPerStrip (278) SHORT (2 bytes) Count: 1 Values: 931

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

XResolution (282) RATIONAL (8 bytes) Count: 1 Values: [200, 1]

YResolution (283) RATIONAL (8 bytes) Count: 1 Values: [200, 1]

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

ResolutionUnit (296) SHORT (2 bytes) Count: 1 Values: 2

PageNumber (297) SHORT (2 bytes) Count: 2 Values: [0, 1]

Predictor (317) SHORT (2 bytes) Count: 1 Values: 2

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

-- Rasters --

Width: 2169 Height: 931 Number of Pixels: 2019339 Samples Per Pixel: 2 Bits Per Sample: [8, 8]

Pixel x = 0, y = 0: [0, 255] Pixel x = 1084, y = 465: [0, 0] Pixel x = 2168, y = 930: [0, 0]

KnIfER commented 5 years ago

@bosborn I have just found another pure java library , apache common imaging , although like imageIO, It can't be used on android directly. But after removing and replacing lots of code ( use bitmap instead of bufferedimage ), It's working now! It's capble of loading such tiff images (multiple pixels in one byte, T6 compression) .