jai-imageio / jai-imageio-core

JAI ImageIO Core (without javax.media.jai dependencies)
Other
234 stars 87 forks source link

TIFFLZWDecompressor Null Pointer Exception #37

Open reeperbahnause opened 7 years ago

reeperbahnause commented 7 years ago

Hi,

I stumbled across an issue while using ZXing try to read the attached TIFF file 00000_001.zip.

The following Exception occurs as soon as the image should be read:

Exception in thread "main" java.lang.NullPointerException
        at com.github.jaiimageio.impl.plugins.tiff.TIFFLZWDecompressor.addStringToTable(TIFFLZWDecompressor.java:246)
        at com.github.jaiimageio.impl.plugins.tiff.TIFFLZWDecompressor.decode(TIFFLZWDecompressor.java:183)
        at com.github.jaiimageio.impl.plugins.tiff.TIFFLZWDecompressor.decodeRaw(TIFFLZWDecompressor.java:131)
        at com.github.jaiimageio.plugins.tiff.TIFFDecompressor.decode(TIFFDecompressor.java:2583)
        at com.github.jaiimageio.impl.plugins.tiff.TIFFImageReader.decodeTile(TIFFImageReader.java:1144)
        at com.github.jaiimageio.impl.plugins.tiff.TIFFImageReader.read(TIFFImageReader.java:1414)
        at javax.imageio.ImageIO.read(Unknown Source)
        at javax.imageio.ImageIO.read(Unknown Source)
        at com.google.zxing.client.j2se.ImageReader.readImage(ImageReader.java:44)
        at com.google.zxing.client.j2se.DecodeWorker.decode(DecodeWorker.java:120)
        at com.google.zxing.client.j2se.DecodeWorker.call(DecodeWorker.java:76)
        at com.google.zxing.client.j2se.CommandLineRunner.main(CommandLineRunner.java:98)

The command line I was testing is the following:

java -classpath .;jai-imageio-core-1.3.1.jar;jcommander-1.69.jar;javase-3.3.0.jar;core-3.3.1.jar com.google.zxing.client.j2se.CommandLineRunner file:////home/phillip/repository/Uploads/5916bdab80775/Optimized --possible_formats CODE_39 CODE_128 ITF --recursive --try_harder --multi

I just cloned the project to see if I can get any additional information about the exception but so far this is what I have.

reeperbahnause commented 7 years ago

Just created and tested the jai-imageio-core-1.3.2-SNAPSHOT.jar with the same result.

lbellonda commented 7 years ago

Hello the problem is in the support to LSB TIFF. The input stream is always read as it was MSB coded. LSB images are not so common among TIFF. A patch is needed to getNextCode method of TIFFLZWDecompressor class to support LSB files.

reeperbahnause commented 7 years ago

Hi,
I was debugging a bit an noticed in this line

addStringToTable(stringTable[oldCode], string[0]);

that the oldCode is > than 258 and because of that the returned value from the stringTable is null

I'm neither familiar with the decoding process nor the algorhythm and so probably the wrong person to contribute.
I would just like to help and let you know how I created this TIFF file.
The original file is a PDF document which I converted to a TIFF file using ImageMagicks convert tool.

convert -background white -alpha remove -scene 1 -density 300 -compress LZW -type grayscale -depth 8 /home/user/source.file /home/user/%03d.tif

After that I testet the Barcode extraction using Zxing which was working totally fine.
Then accidentally I was testing the barcode extraction using the TIFF file, which was converted a second time using ImageMagicks convert tool with the same parameters.
This twice converted TIFF file is the image I attached to this issue.

lbellonda commented 7 years ago

that the oldCode is > than 258 and because of This is the effect of the misinterpretation of the input data in getNextCode() with LSB images. "stringTable" entries should grow with the decompression process.

lbellonda commented 7 years ago

Hello, I am working on a fix on my fork: https://github.com/lbellonda/jai-imageio-core/tree/issue37 It still needs some optimization, but it is working with your image by checking the TIFF tag "FillOrder".

stain commented 6 years ago

Thanks for your contribution, @lbellonda, I've merged #39.