pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From danielwil...@apache.org
Subject svn commit: r916894 - in /pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function: PDDictionaryFunction.java PDFunction.java PDFunctionType0.java PDFunctionType2.java PDFunctionType3.java PDFunctionType4.java
Date Sat, 27 Feb 2010 02:25:16 GMT
Author: danielwilson
Date: Sat Feb 27 02:25:16 2010
New Revision: 916894

URL: http://svn.apache.org/viewvc?rev=916894&view=rev
Log:
Initial implementation of an Eval method for types 2 & 3.
These may need some adjustment, but as far as I can tell, they're according to the spec.

Modified:
    pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDDictionaryFunction.java
    pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunction.java
    pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType0.java
    pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType2.java
    pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType3.java
    pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType4.java

Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDDictionaryFunction.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDDictionaryFunction.java?rev=916894&r1=916893&r2=916894&view=diff
==============================================================================
--- pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDDictionaryFunction.java
(original)
+++ pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDDictionaryFunction.java
Sat Feb 27 02:25:16 2010
@@ -74,7 +74,7 @@
         return function;
     }
 
-    private COSArray getRangeArray( String fieldName, int n )
+    protected COSArray getRangeArray( String fieldName, int n )
     {
         COSArray rangeArray = (COSArray)function.getDictionaryObject( COSName.getPDFName(
"Range" ) );
         if( rangeArray == null )

Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunction.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunction.java?rev=916894&r1=916893&r2=916894&view=diff
==============================================================================
--- pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunction.java (original)
+++ pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunction.java Sat
Feb 27 02:25:16 2010
@@ -22,6 +22,7 @@
 import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSObject;
 import org.apache.pdfbox.cos.COSStream;
+import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.pdmodel.common.COSObjectable;
 import org.apache.pdfbox.pdmodel.common.PDRange;
 import org.apache.pdfbox.pdmodel.common.PDStream;
@@ -152,4 +153,14 @@
      */
     public abstract void setDomainForInput(PDRange range, int n);
 
+
+    /**
+     * Evaluates the function at the given input.
+     * ReturnValue = f(input)
+     *
+     * @param input The array of input values for the function. In many cases will be an
array of a single value, but not always.
+     * @return The of outputs the function returns based on those inputs. In many cases will
be an array of a single value, but not always.
+     */
+    public abstract COSArray Eval(COSArray input) throws IOException;
+
 }

Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType0.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType0.java?rev=916894&r1=916893&r2=916894&view=diff
==============================================================================
--- pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType0.java
(original)
+++ pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType0.java
Sat Feb 27 02:25:16 2010
@@ -17,6 +17,7 @@
 package org.apache.pdfbox.pdmodel.common.function;
 
 import java.util.List;
+import java.io.IOException;
 
 import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSFloat;
@@ -183,4 +184,14 @@
         encode.set( paramNum*2, new COSFloat( range.getMin() ) );
         encode.set( paramNum*2+1, new COSFloat( range.getMax() ) );
     }
+    
+    /**
+    * {@inheritDoc}
+    */
+    public COSArray Eval(COSArray input) throws IOException
+    {
+        //This involves linear interpolation based on a set of sample points.
+        //Theoretically it's not that difficult ... see section 3.9.1 of the PDF Reference.
+        throw new IOException("Not Implemented");
+    }
 }

Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType2.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType2.java?rev=916894&r1=916893&r2=916894&view=diff
==============================================================================
--- pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType2.java
(original)
+++ pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType2.java
Sat Feb 27 02:25:16 2010
@@ -16,7 +16,12 @@
  */
 package org.apache.pdfbox.pdmodel.common.function;
 
+import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSFloat;
+import org.apache.pdfbox.cos.COSName;
+import java.io.IOException;
+import java.lang.Math;
 
 /**
  * This class represents a type 2 function in a PDF document.
@@ -26,6 +31,8 @@
  */
 public class PDFunctionType2 extends PDDictionaryFunction
 {
+    
+    private COSArray C0, C1;
 
     /**
      * Constructor to create a new blank type 2 function.
@@ -45,4 +52,53 @@
         super( functionDictionary );
     }
 
+    /**
+    * {@inheritDoc}
+    */
+    public COSArray Eval(COSArray 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 x = input.toFloatArray()[0];
+        COSArray y = new COSArray();
+        for (int j=0;j<getC0().size();j++)
+        {
+            //y[j] = C0[j] + x^N*(C1[j] - C0[j])
+            float FofX =(float)( ((COSFloat)C0.get(j)).floatValue() + java.lang.Math.pow(x,(double)getN())*(((COSFloat)C1.get(j)).floatValue()
- ((COSFloat)C0.get(j)).floatValue()) );
+            y.add( new COSFloat( FofX));
+        }
+        
+        return y;
+    }
+    
+    protected COSArray getC0()
+    {
+        if(C0 == null)
+        {
+            C0 = getRangeArray("C0",1);
+        }        
+        return C0;
+    }
+    
+    protected COSArray getC1()
+    {
+        if(C1 == null)
+        {
+            //can't use getRangeArray here as the default is 1.0, not 0.0.
+            C1 = (COSArray)getCOSDictionary().getDictionaryObject( COSName.getPDFName( "C1"
) );
+            if( C1 == null )
+            {
+                C1 = new COSArray();
+                getCOSDictionary().setItem( "C1", C1 );
+                C1.add( new COSFloat( 1 ) );
+            }      
+        }            
+        return C1;
+    }
+    
+    protected float getN(){
+        return getCOSDictionary().getFloat("N");
+    }
 }

Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType3.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType3.java?rev=916894&r1=916893&r2=916894&view=diff
==============================================================================
--- pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType3.java
(original)
+++ pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType3.java
Sat Feb 27 02:25:16 2010
@@ -16,7 +16,11 @@
  */
 package org.apache.pdfbox.pdmodel.common.function;
 
+import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSFloat;
+import org.apache.pdfbox.cos.COSName;
+import java.io.IOException;
 
 /**
  * This class represents a type 3 function in a PDF document.
@@ -45,4 +49,60 @@
         super( functionDictionary );
     }
 
+    /**
+    * {@inheritDoc}
+    */
+    public COSArray Eval(COSArray input) throws IOException
+    {
+        //This function is known as a "stitching" function. Based on the input, it decides
which child function to call.
+        //See PDF Reference section 3.9.3.
+        
+        PDFunction F=null;
+        float x = ((COSFloat)input.get(0)).floatValue();
+        COSArray Domain = getDomainForInput(1).getCOSArray();
+        
+        if (getBounds().size() == 0)
+        {
+            F = PDFunction.create(getFunctions().get(0));
+        }
+        else
+        {
+            //check boundary conditions first ...
+            if (x < ((COSFloat)Domain.get(0)).floatValue())
+                F = PDFunction.create(getFunctions().get(0));
+            else if (x > ((COSFloat)Domain.get(1)).floatValue())
+                F = PDFunction.create(getFunctions().get(getFunctions().size()-1));
+            else
+            {
+                float[] fBounds = getBounds().toFloatArray();
+                for (int k = 0; k<getBounds().size(); k++){
+                    if (x <= fBounds[k])
+                    {
+                        F = PDFunction.create(getFunctions().get(k));
+                        break;
+                    }
+                }
+                if(F==null) //must be in last partition
+                {
+                    F = PDFunction.create(getFunctions().get(getFunctions().size()-1));
+                }
+            }
+        }
+        return F.Eval(input);
+    }
+    
+    protected COSArray getFunctions()
+    {
+        return (COSArray)(getCOSDictionary().getDictionaryObject( COSName.getPDFName( "Functions"
)  ));
+    }
+    
+    protected COSArray getBounds()
+    {
+        return (COSArray)(getCOSDictionary().getDictionaryObject( COSName.getPDFName( "Bounds"
)  ));
+    }
+    
+    protected COSArray getEncode()
+    {
+        return (COSArray)(getCOSDictionary().getDictionaryObject( COSName.getPDFName( "Encode"
)  ));
+    }
 }

Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType4.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType4.java?rev=916894&r1=916893&r2=916894&view=diff
==============================================================================
--- pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType4.java
(original)
+++ pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType4.java
Sat Feb 27 02:25:16 2010
@@ -18,6 +18,8 @@
 
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.common.PDStream;
+import org.apache.pdfbox.cos.COSArray;
+import java.io.IOException;
 
 /**
  * This class represents a type 4 function in a PDF document.
@@ -47,4 +49,15 @@
     {
         super( functionDictionary );
     }
+    
+    
+    /**
+    * {@inheritDoc}
+    */
+    public COSArray Eval(COSArray input) throws IOException
+    {
+        //Implementation here will require evaluation of PostScript functions.
+        //See section 3.9.4 of the PDF Reference.
+        throw new IOException("Not Implemented");
+    }
 }



Mime
View raw message