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.COSBasedObject;
034: import de.intarsys.pdf.cos.COSDictionary;
035: import de.intarsys.pdf.cos.COSInteger;
036: import de.intarsys.pdf.cos.COSName;
037: import de.intarsys.pdf.cos.COSNumber;
038: import de.intarsys.pdf.cos.COSObject;
039: import de.intarsys.pdf.cos.COSStream;
040:
041: /**
042: * Abstract superclass for PDF function objects.
043: */
044: abstract public class PDFunction extends PDObject {
045: /**
046: * The meta class implementation
047: */
048: static public class MetaClass extends PDObject.MetaClass {
049: protected MetaClass(Class instanceClass) {
050: super (instanceClass);
051: }
052:
053: public Class getRootClass() {
054: return PDFunction.class;
055: }
056:
057: protected COSBasedObject.MetaClass doDetermineClass(
058: COSObject object) {
059: COSDictionary dict = null;
060: if (object instanceof COSStream) {
061: dict = ((COSStream) object).getDict();
062: } else if (object instanceof COSDictionary) {
063: dict = (COSDictionary) object;
064: }
065: if (dict == null) {
066: throw new IllegalArgumentException(
067: "No Function dictionary available");
068: }
069: COSInteger type = dict.get(DK_FunctionType).asInteger();
070: if (type == null) {
071: throw new IllegalArgumentException(
072: "Function dictionary has no type");
073: }
074: if (type.intValue() == 0) {
075: return PDSampledFunction.META;
076: }
077: if (type.intValue() == 2) {
078: return PDInterpolationFunction.META;
079: }
080: if (type.intValue() == 3) {
081: return PDStitchingFunction.META;
082: }
083: if (type.intValue() == 4) {
084: return PDPostScriptFunction.META;
085: }
086: throw new IllegalArgumentException("Function type " + type
087: + " not supported");
088: }
089: }
090:
091: /** The meta class instance */
092: static public final MetaClass META = new MetaClass(MetaClass.class
093: .getDeclaringClass());
094:
095: /** Common names */
096: static public final COSName DK_FunctionType = COSName
097: .constant("FunctionType");
098:
099: static public final COSName DK_Domain = COSName.constant("Domain");
100:
101: static public final COSName DK_Range = COSName.constant("Range");
102:
103: protected PDFunction(COSObject object) {
104: super (object);
105: }
106:
107: abstract public float[] evaluate(float[] values);
108:
109: public float getDomainMax(int dimension) {
110: return ((COSNumber) cosGetDomain().get((dimension * 2) + 1))
111: .floatValue();
112: }
113:
114: public float getDomainMin(int dimension) {
115: return ((COSNumber) cosGetDomain().get(dimension * 2))
116: .floatValue();
117: }
118:
119: public int getInputSize() {
120: return cosGetDomain().size() / 2;
121: }
122:
123: abstract public int getOutputSize();
124:
125: public COSArray cosGetDomain() {
126: return cosGetDict().get(DK_Domain).asArray();
127: }
128:
129: public COSArray getRange() {
130: return cosGetDict().get(DK_Range).asArray();
131: }
132:
133: public float getRangeMax(int dimension) {
134: return ((COSNumber) getRange().get((dimension * 2) + 1))
135: .floatValue();
136: }
137:
138: public float getRangeMin(int dimension) {
139: return ((COSNumber) getRange().get(dimension * 2)).floatValue();
140: }
141:
142: protected float clip(float x, float min, float max) {
143: if (x < min) {
144: return min;
145: }
146: if (x > max) {
147: return max;
148: }
149: return x;
150: }
151:
152: protected float[] dummyResult() {
153: float[] result = new float[getOutputSize()];
154: for (int i = 0; i < result.length; i++) {
155: result[i] = 0;
156: }
157: return result;
158: }
159: }
|