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.

TIFFImage.java.pres 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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: TIFFImage.java 557337 2007-07-18 17:37:14Z adelmelle $ */
  18. package org.apache.fop.image;
  19. import java.awt.color.ColorSpace;
  20. import java.io.IOException;
  21. import org.apache.xmlgraphics.image.codec.util.SeekableStream;
  22. import org.apache.xmlgraphics.image.codec.tiff.TIFFDirectory;
  23. import org.apache.xmlgraphics.image.codec.tiff.TIFFField;
  24. import org.apache.xmlgraphics.image.codec.tiff.TIFFImageDecoder;
  25. import org.apache.xmlgraphics.image.rendered.CachableRed;
  26. import org.apache.commons.io.IOUtils;
  27. /**
  28. * TIFF implementation using the Batik codecs.
  29. */
  30. public class TIFFImage extends XmlGraphicsCommonsImage {
  31. private int compression = 0;
  32. private int stripCount = 0;
  33. private long stripOffset = 0;
  34. private long stripLength = 0;
  35. private int fillOrder = 1;
  36. /**
  37. * Constructs a new BatikImage instance.
  38. * @param imgReader basic metadata for the image
  39. */
  40. public TIFFImage(FopImage.ImageInfo imgReader) {
  41. super(imgReader);
  42. }
  43. /**
  44. * The compression type set in the TIFF directory
  45. * @return the TIFF compression type
  46. */
  47. public int getCompression() {
  48. return compression;
  49. }
  50. /**
  51. * The number of strips in the image
  52. * @return the number of strips in the image
  53. */
  54. public int getStripCount() {
  55. return stripCount;
  56. }
  57. /**
  58. * {@inheritDoc}
  59. * org.apache.xmlgraphics.image.codec.util.SeekableStream)
  60. */
  61. protected CachableRed decodeImage(SeekableStream stream) throws IOException {
  62. org.apache.xmlgraphics.image.codec.tiff.TIFFImage img
  63. = new org.apache.xmlgraphics.image.codec.tiff.TIFFImage
  64. (stream, null, 0);
  65. TIFFDirectory dir = (TIFFDirectory)img.getProperty("tiff_directory");
  66. TIFFField fld = dir.getField(TIFFImageDecoder.TIFF_X_RESOLUTION);
  67. if (fld != null) {
  68. log.debug("x resolution = " + fld.getAsDouble(0));
  69. this.dpiHorizontal = fld.getAsDouble(0);
  70. } else {
  71. log.warn("Cannot determine x resolution.");
  72. }
  73. fld = dir.getField(TIFFImageDecoder.TIFF_Y_RESOLUTION);
  74. if (fld != null) {
  75. log.debug("y resolution = " + fld.getAsDouble(0));
  76. this.dpiVertical = fld.getAsDouble(0);
  77. } else {
  78. log.warn("Cannot determine y resolution.");
  79. }
  80. fld = dir.getField(TIFFImageDecoder.TIFF_RESOLUTION_UNIT);
  81. if (fld != null) {
  82. int resUnit = fld.getAsInt(0);
  83. if (resUnit == 3) {
  84. //cm
  85. this.dpiHorizontal *= 2.54f;
  86. this.dpiVertical *= 2.54f;
  87. } else if (resUnit != 2) {
  88. //ignored
  89. log.warn("Cannot determine bitmap resolution."
  90. + " Unimplemented resolution unit: " + resUnit);
  91. }
  92. } else {
  93. log.warn("Cannot determine bitmap resolution unit.");
  94. }
  95. fld = dir.getField(TIFFImageDecoder.TIFF_COMPRESSION);
  96. if (fld != null) {
  97. compression = fld.getAsInt(0);
  98. }
  99. fld = dir.getField(TIFFImageDecoder.TIFF_BITS_PER_SAMPLE);
  100. if (fld != null) {
  101. bitsPerPixel = fld.getAsInt(0);
  102. }
  103. fld = dir.getField(TIFFImageDecoder.TIFF_ROWS_PER_STRIP);
  104. if (fld == null) {
  105. stripCount = 1;
  106. } else {
  107. stripCount = (int)(dir.getFieldAsLong(TIFFImageDecoder.TIFF_IMAGE_LENGTH)
  108. / fld.getAsLong(0));
  109. }
  110. fld = dir.getField(TIFFImageDecoder.TIFF_FILL_ORDER);
  111. if (fld != null) {
  112. fillOrder = fld.getAsInt(0);
  113. }
  114. stripOffset = dir.getField(TIFFImageDecoder.TIFF_STRIP_OFFSETS).getAsLong(0);
  115. stripLength = dir.getField(TIFFImageDecoder.TIFF_STRIP_BYTE_COUNTS).getAsLong(0);
  116. if (this.bitsPerPixel == 1) {
  117. this.colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
  118. }
  119. return img;
  120. }
  121. /**
  122. * Load the original TIFF data.
  123. * This loads only strip 1 of the original TIFF data.
  124. *
  125. * @return true if loaded false for any error
  126. * {@inheritDoc}
  127. */
  128. protected boolean loadOriginalData() {
  129. if (loadDimensions()) {
  130. byte[] readBuf = new byte[(int)stripLength];
  131. int bytesRead;
  132. try {
  133. this.seekableInput.reset();
  134. this.seekableInput.skip(stripOffset);
  135. bytesRead = seekableInput.read(readBuf);
  136. if (bytesRead != stripLength) {
  137. log.error("Error while loading image: length mismatch on read");
  138. return false;
  139. }
  140. // need to invert bytes if fill order = 2
  141. if (fillOrder == 2) {
  142. for (int i = 0; i < (int)stripLength; i++) {
  143. readBuf[i] = flipTable[readBuf[i] & 0xff];
  144. }
  145. }
  146. this.raw = readBuf;
  147. return true;
  148. } catch (IOException ioe) {
  149. log.error("Error while loading image strip 1 (TIFF): ", ioe);
  150. return false;
  151. } finally {
  152. IOUtils.closeQuietly(seekableInput);
  153. IOUtils.closeQuietly(inputStream);
  154. this.seekableInput = null;
  155. this.inputStream = null;
  156. this.cr = null;
  157. }
  158. }
  159. return false;
  160. }
  161. // Table to be used when fillOrder = 2, for flipping bytes.
  162. // Copied from XML Graphics Commons' TIFFFaxDecoder class
  163. private static byte[] flipTable = {
  164. 0, -128, 64, -64, 32, -96, 96, -32,
  165. 16, -112, 80, -48, 48, -80, 112, -16,
  166. 8, -120, 72, -56, 40, -88, 104, -24,
  167. 24, -104, 88, -40, 56, -72, 120, -8,
  168. 4, -124, 68, -60, 36, -92, 100, -28,
  169. 20, -108, 84, -44, 52, -76, 116, -12,
  170. 12, -116, 76, -52, 44, -84, 108, -20,
  171. 28, -100, 92, -36, 60, -68, 124, -4,
  172. 2, -126, 66, -62, 34, -94, 98, -30,
  173. 18, -110, 82, -46, 50, -78, 114, -14,
  174. 10, -118, 74, -54, 42, -86, 106, -22,
  175. 26, -102, 90, -38, 58, -70, 122, -6,
  176. 6, -122, 70, -58, 38, -90, 102, -26,
  177. 22, -106, 86, -42, 54, -74, 118, -10,
  178. 14, -114, 78, -50, 46, -82, 110, -18,
  179. 30, -98, 94, -34, 62, -66, 126, -2,
  180. 1, -127, 65, -63, 33, -95, 97, -31,
  181. 17, -111, 81, -47, 49, -79, 113, -15,
  182. 9, -119, 73, -55, 41, -87, 105, -23,
  183. 25, -103, 89, -39, 57, -71, 121, -7,
  184. 5, -123, 69, -59, 37, -91, 101, -27,
  185. 21, -107, 85, -43, 53, -75, 117, -11,
  186. 13, -115, 77, -51, 45, -83, 109, -19,
  187. 29, -99, 93, -35, 61, -67, 125, -3,
  188. 3, -125, 67, -61, 35, -93, 99, -29,
  189. 19, -109, 83, -45, 51, -77, 115, -13,
  190. 11, -117, 75, -53, 43, -85, 107, -21,
  191. 27, -101, 91, -37, 59, -69, 123, -5,
  192. 7, -121, 71, -57, 39, -89, 103, -25,
  193. 23, -105, 87, -41, 55, -73, 119, -9,
  194. 15, -113, 79, -49, 47, -81, 111, -17,
  195. 31, -97, 95, -33, 63, -65, 127, -1,
  196. };
  197. // end
  198. }