001: /*
002: * $Id: PDFDecoder.java,v 1.2 2007/12/20 18:33:33 rbair Exp $
003: *
004: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020: */
021:
022: package com.sun.pdfview.decode;
023:
024: import java.io.IOException;
025: import java.nio.ByteBuffer;
026:
027: import com.sun.pdfview.PDFObject;
028: import com.sun.pdfview.PDFParseException;
029:
030: /**
031: * A PDF Decoder encapsulates all the methods of decoding a stream of bytes
032: * based on all the various encoding methods.
033: * <p>
034: * You should use the decodeStream() method of this object rather than using
035: * any of the decoders directly.
036: */
037: public class PDFDecoder {
038:
039: /** Creates a new instance of PDFDecoder */
040: private PDFDecoder() {
041: }
042:
043: /**
044: * decode a byte[] stream using the filters specified in the object's
045: * dictionary (passed as argument 1).
046: * @param dict the dictionary associated with the stream
047: * @param streamBuf the data in the stream, as a byte buffer
048: */
049: public static ByteBuffer decodeStream(PDFObject dict,
050: ByteBuffer streamBuf) throws IOException {
051: PDFObject encoding = dict.getDictRef("Filter");
052: if (encoding != null) {
053: // it's the name of a filter, or an array of filter names
054: PDFObject ary[];
055: PDFObject params[];
056: if (encoding.getType() == PDFObject.NAME) {
057: ary = new PDFObject[1];
058: ary[0] = encoding;
059: params = new PDFObject[1];
060: params[0] = dict.getDictRef("DecodeParms");
061: } else {
062: ary = encoding.getArray();
063: PDFObject parmsobj = dict.getDictRef("DecodeParms");
064: if (parmsobj != null) {
065: params = parmsobj.getArray();
066: } else {
067: params = new PDFObject[ary.length];
068: }
069: }
070: for (int i = 0; i < ary.length; i++) {
071: String enctype = ary[i].getStringValue();
072: if (enctype == null) {
073: } else if (enctype.equals("FlateDecode")
074: || enctype.equals("Fl")) {
075: streamBuf = FlateDecode.decode(dict, streamBuf,
076: params[i]);
077: } else if (enctype.equals("LZWDecode")
078: || enctype.equals("LZW")) {
079: streamBuf = LZWDecode.decode(streamBuf, params[i]);
080: } else if (enctype.equals("ASCII85Decode")
081: || enctype.equals("A85")) {
082: streamBuf = ASCII85Decode.decode(streamBuf,
083: params[i]);
084: } else if (enctype.equals("ASCIIHexDecode")
085: || enctype.equals("AHx")) {
086: streamBuf = ASCIIHexDecode.decode(streamBuf,
087: params[i]);
088: } else if (enctype.equals("DCTDecode")
089: || enctype.equals("DCT")) {
090: streamBuf = DCTDecode.decode(dict, streamBuf,
091: params[i]);
092: } else if (enctype.equals("CCITTFaxDecode")
093: || enctype.equals("CCF")) {
094: streamBuf = CCITTFaxDecode.decode(dict, streamBuf,
095: params[i]);
096: } else {
097: throw new PDFParseException(
098: "Unknown coding method:"
099: + ary[i].getStringValue());
100: }
101: }
102: }
103:
104: return streamBuf;
105: }
106: }
|