Source src = session.needSource(info.getOriginalURI());
ImageInputStream imgStream = ImageUtil.needImageInputStream(src);
try {
Iterator iter = ImageIO.getImageReaders(imgStream);
while (iter.hasNext()) {
ImageReader reader = (ImageReader)iter.next();
try {
imgStream.mark();
ImageReadParam param = reader.getDefaultReadParam();
reader.setInput(imgStream, false, ignoreMetadata);
final int pageIndex = ImageUtil.needPageIndexFromURI(info.getOriginalURI());
try {
if (ImageFlavor.BUFFERED_IMAGE.equals(this.targetFlavor)) {
imageData = reader.read(pageIndex, param);
} else {
imageData = reader.read(pageIndex, param);
//imageData = reader.readAsRenderedImage(pageIndex, param);
//TODO Reenable the above when proper listeners are implemented
//to react to late pixel population (so the stream can be closed
//properly).
}
if (iiometa == null) {
iiometa = reader.getImageMetadata(pageIndex);
}
providerIgnoresICC = checkProviderIgnoresICC(reader
.getOriginatingProvider());
break; //Quit early, we have the image
} catch (IndexOutOfBoundsException indexe) {
throw new ImageException("Page does not exist. Invalid image index: "
+ pageIndex);
} catch (IllegalArgumentException iae) {
//Some codecs like com.sun.imageio.plugins.wbmp.WBMPImageReader throw
//IllegalArgumentExceptions when they have trouble parsing the image.
throw new ImageException("Error loading image using ImageIO codec", iae);
} catch (IIOException iioe) {
if (firstException == null) {
firstException = iioe;
} else {
log.debug("non-first error loading image: " + iioe.getMessage());
}
}
try {
//Try fallback for CMYK images
BufferedImage bi = getFallbackBufferedImage(reader, pageIndex, param);
imageData = bi;
firstException = null; //Clear exception after successful fallback attempt
break;
} catch (IIOException iioe) {
//ignore
}
imgStream.reset();
} finally {
reader.dispose();
}
}
} finally {
ImageUtil.closeQuietly(src);
//TODO Some codecs may do late reading.