KickJava   Java API By Example, From Geeks To Geeks.

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


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.Rectangle JavaDoc;
21 import java.awt.image.BufferedImageOp JavaDoc;
22 import java.awt.image.ConvolveOp JavaDoc;
23 import java.awt.image.Kernel JavaDoc;
24
25 import org.w3c.dom.Document JavaDoc;
26 import org.w3c.dom.Element JavaDoc;
27
28 /**
29  * Utility class that converts a ConvolveOp object into
30  * an SVG filter descriptor.
31  *
32  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
33  * @version $Id: SVGConvolveOp.java,v 1.11 2005/03/27 08:58:35 cam Exp $
34  * @see org.apache.batik.svggen.SVGBufferedImageOp
35  */

36 public class SVGConvolveOp extends AbstractSVGFilterConverter {
37     /**
38      * @param generatorContext used to build Elements
39      */

40     public SVGConvolveOp(SVGGeneratorContext generatorContext) {
41         super(generatorContext);
42     }
43
44     /**
45      * Converts a Java 2D API BufferedImageOp into
46      * a set of attribute/value pairs and related definitions
47      *
48      * @param filter BufferedImageOp filter to be converted
49      * @param filterRect Rectangle, in device space, that defines the area
50      * to which filtering applies. May be null, meaning that the
51      * area is undefined.
52      * @return descriptor of the attributes required to represent
53      * the input filter
54      * @see org.apache.batik.svggen.SVGFilterDescriptor
55      */

56     public SVGFilterDescriptor toSVG(BufferedImageOp JavaDoc filter,
57                                      Rectangle JavaDoc filterRect){
58         if(filter instanceof ConvolveOp JavaDoc)
59             return toSVG((ConvolveOp JavaDoc)filter);
60         else
61             return null;
62     }
63
64     /**
65      * @param convolveOp the ConvolveOp to be converted
66      * @return a description of the SVG filter corresponding to
67      * convolveOp. The definition of the feConvolveMatrix
68      * filter in put in feConvolveMatrixDefSet
69      */

70     public SVGFilterDescriptor toSVG(ConvolveOp JavaDoc convolveOp){
71         // Reuse definition if convolveOp has already been converted
72
SVGFilterDescriptor filterDesc =
73             (SVGFilterDescriptor)descMap.get(convolveOp);
74         Document JavaDoc domFactory = generatorContext.domFactory;
75
76         if (filterDesc == null) {
77             //
78
// First time filter is converted: create its corresponding
79
// SVG filter
80
//
81
Kernel JavaDoc kernel = convolveOp.getKernel();
82             Element JavaDoc filterDef =
83                 domFactory.createElementNS(SVG_NAMESPACE_URI, SVG_FILTER_TAG);
84             Element JavaDoc feConvolveMatrixDef =
85                 domFactory.createElementNS(SVG_NAMESPACE_URI,
86                                            SVG_FE_CONVOLVE_MATRIX_TAG);
87
88             // Convert the kernel size
89
feConvolveMatrixDef.setAttributeNS(null, SVG_ORDER_ATTRIBUTE,
90                                              kernel.getWidth() + SPACE +
91                                              kernel.getHeight());
92
93             // Convert the kernel values
94
StringBuffer JavaDoc kernelMatrixBuf = new StringBuffer JavaDoc();
95             float data[] = kernel.getKernelData(null);
96             for(int i=0; i<data.length; i++){
97                 kernelMatrixBuf.append(doubleString(data[i]));
98                 kernelMatrixBuf.append(SPACE);
99             }
100
101             feConvolveMatrixDef.
102                 setAttributeNS(null, SVG_KERNEL_MATRIX_ATTRIBUTE,
103                                kernelMatrixBuf.toString().trim());
104
105             filterDef.appendChild(feConvolveMatrixDef);
106
107             filterDef.setAttributeNS(null, ATTR_ID,
108                                      generatorContext.idGenerator.
109                                      generateID(ID_PREFIX_FE_CONVOLVE_MATRIX));
110
111             // Convert the edge mode
112
if(convolveOp.getEdgeCondition() == ConvolveOp.EDGE_NO_OP)
113                 feConvolveMatrixDef.setAttributeNS(null, SVG_EDGE_MODE_ATTRIBUTE,
114                                                  SVG_DUPLICATE_VALUE);
115             else
116                 feConvolveMatrixDef.setAttributeNS(null, SVG_EDGE_MODE_ATTRIBUTE,
117                                                  SVG_NONE_VALUE);
118
119             //
120
// Create a filter descriptor
121
//
122

123             // Process filter attribute
124
StringBuffer JavaDoc filterAttrBuf = new StringBuffer JavaDoc(URL_PREFIX);
125             filterAttrBuf.append(SIGN_POUND);
126             filterAttrBuf.append(filterDef.getAttributeNS(null, ATTR_ID));
127             filterAttrBuf.append(URL_SUFFIX);
128
129             filterDesc = new SVGFilterDescriptor(filterAttrBuf.toString(),
130                                                  filterDef);
131
132             defSet.add(filterDef);
133             descMap.put(convolveOp, filterDesc);
134         }
135
136         return filterDesc;
137     }
138 }
139
Popular Tags