KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > common > function > PDFunctionType0


1 /**
2  * Copyright (c) 2006, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdmodel.common.function;
32
33 import java.util.List JavaDoc;
34
35 import org.pdfbox.cos.COSArray;
36 import org.pdfbox.cos.COSFloat;
37 import org.pdfbox.cos.COSNull;
38 import org.pdfbox.pdmodel.PDDocument;
39 import org.pdfbox.pdmodel.common.COSArrayList;
40 import org.pdfbox.pdmodel.common.PDRange;
41 import org.pdfbox.pdmodel.common.PDStream;
42
43 /**
44  * This class represents a type 0 function in a PDF document.
45  *
46  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
47  * @version $Revision: 1.2 $
48  */

49 public class PDFunctionType0 extends PDStreamFunction
50 {
51
52     /**
53      * Constructor to create a new blank type 0 function.
54      *
55      * @param doc The document that the function will be part of.
56      */

57     protected PDFunctionType0( PDDocument doc )
58     {
59         super( doc, 0 );
60     }
61
62     /**
63      * Constructor.
64      *
65      * @param functionDictionary The prepopulated function dictionary.
66      */

67     public PDFunctionType0( PDStream functionDictionary )
68     {
69         super( functionDictionary );
70     }
71     
72     /**
73      * The "Size" entry, which is the number of samples in
74      * each input dimension of the sample table.
75      *
76      * @return A List of java.lang.Integer objects.
77      */

78     public List JavaDoc getNumberOfSamples()
79     {
80         List JavaDoc retval = null;
81         COSArray size = (COSArray)getCOSStream().getDictionaryObject( "Size" );
82         if( size != null )
83         {
84             retval = COSArrayList.convertIntegerCOSArrayToList( size );
85         }
86         return retval;
87     }
88     
89     /**
90      * Set the samples data, the "Size" entry in the type 0 function.
91      *
92      * @param samples The samples data.
93      */

94     public void setNumberOfSamples( List JavaDoc samples )
95     {
96         getCOSStream().setItem( "Size", COSArrayList.converterToCOSArray( samples ));
97     }
98     
99     /**
100      * Get the number of bits that the output value will take up. Valid values
101      * are 1,2,4,8,12,16,24,32.
102      *
103      * @return Number of bits for each output value.
104      */

105     public int getBitsPerSample()
106     {
107         return getCOSStream().getInt( "BitsPerSample" );
108     }
109     
110     /**
111      * Set the number of bits that the output value will take up. Valid values
112      * are 1,2,4,8,12,16,24,32.
113      *
114      * @param bps The number of bits for each output value.
115      */

116     public void setBitsPerSample( int bps )
117     {
118         getCOSStream().setInt( "BitsPerSample", bps );
119     }
120     
121     /**
122      * Get the encode for the input parameter.
123      *
124      * @param paramNum The function parameter number.
125      *
126      * @return The encode parameter range or null if none is set.
127      */

128     public PDRange getEncodeForParameter( int paramNum )
129     {
130         PDRange retval = null;
131         COSArray encode = (COSArray)getCOSStream().getDictionaryObject( "Encode" );
132         if( encode != null && encode.size() >= paramNum*2+1 )
133         {
134             retval = new PDRange(encode, paramNum );
135         }
136         return retval;
137     }
138     
139     /**
140      * Set the encode range for the param number.
141      *
142      * @param paramNum The parameter number to set then encode values.
143      *
144      * @param range The range value to set.
145      */

146     public void setEncodeForParameter( int paramNum, PDRange range )
147     {
148         COSArray encode = (COSArray)getCOSStream().getDictionaryObject( "Encode" );
149         if( encode == null )
150         {
151             encode = new COSArray();
152         }
153         for( int i=encode.size(); i<paramNum*2+1; i++)
154         {
155             encode.add( COSNull.NULL );
156         }
157         encode.set( paramNum*2, new COSFloat( range.getMin() ) );
158         encode.set( paramNum*2+1, new COSFloat( range.getMax() ) );
159     }
160     
161     /**
162      * Get the decode for the input parameter.
163      *
164      * @param paramNum The function parameter number.
165      *
166      * @return The decode parameter range or null if none is set.
167      */

168     public PDRange getDecodeForParameter( int paramNum )
169     {
170         PDRange retval = null;
171         COSArray encode = (COSArray)getCOSStream().getDictionaryObject( "Decode" );
172         if( encode != null && encode.size() >= paramNum*2+1 )
173         {
174             retval = new PDRange(encode, paramNum );
175         }
176         return retval;
177     }
178     
179     /**
180      * Set the decode range for the param number.
181      *
182      * @param paramNum The parameter number to set then decode values.
183      *
184      * @param range The range value to set.
185      */

186     public void setDecodeForParameter( int paramNum, PDRange range )
187     {
188         COSArray encode = (COSArray)getCOSStream().getDictionaryObject( "Decode" );
189         if( encode == null )
190         {
191             encode = new COSArray();
192         }
193         for( int i=encode.size(); i<paramNum*2+1; i++)
194         {
195             encode.add( COSNull.NULL );
196         }
197         encode.set( paramNum*2, new COSFloat( range.getMin() ) );
198         encode.set( paramNum*2+1, new COSFloat( range.getMax() ) );
199     }
200 }
Popular Tags