KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > svggen > CachedImageHandlerBase64Encoder


1 /*
2
3    Copyright 2001-2003 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.svggen;
19
20 import java.awt.geom.AffineTransform JavaDoc;
21 import java.awt.image.BufferedImage JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24
25 import org.apache.batik.ext.awt.image.codec.ImageEncoder;
26 import org.apache.batik.ext.awt.image.codec.PNGImageEncoder;
27 import org.apache.batik.util.Base64EncoderStream;
28 import org.w3c.dom.Element JavaDoc;
29
30
31 /**
32  * This subclass of {@link ImageHandlerBase64Encoder} implements
33  * functionality specific to the cached version of the image
34  * encoder.
35  *
36  * @author <a HREF="mailto:paul_evenblij@compuware.com">Paul Evenblij</a>
37  * @version $Id: CachedImageHandlerBase64Encoder.java,v 1.7 2004/08/18 07:14:58 vhardy Exp $
38  */

39 public class CachedImageHandlerBase64Encoder extends DefaultCachedImageHandler {
40     /**
41      * Build a <code>CachedImageHandlerBase64Encoder</code> instance.
42      */

43     public CachedImageHandlerBase64Encoder() {
44         super();
45         setImageCacher(new ImageCacher.Embedded());
46     }
47     
48    /**
49     * Creates an Element which can refer to an image.
50     * Note that no assumptions should be made by the caller about the
51     * corresponding SVG tag.
52     */

53     public Element JavaDoc createElement(SVGGeneratorContext generatorContext) {
54         // Create a DOM Element in SVG namespace to refer to an image
55
// For this cached version we return <use>
56
Element JavaDoc imageElement =
57             generatorContext.getDOMFactory().createElementNS(
58                                                              SVG_NAMESPACE_URI, SVG_USE_TAG);
59         
60         return imageElement;
61     }
62
63
64     public String JavaDoc getRefPrefix(){
65         return "";
66     }
67
68     /**
69      * Determines the transformation needed to get the cached image to
70      * scale & position properly. Sets x and y attributes on the element
71      * accordingly.
72      */

73     protected AffineTransform JavaDoc handleTransform(Element JavaDoc imageElement,
74                                               double x, double y,
75                                               double srcWidth,
76                                               double srcHeight,
77                                               double dstWidth,
78                                               double dstHeight,
79                                               SVGGeneratorContext generatorContext) {
80
81         // If scaling is necessary, create a transform, since "width" and "height"
82
// have no effect on a <use> element referring to an <image> element.
83

84         AffineTransform JavaDoc af = new AffineTransform JavaDoc();
85         double hRatio = dstWidth / srcWidth;
86         double vRatio = dstHeight / srcHeight;
87
88         af.translate(x,y);
89
90         if(hRatio != 1 || vRatio != 1) {
91             af.scale(hRatio, vRatio);
92         }
93
94         if (!af.isIdentity()){
95             return af;
96         } else {
97             return null;
98         }
99     }
100
101     /**
102      * Uses PNG encoding.
103      */

104     public void encodeImage(BufferedImage JavaDoc buf, OutputStream JavaDoc os)
105             throws IOException JavaDoc {
106         Base64EncoderStream b64Encoder = new Base64EncoderStream(os);
107         ImageEncoder encoder = new PNGImageEncoder(b64Encoder, null);
108         encoder.encode(buf);
109         b64Encoder.close();
110     }
111
112     public int getBufferedImageType(){
113         return BufferedImage.TYPE_INT_ARGB;
114     }
115 }
116
117
Popular Tags