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.pdmodel.graphics.xobject;
031:
032: import java.awt.image.BufferedImage;
033: import java.io.InputStream;
034: import java.io.IOException;
035: import java.io.OutputStream;
036:
037: import java.util.ArrayList;
038: import java.util.List;
039:
040: import javax.imageio.ImageIO;
041:
042: import org.pdfbox.cos.COSDictionary;
043: import org.pdfbox.cos.COSName;
044:
045: import org.pdfbox.pdmodel.PDDocument;
046: import org.pdfbox.pdmodel.common.PDStream;
047: import org.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
048:
049: /**
050: * An image class for JPegs.
051: *
052: * @author mathiak
053: * @version $Revision: 1.5 $
054: */
055: public class PDJpeg extends PDXObjectImage {
056:
057: private static final List DCT_FILTERS = new ArrayList();
058:
059: static {
060: DCT_FILTERS.add(COSName.DCT_DECODE.getName());
061: DCT_FILTERS.add(COSName.DCT_DECODE_ABBREVIATION.getName());
062: }
063:
064: /**
065: * Standard constructor.
066: *
067: * @param jpeg The COSStream from which to extract the JPeg
068: */
069: public PDJpeg(PDStream jpeg) {
070: super (jpeg, "jpg");
071: }
072:
073: /**
074: * Construct from a stream.
075: *
076: * @param doc The document to create the image as part of.
077: * @param is The stream that contains the jpeg data.
078: * @throws IOException If there is an error reading the jpeg data.
079: */
080: public PDJpeg(PDDocument doc, InputStream is) throws IOException {
081: super (new PDStream(doc, is, true), "jpg");
082: COSDictionary dic = getCOSStream();
083: dic.setItem(COSName.FILTER, COSName.DCT_DECODE);
084: dic.setItem(COSName.SUBTYPE, COSName.IMAGE);
085: dic.setItem(COSName.TYPE, COSName.getPDFName("XObject"));
086:
087: BufferedImage image = getRGBImage();
088: setBitsPerComponent(8);
089: setColorSpace(PDDeviceRGB.INSTANCE);
090: setHeight(image.getHeight());
091: setWidth(image.getWidth());
092:
093: }
094:
095: /**
096: * Construct from a buffered image.
097: *
098: * @param doc The document to create the image as part of.
099: * @param bi The image to convert to a jpeg
100: * @throws IOException If there is an error processing the jpeg data.
101: */
102: public PDJpeg(PDDocument doc, BufferedImage bi) throws IOException {
103: super (new PDStream(doc), "jpg");
104:
105: java.io.OutputStream os = getCOSStream().createFilteredStream();
106: try {
107:
108: ImageIO.write(bi, "jpeg", os);
109:
110: COSDictionary dic = getCOSStream();
111: dic.setItem(COSName.FILTER, COSName.DCT_DECODE);
112: dic.setItem(COSName.SUBTYPE, COSName.IMAGE);
113: dic.setItem(COSName.TYPE, COSName.getPDFName("XObject"));
114:
115: setBitsPerComponent(8);
116: setColorSpace(PDDeviceRGB.INSTANCE);
117: setHeight(bi.getHeight());
118: setWidth(bi.getWidth());
119: } finally {
120: os.close();
121: }
122: }
123:
124: /**
125: * Returns an image of the JPeg, or null if JPegs are not supported. (They should be. )
126: * {@inheritDoc}
127: */
128: public BufferedImage getRGBImage() throws IOException {
129: return ImageIO.read(getPDStream().getPartiallyFilteredStream(
130: DCT_FILTERS));
131: }
132:
133: /**
134: * This writes the JPeg to out.
135: * {@inheritDoc}
136: */
137: public void write2OutputStream(OutputStream out) throws IOException {
138: InputStream data = getPDStream().getPartiallyFilteredStream(
139: DCT_FILTERS);
140: byte[] buf = new byte[1024];
141: int amountRead = -1;
142: while ((amountRead = data.read(buf)) != -1) {
143: out.write(buf, 0, amountRead);
144: }
145: }
146: }
|