diff options
author | Jeremias Maerki <jeremias@apache.org> | 2007-02-07 11:44:49 +0000 |
---|---|---|
committer | Jeremias Maerki <jeremias@apache.org> | 2007-02-07 11:44:49 +0000 |
commit | 42c14d6c02efdbdf41ed828a6f44941965a83f3a (patch) | |
tree | d50f13769713ee3ca33584094cb6358fec2c8699 /src/java/org/apache/fop/render/rtf | |
parent | f183113d083c5e0306cc0e96f28445258419ceb5 (diff) | |
download | xmlgraphics-fop-42c14d6c02efdbdf41ed828a6f44941965a83f3a.tar.gz xmlgraphics-fop-42c14d6c02efdbdf41ed828a6f44941965a83f3a.zip |
Bugzilla #41488:
Fix for NPE with PNG images for RTF output.
Support for GIF images in RTF output (RTF handler, only. Does not affect the RTF library.).
Reverts revision 503326 and fixes the problem in a different way. But the fix is only a work-around and will need to be revisited when redesigning the image package. At any rate, the resolution problem introduced with the previous patch can been avoided with the new approach.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@504511 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/render/rtf')
-rw-r--r-- | src/java/org/apache/fop/render/rtf/RTFHandler.java | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/src/java/org/apache/fop/render/rtf/RTFHandler.java b/src/java/org/apache/fop/render/rtf/RTFHandler.java index d17e3285c..ac0363cb2 100644 --- a/src/java/org/apache/fop/render/rtf/RTFHandler.java +++ b/src/java/org/apache/fop/render/rtf/RTFHandler.java @@ -20,13 +20,24 @@ package org.apache.fop.render.rtf; // Java +import java.awt.color.ColorSpace; import java.awt.geom.Point2D; +import java.awt.image.BufferedImage; +import java.awt.image.ColorModel; +import java.awt.image.ComponentColorModel; +import java.awt.image.DataBuffer; +import java.awt.image.DataBufferByte; +import java.awt.image.PixelInterleavedSampleModel; +import java.awt.image.Raster; +import java.awt.image.SampleModel; +import java.awt.image.WritableRaster; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.Iterator; import org.apache.batik.dom.svg.SVGDOMImplementation; +import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.fop.apps.FOPException; @@ -97,6 +108,8 @@ import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfTextrun; import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfListItem.RtfListItemLabel; import org.apache.fop.render.rtf.rtflib.tools.BuilderContext; import org.apache.fop.render.rtf.rtflib.tools.TableContext; +import org.apache.xmlgraphics.image.writer.ImageWriter; +import org.apache.xmlgraphics.image.writer.ImageWriterRegistry; import org.w3c.dom.Document; import org.xml.sax.SAXException; @@ -1128,7 +1141,12 @@ public class RTFHandler extends FOEventHandler { log.error("Image could not be found: " + url); return; } - fopimage.load(FopImage.ORIGINAL_DATA); + if ("image/gif".equals(fopimage.getMimeType())) { + //GIF is not directly supported by RTF, so it must be converted to PNG + fopimage.load(FopImage.BITMAP); + } else { + fopimage.load(FopImage.ORIGINAL_DATA); + } putGraphic(eg, fopimage); } catch (Exception e) { @@ -1184,6 +1202,26 @@ public class RTFHandler extends FOEventHandler { } } + private BufferedImage createBufferedImageFromBitmaps(FopImage image) { + // TODO Hardcoded color and sample models, FIX ME! + ColorModel cm = new ComponentColorModel( + ColorSpace.getInstance(ColorSpace.CS_sRGB), + new int[] {8, 8, 8}, + false, false, + ColorModel.OPAQUE, DataBuffer.TYPE_BYTE); + SampleModel sampleModel = new PixelInterleavedSampleModel( + DataBuffer.TYPE_BYTE, image.getWidth(), image.getHeight(), 3, image.getWidth() * 3, + new int[] {0, 1, 2}); + DataBuffer dbuf = new DataBufferByte(image.getBitmaps(), + image.getWidth() * image.getHeight() * 3); + + WritableRaster raster = Raster.createWritableRaster(sampleModel, + dbuf, null); + + // Combine the color model and raster into a buffered image + return new BufferedImage(cm, raster, false, null); + } + /** * Puts a graphic/image into the generated RTF file. * @param abstractGraphic the graphic (external-graphic or instream-foreign-object) @@ -1195,8 +1233,16 @@ public class RTFHandler extends FOEventHandler { byte[] rawData; if ("image/svg+xml".equals(fopImage.getMimeType())) { rawData = SVGConverter.convertToJPEG((XMLImage) fopImage); - } else { + } else if (fopImage.getRessourceBytes() != null) { rawData = fopImage.getRessourceBytes(); + } else { + //TODO Revisit after the image library redesign!!! + //Convert the decoded bitmaps to a BufferedImage + BufferedImage bufImage = createBufferedImageFromBitmaps(fopImage); + ImageWriter writer = ImageWriterRegistry.getInstance().getWriterFor("image/png"); + ByteArrayOutputStream baout = new ByteArrayOutputStream(); + writer.writeImage(bufImage, baout); + rawData = baout.toByteArray(); } if (rawData == null) { log.warn(FONode.decorateWithContextInfo("Image could not be embedded: " |