8

ImageIO.read(imagePath) with this file gives a CMMException, why cant Java cope with this seemingly valid file http://www.jthink.net/jaikoz/scratch/front.jpg

java.awt.color.CMMException: Invalid image format
    at sun.awt.color.CMM.checkStatus(Unknown Source)
    at sun.awt.color.ICC_Transform.<init>(Unknown Source)
    at java.awt.image.ColorConvertOp.filter(Unknown Source)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.acceptPixels(Unknown Source)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImage(Native Method)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(Unknown Source)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(Unknown Source)
    at javax.imageio.ImageIO.read(Unknown Source)
    at javax.imageio.ImageIO.read(Unknown Source)
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351

3 Answers3

15

I think I got the hang of your problem. I checked the image you linked ( http://www.jthink.net/jaikoz/scratch/front.jpg ). Its due to Exif and JFIF standard.

when you are doing something like ImageIO.read('some file') it invokes the default sun jpeg implementation com.sun.imageio.plugins.jpeg.JPEGImageReader. Which used to have issues loading JFIF files BUG 6488904 (check the comment towards the end).

According to spec, both Exif and JFIF demands that their respective application marker segment must be the first right after SOI (APP1 and APP0) , so it is actually not possible per spec for an JPEG file to be compliant with both standards.

Though it was reported long time back

The workaround is to use JAI library (https://jai.dev.java.net/binary-builds.html#Release_builds). I am using Java (no native acceleration) version.

SeekableStream seekableStream =  new FileSeekableStream(new File("front.jpg"));
ParameterBlock pb = new ParameterBlock();
pb.add(seekableStream);

BufferedImage image = JAI.create("jpeg", pb).getAsBufferedImage();

hope this will help.

Favonius
  • 13,959
  • 3
  • 55
  • 95
  • @user294896: you are welcome. If the answer solved your problem then pls mark it as accepted. that way it will be out of the unanswered list. thanks. – Favonius Dec 20 '10 at 03:05
  • yes it works, I just wonder is there a way to do it using the public API rather than the underlying Sun classes (such as SeekableStream) – Paul Taylor Jan 05 '11 at 16:18
4

BTW, this problem is fixed in JDK8 (notice the commit at the bottom of http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7064516). I've confirmed that a pre-release build of JDK8 properly loads images that JDK7 fails on as described above.

cemerick
  • 5,916
  • 5
  • 30
  • 51
2

Old post, but for future reference:

Inspired by this question and links found here, I've written a JPEGImageReader plugin for ImageIO that supports JPEG images with these kind of "bad" ICC color profiles (the "issue" is the rendering intent in the ICC profile is incompatible with Java's ColorConvertOp). It's plain Java and does not require JAI. The source code is freely available at:

https://github.com/haraldk/TwelveMonkeys/tree/master/imageio/imageio-jpeg

Harald K
  • 26,314
  • 7
  • 65
  • 111
  • Can you provide a small explanation and example in your GitHub README on how to integrate and use your plugin? That would be great. – Jack Jun 30 '13 at 15:31
  • @Jack I'll do that. Also, feel free to file issues for missing docs at GitHub, if there's anything in particular you like documented. But all you need to do is build the project using Maven, and place JARs in class path. You'll use it just like you'd use the standard JPEG plugin (ie. `ImageIO.read(...)` or `ImageIO.getImageReaders(...)`). – Harald K Jun 30 '13 at 17:48