dragon66 / icafe

Java library for reading, writing, converting and manipulating images and metadata
Eclipse Public License 1.0
204 stars 58 forks source link

EOFException when reading a gray 1x1 png #22

Closed the21st closed 8 years ago

the21st commented 8 years ago

Hi! I am getting an exception when loading a PNG that both OSX Preview, Photoshop and Chrome can load: https://cloud.githubusercontent.com/assets/4610874/11146632/46cbeaf4-8a10-11e5-8ead-696cab4fac82.png

My code:

InputStream stream = new URL("https://cloud.githubusercontent.com/assets/4610874/11146632/46cbeaf4-8a10-11e5-8ead-696cab4fac82.png").openStream();
new PNGReader().read(stream);

Stack trace:

Cause: java.io.EOFException:
at com.icafe4j.io.IOUtils.readFully(Unknown Source)
at com.icafe4j.io.IOUtils.readFully(Unknown Source)
at com.icafe4j.io.IOUtils.readIntMM(Unknown Source)
at com.icafe4j.io.IOUtils.readUnsignedIntMM(Unknown Source)
at com.icafe4j.image.reader.PNGReader.read(Unknown Source)

This was on version 1.1-SNAPSHOT.

dragon66 commented 8 years ago

@the21st : The error message you see indicates a corrupted image which don't have enough bytes for the decoder to read. I tried loading both locally and through the URL you provided but didn't have any problem with it using the current build. I can't reproduce the exception.

dragon66 commented 8 years ago

This is the logging info I got:

2015-11-13 20:50:14,236 [main] INFO com.icafe4j.image.reader.PNGReader - --- PNG IMAGE INFO --- 2015-11-13 20:50:14,243 [main] INFO com.icafe4j.image.reader.PNGReader - image width: 1 2015-11-13 20:50:14,243 [main] INFO com.icafe4j.image.reader.PNGReader - image height: 1 2015-11-13 20:50:14,243 [main] INFO com.icafe4j.image.reader.PNGReader - image bit depth: 8 2015-11-13 20:50:14,243 [main] INFO com.icafe4j.image.reader.PNGReader - Image color type: Image color format: 0 - Gray-scale: each pixel is a grayscale sample. 2015-11-13 20:50:14,243 [main] INFO com.icafe4j.image.reader.PNGReader - image compression: 0 - Deflate/inflate compression with a 32K sliding window 2015-11-13 20:50:14,243 [main] INFO com.icafe4j.image.reader.PNGReader - image filter method: 0 - Adaptive filtering with five basic filter types 2015-11-13 20:50:14,243 [main] INFO com.icafe4j.image.reader.PNGReader - image interlace method: 0 - No interlace 2015-11-13 20:50:14,243 [main] INFO com.icafe4j.image.reader.PNGReader - --- END PNG IMAGE INFO ---

the21st commented 8 years ago

@dragon66 : Sorry, it seems github re-encoded the PNG file during upload. Here's a zip archive containing the png in question https://dl.dropboxusercontent.com/u/73774/loadtest-grayscale.zip

The logging I get:

13:30:30.497 INFO  com.icafe4j.image.reader.PNGReader - --- PNG IMAGE INFO ---
13:30:30.505 INFO  com.icafe4j.image.reader.PNGReader - image width: 1
13:30:30.508 INFO  com.icafe4j.image.reader.PNGReader - image height: 1
13:30:30.508 INFO  com.icafe4j.image.reader.PNGReader - image bit depth: 8
13:30:30.509 INFO  com.icafe4j.image.reader.PNGReader - Image color type: Image color format: 0 - Gray-scale: each pixel is a grayscale sample.
13:30:30.510 INFO  com.icafe4j.image.reader.PNGReader - image compression: 0 - Deflate/inflate compression with a 32K sliding window
13:30:30.510 INFO  com.icafe4j.image.reader.PNGReader - image filter method: 0 - Adaptive filtering with five basic filter types
13:30:30.510 INFO  com.icafe4j.image.reader.PNGReader - image interlace method: 0 - No interlace
13:30:30.510 INFO  com.icafe4j.image.reader.PNGReader - --- END PNG IMAGE INFO ---
java.io.EOFException
  at com.icafe4j.io.IOUtils.readFully(Unknown Source)
  at com.icafe4j.io.IOUtils.readFully(Unknown Source)
  at com.icafe4j.io.IOUtils.readIntMM(Unknown Source)
  at com.icafe4j.io.IOUtils.readUnsignedIntMM(Unknown Source)
  at com.icafe4j.image.reader.PNGReader.read(Unknown Source)
  ... 43 elided
dragon66 commented 8 years ago

@the21st : I still can't reproduce the error using the image loadtest-grayscale.png from the downloaded zip file and I am pretty sure this image is exactly the same as the first one you uploaded to github earlier.

the21st commented 8 years ago

I think I found the problem. I am only getting the exception when I use

new URL("file:/path/to/file/loadtest-grayscale.png").openStream

When I replaced it by FileInputStream or use an http URL (such as the github one), it works. Any idea why this might be a problem? I am using file: URLs with ImageIO and there are no issues.

dragon66 commented 8 years ago

@the21st : interesting finding. I can reproduce it now. I will fix this later. Thanks a lot for bring it up. It is fixed in the latest commit.

dragon66 commented 8 years ago

Problem found! When reading PNG image, we'll skip the chunks we're not interested in. This is fine as long as we skip the whole length of the chunk. The image in question contains XMP data which usually is large and when working with file URL, for some reason, it doesn't skip the whole length of the XMP data which are wrapped in a text chunk. Now we force it to fully skip the supposed length of bytes.

the21st commented 8 years ago

Thank you!