You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2013/04/13 16:11:11 UTC

svn commit: r1467638 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox: pdmodel/graphics/color/ util/operator/

Author: lehmi
Date: Sat Apr 13 14:11:10 2013
New Revision: 1467638

URL: http://svn.apache.org/r1467638
Log:
PDFBOX-1565: added Lab colorspace support

Added:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/ColorSpaceLab.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetNonStrokingLabColor.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetStrokingLabColor.java
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDLab.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetNonStrokingColor.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetStrokingColor.java

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/ColorSpaceLab.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/ColorSpaceLab.java?rev=1467638&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/ColorSpaceLab.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/ColorSpaceLab.java Sat Apr 13 14:11:10 2013
@@ -0,0 +1,186 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.pdmodel.common.PDRange;
+
+/**
+ * This class represents a CalRGB color space.
+ * 
+ * The color conversion uses the algorithm described on wikipedia.
+ * 
+ * The blackpoint isn't used, as I can't find any hint how to do that.
+ * 
+ */
+public class ColorSpaceLab extends ColorSpace
+{
+    private static final long serialVersionUID = -5769360600770807798L;
+
+    private PDTristimulus whitepoint = null;
+    // TODO unused??
+    private PDTristimulus blackpoint = null;
+    private PDRange aRange = null;
+    private PDRange bRange = null;
+
+    /**
+     * Default Constructor.
+     * 
+     */
+    public ColorSpaceLab()
+    {
+        super(ColorSpace.TYPE_3CLR, 3);
+    }
+
+    /** 
+     * Constructor.
+     * 
+     * @param whitept whitepoint values
+     * @param blackpt blackpoint values
+     * @param a range for value a 
+     * @param b range for value b
+     */
+    public ColorSpaceLab(PDTristimulus whitept, PDTristimulus blackpt, PDRange a, PDRange b)
+    {
+        this();
+        whitepoint = whitept;
+        blackpoint = blackpt;
+        aRange = a;
+        bRange = b;
+    }
+
+    /**
+     * Clip the given value to the given range.
+     * 
+     * @param x
+     *            the given value to be clipped
+     * @param range
+     *            the range to be used to clip the value to
+     * 
+     * @return the clipped value
+     */
+    private float clipToRange(float x, PDRange range)
+    {
+        return Math.min(Math.max(x, range.getMin()), range.getMax());
+    }
+
+    private static final float VALUE_6_29 = 6 / 29;
+    private static final float VALUE_4_29 = 4 / 29;
+    private static final float VALUE_108_841 = 108 / 841;
+    private static final float VALUE_841_108 = 841 / 108;
+    private static final float VALUE_216_24389 = 216 / 24389;
+
+    private float calculateStage2ToXYZ(float value)
+    {
+        if (value >= VALUE_6_29)
+        {
+            return (float) Math.pow(value, 3);
+        }
+        else
+        {
+            return VALUE_108_841 * (value - VALUE_4_29);
+        }
+    }
+
+    private float calculateStage2FromXYZ(float value)
+    {
+        if (value >= VALUE_216_24389)
+        {
+            return (float) Math.pow(value, 1 / 3);
+        }
+        else
+        {
+            return VALUE_841_108 * value + VALUE_4_29;
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     */
+    public float[] toRGB(float[] colorvalue)
+    {
+        ColorSpace colorspaceXYZ = ColorSpace.getInstance(CS_CIEXYZ);
+        return colorspaceXYZ.toRGB(toCIEXYZ(colorvalue));
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     */
+    public float[] fromRGB(float[] rgbvalue)
+    {
+        ColorSpace colorspaceXYZ = ColorSpace.getInstance(CS_CIEXYZ);
+        return fromCIEXYZ(colorspaceXYZ.fromRGB(rgbvalue));
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     */
+    public float[] toCIEXYZ(float[] colorvalue)
+    {
+        float a = colorvalue[1];
+        if (aRange != null)
+        {
+            // clip the a value to the given range
+            a = clipToRange(a, aRange);
+        }
+        float b = colorvalue[2];
+        if (bRange != null)
+        {
+            // clip the b value to the given range
+            b = clipToRange(b, bRange);
+        }
+        float m = (colorvalue[0] + 16) / 116;
+        float l = m + (a / 500);
+        float n = m - (b / 200);
+
+        float x = whitepoint.getX() * calculateStage2ToXYZ(l);
+        float y = whitepoint.getY() * calculateStage2ToXYZ(m);
+        float z = whitepoint.getZ() * calculateStage2ToXYZ(n);
+        return new float[] { x, y, z };
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     */
+    public float[] fromCIEXYZ(float[] colorvalue)
+    {
+        float x = calculateStage2FromXYZ(colorvalue[0] / whitepoint.getX());
+        float y = calculateStage2FromXYZ(colorvalue[1] / whitepoint.getY());
+        float z = calculateStage2FromXYZ(colorvalue[2] / whitepoint.getZ());
+
+        float l = 116 * y - 116;
+        float a = 500 * (x - y);
+        float b = 200 * (y - z);
+        if (aRange != null)
+        {
+            // clip the a value to the given range
+            a = clipToRange(a, aRange);
+        }
+        if (bRange != null)
+        {
+            // clip the b value to the given range
+            b = clipToRange(b, bRange);
+        }
+        return new float[] { l, a, b };
+    }
+
+}

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=1467638&r1=1467637&r2=1467638&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 Sat Apr 13 14:11:10 2013
@@ -24,8 +24,11 @@ import org.apache.pdfbox.cos.COSName;
 
 import org.apache.pdfbox.pdmodel.common.PDRange;
 
+import java.awt.Transparency;
 import java.awt.color.ColorSpace;
 import java.awt.image.ColorModel;
+import java.awt.image.ComponentColorModel;
+import java.awt.image.DataBuffer;
 
 import java.io.IOException;
 
@@ -96,7 +99,7 @@ public class PDLab extends PDColorSpace
      */
     protected ColorSpace createColorSpace() throws IOException
     {
-        throw new IOException( "Not implemented" );
+        return new ColorSpaceLab(getWhitepoint(), getBlackPoint(), getARange(), getBRange());
     }
 
     /**
@@ -110,7 +113,13 @@ public class PDLab extends PDColorSpace
      */
     public ColorModel createColorModel( int bpc ) throws IOException
     {
-        throw new IOException( "Not implemented" );
+        int[] nBits = {bpc, bpc, bpc};
+        return new ComponentColorModel( getJavaColorSpace(),
+                   nBits,
+                   false,
+                   false,
+                   Transparency.OPAQUE,
+                   DataBuffer.TYPE_BYTE);
     }
 
     /**
@@ -122,8 +131,6 @@ public class PDLab extends PDColorSpace
      */
     public int getNumberOfComponents() throws IOException
     {
-        //BJL
-        //hmm is this correct, I am not 100% sure.
         return 3;
     }
 

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetNonStrokingColor.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetNonStrokingColor.java?rev=1467638&r1=1467637&r2=1467638&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetNonStrokingColor.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetNonStrokingColor.java Sat Apr 13 14:11:10 2013
@@ -29,6 +29,7 @@ import org.apache.pdfbox.pdmodel.graphic
 import org.apache.pdfbox.pdmodel.graphics.color.PDICCBased;
 import org.apache.pdfbox.pdmodel.graphics.color.PDCalRGB;
 import org.apache.pdfbox.pdmodel.graphics.color.PDIndexed;
+import org.apache.pdfbox.pdmodel.graphics.color.PDLab;
 import org.apache.pdfbox.pdmodel.graphics.color.PDPattern;
 import org.apache.pdfbox.pdmodel.graphics.color.PDSeparation;
 import org.apache.pdfbox.util.PDFOperator;
@@ -37,7 +38,6 @@ import java.io.IOException;
 /**
  * <p>Set the non stroking color space.</p>
  * 
- * @author <a href="mailto:andreas@lehmi.de">Andreas Lehmkühler</a>
  * @version $Revision: 1.0 $
  */
 public class SetNonStrokingColor extends OperatorProcessor 
@@ -46,7 +46,7 @@ public class SetNonStrokingColor extends
     /**
      * Log instance.
      */
-    private static final Log log = LogFactory.getLog(SetNonStrokingColor.class);
+    private static final Log LOG = LogFactory.getLog(SetNonStrokingColor.class);
 
     /**
      * sc,scn Set color space for non stroking operations.
@@ -96,6 +96,10 @@ public class SetNonStrokingColor extends
             {
                 newOperator = new SetNonStrokingIndexed();
             }
+            else if (colorSpace instanceof PDLab)
+            {
+                newOperator = new SetNonStrokingLabColor();
+            }
 
             if (newOperator != null) 
             {
@@ -104,13 +108,13 @@ public class SetNonStrokingColor extends
             }
             else 
             {
-                log.warn("Not supported colorspace "+colorSpace.getName() 
+                LOG.warn("Not supported colorspace "+colorSpace.getName() 
                         + " within operator "+operator.getOperation());
             }
         }
         else
         {
-            log.warn("Colorspace not found in "+getClass().getName()+".process!!");
+            LOG.warn("Colorspace not found in "+getClass().getName()+".process!!");
         }
     }
 }

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetNonStrokingLabColor.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetNonStrokingLabColor.java?rev=1467638&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetNonStrokingLabColor.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetNonStrokingLabColor.java Sat Apr 13 14:11:10 2013
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.util.operator;
+
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.cos.COSNumber;
+import org.apache.pdfbox.pdmodel.graphics.color.PDColorState;
+import org.apache.pdfbox.util.PDFOperator;
+
+/**
+ * Sets the non stroking color values for Lab colorspace.
+ * 
+ */
+public class SetNonStrokingLabColor extends OperatorProcessor
+{
+
+    /**
+     * {@inheritDoc}
+     * 
+     */
+    public void process(PDFOperator operator, List<COSBase> arguments) throws IOException
+    {
+        PDColorState colorInstance = context.getGraphicsState().getNonStrokingColor();
+        float[] values = new float[3];
+        for (int i = 0; i < arguments.size(); i++)
+        {
+            values[i] = ((COSNumber) arguments.get(i)).floatValue();
+        }
+        colorInstance.setColorSpaceValue(values);
+    }
+
+}

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetStrokingColor.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetStrokingColor.java?rev=1467638&r1=1467637&r2=1467638&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetStrokingColor.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetStrokingColor.java Sat Apr 13 14:11:10 2013
@@ -29,6 +29,7 @@ import org.apache.pdfbox.pdmodel.graphic
 import org.apache.pdfbox.pdmodel.graphics.color.PDICCBased;
 import org.apache.pdfbox.pdmodel.graphics.color.PDCalRGB;
 import org.apache.pdfbox.pdmodel.graphics.color.PDIndexed;
+import org.apache.pdfbox.pdmodel.graphics.color.PDLab;
 import org.apache.pdfbox.pdmodel.graphics.color.PDPattern;
 import org.apache.pdfbox.pdmodel.graphics.color.PDSeparation;
 import org.apache.pdfbox.util.PDFOperator;
@@ -37,7 +38,6 @@ import java.io.IOException;
 /**
  * <p>Set the stroking color space.</p>
  * 
- * @author <a href="mailto:andreas@lehmi.de">Andreas Lehmkühler</a>
  * @version $Revision: 1.0 $
  */
 public class SetStrokingColor extends OperatorProcessor 
@@ -46,7 +46,7 @@ public class SetStrokingColor extends Op
     /**
      * Log instance.
      */
-    private static final Log log = LogFactory.getLog(SetStrokingColor.class);
+    private static final Log LOG = LogFactory.getLog(SetStrokingColor.class);
 
     /**
      * SC,SCN Set color space for stroking operations.
@@ -96,6 +96,10 @@ public class SetStrokingColor extends Op
             {
                 newOperator = new SetStrokingIndexed();
             }
+            else if (colorSpace instanceof PDLab)
+            {
+                newOperator = new SetStrokingLabColor();
+            }
 
             if (newOperator != null) 
             {
@@ -104,13 +108,13 @@ public class SetStrokingColor extends Op
             }
             else
             {
-                log.info("Not supported colorspace "+colorSpace.getName() 
+                LOG.info("Not supported colorspace "+colorSpace.getName() 
                         + " within operator "+operator.getOperation());
             }
         }
         else 
         {
-            log.warn("Colorspace not found in "+getClass().getName()+".process!!");
+            LOG.warn("Colorspace not found in "+getClass().getName()+".process!!");
         }
     }
 }

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetStrokingLabColor.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetStrokingLabColor.java?rev=1467638&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetStrokingLabColor.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/SetStrokingLabColor.java Sat Apr 13 14:11:10 2013
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.util.operator;
+
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.cos.COSNumber;
+import org.apache.pdfbox.pdmodel.graphics.color.PDColorState;
+import org.apache.pdfbox.util.PDFOperator;
+
+/**
+ * Sets the stroking color values for Lab colorspace.  
+ * 
+ */
+public class SetStrokingLabColor extends OperatorProcessor 
+{
+
+    /**
+     * {@inheritDoc}
+     */
+    public void process(PDFOperator operator, List<COSBase> arguments) throws IOException
+    {
+        PDColorState color = context.getGraphicsState().getStrokingColor();
+        float[] values = new float[3];
+        for( int i=0; i<arguments.size(); i++ )
+        {
+            values[i] = ((COSNumber)arguments.get( i )).floatValue();
+        }
+        color.setColorSpaceValue( values );
+    }
+
+}