1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.pdfbox.pdmodel.common.function; 18 19 import org.apache.pdfbox.cos.COSArray; 20 import org.apache.pdfbox.cos.COSDictionary; 21 import org.apache.pdfbox.cos.COSFloat; 22 import org.apache.pdfbox.cos.COSName; 23 import java.io.IOException; 24 import java.lang.Math; 25 26 /** 27 * This class represents a type 2 function in a PDF document. 28 * 29 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> 30 * @version $Revision: 1.2 $ 31 */ 32 public class PDFunctionType2 extends PDDictionaryFunction 33 { 34 35 private COSArray C0, C1; 36 37 /** 38 * Constructor to create a new blank type 2 function. 39 */ 40 protected PDFunctionType2() 41 { 42 super( 2 ); 43 } 44 45 /** 46 * Constructor. 47 * 48 * @param functionDictionary The prepopulated function dictionary. 49 */ 50 public PDFunctionType2( COSDictionary functionDictionary ) 51 { 52 super( functionDictionary ); 53 } 54 55 /** 56 * {@inheritDoc} 57 */ 58 public COSArray Eval(COSArray input) throws IOException 59 { 60 //This function performs exponential interpolation. 61 //It uses only a single value as its input, but may produce a multi-valued output. 62 //See PDF Reference section 3.9.2. 63 64 double x = input.toFloatArray()[0]; 65 COSArray y = new COSArray(); 66 for (int j=0;j<getC0().size();j++) 67 { 68 //y[j] = C0[j] + x^N*(C1[j] - C0[j]) 69 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()) ); 70 y.add( new COSFloat( FofX)); 71 } 72 73 return y; 74 } 75 76 protected COSArray getC0() 77 { 78 if(C0 == null) 79 { 80 C0 = getRangeArray("C0",1); 81 } 82 return C0; 83 } 84 85 protected COSArray getC1() 86 { 87 if(C1 == null) 88 { 89 //can't use getRangeArray here as the default is 1.0, not 0.0. 90 C1 = (COSArray)getCOSDictionary().getDictionaryObject( COSName.getPDFName( "C1" ) ); 91 if( C1 == null ) 92 { 93 C1 = new COSArray(); 94 getCOSDictionary().setItem( "C1", C1 ); 95 C1.add( new COSFloat( 1 ) ); 96 } 97 } 98 return C1; 99 } 100 101 protected float getN(){ 102 return getCOSDictionary().getFloat("N"); 103 } 104 }