KickJava   Java API By Example, From Geeks To Geeks.

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


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.Dimension JavaDoc;
21 import java.awt.Graphics2D JavaDoc;
22 import java.awt.Image JavaDoc;
23 import java.awt.image.BufferedImage JavaDoc;
24 import java.awt.image.RenderedImage JavaDoc;
25 import java.awt.image.renderable.RenderableImage JavaDoc;
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStream JavaDoc;
29
30 import org.apache.batik.ext.awt.image.codec.ImageEncoder;
31 import org.apache.batik.ext.awt.image.codec.PNGImageEncoder;
32 import org.apache.batik.util.Base64EncoderStream;
33 import org.w3c.dom.Element JavaDoc;
34
35 /**
36  * This implementation of ImageHandler encodes the input image as
37  * a PNG image first, then encodes the PNG image using Base64
38  * encoding and uses the result to encoder the image url using
39  * the data protocol.
40  *
41  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
42  * @version $Id: ImageHandlerBase64Encoder.java,v 1.18 2004/08/18 07:14:59 vhardy Exp $
43  * @see org.apache.batik.svggen.SVGGraphics2D
44  * @see org.apache.batik.svggen.ImageHandler
45  */

46 public class ImageHandlerBase64Encoder extends DefaultImageHandler {
47     /**
48      * Build an <code>ImageHandlerBase64Encoder</code> instance.
49      */

50     public ImageHandlerBase64Encoder() {
51         super();
52     }
53
54     /**
55      * The handler should set the xlink:href tag and the width and
56      * height attributes.
57      */

58     public void handleHREF(Image JavaDoc image, Element JavaDoc imageElement,
59                               SVGGeneratorContext generatorContext)
60         throws SVGGraphics2DIOException {
61         if (image == null)
62             throw new SVGGraphics2DRuntimeException(ERR_IMAGE_NULL);
63
64         int width = image.getWidth(null);
65         int height = image.getHeight(null);
66
67         if (width==0 || height==0) {
68             handleEmptyImage(imageElement);
69         } else {
70             if (image instanceof RenderedImage JavaDoc) {
71                 handleHREF((RenderedImage JavaDoc)image, imageElement,
72                            generatorContext);
73             } else {
74                 BufferedImage JavaDoc buf =
75                     new BufferedImage JavaDoc(width, height,
76                                       BufferedImage.TYPE_INT_ARGB);
77
78                 Graphics2D JavaDoc g = buf.createGraphics();
79                 g.drawImage(image, 0, 0, null);
80                 g.dispose();
81                 handleHREF((RenderedImage JavaDoc)buf, imageElement,
82                            generatorContext);
83             }
84         }
85     }
86
87     /**
88      * The handler should set the xlink:href tag and the width and
89      * height attributes.
90      */

91     public void handleHREF(RenderableImage JavaDoc image, Element JavaDoc imageElement,
92                               SVGGeneratorContext generatorContext)
93         throws SVGGraphics2DIOException {
94         if (image == null){
95             throw new SVGGraphics2DRuntimeException(ERR_IMAGE_NULL);
96         }
97
98         RenderedImage JavaDoc r = image.createDefaultRendering();
99         if (r == null) {
100             handleEmptyImage(imageElement);
101         } else {
102             handleHREF(r, imageElement, generatorContext);
103         }
104     }
105
106     protected void handleEmptyImage(Element JavaDoc imageElement) {
107         imageElement.setAttributeNS(XLINK_NAMESPACE_URI,
108                                     ATTR_XLINK_HREF, DATA_PROTOCOL_PNG_PREFIX);
109         imageElement.setAttributeNS(null, SVG_WIDTH_ATTRIBUTE, "0");
110         imageElement.setAttributeNS(null, SVG_HEIGHT_ATTRIBUTE, "0");
111     }
112
113     /**
114      * This version of handleHREF encodes the input image into a
115      * PNG image whose bytes are then encoded with Base64. The
116      * resulting encoded data is used to set the url on the
117      * input imageElement, using the data: protocol.
118      */

119     public void handleHREF(RenderedImage JavaDoc image, Element JavaDoc imageElement,
120                               SVGGeneratorContext generatorContext)
121         throws SVGGraphics2DIOException {
122
123         //
124
// Setup Base64Encoder stream to byte array.
125
//
126
ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
127         Base64EncoderStream b64Encoder = new Base64EncoderStream(os);
128         try {
129             //
130
// Now, encode the input image to the base 64 stream.
131
//
132
encodeImage(image, b64Encoder);
133
134             // Close the b64 encoder stream (terminates the b64 streams).
135
b64Encoder.close();
136         } catch (IOException JavaDoc e) {
137             // Should not happen because we are doing in-memory processing
138
throw new SVGGraphics2DIOException(ERR_UNEXPECTED, e);
139         }
140
141         //
142
// Finally, write out url
143
//
144
imageElement.setAttributeNS(XLINK_NAMESPACE_URI,
145                                     ATTR_XLINK_HREF,
146                                     DATA_PROTOCOL_PNG_PREFIX +
147                                     os.toString());
148
149     }
150
151     public void encodeImage(RenderedImage JavaDoc buf, OutputStream JavaDoc os)
152         throws SVGGraphics2DIOException {
153         try{
154             ImageEncoder encoder = new PNGImageEncoder(os, null);
155             encoder.encode(buf);
156         } catch(IOException JavaDoc e) {
157             // We are doing in-memory processing. This should not happen.
158
throw new SVGGraphics2DIOException(ERR_UNEXPECTED);
159         }
160     }
161
162     /**
163      * This method creates a BufferedImage with an alpha channel, as this is
164      * supported by Base64.
165      */

166     public BufferedImage JavaDoc buildBufferedImage(Dimension JavaDoc size) {
167         return new BufferedImage JavaDoc(size.width, size.height,
168                                  BufferedImage.TYPE_INT_ARGB);
169     }
170 }
171
Popular Tags