KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: EMFReader.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.image.analyser;
21
22 // Java
23
import java.io.InputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 // FOP
27
import org.apache.fop.image.FopImage;
28 import org.apache.fop.apps.FOUserAgent;
29
30 /**
31  * ImageReader object for EMF image type.
32  *
33  * @author Peter Herweg
34  */

35 public class EMFReader implements ImageReader {
36
37     /** Length of the EMF header */
38     protected static final int EMF_SIG_LENGTH = 88;
39     
40     /** offset to signature */
41     private static final int SIGNATURE_OFFSET = 40;
42     /** offset to width */
43     private static final int WIDTH_OFFSET = 32;
44     /** offset to height */
45     private static final int HEIGHT_OFFSET = 36;
46     /** offset to horizontal resolution in pixel */
47     private static final int HRES_PIXEL_OFFSET = 72;
48     /** offset to vertical resolution in pixel */
49     private static final int VRES_PIXEL_OFFSET = 76;
50     /** offset to horizontal resolution in mm */
51     private static final int HRES_MM_OFFSET = 80;
52     /** offset to vertical resolution in mm */
53     private static final int VRES_MM_OFFSET = 84;
54
55     /** @see org.apache.fop.image.analyser.ImageReader */
56     public FopImage.ImageInfo verifySignature(String JavaDoc uri, InputStream JavaDoc bis,
57                 FOUserAgent ua) throws IOException JavaDoc {
58         byte[] header = getDefaultHeader(bis);
59         boolean supported
60                 = ( (header[SIGNATURE_OFFSET + 0] == (byte) 0x20)
61                 && (header[SIGNATURE_OFFSET + 1] == (byte) 0x45)
62                 && (header[SIGNATURE_OFFSET + 2] == (byte) 0x4D)
63                 && (header[SIGNATURE_OFFSET + 3] == (byte) 0x46) );
64         
65         if (supported) {
66             FopImage.ImageInfo info = getDimension(header);
67             info.originalURI = uri;
68             info.mimeType = getMimeType();
69             info.inputStream = bis;
70             return info;
71         } else {
72             return null;
73         }
74     }
75
76     /**
77      * Returns the MIME type supported by this implementation.
78      *
79      * @return The MIME type
80      */

81     public String JavaDoc getMimeType() {
82         return "image/emf";
83     }
84
85     private FopImage.ImageInfo getDimension(byte[] header) {
86         FopImage.ImageInfo info = new FopImage.ImageInfo();
87         long value = 0;
88         int byte1;
89         int byte2;
90         int byte3;
91         int byte4;
92         
93         // little endian notation
94

95         //resolution
96
byte1 = header[HRES_MM_OFFSET] & 0xff;
97         byte2 = header[HRES_MM_OFFSET + 1] & 0xff;
98         byte3 = header[HRES_MM_OFFSET + 2] & 0xff;
99         byte4 = header[HRES_MM_OFFSET + 3] & 0xff;
100         long hresMM = (long) ((byte4 << 24) | (byte3 << 16) | (byte2 << 8) | byte1);
101         
102         byte1 = header[VRES_MM_OFFSET] & 0xff;
103         byte2 = header[VRES_MM_OFFSET + 1] & 0xff;
104         byte3 = header[VRES_MM_OFFSET + 2] & 0xff;
105         byte4 = header[VRES_MM_OFFSET + 3] & 0xff;
106         long vresMM = (long) ((byte4 << 24) | (byte3 << 16) | (byte2 << 8) | byte1);
107         
108         byte1 = header[HRES_PIXEL_OFFSET] & 0xff;
109         byte2 = header[HRES_PIXEL_OFFSET + 1] & 0xff;
110         byte3 = header[HRES_PIXEL_OFFSET + 2] & 0xff;
111         byte4 = header[HRES_PIXEL_OFFSET + 3] & 0xff;
112         long hresPixel = (long) ((byte4 << 24) | (byte3 << 16) | (byte2 << 8) | byte1);
113         
114         byte1 = header[VRES_PIXEL_OFFSET] & 0xff;
115         byte2 = header[VRES_PIXEL_OFFSET + 1] & 0xff;
116         byte3 = header[VRES_PIXEL_OFFSET + 2] & 0xff;
117         byte4 = header[VRES_PIXEL_OFFSET + 3] & 0xff;
118         long vresPixel = (long) ((byte4 << 24) | (byte3 << 16) | (byte2 << 8) | byte1);
119         
120         info.dpiHorizontal = hresPixel / (hresMM / 25.4f);
121         info.dpiVertical = vresPixel / (vresMM / 25.4f);
122         
123         //width
124
byte1 = header[WIDTH_OFFSET] & 0xff;
125         byte2 = header[WIDTH_OFFSET + 1] & 0xff;
126         byte3 = header[WIDTH_OFFSET + 2] & 0xff;
127         byte4 = header[WIDTH_OFFSET + 3] & 0xff;
128         value = (long) ((byte4 << 24) | (byte3 << 16)
129                 | (byte2 << 8) | byte1);
130         value = Math.round(value / 100f / 25.4f * info.dpiHorizontal);
131         info.width = (int) (value & 0xffffffff);
132
133         //height
134
byte1 = header[HEIGHT_OFFSET] & 0xff;
135         byte2 = header[HEIGHT_OFFSET + 1] & 0xff;
136         byte3 = header[HEIGHT_OFFSET + 2] & 0xff;
137         byte4 = header[HEIGHT_OFFSET + 3] & 0xff;
138         value = (long) ((byte4 << 24) | (byte3 << 16) | (byte2 << 8) | byte1);
139         value = Math.round(value / 100f / 25.4f * info.dpiVertical);
140         info.height = (int) (value & 0xffffffff);
141
142         return info;
143     }
144
145     private byte[] getDefaultHeader(InputStream JavaDoc imageStream)
146                 throws IOException JavaDoc {
147         byte[] header = new byte[EMF_SIG_LENGTH];
148         try {
149             imageStream.mark(EMF_SIG_LENGTH + 1);
150             imageStream.read(header);
151             imageStream.reset();
152         } catch (IOException JavaDoc ex) {
153             try {
154                 imageStream.reset();
155             } catch (IOException JavaDoc exbis) {
156                 // throw the original exception, not this one
157
}
158             throw ex;
159         }
160         return header;
161     }
162 }
163
Popular Tags