From 42c14d6c02efdbdf41ed828a6f44941965a83f3a Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Wed, 7 Feb 2007 11:44:49 +0000 Subject: [PATCH] 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 --- .../org/apache/fop/image/ImageIOImage.java | 11 ++-- .../apache/fop/image/AbstractFopImage.java | 3 ++ .../org/apache/fop/render/rtf/RTFHandler.java | 50 ++++++++++++++++++- status.xml | 9 ++-- 4 files changed, 63 insertions(+), 10 deletions(-) diff --git a/src/java-1.4/org/apache/fop/image/ImageIOImage.java b/src/java-1.4/org/apache/fop/image/ImageIOImage.java index 1af4f0b9a..7ec6b9004 100644 --- a/src/java-1.4/org/apache/fop/image/ImageIOImage.java +++ b/src/java-1.4/org/apache/fop/image/ImageIOImage.java @@ -64,10 +64,7 @@ public class ImageIOImage extends AbstractFopImage { * @see org.apache.fop.image.AbstractFopImage#loadDimensions() */ protected boolean loadDimensions() { - if ((this.width > 0) && (this.height > 0)) { - return true; - } - else if (this.bitmaps == null) { + if (this.bitmaps == null) { return loadBitmap(); } return true; @@ -211,7 +208,11 @@ public class ImageIOImage extends AbstractFopImage { /** @see org.apache.fop.image.AbstractFopImage#loadOriginalData() */ protected boolean loadOriginalData() { - return loadDefaultOriginalData(); + if (inputStream == null && getBitmaps() != null) { + return false; + } else { + return loadDefaultOriginalData(); + } } /** @see org.apache.fop.image.FopImage#hasSoftMask() */ diff --git a/src/java/org/apache/fop/image/AbstractFopImage.java b/src/java/org/apache/fop/image/AbstractFopImage.java index 8c150df41..4d49034a7 100644 --- a/src/java/org/apache/fop/image/AbstractFopImage.java +++ b/src/java/org/apache/fop/image/AbstractFopImage.java @@ -219,6 +219,9 @@ public abstract class AbstractFopImage implements FopImage { * @return true if the loading was successful */ protected boolean loadDefaultOriginalData() { + if (inputStream == null) { + throw new IllegalStateException("inputStream is already null or was never set"); + } try { this.raw = IOUtils.toByteArray(inputStream); } catch (java.io.IOException ex) { 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: " diff --git a/status.xml b/status.xml index b1ef07f1e..de8c1f99e 100644 --- a/status.xml +++ b/status.xml @@ -28,12 +28,15 @@ - - Fix for properly parsing font-family names containing spaces. + + Support for GIF images in RTF output (RTF handler, only. Does not affect the RTF library.) - + Fix for NPE with PNG images for RTF output. + + Fix for properly parsing font-family names containing spaces. + Support for soft masks (transparency) with ImageIO image adapter. -- 2.39.5