We 've stumbled upon several issues with EmfMetafile rendering of metafiles copied from MS Office.
- How are the dimensions of default rendered image computed? EmfMetafile.createDefaultRendering() returns large rendered images under circumstances we have not been able to pinpoint.
- Does EmfMetafile.createScaledRendering(int w, int h, java.awt.RenderingHints hints) execution time and memory consumption depend on width/height, and if so, how?
We need to create a rendered image from EMF - fast (to be used for a paste function)
- not using a lot of memory
- Using the following code we want to render an EnhMetafile with certain dimensions :
RenderedImage rendImg = null;
try {
ByteArrayInputStream bis = new ByteArrayInputStream(contents);
EmfMetafile emf = new EmfMetafile(bis);
float ratio = emf.getWidth()/emf.getHeight();
final double MilliMetersPerInch = 25.4;
int renderWidth = (int) ((width*toolkit.getScreenResolution())/MilliMetersPerInch);
height = (int) (renderWidth/ratio);
RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
rendImg = emf.createScaledRendering(renderWidth, height, hints);
} catch (MetafilesException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ImageIO.write(rendImg, "png", bos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Unfortunately ImageIO fails with the following stack trace :
!STACK 0
java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at java.awt.image.SinglePixelPackedSampleModel.getPixels(Unknown Source)
at java.awt.image.Raster.getPixels(Unknown Source)
at com.sun.imageio.plugins.png.PNGImageWriter.encodePass(Unknown Source)
at com.sun.imageio.plugins.png.PNGImageWriter.write_IDAT(Unknown Source)
at com.sun.imageio.plugins.png.PNGImageWriter.write(Unknown Source)
at javax.imageio.ImageWriter.write(Unknown Source)
at javax.imageio.ImageIO.write(Unknown Source)
at javax.imageio.ImageIO.write(Unknown Source)
This does not happen if we use emf.createDefaultRendering().
Why would PNGImageWriter fail with this image, but not the default rendered? Is this a bug?
Sakis Kotisis
Senior Software Engineer
SciGen Technologies, Inc.
Office: +30 25410 25801