You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2014/07/16 22:06:11 UTC

svn commit: r1611163 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType2.java

Author: tilman
Date: Wed Jul 16 20:06:10 2014
New Revision: 1611163

URL: http://svn.apache.org/r1611163
Log:
PDFBOX-1915: Optimization based on suggestion by Shaola Ren

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType2.java

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=1611163&r1=1611162&r2=1611163&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 Wed Jul 16 20:06:10 2014
@@ -24,35 +24,57 @@ import org.apache.pdfbox.cos.COSNumber;
 import java.io.IOException;
 
 /**
- * This class represents a Type 2 (exponential interpolation) 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 $
+ * @author Ben Litchfield
  */
 public class PDFunctionType2 extends PDFunction
 {
-    
+
     /**
      * The C0 values of the exponential function.
      */
-    private COSArray C0;
+    private final COSArray c0;
     /**
      * The C1 values of the exponential function.
      */
-    private COSArray C1;
+    private final COSArray c1;
     /**
-     * The N value of the exponential function.
+     * The exponent value of the exponential function.
      */
-    private Float N;
+    private final float exponent;
 
     /**
      * Constructor.
      *
-     * @param function The function .
+     * @param function The function.
      */
     public PDFunctionType2(COSBase function)
     {
-        super( function );
+        super(function);
+
+        if (getDictionary().getDictionaryObject(COSName.C0) == null)
+        {
+            c0 = new COSArray();
+            c0.add(new COSFloat(0));
+        }
+        else
+        {
+            c0 = (COSArray) getDictionary().getDictionaryObject(COSName.C0);
+        }
+
+        if (getDictionary().getDictionaryObject(COSName.C1) == null)
+        {
+            c1 = new COSArray();
+            c1.add(new COSFloat(1));
+        }
+        else
+        {
+            c1 = (COSArray) getDictionary().getDictionaryObject(COSName.C1);
+        }
+
+        exponent = getDictionary().getFloat(COSName.N);
     }
 
     /**
@@ -67,77 +89,53 @@ public class PDFunctionType2 extends PDF
     /**
      * Performs exponential interpolation
      *
-    * {@inheritDoc}
-    */
+     * {@inheritDoc}
+     */
     @Override
     public float[] eval(float[] input) throws IOException
     {
-        COSArray c0 = getC0();
-        COSArray c1 = getC1();
-
         // exponential interpolation
-        float xToN = (float)Math.pow(input[0], getN()); // x^N
+        float xToN = (float) Math.pow(input[0], exponent); // x^exponent
 
         float[] result = new float[c0.size()];
         for (int j = 0; j < result.length; j++)
         {
-            float C0j = ((COSNumber)c0.get(j)).floatValue();
-            float C1j = ((COSNumber)c1.get(j)).floatValue();
+            float C0j = ((COSNumber) c0.get(j)).floatValue();
+            float C1j = ((COSNumber) c1.get(j)).floatValue();
             result[j] = C0j + xToN * (C1j - C0j);
         }
 
         return clipToRange(result);
     }
-    
+
     /**
      * Returns the C0 values of the function, 0 if empty.
+     *
      * @return a COSArray with the C0 values
      */
     public COSArray getC0()
     {
-        if(C0 == null)
-        {
-            C0 = (COSArray)getDictionary().getDictionaryObject( COSName.C0 );
-            if ( C0 == null )
-            {
-                // C0 is optional, default = 0
-                C0 = new COSArray();
-                C0.add( new COSFloat( 0 ) );
-            }
-        }
-        return C0;
+        return c0;
     }
-    
+
     /**
      * Returns the C1 values of the function, 1 if empty.
+     *
      * @return a COSArray with the C1 values
      */
     public COSArray getC1()
     {
-        if(C1 == null)
-        {
-            C1 = (COSArray)getDictionary().getDictionaryObject( COSName.C1 );
-            if( C1 == null )
-            {
-                // C1 is optional, default = 1
-                C1 = new COSArray();
-                C1.add( new COSFloat( 1 ) );
-            }      
-        }            
-        return C1;
+        return c1;
     }
-    
+
     /**
      * Returns the exponent of the function.
+     *
      * @return the float value of the exponent
      */
     public float getN()
     {
-        if (N == null)
-        {
-            N = getDictionary().getFloat(COSName.N);
-        }
-        return N;
+        return exponent;
     }
 
     /**
@@ -146,8 +144,8 @@ public class PDFunctionType2 extends PDF
     @Override
     public String toString()
     {
-        return "FunctionType2{" +
-                "C0:" + getC0() + " " +
-                "C1:" + getC1() + "}";
+        return "FunctionType2{"
+                + "C0:" + getC0() + " "
+                + "C1:" + getC1() + "}";
     }
 }