pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1798865 - /pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/ExtractImages.java
Date Thu, 15 Jun 2017 18:12:02 GMT
Author: tilman
Date: Thu Jun 15 18:12:01 2017
New Revision: 1798865

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

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

Modified: pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/ExtractImages.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/ExtractImages.java?rev=1798865&r1=1798864&r2=1798865&view=diff
==============================================================================
--- pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/ExtractImages.java (original)
+++ pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/ExtractImages.java Thu
Jun 15 18:12:01 2017
@@ -149,9 +149,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);
@@ -324,16 +324,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";
+        }
 
         FileOutputStream out = null;
         try
@@ -361,6 +366,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);



Mime
View raw message