001: /**
002: * Copyright (c) 2004-2005, 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.graphics.xobject;
031:
032: import java.awt.image.BufferedImage;
033: import java.io.FileOutputStream;
034: import java.io.IOException;
035: import java.io.OutputStream;
036:
037: import org.pdfbox.cos.COSBase;
038: import org.pdfbox.cos.COSName;
039: import org.pdfbox.pdmodel.PDDocument;
040: import org.pdfbox.pdmodel.common.PDStream;
041: import org.pdfbox.pdmodel.graphics.color.PDColorSpace;
042: import org.pdfbox.pdmodel.graphics.color.PDColorSpaceFactory;
043: import org.pdfbox.pdmodel.graphics.color.PDDeviceGray;
044:
045: /**
046: * The prototype for all PDImages.
047: *
048: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
049: * @author mathiak
050: * @version $Revision: 1.9 $
051: */
052: public abstract class PDXObjectImage extends PDXObject {
053: /**
054: * The XObject subtype.
055: */
056: public static final String SUB_TYPE = "Image";
057:
058: /**
059: * This contains the suffix used when writing to file.
060: */
061: private String suffix;
062:
063: /**
064: * Standard constuctor.
065: *
066: * @param imageStream The XObject is passed as a COSStream.
067: * @param fileSuffix The file suffix, jpg/png.
068: */
069: public PDXObjectImage(PDStream imageStream, String fileSuffix) {
070: super (imageStream);
071: suffix = fileSuffix;
072: }
073:
074: /**
075: * Standard constuctor.
076: *
077: * @param doc The document to store the stream in.
078: * @param fileSuffix The file suffix, jpg/png.
079: */
080: public PDXObjectImage(PDDocument doc, String fileSuffix) {
081: super (doc);
082: getCOSStream().setName(COSName.SUBTYPE, SUB_TYPE);
083: suffix = fileSuffix;
084: }
085:
086: /**
087: * Returns an java.awt.Image, that can be used for display etc.
088: *
089: * @return This PDF object as an AWT image.
090: *
091: * @throws IOException If there is an error creating the image.
092: */
093: public abstract BufferedImage getRGBImage() throws IOException;
094:
095: /**
096: * Writes the Image to out.
097: * @param out the OutputStream that the Image is written to.
098: * @throws IOException when somethings wrong with out
099: */
100: public abstract void write2OutputStream(OutputStream out)
101: throws IOException;
102:
103: /**
104: * Writes the image to a file with the filename + an appropriate suffix, like "Image.jpg".
105: * The suffix is automatically set by the
106: * @param filename the filename
107: * @throws IOException When somethings wrong with the corresponding file.
108: */
109: public void write2file(String filename) throws IOException {
110: FileOutputStream out = null;
111: try {
112: out = new FileOutputStream(filename + "." + suffix);
113: write2OutputStream(out);
114: out.flush();
115: } finally {
116: if (out != null) {
117: out.close();
118: }
119: }
120: }
121:
122: /**
123: * Get the height of the image.
124: *
125: * @return The height of the image.
126: */
127: public int getHeight() {
128: return getCOSStream().getInt("Height", -1);
129: }
130:
131: /**
132: * Set the height of the image.
133: *
134: * @param height The height of the image.
135: */
136: public void setHeight(int height) {
137: getCOSStream().setInt("Height", height);
138: }
139:
140: /**
141: * Get the width of the image.
142: *
143: * @return The width of the image.
144: */
145: public int getWidth() {
146: return getCOSStream().getInt("Width", -1);
147: }
148:
149: /**
150: * Set the width of the image.
151: *
152: * @param width The width of the image.
153: */
154: public void setWidth(int width) {
155: getCOSStream().setInt("Width", width);
156: }
157:
158: /**
159: * The bits per component of this image. This will return -1 if one has not
160: * been set.
161: *
162: * @return The number of bits per component.
163: */
164: public int getBitsPerComponent() {
165: return getCOSStream().getInt(
166: new String[] { "BPC", "BitsPerComponent" }, -1);
167: }
168:
169: /**
170: * Set the number of bits per component.
171: *
172: * @param bpc The number of bits per component.
173: */
174: public void setBitsPerComponent(int bpc) {
175: getCOSStream().setInt("BitsPerComponent", bpc);
176: }
177:
178: /**
179: * This will get the color space or null if none exists.
180: *
181: * @return The color space for this image.
182: *
183: * @throws IOException If there is an error getting the colorspace.
184: */
185: public PDColorSpace getColorSpace() throws IOException {
186: COSBase cs = getCOSStream().getDictionaryObject(
187: new String[] { "CS", "ColorSpace" });
188: PDColorSpace retval = null;
189: if (cs != null) {
190: retval = PDColorSpaceFactory.createColorSpace(cs);
191: } else {
192: //there are some cases where the 'required' CS value is not present
193: //but we know that it will be grayscale for a CCITT filter.
194: COSBase filter = getCOSStream().getDictionaryObject(
195: "Filter");
196: if (COSName.CCITTFAX_DECODE.equals(filter)
197: || COSName.CCITTFAX_DECODE_ABBREVIATION
198: .equals(filter)) {
199: retval = new PDDeviceGray();
200: }
201: }
202: return retval;
203: }
204:
205: /**
206: * This will set the color space for this image.
207: *
208: * @param cs The color space for this image.
209: */
210: public void setColorSpace(PDColorSpace cs) {
211: COSBase base = null;
212: if (cs != null) {
213: base = cs.getCOSObject();
214: }
215: getCOSStream().setItem(COSName.getPDFName("ColorSpace"), base);
216: }
217:
218: /**
219: * This will get the suffix for this image type, jpg/png.
220: *
221: * @return The image suffix.
222: */
223: public String getSuffix() {
224: return suffix;
225: }
226: }
|