pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1763295 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox: pdmodel/graphics/blend/SoftMaskPaint.java rendering/PageDrawer.java rendering/SoftMask.java
Date Tue, 04 Oct 2016 16:16:53 GMT
Author: tilman
Date: Tue Oct  4 16:16:53 2016
New Revision: 1763295

URL: http://svn.apache.org/viewvc?rev=1763295&view=rev
Log:
PDFBOX-3000: new softmask handling with bounds, by John Hewson and Petr Slaby

Added:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/SoftMask.java   (with props)
Removed:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/blend/SoftMaskPaint.java
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java?rev=1763295&r1=1763294&r2=1763295&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java Tue Oct
 4 16:16:53 2016
@@ -33,7 +33,6 @@ import java.awt.geom.PathIterator;
 import java.awt.geom.Point2D;
 import java.awt.geom.Rectangle2D;
 import java.awt.image.BufferedImage;
-import java.awt.image.Raster;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
@@ -50,7 +49,6 @@ import org.apache.pdfbox.pdmodel.common.
 import org.apache.pdfbox.pdmodel.font.PDFont;
 import org.apache.pdfbox.pdmodel.font.PDVectorFont;
 import org.apache.pdfbox.pdmodel.graphics.PDLineDashPattern;
-import org.apache.pdfbox.pdmodel.graphics.blend.SoftMaskPaint;
 import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
 import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace;
 import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceGray;
@@ -412,47 +410,31 @@ public class PageDrawer extends PDFGraph
         linePath.closePath();
     }
 
-    /**
-     * Generates AWT raster for a soft mask
-     * 
-     * @param softMask soft mask
-     * @return AWT raster for soft mask
-     * @throws IOException
-     */
-    private Raster createSoftMaskRaster(PDSoftMask softMask) throws IOException
+    //TODO: move soft mask apply to getPaint()?
+    private Paint applySoftMaskToPaint(Paint parentPaint, PDSoftMask softMask) throws IOException
     {
+        if (softMask == null)
+        {
+            return parentPaint;
+        }
         TransparencyGroup transparencyGroup = new TransparencyGroup(softMask.getGroup(),
true);
-        COSName subtype = softMask.getSubType();
-        if (COSName.ALPHA.equals(subtype))
+        BufferedImage image = transparencyGroup.getImage();
+        BufferedImage gray = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
+        if (COSName.ALPHA.equals(softMask.getSubType()))
         {
-            return transparencyGroup.getAlphaRaster();
+            gray.setData(image.getAlphaRaster());
         }
-        else if (COSName.LUMINOSITY.equals(subtype))
+        else if (COSName.LUMINOSITY.equals(softMask.getSubType()))
         {
-            return transparencyGroup.getLuminosityRaster();
+            Graphics g = gray.getGraphics();
+            g.drawImage(image, 0, 0, null);
+            g.dispose();
         }
         else
         {
             throw new IOException("Invalid soft mask subtype.");
         }
-    }
-
-    private Paint applySoftMaskToPaint(Paint parentPaint, PDSoftMask softMask) throws IOException
-    {
-        if (softMask != null) 
-        {
-            //TODO PDFBOX-2934
-            if (COSName.ALPHA.equals(softMask.getSubType()))
-            {
-                LOG.info("alpha smask not implemented yet, is ignored");
-                return parentPaint;
-            }
-            return new SoftMaskPaint(parentPaint, createSoftMaskRaster(softMask));
-        }
-        else 
-        {
-            return parentPaint;
-        }
+        return new SoftMask(parentPaint, gray, transparencyGroup.getBounds());
     }
 
     // returns the stroking AWT Paint
@@ -466,6 +448,7 @@ public class PageDrawer extends PDFGraph
     // returns the non-stroking AWT Paint
     private Paint getNonStrokingPaint() throws IOException
     {
+        //TODO why no soft mask?
         return getPaint(getGraphicsState().getNonStrokingColor());
     }
 
@@ -722,6 +705,7 @@ public class PageDrawer extends PDFGraph
         if (pdImage.isStencil())
         {
             // fill the image with paint
+            //TODO why no soft mask?
             BufferedImage image = pdImage.getStencilImage(getNonStrokingPaint());
 
             // draw the image
@@ -1115,6 +1099,8 @@ public class PageDrawer extends PDFGraph
         private final BufferedImage image;
         private final PDRectangle bbox;
 
+        private final int minX;
+        private final int minY;
         private final int width;
         private final int height;
 
@@ -1146,8 +1132,8 @@ public class PageDrawer extends PDFGraph
             Shape deviceClip = dpiTransform.createTransformedShape(clip);
             Rectangle2D bounds = deviceClip.getBounds2D();
 
-            int minX = (int) Math.floor(bounds.getMinX());
-            int minY = (int) Math.floor(bounds.getMinY());
+            minX = (int) Math.floor(bounds.getMinX());
+            minY = (int) Math.floor(bounds.getMinY());
             int maxX = (int) Math.floor(bounds.getMaxX()) + 1;
             int maxY = (int) Math.floor(bounds.getMaxY()) + 1;
 
@@ -1210,19 +1196,15 @@ public class PageDrawer extends PDFGraph
             return bbox;
         }
 
-        public Raster getAlphaRaster()
+        public Rectangle2D getBounds()
         {
-            return image.getAlphaRaster();
-        }
-
-        public Raster getLuminosityRaster()
-        {
-            BufferedImage gray = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
-            Graphics g = gray.getGraphics();
-            g.drawImage(image, 0, 0, null);
-            g.dispose();
-
-            return gray.getRaster();
+            Point2D size = new Point2D.Double(pageSize.getWidth(), pageSize.getHeight());
+            // apply the underlying Graphics2D device's DPI transform and y-axis flip
+            Matrix m = new Matrix(xform);
+            AffineTransform dpiTransform = AffineTransform.getScaleInstance(Math.abs(m.getScalingFactorX()),
Math.abs(m.getScalingFactorY()));
+            size = dpiTransform.transform(size, size);
+            // Flip y
+            return new Rectangle2D.Double(minX, size.getY() - minY - height, width, height);
         }
     }
 }

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/SoftMask.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/SoftMask.java?rev=1763295&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/SoftMask.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/SoftMask.java Tue Oct  4
16:16:53 2016
@@ -0,0 +1,137 @@
+/*
+ * 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.
+ */
+
+package org.apache.pdfbox.rendering;
+
+import java.awt.Paint;
+import java.awt.PaintContext;
+import java.awt.Rectangle;
+import java.awt.RenderingHints;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Rectangle2D;
+import java.awt.image.BufferedImage;
+import java.awt.image.ColorModel;
+import java.awt.image.Raster;
+import java.awt.image.WritableRaster;
+
+/**
+ * A Paint which applies a soft mask to an underlying Paint.
+ * 
+ * @author John Hewson
+ */
+class SoftMask implements Paint
+{
+    private static final ColorModel ARGB_COLOR_MODEL =
+            new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).getColorModel();
+
+    private final Paint paint;
+    private final BufferedImage mask;
+    private final Rectangle2D bboxDevice;
+
+    /**
+     * Creates a new soft mask paint.
+     *
+     * @param paint underlying paint.
+     * @param mask soft mask
+     * @param bboxDevice bbox of the soft mask in the underlying Graphics2D device space
+     */
+    SoftMask(Paint paint, BufferedImage mask, Rectangle2D bboxDevice)
+    {
+        this.paint = paint;
+        this.mask = mask;
+        this.bboxDevice = bboxDevice;
+    }
+
+    @Override
+    public PaintContext createContext(ColorModel cm, Rectangle deviceBounds,
+                                      Rectangle2D userBounds, AffineTransform xform,
+                                      RenderingHints hints)
+    {
+        PaintContext ctx = paint.createContext(cm, deviceBounds, userBounds, xform, hints);
+        return new SoftPaintContext(cm, deviceBounds, userBounds, xform, hints, ctx);
+    }
+
+    @Override
+    public int getTransparency()
+    {
+        return TRANSLUCENT;
+    }
+
+    private class SoftPaintContext implements PaintContext
+    {
+        private final PaintContext context;
+
+        SoftPaintContext(ColorModel cm, Rectangle deviceBounds, Rectangle2D userBounds,
+                         AffineTransform xform, RenderingHints hints, PaintContext context)
+        {
+            this.context = context;
+        }
+
+        @Override
+        public ColorModel getColorModel()
+        {
+            return ARGB_COLOR_MODEL;
+        }
+
+        @Override
+        public Raster getRaster(int x1, int y1, int w, int h)
+        {
+            WritableRaster raster = (WritableRaster)context.getRaster(x1, y1, w, h);
+
+            // buffer
+            BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
+            WritableRaster output = image.getRaster();
+
+            // the soft mask has its own bbox
+            x1 = x1 - (int)bboxDevice.getX();
+            y1 = y1 - (int)bboxDevice.getY();
+
+            int[] gray = new int[4];
+            int[] rgba = new int[4];
+            for (int y = 0; y < h; y++)
+            {
+                for (int x = 0; x < w; x++)
+                {
+                    raster.getPixel(x, y, rgba);
+
+                    // get the alpha value from the gray mask, if within mask bounds
+                    gray[0] = 0;
+                    if (x1 + x < mask.getWidth() && y1 + y < mask.getHeight())
+                    {
+                        try
+                        {
+                            mask.getRaster().getPixel(x1 + x, y1 + y, gray);
+                        }
+                        catch (ArrayIndexOutOfBoundsException e)
+                        {
+                            //TODO bounds check not yet correct
+                        }
+                    }
+                    rgba[3] = Math.round(rgba[3] * (gray[0] / 255f)); // multiply alpha
+                    output.setPixel(x, y, rgba);
+                }
+            }
+
+            return output;
+        }
+
+        @Override
+        public void dispose()
+        {
+        }
+    }
+}

Propchange: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/SoftMask.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message