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 2017/05/12 10:01:25 UTC

svn commit: r1794950 - /pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/SampledImageReader.java

Author: tilman
Date: Fri May 12 10:01:25 2017
New Revision: 1794950

URL: http://svn.apache.org/viewvc?rev=1794950&view=rev
Log:
PDFBOX-3791: read image one row at a time, not one pixel at a time due to overhead in our buffer infrastructure

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=1794950&r1=1794949&r2=1794950&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 Fri May 12 10:01:25 2017
@@ -297,14 +297,21 @@ final class SampledImageReader
             final int width = pdImage.getWidth();
             final int height = pdImage.getHeight();
             final int numComponents = pdImage.getColorSpace().getNumberOfComponents();
-            int max = width * height;
-            byte[] tempBytes = new byte[numComponents];
-            for (int i = 0; i < max; i++)
+            byte[] tempBytes = new byte[numComponents * width];
+            // compromise between memory and time usage:
+            // reading the whole image consumes too much memory
+            // reading one pixel at a time makes it slow in our buffering infrastructure 
+            int i = 0;
+            for (int y = 0; y < height; ++y)
             {
                 input.read(tempBytes);
-                for (int c = 0; c < numComponents; c++)
+                for (int x = 0; x < width; ++x)
                 {
-                    banks[c][i] = tempBytes[0+c];
+                    for (int c = 0; c < numComponents; c++)
+                    {
+                        banks[c][i] = tempBytes[x * numComponents + c];
+                    }
+                    ++i;
                 }
             }
             // use the color space to convert the image to RGB
@@ -314,8 +321,8 @@ final class SampledImageReader
         {
             IOUtils.closeQuietly(input);
         }
-    }    
-    
+    }
+
     // slower, general-purpose image conversion from any image format
     private static BufferedImage fromAny(PDImage pdImage, WritableRaster raster, COSArray colorKey)
             throws IOException