You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ja...@apache.org on 2015/06/19 04:27:50 UTC

svn commit: r1686346 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDDeviceCMYK.java

Author: jahewson
Date: Fri Jun 19 02:27:50 2015
New Revision: 1686346

URL: http://svn.apache.org/r1686346
Log:
PDFBOX-2184: Lazy load the CMYK ICC profile

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

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDDeviceCMYK.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDDeviceCMYK.java?rev=1686346&r1=1686345&r2=1686346&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDDeviceCMYK.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDDeviceCMYK.java Fri Jun 19 02:27:50 2015
@@ -40,33 +40,46 @@ public class PDDeviceCMYK extends PDDevi
     public static PDDeviceCMYK INSTANCE;
     static
     {
-        try
-        {
-            INSTANCE = new PDDeviceCMYK();
-        }
-        catch (IOException e)
-        {
-            throw new RuntimeException(e);
-        }
+        INSTANCE = new PDDeviceCMYK();
     }
 
     private final PDColor initialColor = new PDColor(new float[] { 0, 0, 0, 1 }, this);
-    private final ICC_ColorSpace awtColorSpace;
+    private volatile ICC_ColorSpace awtColorSpace;
+
+    protected PDDeviceCMYK()
+    {
+    }
 
-    protected PDDeviceCMYK() throws IOException
+    /**
+     * Lazy load the ICC profile, because it's slow.
+     */
+    protected void init() throws IOException
     {
-        // loads the ICC color profile for CMYK
-        ICC_Profile iccProfile = getICCProfile();
-        if (iccProfile == null)
+        // no need to synchronize this check as it is atomic
+        if (awtColorSpace != null)
         {
-            throw new IOException("Default CMYK color profile could not be loaded");
+            return;
+        }
+        synchronized (this)
+        {
+            // we might have been waiting for another thread, so check again
+            if (awtColorSpace != null)
+            {
+                return;
+            }
+            // loads the ICC color profile for CMYK
+            ICC_Profile iccProfile = getICCProfile();
+            if (iccProfile == null)
+            {
+                throw new IOException("Default CMYK color profile could not be loaded");
+            }
+            awtColorSpace = new ICC_ColorSpace(iccProfile);
+
+            // there is a JVM bug which results in a CMMException which appears to be a race
+            // condition caused by lazy initialization of the color transform, so we perform
+            // an initial color conversion while we're still in a static context, see PDFBOX-2184
+            awtColorSpace.toRGB(new float[] { 0, 0, 0, 0 });
         }
-        awtColorSpace = new ICC_ColorSpace(iccProfile);
-
-        // there is a JVM bug which results in a CMMException which appears to be a race
-        // condition caused by lazy initialization of the color transform, so we perform
-        // an initial color conversion while we're still in a static context, see PDFBOX-2184
-        awtColorSpace.toRGB(new float[] { 0, 0, 0, 0 });
     }
 
     protected ICC_Profile getICCProfile() throws IOException
@@ -116,14 +129,16 @@ public class PDDeviceCMYK extends PDDevi
     }
 
     @Override
-    public float[] toRGB(float[] value)
+    public float[] toRGB(float[] value) throws IOException
     {
+        init();
         return awtColorSpace.toRGB(value);
     }
 
     @Override
     public BufferedImage toRGBImage(WritableRaster raster) throws IOException
     {
+        init();
         return toRGBImageAWT(raster, awtColorSpace);
     }
 }