Flagstone Software

12769. JPEG SOI and EOI markers are swapped on null encoding tables.

In the FSDefineJPEG2 and FSDefineJPEG3 classes if the encoding table is set to NULL then an empty encoding table is added to the image. This is defined by writing the Start of Image (SOI, 0xFFD8) and End of Image (EOI, 0xFFD9) markers. However in the current release the markers are reversed.

Root Cause

The root cause was to deal with applications that generated Flash files where the encoding table markers were reversed, i.e. 0xFFD9, 0xFFD8 however the table was decoded incorrectly leaving 2 SOI markers which caused problems if the image data was use with other Java classes.

Workaround?

There is no workaround however changing he source code is simple. In FSDefineJPEG2.java change line 185:

bytes = new byte[] { (byte)0xFF, (byte)0xD9, (byte)0xFF, (byte)0xD8 };

to swap the order of the marker bytes:

bytes = new byte[] { (byte)0xFF, (byte)0xD8, (byte)0xFF, (byte)0xD9 };

In both FSDefineJPEG2.java and FSDefineJPEG3.java replace the readJPEGStream method with the following code:

private byte[] readJPEGStream(FSCoder coder)
{
    byte bytes[] = null;
    
    int start = coder.getPointer();
    int end = start + ((length-2) << 3);
    int word = coder.readBits(16, false);
    int eoi = word == 0xFFD8 ? 0xFFD9 : 0xFFD8;

    do {
        word = coder.scanBits(16, false);

        if (word == eoi)
        {
            end = coder.getPointer()+16;
            break;
        }
        coder.adjustPointer(8);
    }
    while (coder.getPointer() < end);

    int len = (end-start) >>> 3;

    coder.setPointer(start);
    bytes = new byte[len];
    coder.readBytes(bytes);

    return bytes;
}

When will it be fixed?

This was fixed in Transform 2.1.6 released on 31-Dec-2007