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/03/06 17:40:13 UTC

svn commit: r1785703 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/CCITTFactory.java

Author: tilman
Date: Mon Mar  6 17:40:13 2017
New Revision: 1785703

URL: http://svn.apache.org/viewvc?rev=1785703&view=rev
Log:
PDFBOX-3696: add methods for byte array; optimize padding

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

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/CCITTFactory.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/CCITTFactory.java?rev=1785703&r1=1785702&r2=1785703&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/CCITTFactory.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/CCITTFactory.java Mon Mar  6 17:40:13 2017
@@ -28,6 +28,7 @@ import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.filter.Filter;
 import org.apache.pdfbox.filter.FilterFactory;
 import org.apache.pdfbox.io.RandomAccess;
+import org.apache.pdfbox.io.RandomAccessBuffer;
 import org.apache.pdfbox.io.RandomAccessFile;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace;
@@ -77,9 +78,9 @@ public final class CCITTFactory
                 // flip bit to avoid having to set /BlackIs1
                 mcios.writeBits(~(image.getRGB(x, y) & 1), 1);
             }
-            while (mcios.getBitOffset() != 0)
+            if (mcios.getBitOffset() != 0)
             {
-                mcios.writeBit(0);
+                mcios.writeBits(0, 8 - mcios.getBitOffset());
             }
         }
         mcios.flush();
@@ -88,6 +89,55 @@ public final class CCITTFactory
         return prepareImageXObject(document, bos.toByteArray(), width, height, PDDeviceGray.INSTANCE);
     }
 
+    /**
+     * Creates a new CCITT Fax compressed image XObject from a specific image of a TIFF file stored
+     * in a byte array. Only single-strip CCITT T4 or T6 compressed TIFF files are supported. If
+     * you're not sure what TIFF files you have, use
+     * {@link LosslessFactory#createFromImage(PDDocument, BufferedImage) }
+     * or {@link CCITTFactory#createFromImage(PDDocument, BufferedImage) }
+     * instead.
+     *
+     * @param document the document to create the image as part of.
+     * @param byteArray the TIFF file in a byte array which contains a suitable CCITT compressed
+     * image
+     * @return a new Image XObject
+     * @throws IOException if there is an error reading the TIFF data.
+     */
+    public static PDImageXObject createFromByteArray(PDDocument document, byte[] byteArray)
+            throws IOException
+    {
+        return createFromByteArray(document, byteArray, 0);
+    }
+
+    /**
+     * Creates a new CCITT Fax compressed image XObject from a specific image of a TIFF file stored
+     * in a byte array. Only single-strip CCITT T4 or T6 compressed TIFF files are supported. If
+     * you're not sure what TIFF files you have, use
+     * {@link LosslessFactory#createFromImage(PDDocument, BufferedImage) }
+     * or {@link CCITTFactory#createFromImage(PDDocument, BufferedImage) }
+     * instead.
+     *
+     * @param document the document to create the image as part of.
+     * @param byteArray the TIFF file in a byte array which contains a suitable CCITT compressed
+     * image
+     * @param number TIFF image number, starting from 0
+     * @return a new Image XObject
+     * @throws IOException if there is an error reading the TIFF data.
+     */
+    public static PDImageXObject createFromByteArray(PDDocument document, byte[] byteArray, int number)
+            throws IOException
+    {
+        RandomAccess raf = new RandomAccessBuffer(byteArray);
+        try
+        {
+            return createFromRandomAccessImpl(document, raf, number);
+        }
+        finally
+        {
+            raf.close();
+        }
+    }
+
     private static PDImageXObject prepareImageXObject(PDDocument document,
             byte[] byteArray, int width, int height,
             PDColorSpace initColorSpace) throws IOException
@@ -161,15 +211,7 @@ public final class CCITTFactory
     public static PDImageXObject createFromFile(PDDocument document, File file)
             throws IOException
     {
-        RandomAccessFile raf = new RandomAccessFile(file, "r");
-        try
-        {
-            return createFromRandomAccessImpl(document, raf, 0);
-        }
-        finally
-        {
-            raf.close();
-        }
+        return createFromFile(document, file, 0);
     }
 
     /**