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 2014/02/20 07:31:05 UTC

svn commit: r1570086 [2/7] - in /pdfbox/trunk: examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ examples/src/main/java/org/apache/pdfbox/examples/util/ pdfbox/src/main/java/org/apache/pdfbox/cos/ pdfbox/src/main/java/org/apache/pdfbox/filter/...

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType2.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType2.java?rev=1570086&r1=1570085&r2=1570086&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType2.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType2.java Thu Feb 20 06:31:01 2014
@@ -25,7 +25,7 @@ import java.io.IOException;
 import java.lang.Math;
 
 /**
- * This class represents a type 2 function in a PDF document.
+ * This class represents a Type 2 (exponential interpolation) function in a PDF document.
  *
  * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
  * @version $Revision: 1.2 $
@@ -61,27 +61,27 @@ public class PDFunctionType2 extends PDF
     }
 
     /**
+     * Performs exponential interpolation
+     *
     * {@inheritDoc}
     */
     public float[] eval(float[] input) throws IOException
     {
-        //This function performs exponential interpolation.
-        //It uses only a single value as its input, but may produce a multi-valued output.
-        //See PDF Reference section 3.9.2.
-                
-        double inputValue = input[0];
-        double exponent = getN();
         COSArray c0 = getC0();
         COSArray c1 = getC1();
-        int c0Size = c0.size();
-        float[] functionResult = new float[c0Size];
-        for (int j=0;j<c0Size;j++)
+
+        // exponential interpolation
+        float xToN = (float)Math.pow(input[0], getN()); // x^N
+
+        float[] result = new float[c0.size()];
+        for (int j = 0; j < result.length; j++)
         {
-            //y[j] = C0[j] + x^N*(C1[j] - C0[j])
-            functionResult[j] = ((COSNumber)c0.get(j)).floatValue() + (float)Math.pow(inputValue,exponent)*(((COSNumber)c1.get(j)).floatValue() - ((COSNumber)c0.get(j)).floatValue());
+            float C0j = ((COSNumber)c0.get(j)).floatValue();
+            float C1j = ((COSNumber)c1.get(j)).floatValue();
+            result[j] = C0j + xToN * (C1j - C0j);
         }
-        // clip to range if available
-        return clipToRange(functionResult);
+
+        return clipToRange(result);
     }
     
     /**
@@ -130,4 +130,14 @@ public class PDFunctionType2 extends PDF
     {
         return getDictionary().getFloat(COSName.N);
     }
+
+    /**
+     * {@inheritDoc}
+     */
+    public String toString()
+    {
+        return "FunctionType2{" +
+                "C0:" + getC0() + " " +
+                "C1:" + getC1() + "}";
+    }
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType3.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType3.java?rev=1570086&r1=1570085&r2=1570086&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType3.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType3.java Thu Feb 20 06:31:01 2014
@@ -24,7 +24,7 @@ import org.apache.pdfbox.pdmodel.common.
 import java.io.IOException;
 
 /**
- * This class represents a type 3 function in a PDF document.
+ * This class represents a Type 3 (stitching) function in a PDF document.
  *
  * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
  * @version $Revision: 1.2 $

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType4.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType4.java?rev=1570086&r1=1570085&r2=1570086&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType4.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType4.java Thu Feb 20 06:31:01 2014
@@ -26,7 +26,7 @@ import org.apache.pdfbox.pdmodel.common.
 import java.io.IOException;
 
 /**
- * This class represents a type 4 function in a PDF document.
+ * This class represents a Type 4 (PostScript calculator) function in a PDF document.
  * <p>
  * See section 3.9.4 of the PDF 1.4 Reference.
  *

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDObjectReference.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDObjectReference.java?rev=1570086&r1=1570085&r2=1570086&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDObjectReference.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDObjectReference.java Thu Feb 20 06:31:01 2014
@@ -22,7 +22,7 @@ import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.pdmodel.common.COSObjectable;
-import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObject;
+import org.apache.pdfbox.pdmodel.graphics.PDXObject;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationUnknown;
 
@@ -96,7 +96,7 @@ public class PDObjectReference implement
         }
         try
         {
-            PDXObject xobject = PDXObject.createXObject(obj);
+            PDXObject xobject = PDXObject.createXObject(obj, null, null); // <-- TODO: valid?
             if (xobject != null)
             {
                 return xobject;

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDStructureNode.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDStructureNode.java?rev=1570086&r1=1570085&r2=1570086&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDStructureNode.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDStructureNode.java Thu Feb 20 06:31:01 2014
@@ -28,7 +28,7 @@ import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.cos.COSObject;
 import org.apache.pdfbox.pdmodel.common.COSArrayList;
 import org.apache.pdfbox.pdmodel.common.COSObjectable;
-import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObject;
+import org.apache.pdfbox.pdmodel.graphics.PDXObject;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
 
 /**

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/markedcontent/PDMarkedContent.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/markedcontent/PDMarkedContent.java?rev=1570086&r1=1570085&r2=1570086&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/markedcontent/PDMarkedContent.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/markedcontent/PDMarkedContent.java Thu Feb 20 06:31:01 2014
@@ -22,7 +22,7 @@ import java.util.List;
 import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.pdmodel.documentinterchange.taggedpdf.PDArtifactMarkedContent;
-import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObject;
+import org.apache.pdfbox.pdmodel.graphics.PDXObject;
 import org.apache.pdfbox.util.TextPosition;
 
 /**

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/prepress/PDBoxStyle.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/prepress/PDBoxStyle.java?rev=1570086&r1=1570085&r2=1570086&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/prepress/PDBoxStyle.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/prepress/PDBoxStyle.java Thu Feb 20 06:31:01 2014
@@ -22,8 +22,7 @@ import org.apache.pdfbox.cos.COSDictiona
 import org.apache.pdfbox.cos.COSInteger;
 import org.apache.pdfbox.pdmodel.common.COSObjectable;
 import org.apache.pdfbox.pdmodel.graphics.PDLineDashPattern;
-import org.apache.pdfbox.pdmodel.graphics.color.PDColorState;
-import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
+import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
 
 /**
  * The Box Style specifies visual characteristics for displaying box areas.
@@ -84,13 +83,12 @@ public class PDBoxStyle implements COSOb
     }
 
     /**
-     * Get the color to be used for the guidelines.  This is guaranteed to
-     * not return null.  The color space will always be DeviceRGB and the
-     * default color is [0,0,0].
+     * Get the RGB color to be used for the guidelines.  This is guaranteed to
+     * not return null. The default color is [0,0,0].
      *
      *@return The guideline color.
      */
-    public PDColorState getGuidelineColor()
+    public PDColor getGuidelineColor()
     {
         COSArray colorValues = (COSArray)dictionary.getDictionaryObject( "C" );
         if( colorValues == null )
@@ -101,9 +99,8 @@ public class PDBoxStyle implements COSOb
             colorValues.add( COSInteger.ZERO );
             dictionary.setItem( "C", colorValues );
         }
-        PDColorState instance = new PDColorState( colorValues );
-        instance.setColorSpace( PDDeviceRGB.INSTANCE );
-        return instance;
+        PDColor color = new PDColor(colorValues.toFloatArray());
+        return color;
     }
 
     /**
@@ -112,12 +109,12 @@ public class PDBoxStyle implements COSOb
      *
      * @param color The new colorspace value.
      */
-    public void setGuideLineColor( PDColorState color )
+    public void setGuideLineColor( PDColor color )
     {
         COSArray values = null;
         if( color != null )
         {
-            values = color.getCOSColorSpaceValue();
+            values = color.toCOSArray();
         }
         dictionary.setItem( "C", values );
     }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/edit/PDPageContentStream.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/edit/PDPageContentStream.java?rev=1570086&r1=1570085&r2=1570086&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/edit/PDPageContentStream.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/edit/PDPageContentStream.java Thu Feb 20 06:31:01 2014
@@ -44,13 +44,12 @@ import org.apache.pdfbox.pdmodel.font.PD
 import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace;
 import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceCMYK;
 import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceGray;
-import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceN;
 import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
 import org.apache.pdfbox.pdmodel.graphics.color.PDICCBased;
 import org.apache.pdfbox.pdmodel.graphics.color.PDPattern;
 import org.apache.pdfbox.pdmodel.graphics.color.PDSeparation;
-import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObject;
-import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
+import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
+import org.apache.pdfbox.pdmodel.graphics.PDXObject;
 
 /**
  * This class is a convenience for creating page content streams.  You MUST
@@ -70,8 +69,8 @@ public class PDPageContentStream
     private boolean inTextMode = false;
     private PDResources resources;
 
-    private PDColorSpace currentStrokingColorSpace = new PDDeviceGray();
-    private PDColorSpace currentNonStrokingColorSpace = new PDDeviceGray();
+    private PDColorSpace currentStrokingColorSpace = PDDeviceGray.INSTANCE;
+    private PDColorSpace currentNonStrokingColorSpace = PDDeviceGray.INSTANCE;
 
     // cached storage component for getting color values
     private float[] colorComponents = new float[4];
@@ -334,7 +333,7 @@ public class PDPageContentStream
      *
      * @throws IOException If there is an error writing to the stream.
      */
-    public void drawImage(PDXObjectImage image, float x, float y) throws IOException
+    public void drawImage(PDImageXObject image, float x, float y) throws IOException
     {
         drawXObject(image, x, y, image.getWidth(), image.getHeight());
     }
@@ -371,7 +370,7 @@ public class PDPageContentStream
             throw new IOException("Error: drawXObject is not allowed within a text block.");
         }
         String xObjectPrefix = null;
-        if (xobject instanceof PDXObjectImage)
+        if (xobject instanceof PDImageXObject)
         {
             xObjectPrefix = "Im";
         }
@@ -635,8 +634,9 @@ public class PDPageContentStream
             appendRawCommands(components[i]);
             appendRawCommands(SPACE);
         }
-        if (currentStrokingColorSpace instanceof PDSeparation || currentStrokingColorSpace instanceof PDPattern
-                || currentStrokingColorSpace instanceof PDDeviceN || currentStrokingColorSpace instanceof PDICCBased)
+        if (currentStrokingColorSpace instanceof PDSeparation ||
+            currentStrokingColorSpace instanceof PDPattern ||
+            currentStrokingColorSpace instanceof PDICCBased)
         {
             appendRawCommands(SET_STROKING_COLOR_COMPLEX);
         }
@@ -647,7 +647,7 @@ public class PDPageContentStream
     }
 
     /**
-     * Set the stroking color, specified as RGB.
+     * Set the stroking color, specified as RGB or Gray.
      *
      * @param color The color to set.
      * @throws IOException If an IO error occurs while writing to the stream.
@@ -664,19 +664,14 @@ public class PDPageContentStream
             color.getColorComponents(colorComponents);
             setStrokingColor(colorComponents[0]);
         }
-        else if (colorSpace.getType() == ColorSpace.TYPE_CMYK)
-        {
-            color.getColorComponents(colorComponents);
-            setStrokingColor(colorComponents[0], colorComponents[1], colorComponents[2], colorComponents[3]);
-        }
         else
         {
-            throw new IOException("Error: unknown colorspace:" + colorSpace);
+            throw new IOException("Error: unknown color space:" + colorSpace);
         }
     }
 
     /**
-     * Set the non stroking color, specified as RGB.
+     * Set the non stroking color, specified as RGB or Gray.
      *
      * @param color The color to set.
      * @throws IOException If an IO error occurs while writing to the stream.
@@ -693,14 +688,9 @@ public class PDPageContentStream
             color.getColorComponents(colorComponents);
             setNonStrokingColor(colorComponents[0]);
         }
-        else if (colorSpace.getType() == ColorSpace.TYPE_CMYK)
-        {
-            color.getColorComponents(colorComponents);
-            setNonStrokingColor(colorComponents[0], colorComponents[1], colorComponents[2], colorComponents[3]);
-        }
         else
         {
-            throw new IOException("Error: unknown colorspace:" + colorSpace);
+            throw new IOException("Error: unknown color space:" + colorSpace);
         }
     }
 
@@ -806,9 +796,9 @@ public class PDPageContentStream
             appendRawCommands(components[i]);
             appendRawCommands(SPACE);
         }
-        if (currentNonStrokingColorSpace instanceof PDSeparation || currentNonStrokingColorSpace instanceof PDPattern
-                || currentNonStrokingColorSpace instanceof PDDeviceN
-                || currentNonStrokingColorSpace instanceof PDICCBased)
+        if (currentNonStrokingColorSpace instanceof PDSeparation ||
+            currentNonStrokingColorSpace instanceof PDPattern ||
+            currentNonStrokingColorSpace instanceof PDICCBased)
         {
             appendRawCommands(SET_NON_STROKING_COLOR_COMPLEX);
         }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDGraphicsState.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDGraphicsState.java?rev=1570086&r1=1570085&r2=1570086&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDGraphicsState.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDGraphicsState.java Thu Feb 20 06:31:01 2014
@@ -18,9 +18,12 @@ package org.apache.pdfbox.pdmodel.graphi
 
 import java.awt.*;
 import java.awt.geom.GeneralPath;
+import java.io.IOException;
 
 import org.apache.pdfbox.pdmodel.common.PDRectangle;
-import org.apache.pdfbox.pdmodel.graphics.color.PDColorState;
+import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
+import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace;
+import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceGray;
 import org.apache.pdfbox.pdmodel.text.PDTextState;
 import org.apache.pdfbox.util.Matrix;
 
@@ -38,10 +41,12 @@ public class PDGraphicsState implements 
     //Here are some attributes of the Graphics state, but have not been created yet.
     //
     //clippingPath
-    private PDColorState strokingColor = new PDColorState();
-    private PDColorState nonStrokingColor = new PDColorState();
+    private PDColor strokingColor = PDColor.DEVICE_GRAY_BLACK;
+    private PDColor nonStrokingColor = PDColor.DEVICE_GRAY_BLACK;
+    private PDColorSpace strokingColorSpace = PDDeviceGray.INSTANCE;
+    private PDColorSpace nonStrokingColorSpace = PDDeviceGray.INSTANCE;
     private PDTextState textState = new PDTextState();
-    private double lineWidth = 0;
+    private double lineWidth = 0.0;
     private int lineCap = 0;
     private int lineJoin = 0;
     private double miterLimit = 0;
@@ -421,8 +426,8 @@ public class PDGraphicsState implements 
             clone = (PDGraphicsState)super.clone();
             clone.setTextState( (PDTextState)textState.clone() );
             clone.setCurrentTransformationMatrix( currentTransformationMatrix.copy() );
-            clone.strokingColor = (PDColorState)strokingColor.clone();
-            clone.nonStrokingColor = ((PDColorState)nonStrokingColor.clone());
+            clone.strokingColor = strokingColor; // immutable
+            clone.nonStrokingColor = nonStrokingColor; // immutable
             if( lineDashPattern != null )
             {
                 clone.setLineDashPattern( (PDLineDashPattern)lineDashPattern.clone() );
@@ -440,26 +445,86 @@ public class PDGraphicsState implements 
     }
 
     /**
-     * Returns the stroking color state.
+     * Returns the stroking color.
      *
-     * @return stroking color state
+     * @return stroking color
      */
-    public PDColorState getStrokingColor()
+    public PDColor getStrokingColor()
     {
         return strokingColor;
     }
 
     /**
-     * Returns the non-stroking color state.
+     * Sets the stroking color.
      *
-     * @return non-stroking color state
+     * @param color The new stroking color
      */
-    public PDColorState getNonStrokingColor()
+    public void setStrokingColor(PDColor color)
+    {
+        strokingColor = color;
+    }
+
+    /**
+     * Returns the non-stroking color.
+     *
+     * @return The non-stroking color
+     */
+    public PDColor getNonStrokingColor()
     {
         return nonStrokingColor;
     }
 
     /**
+     * Sets the non-stroking color.
+     *
+     * @param color The new non-stroking color
+     */
+    public void setNonStrokingColor(PDColor color)
+    {
+        nonStrokingColor = color;
+    }
+
+    /**
+     * Returns the stroking color space.
+     *
+     * @return The stroking color space.
+     */
+    public PDColorSpace getStrokingColorSpace()
+    {
+        return strokingColorSpace;
+    }
+
+    /**
+     * Sets the the stroking color space.
+     *
+     * @param colorSpace The new stroking color space.
+     */
+    public void setStrokingColorSpace(PDColorSpace colorSpace)
+    {
+        strokingColorSpace = colorSpace;
+    }
+
+    /**
+     * Returns the non-stroking color space.
+     *
+     * @return The non-stroking color space.
+     */
+    public PDColorSpace getNonStrokingColorSpace()
+    {
+        return nonStrokingColorSpace;
+    }
+
+    /**
+     * Sets the the non-stroking color space.
+     *
+     * @param colorSpace The new non-stroking color space.
+     */
+    public void setNonStrokingColorSpace(PDColorSpace colorSpace)
+    {
+        nonStrokingColorSpace = colorSpace;
+    }
+
+    /**
      * This will set the current clipping path.
      *
      * @param pCurrentClippingPath The current clipping path.

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDPostScriptXObject.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDPostScriptXObject.java?rev=1570086&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDPostScriptXObject.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDPostScriptXObject.java Thu Feb 20 06:31:01 2014
@@ -0,0 +1,38 @@
+/*
+ * 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;
+
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.pdmodel.common.PDStream;
+
+/**
+ * A PostScript XObject.
+ * Conforming readers may not be able to interpret the PostScript fragments.
+ *
+ * @author John Hewson
+ */
+public class PDPostScriptXObject extends PDXObject
+{
+    /**
+     * Creates a PostScript XObject.
+     * @param stream The XObject stream
+     */
+    public PDPostScriptXObject(PDStream stream)
+    {
+        super(stream, COSName.PS);
+    }
+}

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDShading.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDShading.java?rev=1570086&r1=1570085&r2=1570086&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDShading.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDShading.java Thu Feb 20 06:31:01 2014
@@ -23,16 +23,13 @@ import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSFloat;
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.cos.COSDictionary;
-import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpaceFactory;
 import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace;
 import org.apache.pdfbox.pdmodel.common.function.PDFunction;
 
-
-
 import java.io.IOException;
 
 /**
- * This class represents a Shading Pattern color space.
+ * This class represents a Shading Pattern.
  *  See section 4.6.3 of the PDF 1.7 specification.
  *
  * @author <a href="mailto:Daniel.Wilson@BlackLocustSoftware.com">Daniel wilson</a>
@@ -45,7 +42,7 @@ public class PDShading implements COSObj
     private COSArray domain = null;
     private COSArray extend = null;
     private PDFunction function = null;
-    private PDColorSpace colorspace = null;
+    private PDColorSpace colorSpace = null;
     
     /**
      * The name of this object.
@@ -121,11 +118,11 @@ public class PDShading implements COSObj
     */
     public PDColorSpace getColorSpace() throws IOException
     {
-        if (colorspace == null)
+        if (colorSpace == null)
         {
-            colorspace = PDColorSpaceFactory.createColorSpace(DictShading.getDictionaryObject(COSName.COLORSPACE));
+            colorSpace = PDColorSpace.create(DictShading.getDictionaryObject(COSName.COLORSPACE));
         }
-        return colorspace;
+        return colorSpace;
     }
     
     /**

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDXObject.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDXObject.java?rev=1570086&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDXObject.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDXObject.java Thu Feb 20 06:31:01 2014
@@ -0,0 +1,131 @@
+/*
+ * 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;
+
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSStream;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDResources;
+import org.apache.pdfbox.pdmodel.common.COSObjectable;
+import org.apache.pdfbox.pdmodel.common.PDStream;
+import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
+import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
+
+import java.io.IOException;
+
+/**
+ * An external object, or "XObject".
+ *
+ * @author Ben Litchfield
+ * @author John Hewson
+ */
+public class PDXObject implements COSObjectable
+{
+    private PDStream stream;
+
+    /**
+     * Creates a new XObject instance of the appropriate type for the COS stream.
+     * @param base The stream which is wrapped by this XObject.
+     * @return A new XObject instance.
+     */
+    public static PDXObject createXObject(COSBase base, String name, PDResources resources)
+            throws IOException
+    {
+        if (base == null)
+        {
+            // TODO throw an exception?
+            return null;
+        }
+
+        if (!(base instanceof COSStream))
+        {
+            throw new IOException("Unexpected object type: " + base.getClass().getName());
+        }
+
+        COSStream stream = (COSStream)base;
+        String subtype = stream.getNameAsString(COSName.SUBTYPE);
+
+        if (COSName.IMAGE.getName().equals(subtype))
+        {
+            return new PDImageXObject(new PDStream(stream), resources.getColorSpaces());
+        }
+        else if (COSName.FORM.getName().equals(subtype))
+        {
+            return new PDFormXObject(new PDStream(stream), name);
+        }
+        else if (COSName.PS.getName().equals(subtype))
+        {
+            return new PDPostScriptXObject(new PDStream(stream));
+        }
+        else
+        {
+            throw new IOException("Invalid XObject Subtype: " + subtype);
+        }
+    }
+
+    /**
+     * Creates a new XObject from the given stream and subtype.
+     * @param stream The stream to read.
+     */
+    protected PDXObject(PDStream stream, COSName subtype)
+    {
+        this.stream = stream;
+        // could be used for writing:
+        stream.getStream().setName(COSName.TYPE, COSName.XOBJECT.getName());
+        stream.getStream().setName(COSName.SUBTYPE, subtype.getName());
+    }
+
+    /**
+     * Creates a new XObject of the given subtype for writing.
+     * @param document The document in which to create the XObject.
+     * @param subtype The subtype of the new XObject.
+     */
+    protected PDXObject(PDDocument document, COSName subtype)
+    {
+        stream = new PDStream(document);
+        stream.getStream().setName(COSName.TYPE, COSName.XOBJECT.getName());
+        stream.getStream().setName(COSName.SUBTYPE, subtype.getName());
+    }
+
+    /**
+     * Returns the stream.
+     * {@inheritDoc}
+     */
+    public final COSBase getCOSObject()
+    {
+        return stream.getCOSObject();
+    }
+
+    /**
+     * Returns the stream.
+     * @return The stream for this object.
+     */
+    public final COSStream getCOSStream()
+    {
+        return stream.getStream();
+    }
+
+    /**
+     * Returns the stream.
+     * @return The stream for this object.
+     */
+    public final PDStream getPDStream()
+    {
+        return stream;
+    }
+}

Added: 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=1570086&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCIEBasedColorSpace.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDCIEBasedColorSpace.java Thu Feb 20 06:31:01 2014
@@ -0,0 +1,87 @@
+/*
+ * 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 org.apache.pdfbox.cos.COSBase;
+
+import java.awt.image.BufferedImage;
+import java.awt.image.WritableRaster;
+import java.io.IOException;
+
+/**
+ * CIE-based colour spaces specify colours in a way that is independent of the characteristics
+ * of any particular output device. They are based on an international standard for colour
+ * specification created by the Commission Internationale de l?Éclairage (CIE).
+ *
+ * @author John Hewson
+ */
+public abstract class PDCIEBasedColorSpace extends PDColorSpace
+{
+    //
+    // WARNING: this method is performance sensitive, modify with care!
+    //
+    public BufferedImage toRGBImage(WritableRaster raster) throws IOException
+    {
+        // This method calls toRGB to convert images one pixel at a time. For matrix-based
+        // CIE color spaces this is fast enough. However, it should not be used with any
+        // color space which uses an ICC Profile as it will be far too slow.
+
+        int width = raster.getWidth();
+        int height = raster.getHeight();
+
+        BufferedImage rgbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+        WritableRaster rgbRaster = rgbImage.getRaster();
+
+        // always three components: ABC
+        float[] abc = new float[3];
+        for (int y = 0; y < height; y++)
+        {
+            for (int x = 0; x < width; x++)
+            {
+                raster.getPixel(x, y, abc);
+
+                // 0..255 -> 0..1
+                abc[0] /= 255;
+                abc[1] /= 255;
+                abc[2] /= 255;
+
+                float[] rgb = toRGB(abc);
+
+                // 0..1 -> 0..255
+                rgb[0] *= 255;
+                rgb[1] *= 255;
+                rgb[2] *= 255;
+
+                rgbRaster.setPixel(x, y, rgb);
+            }
+        }
+
+        return rgbImage;
+    }
+
+    @Override
+    public COSBase getCOSObject()
+    {
+        return array;
+    }
+
+    @Override
+    public String toString()
+    {
+        return getName();   // TODO return more info
+    }
+}

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=1570086&r1=1570085&r2=1570086&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 Thu Feb 20 06:31:01 2014
@@ -17,210 +17,61 @@
 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.cos.COSNumber;
-
-import java.awt.color.ColorSpace;
-import java.awt.image.ColorModel;
-
-import java.io.IOException;
 
 /**
- * This class represents a Cal Gray color space.
+ * A CalGray colour space is a special case of a single-component CIE-based colour space.
  *
- * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
- * @version $Revision: 1.5 $
+ * @author John Hewson
+ * @author Ben Litchfield
  */
-public class PDCalGray extends PDColorSpace
+public final class PDCalGray extends PDCalRGB
 {
-    /**
-     * The name of this color space.
-     */
-    public static final String NAME = "CalGray";
-
-    private COSArray array;
-    private COSDictionary dictionary;
+    private static final PDColor INITIAL_COLOR = new PDColor(new float[] { 0 });
 
     /**
-     * Constructor.
+     * Create a new CalGray color space.
      */
     public PDCalGray()
     {
         array = new COSArray();
         dictionary = new COSDictionary();
-        array.add( COSName.CALGRAY );
-        array.add( dictionary );
+        array.add(COSName.CALGRAY);
+        array.add(dictionary);
     }
 
     /**
-     * Constructor with array.
-     *
-     * @param gray The underlying color space.
+     * Creates a new CalGray color space using the given COS array.
+     * @param array the COS array which represents this color space
      */
-    public PDCalGray( COSArray gray )
+    public PDCalGray(COSArray array)
     {
-        array = gray;
-        dictionary = (COSDictionary)array.getObject( 1 );
+        this.array = array;
+        dictionary = (COSDictionary)array.getObject(1);
     }
 
-    /**
-     * This will get the number of components that this color space is made up of.
-     *
-     * @return The number of components in this color space.
-     *
-     * @throws IOException If there is an error getting the number of color components.
-     */
-    public int getNumberOfComponents() throws IOException
-    {
-        return 1;
-    }
-
-    /**
-     * This will return the name of the color space.
-     *
-     * @return The name of the color space.
-     */
+    @Override
     public String getName()
     {
-        return NAME;
-    }
-
-    /**
-     * Create a Java colorspace for this colorspace.
-     *
-     * @return A color space that can be used for Java AWT operations.
-     *
-     * @throws IOException If there is an error creating the color space.
-     */
-    protected ColorSpace createColorSpace() throws IOException
-    {
-        throw new IOException( "Not implemented" );
-    }
-
-    /**
-     * Create a Java color model for this colorspace.
-     *
-     * @param bpc The number of bits per component.
-     *
-     * @return A color model that can be used for Java AWT operations.
-     *
-     * @throws IOException If there is an error creating the color model.
-     */
-    public ColorModel createColorModel( int bpc ) throws IOException
-    {
-        throw new IOException( "Not implemented" );
-    }
-
-    /**
-     * Convert this standard java object to a COS object.
-     *
-     * @return The cos object that matches this Java object.
-     */
-    public COSBase getCOSObject()
-    {
-        return array;
+        return COSName.CALGRAY.getName();
     }
 
-    /**
-     * 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()
+    @Override
+    public int getNumberOfComponents()
     {
-        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 ) );
-    }
-
-    /**
-     * 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 ) );
-            dictionary.setItem( COSName.WHITE_POINT, wp );
-        }
-        return new PDTristimulus( wp );
-    }
-
-    /**
-     * This will set the whitepoint tristimulus.  As this is a required field
-     * this null should not be passed into this function.
-     *
-     * @param wp The whitepoint tristimulus.
-     */
-    public void setWhitepoint( PDTristimulus wp )
-    {
-        COSBase wpArray = wp.getCOSObject();
-        if( wpArray != null )
-        {
-            dictionary.setItem( COSName.WHITE_POINT, wpArray );
-        }
+        return 1;
     }
 
-    /**
-     * 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()
+    @Override
+    public float[] getDefaultDecode()
     {
-        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 );
+        return new float[] { 0, 1 };
     }
 
-    /**
-     * This will set the BlackPoint tristimulus.  As this is a required field
-     * this null should not be passed into this function.
-     *
-     * @param bp The BlackPoint tristimulus.
-     */
-    public void setBlackPoint( PDTristimulus bp )
+    @Override
+    public PDColor getInitialColor()
     {
-        COSBase bpArray = null;
-        if( bp != null )
-        {
-            bpArray = bp.getCOSObject();
-        }
-        dictionary.setItem( COSName.BLACK_POINT, bpArray );
+        return INITIAL_COLOR;
     }
 }

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=1570086&r1=1570085&r2=1570086&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 Thu Feb 20 06:31:01 2014
@@ -24,258 +24,234 @@ import org.apache.pdfbox.cos.COSName;
 
 import org.apache.pdfbox.pdmodel.common.PDMatrix;
 
-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;
 
 /**
- * This class represents a Cal RGB color space.
+ * A CalRGB colour space is a CIE-based colour space with one transformation stage instead of two.
+ * In this type of space, A, B, and C represent calibrated red, green, and blue colour values.
  *
- * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
- * @version $Revision: 1.3 $
+ * @author Ben Litchfield
+ * @author John Hewson
  */
-public class PDCalRGB extends PDColorSpace
+public class PDCalRGB extends PDCIEBasedColorSpace
 {
-    /**
-     * The name of this color space.
-     */
-    public static final String NAME = "CalRGB";
+    private static final ColorSpace CIEXYZ = ColorSpace.getInstance(ColorSpace.CS_CIEXYZ);
+    private static final PDColor INITIAL_COLOR = new PDColor(new float[] { 0, 0, 0 });
 
-    private COSArray array;
-    private COSDictionary dictionary;
+    protected COSArray array;
+    protected COSDictionary dictionary;
 
     /**
-     * Constructor.
+     * Creates a new CalRGB color space.
      */
     public PDCalRGB()
     {
         array = new COSArray();
         dictionary = new COSDictionary();
-        array.add( COSName.CALRGB );
-        array.add( dictionary );
+        array.add(COSName.CALRGB);
+        array.add(dictionary);
     }
 
     /**
-     * Constructor with array.
-     *
-     * @param rgb The underlying color space.
+     * Creates a new CalRGB color space using the given COS array.
+     * @param rgb the cos array which represents this color space
      */
-    public PDCalRGB( COSArray rgb )
+    public PDCalRGB(COSArray rgb)
     {
         array = rgb;
-        dictionary = (COSDictionary)array.getObject( 1 );
+        dictionary = (COSDictionary)array.getObject(1);
     }
 
-    /**
-     * This will get the number of components that this color space is made up of.
-     *
-     * @return The number of components in this color space.
-     *
-     * @throws IOException If there is an error getting the number of color components.
-     */
-    public int getNumberOfComponents() throws IOException
+    @Override
+    public String getName()
     {
-        return 3;
+        return COSName.CALRGB.getName();
     }
 
-    /**
-     * This will return the name of the color space.
-     *
-     * @return The name of the color space.
-     */
-    public String getName()
+    @Override
+    public int getNumberOfComponents()
     {
-        return NAME;
+        return 3;
     }
 
-    /**
-     * Create a Java colorspace for this colorspace.
-     *
-     * @return A color space that can be used for Java AWT operations.
-     */
-    protected ColorSpace createColorSpace()
+    @Override
+    public float[] getDefaultDecode()
     {
-        return new ColorSpaceCalRGB(getGamma(),getWhitepoint(),getBlackPoint(),getLinearInterpretation());
+        return new float[] { 0, 1, 0, 1, 0, 1 };
     }
 
-    /**
-     * Create a Java color model for this colorspace.
-     *
-     * @param bpc The number of bits per component.
-     *
-     * @return A color model that can be used for Java AWT operations.
-     *
-     * @throws IOException If there is an error creating the color model.
-     */
-    public ColorModel createColorModel( int bpc ) throws IOException
+    @Override
+    public PDColor getInitialColor()
     {
-        int[] nBits = {bpc, bpc, bpc};
-        return new ComponentColorModel( getJavaColorSpace(),
-                   nBits,
-                   false,
-                   false,
-                   Transparency.OPAQUE,
-                   DataBuffer.TYPE_BYTE);
+        return INITIAL_COLOR;
     }
 
-    /**
-     * Convert this standard java object to a COS object.
-     *
-     * @return The cos object that matches this Java object.
-     */
-    public COSBase getCOSObject()
+    //
+    // WARNING: this method is performance sensitive, modify with care!
+    //
+    @Override
+    public final float[] toRGB(float[] value)
     {
-        return array;
+        float a = value[0];
+        float b = value[0];
+        float c = value[0];
+
+        PDGamma g = getGamma();
+        PDMatrix m = getLinearInterpretation();
+
+        float xA = m.getValue(0, 0);
+        float xB = m.getValue(0, 1);
+        float xC = m.getValue(0, 2);
+
+        float yA = m.getValue(1, 0);
+        float yB = m.getValue(1, 1);
+        float yC = m.getValue(1, 2);
+
+        float zA = m.getValue(2, 0);
+        float zB = m.getValue(2, 1);
+        float zC = m.getValue(2, 2);
+
+        float v1 = (float)Math.pow(a, g.getR());  // A ^ G_R
+        float v2 = (float)Math.pow(b, g.getG());  // B ^ G_G
+        float v3 = (float)Math.pow(c, g.getB());  // C ^ G_B   NOTE: PDF32000 p147 is mistaken
+
+        float x = xA * v1 + xB * v2 + xC * v3;
+        float y = yA * v1 + yB * v2 + yC * v3;
+        float z = zA * v1 + zB * v2 + zC * v3;
+
+        // TODO scale XYZ values using blackpoint and whitepoint?
+
+        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.
+     * 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 PDTristimulus getWhitepoint()
+    public final PDTristimulus getWhitepoint()
     {
-        COSArray wp = (COSArray)dictionary.getDictionaryObject( COSName.WHITE_POINT );
-        if( wp == null )
+        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 );
+            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 );
+        return new PDTristimulus(wp);
     }
 
     /**
-     * This will set the whitepoint tristimulus.  As this is a required field
-     * this null should not be passed into this function.
-     *
-     * @param wp The whitepoint tristimulus.
+     * 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 void setWhitepoint( PDTristimulus wp )
+    public final PDTristimulus getBlackPoint()
     {
-        COSBase wpArray = wp.getCOSObject();
-        if( wpArray != null )
+        COSArray bp = (COSArray)dictionary.getDictionaryObject(COSName.BLACK_POINT);
+        if(bp == null)
         {
-            dictionary.setItem( COSName.WHITE_POINT, wpArray );
+            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);
     }
 
     /**
-     * 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.
+     * Returns the the gamma value.
+     * If none is present then the default of 1,1,1 will be returned.
+     * @return the gamma value
      */
-    public PDTristimulus getBlackPoint()
+    public final PDGamma getGamma()
     {
-        COSArray bp = (COSArray)dictionary.getDictionaryObject( COSName.BLACK_POINT );
-        if( bp == null )
+        COSArray gamma = (COSArray)dictionary.getDictionaryObject(COSName.GAMMA);
+        if(gamma == 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 );
+            gamma = new COSArray();
+            gamma.add(new COSFloat(1.0f));
+            gamma.add(new COSFloat(1.0f));
+            gamma.add(new COSFloat(1.0f));
+            dictionary.setItem(COSName.GAMMA, gamma);
         }
-        return new PDTristimulus( bp );
+        return new PDGamma(gamma);
     }
 
     /**
-     * This will set the BlackPoint tristimulus.  As this is a required field
-     * this null should not be passed into this function.
-     *
-     * @param bp The BlackPoint tristimulus.
+     * Returns the linear interpretation matrix.
+     * If the underlying dictionary contains null then the identity matrix will be returned.
+     * @return the linear interpretation matrix
      */
-    public void setBlackPoint( PDTristimulus bp )
+    public final PDMatrix getLinearInterpretation()
     {
-
-        COSBase bpArray = null;
-        if( bp != null )
+        COSArray matrix = (COSArray)dictionary.getDictionaryObject(COSName.MATRIX);
+        if(matrix == null)
         {
-            bpArray = bp.getCOSObject();
+            return new PDMatrix();
+        }
+        else
+        {
+           return new PDMatrix(matrix);
         }
-        dictionary.setItem( COSName.BLACK_POINT, bpArray );
     }
 
     /**
-     * This will get the gamma value.  If none is present then the default of 1,1,1
-     * will be returned.
-     *
-     * @return The gamma value.
+     * Sets the whitepoint tristimulus
+     * @param whitepoint the whitepoint tristimulus, which may not be null
      */
-    public PDGamma getGamma()
+    public final void setWhitepoint(PDTristimulus whitepoint)
     {
-        COSArray gamma = (COSArray)dictionary.getDictionaryObject( COSName.GAMMA );
-        if( gamma == null )
+        COSBase wpArray = whitepoint.getCOSObject();
+        if(wpArray != null)
         {
-            gamma = new COSArray();
-            gamma.add( new COSFloat( 1.0f ) );
-            gamma.add( new COSFloat( 1.0f ) );
-            gamma.add( new COSFloat( 1.0f ) );
-            dictionary.setItem( COSName.GAMMA, gamma );
+            dictionary.setItem(COSName.WHITE_POINT, wpArray);
         }
-        return new PDGamma( gamma );
     }
 
     /**
-     * Set the gamma value.
-     *
-     * @param value The new gamma value.
+     * Sets the blackpoint tristimulus
+     * @param blackpoint the blackpoint tristimulus, which may not be null
      */
-    public void setGamma( PDGamma value )
+    public final void setBlackPoint(PDTristimulus blackpoint)
     {
-        COSArray gamma = null;
-        if( value != null )
+        COSBase bpArray = null;
+        if(blackpoint != null)
         {
-            gamma = value.getCOSArray();
+            bpArray = blackpoint.getCOSObject();
         }
-        dictionary.setItem( COSName.GAMMA, gamma );
+        dictionary.setItem(COSName.BLACK_POINT, bpArray);
     }
 
     /**
-     * This will get the linear interpretation array.  This is guaranteed to not
-     * return null.  If the underlying dictionary contains null then the identity
-     * matrix will be returned.
-     *
-     * @return The linear interpretation matrix.
+     * Sets the gamma value.
+     * @param gamma the new gamma value
      */
-    public PDMatrix getLinearInterpretation()
+    public final void setGamma(PDGamma gamma)
     {
-        PDMatrix retval = null;
-        COSArray matrix = (COSArray)dictionary.getDictionaryObject( COSName.MATRIX );
-        if( matrix == null )
-        {
-            retval = new PDMatrix();
-            setLinearInterpretation( retval );
-        }
-        else
+        COSArray array = null;
+        if(gamma != null)
         {
-            retval = new PDMatrix( matrix );
+            array = gamma.getCOSArray();
         }
-        return retval;
+        dictionary.setItem(COSName.GAMMA, array);
     }
 
     /**
-     * This will set the linear interpretation matrix.  Passing in null will
-     * clear the matrix.
-     *
-     * @param matrix The new linear interpretation matrix.
+     * Sets the linear interpretation matrix.
+     * Passing in null will clear the matrix.
+     * @param matrix the new linear interpretation matrix, or null
      */
-    public void setLinearInterpretation( PDMatrix matrix )
+    public final void setLinearInterpretation(PDMatrix matrix)
     {
         COSArray matrixArray = null;
-        if( matrix != null )
+        if(matrix != null)
         {
             matrixArray = matrix.getCOSArray();
         }
-        dictionary.setItem( COSName.MATRIX, matrixArray );
+        dictionary.setItem(COSName.MATRIX, matrixArray);
     }
 }

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDColor.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDColor.java?rev=1570086&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDColor.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDColor.java Thu Feb 20 06:31:01 2014
@@ -0,0 +1,147 @@
+/*
+ * 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 org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSNumber;
+import org.apache.pdfbox.cos.COSString;
+import org.apache.pdfbox.pdmodel.graphics.pattern.PDPatternResources;
+
+import java.awt.Paint;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Map;
+
+/**
+ * A color value, consisting of one or more color components, or for pattern color spaces,
+ * a name and optional color components.
+ * Color values are not associated with any given color space.
+ *
+ * Instances of PDColor are immutable.
+ *
+ * @author John Hewson
+ */
+public final class PDColor
+{
+    /** The color black in the DeviceGray color space. */
+    public static PDColor DEVICE_GRAY_BLACK = new PDColor(new float[] { 0 });
+
+    /** A pattern which leaves no marks on the page. */
+    public static PDColor EMPTY_PATTERN = new PDColor(new float[] { }, null);
+
+    private final float[] components;
+    private final String patternName;
+
+    /**
+     * Creates a PDColor containing the given color value.
+     * @param array a COS array containing the color value
+     */
+    public PDColor(COSArray array)
+    {
+        if (array.get(array.size() - 1) instanceof COSName)
+        {
+            // color components (optional)
+            components = new float[array.size() - 1];
+            for (int i = 0; i < array.size() - 1; i++)
+            {
+                components[i] = ((COSNumber)array.get(i)).floatValue();
+            }
+
+            // pattern name (required)
+            patternName = ((COSName)array.get(array.size() - 1)).getName();
+        }
+        else
+        {
+            // color components only
+            components = new float[array.size()];
+            for (int i = 0; i < array.size(); i++)
+            {
+                components[i] = ((COSNumber)array.get(i)).floatValue();
+            }
+            patternName = null;
+        }
+    }
+
+    /**
+     * Creates a PDColor containing the given color component values.
+     * @param components array of color component values
+     */
+    public PDColor(float[] components)
+    {
+        this.components = components.clone();
+        this.patternName = null;
+    }
+
+    /**
+     * Creates a PDColor containing the given pattern name.
+     * @param patternName the name of a pattern in a pattern dictionary
+     */
+    public PDColor(String patternName)
+    {
+        this.components = new float[0];
+        this.patternName = patternName;
+    }
+
+    /**
+     * Creates a PDColor containing the given color component values and pattern name.
+     * @param components array of color component values
+     * @param patternName the name of a pattern in a pattern dictionary
+     */
+    public PDColor(float[] components, String patternName)
+    {
+        this.components = components.clone();
+        this.patternName = patternName;
+    }
+
+    /**
+     * Returns the components of this color value.
+     * @return the components of this color value
+     */
+    public float[] getComponents()
+    {
+        return components.clone();
+    }
+
+    /**
+     * Returns the pattern name from this color value.
+     * @return the pattern name from this color value
+     */
+    public String getPatternName()
+    {
+        return patternName;
+    }
+
+    /**
+     * Returns this color value as a COS array
+     * @return the color value as a COS array
+     */
+    public COSArray toCOSArray()
+    {
+        COSArray array = new COSArray();
+        array.setFloatArray(components);
+        array.add(new COSString(patternName));
+        return array;
+    }
+
+    @Override
+    public String toString()
+    {
+        return "PDColor{components=" + Arrays.toString(components) +
+                ", patternName=" + patternName + "}";
+    }
+}

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=1570086&r1=1570085&r2=1570086&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 Thu Feb 20 06:31:01 2014
@@ -17,104 +17,236 @@
 package org.apache.pdfbox.pdmodel.graphics.color;
 
 import org.apache.pdfbox.cos.COSBase;
-import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.cos.COSArray;
 
+import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.pdmodel.common.COSObjectable;
+import org.apache.pdfbox.pdmodel.graphics.pattern.PDPatternResources;
 
+import java.awt.Color;
+import java.awt.Paint;
+import java.awt.Transparency;
+import java.awt.image.BufferedImage;
+import java.awt.image.ColorConvertOp;
+import java.awt.image.ComponentColorModel;
+import java.awt.image.DataBuffer;
+import java.awt.image.WritableRaster;
 import java.io.IOException;
 
 import java.awt.color.ColorSpace;
 import java.awt.image.ColorModel;
-
+import java.util.Map;
 
 /**
- * This class represents a color space in a pdf document.
+ * A color space specifies how the colours of graphics objects will be painted on the page.
  *
- * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
- * @version $Revision: 1.5 $
+ * @author John Hewson
+ * @author Ben Litchfield
  */
 public abstract class PDColorSpace implements COSObjectable
 {
     /**
-     * array for the given parameters. 
+     * Creates a color space space given a name or array.
+     * @param colorSpace the color space COS object
+     * @return a new color space
+     * @throws IOException if the color space is unknown or cannot be created
      */
+    public static PDColorSpace create(COSBase colorSpace) throws IOException
+    {
+        return create(colorSpace, null, null);
+    }
+
+    /**
+     * Creates a color space given a name or array.
+     * @param colorSpace the color space COS object
+     * @param colorSpaces the ColorSpace dictionary from the current resources, if any
+     * @param patterns The Pattern dictionary from the current resources, if any
+     * @return a new color space
+     * @throws IOException if the color space is unknown or cannot be created
+     */
+    public static PDColorSpace create(COSBase colorSpace,
+                                      Map<String, PDColorSpace> colorSpaces,
+                                      Map<String, PDPatternResources> patterns)
+                                      throws IOException
+    {
+        if (colorSpace instanceof COSName)
+        {
+            COSName name = (COSName)colorSpace;
+
+            if (name == COSName.DEVICECMYK || name == COSName.CMYK)
+            {
+                return PDDeviceCMYK.INSTANCE;
+            }
+            else if (name == COSName.DEVICERGB || name == COSName.RGB)
+            {
+                return PDDeviceRGB.INSTANCE;
+            }
+            else if (name == COSName.DEVICEGRAY || name == COSName.G)
+            {
+                return PDDeviceGray.INSTANCE;
+            }
+            else if (name == COSName.PATTERN)
+            {
+                return new PDPattern(patterns);
+            }
+            else if (colorSpaces != null && colorSpaces.get(name.getName()) != null)
+            {
+                // a color space resource
+                return colorSpaces.get(name.getName());
+            }
+            else
+            {
+                throw new IOException("The color space '" + name + "' does not exist in the " +
+                                      "current page's resources");
+            }
+        }
+        else if (colorSpace instanceof COSArray)
+        {
+            COSArray array = (COSArray)colorSpace;
+            COSName name = (COSName)array.get(0);
+
+            // TODO cache these returned color spaces?
+
+            if (name == COSName.CALGRAY)
+            {
+                return new PDCalGray(array);
+            }
+            else if (name == COSName.CALRGB)
+            {
+                return new PDCalRGB(array);
+            }
+            else if (name == COSName.DEVICEN)
+            {
+                return new PDDeviceN(array);
+            }
+            else if (name == COSName.INDEXED || name == COSName.I)
+            {
+                return new PDIndexed(array);
+            }
+            else if (name == COSName.SEPARATION)
+            {
+                return new PDSeparation(array);
+            }
+            else if (name == COSName.ICCBASED)
+            {
+                return new PDICCBased(array);
+            }
+            else if (name == COSName.LAB)
+            {
+                return new PDLab(array);
+            }
+            else if (name == COSName.PATTERN)
+            {
+                return new PDPattern(patterns);
+            }
+            else
+            {
+                throw new IOException("Invalid color space kind: " + name);
+            }
+        }
+        else
+        {
+            throw new IOException("Expected a name or array but got: " + colorSpace);
+        }
+    }
+
+    // array for the given parameters
     protected COSArray array;
 
     /**
-     * Cached Java AWT color space.
-     *
-     * @see #getJavaColorSpace()
+     * Returns the name of the color space.
+     * @return the name of the color space
      */
-    private ColorSpace colorSpace = null;
+    public abstract String getName();
 
     /**
-     * This will return the name of the color space.
-     *
-     * @return The name of the color space.
+     * Returns the number of components in this color space
+     * @return the number of components in this color space
      */
-    public abstract String getName();
+    public abstract int getNumberOfComponents();
 
     /**
-     * This will get the number of components that this color space is made up of.
-     *
-     * @return The number of components in this color space.
-     *
-     * @throws IOException If there is an error getting the number of color components.
+     * Returns the default decode array for this color space.
+     * @return the default decode array
      */
-    public abstract int getNumberOfComponents() throws IOException;
+    public abstract float[] getDefaultDecode();
 
     /**
-     * Convert this standard java object to a COS object.
-     *
-     * @return The cos object that matches this Java object.
+     * Returns the initial color value for this color space.
+     * @return the initial color value for this color space
      */
-    public COSBase getCOSObject()
-    {
-        return COSName.getPDFName( getName() );
-    }
+    public abstract PDColor getInitialColor();
 
     /**
-     * Returns the Java AWT color space for this instance.
-     *
-     * @return Java AWT color space
-     * @throws IOException if the color space can not be created
-     */
-    public ColorSpace getJavaColorSpace() throws IOException {
-        if (colorSpace == null) {
-            colorSpace = createColorSpace();
-        }
-        return colorSpace;
-    }
+     * Returns the RGB equivalent of the given color value.
+     * @param value a color value with component values between 0 and 1
+     * @return an array of R,G,B value between 0 and 255
+     * @throws IOException if the color conversion fails
+     */
+    public abstract float[] toRGB(float[] value) throws IOException;
 
     /**
-     * Create a Java colorspace for this colorspace.
-     *
-     * @return A color space that can be used for Java AWT operations.
-     *
-     * @throws IOException If there is an error creating the color space.
+     * Returns the (A)RGB equivalent of the given raster.
+     * @param raster the source raster
+     * @return an (A)RGB buffered image
+     * @throws IOException if the color conversion fails
      */
-    protected abstract ColorSpace createColorSpace() throws IOException;
+    public abstract BufferedImage toRGBImage(WritableRaster raster) throws IOException;
 
     /**
-     * Create a Java color model for this colorspace.
-     *
-     * @param bpc The number of bits per component.
-     *
-     * @return A color model that can be used for Java AWT operations.
-     *
-     * @throws IOException If there is an error creating the color model.
+     * Returns the (A)RGB equivalent of the given raster, using the given AWT color space
+     * to perform the conversion.
+     * @param raster the source raster
+     * @param colorSpace the AWT
+     * @return an (A)RGB buffered image
      */
-    public abstract ColorModel createColorModel( int bpc ) throws IOException;
+    protected BufferedImage toRGBImageAWT(WritableRaster raster, ColorSpace colorSpace)
+    {
+        //
+        // WARNING: this method is performance sensitive, modify with care!
+        //
+
+        // ICC Profile color transforms are only fast when performed using ColorConvertOp
+        ColorModel colorModel = new ComponentColorModel(colorSpace,
+            false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
+
+        BufferedImage src = new BufferedImage(colorModel, raster, false, null);
+        BufferedImage dest = new BufferedImage(raster.getWidth(), raster.getHeight(),
+                                               BufferedImage.TYPE_INT_RGB);
+        ColorConvertOp op = new ColorConvertOp(null);
+        op.filter(src, dest);
+        return dest;
+    }
 
-    /*
-    Don't just tell me its color type -- tell me its contents!
-    */
     /**
-     * {@inheritDoc}
+     * Returns the AWT paint which corresponds to the given color value in this color space.
+     * @param color the color value
+     * @return an AWT paint
+     * @throws IOException if the color conversion fails
      */
-    public String toString()
+    public Paint toPaint(PDColor color) throws IOException
     {
-        return  getName() + "{ " + (array==null? "" : array.toString() ) + " }";
+        return toPaint(color, 0);
     }
+
+    /**
+     * Returns the AWT paint which corresponds to the given color value in this color space
+     * and the height of the current page.
+     * This is for use with pattern color spaces
+     * @param color the color value
+     * @param pageHeight the height of the current page, used by pattern color spaces
+     * @return an AWT paint
+     * @throws IOException if the color conversion fails
+     */
+    public Paint toPaint(PDColor color, int pageHeight) throws IOException
+    {
+        float[] rgb = toRGB(color.getComponents());
+        return new Color(rgb[0], rgb[1], rgb[2]);
+    }
+
+    /**
+     * Converts this standard java object to a COS object.
+     * @return The COS object that matches this Java object.
+     */
+    public abstract COSBase getCOSObject();
 }

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=1570086&r1=1570085&r2=1570086&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 Thu Feb 20 06:31:01 2014
@@ -16,106 +16,65 @@
  */
 package org.apache.pdfbox.pdmodel.graphics.color;
 
+import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.io.IOUtils;
 import org.apache.pdfbox.util.ResourceLoader;
 
 import java.awt.color.ColorSpace;
 import java.awt.color.ICC_ColorSpace;
 import java.awt.color.ICC_Profile;
-import java.awt.image.ColorModel;
+import java.awt.image.BufferedImage;
+import java.awt.image.WritableRaster;
 import java.io.IOException;
 
-import java.awt.Transparency;
-import java.awt.image.ComponentColorModel;
-import java.awt.image.DataBuffer;
 import java.io.InputStream;
 import java.util.Properties;
 
 /**
- * This class represents a CMYK color space.
+ * Allows colors to be specified according to the subtractive CMYK (cyan, magenta, yellow, black)
+ * model typical of printers and other paper-based output devices.
  *
- * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @author John Hewson
+ * @author Ben Litchfield
  */
-public class PDDeviceCMYK extends PDColorSpace
+public final class PDDeviceCMYK extends PDDeviceColorSpace
 {
-    /**
-     * The single instance of this class.
-     */
-    public static final PDDeviceCMYK INSTANCE = new PDDeviceCMYK();
-
-    /**
-     * The name of this color space.
-     */
-    public static final String NAME = "DeviceCMYK";
-
-    /**
-     * The abbreviated name of this color space.
-     */
-    public static final String ABBREVIATED_NAME = "CMYK";
-
-    /**
-     * The external resources
-     */
-    private static Properties defaultProfile = new Properties();
-
+    /**  The single instance of this class. */
+    public static final PDDeviceCMYK INSTANCE;
     static
     {
         try
         {
-            ResourceLoader.loadProperties(
-                    "org/apache/pdfbox/resources/PDDeviceCMYK.properties",
-                    defaultProfile);
+            INSTANCE = new PDDeviceCMYK();
         }
-        catch (IOException io)
+        catch (IOException e)
         {
-            throw new RuntimeException("Error loading resources", io);
+            throw new RuntimeException(e);
         }
     }
 
-    /**
-     * Constructs a new PDDeviceCMYK object.
-     */
-    private PDDeviceCMYK()
-    {
-    }
+    private final ICC_ColorSpace awtColorSpace;
+    private static final PDColor INITIAL_COLOR = new PDColor(new float[] { 0, 0, 0, 1 });
 
-    /**
-     * This will return the name of the color space.
-     *
-     * @return The name of the color space.
-     */
-    public String getName()
+    private PDDeviceCMYK() throws IOException
     {
-        return NAME;
+        awtColorSpace = getAWTColorSpace();
     }
 
-    /**
-     * This will get the number of components that this color space is made up of.
-     *
-     * @return The number of components in this color space.
-     *
-     * @throws IOException If there is an error getting the number of color components.
-     */
-    public int getNumberOfComponents() throws IOException
+    // loads the ICC color profile for CMYK
+    private static ICC_ColorSpace getAWTColorSpace() throws IOException
     {
-        return 4;
-    }
-
-    /**
-     * Create a Java colorspace for this colorspace.
-     *
-     * @return A color space that can be used for Java AWT operations.
-     */
-    protected ColorSpace createColorSpace() throws IOException
-    {
-        ColorSpace colorSpace;
+        ICC_ColorSpace colorSpace;
         InputStream profile = null;
         try
         {
-            profile = ResourceLoader.loadResource(defaultProfile.getProperty("DeviceCMYK"));
+            Properties properties = ResourceLoader.loadProperties(
+                    "org/apache/pdfbox/resources/PDDeviceCMYK.properties", new Properties());
+
+            profile = ResourceLoader.loadResource(properties.getProperty("DeviceCMYK"));
             if (profile == null)
             {
-                throw new IOException("Default CMYK color profile cannot be opened");
+                throw new IOException("Default CMYK color profile could not be loaded");
             }
             ICC_Profile iccProfile = ICC_Profile.getInstance(profile);
             colorSpace = new ICC_ColorSpace(iccProfile);
@@ -127,27 +86,39 @@ public class PDDeviceCMYK extends PDColo
         return colorSpace;
     }
 
-    /**
-     * Create a Java color model for this colorspace.
-     *
-     * @param bpc The number of bits per component.
-     *
-     * @return A color model that can be used for Java AWT operations.
-     *
-     * @throws IOException If there is an error creating the color model.
-     */
-    public ColorModel createColorModel(int bpc) throws IOException
-    {
-
-        int[] nbBits = { bpc, bpc, bpc, bpc };
-        ComponentColorModel componentColorModel =
-                new ComponentColorModel(getJavaColorSpace(),
-                        nbBits,
-                        false,
-                        false,
-                        Transparency.OPAQUE,
-                        DataBuffer.TYPE_BYTE);
-        return componentColorModel;
+    @Override
+    public String getName()
+    {
+        return COSName.DEVICECMYK.getName();
+    }
+
+    @Override
+    public int getNumberOfComponents()
+    {
+        return 4;
+    }
 
+    @Override
+    public float[] getDefaultDecode()
+    {
+        return new float[] { 0, 1, 0, 1, 0, 1, 0, 1 };
+    }
+
+    @Override
+    public PDColor getInitialColor()
+    {
+        return INITIAL_COLOR;
+    }
+
+    @Override
+    public float[] toRGB(float[] value)
+    {
+        return awtColorSpace.toRGB(value);
+    }
+
+    @Override
+    public BufferedImage toRGBImage(WritableRaster raster) throws IOException
+    {
+        return toRGBImageAWT(raster, awtColorSpace);
     }
 }

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDDeviceColorSpace.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDDeviceColorSpace.java?rev=1570086&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDDeviceColorSpace.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDDeviceColorSpace.java Thu Feb 20 06:31:01 2014
@@ -0,0 +1,40 @@
+/*
+ * 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 org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.cos.COSName;
+
+/**
+ * Device colour spaces directly specify colours or shades of gray produced by an output device.
+ *
+ * @author John Hewson
+ */
+public abstract class PDDeviceColorSpace extends PDColorSpace
+{
+    @Override
+    public String toString()
+    {
+        return getName();
+    }
+
+    @Override
+    public COSBase getCOSObject()
+    {
+        return COSName.getPDFName(getName());
+    }
+}

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDDeviceGray.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDDeviceGray.java?rev=1570086&r1=1570085&r2=1570086&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDDeviceGray.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDDeviceGray.java Thu Feb 20 06:31:01 2014
@@ -16,93 +16,70 @@
  */
 package org.apache.pdfbox.pdmodel.graphics.color;
 
+import org.apache.pdfbox.cos.COSName;
+
 import java.awt.color.ColorSpace;
+import java.awt.image.BufferedImage;
+import java.awt.image.ColorConvertOp;
 import java.awt.image.ColorModel;
 import java.awt.image.ComponentColorModel;
 import java.awt.image.DataBuffer;
 import java.awt.image.IndexColorModel;
 import java.awt.Transparency;
+import java.awt.image.WritableRaster;
 import java.io.IOException;
 
 /**
- * This class represents a Gray color space.
+ * A color space with black, white, and intermediate shades of gray.
  *
- * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
- * 
+ * @author Ben Litchfield
+ * @author John Hewson
  */
-public class PDDeviceGray extends PDColorSpace
+public final class PDDeviceGray extends PDDeviceColorSpace
 {
-    /**
-     * The name of this color space.
-     */
-    public static final String NAME = "DeviceGray";
-
-    /**
-     * The abbreviated name of this color space.
-     */
-    public static final String ABBREVIATED_NAME = "G";
-
-    /**
-     * This will return the name of the color space.
-     *
-     * @return The name of the color space.
-     */
+    /** The single instance of this class. */
+    public static final PDDeviceGray INSTANCE = new PDDeviceGray();
+
+    private static final ColorSpace COLOR_SPACE_GRAY = ColorSpace.getInstance(ColorSpace.CS_GRAY);
+    private static final PDColor INITIAL_COLOR = new PDColor(new float[] { 0 });
+
+    private PDDeviceGray()
+    {
+    }
+
+    @Override
     public String getName()
     {
-        return NAME;
+        return COSName.DEVICEGRAY.getName();
     }
 
-    /**
-     * This will get the number of components that this color space is made up of.
-     *
-     * @return The number of components in this color space.
-     *
-     * @throws IOException If there is an error getting the number of color components.
-     */
-    public int getNumberOfComponents() throws IOException
+    @Override
+    public int getNumberOfComponents()
     {
         return 1;
     }
 
-    /**
-     * Create a Java colorspace for this colorspace.
-     *
-     * @return A color space that can be used for Java AWT operations.
-     */
-    protected ColorSpace createColorSpace()
-    {
-        return ColorSpace.getInstance( ColorSpace.CS_GRAY );
-    }
-
-    /**
-     * Create a Java color model for this colorspace.
-     *
-     * @param bpc The number of bits per component.
-     *
-     * @return A color model that can be used for Java AWT operations.
-     *
-     * @throws IOException If there is an error creating the color model.
-     */
-    public ColorModel createColorModel( int bpc ) throws IOException
-    {
-    	ColorModel colorModel = null;
-    	if (bpc == 8)
-    	{
-    		ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
-            int[] nBits = {bpc};
-            colorModel = new ComponentColorModel(cs, nBits, false, false, Transparency.OPAQUE,DataBuffer.TYPE_BYTE);
-    	}
-    	else
-    	{
-        	int numEntries = 1 << bpc;
-            // calculate all possible values
-            byte[] indexedValues = new byte[numEntries];
-            for (int i = 0; i < numEntries; i++) 
-            {
-            	indexedValues[i] = (byte)(i*255/(numEntries - 1));
-            }
-            colorModel = new IndexColorModel(bpc, numEntries, indexedValues, indexedValues, indexedValues);
-    	}
-        return colorModel;
+    @Override
+    public float[] getDefaultDecode()
+    {
+        return new float[] { 0, 1 };
+    }
+
+    @Override
+    public PDColor getInitialColor()
+    {
+        return INITIAL_COLOR;
+    }
+
+    @Override
+    public float[] toRGB(float[] value)
+    {
+        return COLOR_SPACE_GRAY.toRGB(value);
+    }
+
+    @Override
+    public BufferedImage toRGBImage(WritableRaster raster) throws IOException
+    {
+        return toRGBImageAWT(raster, COLOR_SPACE_GRAY);
     }
 }