pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1792757 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/SampledImageReader.java
Date Wed, 26 Apr 2017 15:48:51 GMT
Author: tilman
Date: Wed Apr 26 15:48:51 2017
New Revision: 1792757

URL: http://svn.apache.org/viewvc?rev=1792757&view=rev
Log:
PDFBOX-3768: optimize 1bit path by using TYPE_BYTE_GRAY BufferedImage instead of passing to
RGB image creation

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

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/SampledImageReader.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/SampledImageReader.java?rev=1792757&r1=1792756&r2=1792757&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/SampledImageReader.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/SampledImageReader.java
Wed Apr 26 15:48:51 2017
@@ -34,6 +34,7 @@ import org.apache.commons.logging.LogFac
 import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSNumber;
 import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace;
+import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceGray;
 import org.apache.pdfbox.pdmodel.graphics.color.PDIndexed;
 
 /**
@@ -156,39 +157,50 @@ final class SampledImageReader
             throw new IOException("image width and height must be positive");
         }
 
+        if (bitsPerComponent == 1 && colorKey == null && numComponents ==
1)
+        {
+            return from1Bit(pdImage);
+        }
+
         //
         // An AWT raster must use 8/16/32 bits per component. Images with < 8bpc
         // will be unpacked into a byte-backed raster. Images with 16bpc will be reduced
         // in depth to 8bpc as they will be drawn to TYPE_INT_RGB images anyway. All code
-        // in PDColorSpace#toRGBImage expects and 8-bit range, i.e. 0-255.
+        // in PDColorSpace#toRGBImage expects an 8-bit range, i.e. 0-255.
         //
         WritableRaster raster = Raster.createBandedRaster(DataBuffer.TYPE_BYTE, width, height,
                 numComponents, new Point(0, 0));
-
-        // convert image, faster path for non-decoded, non-colormasked 8-bit images
         final float[] defaultDecode = pdImage.getColorSpace().getDefaultDecode(8);
         if (bitsPerComponent == 8 && Arrays.equals(decode, defaultDecode) &&
colorKey == null)
         {
+            // convert image, faster path for non-decoded, non-colormasked 8-bit images
             return from8bit(pdImage, raster);
         }
-        else if (bitsPerComponent == 1 && colorKey == null && numComponents
== 1)
-        {
-            return from1Bit(pdImage, raster);
-        }
-        else
-        {
-            return fromAny(pdImage, raster, colorKey);
-        }
+        return fromAny(pdImage, raster, colorKey);
     }
-    
-    private static BufferedImage from1Bit(PDImage pdImage, WritableRaster raster)
-            throws IOException
+
+    private static BufferedImage from1Bit(PDImage pdImage) throws IOException
     {
         final PDColorSpace colorSpace = pdImage.getColorSpace();
         final int width = pdImage.getWidth();
         final int height = pdImage.getHeight();
         final float[] decode = getDecodeArray(pdImage);
-        byte[] output = ((DataBufferByte) raster.getDataBuffer()).getData();
+        BufferedImage bim = null;
+        WritableRaster raster;
+        byte[] output;
+        if (colorSpace instanceof PDDeviceGray)
+        {
+            // TYPE_BYTE_GRAY and not TYPE_BYTE_BINARY because this one is handled
+            // without conversion to RGB by Graphics.drawImage
+            // this reduces the memory footprint, only one byte per pixel instead of three.
+            bim = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
+            raster = bim.getRaster();
+        }
+        else
+        {
+            raster = Raster.createBandedRaster(DataBuffer.TYPE_BYTE, width, height, 1, new
Point(0, 0));
+        }
+        output = ((DataBufferByte) raster.getDataBuffer()).getData();
 
         // read bit stream
         try (InputStream iis = pdImage.createInputStream())
@@ -243,6 +255,11 @@ final class SampledImageReader
                 }
             }
 
+            if (bim != null)
+            {
+                return bim;
+            }
+
             // use the color space to convert the image to RGB
             return colorSpace.toRGBImage(raster);
         }



Mime
View raw message