if (log.isDebugEnabled()) {
log.debug("Handling image: " + uri);
}
ImageManager manager = getUserAgent().getFactory().getImageManager();
ImageInfo info = null;
try {
ImageSessionContext sessionContext = getUserAgent().getImageSessionContext();
info = manager.getImageInfo(uri, sessionContext);
int width = (int)pos.getWidth();
int height = (int)pos.getHeight();
//millipoints --> points for PostScript
float ptx = x / 1000f;
float pty = y / 1000f;
float ptw = width / 1000f;
float pth = height / 1000f;
if (isImageInlined(info)) {
if (log.isDebugEnabled()) {
log.debug("Image " + info + " is inlined");
}
//Only now fully load/prepare the image
Map hints = ImageUtil.getDefaultHints(sessionContext);
org.apache.xmlgraphics.image.loader.Image img = manager.getImage(
info, getInlineFlavors(), hints, sessionContext);
//...and embed as inline image
if (img instanceof ImageGraphics2D) {
ImageGraphics2D imageG2D = (ImageGraphics2D)img;
RendererContext context = createRendererContext(
x, y, width, height, foreignAttributes);
getGraphics2DAdapter().paintImage(imageG2D.getGraphics2DImagePainter(),
context, x, y, width, height);
} else if (img instanceof ImageRendered) {
ImageRendered imgRend = (ImageRendered)img;
RenderedImage ri = imgRend.getRenderedImage();
PSImageUtils.renderBitmapImage(ri, ptx, pty, ptw, pth, gen);
} else if (img instanceof ImageXMLDOM) {
ImageXMLDOM imgXML = (ImageXMLDOM)img;
renderDocument(imgXML.getDocument(), imgXML.getRootNamespace(),
pos, foreignAttributes);
} else if (img instanceof ImageRawStream) {
final ImageRawStream raw = (ImageRawStream)img;
if (raw instanceof ImageRawEPS) {
ImageRawEPS eps = (ImageRawEPS)raw;
Rectangle2D bbox = eps.getBoundingBox();
InputStream in = raw.createInputStream();
try {
PSImageUtils.renderEPS(in, uri,
new Rectangle2D.Float(ptx, pty, ptw, pth),
bbox,
gen);
} finally {
IOUtils.closeQuietly(in);
}
} else if (raw instanceof ImageRawCCITTFax) {
final ImageRawCCITTFax ccitt = (ImageRawCCITTFax)raw;
ImageEncoder encoder = new ImageEncoderCCITTFax(ccitt);
Rectangle2D targetRect = new Rectangle2D.Float(
ptx, pty, ptw, pth);
PSImageUtils.writeImage(encoder, info.getSize().getDimensionPx(),
uri, targetRect,
ccitt.getColorSpace(), 1, false, gen);
} else if (raw instanceof ImageRawJPEG) {
ImageRawJPEG jpeg = (ImageRawJPEG)raw;
ImageEncoder encoder = new ImageEncoderJPEG(jpeg);
Rectangle2D targetRect = new Rectangle2D.Float(
ptx, pty, ptw, pth);
PSImageUtils.writeImage(encoder, info.getSize().getDimensionPx(),
uri, targetRect,
jpeg.getColorSpace(), 8, jpeg.isInverted(), gen);
} else {
throw new UnsupportedOperationException("Unsupported raw image: " + info);
}
} else {
throw new UnsupportedOperationException("Unsupported image type: " + img);
}
} else {
if (log.isDebugEnabled()) {
log.debug("Image " + info + " is embedded as a form later");
}
//Don't load image at this time, just put a form placeholder in the stream
PSResource form = getFormForImage(uri);
Rectangle2D targetRect = new Rectangle2D.Double(ptx, pty, ptw, pth);
PSImageUtils.paintForm(form, info.getSize().getDimensionPt(), targetRect, gen);
}
} catch (ImageException ie) {
log.error("Error while processing image: "
+ (info != null ? info.toString() : uri), ie);
} catch (FileNotFoundException fe) {
log.error(fe.getMessage());
} catch (IOException ioe) {
handleIOTrouble(ioe);
}