1 /*
2  * $Id: FunctionType2.java,v 1.2 2007-12-20 18:33:34 rbair Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 package com.sun.pdfview.function;
23 
24 import java.io.IOException;
25 
26 import com.sun.pdfview.PDFObject;
27 import com.sun.pdfview.PDFParseException;
28 
29 /**
30  * A type 2 function is an exponential interpolation function, which maps
31  * from one input value to n output values using a simple exponential
32  * formula.
33  */
34 public class FunctionType2 extends PDFFunction {
35     /** the function's value at zero for the n outputs */
36     private float[] c0 = new float[] { 0f };
37 
38     /** the function's value at one for the n outputs */
39     private float[] c1 = new float[] { 1f };
40 
41     /** the exponent */
42     private float n;
43 
44     /** Creates a new instance of FunctionType2 */
FunctionType2()45     public FunctionType2() {
46         super(TYPE_2);
47     }
48 
49     /**
50      * Read the zeros, ones and exponent
51      */
parse(PDFObject obj)52     protected void parse(PDFObject obj) throws IOException
53     {
54         // read the exponent (required)
55         PDFObject nObj = obj.getDictRef("N");
56         if (nObj == null) {
57             throw new PDFParseException("Exponent required for function type 2!");
58         }
59         setN(nObj.getFloatValue());
60 
61         // read the zeros array (optional)
62         PDFObject cZeroObj = obj.getDictRef("C0");
63         if (cZeroObj != null) {
64             PDFObject[] cZeroAry = cZeroObj.getArray();
65             float[] cZero = new float[cZeroAry.length];
66             for (int i = 0; i < cZeroAry.length; i++) {
67                 cZero[i] = cZeroAry[i].getFloatValue();
68             }
69             setC0(cZero);
70         }
71 
72         // read the ones array (optional)
73         PDFObject cOneObj = obj.getDictRef("C1");
74         if (cOneObj != null) {
75             PDFObject[] cOneAry = cOneObj.getArray();
76             float[] cOne = new float[cOneAry.length];
77             for (int i = 0; i < cOneAry.length; i++) {
78                 cOne[i] = cOneAry[i].getFloatValue();
79             }
80             setC1(cOne);
81         }
82     }
83 
84     /**
85      * Calculate the function value for the input.  For each output (j),
86      * the function value is:
87      * C0(j) + x^N * (C1(j) - C0(j))
88      */
doFunction(float[] inputs, int inputOffset, float[] outputs, int outputOffset)89     protected void doFunction(float[] inputs, int inputOffset,
90                               float[] outputs, int outputOffset)
91     {
92         // read the input value
93         float input = inputs[inputOffset];
94 
95         // calculate the output values
96         for (int i = 0; i < getNumOutputs(); i++) {
97             outputs[i + outputOffset] = getC0(i) +
98                 (float) (Math.pow(input, getN()) * (getC1(i) - getC0(i)));
99         }
100     }
101 
102     /**
103      * Get the exponent
104      */
getN()105     public float getN() {
106         return n;
107     }
108 
109     /**
110      * Set the exponent
111      */
setN(float n)112     protected void setN(float n) {
113         this.n = n;
114     }
115 
116     /**
117      * Get the values at zero
118      */
getC0(int index)119     public float getC0(int index) {
120         return c0[index];
121     }
122 
123     /**
124      * Set the values at zero
125      */
setC0(float[] c0)126     protected void setC0(float[] c0) {
127         this.c0 = c0;
128     }
129 
130     /**
131      * Get the values at one
132      */
getC1(int index)133     public float getC1(int index) {
134         return c1[index];
135     }
136 
137     /**
138      * Set the values at one
139      */
setC1(float[] c1)140     protected void setC1(float[] c1) {
141         this.c1 = c1;
142     }
143 }
144