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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright 1999-2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.render.bitmap;
  18. // Code originaly contributed by Oleg Tkachenko of Multiconn International Ltd
  19. // (olegt@multiconn.com).
  20. import java.awt.image.BufferedImage;
  21. import java.awt.image.DataBuffer;
  22. import java.awt.image.PixelInterleavedSampleModel;
  23. import java.awt.image.RenderedImage;
  24. import java.awt.image.SampleModel;
  25. import java.awt.image.SinglePixelPackedSampleModel;
  26. import java.io.IOException;
  27. import java.io.OutputStream;
  28. import java.util.Iterator;
  29. import org.apache.avalon.framework.configuration.Configuration;
  30. import org.apache.avalon.framework.configuration.ConfigurationException;
  31. import org.apache.batik.ext.awt.image.GraphicsUtil;
  32. import org.apache.batik.ext.awt.image.codec.tiff.TIFFEncodeParam;
  33. import org.apache.batik.ext.awt.image.codec.tiff.TIFFField;
  34. import org.apache.batik.ext.awt.image.codec.tiff.TIFFImageDecoder;
  35. import org.apache.batik.ext.awt.image.codec.tiff.TIFFImageEncoder;
  36. import org.apache.batik.ext.awt.image.rendered.FormatRed;
  37. import org.apache.commons.logging.Log;
  38. import org.apache.fop.apps.FOPException;
  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. /** */
  66. private TIFFEncodeParam renderParams;
  67. private OutputStream outputStream;
  68. /** @see org.apache.fop.render.AbstractRenderer */
  69. public String getMimeType() {
  70. return MIME_TYPE;
  71. }
  72. /** Creates TIFF renderer. */
  73. public TIFFRenderer() {
  74. renderParams = new TIFFEncodeParam();
  75. //Default to packbits compression which is widely supported
  76. renderParams.setCompression(TIFFEncodeParam.COMPRESSION_PACKBITS);
  77. }
  78. /**
  79. * Configure the TIFF renderer. Get the configuration to be used for
  80. * compression
  81. * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
  82. */
  83. public void configure(Configuration cfg) throws ConfigurationException {
  84. //TODO Support output of monochrome bitmaps (fax-style)
  85. int comp = cfg.getChild("compression").getAttributeAsInteger("value", 1);
  86. String name = null;
  87. switch (comp) {
  88. case TIFFEncodeParam.COMPRESSION_NONE:
  89. name = "COMPRESSION_NONE";
  90. break;
  91. case TIFFEncodeParam.COMPRESSION_JPEG_TTN2:
  92. name = "COMPRESSION_JPEG_TTN2";
  93. break;
  94. case TIFFEncodeParam.COMPRESSION_PACKBITS:
  95. name = "COMPRESSION_PACKBITS";
  96. break;
  97. case TIFFEncodeParam.COMPRESSION_DEFLATE:
  98. name = "COMPRESSION_DEFLATE";
  99. break;
  100. default:
  101. log.info("TIFF compression not supported: " + comp);
  102. return;
  103. }
  104. log.info("TIFF compression set to " + name);
  105. }
  106. /** @see org.apache.fop.render.Renderer#startRenderer(java.io.OutputStream) */
  107. public void startRenderer(OutputStream outputStream) throws IOException {
  108. this.outputStream = outputStream;
  109. super.startRenderer(outputStream);
  110. }
  111. /** @see org.apache.fop.render.Renderer#stopRenderer() */
  112. public void stopRenderer() throws IOException {
  113. super.stopRenderer();
  114. log.debug("Starting Tiff encoding ...");
  115. //Set target resolution
  116. float pixSzMM = userAgent.getTargetPixelUnitToMillimeter();
  117. // num Pixs in 100 Meters
  118. int numPix = (int)(((1000 * 100) / pixSzMM) + 0.5);
  119. int denom = 100 * 100; // Centimeters per 100 Meters;
  120. long [] rational = {numPix, denom};
  121. TIFFField [] fields = {
  122. new TIFFField(TIFFImageDecoder.TIFF_RESOLUTION_UNIT,
  123. TIFFField.TIFF_SHORT, 1,
  124. new char[] {(char)3}),
  125. new TIFFField(TIFFImageDecoder.TIFF_X_RESOLUTION,
  126. TIFFField.TIFF_RATIONAL, 1,
  127. new long[][] {rational}),
  128. new TIFFField(TIFFImageDecoder.TIFF_Y_RESOLUTION,
  129. TIFFField.TIFF_RATIONAL, 1,
  130. new long[][] {rational})
  131. };
  132. renderParams.setExtraFields(fields);
  133. // Creates encoder
  134. TIFFImageEncoder enc = new TIFFImageEncoder(outputStream, renderParams);
  135. // Creates lazy iterator over generated page images
  136. Iterator pageImagesItr = new LazyPageImagesIterator(getNumberOfPages(), log);
  137. // The first image to be passed to enc
  138. RenderedImage first = (RenderedImage) pageImagesItr.next();
  139. // The other images are set to the renderParams
  140. renderParams.setExtraImages(pageImagesItr);
  141. // Start encoding
  142. enc.encode(first);
  143. // Cleaning
  144. outputStream.flush();
  145. clearViewportList();
  146. log.debug("Tiff encoding done.");
  147. }
  148. /** Private inner class to lazy page rendering. */
  149. private class LazyPageImagesIterator implements Iterator {
  150. /** logging instance */
  151. private Log log;
  152. private int count;
  153. private int current = 0;
  154. /**
  155. * Main constructor
  156. * @param c number of pages to iterate over
  157. * @param log the logger to use (this is a hack so this compiles under JDK 1.3)
  158. */
  159. public LazyPageImagesIterator(int c, Log log) {
  160. count = c;
  161. this.log = log;
  162. }
  163. public boolean hasNext() {
  164. return current < count;
  165. }
  166. public Object next() {
  167. log.debug("[" + (current + 1) + "]");
  168. // Renders current page as image
  169. BufferedImage pageImage = null;
  170. try {
  171. pageImage = getPageImage(current++);
  172. } catch (FOPException e) {
  173. log.error(e);
  174. return null;
  175. }
  176. switch (renderParams.getCompression()) {
  177. // These types of compression require a monochrome image
  178. /* these compression types are not supported by the Batik codec
  179. case TIFFEncodeParam.COMPRESSION_GROUP3_1D:
  180. case TIFFEncodeParam.COMPRESSION_GROUP3_2D:
  181. case TIFFEncodeParam.COMPRESSION_GROUP4:
  182. BufferedImage faxImage = new BufferedImage(
  183. pageImage.getWidth(), pageImage.getHeight(),
  184. BufferedImage.TYPE_BYTE_BINARY);
  185. faxImage.getGraphics().drawImage(pageImage, 0, 0, null);
  186. return faxImage;*/
  187. default:
  188. //Decorate the image with a packed sample model for encoding by the codec
  189. SinglePixelPackedSampleModel sppsm;
  190. sppsm = (SinglePixelPackedSampleModel)pageImage.getSampleModel();
  191. int bands = sppsm.getNumBands();
  192. int[] off = new int[bands];
  193. int w = pageImage.getWidth();
  194. int h = pageImage.getHeight();
  195. for (int i = 0; i < bands; i++) {
  196. off[i] = i;
  197. }
  198. SampleModel sm = new PixelInterleavedSampleModel(
  199. DataBuffer.TYPE_BYTE, w, h, bands, w * bands, off);
  200. RenderedImage rimg = new FormatRed(GraphicsUtil.wrap(pageImage), sm);
  201. return rimg;
  202. }
  203. }
  204. public void remove() {
  205. throw new UnsupportedOperationException(
  206. "Method 'remove' is not supported.");
  207. }
  208. }
  209. }