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/06/15 18:11:56 UTC

svn commit: r1798864 - /pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExtractImages.java

Author: tilman
Date: Thu Jun 15 18:11:56 2017
New Revision: 1798864

URL: http://svn.apache.org/viewvc?rev=1798864&view=rev
Log:
PDFBOX-3829: favour direct saving of JPX, support directJPEG option for JPX too

Modified:
    pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExtractImages.java

Modified: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExtractImages.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExtractImages.java?rev=1798864&r1=1798863&r2=1798864&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExtractImages.java (original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExtractImages.java Thu Jun 15 18:11:56 2017
@@ -148,9 +148,9 @@ public final class ExtractImages
         String message = "Usage: java " + ExtractImages.class.getName() + " [options] <inputfile>\n"
                 + "\nOptions:\n"
                 + "  -password <password>   : Password to decrypt document\n"
-                + "  -prefix <image-prefix> : Image prefix(default to pdf name)\n"
-                + "  -directJPEG            : Forces the direct extraction of JPEG images "
-                + "regardless of colorspace\n"
+                + "  -prefix <image-prefix> : Image prefix (default to pdf name)\n"
+                + "  -directJPEG            : Forces the direct extraction of JPEG/JPX images "
+                + "                           regardless of colorspace or masking\n"
                 + "  <inputfile>            : The PDF document to use\n";
         
         System.err.println(message);
@@ -314,16 +314,21 @@ public final class ExtractImages
      * The suffix is automatically set depending on the image compression in the PDF.
      * @param pdImage the image.
      * @param prefix the filename prefix.
-     * @param directJPEG if true, force saving JPEG streams as they are in the PDF file. 
+     * @param directJPEG if true, force saving JPEG/JPX streams as they are in the PDF file. 
      * @throws IOException When something is wrong with the corresponding file.
      */
     private void write2file(PDImage pdImage, String filename, boolean directJPEG) throws IOException
     {
         String suffix = pdImage.getSuffix();
-        if (suffix == null || "jpx".equals(suffix) || "jb2".equals(suffix))
+        if (suffix == null || "jb2".equals(suffix))
         {
             suffix = "png";
         }
+        else if ("jpx".equals(suffix))
+        {
+            // use jp2 suffix for file because jpx not known by windows
+            suffix = "jp2";
+        }
 
         try (FileOutputStream out = new FileOutputStream(filename + "." + suffix))
         {
@@ -349,6 +354,26 @@ public final class ExtractImages
                         ImageIOUtil.writeImage(image, suffix, out);
                     }
                 }
+                else if ("jp2".equals(suffix))
+                {
+                    String colorSpaceName = pdImage.getColorSpace().getName();
+                    if (directJPEG || 
+                            !hasMasks(pdImage) && 
+                                     (PDDeviceGray.INSTANCE.getName().equals(colorSpaceName) ||
+                                      PDDeviceRGB.INSTANCE.getName().equals(colorSpaceName)))
+                    {
+                        // RGB or Gray colorspace: get and write the unmodified JPEG2000 stream
+                        InputStream data = pdImage.createInputStream(
+                                Arrays.asList(COSName.JPX_DECODE.getName()));
+                        IOUtils.copy(data, out);
+                        IOUtils.closeQuietly(data);
+                    }
+                    else
+                    {                        
+                        // for CMYK and other "unusual" colorspaces, the image will be converted
+                        ImageIOUtil.writeImage(image, "jpeg2000", out);
+                    }
+                }
                 else 
                 {
                     ImageIOUtil.writeImage(image, suffix, out);