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.

BMPReader.java 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.image.analyser;
  8. // Java
  9. import java.io.BufferedInputStream;
  10. import java.io.IOException;
  11. import org.apache.fop.fo.FOUserAgent;
  12. /**
  13. * ImageReader object for BMP image type.
  14. * @author Pankaj Narula
  15. * @version 1.0
  16. */
  17. public class BMPReader extends AbstractImageReader {
  18. static protected final int BMP_SIG_LENGTH = 26;
  19. protected byte[] header;
  20. public boolean verifySignature(String uri, BufferedInputStream fis,
  21. FOUserAgent ua) throws IOException {
  22. this.imageStream = fis;
  23. this.setDefaultHeader();
  24. boolean supported = ((header[0] == (byte) 0x42) &&
  25. (header[1] == (byte) 0x4d));
  26. if (supported) {
  27. setDimension();
  28. return true;
  29. } else
  30. return false;
  31. }
  32. public String getMimeType() {
  33. return "image/bmp";
  34. }
  35. protected void setDimension() {
  36. // little endian notation
  37. int byte1 = header[18] & 0xff;
  38. int byte2 = header[19] & 0xff;
  39. int byte3 = header[20] & 0xff;
  40. int byte4 = header[21] & 0xff;
  41. long l = (long)((byte4 << 24) | (byte3 << 16) |
  42. (byte2 << 8) | byte1);
  43. this.width = (int)(l & 0xffffffff);
  44. byte1 = header[22] & 0xff;
  45. byte2 = header[23] & 0xff;
  46. byte3 = header[24] & 0xff;
  47. byte4 = header[25] & 0xff;
  48. l = (long)((byte4 << 24) | (byte3 << 16) | (byte2 << 8) | byte1);
  49. this.height = (int)(l & 0xffffffff);
  50. }
  51. protected void setDefaultHeader() throws IOException {
  52. this.header = new byte[BMP_SIG_LENGTH];
  53. try {
  54. this.imageStream.mark(BMP_SIG_LENGTH + 1);
  55. this.imageStream.read(header);
  56. this.imageStream.reset();
  57. } catch (IOException ex) {
  58. try {
  59. this.imageStream.reset();
  60. } catch (IOException exbis) {}
  61. throw ex;
  62. }
  63. }
  64. }