1 18 package org.apache.batik.transcoder.image; 19 20 import java.awt.AlphaComposite ; 21 import java.awt.Graphics2D ; 22 import java.awt.Paint ; 23 import java.awt.Shape ; 24 import java.awt.geom.AffineTransform ; 25 import java.awt.geom.Rectangle2D ; 26 import java.awt.image.BufferedImage ; 27 28 import org.apache.batik.ext.awt.image.GraphicsUtil; 29 import org.apache.batik.gvt.renderer.ConcreteImageRendererFactory; 30 import org.apache.batik.gvt.renderer.ImageRenderer; 31 import org.apache.batik.gvt.renderer.ImageRendererFactory; 32 import org.apache.batik.transcoder.SVGAbstractTranscoder; 33 import org.apache.batik.transcoder.TranscoderException; 34 import org.apache.batik.transcoder.TranscoderOutput; 35 import org.apache.batik.transcoder.TranscodingHints; 36 import org.apache.batik.transcoder.keys.BooleanKey; 37 import org.apache.batik.transcoder.keys.PaintKey; 38 import org.w3c.dom.Document ; 39 40 67 public abstract class ImageTranscoder extends SVGAbstractTranscoder { 68 69 72 protected ImageTranscoder() { 73 } 74 75 83 protected void transcode(Document document, 84 String uri, 85 TranscoderOutput output) 86 throws TranscoderException { 87 88 super.transcode(document, uri, output); 90 91 int w = (int)(width+0.5); 93 int h = (int)(height+0.5); 94 95 ImageRendererFactory rendFactory = new ConcreteImageRendererFactory(); 98 ImageRenderer renderer = rendFactory.createStaticImageRenderer(); 100 renderer.updateOffScreen(w, h); 101 renderer.setTransform(curTxf); 103 renderer.setTree(this.root); 104 this.root = null; 106 try { 107 Shape raoi = new Rectangle2D.Float (0, 0, width, height); 109 renderer.repaint(curTxf.createInverse(). 111 createTransformedShape(raoi)); 112 BufferedImage rend = renderer.getOffScreen(); 113 renderer = null; 115 BufferedImage dest = createImage(w, h); 116 117 Graphics2D g2d = GraphicsUtil.createGraphics(dest); 118 if (hints.containsKey(KEY_BACKGROUND_COLOR)) { 119 Paint bgcolor = (Paint )hints.get(KEY_BACKGROUND_COLOR); 120 g2d.setComposite(AlphaComposite.SrcOver); 121 g2d.setPaint(bgcolor); 122 g2d.fillRect(0, 0, w, h); 123 } 124 if (rend != null) { g2d.drawRenderedImage(rend, new AffineTransform ()); 126 } 127 g2d.dispose(); 128 rend = null; writeImage(dest, output); 130 } catch (Exception ex) { 131 throw new TranscoderException(ex); 132 } 133 } 134 135 140 public abstract BufferedImage createImage(int width, int height); 141 142 148 public abstract void writeImage(BufferedImage img, TranscoderOutput output) 149 throws TranscoderException; 150 151 155 177 public static final TranscodingHints.Key KEY_BACKGROUND_COLOR 178 = new PaintKey(); 179 180 216 public static final TranscodingHints.Key KEY_FORCE_TRANSPARENT_WHITE 217 = new BooleanKey(); 218 } 219 | Popular Tags |