]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Fixes scaling/grayscaling for fox:conversion-mode="bitmap" GOCA fallback.
authorAdrian Cumiskey <acumiskey@apache.org>
Tue, 14 Oct 2008 15:37:54 +0000 (15:37 +0000)
committerAdrian Cumiskey <acumiskey@apache.org>
Tue, 14 Oct 2008 15:37:54 +0000 (15:37 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_AFPGOCAResources@704566 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/fop/render/afp/AFPGraphics2D.java
src/java/org/apache/fop/render/afp/AFPImageRenderedFactory.java

index fba0cff71b1796da04abc4fbd333c3aad5a3d6e4..e389afb521630bd92f1dfc6da0aebc190f16cc93 100644 (file)
@@ -392,65 +392,67 @@ public class AFPGraphics2D extends AbstractGraphics2D {
 
     private static final int Y = 1;
 
-    /** {@inheritDoc} */
-    public boolean drawImage(Image img, int x, int y, int width, int height,
-            ImageObserver observer) {
+    private AFPImageObjectInfo getImageObjectInfo(
+            RenderedImage img, int x, int y, int width, int height) throws IOException {
+        ImageInfo imageInfo = new ImageInfo(null, "image/unknown");
+        ImageSize size = new ImageSize(img.getWidth(), img.getHeight(), 72);
+        imageInfo.setSize(size);
 
-        // draw with AWT Graphics2D
-        AFPUnitConverter unitConv = state.getUnitConverter();
-        int w = Math.round(unitConv.pt2units(width));
-        int h = Math.round(unitConv.pt2units(height));
-        Dimension size = new Dimension(w, h);
-        BufferedImage buf = buildBufferedImage(size);
-
-        java.awt.Graphics2D g = buf.createGraphics();
-        g.setComposite(AlphaComposite.SrcOver);
-        g.setBackground(new Color(1, 1, 1, 0));
-        g.setPaint(new Color(1, 1, 1, 0));
-        g.fillRect(0, 0, w, h);
-        g.clip(new Rectangle(0, 0, buf.getWidth(), buf.getHeight()));
-        g.setComposite(gc.getComposite());
-
-        if (!g.drawImage(img, 0, 0, buf.getWidth(), buf.getHeight(), observer)) {
-            return false;
-        }
+        ImageRendered imageRendered = new ImageRendered(imageInfo, img, null);
+        RenderedImage renderedImage = imageRendered.getRenderedImage();
 
-        ImageSize bufsize = new ImageSize(buf.getWidth(), buf.getHeight(), 72);
+        // create image object info
+        AFPImageObjectInfo imageObjectInfo = new AFPImageObjectInfo();
 
-        ImageInfo imageInfo = new ImageInfo(null, "image/unknown");
-        imageInfo.setSize(bufsize);
+        int dataHeight = renderedImage.getHeight();
+        imageObjectInfo.setDataHeight(dataHeight);
 
-        ImageRendered imgRend = new ImageRendered(imageInfo, buf, null);
-        RenderedImage ri = imgRend.getRenderedImage();
+        int dataWidth = renderedImage.getWidth();
+        imageObjectInfo.setDataWidth(dataWidth);
 
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        try {
-            // Serialize image
-            // TODO Eventually, this should be changed not to buffer as this
-            // increases the
-            // memory consumption (see PostScript output)
-            ImageEncodingHelper.encodeRenderedImageAsRGB(ri, baos);
-        } catch (IOException ioe) {
-            handleIOException(ioe);
-            return false;
+        boolean colorImages = true;//state.isColorImages();
+        imageObjectInfo.setColor(colorImages);
+
+        ByteArrayOutputStream boas = new ByteArrayOutputStream();
+        ImageEncodingHelper.encodeRenderedImageAsRGB(renderedImage, boas);
+        byte[] imageData = boas.toByteArray();
+
+        // convert to grayscale
+        if (!colorImages) {
+            boas.reset();
+            int bitsPerPixel = state.getBitsPerPixel();
+            imageObjectInfo.setBitsPerPixel(bitsPerPixel);
+            ImageEncodingHelper.encodeRGBAsGrayScale(
+                  imageData, dataWidth, dataHeight, bitsPerPixel, boas);
+            imageData = boas.toByteArray();
         }
+        imageObjectInfo.setData(imageData);
 
-        // create image object parameters
-        AFPImageObjectInfo imageObjectInfo = new AFPImageObjectInfo();
         if (imageInfo != null) {
             imageObjectInfo.setUri(imageInfo.getOriginalURI());
         }
 
+        imageObjectInfo.setMimeType(MimeConstants.MIME_AFP_IOCA_FS45);
+        imageObjectInfo.setBitsPerPixel(state.getBitsPerPixel());
+        imageObjectInfo.setResourceInfo(info.getResourceInfo());
+
+
+        // create object area info
         AFPObjectAreaInfo objectAreaInfo = new AFPObjectAreaInfo();
 
         AffineTransform at = gc.getTransform();
         float[] srcPts = new float[] {x, y};
-        float[] dstPts = new float[2];
+        float[] dstPts = new float[srcPts.length];
         at.transform(srcPts, 0, dstPts, 0, 1);
         objectAreaInfo.setX(Math.round(dstPts[X]));
         objectAreaInfo.setY(Math.round(dstPts[Y]));
 
+        AFPUnitConverter unitConv = state.getUnitConverter();
+
+        int w = Math.round(unitConv.pt2units(width));
         objectAreaInfo.setWidth(w);
+
+        int h = Math.round(unitConv.pt2units(height));
         objectAreaInfo.setHeight(h);
 
         int resolution = state.getResolution();
@@ -459,25 +461,50 @@ public class AFPGraphics2D extends AbstractGraphics2D {
 
         imageObjectInfo.setObjectAreaInfo(objectAreaInfo);
 
-        imageObjectInfo.setData(baos.toByteArray());
-        imageObjectInfo.setDataHeight(ri.getHeight());
-        imageObjectInfo.setDataWidth(ri.getWidth());
+        return imageObjectInfo;
+    }
 
-        boolean colorImages = state.isColorImages();
-        imageObjectInfo.setColor(colorImages);
+    /** {@inheritDoc} */
+    public boolean drawImage(Image img, int x, int y, int width, int height,
+            ImageObserver observer) {
 
-        imageObjectInfo.setMimeType(MimeConstants.MIME_AFP_IOCA_FS45);
-        imageObjectInfo.setBitsPerPixel(state.getBitsPerPixel());
-        imageObjectInfo.setResourceInfo(info.getResourceInfo());
+        // draw with AWT Graphics2D
+        Dimension size = new Dimension(width, height);
+        BufferedImage bufferedImage = buildBufferedImage(size);
+
+        java.awt.Graphics2D g2d = bufferedImage.createGraphics();
+        g2d.setComposite(AlphaComposite.SrcOver);
+
+        Color color = new Color(1, 1, 1, 0);
+        g2d.setBackground(color);
+        g2d.setPaint(color);
+
+        g2d.fillRect(0, 0, width, height);
+
+        int bufferedWidth = bufferedImage.getWidth();
+        int bufferedHeight = bufferedImage.getHeight();
+        Rectangle clipRect = new Rectangle(0, 0, bufferedWidth, bufferedHeight);
+        g2d.clip(clipRect);
+
+        g2d.setComposite(gc.getComposite());
+
+        if (!g2d.drawImage(img, 0, 0, bufferedWidth, bufferedHeight, observer)) {
+            return false;
+        }
+        g2d.dispose();
 
-        AFPResourceManager resourceManager = info.getResourceManager();
         try {
+            // get image object info
+            AFPImageObjectInfo imageObjectInfo = getImageObjectInfo(bufferedImage, x, y, width, height);
+
+            // create image resource
+            AFPResourceManager resourceManager = info.getResourceManager();
             resourceManager.createObject(imageObjectInfo);
+            return true;
         } catch (IOException ioe) {
-            log.error(ioe.getMessage());
-            return false;
+            handleIOException(ioe);
         }
-        return true;
+        return false;
     }
 
     /** {@inheritDoc} */
index df806a4d945fae1b22fbebadda553c0ccab3bdba..b3ffe31db823c68c414a93cfa1246b27f26c8676 100644 (file)
@@ -62,21 +62,21 @@ public class AFPImageRenderedFactory extends AFPDataObjectInfoFactory {
         int dataWidth = renderedImage.getWidth();
         imageObjectInfo.setDataWidth(dataWidth);
 
-        ByteArrayOutputStream baout = new ByteArrayOutputStream();
-        ImageEncodingHelper.encodeRenderedImageAsRGB(renderedImage, baout);
-        byte[] imageData = baout.toByteArray();
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ImageEncodingHelper.encodeRenderedImageAsRGB(renderedImage, baos);
+        byte[] imageData = baos.toByteArray();
 
         boolean colorImages = state.isColorImages();
         imageObjectInfo.setColor(colorImages);
 
         // convert to grayscale
         if (!colorImages) {
-            baout.reset();
+            baos.reset();
             int bitsPerPixel = state.getBitsPerPixel();
             imageObjectInfo.setBitsPerPixel(bitsPerPixel);
             ImageEncodingHelper.encodeRGBAsGrayScale(
-                  imageData, dataWidth, dataHeight, bitsPerPixel, baout);
-            imageData = baout.toByteArray();
+                  imageData, dataWidth, dataHeight, bitsPerPixel, baos);
+            imageData = baos.toByteArray();
         }
         imageObjectInfo.setData(imageData);