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.COSNumber;
035: import de.intarsys.pdf.cos.COSObject;
036:
037: /**
038: * Function implementation for stitching functions.
039: */
040: public class PDStitchingFunction extends PDFunction {
041: /**
042: * The meta class implementation
043: */
044: static public class MetaClass extends PDFunction.MetaClass {
045: protected MetaClass(Class paramInstanceClass) {
046: super (paramInstanceClass);
047: }
048: }
049:
050: private static final COSName DK_Bounds = COSName.constant("Bounds"); //$NON-NLS-1$
051:
052: private static final COSName DK_Encode = COSName.constant("Encode"); //$NON-NLS-1$
053:
054: /** The meta class instance */
055: static public final MetaClass META = new MetaClass(MetaClass.class
056: .getDeclaringClass());
057:
058: private static final COSName DK_Functions = COSName
059: .constant("Functions"); //$NON-NLS-1$
060:
061: private float[] bounds;
062:
063: private float[] encode;
064:
065: private PDFunction[] functions;
066:
067: protected PDStitchingFunction(COSObject object) {
068: super (object);
069:
070: COSArray cosBounds;
071: COSArray cosEncode;
072: int index;
073:
074: cosBounds = object.asDictionary().get(DK_Bounds).asArray();
075: bounds = new float[cosBounds.size()];
076: for (index = 0; index < cosBounds.size(); index++) {
077: bounds[index] = ((COSNumber) cosBounds.get(index))
078: .floatValue();
079: }
080:
081: cosEncode = object.asDictionary().get(DK_Encode).asArray();
082: encode = new float[cosEncode.size()];
083: for (index = 0; index < cosEncode.size(); index++) {
084: encode[index] = ((COSNumber) cosEncode.get(index))
085: .floatValue();
086: }
087:
088: // color space will be resolved lazily
089: }
090:
091: public float[] evaluate(float[] values) {
092: int index;
093:
094: for (index = 0; index < bounds.length; index++) {
095: if (values[0] < bounds[index]) {
096: return getFunctions()[index].evaluate(values);
097: }
098: }
099: return getFunctions()[index].evaluate(values);
100: }
101:
102: public int getOutputSize() {
103: return getRange().size() / 2;
104: }
105:
106: public float[] getBounds() {
107: return bounds;
108: }
109:
110: public float[] getEncode() {
111: return encode;
112: }
113:
114: public PDFunction[] getFunctions() {
115: if (functions == null) {
116: COSArray cosFunctions;
117: int index;
118:
119: cosFunctions = cosGetObject().asDictionary().get(
120: DK_Functions).asArray();
121: functions = new PDFunction[cosFunctions.size()];
122: for (index = 0; index < cosFunctions.size(); index++) {
123: functions[index] = (PDFunction) PDFunction.META
124: .createFromCos(cosFunctions.get(index));
125: }
126: }
127: return functions;
128: }
129: }
|