United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6488904 : Problem reading JPEG file - JFIF APP0 must be first marker after SOI

Details
Type:
Bug
Submit Date:
2006-11-01
Status:
Open
Updated Date:
2015-11-03
Project Name:
JDK
Resolved Date:
Component:
client-libs
OS:
windows_xp
Sub-Component:
javax.imageio
CPU:
x86
Priority:
P4
Resolution:
Unresolved
Affected Versions:
5.0
Targeted Versions:

Related Reports
Relates:
Relates:

Sub Tasks

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: JFIF APP0 must be first marker after SOI

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 :
javax.imageio.IIOException: JFIF APP0 must be first marker after SOI
	at com.sun.imageio.plugins.jpeg.JPEGMetadata.<init>(Unknown Source)
	at com.sun.imageio.plugins.jpeg.JPEGImageReader.getImageMetadata(Unknown Source)
	at JPEGTest.main(JPEGTest.java:46)


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
Please re-open if
 - if fix is in progress or on the plan to fix soon
 - if this is a P3 (file as P3, not P4) 
                                     
2014-03-18
EVALUATION

In contrast to the test image for 6488902, this image seems to be EXIF image with few peculiarities:

Marker d8 (SOI) (0 bytes)
Marker e0 (APP0) (6 bytes) - the APP0 marker with "Picasa" string inside
Marker e1 (APP1) (2898 bytes) - APP1 marker that points that this is  EXIF
Marker ed (APP13) (220 bytes)
Marker e0 (APP0) (14 bytes) - JFIF header that seems to be extra here.
Marker db (DQT) (65 bytes)
Marker db (DQT) (65 bytes)
Marker c4 (DHT) (416 bytes)
Marker c0 (SOF0) (15 bytes)
Marker da (SOS) (10 bytes)
skip 123420 bytes.
Marker d9 (EOI) (0 bytes)

So, this file contains:

1. The "custom" APP0 marker that can be ignored because it does not
     affect the image processing.

2. The APP1 marker that points that file in question is EXIF.

3. The second APP0 marker (which conforms the JFIF spec).

It is not clear for me how legal is appearance of both EXIF and JFIF headers simultaneously and the clarification of this is required.
However, I tend to ignore the JFIF header in this case.

However, if this contradiction will be resolved, then the problem with lack of EXIF support in the JPEG metadata sill need to be resolved.
                                     
2006-11-02



Hardware and Software, Engineered to Work Together