pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1601080 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/SampledImageReader.java
Date Sat, 07 Jun 2014 09:38:44 GMT
Author: tilman
Date: Sat Jun  7 09:38:44 2014
New Revision: 1601080

URL: http://svn.apache.org/r1601080
Log:
PDFBOX-2123: optimize reading of 1 bit images, as suggested by Petr Slaby

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=1601080&r1=1601079&r2=1601080&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
Sat Jun  7 09:38:44 2014
@@ -19,14 +19,9 @@ package org.apache.pdfbox.pdmodel.graphi
 import java.awt.Graphics2D;
 import java.awt.Paint;
 import java.awt.Point;
-import java.awt.Transparency;
-import java.awt.color.ColorSpace;
 import java.awt.image.BufferedImage;
-import java.awt.image.ColorModel;
-import java.awt.image.ComponentColorModel;
 import java.awt.image.DataBuffer;
 import java.awt.image.DataBufferByte;
-import java.awt.image.PackedColorModel;
 import java.awt.image.Raster;
 import java.awt.image.WritableRaster;
 import java.io.IOException;
@@ -153,11 +148,89 @@ final class SampledImageReader
         {
             return from8bit(pdImage, raster);
         }
+        else if (bitsPerComponent == 1 && colorKey == null)
+        {
+            return from1Bit(pdImage, raster);
+        }
         else
         {
             return fromAny(pdImage, raster, colorKey);
         }
     }
+    
+    private static BufferedImage from1Bit(PDImage pdImage, WritableRaster raster)
+            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();
+
+        // read bit stream
+        InputStream iis = null;
+        try
+        {
+            // create stream
+            iis = pdImage.getStream().createInputStream();
+            final boolean isIndexed = colorSpace instanceof PDIndexed;
+
+            int rowLen = width / 8;
+            if (width % 8 > 0)
+            {
+                rowLen++;
+            }
+
+            // read stream
+            byte value0;
+            byte value1;
+            if (isIndexed || decode[0] < decode[1])
+            {
+                value0 = 0;
+                value1 = (byte) 255;
+            }
+            else
+            {
+                value0 = (byte) 255;
+                value1 = 0;
+            }
+            byte[] buff = new byte[rowLen];
+            int idx = 0;
+            for (int y = 0; y < height; y++)
+            {
+                int x = 0;
+                iis.read(buff);
+                for (int r = 0; r < rowLen; r++)
+                {
+                    int value = buff[r];
+                    int mask = 128;
+                    for (int i = 0; i < 8; i++)
+                    {
+                        int bit = value & mask;
+                        mask >>= 1;
+                        output[idx++] = bit == 0 ? value0 : value1;
+                        x++;
+                        if (x == width)
+                        {
+                            break;
+                        }
+                    }
+                }
+            }
+
+            // use the color space to convert the image to RGB
+            BufferedImage rgbImage = colorSpace.toRGBImage(raster);
+
+            return rgbImage;
+        }
+        finally
+        {
+            if (iis != null)
+            {
+                iis.close();
+            }
+        }
+    }
 
     // faster, 8-bit non-decoded, non-colormasked image conversion
     private static BufferedImage from8bit(PDImage pdImage, WritableRaster raster)
@@ -192,8 +265,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



Mime
View raw message