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 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.image.analyser;
  19. // Java
  20. import java.io.InputStream;
  21. import java.io.IOException;
  22. // FOP
  23. import org.apache.fop.image.FopImage;
  24. import org.apache.fop.apps.FOUserAgent;
  25. /**
  26. * ImageReader object for GIF image type.
  27. *
  28. * @author Pankaj Narula
  29. * @version $Id$
  30. */
  31. public class GIFReader implements ImageReader {
  32. private static final int GIF_SIG_LENGTH = 10;
  33. /** {@inheritDoc} */
  34. public FopImage.ImageInfo verifySignature(String uri, InputStream bis,
  35. FOUserAgent ua) throws IOException {
  36. byte[] header = getDefaultHeader(bis);
  37. boolean supported = ((header[0] == 'G')
  38. && (header[1] == 'I')
  39. && (header[2] == 'F')
  40. && (header[3] == '8')
  41. && (header[4] == '7' || header[4] == '9')
  42. && (header[5] == 'a'));
  43. if (supported) {
  44. FopImage.ImageInfo info = new FopImage.ImageInfo();
  45. info.dpiHorizontal = ua.getFactory().getSourceResolution();
  46. info.dpiVertical = info.dpiHorizontal;
  47. getDimension(header, info);
  48. info.originalURI = uri;
  49. info.mimeType = getMimeType();
  50. info.inputStream = bis;
  51. return info;
  52. } else {
  53. return null;
  54. }
  55. }
  56. /**
  57. * Returns the MIME type supported by this implementation.
  58. *
  59. * @return The MIME type
  60. */
  61. public String getMimeType() {
  62. return "image/gif";
  63. }
  64. private void getDimension(byte[] header, FopImage.ImageInfo info) {
  65. // little endian notation
  66. int byte1 = header[6] & 0xff;
  67. int byte2 = header[7] & 0xff;
  68. info.width = ((byte2 << 8) | byte1) & 0xffff;
  69. byte1 = header[8] & 0xff;
  70. byte2 = header[9] & 0xff;
  71. info.height = ((byte2 << 8) | byte1) & 0xffff;
  72. }
  73. private byte[] getDefaultHeader(InputStream imageStream)
  74. throws IOException {
  75. byte[] header = new byte[GIF_SIG_LENGTH];
  76. try {
  77. imageStream.mark(GIF_SIG_LENGTH + 1);
  78. imageStream.read(header);
  79. imageStream.reset();
  80. } catch (IOException ex) {
  81. try {
  82. imageStream.reset();
  83. } catch (IOException exbis) {
  84. // throw the original exception, not this one
  85. }
  86. throw ex;
  87. }
  88. return header;
  89. }
  90. }