001: /**
002: * Copyright (c) 2004, 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.util;
031:
032: import org.pdfbox.cos.COSArray;
033: import org.pdfbox.cos.COSBase;
034: import org.pdfbox.cos.COSDictionary;
035: import org.pdfbox.cos.COSInteger;
036: import org.pdfbox.cos.COSName;
037: import org.pdfbox.cos.COSNumber;
038:
039: import org.pdfbox.pdmodel.common.COSArrayList;
040: import org.pdfbox.pdmodel.graphics.color.PDColorSpace;
041: import org.pdfbox.pdmodel.graphics.color.PDColorSpaceFactory;
042:
043: import java.io.IOException;
044: import java.util.List;
045:
046: /**
047: * This contains all of the image parameters for in inlined image.
048: *
049: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
050: * @version $Revision: 1.4 $
051: */
052: public class ImageParameters {
053: private COSDictionary dictionary;
054:
055: /**
056: * Constructor.
057: */
058: public ImageParameters() {
059: dictionary = new COSDictionary();
060: }
061:
062: /**
063: * Constructor.
064: *
065: * @param params The image parameters.
066: */
067: public ImageParameters(COSDictionary params) {
068: dictionary = params;
069: }
070:
071: /**
072: * This will get the dictionary that stores the image parameters.
073: *
074: * @return The COS dictionary that stores the image parameters.
075: */
076: public COSDictionary getDictionary() {
077: return dictionary;
078: }
079:
080: private COSBase getCOSObject(String abbreviatedName, String name) {
081: COSBase retval = dictionary.getDictionaryObject(COSName
082: .getPDFName(abbreviatedName));
083: if (retval == null) {
084: retval = dictionary.getDictionaryObject(COSName
085: .getPDFName(name));
086: }
087: return retval;
088: }
089:
090: private int getNumberOrNegativeOne(String abbreviatedName,
091: String name) {
092: int retval = -1;
093: COSNumber number = (COSNumber) getCOSObject(abbreviatedName,
094: name);
095: if (number != null) {
096: retval = number.intValue();
097: }
098: return retval;
099: }
100:
101: /**
102: * The bits per component of this image. This will return -1 if one has not
103: * been set.
104: *
105: * @return The number of bits per component.
106: */
107: public int getBitsPerComponent() {
108: return getNumberOrNegativeOne("BPC", "BitsPerComponent");
109: }
110:
111: /**
112: * Set the number of bits per component.
113: *
114: * @param bpc The number of bits per component.
115: */
116: public void setBitsPerComponent(int bpc) {
117: dictionary.setItem(COSName.getPDFName("BPC"), new COSInteger(
118: bpc));
119: }
120:
121: /**
122: * This will get the color space or null if none exists.
123: *
124: * @return The color space for this image.
125: *
126: * @throws IOException If there is an error getting the colorspace.
127: */
128: public PDColorSpace getColorSpace() throws IOException {
129: COSBase cs = getCOSObject("CS", "ColorSpace");
130: PDColorSpace retval = null;
131: if (cs != null) {
132: retval = PDColorSpaceFactory.createColorSpace(cs);
133: }
134: return retval;
135: }
136:
137: /**
138: * This will set the color space for this image.
139: *
140: * @param cs The color space for this image.
141: */
142: public void setColorSpace(PDColorSpace cs) {
143: COSBase base = null;
144: if (cs != null) {
145: base = cs.getCOSObject();
146: }
147: dictionary.setItem(COSName.getPDFName("CS"), base);
148: }
149:
150: /**
151: * The height of this image. This will return -1 if one has not
152: * been set.
153: *
154: * @return The height.
155: */
156: public int getHeight() {
157: return getNumberOrNegativeOne("H", "Height");
158: }
159:
160: /**
161: * Set the height of the image.
162: *
163: * @param h The height of the image.
164: */
165: public void setHeight(int h) {
166: dictionary.setItem(COSName.getPDFName("H"), new COSInteger(h));
167: }
168:
169: /**
170: * The width of this image. This will return -1 if one has not
171: * been set.
172: *
173: * @return The width.
174: */
175: public int getWidth() {
176: return getNumberOrNegativeOne("W", "Width");
177: }
178:
179: /**
180: * Set the width of the image.
181: *
182: * @param w The width of the image.
183: */
184: public void setWidth(int w) {
185: dictionary.setItem(COSName.getPDFName("W"), new COSInteger(w));
186: }
187:
188: /**
189: * This will get the list of filters that are associated with this stream. Or
190: * null if there are none.
191: * @return A list of all encoding filters to apply to this stream.
192: */
193: public List getFilters() {
194: List retval = null;
195: COSBase filters = dictionary.getDictionaryObject(new String[] {
196: "Filter", "F" });
197: if (filters instanceof COSName) {
198: COSName name = (COSName) filters;
199: retval = new COSArrayList(name.getName(), name, dictionary,
200: "Filter");
201: } else if (filters instanceof COSArray) {
202: retval = COSArrayList
203: .convertCOSNameCOSArrayToList((COSArray) filters);
204: }
205: return retval;
206: }
207:
208: /**
209: * This will set the filters that are part of this stream.
210: *
211: * @param filters The filters that are part of this stream.
212: */
213: public void setFilters(List filters) {
214: COSBase obj = COSArrayList
215: .convertStringListToCOSNameCOSArray(filters);
216: dictionary.setItem("Filter", obj);
217: }
218: }
|