1 51 package org.apache.fop.image.analyser; 52 53 import java.io.BufferedInputStream ; 55 import java.io.ByteArrayOutputStream ; 56 import java.io.IOException ; 57 import java.io.InputStream ; 58 59 62 public class EPSReader extends AbstractImageReader { 63 private long[] bbox; 64 private boolean isAscii; 66 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 75 private byte[] rawEps; 76 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)((b4 << 24) | (b3 << 16) | (b2 << 8) | b1); 88 } 89 90 public boolean verifySignature(String uri, BufferedInputStream fis) 91 throws IOException { 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 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 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 isEPS = false; 134 } 135 } 136 137 return isEPS; 138 } 139 140 141 private void readEPSImage(BufferedInputStream fis) throws IOException { 142 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 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 ex) { 155 throw new IOException ("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 177 public byte[] getPreview() { 178 InputStream 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 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 ns = new String (num); 239 mbbox[i] = Long.parseLong(ns); 240 241 return (1+nidx - idx); 242 } 243 244 public String getMimeType() { 245 return "image/eps"; 246 } 247 248 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 |