colorSpace = ColorSpace.getInstance(
ColorSpace.CS_LINEAR_RGB);
} else if (numComponents == 4) {
colorSpace = CMYKColorSpace.getInstance();
} else {
throw new ImageException("Unsupported ColorSpace for image "
+ info
+ ". The number of components supported are 1, 3 and 4.");
}
} finally {
in.reset();
}
in.skipBytes(reclen);
break;
case APP2: //ICC (see ICC1V42.pdf)
in.mark();
try {
reclen = in.readUnsignedShort();
// Check for ICC profile
byte[] iccString = new byte[11];
in.readFully(iccString);
in.skipBytes(1); //string terminator (null byte)
if ("ICC_PROFILE".equals(new String(iccString, "US-ASCII"))) {
log.debug("JPEG has an ICC profile");
in.skipBytes(2); //chunk sequence number and total number of chunks
if (iccStream == null) {
//ICC profiles can be split into several chunks
//so collect in a byte array output stream
iccStream = new ByteArrayOutputStream();
}
byte[] buf = new byte[reclen - 18];
in.readFully(buf);
iccStream.write(buf);
}
} finally {
in.reset();
}
in.skipBytes(reclen);
break;
case APPE: //Adobe-specific (see 5116.DCT_Filter.pdf)
in.mark();
try {
reclen = in.readUnsignedShort();
// Check for Adobe header
byte[] adobeHeader = new byte[5];
in.readFully(adobeHeader);
if ("Adobe".equals(new String(adobeHeader, "US-ASCII"))) {
// The reason for reading the APPE marker is that Adobe Photoshop
// generates CMYK JPEGs with inverted values. The correct thing
// to do would be to interpret the values in the marker, but for now
// only assume that if APPE marker is present and colorspace is CMYK,
// the image is inverted.
appeFound = true;
}
} finally {
in.reset();
}
in.skipBytes(reclen);
break;
default:
reclen = in.readUnsignedShort();
in.skipBytes(reclen - 2);
}
}
} finally {
in.reset();
}
ICC_Profile iccProfile = buildICCProfile(info, colorSpace, iccStream);
if (iccProfile == null && colorSpace == null) {
throw new ImageException("ColorSpace not be identified for JPEG image " + info);
}
boolean invertImage = false;
if (appeFound && colorSpace.getType() == ColorSpace.TYPE_CMYK) {
if (log.isDebugEnabled()) {