KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > transcoder > image > ImageTranscoder


1 /*
2
3    Copyright 2001-2004 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    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 package org.apache.batik.transcoder.image;
19
20 import java.awt.AlphaComposite JavaDoc;
21 import java.awt.Graphics2D JavaDoc;
22 import java.awt.Paint JavaDoc;
23 import java.awt.Shape JavaDoc;
24 import java.awt.geom.AffineTransform JavaDoc;
25 import java.awt.geom.Rectangle2D JavaDoc;
26 import java.awt.image.BufferedImage JavaDoc;
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 JavaDoc;
39
40 /**
41  * This class enables to transcode an input to an image of any format.
42  *
43  * <p>Two transcoding hints (<tt>KEY_WIDTH</tt> and
44  * <tt>KEY_HEIGHT</tt>) can be used to respectively specify the image
45  * width and the image height. If only one of these keys is specified,
46  * the transcoder preserves the aspect ratio of the original image.
47  *
48  * <p>The <tt>KEY_BACKGROUND_COLOR</tt> defines the background color
49  * to use for opaque image formats, or the background color that may
50  * be used for image formats that support alpha channel.
51  *
52  * <p>The <tt>KEY_AOI</tt> represents the area of interest to paint
53  * in device space.
54  *
55  * <p>Three additional transcoding hints that act on the SVG
56  * processor can be specified:
57  *
58  * <p><tt>KEY_LANGUAGE</tt> to set the default language to use (may be
59  * used by a &lt;switch> SVG element for example),
60  * <tt>KEY_USER_STYLESHEET_URI</tt> to fix the URI of a user
61  * stylesheet, and <tt>KEY_MM_PER_PIXEL</tt> to specify the number of
62  * millimeters in each pixel .
63  *
64  * @author <a HREF="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
65  * @version $Id: ImageTranscoder.java,v 1.49 2005/03/27 08:58:36 cam Exp $
66  */

67 public abstract class ImageTranscoder extends SVGAbstractTranscoder {
68
69     /**
70      * Constructs a new <tt>ImageTranscoder</tt>.
71      */

72     protected ImageTranscoder() {
73     }
74
75     /**
76      * Transcodes the specified Document as an image in the specified output.
77      *
78      * @param document the document to transcode
79      * @param uri the uri of the document or null if any
80      * @param output the ouput where to transcode
81      * @exception TranscoderException if an error occured while transcoding
82      */

83     protected void transcode(Document JavaDoc document,
84                              String JavaDoc uri,
85                              TranscoderOutput output)
86             throws TranscoderException {
87
88         // Sets up root, curTxf & curAoi
89
super.transcode(document, uri, output);
90
91         // prepare the image to be painted
92
int w = (int)(width+0.5);
93         int h = (int)(height+0.5);
94
95         // paint the SVG document using the bridge package
96
// create the appropriate renderer
97
ImageRendererFactory rendFactory = new ConcreteImageRendererFactory();
98         // ImageRenderer renderer = rendFactory.createDynamicImageRenderer();
99
ImageRenderer renderer = rendFactory.createStaticImageRenderer();
100         renderer.updateOffScreen(w, h);
101         // curTxf.translate(0.5, 0.5);
102
renderer.setTransform(curTxf);
103         renderer.setTree(this.root);
104         this.root = null; // We're done with it...
105

106         try {
107             // now we are sure that the aoi is the image size
108
Shape JavaDoc raoi = new Rectangle2D.Float JavaDoc(0, 0, width, height);
109             // Warning: the renderer's AOI must be in user space
110
renderer.repaint(curTxf.createInverse().
111                              createTransformedShape(raoi));
112             BufferedImage JavaDoc rend = renderer.getOffScreen();
113             renderer = null; // We're done with it...
114

115             BufferedImage JavaDoc dest = createImage(w, h);
116
117             Graphics2D JavaDoc g2d = GraphicsUtil.createGraphics(dest);
118             if (hints.containsKey(KEY_BACKGROUND_COLOR)) {
119                 Paint JavaDoc bgcolor = (Paint JavaDoc)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) { // might be null if the svg document is empty
125
g2d.drawRenderedImage(rend, new AffineTransform JavaDoc());
126             }
127             g2d.dispose();
128             rend = null; // We're done with it...
129
writeImage(dest, output);
130         } catch (Exception JavaDoc ex) {
131             throw new TranscoderException(ex);
132         }
133     }
134
135     /**
136      * Creates a new image with the specified dimension.
137      * @param width the image width in pixels
138      * @param height the image height in pixels
139      */

140     public abstract BufferedImage JavaDoc createImage(int width, int height);
141
142     /**
143      * Writes the specified image to the specified output.
144      * @param img the image to write
145      * @param output the output where to store the image
146      * @throws TranscoderException if an error occured while storing the image
147      */

148     public abstract void writeImage(BufferedImage JavaDoc img, TranscoderOutput output)
149         throws TranscoderException;
150
151     // --------------------------------------------------------------------
152
// Keys definition
153
// --------------------------------------------------------------------
154

155     /**
156      * The image background paint key.
157      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
158      * <TR>
159      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
160      * <TD VALIGN="TOP">KEY_BACKGROUND_COLOR</TD></TR>
161      * <TR>
162      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
163      * <TD VALIGN="TOP">Paint</TD></TR>
164      * <TR>
165      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
166      * <TD VALIGN="TOP">null</TD></TR>
167      * <TR>
168      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
169      * <TD VALIGN="TOP">No</TD></TR>
170      * <TR>
171      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
172      * <TD VALIGN="TOP">Specify the background color to use.
173      * The color is required by opaque image formats and is used by
174      * image formats that support alpha channel.</TD></TR>
175      * </TABLE>
176      */

177     public static final TranscodingHints.Key KEY_BACKGROUND_COLOR
178         = new PaintKey();
179
180     /**
181      * The forceTransparentWhite key.
182      *
183      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
184      * <TR>
185      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
186      * <TD VALIGN="TOP">KEY_FORCE_TRANSPARENT_WHITE</TD></TR>
187      * <TR>
188      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
189      * <TD VALIGN="TOP">Boolean</TD></TR>
190      * <TR>
191      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
192      * <TD VALIGN="TOP">false</TD></TR>
193      * <TR>
194      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
195      * <TD VALIGN="TOP">No</TD></TR>
196      * <TR>
197      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
198
199      * <TD VALIGN="TOP">It controls whether the encoder should force
200      * the image's fully transparent pixels to be fully transparent
201      * white instead of fully transparent black. This is usefull when
202      * the encoded file is displayed in a browser which does not
203      * support transparency correctly and lets the image display with
204      * a white background instead of a black background. <br />
205      *
206      * However, note that the modified image will display differently
207      * over a white background in a viewer that supports
208      * transparency.<br/>
209      *
210      * Not all Transcoders use this key (in particular some formats
211      * can't preserve the alpha channel at all in which case this
212      * is not used.
213      * </TD></TR>
214      * </TABLE>
215      */

216     public static final TranscodingHints.Key KEY_FORCE_TRANSPARENT_WHITE
217         = new BooleanKey();
218 }
219
Popular Tags