KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.batik.ext.awt.image.ARGBChannel;
26 import org.apache.batik.ext.awt.image.PadMode;
27 import org.apache.batik.ext.awt.image.renderable.DisplacementMapRable8Bit;
28 import org.apache.batik.ext.awt.image.renderable.Filter;
29 import org.apache.batik.ext.awt.image.renderable.PadRable;
30 import org.apache.batik.ext.awt.image.renderable.PadRable8Bit;
31 import org.apache.batik.gvt.GraphicsNode;
32 import org.w3c.dom.Element JavaDoc;
33
34 /**
35  * Bridge class for the <feDisplacementMap> element.
36  *
37  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
38  * @version $Id: SVGFeDisplacementMapElementBridge.java,v 1.15 2004/08/18 07:12:33 vhardy Exp $
39  */

40 public class SVGFeDisplacementMapElementBridge
41     extends AbstractSVGFilterPrimitiveElementBridge {
42
43
44     /**
45      * Constructs a new bridge for the &lt;feDisplacementMap> element.
46      */

47     public SVGFeDisplacementMapElementBridge() {}
48
49     /**
50      * Returns 'feDisplacementMap'.
51      */

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

74     public Filter createFilter(BridgeContext ctx,
75                                Element JavaDoc filterElement,
76                                Element JavaDoc filteredElement,
77                                GraphicsNode filteredNode,
78                                Filter inputFilter,
79                                Rectangle2D JavaDoc filterRegion,
80                                Map JavaDoc filterMap) {
81
82         // 'scale' attribute - default is 0
83
float scale = convertNumber(filterElement, SVG_SCALE_ATTRIBUTE, 0);
84
85         // 'xChannelSelector' attribute - default is 'A'
86
ARGBChannel xChannelSelector = convertChannelSelector
87             (filterElement, SVG_X_CHANNEL_SELECTOR_ATTRIBUTE, ARGBChannel.A);
88
89         // 'yChannelSelector' attribute - default is 'A'
90
ARGBChannel yChannelSelector = convertChannelSelector
91             (filterElement, SVG_Y_CHANNEL_SELECTOR_ATTRIBUTE, ARGBChannel.A);
92
93         // 'in' attribute
94
Filter in = getIn(filterElement,
95                           filteredElement,
96                           filteredNode,
97                           inputFilter,
98                           filterMap,
99                           ctx);
100         if (in == null) {
101             return null; // disable the filter
102
}
103
104         // 'in2' attribute - required
105
Filter in2 = getIn2(filterElement,
106                             filteredElement,
107                             filteredNode,
108                             inputFilter,
109                             filterMap,
110                             ctx);
111         if (in2 == null) {
112             return null; // disable the filter
113
}
114
115         Rectangle2D JavaDoc defaultRegion;
116         defaultRegion = (Rectangle2D JavaDoc)in.getBounds2D().clone();
117         defaultRegion.add(in2.getBounds2D());
118         // get filter primitive chain region
119
Rectangle2D JavaDoc primitiveRegion
120             = SVGUtilities.convertFilterPrimitiveRegion(filterElement,
121                                                         filteredElement,
122                                                         filteredNode,
123                                                         defaultRegion,
124                                                         filterRegion,
125                                                         ctx);
126
127         PadRable pad
128             = new PadRable8Bit(in, primitiveRegion, PadMode.ZERO_PAD);
129
130         // build the displacement map filter
131
List JavaDoc srcs = new ArrayList JavaDoc(2);
132         srcs.add(pad);
133         srcs.add(in2);
134         Filter displacementMap = new DisplacementMapRable8Bit
135             (srcs, scale, xChannelSelector, yChannelSelector);
136
137         // handle the 'color-interpolation-filters' property
138
handleColorInterpolationFilters(displacementMap, filterElement);
139
140         PadRable filter = new PadRable8Bit
141             (displacementMap, primitiveRegion, PadMode.ZERO_PAD);
142
143         // update the filter Map
144
updateFilterMap(filterElement, filter, filterMap);
145
146         return filter;
147     }
148
149     /**
150      * Returns the channel for the specified feDisplacementMap filter
151      * primitive attribute, considering the specified attribute name.
152      *
153      * @param filterElement the feDisplacementMap filter primitive element
154      * @param attrName the name of the channel attribute
155      */

156     protected static
157         ARGBChannel convertChannelSelector(Element JavaDoc filterElement,
158                                            String JavaDoc attrName,
159                                            ARGBChannel defaultChannel) {
160
161         String JavaDoc s = filterElement.getAttributeNS(null, attrName);
162         if (s.length() == 0) {
163             return defaultChannel;
164         }
165         if (SVG_A_VALUE.equals(s)) {
166             return ARGBChannel.A;
167         }
168         if (SVG_R_VALUE.equals(s)) {
169             return ARGBChannel.R;
170         }
171         if (SVG_G_VALUE.equals(s)) {
172             return ARGBChannel.G;
173         }
174         if (SVG_B_VALUE.equals(s)) {
175             return ARGBChannel.B;
176         }
177         throw new BridgeException(filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
178                                   new Object JavaDoc[] {attrName, s});
179     }
180 }
181
Popular Tags