pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1777333 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering: PageDrawer.java SoftMask.java
Date Wed, 04 Jan 2017 16:30:09 GMT
Author: tilman
Date: Wed Jan  4 16:30:08 2017
New Revision: 1777333

URL: http://svn.apache.org/viewvc?rev=1777333&view=rev
Log:
PDFBOX-3000: use /bc color in softmask outside bounds, but ignore empty softmask

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/SoftMask.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=1777333&r1=1777332&r2=1777333&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 Wed Jan
 4 16:30:08 2017
@@ -430,7 +430,14 @@ public class PageDrawer extends PDFGraph
         }
         TransparencyGroup transparencyGroup = new TransparencyGroup(softMask.getGroup(),
true, softMask.getInitialTransformationMatrix());
         BufferedImage image = transparencyGroup.getImage();
+        if (image == null)
+        {
+            // Adobe Reader ignores empty softmasks instead of using bc color
+            // sample file: PDFJS-6967_reduced_outside_softmask.pdf
+            return parentPaint;
+        }
         BufferedImage gray = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
+        PDColor backdropColor = null;
         if (COSName.ALPHA.equals(softMask.getSubType()))
         {
             gray.setData(image.getAlphaRaster());
@@ -440,12 +447,19 @@ public class PageDrawer extends PDFGraph
             Graphics g = gray.getGraphics();
             g.drawImage(image, 0, 0, null);
             g.dispose();
+            
+            COSArray backdropColorArray = softMask.getBackdropColor();
+            PDColorSpace colorSpace = softMask.getGroup().getGroup().getColorSpace();
+            if (colorSpace != null && backdropColorArray != null)
+            {
+                backdropColor = new PDColor(backdropColorArray, colorSpace);
+            }
         }
         else
         {
             throw new IOException("Invalid soft mask subtype.");
         }
-        return new SoftMask(parentPaint, gray, transparencyGroup.getBounds());
+        return new SoftMask(parentPaint, gray, transparencyGroup.getBounds(), backdropColor);
     }
 
     // returns the stroking AWT Paint
@@ -1146,6 +1160,16 @@ public class PageDrawer extends PDFGraph
             Area clip = (Area)getGraphicsState().getCurrentClippingPath().clone();
             clip.intersect(new Area(transformedBox));
             Rectangle2D clipRect = clip.getBounds2D();
+            if (isSoftMask && clipRect.isEmpty())
+            {
+                image = null;
+                bbox = null;
+                minX = 0;
+                minY = 0;
+                width = 0;
+                height = 0;
+                return;
+            }
             this.bbox = new PDRectangle((float)clipRect.getX(), (float)clipRect.getY(),
                                         (float)clipRect.getWidth(), (float)clipRect.getHeight());
 

Modified: 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=1777333&r1=1777332&r2=1777333&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/SoftMask.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/SoftMask.java Wed Jan  4
16:30:08 2017
@@ -17,6 +17,7 @@
 
 package org.apache.pdfbox.rendering;
 
+import java.awt.Color;
 import java.awt.Paint;
 import java.awt.PaintContext;
 import java.awt.Rectangle;
@@ -27,6 +28,8 @@ import java.awt.image.BufferedImage;
 import java.awt.image.ColorModel;
 import java.awt.image.Raster;
 import java.awt.image.WritableRaster;
+import java.io.IOException;
+import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
 
 /**
  * A Paint which applies a soft mask to an underlying Paint.
@@ -41,6 +44,7 @@ class SoftMask implements Paint
     private final Paint paint;
     private final BufferedImage mask;
     private final Rectangle2D bboxDevice;
+    private int bc = 0;
 
     /**
      * Creates a new soft mask paint.
@@ -48,12 +52,27 @@ class SoftMask implements Paint
      * @param paint underlying paint.
      * @param mask soft mask
      * @param bboxDevice bbox of the soft mask in the underlying Graphics2D device space
+     * @param backdropColor the color to be used outside the transparency group’s bounding
box; if
+     * null, black will be used.
      */
-    SoftMask(Paint paint, BufferedImage mask, Rectangle2D bboxDevice)
+    SoftMask(Paint paint, BufferedImage mask, Rectangle2D bboxDevice, PDColor backdropColor)
     {
         this.paint = paint;
         this.mask = mask;
-        this.bboxDevice = bboxDevice;
+        this.bboxDevice = bboxDevice;        
+        if (backdropColor != null)
+        {
+            try
+            {
+                Color color = new Color(backdropColor.toRGB());
+                // http://stackoverflow.com/a/25463098/535646
+                bc = (299 * color.getRed() + 587 * color.getGreen() + 114 * color.getBlue())
/ 1000;
+            }
+            catch (IOException ex)
+            {
+                // keep default
+            }
+        }
     }
 
     @Override
@@ -122,6 +141,10 @@ class SoftMask implements Paint
 
                         pixelOutput[3] = Math.round(pixelOutput[3] * (gray[0] / 255f));
                     }
+                    else
+                    {
+                        pixelOutput[3] = Math.round(pixelOutput[3] * (bc / 255f));
+                    }
                     output.setPixel(x, y, pixelOutput);
                 }
             }



Mime
View raw message