KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: EPSReader.java,v 1.2.2.3 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.ByteArrayOutputStream JavaDoc;
56 import java.io.IOException JavaDoc;
57 import java.io.InputStream JavaDoc;
58
59 /**
60  * ImageReader object for SVG document image type.
61  */

62 public class EPSReader extends AbstractImageReader {
63     private long[] bbox;
64     private boolean isAscii; // True if plain ascii eps file
65

66     // offsets if not ascii
67
long psStart = 0;
68     long psLength = 0;
69     long wmfStart = 0;
70     long wmfLength = 0;
71     long tiffStart = 0;
72     long tiffLength = 0;
73
74     /** raw eps file */
75     private byte[] rawEps;
76     /** eps part */
77     private byte[] epsFile;
78     private byte[] preview = null;
79
80     private long getLong(byte[] buf, int idx) {
81         int b1 = buf[idx] & 0xff;
82         int b2 = buf[idx+1] & 0xff;
83         int b3 = buf[idx+2] & 0xff;
84         int b4 = buf[idx+3] & 0xff;
85
86         //return (long)((b1 << 24) | (b2 << 16) | (b3 << 8) | b4);
87
return (long)((b4 << 24) | (b3 << 16) | (b2 << 8) | b1);
88     }
89
90     public boolean verifySignature(String JavaDoc uri, BufferedInputStream JavaDoc fis)
91             throws IOException JavaDoc {
92         boolean isEPS = false;
93         this.imageStream = fis;
94         fis.mark(32);
95         byte[] header = new byte[30];
96         fis.read(header, 0, 30);
97         fis.reset();
98
99         // Check if binary header
100
//if (getLong(header, 0) == 0xC5D0D3C6) {
101
if (getLong(header, 0) == 0xC6D3D0C5) {
102             isAscii = false;
103             isEPS = true;
104
105             psStart = getLong(header, 4);
106             psLength = getLong(header, 8);
107             wmfStart = getLong(header, 12);
108             wmfLength = getLong(header, 16);
109             tiffStart = getLong(header, 20);
110             tiffLength = getLong(header, 24);
111
112         } else {
113             // Check if plain ascii
114
byte[] epsh = "%!PS".getBytes();
115             if (epsh[0] == header[0] &&
116                 epsh[1] == header[1] &&
117                 epsh[2] == header[2] &&
118                 epsh[3] == header[3]) {
119                 isAscii = true;
120                 isEPS = true;
121             }
122         }
123
124         if (isEPS) {
125             readEPSImage(fis);
126             bbox = readBBox();
127
128             if (bbox != null) {
129                 width = (int)(bbox[2]-bbox[0]);
130                 height = (int)(bbox[3]-bbox[1]);
131             } else {
132                 // Ain't eps if no BoundingBox
133
isEPS = false;
134             }
135         }
136
137         return isEPS;
138     }
139
140     /** read the eps file and extract eps part */
141     private void readEPSImage(BufferedInputStream JavaDoc fis) throws IOException JavaDoc {
142         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
143         byte[] file;
144         byte[] readBuf = new byte[20480];
145         int bytes_read;
146         int index = 0;
147         boolean cont = true;
148
149
150         try {
151             while ((bytes_read = fis.read(readBuf)) != -1) {
152                 baos.write(readBuf, 0, bytes_read);
153             }
154         } catch (java.io.IOException JavaDoc ex) {
155             throw new IOException JavaDoc("Error while loading EPS image " + ex.getMessage());
156         }
157
158         file = baos.toByteArray();
159
160         if (isAscii) {
161             rawEps = null;
162             epsFile = new byte[file.length];
163             System.arraycopy(file, 0, epsFile, 0, epsFile.length);
164         } else {
165             rawEps = new byte[file.length];
166             epsFile = new byte[(int)psLength];
167             System.arraycopy(file, 0, rawEps, 0, rawEps.length);
168             System.arraycopy(rawEps, (int)psStart, epsFile, 0, (int)psLength);
169         }
170     }
171
172     public byte[] getEpsFile() {
173         return epsFile;
174     }
175
176     /* Get embedded preview or null */
177     public byte[] getPreview() {
178         InputStream JavaDoc is = null;
179         if (preview == null) {
180             if (tiffLength > 0) {
181                 preview = new byte[(int)tiffLength];
182                 System.arraycopy(rawEps, (int)tiffStart, preview, 0, (int)tiffLength);
183             }
184         }
185         return preview;
186     }
187
188     /** Extract bounding box from eps part
189     */

190     private long[] readBBox() {
191         long[] mbbox = null;
192         int idx = 0;
193         byte[] bbxName = "%%BoundingBox: ".getBytes();
194         boolean found = false;
195
196         while (!found && (epsFile.length > (idx + bbxName.length))) {
197             boolean sfound = true;
198             int i = idx;
199             for (i = idx; sfound && (i-idx) < bbxName.length; i++) {
200                 if (bbxName[i - idx] != epsFile[i])
201                     sfound = false;
202             }
203             if (sfound) {
204                 found = true;
205                 idx = i;
206             } else {
207                 idx++;
208             }
209         }
210
211         if (!found)
212             return mbbox;
213
214
215         mbbox = new long[4];
216         idx += readLongString(mbbox, 0, idx);
217         idx += readLongString(mbbox, 1, idx);
218         idx += readLongString(mbbox, 2, idx);
219         idx += readLongString(mbbox, 3, idx);
220
221         return mbbox;
222     }
223
224     private int readLongString(long[] mbbox, int i, int idx) {
225         while (idx < epsFile.length &&
226                (epsFile[idx] == 32))
227                idx++;
228
229         int nidx = idx;
230
231         while (nidx < epsFile.length &&
232             ((epsFile[nidx] >= 48 && epsFile[nidx] <= 57) ||
233             (epsFile[nidx] == 45)))
234             nidx++;
235
236         byte[] num = new byte[nidx - idx];
237         System.arraycopy(epsFile, idx, num, 0, nidx-idx);
238         String JavaDoc ns = new String JavaDoc(num);
239         mbbox[i] = Long.parseLong(ns);
240
241         return (1+nidx - idx);
242     }
243
244     public String JavaDoc getMimeType() {
245         return "image/eps";
246     }
247
248     /**
249     * Return the BoundingBox
250     */

251     public int[] getBBox() {
252         int[] bbox = new int[4];
253         bbox[0] = (int)this.bbox[0];
254         bbox[1] = (int)this.bbox[1];
255         bbox[2] = (int)this.bbox[2];
256         bbox[3] = (int)this.bbox[3];
257         return bbox;
258     }
259 }
260
261
Popular Tags