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/11/23 16:58:31 UTC

svn commit: r1816171 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox: cos/COSDictionary.java pdmodel/graphics/image/PDImageXObject.java

Author: tilman
Date: Thu Nov 23 16:58:30 2017
New Revision: 1816171

URL: http://svn.apache.org/viewvc?rev=1816171&view=rev
Log:
PDFBOX-4022: cache colorspace indirect objects

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDictionary.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDictionary.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDictionary.java?rev=1816171&r1=1816170&r2=1816171&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDictionary.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDictionary.java Thu Nov 23 16:58:30 2017
@@ -1344,6 +1344,26 @@ public class COSDictionary extends COSBa
     }
 
     /**
+     * This is a special case of getItem that takes multiple keys, it will handle the situation
+     * where multiple keys could get the same value, ie if either CS or ColorSpace is used to get
+     * the colorspace. This will get an object from this dictionary.
+     *
+     * @param firstKey The first key to try.
+     * @param secondKey The second key to try.
+     *
+     * @return The object that matches the key.
+     */
+    public COSBase getItem(COSName firstKey, COSName secondKey)
+    {
+        COSBase retval = getItem(firstKey);
+        if (retval == null && secondKey != null)
+        {
+            retval = getItem(secondKey);
+        }
+        return retval;
+    }
+    
+    /**
      * Returns the names of the entries in this dictionary. The returned set is in the order the entries were added to
      * the dictionary.
      *

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java?rev=1816171&r1=1816170&r2=1816171&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java Thu Nov 23 16:58:30 2017
@@ -37,6 +37,7 @@ import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSInputStream;
 import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSObject;
 import org.apache.pdfbox.cos.COSStream;
 import org.apache.pdfbox.io.IOUtils;
 import org.apache.pdfbox.pdmodel.PDDocument;
@@ -585,10 +586,26 @@ public final class PDImageXObject extend
     {
         if (colorSpace == null)
         {
-            COSBase cosBase = getCOSObject().getDictionaryObject(COSName.COLORSPACE, COSName.CS);
+            COSBase cosBase = getCOSObject().getItem(COSName.COLORSPACE, COSName.CS);
             if (cosBase != null)
             {
+                COSObject indirect = null;
+                if (cosBase instanceof COSObject && resources.getResourceCache() != null)
+                {
+                    // PDFBOX-4022: use the resource cache because several images
+                    // might have the same colorspace indirect object.
+                    indirect = (COSObject) cosBase;
+                    colorSpace = resources.getResourceCache().getColorSpace(indirect);
+                    if (colorSpace != null)
+                    {
+                        return colorSpace;
+                    }
+                }
                 colorSpace = PDColorSpace.create(cosBase, resources);
+                if (indirect != null)
+                {
+                    resources.getResourceCache().put(indirect, colorSpace);
+                }
             }
             else if (isStencil())
             {