aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/render/bitmap/PNGRenderer.java
diff options
context:
space:
mode:
authorJeremias Maerki <jeremias@apache.org>2005-06-09 08:49:27 +0000
committerJeremias Maerki <jeremias@apache.org>2005-06-09 08:49:27 +0000
commit4aa0eca9779c3baf8ed3a21d6fd9e06fa2d71af9 (patch)
treee573b7bc321d90ea2d40719ec0e855557c56180e /src/java/org/apache/fop/render/bitmap/PNGRenderer.java
parentdd08f131b250a44f7b900b895deadf70981114fa (diff)
downloadxmlgraphics-fop-4aa0eca9779c3baf8ed3a21d6fd9e06fa2d71af9.tar.gz
xmlgraphics-fop-4aa0eca9779c3baf8ed3a21d6fd9e06fa2d71af9.zip
Bugzilla: #33760
Resurrected Java2D/AWT Renderer (including bitmap output to PNG and TIFF using the Batik codecs and print capability). Submitted by: Renaud Richardet <renaud.richardet.at.gmail.com> Changes on the original patch: - PageViewport: isResolved() caused a NPE which I fixed. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@198725 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/render/bitmap/PNGRenderer.java')
-rw-r--r--src/java/org/apache/fop/render/bitmap/PNGRenderer.java116
1 files changed, 116 insertions, 0 deletions
diff --git a/src/java/org/apache/fop/render/bitmap/PNGRenderer.java b/src/java/org/apache/fop/render/bitmap/PNGRenderer.java
new file mode 100644
index 000000000..1e2820886
--- /dev/null
+++ b/src/java/org/apache/fop/render/bitmap/PNGRenderer.java
@@ -0,0 +1,116 @@
+package org.apache.fop.render.bitmap;
+
+import java.awt.image.RenderedImage;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.batik.ext.awt.image.codec.PNGEncodeParam;
+import org.apache.batik.ext.awt.image.codec.PNGImageEncoder;
+import org.apache.fop.apps.FOPException;
+import org.apache.fop.area.PageViewport;
+import org.apache.fop.render.java2d.Java2DRenderer;
+
+/**
+ * PNG Renderer This class actually does not render itself, instead it extends
+ * <code>org.apache.fop.render.java2D.Java2DRenderer</code> and just encode
+ * rendering results into PNG format using Batik's image codec
+ */
+public class PNGRenderer extends Java2DRenderer {
+
+ /** The MIME type for png-Rendering */
+ public static final String MIME_TYPE = "image/png";
+
+ /** The file syntax prefix, eg. "page" will output "page1.png" etc */
+ private String fileSyntax;
+
+ /** The output directory where images are to be written */
+ private File outputDir;
+
+ /** The PNGEncodeParam for the image */
+ private PNGEncodeParam renderParams;
+
+ /** The OutputStream for the first Image */
+ private OutputStream firstOutputStream;
+
+ /** @see org.apache.fop.render.AbstractRenderer */
+ public String getMimeType() {
+ return MIME_TYPE;
+ }
+
+ /** default constructor */
+ public PNGRenderer() {}
+
+ /** @see org.apache.fop.render.Renderer#startRenderer(java.io.OutputStream) */
+ public void startRenderer(OutputStream outputStream) throws IOException {
+ getLogger().info("rendering areas to PNG");
+ setOutputDirectory();
+ this.firstOutputStream = outputStream;
+ }
+
+ /**
+ * Sets the output directory, either from the outfile specified on the
+ * command line, or from the directory specified in configuration file. Also
+ * sets the file name syntax, eg. "page"
+ */
+ private void setOutputDirectory() {
+
+ // the file provided on the command line
+ File f = getUserAgent().getOutputFile();
+
+ outputDir = f.getParentFile();
+
+ // extracting file name syntax
+ String s = f.getName();
+ int i = s.lastIndexOf(".");
+ if (s.charAt(i - 1) == '1') {
+ i--; // getting rid of the "1"
+ }
+ fileSyntax = s.substring(0, i);
+ }
+
+ public void stopRenderer() throws IOException {
+
+ super.stopRenderer();
+
+ for (int i = 0; i < pageViewportList.size(); i++) {
+
+ // Do the rendering: get the image for this page
+ RenderedImage image = (RenderedImage) getPageImage((PageViewport) pageViewportList
+ .get(i));
+
+ // Encode this image
+ getLogger().debug("Encoding Page " + (i + 1));
+ renderParams = PNGEncodeParam.getDefaultEncodeParam(image);
+ OutputStream os = getCurrentOutputStream(i);
+ PNGImageEncoder encoder = new PNGImageEncoder(os, renderParams);
+ encoder.encode(image);
+ os.flush();
+ }
+ }
+
+ /**
+ * Builds the OutputStream corresponding to this page
+ * @param 0-based pageNumber
+ * @return the corresponding OutputStream
+ */
+ private OutputStream getCurrentOutputStream(int pageNumber) {
+
+ if (pageNumber == 0) {
+ return firstOutputStream;
+ }
+
+ File f = new File(outputDir + File.separator + fileSyntax
+ + (pageNumber + 1) + ".png");
+ try {
+ OutputStream os = new BufferedOutputStream(new FileOutputStream(f));
+ return os;
+ } catch (FileNotFoundException e) {
+ new FOPException("Can't build the OutputStream\n" + e);
+ return null;
+ }
+ }
+}