Author: tilman
Date: Mon Mar 12 06:44:23 2018
New Revision: 1826505

URL: http://svn.apache.org/viewvc?rev=1826505&view=rev
Log:
PDFBOX-4149: use cache when creating base colorspace in indexed colorspace

Modified:
    
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDColorSpace.java
    
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDIndexed.java

Modified: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDColorSpace.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDColorSpace.java?rev=1826505&r1=1826504&r2=1826505&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDColorSpace.java
 (original)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDColorSpace.java
 Mon Mar 12 06:44:23 2018
@@ -32,6 +32,7 @@ import java.awt.image.WritableRaster;
 import java.io.IOException;
 import java.awt.color.ColorSpace;
 import java.awt.image.ColorModel;
+import org.apache.pdfbox.pdmodel.ResourceCache;
 
 /**
  * A color space specifies how the colours of graphics objects will be painted 
on the page.
@@ -88,7 +89,7 @@ public abstract class PDColorSpace imple
     {
         if (colorSpace instanceof COSObject)
         {
-            return create(((COSObject) colorSpace).getObject(), resources);
+            return createFromCOSObject((COSObject) colorSpace, resources);
         }
         else if (colorSpace instanceof COSName)
         {
@@ -180,7 +181,7 @@ public abstract class PDColorSpace imple
             }
             else if (name == COSName.INDEXED)
             {
-                return new PDIndexed(array);
+                return new PDIndexed(array, resources);
             }
             else if (name == COSName.SEPARATION)
             {
@@ -223,6 +224,28 @@ public abstract class PDColorSpace imple
         }
     }
 
+    private static PDColorSpace createFromCOSObject(COSObject colorSpace, 
PDResources resources)
+            throws IOException
+    {
+        PDColorSpace cs;
+        if (resources != null && resources.getResourceCache() != null)
+        {
+            ResourceCache resourceCache = resources.getResourceCache();
+            cs = resourceCache.getColorSpace(colorSpace);
+            if (cs != null)
+            {
+                return cs;
+            }
+        }
+        cs = create(colorSpace.getObject(), resources);
+        if (resources != null && resources.getResourceCache() != null && cs != 
null)
+        {
+            ResourceCache resourceCache = resources.getResourceCache();
+            resourceCache.put(colorSpace, cs);
+        }
+        return cs;
+    }
+
     // array for the given parameters
     protected COSArray array;
 

Modified: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDIndexed.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDIndexed.java?rev=1826505&r1=1826504&r2=1826505&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDIndexed.java
 (original)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDIndexed.java
 Mon Mar 12 06:44:23 2018
@@ -30,6 +30,7 @@ import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.cos.COSNumber;
 import org.apache.pdfbox.cos.COSStream;
 import org.apache.pdfbox.cos.COSString;
+import org.apache.pdfbox.pdmodel.PDResources;
 import org.apache.pdfbox.pdmodel.common.PDStream;
 
 /**
@@ -65,13 +66,27 @@ public final class PDIndexed extends PDS
     }
 
     /**
-     * Creates a new Indexed color space from the given PDF array.
+     * Creates a new indexed color space from the given PDF array.
      * @param indexedArray the array containing the indexed parameters
+     * @throws java.io.IOException
      */
     public PDIndexed(COSArray indexedArray) throws IOException
     {
+        this(indexedArray, null);
+    }
+
+    /**
+     * Creates a new indexed color space from the given PDF array.
+     * @param indexedArray the array containing the indexed parameters
+     * @param resources the resources, can be null. Allows to use its cache 
for the colorspace.
+     * @throws java.io.IOException
+     */
+    public PDIndexed(COSArray indexedArray, PDResources resources) throws 
IOException
+    {
         array = indexedArray;
-        baseColorSpace = PDColorSpace.create(array.getObject(1));
+        // don't call getObject(1), we want to pass a reference if possible
+        // to profit of caching (PDFBOX-4149)
+        baseColorSpace = PDColorSpace.create(array.get(1), resources);
         readColorTable();
         initRgbColorTable();
     }


Reply via email to