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/19 19:58:11 UTC

svn commit: r1469972 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading: AxialShadingContext.java PDShadingType2.java RadialShadingContext.java

Author: lehmi
Date: Fri Apr 19 17:58:10 2013
New Revision: 1469972

URL: http://svn.apache.org/r1469972
Log:
PDFBOX-1451 a shading type2/3 dictionary may contain an array of functions instead of just one

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/AxialShadingContext.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/PDShadingType2.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/RadialShadingContext.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/AxialShadingContext.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/AxialShadingContext.java?rev=1469972&r1=1469971&r2=1469972&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/AxialShadingContext.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/AxialShadingContext.java Fri Apr 19 17:58:10 2013
@@ -46,9 +46,9 @@ public class AxialShadingContext impleme
 {
 
     private ColorModel colorModel;
-    private PDFunction function;
     private ColorSpace shadingColorSpace;
     private PDFunction shadingTinttransform;
+    private PDShadingType2 shadingType;
 
     private float[] coords;
     private float[] domain;
@@ -76,7 +76,8 @@ public class AxialShadingContext impleme
     public AxialShadingContext(PDShadingType2 shadingType2, ColorModel colorModelValue, 
             AffineTransform xform, Matrix ctm, int pageHeight) 
     {
-        coords = shadingType2.getCoords().toFloatArray();
+        shadingType = shadingType2;
+        coords = shadingType.getCoords().toFloatArray();
         if (ctm != null)
         {
             // the shading is used in combination with the sh-operator
@@ -101,7 +102,7 @@ public class AxialShadingContext impleme
         // colorSpace 
         try 
         {
-            PDColorSpace cs = shadingType2.getColorSpace();
+            PDColorSpace cs = shadingType.getColorSpace();
             if (!(cs instanceof PDDeviceRGB))
             {
                 // we have to create an instance of the shading colorspace if it isn't RGB
@@ -130,26 +131,17 @@ public class AxialShadingContext impleme
             try
             {
                 // TODO bpc != 8 ??  
-                colorModel = shadingType2.getColorSpace().createColorModel(8);
+                colorModel = shadingType.getColorSpace().createColorModel(8);
             }
             catch(IOException exception)
             {
                 LOG.error("error while creating colorModel", exception);
             }
         }
-        // shading function
-        try
-        {
-            function = shadingType2.getFunction();
-        }
-        catch(IOException exception)
-        {
-            LOG.error("error while creating a function", exception);
-        }
         // domain values
-        if (shadingType2.getDomain() != null)
+        if (shadingType.getDomain() != null)
         {
-            domain = shadingType2.getDomain().toFloatArray();
+            domain = shadingType.getDomain().toFloatArray();
         }
         else 
         {
@@ -157,8 +149,8 @@ public class AxialShadingContext impleme
             domain = new float[]{0,1};
         }
         // extend values
-        COSArray extendValues = shadingType2.getExtend();
-        if (shadingType2.getExtend() != null)
+        COSArray extendValues = shadingType.getExtend();
+        if (shadingType.getExtend() != null)
         {
             extend = new boolean[2];
             extend[0] = ((COSBoolean)extendValues.get(0)).getValue();
@@ -183,9 +175,9 @@ public class AxialShadingContext impleme
     public void dispose() 
     {
         colorModel = null;
-        function = null;
         shadingColorSpace = null;
         shadingTinttransform = null;
+        shadingType = null;
     }
 
     /**
@@ -203,7 +195,6 @@ public class AxialShadingContext impleme
     {
         // create writable raster
         WritableRaster raster = getColorModel().createCompatibleWritableRaster(w, h);
-        float[] input = new float[1];
         int[] data = new int[w * h * 3];
         for (int j = 0; j < h; j++) 
         {
@@ -238,12 +229,12 @@ public class AxialShadingContext impleme
                         continue;
                     }
                 }
-                input[0] = (float)(domain[0] + (d1d0*inputValue));
+                float input = (float)(domain[0] + (d1d0*inputValue));
                 float[] values = null;
                 int index = (j * w + i) * 3;
                 try 
                 {
-                    values = function.eval(input);
+                    values = shadingType.evalFunction(input);
                     // convert color values from shading colorspace to RGB 
                     if (shadingColorSpace != null)
                     {

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/PDShadingType2.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/PDShadingType2.java?rev=1469972&r1=1469971&r2=1469972&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/PDShadingType2.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/PDShadingType2.java Fri Apr 19 17:58:10 2013
@@ -21,6 +21,7 @@ package org.apache.pdfbox.pdmodel.graphi
 import java.io.IOException;
 
 import org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.pdmodel.common.function.PDFunction;
@@ -36,7 +37,7 @@ public class PDShadingType2 extends PDSh
     private COSArray coords = null;
     private COSArray domain = null;
     private COSArray extend = null;
-    private PDFunction function = null;
+    private PDFunction[] functionArray = null;
     
     /**
      * Constructor using the given shading dictionary.
@@ -159,7 +160,7 @@ public class PDShadingType2 extends PDSh
      */
     public void setFunction(PDFunction newFunction)
     {
-        function = newFunction;
+        functionArray = null;
         if (newFunction == null)
         {
             getCOSDictionary().removeItem(COSName.FUNCTION);
@@ -171,20 +172,96 @@ public class PDShadingType2 extends PDSh
     }
 
     /**
+     * This will set the functions COSArray for the color conversion.
+     *
+     * @param newFunctions The new COSArray containing all functions.
+     */
+    public void setFunction(COSArray newFunctions)
+    {
+        functionArray = null;
+        if (newFunctions == null)
+        {
+            getCOSDictionary().removeItem(COSName.FUNCTION);
+        }
+        else
+        {
+            getCOSDictionary().setItem(COSName.FUNCTION, newFunctions);
+        }
+    }
+    /**
      * This will return the function used to convert the color values.
      *
      * @return The function
      * 
      * @exception IOException If we are unable to create the PDFunction object. 
      * 
+     * @deprecated
+     * 
      */
     public PDFunction getFunction() throws IOException
     {
-        if (function == null)
+        // TODO
+        // change the return value to COSArray
+        return null;
+    }
+    
+    /**
+     * Provide the function(s) of the shading dictionary as array.
+     * 
+     * @return an array containing the function(s) 
+     * @throws IOException throw if something went wrong
+     */
+    private PDFunction[] getFunctionsArray() throws IOException
+    {
+        if (functionArray == null)
+        {
+            COSBase functionObject = getCOSDictionary().getDictionaryObject(COSName.FUNCTION);
+            if (functionObject instanceof COSDictionary)
+            {
+                functionArray = new PDFunction[1];
+                functionArray[0] = PDFunction.create(functionObject);
+            }
+            else
+            {
+                COSArray functionCOSArray = (COSArray)functionObject;
+                int numberOfFunctions = functionCOSArray.size();
+                functionArray = new PDFunction[numberOfFunctions];
+                for (int i=0; i<numberOfFunctions; i++)
+                {
+                    functionArray[i] = PDFunction.create(functionCOSArray.get(i));
+                }
+            }
+        }
+        return functionArray;
+    }
+    
+    /**
+     * Convert the input value using the functions of the shading dictionary.
+     * 
+     * @param inputValue the input value
+     * @return the output values
+     * @throws IOException thrown if something went wrong
+     */
+    public float[] evalFunction(float inputValue) throws IOException
+    {
+        float[] input = new float[] {inputValue};
+        PDFunction[] functions = getFunctionsArray();
+        int numberOfFunctions = functions.length;
+        float[] returnValues = null;
+        if (numberOfFunctions == 1)
+        {
+            returnValues = functions[0].eval(input);
+        }
+        else
         {
-            function = PDFunction.create(getCOSDictionary().getDictionaryObject(COSName.FUNCTION));
+            returnValues = new float[numberOfFunctions];
+            for (int i=0; i<numberOfFunctions;i++)
+            {
+                float[] newValue = functions[i].eval(input);
+                returnValues[i] = newValue[0];
+            }
         }
-        return function;
+        return returnValues;
     }
 
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/RadialShadingContext.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/RadialShadingContext.java?rev=1469972&r1=1469971&r2=1469972&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/RadialShadingContext.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/RadialShadingContext.java Fri Apr 19 17:58:10 2013
@@ -46,9 +46,9 @@ public class RadialShadingContext implem
 {
 
     private ColorModel colorModel;
-    private PDFunction function;
     private ColorSpace shadingColorSpace;
     private PDFunction shadingTinttransform;
+    private PDShadingType3 shadingType;
 
     private float[] coords;
     private float[] domain;
@@ -81,7 +81,8 @@ public class RadialShadingContext implem
     public RadialShadingContext(PDShadingType3 shadingType3, ColorModel colorModelValue, 
             AffineTransform xform, Matrix ctm, int pageHeight) 
     {
-        coords = shadingType3.getCoords().toFloatArray();
+        shadingType = shadingType3;
+        coords = shadingType.getCoords().toFloatArray();
         if (ctm != null)
         {
             // the shading is used in combination with the sh-operator
@@ -108,7 +109,7 @@ public class RadialShadingContext implem
         // colorSpace 
         try 
         {
-            PDColorSpace cs = shadingType3.getColorSpace();
+            PDColorSpace cs = shadingType.getColorSpace();
             if (!(cs instanceof PDDeviceRGB))
             {
                 // we have to create an instance of the shading colorspace if it isn't RGB
@@ -137,26 +138,17 @@ public class RadialShadingContext implem
             try
             {
                 // TODO bpc != 8 ??  
-                colorModel = shadingType3.getColorSpace().createColorModel(8);
+                colorModel = shadingType.getColorSpace().createColorModel(8);
             }
             catch(IOException exception)
             {
                 LOG.error("error while creating colorModel", exception);
             }
         }
-        // shading function
-        try
-        {
-            function = shadingType3.getFunction();
-        }
-        catch(IOException exception)
-        {
-            LOG.error("error while creating a function", exception);
-        }
         // domain values
-        if (shadingType3.getDomain() != null)
+        if (shadingType.getDomain() != null)
         {
-            domain = shadingType3.getDomain().toFloatArray();
+            domain = shadingType.getDomain().toFloatArray();
         }
         else 
         {
@@ -164,8 +156,8 @@ public class RadialShadingContext implem
             domain = new float[]{0,1};
         }
         // extend values
-        COSArray extendValues = shadingType3.getExtend();
-        if (shadingType3.getExtend() != null)
+        COSArray extendValues = shadingType.getExtend();
+        if (shadingType.getExtend() != null)
         {
             extend = new boolean[2];
             extend[0] = ((COSBoolean)extendValues.get(0)).getValue();
@@ -194,7 +186,7 @@ public class RadialShadingContext implem
     public void dispose() 
     {
         colorModel = null;
-        function = null;
+        shadingType = null;
         shadingColorSpace = null;
         shadingTinttransform = null;
     }
@@ -214,7 +206,6 @@ public class RadialShadingContext implem
     {
         // create writable raster
         WritableRaster raster = getColorModel().createCompatibleWritableRaster(w, h);
-        float[] input = new float[1];
         float inputValue;
         int[] data = new int[w * h * 3];
         for (int j = 0; j < h; j++) 
@@ -276,12 +267,12 @@ public class RadialShadingContext implem
                         continue;
                     }
                 }
-                input[0] = (float)(domain[0] + (d1d0*inputValue));
+                float input = (float)(domain[0] + (d1d0*inputValue));
                 float[] values = null;
                 int index = (j * w + i) * 3;
                 try 
                 {
-                    values = function.eval(input);
+                    values = shadingType.evalFunction(input);
                     // convert color values from shading colorspace to RGB 
                     if (shadingColorSpace != null)
                     {