You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2018/03/08 18:21:35 UTC

svn commit: r1826261 [2/2] - in /pdfbox/branches/2.0/pdfbox/src: main/java/org/apache/pdfbox/cos/ main/java/org/apache/pdfbox/filter/ main/java/org/apache/pdfbox/pdmodel/common/ main/java/org/apache/pdfbox/pdmodel/graphics/image/ main/java/org/apache/p...

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java?rev=1826261&r1=1826260&r2=1826261&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java Thu Mar  8 18:21:35 2018
@@ -105,6 +105,8 @@ public class PageDrawer extends PDFGraph
     // parent document renderer - note: this is needed for not-yet-implemented resource caching
     private final PDFRenderer renderer;
     
+    private final boolean subsamplingAllowed;
+    
     // the graphics device to draw to, xform is the initial transform of the device (i.e. DPI)
     private Graphics2D graphics;
     private AffineTransform xform;
@@ -155,6 +157,7 @@ public class PageDrawer extends PDFGraph
     {
         super(parameters.getPage());
         this.renderer = parameters.getRenderer();
+        this.subsamplingAllowed = parameters.isSubsamplingAllowed();
     }
 
     /**
@@ -1022,8 +1025,17 @@ public class PageDrawer extends PDFGraph
         }
         else
         {
-            // draw the image
-            drawBufferedImage(pdImage.getImage(), at);
+            if (subsamplingAllowed)
+            {
+                int subsampling = getSubsampling(pdImage, at);
+                // draw the subsampled image
+                drawBufferedImage(pdImage.getImage(null, subsampling), at);
+            }
+            else
+            {
+                // subsampling not allowed, draw the image
+                drawBufferedImage(pdImage.getImage(), at);
+            }
         }
 
         if (!pdImage.getInterpolate())
@@ -1034,6 +1046,38 @@ public class PageDrawer extends PDFGraph
         }
     }
 
+    /**
+     * Calculated the subsampling frequency for a given PDImage based on the current transformation
+     * and its calculated transform
+     *
+     * @param pdImage PDImage to be drawn
+     * @param at Transform that will be applied to the image when drawing
+     * @return The rounded-down ratio of image pixels to drawn pixels. Returned value will always be
+     * >=1.
+     */
+    private int getSubsampling(PDImage pdImage, AffineTransform at)
+    {
+        // calculate subsampling according to the resulting image size
+        double scale = Math.abs(at.getDeterminant() * xform.getDeterminant());
+
+        int subsampling = (int) Math.floor(Math.sqrt(pdImage.getWidth() * pdImage.getHeight() / scale));
+        if (subsampling > 8)
+        {
+            subsampling = 8;
+        }
+        if (subsampling < 1)
+        {
+            subsampling = 1;
+        }
+        if (subsampling > pdImage.getWidth() || subsampling > pdImage.getHeight())
+        {
+            // For very small images it is possible that the subsampling would imply 0 size.
+            // To avoid problems, the subsampling is set to no less than the smallest dimension.
+            subsampling = Math.min(pdImage.getWidth(), pdImage.getHeight());
+        }
+        return subsampling;
+    }
+
     private void drawBufferedImage(BufferedImage image, AffineTransform at) throws IOException
     {
         graphics.setComposite(getGraphicsState().getNonStrokingJavaComposite());

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawerParameters.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawerParameters.java?rev=1826261&r1=1826260&r2=1826261&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawerParameters.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawerParameters.java Thu Mar  8 18:21:35 2018
@@ -30,14 +30,16 @@ public final class PageDrawerParameters
 {
     private final PDFRenderer renderer;
     private final PDPage page;
+    private final boolean subsamplingAllowed;
 
     /**
      * Package-private constructor.
      */
-    PageDrawerParameters(PDFRenderer renderer, PDPage page)
+    PageDrawerParameters(PDFRenderer renderer, PDPage page, boolean subsamplingAllowed)
     {
         this.renderer = renderer;
         this.page = page;
+        this.subsamplingAllowed = subsamplingAllowed;
     }
 
     /**
@@ -55,4 +57,12 @@ public final class PageDrawerParameters
     {
         return renderer;
     }
+
+    /**
+     * Returns whether to allow subsampling of images.
+     */
+    public boolean isSubsamplingAllowed()
+    {
+        return subsamplingAllowed;
+    }
 }

Modified: pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/PDStreamTest.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/PDStreamTest.java?rev=1826261&r1=1826260&r2=1826261&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/PDStreamTest.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/PDStreamTest.java Thu Mar  8 18:21:35 2018
@@ -91,7 +91,7 @@ public class PDStreamTest
         PDStream pdStream = new PDStream(doc, is, new COSArray());
         Assert.assertEquals(0, pdStream.getFilters().size());
 
-        is = pdStream.createInputStream(null);
+        is = pdStream.createInputStream((List<String>) null);
         Assert.assertEquals(12, is.read());
         Assert.assertEquals(34, is.read());
         Assert.assertEquals(56, is.read());