pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1868837 - /pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/LosslessFactory.java
Date Thu, 24 Oct 2019 02:49:30 GMT
Author: tilman
Date: Thu Oct 24 02:49:30 2019
New Revision: 1868837

URL: http://svn.apache.org/viewvc?rev=1868837&view=rev
Log:
PDFBOX-4672: refactor

Modified:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/LosslessFactory.java

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/LosslessFactory.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/LosslessFactory.java?rev=1868837&r1=1868836&r2=1868837&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/LosslessFactory.java
(original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/LosslessFactory.java
Thu Oct 24 02:49:30 2019
@@ -79,46 +79,60 @@ public final class LosslessFactory
     public static PDImageXObject createFromImage(PDDocument document, BufferedImage image)
             throws IOException
     {
-        if (image.getTransparency() == BufferedImage.OPAQUE &&
-                ((image.getType() == BufferedImage.TYPE_BYTE_GRAY && image.getColorModel().getPixelSize()
<= 8)
-                || (image.getType() == BufferedImage.TYPE_BYTE_BINARY && image.getColorModel().getPixelSize()
== 1)))
+        if (isGrayImage(image))
         {
             return createFromGrayImage(image, document);
         }
-        else
+
+        // We try to encode the image with predictor
+        if (usePredictorEncoder)
         {
-            // We try to encode the image with predictor
-            if (usePredictorEncoder)
+            PDImageXObject pdImageXObject = new PredictorEncoder(document, image).encode();
+            if (pdImageXObject != null)
             {
-                PDImageXObject pdImageXObject = new PredictorEncoder(document, image).encode();
-                if (pdImageXObject != null)
+                if (pdImageXObject.getColorSpace() == PDDeviceRGB.INSTANCE &&
+                    pdImageXObject.getBitsPerComponent() < 16 &&
+                    image.getWidth() * image.getHeight() <= 50 * 50)
                 {
-                    if (pdImageXObject.getColorSpace() == PDDeviceRGB.INSTANCE &&
-                        pdImageXObject.getBitsPerComponent() < 16 &&
-                        image.getWidth() * image.getHeight() <= 50 * 50)
+                    // also create classic compressed image, compare sizes
+                    PDImageXObject pdImageXObjectClassic = createFromRGBImage(image, document);
+                    if (pdImageXObjectClassic.getCOSObject().getLength() < 
+                        pdImageXObject.getCOSObject().getLength())
+                    {
+                        pdImageXObject.getCOSObject().close();
+                        return pdImageXObjectClassic;
+                    }
+                    else
                     {
-                        // also create classic compressed image, compare sizes
-                        PDImageXObject pdImageXObjectClassic = createFromRGBImage(image,
document);
-                        if (pdImageXObjectClassic.getCOSObject().getLength() < 
-                            pdImageXObject.getCOSObject().getLength())
-                        {
-                            pdImageXObject.getCOSObject().close();
-                            return pdImageXObjectClassic;
-                        }
-                        else
-                        {
-                            pdImageXObjectClassic.getCOSObject().close();
-                        }
+                        pdImageXObjectClassic.getCOSObject().close();
                     }
-                    return pdImageXObject;
                 }
+                return pdImageXObject;
             }
+        }
 
-            // Fallback: We export the image as 8-bit sRGB and might loose color information
-            return createFromRGBImage(image, document);
+        // Fallback: We export the image as 8-bit sRGB and might loose color information
+        return createFromRGBImage(image, document);
+    }
+
+    private static boolean isGrayImage(BufferedImage image)
+    {
+        if (image.getTransparency() != BufferedImage.OPAQUE)
+        {
+            return false;
+        }
+        if (image.getType() == BufferedImage.TYPE_BYTE_GRAY && image.getColorModel().getPixelSize()
<= 8)
+        {
+            return true;
         }
+        if (image.getType() == BufferedImage.TYPE_BYTE_BINARY && image.getColorModel().getPixelSize()
== 1)
+        {
+            return true;
+        }
+        return false;
     }
 
+
     // grayscale images need one color per sample
     private static PDImageXObject createFromGrayImage(BufferedImage image, PDDocument document)
             throws IOException



Mime
View raw message