1 17 18 19 20 package org.apache.fop.render.bitmap; 21 22 import java.awt.image.RenderedImage ; 23 import java.io.BufferedOutputStream ; 24 import java.io.File ; 25 import java.io.FileNotFoundException ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.io.OutputStream ; 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 46 public class PNGRenderer extends Java2DRenderer { 47 48 49 public static final String MIME_TYPE = MimeConstants.MIME_PNG; 50 51 52 private static final String PNG_FILE_EXTENSION = "png"; 53 54 55 private String filePrefix; 56 57 58 private File outputDir; 59 60 61 private OutputStream firstOutputStream; 62 63 64 public String getMimeType() { 65 return MIME_TYPE; 66 } 67 68 69 public void startRenderer(OutputStream outputStream) throws IOException { 70 log.info("rendering areas to PNG"); 71 setOutputDirectory(); 72 this.firstOutputStream = outputStream; 73 } 74 75 85 private void setOutputDirectory() throws IOException { 86 87 File f = getUserAgent().getOutputFile(); 89 if (f == null) { 90 outputDir = null; 92 filePrefix = null; 93 } else { 94 outputDir = f.getParentFile(); 95 96 String s = f.getName(); 98 int i = s.lastIndexOf("."); 99 if (i > 0) { 100 String extension = s.substring(i + 1).toLowerCase(); 102 if (!PNG_FILE_EXTENSION.equals(extension)) { 103 throw new IOException ("Invalid file extension ('" 104 + extension + "') specified"); 105 } 106 } else if (i == -1) { 107 i = s.length(); 108 } else { throw new IOException ("Invalid file name ('" 110 + s + "') specified"); 111 } 112 if (s.charAt(i - 1) == '1') { 113 i--; } 115 filePrefix = s.substring(0, i); 116 } 117 } 118 119 120 public void stopRenderer() throws IOException { 121 122 super.stopRenderer(); 123 124 for (int i = 0; i < pageViewportList.size(); i++) { 125 126 OutputStream 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 RenderedImage image = (RenderedImage ) getPageImage((PageViewport) pageViewportList 135 .get(i)); 136 137 log.debug("Encoding page " + (i + 1)); 139 ImageWriterParams params = new ImageWriterParams(); 140 params.setResolution(Math.round(userAgent.getTargetResolution())); 141 142 ImageWriter writer = ImageWriterRegistry.getInstance().getWriterFor(getMimeType()); 144 if (writer == null) { 145 throw new IOException ("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 if (os != firstOutputStream) { 154 IOUtils.closeQuietly(os); 155 } 156 } 157 } 158 } 159 160 165 private OutputStream getCurrentOutputStream(int pageNumber) { 166 167 if (pageNumber == 0) { 168 return firstOutputStream; 169 } 170 171 if (filePrefix == null) { 172 return null; 173 } else { 174 File f = new File (outputDir, 175 filePrefix + (pageNumber + 1) + "." + PNG_FILE_EXTENSION); 176 try { 177 OutputStream os = new BufferedOutputStream (new FileOutputStream (f)); 178 return os; 179 } catch (FileNotFoundException e) { 180 new FOPException("Can't build the OutputStream\n" + e); 181 return null; 182 } 183 } 184 } 185 } 186 | Popular Tags |