}
private int loadImage(final ImageContainer reference)
throws IOException, UnsupportedEncoderException
{
final Workbook workbook = printerBase.getWorkbook();
Image image = null;
// The image has an assigned URL ...
if (reference instanceof URLImageContainer)
{
final URLImageContainer urlImage = (URLImageContainer) reference;
final ResourceKey url = urlImage.getResourceKey();
// if we have an source to load the image data from ..
if (url != null && urlImage.isLoadable())
{
// and the image is one of the supported image formats ...
// we we can embedd it directly ...
final int format = getImageFormat(url);
if (format == -1)
{
// This is a unsupported image format.
if (reference instanceof LocalImageContainer)
{
final LocalImageContainer li = (LocalImageContainer) reference;
image = li.getImage();
}
if (image == null)
{
try
{
final Resource resource = resourceManager.create(url, null, Image.class);
image = (Image) resource.getResource();
}
catch (final ResourceException re)
{
logger.info("Failed to load image from URL " + url, re); // NON-NLS
}
}
}
else
{
try
{
final ResourceData data = resourceManager.load(url);
// create the image
return workbook.addPicture(data.getResource(resourceManager), format);
}
catch (final ResourceException re)
{
logger.info("Failed to load image from URL " + url, re); // NON-NLS
}
}
}
}
if (reference instanceof LocalImageContainer)
{
// Check, whether the imagereference contains an AWT image.
// if so, then we can use that image instance for the recoding
final LocalImageContainer li = (LocalImageContainer) reference;
if (image == null)
{
image = li.getImage();
}
}
if (image != null)
{
// now encode the image. We don't need to create digest data
// for the image contents, as the image is perfectly identifyable
// by its URL
final byte[] data = RenderUtility.encodeImage(image);
return workbook.addPicture(data, Workbook.PICTURE_TYPE_PNG);
}
return -1;
}