pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1791946 - /pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/SampledImageReader.java
Date Wed, 19 Apr 2017 16:45:01 GMT
Author: tilman
Date: Wed Apr 19 16:45:01 2017
New Revision: 1791946

URL: http://svn.apache.org/viewvc?rev=1791946&view=rev
Log:
PDFBOX-3763: apply stencil from the bit stream

Modified:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/SampledImageReader.java

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/SampledImageReader.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/SampledImageReader.java?rev=1791946&r1=1791945&r2=1791946&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/SampledImageReader.java
(original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/SampledImageReader.java
Wed Apr 19 16:45:01 2017
@@ -58,12 +58,11 @@ final class SampledImageReader
      */
     public static BufferedImage getStencilImage(PDImage pdImage, Paint paint) throws IOException
     {
-        // get mask (this image)
-        BufferedImage mask = getRGBImage(pdImage, null);
+        int width = pdImage.getWidth();
+        int height = pdImage.getHeight();
 
         // compose to ARGB
-        BufferedImage masked = new BufferedImage(mask.getWidth(), mask.getHeight(),
-                BufferedImage.TYPE_INT_ARGB);
+        BufferedImage masked = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
         Graphics2D g = masked.createGraphics();
 
         // draw the mask
@@ -72,26 +71,57 @@ final class SampledImageReader
         // fill with paint using src-in
         //g.setComposite(AlphaComposite.SrcIn);
         g.setPaint(paint);
-        g.fillRect(0, 0, mask.getWidth(), mask.getHeight());
+        g.fillRect(0, 0, width, height);
         g.dispose();
 
         // set the alpha
-        int width = masked.getWidth();
-        int height = masked.getHeight();
         WritableRaster raster = masked.getRaster();
-        WritableRaster alpha = mask.getRaster();
 
         final int[] transparent = new int[4];
-        int[] alphaPixel = null;
-        for (int y = 0; y < height; y++)
+
+        // avoid getting a BufferedImage for the mask to lessen memory footprint.
+        // Such masks are always bpc=1 and have no colorspace, but have a decode.
+        // (see 8.9.6.2 Stencil Masking)
+        ImageInputStream iis = null;
+        try
         {
-            for (int x = 0; x < width; x++)
+            iis = new MemoryCacheImageInputStream(pdImage.createInputStream());
+
+            final float[] decode = getDecodeArray(pdImage);
+            int value;
+            if (decode[0] < decode[1])
+            {
+                value = 1;
+            }
+            else
+            {
+                value = 0;
+            }
+
+            // calculate row padding
+            int padding = 0;
+            if (width % 8 > 0)
             {
-                alphaPixel = alpha.getPixel(x, y, alphaPixel);
-                if (alphaPixel[0] == 255)
+                padding = 8 - (width % 8);
+            }
+            
+            for (int y = 0; y < height; ++y)
+            {
+                for (int x = 0; x < width; ++x)
                 {
-                    raster.setPixel(x, y, transparent);
+                    if (iis.readBit() == value)
+                    {
+                        raster.setPixel(x, y, transparent);
+                    }
                 }
+                iis.readBits(padding);
+            }
+        }
+        finally
+        {
+            if (iis != null)
+            {
+                iis.close();
             }
         }
 



Mime
View raw message