001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.pdf.pd;
031:
032: import de.intarsys.pdf.cos.COSArray;
033: import de.intarsys.pdf.cos.COSName;
034: import de.intarsys.pdf.cos.COSObject;
035:
036: /**
037: * Function implementation supporting interpolation.
038: */
039: public class PDInterpolationFunction extends PDFunction {
040: /**
041: * The meta class implementation
042: */
043: static public class MetaClass extends PDFunction.MetaClass {
044: protected MetaClass(Class paramInstanceClass) {
045: super (paramInstanceClass);
046: }
047: }
048:
049: public static final COSName DK_C0 = COSName.constant("C0"); //$NON-NLS-1$
050:
051: public static final COSName DK_C1 = COSName.constant("C1"); //$NON-NLS-1$
052:
053: public static final COSName DK_N = COSName.constant("N"); //$NON-NLS-1$
054:
055: /** The meta class instance */
056: public static final MetaClass META = new MetaClass(MetaClass.class
057: .getDeclaringClass());
058:
059: private float[] c0;
060:
061: private float[] c1;
062:
063: private float n;
064:
065: protected PDInterpolationFunction(COSObject object) {
066: super (object);
067:
068: COSArray cosC0;
069: COSArray cosC1;
070:
071: cosC0 = cosGetDict().get(DK_C0).asArray();
072: if (cosC0 == null) {
073: c0 = new float[] { 0.0f };
074: } else {
075: c0 = new float[cosC0.size()];
076: for (int i = 0; i < c0.length; i++) {
077: c0[i] = cosC0.get(i).asNumber().floatValue();
078: }
079: }
080:
081: cosC1 = cosGetDict().get(DK_C1).asArray();
082: if (cosC1 == null) {
083: c1 = new float[] { 1.0f };
084: } else {
085: c1 = new float[cosC1.size()];
086: for (int i = 0; i < c1.length; i++) {
087: c1[i] = cosC1.get(i).asNumber().floatValue();
088: }
089: }
090:
091: // TODO 2 @ehk appropriate exception if null (required value according
092: // to spec)
093: n = cosGetDict().get(DK_N).asNumber().floatValue();
094: }
095:
096: protected float[] evaluate(float value) {
097: float[] result;
098:
099: if (value == 0.0) {
100: return getC0();
101: }
102: if (value == 1.0) {
103: return getC1();
104: }
105:
106: // try to make faster by skipping accessor methods
107: result = new float[getOutputSize()];
108: for (int i = 0; i < result.length; i++) {
109: result[i] = (float) (c0[i] + (Math.pow(value, n) * (c1[i] - c0[i])));
110: }
111: return result;
112: }
113:
114: /*
115: * (non-Javadoc)
116: *
117: * @see de.intarsys.pdf.pd.PDFunction#evaluate(float[])
118: */
119: public float[] evaluate(float[] values) {
120: int outputSize;
121: float[] result;
122:
123: outputSize = getOutputSize();
124: result = new float[values.length * outputSize];
125:
126: for (int i = 0; i < values.length; i++) {
127: System.arraycopy(evaluate(values[i]), i, result, i
128: * outputSize, outputSize);
129: }
130: return result;
131: }
132:
133: public float[] getC0() {
134: return c0;
135: }
136:
137: public float[] getC1() {
138: return c1;
139: }
140:
141: public float getN() {
142: return n;
143: }
144:
145: /*
146: * (non-Javadoc)
147: *
148: * @see de.intarsys.pdf.pd.PDFunction#getOutputSize()
149: */
150: public int getOutputSize() {
151: return getC0().length;
152: }
153: }
|