JDK-6488902 : Problem reading JPEG file - ICC APP2 encountered without prior JFIF!
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.imageio
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-11-01
  • Updated: 2011-01-19
  • Resolved: 2006-11-02
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_09"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_09-b01)
Java HotSpot(TM) Client VM (build 1.5.0_09-b01, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
Image works with standard ImageIO.read() and JAI-ImageIO CLibJPEGImageReader.  But when the SE JPEG reader: com.sun.imageio.plugins.jpeg.JPEGImageReader is used, it throws the following exception:
javax.imageio.IIOException: ICC APP2 encountered without prior JFIF!

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Load problematic JPEG file

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
JPEG loads and can be displayed or operated on.
ACTUAL -
JPEG fails to load and throws an exception.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Caused by: javax.imageio.IIOException: ICC APP2 encountered without prior JFIF!
 at com.sun.imageio.plugins.jpeg.JPEGMetadata.<init>(JPEGMetadata.java:263)
 at com.sun.imageio.plugins.jpeg.JPEGImageReader.getImageMetadata(JPEGImageReader.java:853)
 at com.sun.imageio.plugins.jpeg.JPEGImageReader.getNumThumbnails(JPEGImageReader.java:1250)
 at javax.imageio.ImageReader.hasThumbnails(ImageReader.java:1608)
 at com.sun.media.jai.imageioimpl.ImageReadOpImage.<init>(ImageReadOpImage.java:535)
 at com.sun.media.jai.imageioimpl.ImageReadCRIF.create(ImageReadCRIF.java:309)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.FileImageInputStream;


public class JPEGTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        FileImageInputStream fiis = null;
        try {
            fiis = new FileImageInputStream(new File(args[0]));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        if(fiis == null)
            return;

        Iterator<ImageReader> iter = ImageIO.getImageReaders(fiis);
        ImageReader reader = null;
        while (iter.hasNext()) {
            reader = iter.next();
            if(reader.getClass().getName().equals("com.sun.imageio.plugins.jpeg.JPEGImageReader"))
                break;
        }

        if(reader == null)
            return;
        
        if(!reader.getClass().getName().equals("com.sun.imageio.plugins.jpeg.JPEGImageReader"))
            System.out.println("com.sun.imageio.plugins.jpeg.JPEGImageReader not found, please disable reader plugin: " +
                    reader.getClass().toString());
        
        reader.setInput(fiis);
        try {
            reader.getImageMetadata(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Do not use the SE JPEGImageReader, use the JAI-ImageIO version instead (com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageReader)

Comments
EVALUATION The test image seems to conform the EXIF spec. The markers sequence is: Marker d8 (SOI) (0 bytes) Marker e1 (APP1) (8106 bytes) Marker e2 (APP2) (7275 bytes) Marker c0 (SOF0) (15 bytes) Marker db (DQT) (130 bytes) Marker c4 (DHT) (416 bytes) Marker da (SOS) (10 bytes) skip 700212 bytes. Marker d9 (EOI) (0 bytes) However, the JPEGMetadata is not ready to handle EXIF images because it expects the marker sequence that conforms the JFIF specification. The support of EXIF need to be add to JPEG metadata (to implementation and to metadata format specification both) to solve this particular problem. There is, actually, CR related to decribed problem that is already opened, so I am going to close this one as the duplicate.
2006-11-02