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 2014/05/31 23:42:46 UTC

svn commit: r1598926 - in /pdfbox/branches/1.8/pdfbox/src: main/java/org/apache/pdfbox/pdmodel/graphics/xobject/ test/java/org/apache/pdfbox/ test/java/org/apache/pdfbox/pdmodel/graphics/xobject/ test/resources/org/apache/pdfbox/pdmodel/graphics/xobject/

Author: tilman
Date: Sat May 31 21:42:46 2014
New Revision: 1598926

URL: http://svn.apache.org/r1598926
Log:
PDFBOX-2105: added support and test for multipage tiffs

Added:
    pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDCcittTest.java   (with props)
    pdfbox/branches/1.8/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/graphics/xobject/ccittg4multi.tif   (with props)
Modified:
    pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDCcitt.java
    pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/TestAll.java
    pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDPixelMapTest.java
    pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDUtils.java

Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDCcitt.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDCcitt.java?rev=1598926&r1=1598925&r2=1598926&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDCcitt.java (original)
+++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDCcitt.java Sat May 31 21:42:46 2014
@@ -68,18 +68,19 @@ public class PDCcitt extends PDXObjectIm
     public PDCcitt(PDStream ccitt)
     {
         super(ccitt, "tiff");
-
     }
-
+    
     /**
      * Construct from a tiff file.
      * 
      * @param doc The document to create the image as part of.
      * @param raf The random access TIFF file which contains a suitable CCITT compressed image
+     * @param number TIFF image number, starting from 0
      * @throws IOException If there is an error reading the tiff data.
+     * @throws IllegalArgumentException If there is no such image number
      */
-
-    public PDCcitt(PDDocument doc, RandomAccess raf) throws IOException
+    public PDCcitt(PDDocument doc, RandomAccess raf, int number) 
+            throws IOException, IllegalArgumentException
     {
         super(new PDStream(doc), "tiff");
 
@@ -98,7 +99,20 @@ public class PDCcitt extends PDXObjectIm
         setColorSpace(new PDDeviceGray());
         setWidth(decodeParms.getInt(COSName.COLUMNS));
         setHeight(decodeParms.getInt(COSName.ROWS));
+    }    
+    
 
+    /**
+     * Construct from a tiff file.
+     * 
+     * @param doc The document to create the image as part of.
+     * @param raf The random access TIFF file which contains a suitable CCITT compressed image
+     * @throws IOException If there is an error reading the tiff data.
+     */
+
+    public PDCcitt(PDDocument doc, RandomAccess raf) throws IOException
+    {
+        this(doc, raf, 0);
     }
 
     /**
@@ -230,13 +244,16 @@ public class PDCcitt extends PDXObjectIm
      * @param raf - TIFF File
      * @param os - Stream to write raw ccitt data two
      * @param parms - COSDictionary which the encoding parameters are added to
+     * @param number TIFF image number, starting from 0
      * @throws IOException If there is an error reading/writing to/from the stream
+     * @throws IllegalArgumentException If there is no such image number
      */
-    private void extractFromTiff(RandomAccess raf, OutputStream os, COSDictionary parms) throws IOException
+    private void extractFromTiff(RandomAccess raf, OutputStream os, 
+            COSDictionary parms, int number) 
+            throws IOException, IllegalArgumentException
     {
         try
         {
-
             // First check the basic tiff header
             raf.seek(0);
             char endianess = (char) raf.read();
@@ -256,7 +273,26 @@ public class PDCcitt extends PDXObjectIm
             }
 
             // Relocate to the first set of tags
-            raf.seek(readlong(endianess, raf));
+            int address = readlong(endianess, raf);
+            raf.seek(address);
+    
+            // If some higher page number is required, skip this page's tags, 
+            // then read the next page's address
+            for (int i = 0; i < number; i++)
+            {
+                int numtags = readshort(endianess, raf);
+                if (numtags > 50)
+                {
+                    throw new IOException("Not a valid tiff file");
+                }
+                raf.seek(address + 2 + numtags * 12);
+                address = readlong(endianess, raf);
+                if (address == 0)
+                {
+                    throw new IllegalArgumentException("Image number " + number + " does not exist");
+                }
+                raf.seek(address);
+            }
 
             int numtags = readshort(endianess, raf);
 

Modified: pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/TestAll.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/TestAll.java?rev=1598926&r1=1598925&r2=1598926&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/TestAll.java (original)
+++ pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/TestAll.java Sat May 31 21:42:46 2014
@@ -42,6 +42,7 @@ import org.apache.pdfbox.pdmodel.edit.Te
 import org.apache.pdfbox.pdmodel.graphics.color.PDColorStateTest;
 import org.apache.pdfbox.pdmodel.graphics.color.PDLabTest;
 import org.apache.pdfbox.pdmodel.graphics.optionalcontent.TestOptionalContentGroups;
+import org.apache.pdfbox.pdmodel.graphics.xobject.PDCcittTest;
 import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpegTest;
 import org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMapTest;
 import org.apache.pdfbox.pdmodel.interactive.form.TestFields;
@@ -122,7 +123,8 @@ public class TestAll extends TestCase
         suite.addTestSuite(PDFCloneUtilityTest.class);
         suite.addTestSuite(PDLabTest.class);
         suite.addTestSuite(PDPixelMapTest.class);
-        suite.addTestSuite(PDJpegTest.class);        
+        suite.addTestSuite(PDJpegTest.class);       
+        suite.addTestSuite(PDCcittTest.class);        
         suite.addTestSuite(TestPDFText2HTML.class);
         suite.addTestSuite(PDColorStateTest.class);
 

Added: pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDCcittTest.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDCcittTest.java?rev=1598926&view=auto
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDCcittTest.java (added)
+++ pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDCcittTest.java Sat May 31 21:42:46 2014
@@ -0,0 +1,125 @@
+/*
+ * Copyright 2014 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.pdfbox.pdmodel.graphics.xobject;
+
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import javax.imageio.ImageIO;
+import javax.imageio.ImageReader;
+import javax.imageio.stream.ImageInputStream;
+import junit.framework.TestCase;
+import org.apache.pdfbox.exceptions.COSVisitorException;
+import org.apache.pdfbox.io.RandomAccess;
+import org.apache.pdfbox.io.RandomAccessFile;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
+import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceGray;
+import static org.apache.pdfbox.pdmodel.graphics.xobject.PDJpegTest.validate;
+import static org.apache.pdfbox.pdmodel.graphics.xobject.PDUtils.checkIdent;
+import static org.apache.pdfbox.pdmodel.graphics.xobject.PDUtils.doWritePDF;
+
+/**
+ *
+ * @author Tilman Hausherr
+ */
+public class PDCcittTest extends TestCase
+{
+    private final File testResultsDir = new File("target/test-output/graphics");
+
+    @Override
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        testResultsDir.mkdirs();
+    }
+
+    /**
+     * Tests CCITTFactory#createFromRandomAccess(PDDocument document,
+     * RandomAccess reader) with a single page TIFF
+     */
+    public void testCreateFromRandomAccessSingle() throws IOException, COSVisitorException
+    {
+        String tiffPath = "src/test/resources/org/apache/pdfbox/pdmodel/graphics/xobject/ccittg4.tif";
+        
+        PDDocument document = new PDDocument();
+        RandomAccess reader = new RandomAccessFile(new File(tiffPath), "r");
+        PDXObjectImage ximage = new PDCcitt(document, reader);
+        validate(ximage, 1, 344, 287, "tiff", PDDeviceGray.NAME);
+        
+        doWritePDF(document, ximage, testResultsDir, "singletiff.pdf");
+    }
+    
+    /**
+     * Tests CCITTFactory#createFromRandomAccess(PDDocument document,
+     * RandomAccess reader) with a multi page TIFF
+     */
+    public void testCreateFromRandomAccessMulti() throws IOException, COSVisitorException
+    {
+        String tiffPath = "src/test/resources/org/apache/pdfbox/pdmodel/graphics/xobject/ccittg4multi.tif";
+        
+        ImageInputStream is = ImageIO.createImageInputStream(new File(tiffPath));
+        ImageReader imageReader = ImageIO.getImageReaders(is).next();
+        imageReader.setInput(is);
+        int countTiffImages = imageReader.getNumImages(true);
+        assertTrue(countTiffImages > 1);
+        
+        PDDocument document = new PDDocument();
+        RandomAccess reader = new RandomAccessFile(new File(tiffPath), "r");
+        
+        int pdfPageNum = 0;
+        while (true)
+        {
+            PDXObjectImage ximage = null;
+            try
+            {
+                ximage = new PDCcitt(document, reader, pdfPageNum);
+            }
+            catch (IllegalArgumentException e)
+            {
+                break;
+            }
+            BufferedImage bim = imageReader.read(pdfPageNum);
+            validate(ximage, 1, bim.getWidth(), bim.getHeight(), "tiff", PDDeviceGray.NAME);
+            checkIdent(bim, ximage.getRGBImage());
+            PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
+            float fX = ximage.getWidth() / page.getMediaBox().getWidth();
+            float fY = ximage.getHeight() / page.getMediaBox().getHeight();
+            float factor = Math.max(fX, fY);
+            document.addPage(page);
+            PDPageContentStream contentStream = new PDPageContentStream(document, page, true, false);
+            contentStream.drawXObject(ximage, 0, 0, ximage.getWidth() / factor, ximage.getHeight() / factor);
+            contentStream.close();
+            ++pdfPageNum;
+        }
+        
+        assertEquals(countTiffImages, pdfPageNum);
+
+        document.save(testResultsDir + "/multitiff.pdf");
+        document.close();
+        
+        document = PDDocument.loadNonSeq(new File(testResultsDir, "multitiff.pdf"), null);
+        List pages = document.getDocumentCatalog().getAllPages();
+        assertEquals(countTiffImages, pages.size());
+        
+        document.close();  
+        imageReader.dispose();
+    }
+    
+}

Propchange: pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDCcittTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDPixelMapTest.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDPixelMapTest.java?rev=1598926&r1=1598925&r2=1598926&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDPixelMapTest.java (original)
+++ pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDPixelMapTest.java Sat May 31 21:42:46 2014
@@ -20,6 +20,7 @@ import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.GraphicsConfiguration;
 import java.awt.Transparency;
+import java.awt.geom.AffineTransform;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
@@ -240,6 +241,13 @@ public class PDPixelMapTest extends Test
         PDPageContentStream contentStream = new PDPageContentStream(document, page, true, false);
         contentStream.drawXObject(ximage, 150, 300, ximage.getWidth(), ximage.getHeight());
         contentStream.drawXObject(ximage, 200, 350, ximage.getWidth(), ximage.getHeight());
+        
+        // Example to use rotation, also posted to
+        // http://stackoverflow.com/a/23876858/535646
+        AffineTransform at = new AffineTransform(ximage.getWidth() / 4, 0, 0, ximage.getHeight() / 4, 100, 600);
+        at.rotate(Math.toRadians(10));
+        contentStream.drawXObject(ximage, at);
+        
         contentStream.close();
         File pdfFile = new File(testResultsDir, "intrgb.pdf");
         document.save(pdfFile);

Modified: pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDUtils.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDUtils.java?rev=1598926&r1=1598925&r2=1598926&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDUtils.java (original)
+++ pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDUtils.java Sat May 31 21:42:46 2014
@@ -19,9 +19,16 @@ package org.apache.pdfbox.pdmodel.graphi
 import java.awt.Color;
 import java.awt.Graphics;
 import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 import static junit.framework.Assert.assertEquals;
+import org.apache.pdfbox.exceptions.COSVisitorException;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
 
 /**
  *
@@ -85,4 +92,37 @@ public class PDUtils
         return colors.size();
     }
     
+
+    // write image twice (overlapped) in document, close document and re-read PDF
+    static void doWritePDF(PDDocument document, PDXObjectImage ximage, File testResultsDir, String filename)
+            throws IOException, COSVisitorException
+    {
+        File pdfFile = new File(testResultsDir, filename);
+
+        // This part isn't really needed because this test doesn't break
+        // if the mask has the wrong colorspace (PDFBOX-2057), but it is still useful
+        // if something goes wrong in the future and we want to have a PDF to open.
+        int width = ximage.getWidth();
+        int height = ximage.getHeight();
+
+        PDPage page = new PDPage();
+        document.addPage(page);
+        PDPageContentStream contentStream = new PDPageContentStream(document, page, true, false);
+        contentStream.drawXObject(ximage, 150, 300, width, height);
+        contentStream.drawXObject(ximage, 200, 350, width, height);
+        contentStream.close();
+
+        document.save(pdfFile);
+        document.close();
+
+        document = PDDocument.loadNonSeq(pdfFile, null);
+        List<PDPage> pdPages = document.getDocumentCatalog().getAllPages();
+        for (PDPage pdPage : pdPages)
+        {
+            pdPage.convertToImage();
+        }
+        document.close();
+    }
+        
+    
 }

Added: pdfbox/branches/1.8/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/graphics/xobject/ccittg4multi.tif
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/graphics/xobject/ccittg4multi.tif?rev=1598926&view=auto
==============================================================================
Binary file - no diff available.

Propchange: pdfbox/branches/1.8/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/graphics/xobject/ccittg4multi.tif
------------------------------------------------------------------------------
    svn:mime-type = image/tiff