Author: jahewson
Date: Sat Mar 15 23:53:10 2014
New Revision: 1577959

URL: http://svn.apache.org/r1577959
Log:
PDFBOX-1983: Unable to add TIF images, CCITTFactory not working

Modified:
    
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/CCITTFactory.java
    
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java
    
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java
    
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/SampledImageReader.java
    
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/image/CCITTFactoryTest.java
    
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactoryTest.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=1577959&r1=1577958&r2=1577959&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
 Sat Mar 15 23:53:10 2014
@@ -54,7 +54,7 @@ public final class CCITTFactory
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         extractFromTiff(reader, bos, decodeParms);
         ByteArrayInputStream byteStream = new 
ByteArrayInputStream(bos.toByteArray());
-        PDImageXObject pdImage = new PDImageXObject(new PDStream(document, 
byteStream, true), null);
+        PDImageXObject pdImage = new PDImageXObject(document, byteStream);
 
         COSDictionary dict = pdImage.getCOSStream();
 

Modified: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java?rev=1577959&r1=1577958&r2=1577959&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java
 (original)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java
 Sat Mar 15 23:53:10 2014
@@ -65,7 +65,7 @@ public final class JPEGFactory extends I
         byteStream.reset();
 
         // create Image XObject from stream
-        PDImageXObject pdImage = new PDImageXObject(new PDStream(document, 
byteStream, true), null);
+        PDImageXObject pdImage = new PDImageXObject(document, byteStream);
 
         // add DCT filter
         pdImage.getCOSStream().setItem(COSName.FILTER, COSName.DCT_DECODE);
@@ -176,7 +176,7 @@ public final class JPEGFactory extends I
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         ImageIOUtil.writeImage(image, "jpeg", bos, dpi, quality);
         ByteArrayInputStream byteStream = new 
ByteArrayInputStream(bos.toByteArray());
-        PDImageXObject pdImage = new PDImageXObject(new PDStream(document, 
byteStream, true), null);
+        PDImageXObject pdImage = new PDImageXObject(document, byteStream);
         
         // add DCT filter
         COSStream dict = pdImage.getCOSStream();

Modified: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java?rev=1577959&r1=1577958&r2=1577959&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java
 (original)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java
 Sat Mar 15 23:53:10 2014
@@ -33,6 +33,7 @@ import java.awt.Paint;
 import java.awt.image.BufferedImage;
 import java.awt.image.WritableRaster;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.List;
 import java.util.Map;
 
@@ -71,6 +72,19 @@ public final class PDImageXObject extend
     }
 
     /**
+     * Creates an Image XObject in the given document using the given filtered 
stream.
+     * @param document the current document
+     * @param filteredStream a filtered stream of image data
+     * @throws IOException
+     */
+    public PDImageXObject(PDDocument document, InputStream filteredStream) 
throws IOException
+    {
+        super(new PDStream(document, filteredStream, true), COSName.IMAGE);
+        colorSpaces = null;
+        colorSpace = null;
+    }
+
+    /**
      * Creates an Image XObject with the given stream as its contents and 
current color spaces.
      * @param stream the XObject stream to read
      * @param colorSpaces the color spaces in the current resources 
dictionary, null for masks

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=1577959&r1=1577958&r2=1577959&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 Mar 15 23:53:10 2014
@@ -107,7 +107,7 @@ final class SampledImageReader
      */
     public static BufferedImage getRGBImage(PDImage pdImage, COSArray 
colorKey) throws IOException
     {
-        if (pdImage.getStream().getLength() == 0)
+        if (pdImage.getStream().getStream().getFilteredLength() == 0)
         {
             throw new IOException("Image stream is empty");
         }

Modified: 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/image/CCITTFactoryTest.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/image/CCITTFactoryTest.java?rev=1577959&r1=1577958&r2=1577959&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/image/CCITTFactoryTest.java
 (original)
+++ 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/image/CCITTFactoryTest.java
 Sat Mar 15 23:53:10 2014
@@ -16,38 +16,30 @@
 package org.apache.pdfbox.pdmodel.graphics.image;
 
 import java.io.File;
+import java.io.IOException;
+
 import junit.framework.TestCase;
-import static junit.framework.TestCase.assertEquals;
 import org.apache.pdfbox.io.RandomAccess;
 import org.apache.pdfbox.io.RandomAccessFile;
 import org.apache.pdfbox.pdmodel.PDDocument;
-import org.junit.Test;
-import static org.junit.Assert.*;
 
 /**
- *
+ * Unit tests for CCITTFactory
  * @author Tilman Hausherr
  */
 public class CCITTFactoryTest extends TestCase
 {
     /**
-     * {@inheritDoc}
+     * Tests CCITTFactory#createFromRandomAccess(PDDocument document, 
RandomAccess reader)
      */
-    @Override
-    public void setUp() throws Exception
-    {
-        super.setUp();
-    }
-
-    /**
-     * Test of createFromRandomAccess method, of class CCITTFactory.
-     */
-    @Test
-    public void testCreateFromRandomAccess() throws Exception
+    public void testCreateFromRandomAccess() throws IOException
     {
+        String tiffPath = 
"src/test/resources/org/apache/pdfbox/pdmodel/graphics/image/ccittg4.tif";
         PDDocument document = new PDDocument();
-        RandomAccess reader = new RandomAccessFile(new 
File("src/test/resources/org/apache/pdfbox/pdmodel/graphics/image/ccittg4.tif"),
 "r");
+        RandomAccess reader = new RandomAccessFile(new File(tiffPath), "r");
         PDImageXObject ximage = CCITTFactory.createFromRandomAccess(document, 
reader);
+
+        // check the dictionary
         assertNotNull(ximage);
         assertNotNull(ximage.getCOSStream());
         assertTrue(ximage.getCOSStream().getFilteredLength() > 0);
@@ -56,11 +48,11 @@ public class CCITTFactoryTest extends Te
         assertEquals(287, ximage.getHeight());
         assertEquals("tiff", ximage.getSuffix());
 
-        //TODO shouldn't ximage.getImage() return a real image?
-//        assertNotNull(ximage.getImage());
-//        assertEquals(344, ximage.getImage().getWidth());
-//        assertEquals(287, ximage.getImage().getHeight());
+        // check the image
+        assertNotNull(ximage.getImage());
+        assertEquals(344, ximage.getImage().getWidth());
+        assertEquals(287, ximage.getImage().getHeight());
+
         document.close();
     }
-
 }

Modified: 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactoryTest.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactoryTest.java?rev=1577959&r1=1577958&r2=1577959&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactoryTest.java
 (original)
+++ 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactoryTest.java
 Sat Mar 15 23:53:10 2014
@@ -15,86 +15,79 @@
  */
 package org.apache.pdfbox.pdmodel.graphics.image;
 
-import java.awt.Color;
 import java.awt.Graphics;
 import java.awt.image.BufferedImage;
-import java.io.File;
-import java.io.FileInputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import javax.imageio.ImageIO;
 import junit.framework.TestCase;
-import static junit.framework.TestCase.assertEquals;
 import org.apache.pdfbox.pdmodel.PDDocument;
-import org.junit.Test;
-import static org.junit.Assert.*;
 
 /**
- *
+ * Unit tests for JPEGFactory
  * @author Tilman Hausherr
  */
 public class JPEGFactoryTest extends TestCase
 {
-
     /**
-     * {@inheritDoc}
+     * Tests JPEGFactory#createFromStream(PDDocument document, InputStream 
stream)
      */
-    @Override
-    public void setUp() throws Exception
+    public void testCreateFromStream() throws IOException
     {
-        super.setUp();
+        PDDocument document = new PDDocument();
+        InputStream stream = 
JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg");
+        PDImageXObject ximage = JPEGFactory.createFromStream(document, stream);
+        validate(ximage);
+        document.close();
     }
 
     /**
-     * Test of createFromStream method, of class JPEGFactory.
+     * Tests RGB JPEGFactory#createFromImage(PDDocument document, 
BufferedImage image)
      */
-    @Test
-    public void testCreateFromStream() throws Exception
+    public void testCreateFromImageRGB() throws IOException
     {
         PDDocument document = new PDDocument();
-        InputStream stream = new FileInputStream(new 
File("src/test/resources/org/apache/pdfbox/pdmodel/graphics/image/jpeg.jpg"));
-        PDImageXObject ximage = JPEGFactory.createFromStream(document, stream);
-        assertNotNull(ximage);
-        assertNotNull(ximage.getCOSStream());
-        assertTrue(ximage.getCOSStream().getFilteredLength() > 0);
-        assertEquals(8, ximage.getBitsPerComponent());
-        assertEquals(344, ximage.getWidth());
-        assertEquals(287, ximage.getHeight());
-        assertEquals("jpg", ximage.getSuffix());
-
-        //TODO shouldn't ximage.getImage() return a real image?
-//        assertNotNull(ximage.getImage());
-//        assertEquals(344, ximage.getImage().getWidth());
-//        assertEquals(287, ximage.getImage().getHeight());
+        BufferedImage image = 
ImageIO.read(JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg"));
+        PDImageXObject ximage = JPEGFactory.createFromImage(document, image);
+        validate(ximage);
         document.close();
     }
 
     /**
-     * Test of createFromImage method, of class JPEGFactory.
+     * Tests ARGB JPEGFactory#createFromImage(PDDocument document, 
BufferedImage image)
      */
-    @Test
-    public void testCreateFromImage() throws Exception
+    public void testCreateFromImageARGB() throws IOException
     {
-
         PDDocument document = new PDDocument();
-        BufferedImage rgbImage = ImageIO.read(new 
File("src/test/resources/org/apache/pdfbox/pdmodel/graphics/image/jpeg.jpg"));
+        BufferedImage image = 
ImageIO.read(JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg"));
 
-        // Create an ARGB image
-        int w = rgbImage.getWidth();
-        int h = rgbImage.getHeight();
+        // create an ARGB image
+        int w = image.getWidth();
+        int h = image.getHeight();
         BufferedImage argbImage = new BufferedImage(w, h, 
BufferedImage.TYPE_INT_ARGB);
         Graphics ag = argbImage.getGraphics();
-        ag.drawImage(rgbImage, 0, 0, null);
+        ag.drawImage(image, 0, 0, null);
         ag.dispose();
-        // left half of image with 1/2 transparency
+
+        // left half of image with 50% alpha
         for (int x = 0; x < w / 2; ++x)
         {
             for (int y = 0; y < h; ++y)
             {
-                argbImage.setRGB(x, y, rgbImage.getRGB(x, y) & 0x7FFFFFFF);
+                argbImage.setRGB(x, y, image.getRGB(x, y) & 0x7FFFFFFF);
             }
         }
 
-        PDImageXObject ximage = JPEGFactory.createFromImage(document, 
rgbImage);
+        PDImageXObject ximage = JPEGFactory.createFromImage(document, image);
+        validate(ximage);
+        assertNull(ximage.getSoftMask());
+
+        document.close();
+    }
+
+    private void validate(PDImageXObject ximage) throws IOException
+    {
+        // check the dictionary
         assertNotNull(ximage);
         assertNotNull(ximage.getCOSStream());
         assertTrue(ximage.getCOSStream().getFilteredLength() > 0);
@@ -102,20 +95,10 @@ public class JPEGFactoryTest extends Tes
         assertEquals(344, ximage.getWidth());
         assertEquals(287, ximage.getHeight());
         assertEquals("jpg", ximage.getSuffix());
-        assertNull(ximage.getSoftMask());
-        
-//TODO when ARGB works        
-//        PDImageXObject ximage = JPEGFactory.createFromImage(document, 
argbImage);
-//        assertNotNull(ximage.getSoftMask());
-// etc...        
-
 
-        
-        //TODO shouldn't ximage.getImage() return a real image?
-//        assertNotNull(ximage.getImage());
-//        assertEquals(344, ximage.getImage().getWidth());
-//        assertEquals(287, ximage.getImage().getHeight());
-//        document.close();
+        // check the image
+        assertNotNull(ximage.getImage());
+        assertEquals(344, ximage.getImage().getWidth());
+        assertEquals(287, ximage.getImage().getHeight());
     }
-
 }


Reply via email to