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.

TIFFRenderer.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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.render.bitmap;
  19. // Code originaly contributed by Oleg Tkachenko of Multiconn International Ltd
  20. // (olegt@multiconn.com).
  21. import java.awt.image.BufferedImage;
  22. import java.awt.image.DataBuffer;
  23. import java.awt.image.PixelInterleavedSampleModel;
  24. import java.awt.image.RenderedImage;
  25. import java.awt.image.SampleModel;
  26. import java.awt.image.SinglePixelPackedSampleModel;
  27. import java.io.IOException;
  28. import java.io.OutputStream;
  29. import java.util.Iterator;
  30. import org.apache.commons.logging.Log;
  31. import org.apache.xmlgraphics.image.GraphicsUtil;
  32. import org.apache.xmlgraphics.image.rendered.FormatRed;
  33. import org.apache.xmlgraphics.image.writer.ImageWriter;
  34. import org.apache.xmlgraphics.image.writer.ImageWriterParams;
  35. import org.apache.xmlgraphics.image.writer.ImageWriterRegistry;
  36. import org.apache.xmlgraphics.image.writer.MultiImageWriter;
  37. import org.apache.fop.apps.FOPException;
  38. import org.apache.fop.apps.FOUserAgent;
  39. import org.apache.fop.apps.MimeConstants;
  40. import org.apache.fop.render.java2d.Java2DRenderer;
  41. /**
  42. * <p>
  43. * This class represents renderer to TIFF (Tagged Image File Format) format. It
  44. * is one of the most popular and flexible of the current public domain raster
  45. * file formats, which was is primarily designed for raster data interchange.
  46. * Supported compression types are:
  47. * <ul>
  48. * <li>Raw noncompressed data</li>
  49. * <li>Byte-oriented run-length encoding "PackBits" compression.</li>
  50. * <li>Modified Huffman Compression (CCITT Group 3 1D facsimile compression)</li>
  51. * <li>CCITT T.4 bilevel compression (CCITT Group 3 2D facsimile compression)</li>
  52. * <li>CCITT T.6 bilevel compression (CCITT Group 4 facsimile compression)</li>
  53. * <li>JPEG-in-TIFF compression</li>
  54. * <li>DEFLATE lossless compression (also known as "Zip-in-TIFF")</li>
  55. * <li>LZW compression</li>
  56. * TODO
  57. * <p>
  58. * This class actually does not render itself, instead it extends
  59. * <code>org.apache.fop.render.java2D.Java2DRenderer</code> and just encode
  60. * rendering results into TIFF format using Batik's image codec
  61. */
  62. public class TIFFRenderer extends Java2DRenderer {
  63. /** The MIME type for tiff-Rendering */
  64. public static final String MIME_TYPE = MimeConstants.MIME_TIFF;
  65. //private static final String COMPRESSION_NONE = "NONE";
  66. //private static final String COMPRESSION_JPEG = "JPEG";
  67. public static final String COMPRESSION_PACKBITS = "PackBits";
  68. //private static final String COMPRESSION_DEFLATE = "Deflate";
  69. //private static final String COMPRESSION_LZW = "LZW";
  70. //private static final String COMPRESSION_ZLIB = "ZLib";
  71. public static final String COMPRESSION_CCITT_T6 = "CCITT T.6"; //CCITT Group 4
  72. public static final String COMPRESSION_CCITT_T4 = "CCITT T.4"; //CCITT Group 3
  73. /** ImageWriter parameters */
  74. private ImageWriterParams writerParams;
  75. /** Image Type as parameter for the BufferedImage constructor (see BufferedImage.TYPE_*) */
  76. private int bufferedImageType = BufferedImage.TYPE_INT_ARGB;
  77. private OutputStream outputStream;
  78. /** @see org.apache.fop.render.AbstractRenderer */
  79. public String getMimeType() {
  80. return MIME_TYPE;
  81. }
  82. /** Creates TIFF renderer. */
  83. public TIFFRenderer() {
  84. writerParams = new ImageWriterParams();
  85. writerParams.setCompressionMethod(COMPRESSION_PACKBITS);
  86. }
  87. /**
  88. * @see org.apache.fop.render.java2d.Java2DRenderer#setUserAgent(
  89. * org.apache.fop.apps.FOUserAgent)
  90. */
  91. public void setUserAgent(FOUserAgent foUserAgent) {
  92. super.setUserAgent(foUserAgent);
  93. //Set target resolution
  94. int dpi = Math.round(userAgent.getTargetResolution());
  95. writerParams.setResolution(dpi);
  96. }
  97. /** @see org.apache.fop.render.Renderer#startRenderer(java.io.OutputStream) */
  98. public void startRenderer(OutputStream outputStream) throws IOException {
  99. this.outputStream = outputStream;
  100. super.startRenderer(outputStream);
  101. }
  102. /** @see org.apache.fop.render.Renderer#stopRenderer() */
  103. public void stopRenderer() throws IOException {
  104. super.stopRenderer();
  105. log.debug("Starting TIFF encoding ...");
  106. // Creates lazy iterator over generated page images
  107. Iterator pageImagesItr = new LazyPageImagesIterator(getNumberOfPages(), log);
  108. // Creates writer
  109. ImageWriter writer = ImageWriterRegistry.getInstance().getWriterFor(getMimeType());
  110. if (writer == null) {
  111. throw new NullPointerException("No ImageWriter for " + getMimeType() + " available!");
  112. }
  113. if (writer.supportsMultiImageWriter()) {
  114. MultiImageWriter multiWriter = writer.createMultiImageWriter(outputStream);
  115. try {
  116. // Write all pages/images
  117. while (pageImagesItr.hasNext()) {
  118. RenderedImage img = (RenderedImage) pageImagesItr.next();
  119. multiWriter.writeImage(img, writerParams);
  120. }
  121. } finally {
  122. multiWriter.close();
  123. }
  124. } else {
  125. writer.writeImage((RenderedImage) pageImagesItr.next(), outputStream, writerParams);
  126. if (pageImagesItr.hasNext()) {
  127. log.error("Image encoder does not support multiple images. Only the first page"
  128. + " has been produced.");
  129. }
  130. }
  131. // Cleaning
  132. outputStream.flush();
  133. clearViewportList();
  134. log.debug("TIFF encoding done.");
  135. }
  136. /** @see org.apache.fop.render.java2d.Java2DRenderer#getBufferedImage(int, int) */
  137. protected BufferedImage getBufferedImage(int bitmapWidth, int bitmapHeight) {
  138. return new BufferedImage(bitmapWidth, bitmapHeight, bufferedImageType);
  139. }
  140. /** Private inner class to lazy page rendering. */
  141. private class LazyPageImagesIterator implements Iterator {
  142. /** logging instance */
  143. private Log log;
  144. private int count;
  145. private int current = 0;
  146. /**
  147. * Main constructor
  148. * @param c number of pages to iterate over
  149. * @param log the logger to use (this is a hack so this compiles under JDK 1.3)
  150. */
  151. public LazyPageImagesIterator(int c, Log log) {
  152. count = c;
  153. this.log = log;
  154. }
  155. public boolean hasNext() {
  156. return current < count;
  157. }
  158. public Object next() {
  159. if (log.isDebugEnabled()) {
  160. log.debug("[" + (current + 1) + "]");
  161. }
  162. // Renders current page as image
  163. BufferedImage pageImage = null;
  164. try {
  165. pageImage = getPageImage(current++);
  166. } catch (FOPException e) {
  167. log.error(e);
  168. return null;
  169. }
  170. if (COMPRESSION_CCITT_T4.equalsIgnoreCase(writerParams.getCompressionMethod())
  171. || COMPRESSION_CCITT_T6.equalsIgnoreCase(writerParams.getCompressionMethod())) {
  172. return pageImage;
  173. } else {
  174. //Decorate the image with a packed sample model for encoding by the codec
  175. SinglePixelPackedSampleModel sppsm;
  176. sppsm = (SinglePixelPackedSampleModel)pageImage.getSampleModel();
  177. int bands = sppsm.getNumBands();
  178. int[] off = new int[bands];
  179. int w = pageImage.getWidth();
  180. int h = pageImage.getHeight();
  181. for (int i = 0; i < bands; i++) {
  182. off[i] = i;
  183. }
  184. SampleModel sm = new PixelInterleavedSampleModel(
  185. DataBuffer.TYPE_BYTE, w, h, bands, w * bands, off);
  186. RenderedImage rimg = new FormatRed(GraphicsUtil.wrap(pageImage), sm);
  187. return rimg;
  188. }
  189. }
  190. public void remove() {
  191. throw new UnsupportedOperationException(
  192. "Method 'remove' is not supported.");
  193. }
  194. }
  195. public void setBufferedImageType(int bufferedImageType) {
  196. this.bufferedImageType = bufferedImageType;
  197. }
  198. public ImageWriterParams getWriterParams() {
  199. return writerParams;
  200. }
  201. }