KickJava   Java API By Example, From Geeks To Geeks.

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


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.MorphologyRable8Bit;
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 <feMorphology> element.
34  *
35  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
36  * @version $Id: SVGFeMorphologyElementBridge.java,v 1.15 2004/08/18 07:12:34 vhardy Exp $
37  */

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

45     public SVGFeMorphologyElementBridge() {}
46
47     /**
48      * Returns 'feMorphology'.
49      */

50     public String JavaDoc getLocalName() {
51         return SVG_FE_MORPHOLOGY_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         // 'radius' attribute - default is [0, 0]
81
float [] radii = convertRadius(filterElement);
82         if (radii[0] == 0 || radii[1] == 0) {
83             return null; // disable the filter
84
}
85
86         // 'operator' attribute - default is 'erode'
87
boolean isDilate = convertOperator(filterElement);
88
89         // 'in' attribute
90
Filter in = getIn(filterElement,
91                           filteredElement,
92                           filteredNode,
93                           inputFilter,
94                           filterMap,
95                           ctx);
96         if (in == null) {
97             return null; // disable the filter
98
}
99
100         // Default region is the size of in (if in is SourceGraphic or
101
// SourceAlpha it will already include a pad/crop to the
102
// proper filter region size).
103
Rectangle2D JavaDoc defaultRegion = in.getBounds2D();
104         Rectangle2D JavaDoc primitiveRegion
105             = SVGUtilities.convertFilterPrimitiveRegion(filterElement,
106                                                         filteredElement,
107                                                         filteredNode,
108                                                         defaultRegion,
109                                                         filterRegion,
110                                                         ctx);
111
112         // Take the filter primitive region into account, we need to
113
// pad/crop the input and output.
114
PadRable pad = new PadRable8Bit(in, primitiveRegion, PadMode.ZERO_PAD);
115
116         // build tfilter
117
Filter morphology
118             = new MorphologyRable8Bit(pad, radii[0], radii[1], isDilate);
119
120         // handle the 'color-interpolation-filters' property
121
handleColorInterpolationFilters(morphology, filterElement);
122
123         PadRable filter = new PadRable8Bit
124             (morphology, primitiveRegion, PadMode.ZERO_PAD);
125
126         // update the filter Map
127
updateFilterMap(filterElement, filter, filterMap);
128
129         return filter;
130     }
131
132     /**
133      * Returns the radius (or radii) of the specified feMorphology
134      * filter primitive.
135      *
136      * @param filterElement the feMorphology filter primitive
137      */

138     protected static float [] convertRadius(Element JavaDoc filterElement) {
139         String JavaDoc s = filterElement.getAttributeNS(null, SVG_RADIUS_ATTRIBUTE);
140         if (s.length() == 0) {
141             return new float[] {0, 0};
142         }
143         float [] radii = new float[2];
144         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(s, " ,");
145         try {
146             radii[0] = SVGUtilities.convertSVGNumber(tokens.nextToken());
147             if (tokens.hasMoreTokens()) {
148                 radii[1] = SVGUtilities.convertSVGNumber(tokens.nextToken());
149             } else {
150                 radii[1] = radii[0];
151             }
152         } catch (NumberFormatException JavaDoc ex) {
153             throw new BridgeException
154                 (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
155                  new Object JavaDoc[] {SVG_RADIUS_ATTRIBUTE, s, ex});
156         }
157         if (tokens.hasMoreTokens() || radii[0] < 0 || radii[1] < 0) {
158             throw new BridgeException
159                 (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
160                  new Object JavaDoc[] {SVG_RADIUS_ATTRIBUTE, s});
161         }
162         return radii;
163     }
164
165     /**
166      * Returns the 'operator' of the specified feMorphology filter
167      * primitive.
168      *
169      * @param filterElement the feMorphology filter primitive
170      */

171     protected static boolean convertOperator(Element JavaDoc filterElement) {
172         String JavaDoc s = filterElement.getAttributeNS(null, SVG_OPERATOR_ATTRIBUTE);
173         if (s.length() == 0) {
174             return false;
175         }
176         if (SVG_ERODE_VALUE.equals(s)) {
177             return false;
178         }
179         if (SVG_DILATE_VALUE.equals(s)) {
180             return true;
181         }
182         throw new BridgeException(filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
183                                   new Object JavaDoc[] {SVG_OPERATOR_ATTRIBUTE, s});
184     }
185
186 }
187
Popular Tags