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 2014/12/15 22:22:53 UTC

svn commit: r1645762 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color: PDCIEBasedColorSpace.java PDCIEDictionaryBasedColorSpace.java PDCalGray.java PDCalRGB.java PDICCBased.java PDLab.java

Author: tilman
Date: Mon Dec 15 21:22:52 2014
New Revision: 1645762

URL: http://svn.apache.org/r1645762
Log:
PDFBOX-2570: refactoring to get rid of double code in PDCal* / PDLab; corrected gamma according to spec

Added:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCIEDictionaryBasedColorSpace.java   (with props)
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCIEBasedColorSpace.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCalGray.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCalRGB.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDICCBased.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDLab.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCIEBasedColorSpace.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCIEBasedColorSpace.java?rev=1645762&r1=1645761&r2=1645762&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCIEBasedColorSpace.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCIEBasedColorSpace.java Mon Dec 15 21:22:52 2014
@@ -16,8 +16,6 @@
  */
 package org.apache.pdfbox.pdmodel.graphics.color;
 
-
-import java.awt.color.ColorSpace;
 import java.awt.image.BufferedImage;
 import java.awt.image.WritableRaster;
 import java.io.IOException;
@@ -31,31 +29,6 @@ import java.io.IOException;
  */
 public abstract class PDCIEBasedColorSpace extends PDColorSpace
 {
-    private static final ColorSpace CIEXYZ = ColorSpace.getInstance(ColorSpace.CS_CIEXYZ);
-    
-    protected float[] convXYZtoRGB(float x, float y, float z)
-    {
-        // toRGB() malfunctions with negative values
-        // XYZ must be non-negative anyway:
-        // http://ninedegreesbelow.com/photography/icc-profile-negative-tristimulus.html
-        if (x < 0)
-        {
-            x = 0;
-        }
-        if (y < 0)
-        {
-            y = 0;
-        }
-        if (z < 0)
-        {
-            z = 0;
-        }
-        return CIEXYZ.toRGB(new float[]
-        {
-            x, y, z
-        });
-    }
-
     //
     // WARNING: this method is performance sensitive, modify with care!
     //

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCIEDictionaryBasedColorSpace.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCIEDictionaryBasedColorSpace.java?rev=1645762&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCIEDictionaryBasedColorSpace.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCIEDictionaryBasedColorSpace.java Mon Dec 15 21:22:52 2014
@@ -0,0 +1,169 @@
+/*
+ * Copyright 2014 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.pdmodel.graphics.color;
+
+import java.awt.color.ColorSpace;
+import org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSFloat;
+import org.apache.pdfbox.cos.COSName;
+
+/**
+ * CIE-based colour spaces that use a dictionary.
+ *
+ * @author Ben Litchfield
+ * @author John Hewson
+ */
+public abstract class PDCIEDictionaryBasedColorSpace extends PDCIEBasedColorSpace
+{
+    protected COSDictionary dictionary;
+
+    private static final ColorSpace CIEXYZ = ColorSpace.getInstance(ColorSpace.CS_CIEXYZ);
+
+    // we need to cache whitepoint values, because using getWhitePoint()
+    // would create a new default object for each pixel conversion if the original
+    // PDF didn't have a whitepoint array
+    protected float wpX = 1;
+    protected float wpY = 1;
+    protected float wpZ = 1;
+
+    protected PDCIEDictionaryBasedColorSpace(COSName cosName)
+    {
+        array = new COSArray();
+        dictionary = new COSDictionary();
+        array.add(cosName);
+        array.add(dictionary);
+
+        fillWhitepointCache(getWhitepoint());
+    }
+
+    /**
+     * Creates a new CalRGB color space using the given COS array.
+     *
+     * @param rgb the cos array which represents this color space
+     */
+    protected PDCIEDictionaryBasedColorSpace(COSArray rgb)
+    {
+        array = rgb;
+        dictionary = (COSDictionary) array.getObject(1);
+
+        fillWhitepointCache(getWhitepoint());
+    }
+
+    private void fillWhitepointCache(PDTristimulus whitepoint)
+    {
+        wpX = whitepoint.getX();
+        wpY = whitepoint.getY();
+        wpZ = whitepoint.getZ();
+    }
+
+    protected float[] convXYZtoRGB(float x, float y, float z)
+    {
+        // toRGB() malfunctions with negative values
+        // XYZ must be non-negative anyway:
+        // http://ninedegreesbelow.com/photography/icc-profile-negative-tristimulus.html
+        if (x < 0)
+        {
+            x = 0;
+        }
+        if (y < 0)
+        {
+            y = 0;
+        }
+        if (z < 0)
+        {
+            z = 0;
+        }
+        return CIEXYZ.toRGB(new float[]
+        {
+            x, y, z
+        });
+    }
+
+    /**
+     * This will return the whitepoint tristimulus. As this is a required field
+     * this will never return null. A default of 1,1,1 will be returned if the
+     * pdf does not have any values yet.
+     *
+     * @return the whitepoint tristimulus
+     */
+    public final PDTristimulus getWhitepoint()
+    {
+        COSArray wp = (COSArray) dictionary.getDictionaryObject(COSName.WHITE_POINT);
+        if (wp == null)
+        {
+            wp = new COSArray();
+            wp.add(new COSFloat(1.0f));
+            wp.add(new COSFloat(1.0f));
+            wp.add(new COSFloat(1.0f));
+        }
+        return new PDTristimulus(wp);
+    }
+
+    /**
+     * This will return the BlackPoint tristimulus. This is an optional field
+     * but has defaults so this will never return null. A default of 0,0,0 will
+     * be returned if the pdf does not have any values yet.
+     *
+     * @return the blackpoint tristimulus
+     */
+    public final PDTristimulus getBlackPoint()
+    {
+        COSArray bp = (COSArray) dictionary.getDictionaryObject(COSName.BLACK_POINT);
+        if (bp == null)
+        {
+            bp = new COSArray();
+            bp.add(new COSFloat(0.0f));
+            bp.add(new COSFloat(0.0f));
+            bp.add(new COSFloat(0.0f));
+        }
+        return new PDTristimulus(bp);
+    }
+
+    /**
+     * This will set the whitepoint tristimulus. As this is a required field
+     * this null should not be passed into this function.
+     *
+     * @param whitepoint the whitepoint tristimulus
+     */
+    public void setWhitePoint(PDTristimulus whitepoint)
+    {
+        COSBase wpArray = whitepoint.getCOSObject();
+        if (wpArray != null)
+        {
+            dictionary.setItem(COSName.WHITE_POINT, wpArray);
+        }
+        fillWhitepointCache(whitepoint);
+    }
+
+    /**
+     * This will set the BlackPoint tristimulus. As this is a required field
+     * this null should not be passed into this function.
+     *
+     * @param blackpoint the BlackPoint tristimulus
+     */
+    public void setBlackPoint(PDTristimulus blackpoint)
+    {
+        COSBase bpArray = null;
+        if (blackpoint != null)
+        {
+            bpArray = blackpoint.getCOSObject();
+        }
+        dictionary.setItem(COSName.BLACK_POINT, bpArray);
+    }
+
+}

Propchange: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCIEDictionaryBasedColorSpace.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCalGray.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCalGray.java?rev=1645762&r1=1645761&r2=1645762&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCalGray.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCalGray.java Mon Dec 15 21:22:52 2014
@@ -17,16 +17,18 @@
 package org.apache.pdfbox.pdmodel.graphics.color;
 
 import org.apache.pdfbox.cos.COSArray;
-import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSFloat;
 import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSNumber;
 
 /**
- * A CalGray colour space is a special case of a single-component CIE-based colour space.
+ * A CalGray colour space is a special case of a single-component CIE-based
+ * colour space.
  *
  * @author John Hewson
  * @author Ben Litchfield
  */
-public final class PDCalGray extends PDCalRGB
+public final class PDCalGray extends PDCIEDictionaryBasedColorSpace
 {
     private final PDColor initialColor = new PDColor(new float[] { 0 }, this);
 
@@ -35,20 +37,17 @@ public final class PDCalGray extends PDC
      */
     public PDCalGray()
     {
-        array = new COSArray();
-        dictionary = new COSDictionary();
-        array.add(COSName.CALGRAY);
-        array.add(dictionary);
+        super(COSName.CALGRAY);
     }
 
     /**
      * Creates a new CalGray color space using the given COS array.
+     *
      * @param array the COS array which represents this color space
      */
     public PDCalGray(COSArray array)
     {
-        this.array = array;
-        dictionary = (COSDictionary)array.getObject(1);
+        super(array);
     }
 
     @Override
@@ -76,8 +75,41 @@ public final class PDCalGray extends PDC
     }
 
     @Override
-    public final float[] toRGB(float[] value)
+    public float[] toRGB(float[] value)
     {
-        return super.toRGB(new float[] { value[0], value[0], value[0] });
+        float a = value[0];
+        float gamma = getGamma();
+        double powAG = Math.pow(a, gamma);
+        float x = (float) (wpX * powAG);
+        float y = (float) (wpY * powAG);
+        float z = (float) (wpZ * powAG);
+        return convXYZtoRGB(x, y, z);
+    }
+
+    /**
+     * This will get the gamma value. If none is present then the default of 1
+     * will be returned.
+     *
+     * @return The gamma value.
+     */
+    public float getGamma()
+    {
+        float retval = 1.0f;
+        COSNumber gamma = (COSNumber) dictionary.getDictionaryObject(COSName.GAMMA);
+        if (gamma != null)
+        {
+            retval = gamma.floatValue();
+        }
+        return retval;
+    }
+
+    /**
+     * Set the gamma value.
+     *
+     * @param value The new gamma value.
+     */
+    public void setGamma(float value)
+    {
+        dictionary.setItem(COSName.GAMMA, new COSFloat(value));
     }
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCalRGB.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCalRGB.java?rev=1645762&r1=1645761&r2=1645762&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCalRGB.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCalRGB.java Mon Dec 15 21:22:52 2014
@@ -17,8 +17,6 @@
 package org.apache.pdfbox.pdmodel.graphics.color;
 
 import org.apache.pdfbox.cos.COSArray;
-import org.apache.pdfbox.cos.COSBase;
-import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSFloat;
 import org.apache.pdfbox.cos.COSName;
 
@@ -31,9 +29,8 @@ import org.apache.pdfbox.util.Matrix;
  * @author Ben Litchfield
  * @author John Hewson
  */
-public class PDCalRGB extends PDCIEBasedColorSpace
+public class PDCalRGB extends PDCIEDictionaryBasedColorSpace
 {
-    protected COSDictionary dictionary;
     private final PDColor initialColor = new PDColor(new float[] { 0, 0, 0 }, this);
 
     /**
@@ -41,10 +38,7 @@ public class PDCalRGB extends PDCIEBased
      */
     public PDCalRGB()
     {
-        array = new COSArray();
-        dictionary = new COSDictionary();
-        array.add(COSName.CALRGB);
-        array.add(dictionary);
+        super(COSName.CALRGB);
     }
 
     /**
@@ -53,8 +47,7 @@ public class PDCalRGB extends PDCIEBased
      */
     public PDCalRGB(COSArray rgb)
     {
-        array = rgb;
-        dictionary = (COSDictionary)array.getObject(1);
+        super(rgb);
     }
 
     @Override
@@ -84,8 +77,7 @@ public class PDCalRGB extends PDCIEBased
     @Override
     public float[] toRGB(float[] value)
     {
-        PDTristimulus whitepoint = getWhitepoint();
-        if (whitepoint.getX() == 1 && whitepoint.getX() == 1 && whitepoint.getX() == 1)
+        if (wpX == 1 && wpY == 1 && wpZ == 1)
         {
             float a = value[0];
             float b = value[1];
@@ -111,44 +103,6 @@ public class PDCalRGB extends PDCIEBased
     }
 
     /**
-     * Returns the whitepoint tristimulus.
-     * A default of 1,1,1 will be returned if the PDF does not have any values yet.
-     * @return the whitepoint tristimulus.
-     */
-    public final PDTristimulus getWhitepoint()
-    {
-        COSArray wp = (COSArray)dictionary.getDictionaryObject(COSName.WHITE_POINT);
-        if(wp == null)
-        {
-            wp = new COSArray();
-            wp.add(new COSFloat(1.0f));
-            wp.add(new COSFloat(1.0f));
-            wp.add(new COSFloat(1.0f));
-            dictionary.setItem(COSName.WHITE_POINT, wp);
-        }
-        return new PDTristimulus(wp);
-    }
-
-    /**
-     * Returns the blackpoint tristimulus.
-     * A default of 0,0,0 will be returned if the PDF does not have any values yet.
-     * @return the blackpoint tristimulus
-     */
-    public final PDTristimulus getBlackPoint()
-    {
-        COSArray bp = (COSArray)dictionary.getDictionaryObject(COSName.BLACK_POINT);
-        if(bp == null)
-        {
-            bp = new COSArray();
-            bp.add(new COSFloat(0.0f));
-            bp.add(new COSFloat(0.0f));
-            bp.add(new COSFloat(0.0f));
-            dictionary.setItem(COSName.BLACK_POINT, bp);
-        }
-        return new PDTristimulus(bp);
-    }
-
-    /**
      * Returns the gamma value.
      * If none is present then the default of 1,1,1 will be returned.
      * @return the gamma value
@@ -186,33 +140,6 @@ public class PDCalRGB extends PDCIEBased
     }
 
     /**
-     * Sets the whitepoint tristimulus
-     * @param whitepoint the whitepoint tristimulus, which may not be null
-     */
-    public final void setWhitepoint(PDTristimulus whitepoint)
-    {
-        COSBase wpArray = whitepoint.getCOSObject();
-        if(wpArray != null)
-        {
-            dictionary.setItem(COSName.WHITE_POINT, wpArray);
-        }
-    }
-
-    /**
-     * Sets the blackpoint tristimulus
-     * @param blackpoint the blackpoint tristimulus, which may not be null
-     */
-    public final void setBlackPoint(PDTristimulus blackpoint)
-    {
-        COSBase bpArray = null;
-        if(blackpoint != null)
-        {
-            bpArray = blackpoint.getCOSObject();
-        }
-        dictionary.setItem(COSName.BLACK_POINT, bpArray);
-    }
-
-    /**
      * Sets the gamma value.
      * @param gamma the new gamma value
      */

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDICCBased.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDICCBased.java?rev=1645762&r1=1645761&r2=1645762&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDICCBased.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDICCBased.java Mon Dec 15 21:22:52 2014
@@ -41,7 +41,6 @@ import java.awt.image.WritableRaster;
 import java.io.InputStream;
 import java.io.IOException;
 
-import java.util.ArrayList;
 import java.util.List;
 
 /**

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDLab.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDLab.java?rev=1645762&r1=1645761&r2=1645762&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDLab.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDLab.java Mon Dec 15 21:22:52 2014
@@ -17,8 +17,6 @@
 package org.apache.pdfbox.pdmodel.graphics.color;
 
 import org.apache.pdfbox.cos.COSArray;
-import org.apache.pdfbox.cos.COSBase;
-import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSFloat;
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.pdmodel.common.PDRange;
@@ -32,27 +30,16 @@ import java.io.IOException;
  * @author Ben Litchfield
  * @author John Hewson
  */
-public final class PDLab extends PDCIEBasedColorSpace
+public final class PDLab extends PDCIEDictionaryBasedColorSpace
 {
-    private final COSDictionary dictionary;
     private PDColor initialColor;
     
-    // we need to cache whitepoint values, because using getWhitePoint()
-    // would create a new default object for each pixel conversion if the original
-    // PDF didn't have a whitepoint array
-    private float wpX = 1;
-    private float wpY = 1;
-    private float wpZ = 1;
-
     /**
      * Creates a new Lab color space.
      */
     public PDLab()
     {
-        array = new COSArray();
-        dictionary = new COSDictionary();
-        array.add(COSName.LAB);
-        array.add(dictionary);
+        super(COSName.LAB);
     }
 
     /**
@@ -61,16 +48,9 @@ public final class PDLab extends PDCIEBa
      */
     public PDLab(COSArray lab)
     {
-        array = lab;
-        dictionary = (COSDictionary)array.getObject(1);
-        
-        // init whitepoint cache
-        PDTristimulus whitepoint = getWhitepoint();
-        wpX = whitepoint.getX();
-        wpY = whitepoint.getY();
-        wpZ = whitepoint.getZ();
+        super(lab);
     }
-
+    
     @Override
     public String getName()
     {
@@ -186,44 +166,6 @@ public final class PDLab extends PDCIEBa
     }
 
     /**
-     * This will return the whitepoint tristimulus.
-     * As this is a required field this will never return null.
-     * A default of 1,1,1 will be returned if the pdf does not have any values yet.
-     * @return the whitepoint tristimulus
-     */
-    public PDTristimulus getWhitepoint()
-    {
-        COSArray wp = (COSArray)dictionary.getDictionaryObject(COSName.WHITE_POINT);
-        if(wp == null)
-        {
-            wp = new COSArray();
-            wp.add(new COSFloat(1.0f));
-            wp.add(new COSFloat(1.0f));
-            wp.add(new COSFloat(1.0f));
-        }
-        return new PDTristimulus(wp);
-    }
-
-    /**
-     * This will return the BlackPoint tristimulus.
-     * This is an optional field but has defaults so this will never return null.
-     * A default of 0,0,0 will be returned if the pdf does not have any values yet.
-     * @return the blackpoint tristimulus
-     */
-    public PDTristimulus getBlackPoint()
-    {
-        COSArray bp = (COSArray)dictionary.getDictionaryObject(COSName.BLACK_POINT);
-        if(bp == null)
-        {
-            bp = new COSArray();
-            bp.add(new COSFloat(0.0f));
-            bp.add(new COSFloat(0.0f));
-            bp.add(new COSFloat(0.0f));
-        }
-        return new PDTristimulus(bp);
-    }
-
-    /**
      * creates a range array with default values (-100..100 -100..100).
      * @return the new range array.
      */
@@ -268,40 +210,6 @@ public final class PDLab extends PDCIEBa
     }
 
     /**
-     * This will set the whitepoint tristimulus.
-     * As this is a required field this null should not be passed into this function.
-     * @param whitepoint the whitepoint tristimulus
-     */
-    public void setWhitePoint(PDTristimulus whitepoint)
-    {
-        COSBase wpArray = whitepoint.getCOSObject();
-        if(wpArray != null)
-        {
-            dictionary.setItem(COSName.WHITE_POINT, wpArray);
-        }
-        
-        // update cached values
-        wpX = whitepoint.getX();
-        wpY = whitepoint.getY();
-        wpZ = whitepoint.getZ();
-    }
-
-    /**
-     * This will set the BlackPoint tristimulus.
-     * As this is a required field this null should not be passed into this function.
-     * @param blackpoint the BlackPoint tristimulus
-     */
-    public void setBlackPoint(PDTristimulus blackpoint)
-    {
-        COSBase bpArray = null;
-        if(blackpoint != null)
-        {
-            bpArray = blackpoint.getCOSObject();
-        }
-        dictionary.setItem(COSName.BLACK_POINT, bpArray);
-    }
-
-    /**
      * This will set the a range for the "a" component.
      * @param range the new range for the "a" component, 
      * or null if defaults (-100..100) are to be set.