1 17 18 19 20 package org.apache.fop.image.analyser; 21 22 import java.io.InputStream ; 24 import java.io.IOException ; 25 26 import org.apache.fop.image.FopImage; 28 import org.apache.fop.apps.FOUserAgent; 29 30 35 public class EMFReader implements ImageReader { 36 37 38 protected static final int EMF_SIG_LENGTH = 88; 39 40 41 private static final int SIGNATURE_OFFSET = 40; 42 43 private static final int WIDTH_OFFSET = 32; 44 45 private static final int HEIGHT_OFFSET = 36; 46 47 private static final int HRES_PIXEL_OFFSET = 72; 48 49 private static final int VRES_PIXEL_OFFSET = 76; 50 51 private static final int HRES_MM_OFFSET = 80; 52 53 private static final int VRES_MM_OFFSET = 84; 54 55 56 public FopImage.ImageInfo verifySignature(String uri, InputStream bis, 57 FOUserAgent ua) throws IOException { 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 81 public String 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 95 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 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 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 imageStream) 146 throws IOException { 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 ex) { 153 try { 154 imageStream.reset(); 155 } catch (IOException exbis) { 156 } 158 throw ex; 159 } 160 return header; 161 } 162 } 163 | Popular Tags |