KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > bitmap > PNGRenderer_onthefly


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
18 /* $Id: PNGRenderer_onthefly.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.bitmap;
21
22 import java.awt.image.RenderedImage JavaDoc;
23 import java.io.BufferedOutputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStream JavaDoc;
29
30 import org.apache.xmlgraphics.image.codec.png.PNGEncodeParam;
31 import org.apache.xmlgraphics.image.codec.png.PNGImageEncoder;
32
33 import org.apache.commons.io.IOUtils;
34
35 import org.apache.fop.apps.FOPException;
36 import org.apache.fop.area.PageViewport;
37 import org.apache.fop.render.java2d.Java2DRenderer;
38
39 /**
40  * PNG Renderer This class actually does not render itself, instead it extends
41  * <code>org.apache.fop.render.java2D.Java2DRenderer</code> and just encode
42  * rendering results into PNG format using Batik's image codec
43  */

44 public class PNGRenderer_onthefly extends Java2DRenderer {
45
46     /** The MIME type for png-Rendering */
47     public static final String JavaDoc MIME_TYPE = "image/png";
48
49     /** The file syntax prefix, eg. "page" will output "page1.png" etc */
50     private String JavaDoc fileSyntax;
51
52     /** The output directory where images are to be written */
53     private File JavaDoc outputDir;
54
55     /** The PNGEncodeParam for the image */
56     private PNGEncodeParam renderParams;
57
58     /** The OutputStream for the first Image */
59     private OutputStream JavaDoc firstOutputStream;
60
61     /** @see org.apache.fop.render.AbstractRenderer */
62     public String JavaDoc getMimeType() {
63         return MIME_TYPE;
64     }
65
66     /** @see org.apache.fop.render.Renderer#supportsOutOfOrder() */
67     public boolean supportsOutOfOrder() {
68         return true;
69     }
70
71     /** @see org.apache.fop.render.Renderer#startRenderer(java.io.OutputStream) */
72     public void startRenderer(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
73         log.info("rendering areas to PNG");
74         setOutputDirectory();
75         this.firstOutputStream = outputStream;
76     }
77
78     /**
79      * Sets the output directory, either from the outfile specified on the
80      * command line, or from the directory specified in configuration file. Also
81      * sets the file name syntax, eg. "page"
82      */

83     private void setOutputDirectory() {
84
85         // the file provided on the command line
86
File JavaDoc f = getUserAgent().getOutputFile();
87
88         outputDir = f.getParentFile();
89
90         // extracting file name syntax
91
String JavaDoc s = f.getName();
92         int i = s.lastIndexOf(".");
93         if (s.charAt(i - 1) == '1') {
94             i--; // getting rid of the "1"
95
}
96         fileSyntax = s.substring(0, i);
97     }
98
99     /**
100      * @see org.apache.fop.render.Renderer#renderPage(org.apache.fop.area.PageViewport)
101      */

102     public void renderPage(PageViewport pageViewport) throws IOException JavaDoc {
103
104         // Do the rendering: get the image for this page
105
RenderedImage JavaDoc image = (RenderedImage JavaDoc) getPageImage(pageViewport);
106
107         // Encode this image
108
log.debug("Encoding page" + (getCurrentPageNumber() + 1));
109         renderParams = PNGEncodeParam.getDefaultEncodeParam(image);
110         OutputStream JavaDoc os = getCurrentOutputStream(getCurrentPageNumber());
111         if (os != null) {
112             try {
113                 PNGImageEncoder encoder = new PNGImageEncoder(os, renderParams);
114                 encoder.encode(image);
115             } finally {
116                 //Only close self-created OutputStreams
117
if (os != firstOutputStream) {
118                     IOUtils.closeQuietly(os);
119                 }
120             }
121         }
122
123         setCurrentPageNumber(getCurrentPageNumber() + 1);
124     }
125
126     /**
127      * Builds the OutputStream corresponding to this page
128      * @param 0-based pageNumber
129      * @return the corresponding OutputStream
130      */

131     private OutputStream JavaDoc getCurrentOutputStream(int pageNumber) {
132
133         if (pageNumber == 0) {
134             return firstOutputStream;
135         }
136
137         File JavaDoc f = new File JavaDoc(outputDir + File.separator + fileSyntax
138                 + (pageNumber + 1) + ".png");
139         try {
140             OutputStream JavaDoc os = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(f));
141             return os;
142         } catch (FileNotFoundException JavaDoc e) {
143             new FOPException("Can't build the OutputStream\n" + e);
144             return null;
145         }
146     }
147 }
148
Popular Tags