pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1599229 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image: CCITTFactory.java ImageFactory.java JPEGFactory.java LosslessFactory.java PDImageXObject.java
Date Mon, 02 Jun 2014 14:40:37 GMT
Author: tilman
Date: Mon Jun  2 14:40:36 2014
New Revision: 1599229

URL: http://svn.apache.org/r1599229
Log:
PDFBOX-2068: added width, height, bpc and colorspace to PDImageXObject constructor

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/ImageFactory.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/LosslessFactory.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.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=1599229&r1=1599228&r2=1599229&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 Jun  2 14:40:36 2014
@@ -75,18 +75,17 @@ public final class CCITTFactory
         {
             return null;
         }
-        ByteArrayInputStream byteStream = new ByteArrayInputStream(bos.toByteArray());
-        PDImageXObject pdImage = new PDImageXObject(document, byteStream, COSName.CCITTFAX_DECODE);
-
+        ByteArrayInputStream filteredByteStream = new ByteArrayInputStream(bos.toByteArray());
+        PDImageXObject pdImage = new PDImageXObject(document, 
+                filteredByteStream, 
+                COSName.CCITTFAX_DECODE, 
+                decodeParms.getInt(COSName.COLUMNS), 
+                decodeParms.getInt(COSName.ROWS),
+                1,
+                PDDeviceGray.INSTANCE);
+        
         COSDictionary dict = pdImage.getCOSStream();
-
         dict.setItem(COSName.DECODE_PARMS, decodeParms);
-
-        pdImage.setBitsPerComponent(1);
-        pdImage.setColorSpace(PDDeviceGray.INSTANCE);
-        pdImage.setWidth(decodeParms.getInt(COSName.COLUMNS));
-        pdImage.setHeight(decodeParms.getInt(COSName.ROWS));
-
         return pdImage;
     }
 

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/ImageFactory.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/ImageFactory.java?rev=1599229&r1=1599228&r2=1599229&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/ImageFactory.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/ImageFactory.java
Mon Jun  2 14:40:36 2014
@@ -1,102 +1,99 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.image;
-
-import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace;
-import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceCMYK;
-import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceGray;
-import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
-
-import java.awt.color.ColorSpace;
-import java.awt.color.ICC_ColorSpace;
-import java.awt.image.BufferedImage;
-import java.awt.image.ColorConvertOp;
-
-/**
- * An image factory.
- *
- * @author John Hewson
- * @author Brigitte Mathiak
- */
-class ImageFactory
-{
-    protected ImageFactory()
-    {
-    }
-
-    // sets Image XObject properties from an AWT buffered image
-    protected static void setPropertiesFromAWT(BufferedImage awtImage, PDImageXObject pdImage)
-    {
-        if (awtImage.getColorModel().getNumComponents() == 1)
-        {
-            // 256 color (gray) JPEG
-            pdImage.setColorSpace(PDDeviceGray.INSTANCE);
-        }
-        else
-        {
-            pdImage.setColorSpace(toPDColorSpace(awtImage.getColorModel().getColorSpace()));
-        }
-        pdImage.setBitsPerComponent(awtImage.getColorModel().getComponentSize(0));
-        pdImage.setHeight(awtImage.getHeight());
-        pdImage.setWidth(awtImage.getWidth());
-    }
-
-    // returns a PDColorSpace for a given AWT ColorSpace
-    protected static PDColorSpace toPDColorSpace(ColorSpace awtColorSpace)
-    {
-        if (awtColorSpace instanceof ICC_ColorSpace && !awtColorSpace.isCS_sRGB())
-        {
-            throw new UnsupportedOperationException("ICC color spaces not implemented");
-        }
-        else
-        {
-            switch (awtColorSpace.getType())
-            {
-                case ColorSpace.TYPE_RGB:  return PDDeviceRGB.INSTANCE;
-                case ColorSpace.TYPE_GRAY: return PDDeviceGray.INSTANCE;
-                case ColorSpace.TYPE_CMYK: return PDDeviceCMYK.INSTANCE;
-                default: throw new UnsupportedOperationException("color space not implemented:
" +
-                        awtColorSpace.getType());
-            }
-        }
-    }
-
-    // returns the color channels of an image
-    protected static BufferedImage getColorImage(BufferedImage image)
-    {
-        if (!image.getColorModel().hasAlpha())
-        {
-            return image;
-        }
-
-        if (image.getColorModel().getColorSpace().getType() != ColorSpace.TYPE_RGB)
-        {
-            throw new UnsupportedOperationException("only RGB color spaces are implemented");
-        }
-
-        // create an RGB image without alpha
-        //BEWARE: the previous solution in the history 
-        // g.setComposite(AlphaComposite.Src) and g.drawImage()
-        // didn't work properly for TYPE_4BYTE_ABGR.
-        // alpha values of 0 result in a black dest pixel!!!
-        BufferedImage rgbImage = new BufferedImage(
-                image.getWidth(),
-                image.getHeight(),
-                BufferedImage.TYPE_3BYTE_BGR);
-        return new ColorConvertOp(null).filter(image, rgbImage);
-    }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.image;
+
+import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace;
+import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceCMYK;
+import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceGray;
+import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
+
+import java.awt.color.ColorSpace;
+import java.awt.color.ICC_ColorSpace;
+import java.awt.image.BufferedImage;
+import java.awt.image.ColorConvertOp;
+
+/**
+ * An image factory.
+ *
+ * @author John Hewson
+ * @author Brigitte Mathiak
+ */
+class ImageFactory
+{
+    protected ImageFactory()
+    {
+    }
+
+    // returns a PDColorSpace for a given BufferedImage
+    protected static PDColorSpace getColorSpaceFromAWT(BufferedImage awtImage)
+    {
+        if (awtImage.getColorModel().getNumComponents() == 1)
+        {
+            // 256 color (gray) JPEG
+            return PDDeviceGray.INSTANCE;
+        }
+        else
+        {
+            return toPDColorSpace(awtImage.getColorModel().getColorSpace());
+        }
+    }
+
+    // returns a PDColorSpace for a given AWT ColorSpace
+    protected static PDColorSpace toPDColorSpace(ColorSpace awtColorSpace)
+    {
+        if (awtColorSpace instanceof ICC_ColorSpace && !awtColorSpace.isCS_sRGB())
+        {
+            throw new UnsupportedOperationException("ICC color spaces not implemented");
+        }
+        else
+        {
+            switch (awtColorSpace.getType())
+            {
+                case ColorSpace.TYPE_RGB:  return PDDeviceRGB.INSTANCE;
+                case ColorSpace.TYPE_GRAY: return PDDeviceGray.INSTANCE;
+                case ColorSpace.TYPE_CMYK: return PDDeviceCMYK.INSTANCE;
+                default: throw new UnsupportedOperationException("color space not implemented:
" +
+                        awtColorSpace.getType());
+            }
+        }
+    }
+
+    // returns the color channels of an image
+    protected static BufferedImage getColorImage(BufferedImage image)
+    {
+        if (!image.getColorModel().hasAlpha())
+        {
+            return image;
+        }
+
+        if (image.getColorModel().getColorSpace().getType() != ColorSpace.TYPE_RGB)
+        {
+            throw new UnsupportedOperationException("only RGB color spaces are implemented");
+        }
+
+        // create an RGB image without alpha
+        //BEWARE: the previous solution in the history 
+        // g.setComposite(AlphaComposite.Src) and g.drawImage()
+        // didn't work properly for TYPE_4BYTE_ABGR.
+        // alpha values of 0 result in a black dest pixel!!!
+        BufferedImage rgbImage = new BufferedImage(
+                image.getWidth(),
+                image.getHeight(),
+                BufferedImage.TYPE_3BYTE_BGR);
+        return new ColorConvertOp(null).filter(image, rgbImage);
+    }
+}

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=1599229&r1=1599228&r2=1599229&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
Mon Jun  2 14:40:36 2014
@@ -1,207 +1,209 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.image;
-
-import java.awt.Transparency;
-import java.awt.image.BufferedImage;
-import java.awt.image.WritableRaster;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.InputStream;
-import java.io.IOException;
-import java.util.Iterator;
-
-import org.apache.pdfbox.cos.COSName;
-import org.apache.pdfbox.filter.MissingImageReaderException;
-import org.apache.pdfbox.io.IOUtils;
-import org.apache.pdfbox.pdmodel.PDDocument;
-import javax.imageio.ImageIO;
-import javax.imageio.ImageReader;
-import javax.imageio.stream.ImageInputStream;
-import org.apache.pdfbox.util.ImageIOUtil;
-
-/**
- * Factory for creating a PDImageXObject containing a JPEG compressed image.
- * @author John Hewson
- */
-public final class JPEGFactory extends ImageFactory
-{
-    private JPEGFactory()
-    {
-    }
-
-    /**
-     * Creates a new JPEG Image XObject from an input stream containing JPEG data.
-     * 
-     * The input stream data will be preserved and embedded in the PDF file without modification.
-     * @param document the document where the image will be created
-     * @param stream a stream of JPEG data
-     * @return a new Image XObject
-     * 
-     * @throws IOException if the input stream cannot be read
-     */
-    public static PDImageXObject createFromStream(PDDocument document, InputStream stream)
-            throws IOException
-    {
-        // copy stream
-        ByteArrayInputStream byteStream = new ByteArrayInputStream(IOUtils.toByteArray(stream));
-
-        // read image
-        BufferedImage awtImage = readJPEG(byteStream);
-        byteStream.reset();
-
-        // create Image XObject from stream
-        PDImageXObject pdImage = new PDImageXObject(document, byteStream, COSName.DCT_DECODE);
-
-        // no alpha
-        if (awtImage.getColorModel().hasAlpha())
-        {
-            throw new UnsupportedOperationException("alpha channel not implemented");
-        }
-
-        // set properties (width, height, depth, color space, etc.)
-        setPropertiesFromAWT(awtImage, pdImage);
-
-        return pdImage;
-    }
-
-    private static BufferedImage readJPEG(InputStream stream) throws IOException
-    {
-        // find suitable image reader
-        Iterator readers = ImageIO.getImageReadersByFormatName("JPEG");
-        ImageReader reader = null;
-        while (readers.hasNext())
-        {
-            reader = (ImageReader) readers.next();
-            if (reader.canReadRaster())
-            {
-                break;
-            }
-        }
-
-        if (reader == null)
-        {
-            throw new MissingImageReaderException("Cannot read JPEG image: " +
-                    "a suitable JAI I/O image filter is not installed");
-        }
-
-        ImageInputStream iis = null;
-        try
-        {
-            iis = ImageIO.createImageInputStream(stream);
-            reader.setInput(iis);
-
-            ImageIO.setUseCache(false);
-            return reader.read(0);
-        }
-        finally
-        {
-            if (iis != null)
-            {
-                iis.close();
-            }
-            reader.dispose();
-        }
-    }
-
-    /**
-     * Creates a new JPEG Image XObject from a Buffered Image.
-     * @param document the document where the image will be created
-     * @param image the buffered image to embed
-     * @return a new Image XObject
-     * @throws IOException if the JPEG data cannot be written
-     */
-    public static PDImageXObject createFromImage(PDDocument document, BufferedImage image)
-        throws IOException
-    {
-        return createFromImage(document, image, 0.75f);
-    }
-
-    /**
-     * Creates a new JPEG Image XObject from a Buffered Image and a given quality.
-     * The image will be created at 72 DPI.
-     * @param document the document where the image will be created
-     * @param image the buffered image to embed
-     * @param quality the desired JPEG compression quality
-     * @return a new Image XObject
-     * @throws IOException if the JPEG data cannot be written
-     */
-    public static PDImageXObject createFromImage(PDDocument document, BufferedImage image,
-                                                 float quality) throws IOException
-    {
-        return createFromImage(document, image, quality, 72);
-    }
-
-    /**
-     * Creates a new JPEG Image XObject from a Buffered Image, a given quality and DPI.
-     * @param document the document where the image will be created
-     * @param image the buffered image to embed
-     * @param quality the desired JPEG compression quality
-     * @param dpi the desired DPI (resolution) of the JPEG
-     * @return a new Image XObject
-     * @throws IOException if the JPEG data cannot be written
-     */
-    public static PDImageXObject createFromImage(PDDocument document, BufferedImage image,
-                                                 float quality, int dpi) throws IOException
-    {
-        return createJPEG(document, image, quality, dpi);
-    }
-    
-    // returns the alpha channel of an image
-    private static BufferedImage getAlphaImage(BufferedImage image) throws IOException
-    {
-        if (!image.getColorModel().hasAlpha())
-        {
-            return null;
-        }
-        if (image.getTransparency() == Transparency.BITMASK)
-        {
-            throw new UnsupportedOperationException("BITMASK Transparency JPEG compression
is not useful, use LosslessImageFactory instead");
-        }
-        WritableRaster alphaRaster = image.getAlphaRaster();
-        BufferedImage alphaImage = new BufferedImage(image.getWidth(), image.getHeight(),
BufferedImage.TYPE_BYTE_GRAY);
-        alphaImage.setData(alphaRaster);
-        return alphaImage;
-    }
-    
-    // Creates an Image XObject from a Buffered Image using JAI Image I/O
-    private static PDImageXObject createJPEG(PDDocument document, BufferedImage image,
-                                             float quality, int dpi) throws IOException
-    {
-        // extract alpha channel (if any)
-        BufferedImage awtColorImage = getColorImage(image);
-        BufferedImage awtAlphaImage = getAlphaImage(image);
-
-        // create XObject
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        ImageIOUtil.writeImage(awtColorImage, "jpeg", baos, dpi, quality);
-        ByteArrayInputStream byteStream = new ByteArrayInputStream(baos.toByteArray());
-        PDImageXObject pdImage = new PDImageXObject(document, byteStream, COSName.DCT_DECODE);
-
-        // alpha -> soft mask
-        if (awtAlphaImage != null)
-        {
-            PDImage xAlpha = JPEGFactory.createFromImage(document, awtAlphaImage, quality);
-            pdImage.getCOSStream().setItem(COSName.SMASK, xAlpha);
-        }
-        
-        // set properties (width, height, depth, color space, etc.)
-        setPropertiesFromAWT(awtColorImage, pdImage);
-
-        return pdImage;
-    }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.image;
+
+import java.awt.Transparency;
+import java.awt.image.BufferedImage;
+import java.awt.image.WritableRaster;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.io.IOException;
+import java.util.Iterator;
+
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.filter.MissingImageReaderException;
+import org.apache.pdfbox.io.IOUtils;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import javax.imageio.ImageIO;
+import javax.imageio.ImageReader;
+import javax.imageio.stream.ImageInputStream;
+import org.apache.pdfbox.util.ImageIOUtil;
+
+/**
+ * Factory for creating a PDImageXObject containing a JPEG compressed image.
+ * @author John Hewson
+ */
+public final class JPEGFactory extends ImageFactory
+{
+    private JPEGFactory()
+    {
+    }
+
+    /**
+     * Creates a new JPEG Image XObject from an input stream containing JPEG data.
+     * 
+     * The input stream data will be preserved and embedded in the PDF file without modification.
+     * @param document the document where the image will be created
+     * @param stream a stream of JPEG data
+     * @return a new Image XObject
+     * 
+     * @throws IOException if the input stream cannot be read
+     */
+    public static PDImageXObject createFromStream(PDDocument document, InputStream stream)
+            throws IOException
+    {
+        // copy stream
+        ByteArrayInputStream byteStream = new ByteArrayInputStream(IOUtils.toByteArray(stream));
+
+        // read image
+        BufferedImage awtImage = readJPEG(byteStream);
+        byteStream.reset();
+
+        // create Image XObject from stream
+        PDImageXObject pdImage = new PDImageXObject(document, byteStream, 
+                COSName.DCT_DECODE, awtImage.getWidth(), awtImage.getHeight(), 
+                awtImage.getColorModel().getComponentSize(0),
+                getColorSpaceFromAWT(awtImage));
+
+        // no alpha
+        if (awtImage.getColorModel().hasAlpha())
+        {
+            throw new UnsupportedOperationException("alpha channel not implemented");
+        }
+
+        return pdImage;
+    }
+
+    private static BufferedImage readJPEG(InputStream stream) throws IOException
+    {
+        // find suitable image reader
+        Iterator readers = ImageIO.getImageReadersByFormatName("JPEG");
+        ImageReader reader = null;
+        while (readers.hasNext())
+        {
+            reader = (ImageReader) readers.next();
+            if (reader.canReadRaster())
+            {
+                break;
+            }
+        }
+
+        if (reader == null)
+        {
+            throw new MissingImageReaderException("Cannot read JPEG image: " +
+                    "a suitable JAI I/O image filter is not installed");
+        }
+
+        ImageInputStream iis = null;
+        try
+        {
+            iis = ImageIO.createImageInputStream(stream);
+            reader.setInput(iis);
+
+            ImageIO.setUseCache(false);
+            return reader.read(0);
+        }
+        finally
+        {
+            if (iis != null)
+            {
+                iis.close();
+            }
+            reader.dispose();
+        }
+    }
+
+    /**
+     * Creates a new JPEG Image XObject from a Buffered Image.
+     * @param document the document where the image will be created
+     * @param image the buffered image to embed
+     * @return a new Image XObject
+     * @throws IOException if the JPEG data cannot be written
+     */
+    public static PDImageXObject createFromImage(PDDocument document, BufferedImage image)
+        throws IOException
+    {
+        return createFromImage(document, image, 0.75f);
+    }
+
+    /**
+     * Creates a new JPEG Image XObject from a Buffered Image and a given quality.
+     * The image will be created at 72 DPI.
+     * @param document the document where the image will be created
+     * @param image the buffered image to embed
+     * @param quality the desired JPEG compression quality
+     * @return a new Image XObject
+     * @throws IOException if the JPEG data cannot be written
+     */
+    public static PDImageXObject createFromImage(PDDocument document, BufferedImage image,
+                                                 float quality) throws IOException
+    {
+        return createFromImage(document, image, quality, 72);
+    }
+
+    /**
+     * Creates a new JPEG Image XObject from a Buffered Image, a given quality and DPI.
+     * @param document the document where the image will be created
+     * @param image the buffered image to embed
+     * @param quality the desired JPEG compression quality
+     * @param dpi the desired DPI (resolution) of the JPEG
+     * @return a new Image XObject
+     * @throws IOException if the JPEG data cannot be written
+     */
+    public static PDImageXObject createFromImage(PDDocument document, BufferedImage image,
+                                                 float quality, int dpi) throws IOException
+    {
+        return createJPEG(document, image, quality, dpi);
+    }
+    
+    // returns the alpha channel of an image
+    private static BufferedImage getAlphaImage(BufferedImage image) throws IOException
+    {
+        if (!image.getColorModel().hasAlpha())
+        {
+            return null;
+        }
+        if (image.getTransparency() == Transparency.BITMASK)
+        {
+            throw new UnsupportedOperationException("BITMASK Transparency JPEG compression
is not useful, use LosslessImageFactory instead");
+        }
+        WritableRaster alphaRaster = image.getAlphaRaster();
+        BufferedImage alphaImage = new BufferedImage(image.getWidth(), image.getHeight(),
BufferedImage.TYPE_BYTE_GRAY);
+        alphaImage.setData(alphaRaster);
+        return alphaImage;
+    }
+    
+    // Creates an Image XObject from a Buffered Image using JAI Image I/O
+    private static PDImageXObject createJPEG(PDDocument document, BufferedImage image,
+                                             float quality, int dpi) throws IOException
+    {
+        // extract alpha channel (if any)
+        BufferedImage awtColorImage = getColorImage(image);
+        BufferedImage awtAlphaImage = getAlphaImage(image);
+
+        // create XObject
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ImageIOUtil.writeImage(awtColorImage, "jpeg", baos, dpi, quality);
+        ByteArrayInputStream byteStream = new ByteArrayInputStream(baos.toByteArray());
+        
+        
+        PDImageXObject pdImage = new PDImageXObject(document, byteStream, 
+                COSName.DCT_DECODE, awtColorImage.getWidth(), awtColorImage.getHeight(),

+                awtColorImage.getColorModel().getComponentSize(0),
+                getColorSpaceFromAWT(awtColorImage));
+
+        // alpha -> soft mask
+        if (awtAlphaImage != null)
+        {
+            PDImage xAlpha = JPEGFactory.createFromImage(document, awtAlphaImage, quality);
+            pdImage.getCOSStream().setItem(COSName.SMASK, xAlpha);
+        }
+
+        return pdImage;
+    }
+}

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/LosslessFactory.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/LosslessFactory.java?rev=1599229&r1=1599228&r2=1599229&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/LosslessFactory.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/LosslessFactory.java
Mon Jun  2 14:40:36 2014
@@ -23,11 +23,14 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import javax.imageio.stream.MemoryCacheImageOutputStream;
+import org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.filter.Filter;
 import org.apache.pdfbox.filter.FilterFactory;
 import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace;
 import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceColorSpace;
 import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceGray;
 import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
@@ -97,12 +100,8 @@ public class LosslessFactory
             }
         }
 
-        PDImageXObject pdImage = prepareImageXObject(document, bos.toByteArray());
-
-        pdImage.setColorSpace(deviceColorSpace);
-        pdImage.setBitsPerComponent(bpc);
-        pdImage.setHeight(image.getHeight());
-        pdImage.setWidth(image.getWidth());
+        PDImageXObject pdImage = prepareImageXObject(document, bos.toByteArray(), 
+                image.getWidth(), image.getHeight(), bpc, deviceColorSpace);
 
         // alpha -> soft mask
         PDImage xAlpha = createAlphaFromARGBImage(document, image);
@@ -170,35 +169,37 @@ public class LosslessFactory
             }
         }
 
-        PDImageXObject pdImage = prepareImageXObject(document, bos.toByteArray());
-
-        pdImage.setColorSpace(PDDeviceGray.INSTANCE);
-        pdImage.setBitsPerComponent(bpc);
-        pdImage.setHeight(image.getHeight());
-        pdImage.setWidth(image.getWidth());
+        PDImageXObject pdImage = prepareImageXObject(document, bos.toByteArray(), 
+                image.getWidth(), image.getHeight(), bpc, PDDeviceGray.INSTANCE);
 
         return pdImage;
     }
 
     /**
-     * Create a PDImageXObject with the data encoded with the Flate filter.
-     *
+     * Create a PDImageXObject while making a decision whether not to 
+     * compress, use Flate filter only, or Flate and LZW filters.
+     * 
      * @param document The document.
      * @param byteArray array with data.
+     * @param width the image width
+     * @param height the image height
+     * @param bitsPerComponent the bits per component
+     * @param initColorSpace the color space
      * @return the newly created PDImageXObject with the data compressed.
-     * @throws IOException
+     * @throws IOException 
      */
-    private static PDImageXObject prepareImageXObject(PDDocument document, byte[] byteArray)

-            throws IOException
+    private static PDImageXObject prepareImageXObject(PDDocument document, 
+            byte [] byteArray, int width, int height, int bitsPerComponent, 
+            PDColorSpace initColorSpace) throws IOException
     {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
         Filter filter = FilterFactory.INSTANCE.getFilter(COSName.FLATE_DECODE);
         filter.encode(new ByteArrayInputStream(byteArray), baos, new COSDictionary(), 0);
 
-        return new PDImageXObject(document,
-                new ByteArrayInputStream(baos.toByteArray()),
-                COSName.FLATE_DECODE);
+        ByteArrayInputStream filteredByteStream = new ByteArrayInputStream(baos.toByteArray());
+        return new PDImageXObject(document, filteredByteStream, COSName.FLATE_DECODE, 
+                width, height, bitsPerComponent, initColorSpace);
     }
 
 }

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=1599229&r1=1599228&r2=1599229&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
Mon Jun  2 14:40:36 2014
@@ -84,14 +84,24 @@ public final class PDImageXObject extend
      * @param document the current document
      * @param filteredStream a filtered stream of image data
      * @param cosFilter the filter or a COSArray of filters
+     * @param width the image width
+     * @param height the image height
+     * @param bitsPerComponent the bits per component
+     * @param initColorSpace the color space
      * @throws IOException if there is an error creating the XObject.
      */
-    public PDImageXObject(PDDocument document, InputStream filteredStream, COSBase cosFilter)
throws IOException
+    public PDImageXObject(PDDocument document, InputStream filteredStream, 
+            COSBase cosFilter, int width, int height, int bitsPerComponent, 
+            PDColorSpace initColorSpace) throws IOException
     {
         super(new PDStream(document, filteredStream, true), COSName.IMAGE);
         getCOSStream().setItem(COSName.FILTER, cosFilter);
         colorSpaces = null;
         colorSpace = null;
+        setBitsPerComponent(bitsPerComponent);
+        setWidth(width);
+        setHeight(height);
+        setColorSpace(initColorSpace);
     }
 
     /**



Mime
View raw message