aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/image/analyser/EPSReader.java
blob: eba620feeb5ec95fd0387c54a603016c9570bf98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */
 
package org.apache.fop.image.analyser;

// Java
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

// FOP
import org.apache.commons.io.IOUtils;
import org.apache.fop.image.FopImage;
import org.apache.fop.image.EPSImage;
import org.apache.fop.apps.FOUserAgent;

/**
 * ImageReader object for EPS document image type.
 *
 * @version   $Id$
 */
public class EPSReader implements ImageReader {

    private static final byte[] EPS_HEADER_ASCII = "%!PS".getBytes();
    private static final byte[] BOUNDINGBOX = "%%BoundingBox: ".getBytes();
    //private static final byte[] HIRESBOUNDINGBOX = "%%HiResBoundingBox: ".getBytes();
    //TODO Implement HiResBoundingBox, ImageInfo probably needs some changes for that

    /** @see org.apache.fop.image.analyser.ImageReader */
    public FopImage.ImageInfo verifySignature(String uri, InputStream bis,
                FOUserAgent ua) throws IOException {

        boolean isEPS = false;

        bis.mark(32);
        byte[] header = new byte[30];
        bis.read(header, 0, 30);
        bis.reset();

        EPSImage.EPSData data = new EPSImage.EPSData();

        // Check if binary header
        if (getLong(header, 0) == 0xC6D3D0C5) {
            data.isAscii = false;
            isEPS = true;

            data.psStart = getLong(header, 4);
            data.psLength = getLong(header, 8);
            data.wmfStart = getLong(header, 12);
            data.wmfLength = getLong(header, 16);
            data.tiffStart = getLong(header, 20);
            data.tiffLength = getLong(header, 24);

        } else {
            // Check if plain ascii
            byte[] epsh = "%!PS".getBytes();
            if (EPS_HEADER_ASCII[0] == header[0]
                    && EPS_HEADER_ASCII[1] == header[1]
                    && EPS_HEADER_ASCII[2] == header[2]
                    && EPS_HEADER_ASCII[3] == header[3]) {
                data.isAscii = true;
                isEPS = true;
            }
        }

        if (isEPS) {
            FopImage.ImageInfo info = new FopImage.ImageInfo();
            info.originalURI = uri;
            info.mimeType = getMimeType();
            info.data = data;
            readEPSImage(bis, data);
            data.bbox = readBBox(data);

            if (data.bbox != null) {
                info.width = (int) (data.bbox[2] - data.bbox[0]);
                info.height = (int) (data.bbox[3] - data.bbox[1]);

                // image data read
                IOUtils.closeQuietly(bis);
                info.inputStream = null;

                return info;
            } else {
                // Ain't eps if no BoundingBox
                isEPS = false;
            }
        }

        return null;
    }

    /**
     * Returns the MIME type supported by this implementation.
     *
     * @return   The MIME type
     */
    public String getMimeType() {
        return "image/eps";
    }

    private long getLong(byte[] buf, int idx) {
        int b1 = buf[idx] & 0xff;
        int b2 = buf[idx + 1] & 0xff;
        int b3 = buf[idx + 2] & 0xff;
        int b4 = buf[idx + 3] & 0xff;

        return (long) ((b4 << 24) | (b3 << 16) | (b2 << 8) | b1);
    }

    /**
     * Read the eps file and extract eps part.
     *
     * @param bis              The InputStream
     * @param data             EPSData object to write the results to
     * @exception IOException  If an I/O error occurs
     */
    private void readEPSImage(InputStream bis, EPSImage.EPSData data)
                throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] file;
        byte[] readBuf = new byte[20480];
        int bytesRead;
        int index = 0;
        boolean cont = true;

        try {
            while ((bytesRead = bis.read(readBuf)) != -1) {
                baos.write(readBuf, 0, bytesRead);
            }
        } catch (java.io.IOException ex) {
            throw new IOException("Error while loading EPS image: "
                    + ex.getMessage());
        }

        file = baos.toByteArray();

        if (data.isAscii) {
            data.rawEps = null;
            data.epsFile = new byte[file.length];
            System.arraycopy(file, 0, data.epsFile, 0, data.epsFile.length);
        } else {
            data.rawEps = new byte[file.length];
            data.epsFile = new byte[(int) data.psLength];
            System.arraycopy(file, 0, data.rawEps, 0, data.rawEps.length);
            System.arraycopy(data.rawEps, (int) data.psStart, data.epsFile, 0,
                    (int) data.psLength);
        }
    }

    /**
     * Get embedded TIFF preview or null.
     *
     * @param data  The EPS payload
     * @return      The embedded preview
     */
    public byte[] getPreview(EPSImage.EPSData data) {
        if (data.preview == null) {
            if (data.tiffLength > 0) {
                data.preview = new byte[(int) data.tiffLength];
                System.arraycopy(data.rawEps, (int) data.tiffStart, data.preview, 0,
                        (int) data.tiffLength);
            }
        }
        return data.preview;
    }

    /**
     * Extract bounding box from eps part.
     *
     * @param data  The EPS payload
     * @return      An Array of four coordinates making up the bounding box
     */
    private long[] readBBox(EPSImage.EPSData data) {
        long[] mbbox = null;
        int idx = 0;
        boolean found = false;

        while (!found && (data.epsFile.length > (idx + BOUNDINGBOX.length))) {
            boolean sfound = true;
            int i = idx;
            for (i = idx; sfound && (i - idx) < BOUNDINGBOX.length; i++) {
                if (BOUNDINGBOX[i - idx] != data.epsFile[i]) {
                    sfound = false;
                }
            }
            if (sfound) {
                found = true;
                idx = i;
            } else {
                idx++;
            }
        }

        if (!found) {
            return mbbox;
        }

        mbbox = new long[4];
        idx += readLongString(data, mbbox, 0, idx);
        idx += readLongString(data, mbbox, 1, idx);
        idx += readLongString(data, mbbox, 2, idx);
        idx += readLongString(data, mbbox, 3, idx);

        return mbbox;
    }

    private int readLongString(EPSImage.EPSData data, long[] mbbox, int i, int idx) {
        while (idx < data.epsFile.length && (data.epsFile[idx] == 32)) {
            idx++;
        }

        int nidx = idx;

        // check also for ANSI46(".") to identify floating point values
        while (nidx < data.epsFile.length
                && ((data.epsFile[nidx] >= 48 && data.epsFile[nidx] <= 57)
                || (data.epsFile[nidx] == 45)
                || (data.epsFile[nidx] == 46))) {
            nidx++;
        }

        byte[] num = new byte[nidx - idx];
        System.arraycopy(data.epsFile, idx, num, 0, nidx - idx);
        String ns = new String(num);

        //if( ns.indexOf(".") != -1 ) {
        // do something like logging a warning
        //}

        // then parse the double and round off to the next math. Integer
        mbbox[i] = (long) Math.ceil(Double.parseDouble(ns));

        return (1 + nidx - idx);
    }

}