KickJava   Java API By Example, From Geeks To Geeks.

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


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.java 472508 2006-11-08 14:57:00Z 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.writer.ImageWriter;
31 import org.apache.xmlgraphics.image.writer.ImageWriterParams;
32 import org.apache.xmlgraphics.image.writer.ImageWriterRegistry;
33
34 import org.apache.commons.io.IOUtils;
35
36 import org.apache.fop.apps.FOPException;
37 import org.apache.fop.apps.MimeConstants;
38 import org.apache.fop.area.PageViewport;
39 import org.apache.fop.render.java2d.Java2DRenderer;
40
41 /**
42  * PNG Renderer This class actually does not render itself, instead it extends
43  * <code>org.apache.fop.render.java2D.Java2DRenderer</code> and just encode
44  * rendering results into PNG format using Batik's image codec
45  */

46 public class PNGRenderer extends Java2DRenderer {
47
48     /** The MIME type for png-Rendering */
49     public static final String JavaDoc MIME_TYPE = MimeConstants.MIME_PNG;
50
51     /** The file extension expected for PNG files */
52     private static final String JavaDoc PNG_FILE_EXTENSION = "png";
53
54     /** The file syntax prefix, eg. "page" will output "page1.png" etc */
55     private String JavaDoc filePrefix;
56
57     /** The output directory where images are to be written */
58     private File JavaDoc outputDir;
59
60     /** The OutputStream for the first Image */
61     private OutputStream JavaDoc firstOutputStream;
62
63     /** @see org.apache.fop.render.AbstractRenderer */
64     public String JavaDoc getMimeType() {
65         return MIME_TYPE;
66     }
67
68     /** @see org.apache.fop.render.Renderer#startRenderer(java.io.OutputStream) */
69     public void startRenderer(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
70         log.info("rendering areas to PNG");
71         setOutputDirectory();
72         this.firstOutputStream = outputStream;
73     }
74
75     /**
76      * Sets the output directory, either from the outfile specified on the
77      * command line, or from the directory specified in configuration file.
78      * Also sets the file name syntax, eg. "page".
79      * The file name must not have an extension, or must have extension "png",
80      * and its last period must not be at the start (empty file prefix).
81      *
82      * @throws IOException if an invalid output file name was specified
83      * (e.g., with an extension other than '.png')
84      */

85     private void setOutputDirectory() throws IOException JavaDoc {
86
87         // the file provided on the command line
88
File JavaDoc f = getUserAgent().getOutputFile();
89         if (f == null) {
90             //No filename information available. Only the first page will be rendered.
91
outputDir = null;
92             filePrefix = null;
93         } else {
94             outputDir = f.getParentFile();
95
96             // extracting file name syntax
97
String JavaDoc s = f.getName();
98             int i = s.lastIndexOf(".");
99             if (i > 0) {
100                 // Make sure that the file extension was "png"
101
String JavaDoc extension = s.substring(i + 1).toLowerCase();
102                 if (!PNG_FILE_EXTENSION.equals(extension)) {
103                     throw new IOException JavaDoc("Invalid file extension ('"
104                                           + extension + "') specified");
105                 }
106             } else if (i == -1) {
107                 i = s.length();
108             } else { // i == 0
109
throw new IOException JavaDoc("Invalid file name ('"
110                                       + s + "') specified");
111             }
112             if (s.charAt(i - 1) == '1') {
113                 i--; // getting rid of the "1"
114
}
115             filePrefix = s.substring(0, i);
116         }
117     }
118
119     /** @see org.apache.fop.render.Renderer#stopRenderer() */
120     public void stopRenderer() throws IOException JavaDoc {
121
122         super.stopRenderer();
123
124         for (int i = 0; i < pageViewportList.size(); i++) {
125
126             OutputStream JavaDoc os = getCurrentOutputStream(i);
127             if (os == null) {
128                 log.warn("No filename information available."
129                         + " Stopping early after the first page.");
130                 break;
131             }
132             try {
133                 // Do the rendering: get the image for this page
134
RenderedImage JavaDoc image = (RenderedImage JavaDoc) getPageImage((PageViewport) pageViewportList
135                         .get(i));
136     
137                 // Encode this image
138
log.debug("Encoding page " + (i + 1));
139                 ImageWriterParams params = new ImageWriterParams();
140                 params.setResolution(Math.round(userAgent.getTargetResolution()));
141                 
142                 // Encode PNG image
143
ImageWriter writer = ImageWriterRegistry.getInstance().getWriterFor(getMimeType());
144                 if (writer == null) {
145                     throw new IOException JavaDoc("Could not get an ImageWriter to produce "
146                             + getMimeType() + ". The most likely explanation for this is a class"
147                             + " loading problem.");
148                 }
149                 log.debug("Writing image using " + writer.getClass().getName());
150                 writer.writeImage(image, os, params);
151             } finally {
152                 //Only close self-created OutputStreams
153
if (os != firstOutputStream) {
154                     IOUtils.closeQuietly(os);
155                 }
156             }
157         }
158     }
159
160     /**
161      * Builds the OutputStream corresponding to this page
162      * @param 0-based pageNumber
163      * @return the corresponding OutputStream
164      */

165     private OutputStream JavaDoc getCurrentOutputStream(int pageNumber) {
166
167         if (pageNumber == 0) {
168             return firstOutputStream;
169         }
170
171         if (filePrefix == null) {
172             return null;
173         } else {
174             File JavaDoc f = new File JavaDoc(outputDir,
175                     filePrefix + (pageNumber + 1) + "." + PNG_FILE_EXTENSION);
176             try {
177                 OutputStream JavaDoc os = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(f));
178                 return os;
179             } catch (FileNotFoundException JavaDoc e) {
180                 new FOPException("Can't build the OutputStream\n" + e);
181                 return null;
182             }
183         }
184     }
185 }
186
Popular Tags