您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TIFFReader.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 TIFF image type.
  27. *
  28. * @author Pankaj Narula, Michael Lee
  29. * @version $Id$
  30. */
  31. public class TIFFReader implements ImageReader {
  32. private static final int TIFF_SIG_LENGTH = 8;
  33. /** {@inheritDoc} */
  34. public FopImage.ImageInfo verifySignature(String uri, InputStream bis,
  35. FOUserAgent ua) throws IOException {
  36. byte[] header = getDefaultHeader(bis);
  37. boolean supported = false;
  38. // first 2 bytes = II (little endian encoding)
  39. if (header[0] == (byte) 0x49 && header[1] == (byte) 0x49) {
  40. // look for '42' in byte 3 and '0' in byte 4
  41. if (header[2] == 42 && header[3] == 0) {
  42. supported = true;
  43. }
  44. }
  45. // first 2 bytes == MM (big endian encoding)
  46. if (header[0] == (byte) 0x4D && header[1] == (byte) 0x4D) {
  47. // look for '42' in byte 4 and '0' in byte 3
  48. if (header[2] == 0 && header[3] == 42) {
  49. supported = true;
  50. }
  51. }
  52. if (supported) {
  53. FopImage.ImageInfo info = new FopImage.ImageInfo();
  54. info.dpiHorizontal = ua.getFactory().getSourceResolution();
  55. info.dpiVertical = info.dpiHorizontal;
  56. getDimension(header, info);
  57. info.originalURI = uri;
  58. info.mimeType = getMimeType();
  59. info.inputStream = bis;
  60. return info;
  61. } else {
  62. return null;
  63. }
  64. }
  65. /**
  66. * Returns the MIME type supported by this implementation.
  67. *
  68. * @return The MIME type
  69. */
  70. public String getMimeType() {
  71. return "image/tiff";
  72. }
  73. private void getDimension(byte[] header, FopImage.ImageInfo info) {
  74. // currently not setting the width and height
  75. // these are set again by the Jimi image reader.
  76. // I suppose I'll do it one day to be complete. Or
  77. // someone else will.
  78. // Note: bytes 4,5,6,7 contain the byte offset in the stream of the first IFD block
  79. info.width = -1;
  80. info.height = -1;
  81. }
  82. private byte[] getDefaultHeader(InputStream imageStream)
  83. throws IOException {
  84. byte[] header = new byte[TIFF_SIG_LENGTH];
  85. try {
  86. imageStream.mark(TIFF_SIG_LENGTH + 1);
  87. imageStream.read(header);
  88. imageStream.reset();
  89. } catch (IOException ex) {
  90. try {
  91. imageStream.reset();
  92. } catch (IOException exbis) {
  93. // throw the original exception, not this one
  94. }
  95. throw ex;
  96. }
  97. return header;
  98. }
  99. }