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.

PNGRenderer.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright 2005-2006 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. import java.awt.image.RenderedImage;
  19. import java.io.BufferedOutputStream;
  20. import java.io.File;
  21. import java.io.FileNotFoundException;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import java.io.OutputStream;
  25. import org.apache.xmlgraphics.image.writer.ImageWriter;
  26. import org.apache.xmlgraphics.image.writer.ImageWriterParams;
  27. import org.apache.xmlgraphics.image.writer.ImageWriterRegistry;
  28. import org.apache.commons.io.IOUtils;
  29. import org.apache.fop.apps.FOPException;
  30. import org.apache.fop.apps.MimeConstants;
  31. import org.apache.fop.area.PageViewport;
  32. import org.apache.fop.render.java2d.Java2DRenderer;
  33. /**
  34. * PNG Renderer This class actually does not render itself, instead it extends
  35. * <code>org.apache.fop.render.java2D.Java2DRenderer</code> and just encode
  36. * rendering results into PNG format using Batik's image codec
  37. */
  38. public class PNGRenderer extends Java2DRenderer {
  39. /** The MIME type for png-Rendering */
  40. public static final String MIME_TYPE = MimeConstants.MIME_PNG;
  41. /** The file syntax prefix, eg. "page" will output "page1.png" etc */
  42. private String filePrefix;
  43. /** The output directory where images are to be written */
  44. private File outputDir;
  45. /** The OutputStream for the first Image */
  46. private OutputStream firstOutputStream;
  47. /** @see org.apache.fop.render.AbstractRenderer */
  48. public String getMimeType() {
  49. return MIME_TYPE;
  50. }
  51. /** @see org.apache.fop.render.Renderer#startRenderer(java.io.OutputStream) */
  52. public void startRenderer(OutputStream outputStream) throws IOException {
  53. log.info("rendering areas to PNG");
  54. setOutputDirectory();
  55. this.firstOutputStream = outputStream;
  56. }
  57. /**
  58. * Sets the output directory, either from the outfile specified on the
  59. * command line, or from the directory specified in configuration file. Also
  60. * sets the file name syntax, eg. "page"
  61. */
  62. private void setOutputDirectory() {
  63. // the file provided on the command line
  64. File f = getUserAgent().getOutputFile();
  65. if (f == null) {
  66. //No filename information available. Only the first page will be rendered.
  67. outputDir = null;
  68. filePrefix = null;
  69. } else {
  70. outputDir = f.getParentFile();
  71. // extracting file name syntax
  72. String s = f.getName();
  73. int i = s.lastIndexOf(".");
  74. if (s.charAt(i - 1) == '1') {
  75. i--; // getting rid of the "1"
  76. }
  77. filePrefix = s.substring(0, i);
  78. }
  79. }
  80. /** @see org.apache.fop.render.Renderer#stopRenderer() */
  81. public void stopRenderer() throws IOException {
  82. super.stopRenderer();
  83. for (int i = 0; i < pageViewportList.size(); i++) {
  84. OutputStream os = getCurrentOutputStream(i);
  85. if (os == null) {
  86. log.warn("No filename information available."
  87. + " Stopping early after the first page.");
  88. break;
  89. }
  90. try {
  91. // Do the rendering: get the image for this page
  92. RenderedImage image = (RenderedImage) getPageImage((PageViewport) pageViewportList
  93. .get(i));
  94. // Encode this image
  95. log.debug("Encoding page " + (i + 1));
  96. ImageWriterParams params = new ImageWriterParams();
  97. params.setResolution(Math.round(userAgent.getTargetResolution()));
  98. // Encode PNG image
  99. ImageWriter writer = ImageWriterRegistry.getInstance().getWriterFor(getMimeType());
  100. log.debug("Writing image using " + writer.getClass().getName());
  101. writer.writeImage(image, os, params);
  102. } finally {
  103. //Only close self-created OutputStreams
  104. if (os != firstOutputStream) {
  105. IOUtils.closeQuietly(os);
  106. }
  107. }
  108. }
  109. }
  110. /**
  111. * Builds the OutputStream corresponding to this page
  112. * @param 0-based pageNumber
  113. * @return the corresponding OutputStream
  114. */
  115. private OutputStream getCurrentOutputStream(int pageNumber) {
  116. if (pageNumber == 0) {
  117. return firstOutputStream;
  118. }
  119. if (filePrefix == null) {
  120. return null;
  121. } else {
  122. File f = new File(outputDir,
  123. filePrefix + (pageNumber + 1) + ".png");
  124. try {
  125. OutputStream os = new BufferedOutputStream(new FileOutputStream(f));
  126. return os;
  127. } catch (FileNotFoundException e) {
  128. new FOPException("Can't build the OutputStream\n" + e);
  129. return null;
  130. }
  131. }
  132. }
  133. }