Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does loading this jpg using JavaIO give CMMException?

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)
like image 425
Paul Taylor Avatar asked Dec 17 '10 13:12

Paul Taylor


1 Answers

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.

like image 169
Favonius Avatar answered Sep 30 '22 20:09

Favonius