001: /**
002: * Copyright (c) 2006, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. 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: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdmodel.common.function;
031:
032: import java.util.List;
033:
034: import org.pdfbox.cos.COSArray;
035: import org.pdfbox.cos.COSFloat;
036: import org.pdfbox.cos.COSNull;
037: import org.pdfbox.pdmodel.PDDocument;
038: import org.pdfbox.pdmodel.common.COSArrayList;
039: import org.pdfbox.pdmodel.common.PDRange;
040: import org.pdfbox.pdmodel.common.PDStream;
041:
042: /**
043: * This class represents a type 0 function in a PDF document.
044: *
045: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
046: * @version $Revision: 1.2 $
047: */
048: public class PDFunctionType0 extends PDStreamFunction {
049:
050: /**
051: * Constructor to create a new blank type 0 function.
052: *
053: * @param doc The document that the function will be part of.
054: */
055: protected PDFunctionType0(PDDocument doc) {
056: super (doc, 0);
057: }
058:
059: /**
060: * Constructor.
061: *
062: * @param functionDictionary The prepopulated function dictionary.
063: */
064: public PDFunctionType0(PDStream functionDictionary) {
065: super (functionDictionary);
066: }
067:
068: /**
069: * The "Size" entry, which is the number of samples in
070: * each input dimension of the sample table.
071: *
072: * @return A List of java.lang.Integer objects.
073: */
074: public List getNumberOfSamples() {
075: List retval = null;
076: COSArray size = (COSArray) getCOSStream().getDictionaryObject(
077: "Size");
078: if (size != null) {
079: retval = COSArrayList.convertIntegerCOSArrayToList(size);
080: }
081: return retval;
082: }
083:
084: /**
085: * Set the samples data, the "Size" entry in the type 0 function.
086: *
087: * @param samples The samples data.
088: */
089: public void setNumberOfSamples(List samples) {
090: getCOSStream().setItem("Size",
091: COSArrayList.converterToCOSArray(samples));
092: }
093:
094: /**
095: * Get the number of bits that the output value will take up. Valid values
096: * are 1,2,4,8,12,16,24,32.
097: *
098: * @return Number of bits for each output value.
099: */
100: public int getBitsPerSample() {
101: return getCOSStream().getInt("BitsPerSample");
102: }
103:
104: /**
105: * Set the number of bits that the output value will take up. Valid values
106: * are 1,2,4,8,12,16,24,32.
107: *
108: * @param bps The number of bits for each output value.
109: */
110: public void setBitsPerSample(int bps) {
111: getCOSStream().setInt("BitsPerSample", bps);
112: }
113:
114: /**
115: * Get the encode for the input parameter.
116: *
117: * @param paramNum The function parameter number.
118: *
119: * @return The encode parameter range or null if none is set.
120: */
121: public PDRange getEncodeForParameter(int paramNum) {
122: PDRange retval = null;
123: COSArray encode = (COSArray) getCOSStream()
124: .getDictionaryObject("Encode");
125: if (encode != null && encode.size() >= paramNum * 2 + 1) {
126: retval = new PDRange(encode, paramNum);
127: }
128: return retval;
129: }
130:
131: /**
132: * Set the encode range for the param number.
133: *
134: * @param paramNum The parameter number to set then encode values.
135: *
136: * @param range The range value to set.
137: */
138: public void setEncodeForParameter(int paramNum, PDRange range) {
139: COSArray encode = (COSArray) getCOSStream()
140: .getDictionaryObject("Encode");
141: if (encode == null) {
142: encode = new COSArray();
143: }
144: for (int i = encode.size(); i < paramNum * 2 + 1; i++) {
145: encode.add(COSNull.NULL);
146: }
147: encode.set(paramNum * 2, new COSFloat(range.getMin()));
148: encode.set(paramNum * 2 + 1, new COSFloat(range.getMax()));
149: }
150:
151: /**
152: * Get the decode for the input parameter.
153: *
154: * @param paramNum The function parameter number.
155: *
156: * @return The decode parameter range or null if none is set.
157: */
158: public PDRange getDecodeForParameter(int paramNum) {
159: PDRange retval = null;
160: COSArray encode = (COSArray) getCOSStream()
161: .getDictionaryObject("Decode");
162: if (encode != null && encode.size() >= paramNum * 2 + 1) {
163: retval = new PDRange(encode, paramNum);
164: }
165: return retval;
166: }
167:
168: /**
169: * Set the decode range for the param number.
170: *
171: * @param paramNum The parameter number to set then decode values.
172: *
173: * @param range The range value to set.
174: */
175: public void setDecodeForParameter(int paramNum, PDRange range) {
176: COSArray encode = (COSArray) getCOSStream()
177: .getDictionaryObject("Decode");
178: if (encode == null) {
179: encode = new COSArray();
180: }
181: for (int i = encode.size(); i < paramNum * 2 + 1; i++) {
182: encode.add(COSNull.NULL);
183: }
184: encode.set(paramNum * 2, new COSFloat(range.getMin()));
185: encode.set(paramNum * 2 + 1, new COSFloat(range.getMax()));
186: }
187: }
|