pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1837545 - in /pdfbox/branches/2.0/tools/src: main/java/org/apache/pdfbox/tools/imageio/ImageIOUtil.java test/java/org/apache/pdfbox/tools/imageio/TestImageIOUtils.java
Date Mon, 06 Aug 2018 18:26:34 GMT
Author: tilman
Date: Mon Aug  6 18:26:34 2018
New Revision: 1837545

URL: http://svn.apache.org/viewvc?rev=1837545&view=rev
Log:
PDFBOX-4285: allow user to choose tiff compression, as suggested by Alexandre Moraes do Nasciment

Modified:
    pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/imageio/ImageIOUtil.java
    pdfbox/branches/2.0/tools/src/test/java/org/apache/pdfbox/tools/imageio/TestImageIOUtils.java

Modified: pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/imageio/ImageIOUtil.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/imageio/ImageIOUtil.java?rev=1837545&r1=1837544&r2=1837545&view=diff
==============================================================================
--- pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/imageio/ImageIOUtil.java
(original)
+++ pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/imageio/ImageIOUtil.java
Mon Aug  6 18:26:34 2018
@@ -170,8 +170,8 @@ public final class ImageIOUtil
      * Compression is fixed for PNG, GIF, BMP and WBMP, dependent of the quality
      * parameter for JPG, and dependent of bit count for TIFF (a bitonal image
      * will be compressed with CCITT G4, a color image with LZW). Creating a
-     * TIFF image is only supported if the jai_imageio library is in the class
-     * path.
+     * TIFF image is only supported if the jai_imageio library (or equivalent)
+     * is in the class path.
      *
      * @param image the image to be written
      * @param formatName the target format (ex. "png")
@@ -185,6 +185,33 @@ public final class ImageIOUtil
     public static boolean writeImage(BufferedImage image, String formatName, OutputStream
output,
             int dpi, float quality) throws IOException
     {
+        return writeImage(image, formatName, output, dpi, 1.0f, "");
+    }
+
+    /**
+     * Writes a buffered image to a file using the given image format.
+     * Compression is fixed for PNG, GIF, BMP and WBMP, dependent of the quality
+     * parameter for JPG, and dependent of bit count for TIFF (a bitonal image
+     * will be compressed with CCITT G4, a color image with LZW). Creating a
+     * TIFF image is only supported if the jai_imageio library is in the class
+     * path.
+     *
+     * @param image the image to be written
+     * @param formatName the target format (ex. "png")
+     * @param output the output stream to be used for writing
+     * @param dpi the resolution in dpi (dots per inch) to be used in metadata
+     * @param compressionQuality quality to be used when compressing the image
+     * (0 < quality < 1.0f)
+     * @param compressionType Advanced users only, and only relevant for TIFF
+     * files: If null, save uncompressed; if empty string, use logic explained
+     * above; other valid values are found in the javadoc of
+     * <a href="https://download.java.net/media/jai-imageio/javadoc/1.1/com/sun/media/imageio/plugins/tiff/TIFFImageWriteParam.html">TIFFImageWriteParam</a>.
+     * @return true if the image file was produced, false if there was an error.
+     * @throws IOException if an I/O error occurs
+     */
+    public static boolean writeImage(BufferedImage image, String formatName, OutputStream
output,
+            int dpi, float compressionQuality, String compressionType) throws IOException
+    {
         ImageOutputStream imageOutput = null;
         ImageWriter writer = null;
         try
@@ -236,13 +263,24 @@ public final class ImageIOUtil
                 param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                 if (formatName.toLowerCase().startsWith("tif"))
                 {
-                    // TIFF compression
-                    TIFFUtil.setCompressionType(param, image);
+                    if ("".equals(compressionType))
+                    {
+                        // default logic
+                        TIFFUtil.setCompressionType(param, image);
+                    }
+                    else
+                    {
+                        param.setCompressionType(compressionType);
+                        if (compressionType != null)
+                        {
+                            param.setCompressionQuality(compressionQuality);
+                        }
+                    }
                 }
                 else
                 {
                     param.setCompressionType(param.getCompressionTypes()[0]);
-                    param.setCompressionQuality(quality);
+                    param.setCompressionQuality(compressionQuality);
                 }
             }
 

Modified: pdfbox/branches/2.0/tools/src/test/java/org/apache/pdfbox/tools/imageio/TestImageIOUtils.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/tools/src/test/java/org/apache/pdfbox/tools/imageio/TestImageIOUtils.java?rev=1837545&r1=1837544&r2=1837545&view=diff
==============================================================================
--- pdfbox/branches/2.0/tools/src/test/java/org/apache/pdfbox/tools/imageio/TestImageIOUtils.java
(original)
+++ pdfbox/branches/2.0/tools/src/test/java/org/apache/pdfbox/tools/imageio/TestImageIOUtils.java
Mon Aug  6 18:26:34 2018
@@ -24,8 +24,10 @@ import java.io.DataInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.FilenameFilter;
 import java.io.IOException;
+import java.io.OutputStream;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Set;
@@ -123,43 +125,54 @@ public class TestImageIOUtils extends Te
             checkSaveResources(document.getPage(0).getResources());
 
             // testing PNG
-            writeImage(document, imageType, outDir + file.getName() + "-", ImageType.RGB,
dpi);
+            writeImage(document, imageType, outDir + file.getName() + "-", ImageType.RGB,
dpi, 1, "");
             checkResolution(outDir + file.getName() + "-1." + imageType, (int) dpi);
             checkFileTypeByContent(outDir + file.getName() + "-1." + imageType, FileType.PNG);
 
             // testing JPG/JPEG
             imageType = "jpg";
-            writeImage(document, imageType, outDir + file.getName() + "-", ImageType.RGB,
dpi);
+            writeImage(document, imageType, outDir + file.getName() + "-", ImageType.RGB,
dpi, 0.5f, "");
             checkResolution(outDir + file.getName() + "-1." + imageType, (int) dpi);
             checkFileTypeByContent(outDir + file.getName() + "-1." + imageType, FileType.JPEG);
 
             // testing BMP
             imageType = "bmp";
-            writeImage(document, imageType, outDir + file.getName() + "-", ImageType.RGB,
dpi);
+            writeImage(document, imageType, outDir + file.getName() + "-", ImageType.RGB,
dpi, 1, "");
             checkResolution(outDir + file.getName() + "-1." + imageType, (int) dpi);
             checkFileTypeByContent(outDir + file.getName() + "-1." + imageType, FileType.BMP);
 
             // testing GIF
             imageType = "gif";
-            writeImage(document, imageType, outDir + file.getName() + "-", ImageType.RGB,
dpi);
+            writeImage(document, imageType, outDir + file.getName() + "-", ImageType.RGB,
dpi, 1, "");
             // no META data posible for GIF, thus no dpi test
             checkFileTypeByContent(outDir + file.getName() + "-1." + imageType, FileType.GIF);
 
             // testing WBMP
             imageType = "wbmp";
-            writeImage(document, imageType, outDir + file.getName() + "-", ImageType.BINARY,
dpi);
+            writeImage(document, imageType, outDir + file.getName() + "-", ImageType.BINARY,
dpi, 1, "");
             // no META data posible for WBMP, thus no dpi test
 
             // testing TIFF
             imageType = "tif";
-            writeImage(document, imageType, outDir + file.getName() + "-bw-", ImageType.BINARY,
dpi);
+            writeImage(document, imageType, outDir + file.getName() + "-bw-", ImageType.BINARY,
dpi, 1, "");
             checkResolution(outDir + file.getName() + "-bw-1." + imageType, (int) dpi);
             checkTiffCompression(outDir + file.getName() + "-bw-1." + imageType, "CCITT T.6");
             checkFileTypeByContent(outDir + file.getName() + "-bw-1." + imageType, FileType.TIFF);
-            writeImage(document, imageType, outDir + file.getName() + "-co-", ImageType.RGB,
dpi);
-            checkResolution(outDir + file.getName() + "-co-1." + imageType, (int) dpi);
-            checkTiffCompression(outDir + file.getName() + "-co-1." + imageType, "LZW");
-            checkFileTypeByContent(outDir + file.getName() + "-co-1." + imageType, FileType.TIFF);
+
+            writeImage(document, imageType, outDir + file.getName() + "-coLZW-", ImageType.RGB,
dpi, 1, "");
+            checkResolution(outDir + file.getName() + "-coLZW-1." + imageType, (int) dpi);
+            checkTiffCompression(outDir + file.getName() + "-coLZW-1." + imageType, "LZW");
+            checkFileTypeByContent(outDir + file.getName() + "-coLZW-1." + imageType, FileType.TIFF);
+
+            writeImage(document, imageType, outDir + file.getName() + "-coJPEG-", ImageType.RGB,
dpi, 0.5f, "JPEG");
+            checkResolution(outDir + file.getName() + "-coJPEG-1." + imageType, (int) dpi);
+            checkTiffCompression(outDir + file.getName() + "-coJPEG-1." + imageType, "JPEG");
+            checkFileTypeByContent(outDir + file.getName() + "-coJPEG-1." + imageType, FileType.TIFF);
+
+            writeImage(document, imageType, outDir + file.getName() + "-coNone-", ImageType.RGB,
dpi, 1, null);
+            checkResolution(outDir + file.getName() + "-coNone-1." + imageType, (int) dpi);
+            checkTiffCompression(outDir + file.getName() + "-coNone-1." + imageType, "None");
+            checkFileTypeByContent(outDir + file.getName() + "-coNone-1." + imageType, FileType.TIFF);
         }
         finally
         {
@@ -236,16 +249,19 @@ public class TestImageIOUtils extends Te
     }
 
     private void writeImage(PDDocument document, String imageFormat, String outputPrefix,
-            ImageType imageType, float dpi) throws IOException
+            ImageType imageType, float dpi, float compressionQuality,
+            String compressionType) throws IOException
     {
         PDFRenderer renderer = new PDFRenderer(document);
         BufferedImage image = renderer.renderImageWithDPI(0, dpi, imageType);
         String fileName = outputPrefix + 1;
         LOG.info("Writing: " + fileName + "." + imageFormat);
         System.out.println("  " + fileName + "." + imageFormat); // for Maven (keep me!)
-        boolean res = ImageIOUtil.writeImage(image, fileName + "." + imageFormat, Math.round(dpi));
+        OutputStream os = new FileOutputStream(fileName + "." + imageFormat);
+        boolean res = ImageIOUtil.writeImage(image, imageFormat, os, Math.round(dpi), compressionQuality,
compressionType);
+        os.close();
         assertTrue("ImageIOUtil.writeImage() failed for file " + fileName, res);
-        if ("jpg".equals(imageFormat) || "gif".equals(imageFormat))
+        if ("jpg".equals(imageFormat) || "gif".equals(imageFormat) || "JPEG".equals(compressionType))
         {
             // jpeg is lossy, gif has 256 colors, 
             // so we can't check for content identity



Mime
View raw message