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.

GIFReader.java 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 GIF image type.
  14. * @author Pankaj Narula
  15. * @version 1.0
  16. */
  17. public class GIFReader extends AbstractImageReader {
  18. static protected final int GIF_SIG_LENGTH = 10;
  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] == 'G') && (header[1] == 'I') &&
  25. (header[2] == 'F') && (header[3] == '8') &&
  26. (header[4] == '7' || header[4] == '9') &&
  27. (header[5] == 'a'));
  28. if (supported) {
  29. setDimension();
  30. return true;
  31. } else
  32. return false;
  33. }
  34. public String getMimeType() {
  35. return "image/gif";
  36. }
  37. protected void setDimension() {
  38. // little endian notation
  39. int byte1 = header[6] & 0xff;
  40. int byte2 = header[7] & 0xff;
  41. this.width = ((byte2 << 8) | byte1) & 0xffff;
  42. byte1 = header[8] & 0xff;
  43. byte2 = header[9] & 0xff;
  44. this.height = ((byte2 << 8) | byte1) & 0xffff;
  45. }
  46. protected void setDefaultHeader() throws IOException {
  47. this.header = new byte[GIF_SIG_LENGTH];
  48. try {
  49. this.imageStream.mark(GIF_SIG_LENGTH + 1);
  50. this.imageStream.read(header);
  51. this.imageStream.reset();
  52. } catch (IOException ex) {
  53. try {
  54. this.imageStream.reset();
  55. } catch (IOException exbis) {}
  56. throw ex;
  57. }
  58. }
  59. }