xmlgraphics-fop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jerem...@apache.org
Subject svn commit: r603254 - in /xmlgraphics/fop/branches/Temp_ImagePackageRedesign: lib/ src/java/org/apache/fop/render/ps/
Date Tue, 11 Dec 2007 14:29:50 GMT
Author: jeremias
Date: Tue Dec 11 06:29:48 2007
New Revision: 603254

URL: http://svn.apache.org/viewvc?rev=603254&view=rev
Log:
Plugged new image package into form generation for PostScript.
XML images and EPS files are currently supported as forms. They are added inline.

Added:
    xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/PSSupportedFlavors.java
  (with props)
Modified:
    xmlgraphics/fop/branches/Temp_ImagePackageRedesign/lib/xmlgraphics-commons-1.3svn.jar
    xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/PSImageUtils.java
    xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/PSRenderer.java
    xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/ResourceHandler.java

Modified: xmlgraphics/fop/branches/Temp_ImagePackageRedesign/lib/xmlgraphics-commons-1.3svn.jar
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_ImagePackageRedesign/lib/xmlgraphics-commons-1.3svn.jar?rev=603254&r1=603253&r2=603254&view=diff
==============================================================================
Binary files - no diff available.

Modified: xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/PSImageUtils.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/PSImageUtils.java?rev=603254&r1=603253&r2=603254&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/PSImageUtils.java
(original)
+++ xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/PSImageUtils.java
Tue Dec 11 06:29:48 2007
@@ -19,20 +19,15 @@
  
 package org.apache.fop.render.ps;
 
-import java.awt.Dimension;
-import java.awt.geom.Rectangle2D;
-import java.io.IOException;
-
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.xmlgraphics.ps.PSGenerator;
+
 import org.apache.fop.image.EPSImage;
 import org.apache.fop.image.FopImage;
-import org.apache.fop.image.JpegImage;
-import org.apache.xmlgraphics.ps.PSGenerator;
-import org.apache.xmlgraphics.ps.PSResource;
 
 /**
- * Utility code for rendering images in PostScript. 
+ * Utility code for rendering images in PostScript.
  */
 public class PSImageUtils extends org.apache.xmlgraphics.ps.PSImageUtils {
 
@@ -40,93 +35,6 @@
     protected static Log log = LogFactory.getLog(PSImageUtils.class);
 
     /**
-     * Renders a bitmap image to PostScript.
-     * @param img image to render
-     * @param x x position
-     * @param y y position
-     * @param w width
-     * @param h height
-     * @param gen PS generator
-     * @throws IOException In case of an I/O problem while rendering the image
-     */
-    public static void renderBitmapImage(FopImage img, 
-                float x, float y, float w, float h, PSGenerator gen)
-                    throws IOException {
-        boolean isJPEG = (img instanceof JpegImage && (gen.getPSLevel() >= 3));
-        byte[] imgmap = convertImageToRawBitmapArray(img, isJPEG);
-        if (imgmap == null) {
-            gen.commentln("%Image data is not available: " + img);
-            return; //Image cannot be converted 
-        }
-        
-        String imgDescription = img.getMimeType() + " " + img.getOriginalURI();
-        Dimension imgDim = new Dimension(img.getWidth(), img.getHeight());
-        Rectangle2D targetRect = new Rectangle2D.Double(x, y, w, h);
-        writeImage(imgmap, imgDim, imgDescription, targetRect, isJPEG, 
-                img.getColorSpace(), gen);
-    }
-
-    /**
-     * Renders a bitmap image (as form) to PostScript.
-     * @param img image to render
-     * @param form the form resource
-     * @param x x position
-     * @param y y position
-     * @param w width
-     * @param h height
-     * @param gen PS generator
-     * @throws IOException In case of an I/O problem while rendering the image
-     */
-    public static void renderForm(FopImage img, PSResource form, 
-                float x, float y, float w, float h, PSGenerator gen)
-                    throws IOException {
-        Rectangle2D targetRect = new Rectangle2D.Double(x, y, w, h);
-        paintForm(form, targetRect, gen);
-    }
-    
-    /**
-     * Generates a form resource for a FopImage in PostScript.
-     * @param img image to render
-     * @param form the form resource
-     * @param gen PS generator
-     * @throws IOException In case of an I/O problem while rendering the image
-     */
-    public static void generateFormResourceForImage(FopImage img, PSResource form,
-                PSGenerator gen) throws IOException {
-        boolean isJPEG = (img instanceof JpegImage && (gen.getPSLevel() >= 3));
-        byte[] imgmap = convertImageToRawBitmapArray(img, isJPEG);
-        if (imgmap == null) {
-            gen.commentln("%Image data is not available: " + img);
-            return; //Image cannot be converted 
-        }
-        
-        String imgDescription = img.getMimeType() + " " + img.getOriginalURI();
-        Dimension imgDim = new Dimension(img.getWidth(), img.getHeight());
-        writeReusableImage(imgmap, imgDim, form.getName(), imgDescription, isJPEG, 
-                img.getColorSpace(), gen);
-    }
-
-    private static byte[] convertImageToRawBitmapArray(FopImage img, boolean allowUndecodedJPEG)
-                throws IOException {
-        if (img instanceof JpegImage && allowUndecodedJPEG) {
-            if (!img.load(FopImage.ORIGINAL_DATA)) {
-                return null;
-            }
-        } else {
-            if (!img.load(FopImage.BITMAP)) {
-                return null;
-            }
-        }
-        byte[] imgmap;
-        if (img.getBitmapsSize() > 0) {
-            imgmap = img.getBitmaps();
-        } else {
-            imgmap = img.getRessourceBytes();
-        }
-        return imgmap;
-    }
-
-    /**
      * Renders an EPS image to PostScript.
      * @param img EPS image to render
      * @param x x position
@@ -134,6 +42,8 @@
      * @param w width
      * @param h height
      * @param gen PS generator
+     * @deprecated Use {@link #renderEPS(java.io.InputStream, String, java.awt.geom.Rectangle2D,
+     *          java.awt.geom.Rectangle2D, PSGenerator)} instead
      */
     public static void renderEPS(EPSImage img, 
             float x, float y, float w, float h,

Modified: xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/PSRenderer.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/PSRenderer.java?rev=603254&r1=603253&r2=603254&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/PSRenderer.java
(original)
+++ xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/PSRenderer.java
Tue Dec 11 06:29:48 2007
@@ -42,6 +42,7 @@
 import org.apache.xmlgraphics.ps.DSCConstants;
 import org.apache.xmlgraphics.ps.ImageEncoder;
 import org.apache.xmlgraphics.ps.PSGenerator;
+import org.apache.xmlgraphics.ps.PSImageUtils;
 import org.apache.xmlgraphics.ps.PSProcSets;
 import org.apache.xmlgraphics.ps.PSResource;
 import org.apache.xmlgraphics.ps.PSState;
@@ -84,6 +85,7 @@
 import org.apache.fop.image2.impl.ImageRawStream;
 import org.apache.fop.image2.impl.ImageRendered;
 import org.apache.fop.image2.impl.ImageXMLDOM;
+import org.apache.fop.image2.pipeline.ImageProviderPipeline;
 import org.apache.fop.image2.util.ImageUtil;
 import org.apache.fop.render.AbstractPathOrientedRenderer;
 import org.apache.fop.render.Graphics2DAdapter;
@@ -115,7 +117,8 @@
  * @author <a href="mailto:fop-dev@xmlgraphics.apache.org">Apache FOP Development Team</a>
  * @version $Id$
  */
-public class PSRenderer extends AbstractPathOrientedRenderer implements ImageAdapter {
+public class PSRenderer extends AbstractPathOrientedRenderer
+            implements ImageAdapter, PSSupportedFlavors {
 
     /** logging instance */
     private static Log log = LogFactory.getLog(PSRenderer.class);
@@ -381,20 +384,50 @@
         }
     }
 
-    private final ImageFlavor[] level2Flavors = new ImageFlavor[]
-                                                  {ImageFlavor.RAW_EPS,
-                                                   ImageFlavor.GRAPHICS2D,
-                                                   ImageFlavor.BUFFERED_IMAGE, 
-                                                   ImageFlavor.RENDERED_IMAGE, 
-                                                   ImageFlavor.XML_DOM};
-
-    private final ImageFlavor[] level3Flavors = new ImageFlavor[]
-                                                  {ImageFlavor.RAW_EPS,
-                                                   ImageFlavor.RAW_JPEG,
-                                                   ImageFlavor.GRAPHICS2D,
-                                                   ImageFlavor.BUFFERED_IMAGE, 
-                                                   ImageFlavor.RENDERED_IMAGE, 
-                                                   ImageFlavor.XML_DOM};
+    /**
+     * Indicates whether an image should be inlined or added as a PostScript form.
+     * @param uri the URI of the image
+     * @return true if the image should be inlined rather than added as a form
+     */
+    protected boolean isImageInlined(String uri) {
+        return !isOptimizeResources() || uri == null || "".equals(uri);
+    }
+    
+    /**
+     * Indicates whether an image should be inlined or added as a PostScript form.
+     * @param info the ImageInfo object of the image
+     * @return true if the image should be inlined rather than added as a form
+     */
+    protected boolean isImageInlined(ImageInfo info) {
+        if (isImageInlined(info.getOriginalURI())) {
+            return true;
+        }
+        
+        if (!isOptimizeResources()) {
+            throw new IllegalStateException("Must not get here if form support is enabled");
+        }
+
+        //Investigate choice for inline mode
+        ImageFlavor[] inlineFlavors = getInlineFlavors();
+        ImageManager manager = getUserAgent().getFactory().getImageManager();
+        ImageProviderPipeline[] inlineCandidates
+            = manager.getPipelineFactory().determineCandidatePipelines(
+                    info.getMimeType(), inlineFlavors);
+        ImageProviderPipeline inlineChoice = manager.choosePipeline(inlineCandidates);
+        ImageFlavor inlineFlavor = (inlineChoice != null ? inlineChoice.getTargetFlavor()
: null);
+        
+        //Investigate choice for form mode
+        ImageFlavor[] formFlavors = getFormFlavors();
+        ImageProviderPipeline[] formCandidates
+            = manager.getPipelineFactory().determineCandidatePipelines(
+                    info.getMimeType(), formFlavors);
+        ImageProviderPipeline formChoice = manager.choosePipeline(formCandidates);
+        ImageFlavor formFlavor = (formChoice != null ? formChoice.getTargetFlavor() : null);
+        
+        //Inline if form is not supported or if a better choice is available with inline
mode
+        return formFlavor == null || !formFlavor.equals(inlineFlavor);
+    }
+    
     /** {@inheritDoc} */
     protected void drawImage(String uri, Rectangle2D pos, Map foreignAttributes) {
         endTextObject();
@@ -410,68 +443,84 @@
         try {
             ImageSessionContext sessionContext = getUserAgent().getImageSessionContext();
             info = manager.getImageInfo(uri, sessionContext);
-            Map hints = ImageUtil.getDefaultHints(sessionContext);
-            ImageFlavor[] flavors;
-            if (gen.getPSLevel() >= 3) {
-                flavors = level3Flavors;
-            } else {
-                flavors = level2Flavors;
-            }
             int width = (int)pos.getWidth();
             int height = (int)pos.getHeight();
-            org.apache.fop.image2.Image img = manager.getImage(
-                        info, flavors, hints, sessionContext);
-            if (img instanceof ImageGraphics2D) {
-                ImageGraphics2D imageG2D = (ImageGraphics2D)img;
-                RendererContext context = createRendererContext(
-                        x, y, width, height, foreignAttributes);
-                getGraphics2DAdapter().paintImage(imageG2D.getGraphics2DImagePainter(),
-                        context, x, y, width, height);
-            } else if (img instanceof ImageRendered) {
-                ImageRendered imgRend = (ImageRendered)img;
-                RendererContext context = createRendererContext(
-                        x, y, width, height, foreignAttributes);
-                paintImage(imgRend.getRenderedImage(), context, x, y, width, height);
-            } else if (img instanceof ImageXMLDOM) {
-                ImageXMLDOM imgXML = (ImageXMLDOM)img;
-                renderDocument(imgXML.getDocument(), imgXML.getRootNamespace(),
-                        pos, foreignAttributes);
-            } else if (img instanceof ImageRawStream) {
-                final ImageRawStream raw = (ImageRawStream)img;
-                if (MimeConstants.MIME_EPS.equals(raw.getInfo().getMimeType())) {
-                    ImageRawEPS eps = (ImageRawEPS)raw;
-                    Rectangle2D bbox = eps.getBoundingBox(); 
-                    InputStream in = raw.createInputStream();
-                    try {
-                        PSImageUtils.renderEPS(in, uri,
-                                x / 1000f, y / 1000f,
-                                width / 1000f, height / 1000f,
-                                (float)bbox.getX(), (float)bbox.getY(),
-                                (float)bbox.getWidth(), (float)bbox.getHeight(),
-                                gen);
-                    } finally {
-                        IOUtils.closeQuietly(in);
-                    }
-                } else if (MimeConstants.MIME_JPEG.equals(raw.getInfo().getMimeType())) {
-                    ImageRawJPEG jpeg = (ImageRawJPEG)raw;
-                    ImageEncoder encoder = new ImageEncoder() {
-                        public void writeTo(OutputStream out) throws IOException {
-                            raw.writeTo(out);
-                        }
-                        public String getImplicitFilter() {
-                            return "<< >> /DCTDecode";
+            
+            //millipoints --> points for PostScript
+            float ptx = x / 1000f;
+            float pty = y / 1000f;
+            float ptw = width / 1000f;
+            float pth = height / 1000f;
+
+            if (isImageInlined(info)) {
+                if (log.isDebugEnabled()) {
+                    log.debug("Image " + info + " is inlined");
+                }
+                //Only now fully load/prepare the image
+                Map hints = ImageUtil.getDefaultHints(sessionContext);
+                org.apache.fop.image2.Image img = manager.getImage(
+                        info, getInlineFlavors(), hints, sessionContext);
+                
+                //...and embed as inline image
+                if (img instanceof ImageGraphics2D) {
+                    ImageGraphics2D imageG2D = (ImageGraphics2D)img;
+                    RendererContext context = createRendererContext(
+                            x, y, width, height, foreignAttributes);
+                    getGraphics2DAdapter().paintImage(imageG2D.getGraphics2DImagePainter(),
+                            context, x, y, width, height);
+                } else if (img instanceof ImageRendered) {
+                    ImageRendered imgRend = (ImageRendered)img;
+                    RenderedImage ri = imgRend.getRenderedImage();
+                    PSImageUtils.renderBitmapImage(ri, ptx, pty, ptw, pth, gen);
+                } else if (img instanceof ImageXMLDOM) {
+                    ImageXMLDOM imgXML = (ImageXMLDOM)img;
+                    renderDocument(imgXML.getDocument(), imgXML.getRootNamespace(),
+                            pos, foreignAttributes);
+                } else if (img instanceof ImageRawStream) {
+                    final ImageRawStream raw = (ImageRawStream)img;
+                    if (MimeConstants.MIME_EPS.equals(raw.getInfo().getMimeType())) {
+                        ImageRawEPS eps = (ImageRawEPS)raw;
+                        Rectangle2D bbox = eps.getBoundingBox(); 
+                        InputStream in = raw.createInputStream();
+                        try {
+                            PSImageUtils.renderEPS(in, uri,
+                                    new Rectangle2D.Float(ptx, pty, ptw, pth),
+                                    bbox,
+                                    gen);
+                        } finally {
+                            IOUtils.closeQuietly(in);
                         }
-                    };
-                    Rectangle2D targetRect = new Rectangle2D.Float(
-                            x / 1000f, y / 1000f,
-                            width / 1000f, height / 1000f);
-                    PSImageUtils.writeImage(encoder, info.getSize().getDimensionPx(),
-                            uri, targetRect,
-                            jpeg.getColorSpace(), jpeg.isInverted(), gen);
+                    } else if (MimeConstants.MIME_JPEG.equals(raw.getInfo().getMimeType()))
{
+                        ImageRawJPEG jpeg = (ImageRawJPEG)raw;
+                        ImageEncoder encoder = new ImageEncoder() {
+                            public void writeTo(OutputStream out) throws IOException {
+                                raw.writeTo(out);
+                            }
+                            public String getImplicitFilter() {
+                                return "<< >> /DCTDecode";
+                            }
+                        };
+                        Rectangle2D targetRect = new Rectangle2D.Float(
+                                ptx, pty, ptw, pth);
+                        PSImageUtils.writeImage(encoder, info.getSize().getDimensionPx(),
+                                uri, targetRect,
+                                jpeg.getColorSpace(), jpeg.isInverted(), gen);
+                    } else {
+                        throw new UnsupportedOperationException("Unsupported raw image: "
+ info);
+                    }
                 } else {
-                    throw new UnsupportedOperationException("Unsupported raw image: " + info);
+                    throw new UnsupportedOperationException("Unsupported image type: " +
img);
+                }
+            } else {
+                if (log.isDebugEnabled()) {
+                    log.debug("Image " + info + " is embedded as a form later");
                 }
+                //Don't load image at this time, just put a form placeholder in the stream
+                PSResource form = getFormForImage(uri);   
+                Rectangle2D targetRect = new Rectangle2D.Double(ptx, pty, ptw, pth);
+                PSImageUtils.paintForm(form, info.getSize().getDimensionPt(), targetRect,
gen);   
             }
+
         } catch (ImageException ie) {
             log.error("Error while processing image: "
                     + (info != null ? info.toString() : uri), ie);
@@ -482,12 +531,35 @@
         }
     }
 
+    private ImageFlavor[] getInlineFlavors() {
+        ImageFlavor[] flavors;
+        if (gen.getPSLevel() >= 3) {
+            flavors = LEVEL_3_FLAVORS_INLINE;
+        } else {
+            flavors = LEVEL_2_FLAVORS_INLINE;
+        }
+        return flavors;
+    }
+
+    private ImageFlavor[] getFormFlavors() {
+        ImageFlavor[] flavors;
+        if (gen.getPSLevel() >= 3) {
+            flavors = LEVEL_3_FLAVORS_FORM;
+        } else {
+            flavors = LEVEL_2_FLAVORS_FORM;
+        }
+        return flavors;
+    }
+
     /**
      * Returns a PSResource instance representing a image as a PostScript form.
      * @param uri the image URI
      * @return a PSResource instance
      */
     protected PSResource getFormForImage(String uri) {
+        if (uri == null || "".equals(uri)) {
+            throw new IllegalArgumentException("uri must not be empty or null");
+        }
         if (this.formResources == null) {
             this.formResources = new java.util.HashMap();
         }
@@ -499,16 +571,6 @@
         return form;
     }
 
-    /**
-     * Indicates whether an image with the given URI should be inlined or added as a PostScript
-     * form.
-     * @param uri the image URI
-     * @return true if the image should be inlined rather than added as a form
-     */
-    protected boolean isImageInlined(String uri) {
-        return !isOptimizeResources();
-    }
-    
     /** {@inheritDoc} */
     public void paintImage(RenderedImage image, RendererContext context, 
             int x, int y, int width, int height) throws IOException {

Added: xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/PSSupportedFlavors.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/PSSupportedFlavors.java?rev=603254&view=auto
==============================================================================
--- xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/PSSupportedFlavors.java
(added)
+++ xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/PSSupportedFlavors.java
Tue Dec 11 06:29:48 2007
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+ 
+package org.apache.fop.render.ps;
+
+import org.apache.fop.image2.ImageFlavor;
+
+/**
+ * Defines the set of supported ImageFlavors for the PostScript renderer.
+ */
+public interface PSSupportedFlavors {
+
+    /** The flavors supported inline with PostScript level 2. */
+    ImageFlavor[] LEVEL_2_FLAVORS_INLINE = new ImageFlavor[]
+                                                 {ImageFlavor.RAW_EPS,
+                                                  ImageFlavor.GRAPHICS2D,
+                                                  ImageFlavor.BUFFERED_IMAGE, 
+                                                  ImageFlavor.RENDERED_IMAGE, 
+                                                  ImageFlavor.XML_DOM};
+
+    /** The flavors supported inline with PostScript level 3 and higher. */
+    ImageFlavor[] LEVEL_3_FLAVORS_INLINE = new ImageFlavor[]
+                                                 {ImageFlavor.RAW_EPS,
+                                                  ImageFlavor.RAW_JPEG,
+                                                  ImageFlavor.GRAPHICS2D,
+                                                  ImageFlavor.BUFFERED_IMAGE, 
+                                                  ImageFlavor.RENDERED_IMAGE, 
+                                                  ImageFlavor.XML_DOM};
+                   
+    /** The flavors supported as forms with PostScript level 2. */
+    ImageFlavor[] LEVEL_2_FLAVORS_FORM = new ImageFlavor[]
+                                                 {//ImageFlavor.RAW_EPS,
+                                                  ImageFlavor.GRAPHICS2D,
+                                                  ImageFlavor.BUFFERED_IMAGE, 
+                                                  ImageFlavor.RENDERED_IMAGE/*, 
+                                                  ImageFlavor.XML_DOM*/};
+
+    /** The flavors supported as forms with PostScript level 3 or higher. */
+    ImageFlavor[] LEVEL_3_FLAVORS_FORM = new ImageFlavor[]
+                                                 {//ImageFlavor.RAW_EPS,
+                                                  ImageFlavor.RAW_JPEG,
+                                                  ImageFlavor.GRAPHICS2D,
+                                                  ImageFlavor.BUFFERED_IMAGE, 
+                                                  ImageFlavor.RENDERED_IMAGE/*, 
+                                                  ImageFlavor.XML_DOM*/};
+    
+}

Propchange: xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/PSSupportedFlavors.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/PSSupportedFlavors.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/ResourceHandler.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/ResourceHandler.java?rev=603254&r1=603253&r2=603254&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/ResourceHandler.java
(original)
+++ xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/render/ps/ResourceHandler.java
Tue Dec 11 06:29:48 2007
@@ -19,6 +19,8 @@
 
 package org.apache.fop.render.ps;
 
+import java.awt.geom.Dimension2D;
+import java.awt.image.RenderedImage;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -26,12 +28,12 @@
 import java.util.Map;
 import java.util.Set;
 
-import org.apache.fop.apps.FOUserAgent;
-import org.apache.fop.fonts.FontInfo;
-import org.apache.fop.image.FopImage;
-import org.apache.fop.image.ImageFactory;
 import org.apache.xmlgraphics.ps.DSCConstants;
+import org.apache.xmlgraphics.ps.FormGenerator;
+import org.apache.xmlgraphics.ps.ImageEncoder;
+import org.apache.xmlgraphics.ps.ImageFormGenerator;
 import org.apache.xmlgraphics.ps.PSGenerator;
+import org.apache.xmlgraphics.ps.PSProcSets;
 import org.apache.xmlgraphics.ps.dsc.DSCException;
 import org.apache.xmlgraphics.ps.dsc.DSCFilter;
 import org.apache.xmlgraphics.ps.dsc.DSCParser;
@@ -49,13 +51,28 @@
 import org.apache.xmlgraphics.ps.dsc.events.PostScriptComment;
 import org.apache.xmlgraphics.ps.dsc.tools.DSCTools;
 
+import org.apache.fop.apps.FOUserAgent;
+import org.apache.fop.fonts.FontInfo;
+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.ImageRawEPS;
+import org.apache.fop.image2.impl.ImageRawJPEG;
+import org.apache.fop.image2.impl.ImageRawStream;
+import org.apache.fop.image2.impl.ImageRendered;
+import org.apache.fop.image2.impl.ImageXMLDOM;
+import org.apache.fop.image2.util.ImageUtil;
+
 /**
  * This class is used when two-pass production is used to generate the PostScript file (setting
  * "optimize-resources"). It uses the DSC parser from XML Graphics Commons to go over the
  * temporary file generated by the PSRenderer and adds all used fonts and images as resources
  * to the PostScript file.
  */
-public class ResourceHandler implements DSCParserConstants {
+public class ResourceHandler implements DSCParserConstants, PSSupportedFlavors {
 
     /**
      * Rewrites the temporary PostScript file generated by PSRenderer adding all needed resources
@@ -197,13 +214,126 @@
         Iterator iter = formResources.values().iterator();
         while (iter.hasNext()) {
             PSImageFormResource form = (PSImageFormResource)iter.next();
-            ImageFactory fact = userAgent.getFactory().getImageFactory();
-            FopImage image = fact.getImage(form.getImageURI(), userAgent);
-            if (image == null) {
-                throw new NullPointerException("Image not found: " + form.getImageURI());
+            final String uri = form.getImageURI();
+            
+            ImageManager manager = userAgent.getFactory().getImageManager();
+            ImageInfo info = null;
+            try {
+                ImageSessionContext sessionContext = userAgent.getImageSessionContext();
+                info = manager.getImageInfo(uri, sessionContext);
+                
+                ImageFlavor[] flavors;
+                if (gen.getPSLevel() >= 3) {
+                    flavors = LEVEL_3_FLAVORS_FORM;
+                } else {
+                    flavors = LEVEL_2_FLAVORS_FORM;
+                }
+                Map hints = ImageUtil.getDefaultHints(sessionContext);
+                org.apache.fop.image2.Image img = manager.getImage(
+                        info, flavors, hints, sessionContext);
+                
+                String imageDescription = info.getMimeType() + " " + info.getOriginalURI();
+                final Dimension2D dimensionsPt = info.getSize().getDimensionPt();
+                final Dimension2D dimensionsMpt = info.getSize().getDimensionMpt();
+
+                if (img instanceof ImageGraphics2D) {
+                    final ImageGraphics2D imageG2D = (ImageGraphics2D)img;
+                    FormGenerator formGen = new FormGenerator(
+                            form.getName(), imageDescription, dimensionsPt) {
+
+                        protected void generatePaintProc(PSGenerator gen)
+                                throws IOException {
+                            gen.getResourceTracker().notifyResourceUsageOnPage(
+                                    PSProcSets.EPS_PROCSET);
+                            gen.writeln("BeginEPSF");
+                            PSGraphics2DAdapter adapter = new PSGraphics2DAdapter(gen, false);
+                            adapter.paintImage(imageG2D.getGraphics2DImagePainter(),
+                                    null,
+                                    0, 0, 
+                                    (int)Math.round(dimensionsMpt.getWidth()),
+                                    (int)Math.round(dimensionsMpt.getHeight()));
+                            gen.writeln("EndEPSF");
+                        }
+                        
+                    };
+                    formGen.generate(gen);
+                } else if (img instanceof ImageRendered) {
+                    ImageRendered imgRend = (ImageRendered)img;
+                    RenderedImage ri = imgRend.getRenderedImage();
+                    FormGenerator formGen = new ImageFormGenerator(
+                            form.getName(), imageDescription,
+                            info.getSize().getDimensionPt(),
+                            ri, false);
+                    formGen.generate(gen);
+                } else if (img instanceof ImageXMLDOM) {
+                    throw new UnsupportedOperationException(
+                            "Embedding an ImageXMLDOM as a form isn't supported, yet");
+                } else if (img instanceof ImageRawStream) {
+                    final ImageRawStream raw = (ImageRawStream)img;
+                    if (raw instanceof ImageRawEPS) {
+                        final ImageRawEPS eps = (ImageRawEPS)raw;
+                        throw new UnsupportedOperationException(
+                                "Embedding EPS as forms isn't supported, yet");
+                        /*
+                        InputStream in = eps.createInputStream();
+                        try {
+                            FormGenerator formGen = new EPSFormGenerator(form.getName(),
+                                    imageDescription, dimensions, in);
+                            formGen.generate(gen);
+                        } finally {
+                            IOUtils.closeQuietly(in);
+                        }*/
+                    } else if (raw instanceof ImageRawJPEG) {
+                        ImageRawJPEG jpeg = (ImageRawJPEG)raw;
+                        ImageEncoder encoder = new ImageEncoder() {
+                            public void writeTo(OutputStream out) throws IOException {
+                                raw.writeTo(out);
+                            }
+                            public String getImplicitFilter() {
+                                return "<< >> /DCTDecode";
+                            }
+                        };
+                        FormGenerator formGen = new ImageFormGenerator(
+                                form.getName(), imageDescription,
+                                info.getSize().getDimensionPt(),
+                                info.getSize().getDimensionPx(),
+                                encoder,
+                                jpeg.getColorSpace(), jpeg.isInverted());
+                        formGen.generate(gen);
+                    } else {
+                        throw new UnsupportedOperationException("Unsupported raw image: "
+ info);
+                    }
+                } else {
+                    throw new UnsupportedOperationException("Unsupported image type: " +
img);
+                }
+            } catch (ImageException ie) {
+                throw new IOException("Error while generating form for image: " + ie.getMessage());
             }
-            PSImageUtils.generateFormResourceForImage(image, form, gen);
         }
     }
 
+    private static FormGenerator createMissingForm(String formName, final Dimension2D dimensions)
{
+        FormGenerator formGen = new FormGenerator(formName, null, dimensions) {
+
+            protected void generatePaintProc(PSGenerator gen) throws IOException {
+                gen.writeln("0 setgray");
+                gen.writeln("0 setlinewidth");
+                String w = gen.formatDouble(dimensions.getWidth());
+                String h = gen.formatDouble(dimensions.getHeight());
+                gen.writeln(w + " " + h  + " scale");
+                gen.writeln("0 0 1 1 rectstroke");
+                gen.writeln("newpath");
+                gen.writeln("0 0 moveto");
+                gen.writeln("1 1 lineto");
+                gen.writeln("stroke");
+                gen.writeln("newpath");
+                gen.writeln("0 1 moveto");
+                gen.writeln("1 0 lineto");
+                gen.writeln("stroke");
+            }
+            
+        };
+        return formGen;
+    }
+    
 }



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


Mime
View raw message