You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-commits@xmlgraphics.apache.org by je...@apache.org on 2007/12/14 10:04:44 UTC

svn commit: r604135 - /xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/afp/AFPRenderer.java

Author: jeremias
Date: Fri Dec 14 01:04:43 2007
New Revision: 604135

URL: http://svn.apache.org/viewvc?rev=604135&view=rev
Log:
Preliminary changes to switch to the new image library. TIFF optimizations still missing. Untested.

Modified:
    xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/afp/AFPRenderer.java

Modified: xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/afp/AFPRenderer.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/afp/AFPRenderer.java?rev=604135&r1=604134&r2=604135&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/afp/AFPRenderer.java (original)
+++ xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/afp/AFPRenderer.java Fri Dec 14 01:04:43 2007
@@ -20,9 +20,11 @@
 package org.apache.fop.render.afp;
 
 import java.awt.Color;
+import java.awt.Point;
 import java.awt.Rectangle;
 import java.awt.geom.Rectangle2D;
-import java.awt.image.BufferedImage;
+import java.awt.image.RenderedImage;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
@@ -33,6 +35,8 @@
 import java.util.Map;
 
 import org.apache.commons.io.output.ByteArrayOutputStream;
+import org.apache.xmlgraphics.ps.ImageEncodingHelper;
+
 import org.apache.fop.apps.FOUserAgent;
 import org.apache.fop.apps.MimeConstants;
 import org.apache.fop.area.Block;
@@ -49,6 +53,7 @@
 import org.apache.fop.area.inline.SpaceArea;
 import org.apache.fop.area.inline.TextArea;
 import org.apache.fop.area.inline.WordArea;
+import org.apache.fop.datatypes.URISpecification;
 import org.apache.fop.fo.Constants;
 import org.apache.fop.fo.extensions.ExtensionAttachment;
 import org.apache.fop.fonts.FontInfo;
@@ -56,10 +61,15 @@
 import org.apache.fop.fonts.base14.Courier;
 import org.apache.fop.fonts.base14.Helvetica;
 import org.apache.fop.fonts.base14.TimesRoman;
-import org.apache.fop.image.FopImage;
-import org.apache.fop.image.ImageFactory;
-import org.apache.fop.image.TIFFImage;
-import org.apache.fop.image.XMLImage;
+import org.apache.fop.image2.ImageException;
+import org.apache.fop.image2.ImageFlavor;
+import org.apache.fop.image2.ImageInfo;
+import org.apache.fop.image2.ImageManager;
+import org.apache.fop.image2.ImageSessionContext;
+import org.apache.fop.image2.impl.ImageGraphics2D;
+import org.apache.fop.image2.impl.ImageRendered;
+import org.apache.fop.image2.impl.ImageXMLDOM;
+import org.apache.fop.image2.util.ImageUtil;
 import org.apache.fop.render.AbstractPathOrientedRenderer;
 import org.apache.fop.render.Graphics2DAdapter;
 import org.apache.fop.render.RendererContext;
@@ -74,7 +84,6 @@
 import org.apache.fop.render.afp.modca.AFPDataStream;
 import org.apache.fop.render.afp.modca.ImageObject;
 import org.apache.fop.render.afp.modca.PageObject;
-import org.w3c.dom.Document;
 
 /**
  * This is an implementation of a FOP Renderer that renders areas to AFP.
@@ -672,7 +681,7 @@
             }
             break;
         case Constants.EN_GROOVE:
-        case Constants.EN_RIDGE: {
+        case Constants.EN_RIDGE:
             float colFactor = (style == EN_GROOVE ? 0.4f : -0.4f);
             if (horz) {
                 Color uppercol = lightenColor(col, -colFactor);
@@ -700,7 +709,6 @@
                         pts2units(w3), rightcol);
             }
             break;
-        }
         case Constants.EN_HIDDEN:
             break;
         case Constants.EN_INSET:
@@ -735,20 +743,80 @@
         return context;
     }
 
-    /**
-     * {@inheritDoc}
-     */
-    public void drawImage(String url, Rectangle2D pos, Map foreignAttributes) {
+    private static final ImageFlavor[] FLAVORS = new ImageFlavor[]
+                                                     {ImageFlavor.GRAPHICS2D,
+                                                      ImageFlavor.BUFFERED_IMAGE, 
+                                                      ImageFlavor.RENDERED_IMAGE,
+                                                      ImageFlavor.XML_DOM};
+
+    /** {@inheritDoc} */
+    public void drawImage(String uri, Rectangle2D pos, Map foreignAttributes) {
+        uri = URISpecification.getURL(uri);
+        Rectangle posInt = new Rectangle(
+                (int)pos.getX(),
+                (int)pos.getY(),
+                (int)pos.getWidth(),
+                (int)pos.getHeight());
+        Point origin = new Point(currentIPPosition, currentBPPosition);
+        int x = origin.x + posInt.x;
+        int y = origin.y + posInt.y;
+
         String name = null;
         if (pageSegmentsMap != null) {
-            name = (String) pageSegmentsMap.get(url);
+            name = (String) pageSegmentsMap.get(uri);
         }
         if (name != null) {
-            int x = mpts2units(pos.getX() + currentIPPosition);
-            int y = mpts2units(pos.getY() + currentBPPosition);
-            afpDataStream.createIncludePageSegment(name, x, y);
+            afpDataStream.createIncludePageSegment(name, mpts2units(x), mpts2units(y));
         } else {
-            url = ImageFactory.getURL(url);
+            ImageManager manager = getUserAgent().getFactory().getImageManager();
+            ImageInfo info = null;
+            try {
+                ImageSessionContext sessionContext = getUserAgent().getImageSessionContext();
+                info = manager.getImageInfo(uri, sessionContext);
+                
+                //Only now fully load/prepare the image
+                Map hints = ImageUtil.getDefaultHints(sessionContext);
+                org.apache.fop.image2.Image img = manager.getImage(
+                        info, FLAVORS, hints, sessionContext);
+                
+                //...and process the image
+                if (img instanceof ImageGraphics2D) {
+                    ImageGraphics2D imageG2D = (ImageGraphics2D)img;
+                    RendererContext context = createRendererContext(
+                            posInt.x, posInt.y,
+                            posInt.width, posInt.height, foreignAttributes);
+                    getGraphics2DAdapter().paintImage(imageG2D.getGraphics2DImagePainter(),
+                            context,
+                            origin.x + posInt.x, origin.y + posInt.y,
+                            posInt.width, posInt.height);
+                } else if (img instanceof ImageRendered) {
+                    ImageRendered imgRend = (ImageRendered)img;
+                    RenderedImage ri = imgRend.getRenderedImage();
+                    
+                    drawBufferedImage(ri, getResolution(),
+                            posInt.x + currentIPPosition,
+                            posInt.y + currentBPPosition,
+                            posInt.width,
+                            posInt.height);
+                } else if (img instanceof ImageXMLDOM) {
+                    ImageXMLDOM imgXML = (ImageXMLDOM)img;
+                    renderDocument(imgXML.getDocument(), imgXML.getRootNamespace(),
+                            pos, foreignAttributes);
+                } else {
+                    throw new UnsupportedOperationException("Unsupported image type: " + img);
+                }
+
+            } 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) {
+                log.error("I/O error while processing image: "
+                        + (info != null ? info.toString() : uri), ioe);
+            }
+            
+            /*
             ImageFactory fact = userAgent.getFactory().getImageFactory();
             FopImage fopimage = fact.getImage(url, userAgent);
             if (fopimage == null) {
@@ -768,6 +836,7 @@
                 renderDocument(doc, ns, pos, foreignAttributes);
             } else if (MimeConstants.MIME_EPS.equals(mime)) {
                 log.warn("EPS images are not supported by this renderer");
+                */
                 /*
                  * } else if (MimeConstants.MIME_JPEG.equals(mime)) { if
                  * (!fopimage.load(FopImage.ORIGINAL_DATA)) { return; }
@@ -785,7 +854,7 @@
                  * io.setImageIDESize((byte)fopimage.getBitsPerPixel());
                  * io.setImageEncoding((byte)0x83);
                  * io.setImageData(fopimage.getRessourceBytes());
-                 */
+                 *//*
             } else if (MimeConstants.MIME_TIFF.equals(mime)
                     && fopimage instanceof TIFFImage) {
                 TIFFImage tiffImage = (TIFFImage) fopimage;
@@ -852,43 +921,30 @@
                     convertToGrayScaleImage(io, fopimage.getBitmaps(), fopimage
                             .getWidth(), fopimage.getHeight(), this.bitsPerPixel);
                 }
-            }
+            }*/
         }
     }
 
     /**
-     * Writes a BufferedImage to an OutputStream as raw sRGB bitmaps.
+     * Writes a RenderedImage to an OutputStream as raw sRGB bitmaps.
      * 
-     * @param img
-     *            the BufferedImage
+     * @param image
+     *            the RenderedImage
      * @param out
      *            the OutputStream
      * @throws IOException
      *             In case of an I/O error.
      */
-    public static void writeImage(BufferedImage img, OutputStream out)
+    public static void writeImage(RenderedImage image, OutputStream out)
             throws IOException {
-        int w = img.getWidth();
-        int h = img.getHeight();
-        int[] tmpMap = img.getRGB(0, 0, w, h, null, 0, w);
-        for (int i = 0; i < h; i++) {
-            for (int j = 0; j < w; j++) {
-                int p = tmpMap[i * w + j];
-                int r = (p >> 16) & 0xFF;
-                int g = (p >> 8) & 0xFF;
-                int b = (p) & 0xFF;
-                out.write((byte) (r & 0xFF));
-                out.write((byte) (g & 0xFF));
-                out.write((byte) (b & 0xFF));
-            }
-        }
+        ImageEncodingHelper.encodeRenderedImageAsRGB(image, out);
     }
 
     /**
      * Draws a BufferedImage to AFP.
      * 
-     * @param bi
-     *            the BufferedImage
+     * @param image
+     *            the RenderedImage
      * @param imageResolution
      *            the resolution of the BufferedImage
      * @param x
@@ -900,7 +956,7 @@
      * @param h
      *            the height of the viewport (in mpt)
      */
-    public void drawBufferedImage(BufferedImage bi, int imageResolution, int x,
+    public void drawBufferedImage(RenderedImage image, int imageResolution, int x,
             int y, int w, int h) {
         int afpx = mpts2units(x);
         int afpy = mpts2units(y);
@@ -910,21 +966,24 @@
         ByteArrayOutputStream baout = new ByteArrayOutputStream();
         try {
             // Serialize image
-            writeImage(bi, baout);
+            //TODO Eventually, this should be changed not to buffer as this increases the
+            //memory consumption (see PostScript output)
+            writeImage(image, baout);
             byte[] buf = baout.toByteArray();
 
             // Generate image
             ImageObject io = afpDataStream.getImageObject(afpx, afpy, afpw,
                     afph, afpres, afpres);
             io.setImageParameters(imageResolution, imageResolution,
-                    bi.getWidth(), bi.getHeight());
+                    image.getWidth(), image.getHeight());
             if (colorImages) {
                 io.setImageIDESize((byte)24);
                 io.setImageData(buf);
             } else {
                 // TODO Teach it how to handle grayscale BufferedImages directly
                 // because this is pretty inefficient
-                convertToGrayScaleImage(io, buf, bi.getWidth(), bi.getHeight(), this.bitsPerPixel);
+                convertToGrayScaleImage(io, buf,
+                        image.getWidth(), image.getHeight(), this.bitsPerPixel);
             }
         } catch (IOException ioe) {
             log.error("Error while serializing bitmap: " + ioe.getMessage(),



---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-help@xmlgraphics.apache.org