KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > bridge > SVGFeGaussianBlurElementBridge


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.bridge;
19
20 import java.awt.geom.Rectangle2D JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23
24 import org.apache.batik.ext.awt.image.PadMode;
25 import org.apache.batik.ext.awt.image.renderable.Filter;
26 import org.apache.batik.ext.awt.image.renderable.GaussianBlurRable8Bit;
27 import org.apache.batik.ext.awt.image.renderable.PadRable;
28 import org.apache.batik.ext.awt.image.renderable.PadRable8Bit;
29 import org.apache.batik.gvt.GraphicsNode;
30 import org.w3c.dom.Element JavaDoc;
31
32 /**
33  * Bridge class for the <feGaussianBlur> element.
34  *
35  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
36  * @version $Id: SVGFeGaussianBlurElementBridge.java,v 1.18 2004/08/18 07:12:33 vhardy Exp $
37  */

38 public class SVGFeGaussianBlurElementBridge
39     extends AbstractSVGFilterPrimitiveElementBridge {
40
41
42     /**
43      * Constructs a new bridge for the &lt;feGaussianBlur> element.
44      */

45     public SVGFeGaussianBlurElementBridge() {}
46
47     /**
48      * Returns 'feGaussianBlur'.
49      */

50     public String JavaDoc getLocalName() {
51         return SVG_FE_GAUSSIAN_BLUR_TAG;
52     }
53
54     /**
55      * Creates a <tt>Filter</tt> primitive according to the specified
56      * parameters.
57      *
58      * @param ctx the bridge context to use
59      * @param filterElement the element that defines a filter
60      * @param filteredElement the element that references the filter
61      * @param filteredNode the graphics node to filter
62      *
63      * @param inputFilter the <tt>Filter</tt> that represents the current
64      * filter input if the filter chain.
65      * @param filterRegion the filter area defined for the filter chain
66      * the new node will be part of.
67      * @param filterMap a map where the mediator can map a name to the
68      * <tt>Filter</tt> it creates. Other <tt>FilterBridge</tt>s
69      * can then access a filter node from the filterMap if they
70      * know its name.
71      */

72     public Filter createFilter(BridgeContext ctx,
73                                Element JavaDoc filterElement,
74                                Element JavaDoc filteredElement,
75                                GraphicsNode filteredNode,
76                                Filter inputFilter,
77                                Rectangle2D JavaDoc filterRegion,
78                                Map JavaDoc filterMap) {
79
80         // 'stdDeviation' attribute - default is [0, 0]
81
float [] stdDeviationXY = convertStdDeviation(filterElement);
82         if (stdDeviationXY[0] < 0 || stdDeviationXY[1] < 0) {
83             throw new BridgeException(filterElement,
84                                       ERR_ATTRIBUTE_VALUE_MALFORMED,
85                                       new Object JavaDoc[] {SVG_STD_DEVIATION_ATTRIBUTE,
86                                                     "" + stdDeviationXY[0] +
87                                                     stdDeviationXY[1]});
88         }
89
90         // 'in' attribute
91
Filter in = getIn(filterElement,
92                           filteredElement,
93                           filteredNode,
94                           inputFilter,
95                           filterMap,
96                           ctx);
97         if (in == null) {
98             return null; // disable the filter
99
}
100
101         // Default region is the size of in (if in is SourceGraphic or
102
// SourceAlpha it will already include a pad/crop to the
103
// proper filter region size).
104
Rectangle2D JavaDoc defaultRegion = in.getBounds2D();
105         Rectangle2D JavaDoc primitiveRegion
106             = SVGUtilities.convertFilterPrimitiveRegion(filterElement,
107                                                         filteredElement,
108                                                         filteredNode,
109                                                         defaultRegion,
110                                                         filterRegion,
111                                                         ctx);
112
113         // Take the filter primitive region into account, we need to
114
// pad/crop the input and output.
115
PadRable pad = new PadRable8Bit(in, primitiveRegion, PadMode.ZERO_PAD);
116
117         // build filter
118
Filter blur = new GaussianBlurRable8Bit
119             (pad, stdDeviationXY[0], stdDeviationXY[1]);
120
121         // handle the 'color-interpolation-filters' property
122
handleColorInterpolationFilters(blur, filterElement);
123
124         PadRable filter
125             = new PadRable8Bit(blur, primitiveRegion, PadMode.ZERO_PAD);
126
127         // update the filter Map
128
updateFilterMap(filterElement, filter, filterMap);
129
130         return filter;
131     }
132
133     /**
134      * Returns the standard deviation of the specified feGaussianBlur
135      * filter primitive element.
136      *
137      * @param filterElement the feGaussianBlur filter primitive element
138      */

139     protected static float [] convertStdDeviation(Element JavaDoc filterElement) {
140         String JavaDoc s
141             = filterElement.getAttributeNS(null, SVG_STD_DEVIATION_ATTRIBUTE);
142         if (s.length() == 0) {
143             return new float[] {0, 0};
144         }
145         float [] stdDevs = new float[2];
146         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(s, " ,");
147         try {
148             stdDevs[0] = SVGUtilities.convertSVGNumber(tokens.nextToken());
149             if (tokens.hasMoreTokens()) {
150                 stdDevs[1] = SVGUtilities.convertSVGNumber(tokens.nextToken());
151             } else {
152                 stdDevs[1] = stdDevs[0];
153             }
154         } catch (NumberFormatException JavaDoc ex) {
155             throw new BridgeException
156                 (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
157                  new Object JavaDoc[] {SVG_STD_DEVIATION_ATTRIBUTE, s, ex});
158         }
159         if (tokens.hasMoreTokens()) {
160             throw new BridgeException
161                 (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
162                  new Object JavaDoc[] {SVG_STD_DEVIATION_ATTRIBUTE, s});
163         }
164         return stdDevs;
165     }
166 }
167
Popular Tags