You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EPSReader.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * $Id: EPSReader.java,v 1.9 2003/03/06 21:25:45 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. // Java
  53. import java.io.InputStream;
  54. import java.io.ByteArrayOutputStream;
  55. import java.io.IOException;
  56. // FOP
  57. import org.apache.fop.image.FopImage;
  58. import org.apache.fop.image.EPSImage;
  59. import org.apache.fop.apps.FOUserAgent;
  60. /**
  61. * ImageReader object for EPS document image type.
  62. *
  63. * @version $Id: EPSReader.java,v 1.9 2003/03/06 21:25:45 jeremias Exp $
  64. */
  65. public class EPSReader implements ImageReader {
  66. private static final byte[] EPS_HEADER_ASCII = "%!PS".getBytes();
  67. private static final byte[] BOUNDINGBOX = "%%BoundingBox: ".getBytes();
  68. /** @see org.apache.fop.image.analyser.ImageReader */
  69. public FopImage.ImageInfo verifySignature(String uri, InputStream bis,
  70. FOUserAgent ua) throws IOException {
  71. boolean isEPS = false;
  72. bis.mark(32);
  73. byte[] header = new byte[30];
  74. bis.read(header, 0, 30);
  75. bis.reset();
  76. EPSImage.EPSData data = new EPSImage.EPSData();
  77. // Check if binary header
  78. if (getLong(header, 0) == 0xC6D3D0C5) {
  79. data.isAscii = false;
  80. isEPS = true;
  81. data.psStart = getLong(header, 4);
  82. data.psLength = getLong(header, 8);
  83. data.wmfStart = getLong(header, 12);
  84. data.wmfLength = getLong(header, 16);
  85. data.tiffStart = getLong(header, 20);
  86. data.tiffLength = getLong(header, 24);
  87. } else {
  88. // Check if plain ascii
  89. byte[] epsh = "%!PS".getBytes();
  90. if (EPS_HEADER_ASCII[0] == header[0]
  91. && EPS_HEADER_ASCII[1] == header[1]
  92. && EPS_HEADER_ASCII[2] == header[2]
  93. && EPS_HEADER_ASCII[3] == header[3]) {
  94. data.isAscii = true;
  95. isEPS = true;
  96. }
  97. }
  98. if (isEPS) {
  99. FopImage.ImageInfo info = new FopImage.ImageInfo();
  100. info.mimeType = getMimeType();
  101. info.data = data;
  102. readEPSImage(bis, data);
  103. data.bbox = readBBox(data);
  104. if (data.bbox != null) {
  105. info.width = (int) (data.bbox[2] - data.bbox[0]);
  106. info.height = (int) (data.bbox[3] - data.bbox[1]);
  107. // image data read
  108. bis.close();
  109. info.inputStream = null;
  110. return info;
  111. } else {
  112. // Ain't eps if no BoundingBox
  113. isEPS = false;
  114. }
  115. }
  116. return null;
  117. }
  118. /**
  119. * Returns the MIME type supported by this implementation.
  120. *
  121. * @return The MIME type
  122. */
  123. public String getMimeType() {
  124. return "image/eps";
  125. }
  126. private long getLong(byte[] buf, int idx) {
  127. int b1 = buf[idx] & 0xff;
  128. int b2 = buf[idx + 1] & 0xff;
  129. int b3 = buf[idx + 2] & 0xff;
  130. int b4 = buf[idx + 3] & 0xff;
  131. return (long) ((b4 << 24) | (b3 << 16) | (b2 << 8) | b1);
  132. }
  133. /**
  134. * Read the eps file and extract eps part.
  135. *
  136. * @param bis The InputStream
  137. * @param data EPSData object to write the results to
  138. * @exception IOException If an I/O error occurs
  139. */
  140. private void readEPSImage(InputStream bis, EPSImage.EPSData data)
  141. throws IOException {
  142. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  143. byte[] file;
  144. byte[] readBuf = new byte[20480];
  145. int bytesRead;
  146. int index = 0;
  147. boolean cont = true;
  148. try {
  149. while ((bytesRead = bis.read(readBuf)) != -1) {
  150. baos.write(readBuf, 0, bytesRead);
  151. }
  152. } catch (java.io.IOException ex) {
  153. throw new IOException("Error while loading EPS image: "
  154. + ex.getMessage());
  155. }
  156. file = baos.toByteArray();
  157. if (data.isAscii) {
  158. data.rawEps = null;
  159. data.epsFile = new byte[file.length];
  160. System.arraycopy(file, 0, data.epsFile, 0, data.epsFile.length);
  161. } else {
  162. data.rawEps = new byte[file.length];
  163. data.epsFile = new byte[(int) data.psLength];
  164. System.arraycopy(file, 0, data.rawEps, 0, data.rawEps.length);
  165. System.arraycopy(data.rawEps, (int) data.psStart, data.epsFile, 0,
  166. (int) data.psLength);
  167. }
  168. }
  169. /**
  170. * Get embedded TIFF preview or null.
  171. *
  172. * @param data The EPS payload
  173. * @return The embedded preview
  174. */
  175. public byte[] getPreview(EPSImage.EPSData data) {
  176. if (data.preview == null) {
  177. if (data.tiffLength > 0) {
  178. data.preview = new byte[(int) data.tiffLength];
  179. System.arraycopy(data.rawEps, (int) data.tiffStart, data.preview, 0,
  180. (int) data.tiffLength);
  181. }
  182. }
  183. return data.preview;
  184. }
  185. /**
  186. * Extract bounding box from eps part.
  187. *
  188. * @param data The EPS payload
  189. * @return An Array of four coordinates making up the bounding box
  190. */
  191. private long[] readBBox(EPSImage.EPSData data) {
  192. long[] mbbox = null;
  193. int idx = 0;
  194. boolean found = false;
  195. while (!found && (data.epsFile.length > (idx + BOUNDINGBOX.length))) {
  196. boolean sfound = true;
  197. int i = idx;
  198. for (i = idx; sfound && (i - idx) < BOUNDINGBOX.length; i++) {
  199. if (BOUNDINGBOX[i - idx] != data.epsFile[i]) {
  200. sfound = false;
  201. }
  202. }
  203. if (sfound) {
  204. found = true;
  205. idx = i;
  206. } else {
  207. idx++;
  208. }
  209. }
  210. if (!found) {
  211. return mbbox;
  212. }
  213. mbbox = new long[4];
  214. idx += readLongString(data, mbbox, 0, idx);
  215. idx += readLongString(data, mbbox, 1, idx);
  216. idx += readLongString(data, mbbox, 2, idx);
  217. idx += readLongString(data, mbbox, 3, idx);
  218. return mbbox;
  219. }
  220. private int readLongString(EPSImage.EPSData data, long[] mbbox, int i, int idx) {
  221. while (idx < data.epsFile.length && (data.epsFile[idx] == 32)) {
  222. idx++;
  223. }
  224. int nidx = idx;
  225. // check also for ANSI46(".") to identify floating point values
  226. while (nidx < data.epsFile.length
  227. && ((data.epsFile[nidx] >= 48 && data.epsFile[nidx] <= 57)
  228. || (data.epsFile[nidx] == 45)
  229. || (data.epsFile[nidx] == 46))) {
  230. nidx++;
  231. }
  232. byte[] num = new byte[nidx - idx];
  233. System.arraycopy(data.epsFile, idx, num, 0, nidx - idx);
  234. String ns = new String(num);
  235. //if( ns.indexOf(".") != -1 ) {
  236. // do something like logging a warning
  237. //}
  238. // then parse the double and round off to the next math. Integer
  239. mbbox[i] = (long) Math.ceil(Double.parseDouble(ns));
  240. return (1 + nidx - idx);
  241. }
  242. }