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 2020/02/12 19:42:37 UTC

svn commit: r1873959 - /pdfbox/branches/issue45/tools/src/main/java/org/apache/pdfbox/tools/ExtractImages.java

Author: tilman
Date: Wed Feb 12 19:42:37 2020
New Revision: 1873959

URL: http://svn.apache.org/viewvc?rev=1873959&view=rev
Log:
PDFBOX-4771: get the image only when needed, as suggested by Maruan Sahyoun

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

Modified: pdfbox/branches/issue45/tools/src/main/java/org/apache/pdfbox/tools/ExtractImages.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue45/tools/src/main/java/org/apache/pdfbox/tools/ExtractImages.java?rev=1873959&r1=1873958&r2=1873959&view=diff
==============================================================================
--- pdfbox/branches/issue45/tools/src/main/java/org/apache/pdfbox/tools/ExtractImages.java (original)
+++ pdfbox/branches/issue45/tools/src/main/java/org/apache/pdfbox/tools/ExtractImages.java Wed Feb 12 19:42:37 2020
@@ -382,12 +382,7 @@ public final class ExtractImages
             suffix = "jp2";
         }
 
-        BufferedImage image = pdImage.getImage();
-        if (image == null)
-        {
-            return;
-        }
-        if (image.getColorModel().hasAlpha())
+        if (hasMasks(pdImage))
         {
             // TIKA-3040, PDFBOX-4771: can't save ARGB as JPEG
             suffix = "png";
@@ -412,7 +407,11 @@ public final class ExtractImages
                 else
                 {
                     // for CMYK and other "unusual" colorspaces, the JPEG will be converted
-                    ImageIOUtil.writeImage(image, suffix, out);
+                    BufferedImage image = pdImage.getImage();
+                    if (image != null)
+                    {
+                        ImageIOUtil.writeImage(image, suffix, out);
+                    }
                 }
             }
             else if ("jp2".equals(suffix))
@@ -431,11 +430,20 @@ public final class ExtractImages
                 else
                 {                        
                     // for CMYK and other "unusual" colorspaces, the image will be converted
-                    ImageIOUtil.writeImage(image, "jpeg2000", out);
+                    BufferedImage image = pdImage.getImage();
+                    if (image != null)
+                    {
+                        ImageIOUtil.writeImage(image, "jpeg2000", out);
+                    }
                 }
             }
             else if ("tiff".equals(suffix) && pdImage.getColorSpace().equals(PDDeviceGray.INSTANCE))
             {
+                BufferedImage image = pdImage.getImage();
+                if (image == null)
+                {
+                    return;
+                }
                 // CCITT compressed images can have a different colorspace, but this one is B/W
                 // This is a bitonal image, so copy to TYPE_BYTE_BINARY
                 // so that a G4 compressed TIFF image is created by ImageIOUtil.writeImage()
@@ -452,9 +460,13 @@ public final class ExtractImages
                 }
                 ImageIOUtil.writeImage(bitonalImage, suffix, out);
             }
-            else 
+            else
             {
-                ImageIOUtil.writeImage(image, suffix, out);
+                BufferedImage image = pdImage.getImage();
+                if (image != null)
+                {
+                    ImageIOUtil.writeImage(image, suffix, out);
+                }
             }
             out.flush();
         }
@@ -466,4 +478,14 @@ public final class ExtractImages
             }
         }
     }
+
+    private boolean hasMasks(PDImage pdImage) throws IOException
+    {
+        if (pdImage instanceof PDImageXObject)
+        {
+            PDImageXObject ximg = (PDImageXObject) pdImage;
+            return ximg.getMask() != null || ximg.getSoftMask() != null;
+        }
+        return false;
+    }
 }