KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > image > analyser > JPEGReader


1 /*
2  * $Id: JPEGReader.java,v 1.3.2.1 2003/02/25 13:38:24 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.image.analyser;
52
53 // Java
54
import java.io.BufferedInputStream JavaDoc;
55 import java.io.IOException JavaDoc;
56
57 /**
58  * ImageReader object for JPEG image type.
59  * @author Pankaj Narula
60  * @version 1.0
61  */

62 public class JPEGReader extends AbstractImageReader {
63
64     /**
65      * Only SOFn and APPn markers are defined as SOFn is needed for the height and
66      * width search. APPn is also defined because if the JPEG contains thumbnails
67      * the dimensions of the thumnail would also be after the SOFn marker enclosed
68      * inside the APPn marker. And we don't want to confuse those dimensions with
69      * the image dimensions.
70      */

71     protected static final int MARK = 0xff; // Beginneing of a Marker
72
protected static final int NULL = 0x00; // Special case for 0xff00
73
protected static final int SOF1 = 0xc0; // Baseline DCT
74
protected static final int SOF2 = 0xc1; // Extended Sequential DCT
75
protected static final int SOF3 = 0xc2; // Progrssive DCT only PDF 1.3
76
protected static final int SOFA = 0xca; // Progressice DCT only PDF 1.3
77
protected static final int APP0 = 0xe0; // Application marker, JFIF
78
protected static final int APPF = 0xef; // Application marker
79
protected static final int SOS = 0xda; // Start of Scan
80
protected static final int SOI = 0xd8; // start of Image
81
protected static final int JPG_SIG_LENGTH = 2;
82
83     protected byte[] header;
84
85     public boolean verifySignature(String JavaDoc uri, BufferedInputStream JavaDoc fis)
86             throws IOException JavaDoc {
87         this.imageStream = fis;
88         this.setDefaultHeader();
89         boolean supported = ((header[0] == (byte)0xff)
90                              && (header[1] == (byte)0xd8));
91         if (supported) {
92             setDimension();
93             return true;
94         } else
95             return false;
96     }
97
98     public String JavaDoc getMimeType() {
99         return "image/jpeg";
100     }
101
102     protected void setDefaultHeader() throws IOException JavaDoc {
103         this.header = new byte[JPG_SIG_LENGTH];
104         try {
105             this.imageStream.mark(JPG_SIG_LENGTH + 1);
106             this.imageStream.read(header);
107             this.imageStream.reset();
108         } catch (IOException JavaDoc ex) {
109             try {
110                 this.imageStream.reset();
111             } catch (IOException JavaDoc exbis) {}
112             throw ex;
113         }
114     }
115
116     protected void setDimension() throws IOException JavaDoc {
117         try {
118             int marker = NULL;
119             long length, skipped;
120             outer:
121             while (imageStream.available() > 0) {
122                 while ((marker = imageStream.read()) != MARK) {
123                     ;
124                 }
125                 do {
126                     marker = imageStream.read();
127                 } while (marker == MARK);
128                 switch (marker) {
129                 case SOI:
130                     break;
131                 case NULL:
132                     break;
133                 case SOF1:
134                 case SOF2:
135                 case SOF3: // SOF3 and SOFA are only supported by PDF 1.3
136
case SOFA:
137                     this.skip(3);
138                     this.height = this.read2bytes();
139                     this.width = this.read2bytes();
140                     break outer;
141                 default:
142                     length = this.read2bytes();
143                     skipped = this.skip(length - 2);
144                     if (skipped != length - 2)
145                         throw new IOException JavaDoc("Skipping Error");
146                 }
147             }
148         } catch (IOException JavaDoc ioe) {
149             try {
150                 this.imageStream.reset();
151             } catch (IOException JavaDoc exbis) {}
152             throw ioe;
153         }
154     }
155
156     protected int read2bytes() throws IOException JavaDoc {
157         int byte1 = imageStream.read();
158         int byte2 = imageStream.read();
159         return (int)((byte1 << 8) | byte2);
160     }
161
162     protected long skip(long n) throws IOException JavaDoc {
163         long discarded = 0;
164         while (discarded != n) {
165             imageStream.read();
166             discarded++;
167         }
168         return discarded; // scope for exception
169
}
170
171 }
172
173
Popular Tags