KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

46     public SVGFeComponentTransferElementBridge() {}
47
48     /**
49      * Returns 'feComponentTransfer'.
50      */

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

73     public Filter createFilter(BridgeContext ctx,
74                                Element JavaDoc filterElement,
75                                Element JavaDoc filteredElement,
76                                GraphicsNode filteredNode,
77                                Filter inputFilter,
78                                Rectangle2D JavaDoc filterRegion,
79                                Map JavaDoc filterMap) {
80
81         // 'in' attribute
82
Filter in = getIn(filterElement,
83                           filteredElement,
84                           filteredNode,
85                           inputFilter,
86                           filterMap,
87                           ctx);
88         if (in == null) {
89             return null; // disable the filter
90
}
91
92         // Default region is the size of in (if in is SourceGraphic or
93
// SourceAlpha it will already include a pad/crop to the
94
// proper filter region size).
95
Rectangle2D JavaDoc defaultRegion = in.getBounds2D();
96         Rectangle2D JavaDoc primitiveRegion
97             = SVGUtilities.convertFilterPrimitiveRegion(filterElement,
98                                                         filteredElement,
99                                                         filteredNode,
100                                                         defaultRegion,
101                                                         filterRegion,
102                                                         ctx);
103
104         // Now, extract the various transfer functions. They are
105
// defined in the filterElement's children.
106
// Functions are ordered as follow: r, g, b, a.
107
ComponentTransferFunction funcR = null;
108         ComponentTransferFunction funcG = null;
109         ComponentTransferFunction funcB = null;
110         ComponentTransferFunction funcA = null;
111
112         for (Node n = filterElement.getFirstChild();
113              n != null;
114              n = n.getNextSibling()) {
115
116             if (n.getNodeType() != Node.ELEMENT_NODE) {
117                 continue;
118             }
119
120             Element JavaDoc e = (Element JavaDoc)n;
121             Bridge bridge = ctx.getBridge(e);
122             if (bridge == null || !(bridge instanceof SVGFeFuncElementBridge)) {
123                 continue;
124             }
125             SVGFeFuncElementBridge funcBridge
126                 = (SVGFeFuncElementBridge)bridge;
127             ComponentTransferFunction func
128                 = funcBridge.createComponentTransferFunction(filterElement, e);
129             if (funcBridge instanceof SVGFeFuncRElementBridge) {
130                 funcR = func;
131             } else if (funcBridge instanceof SVGFeFuncGElementBridge) {
132                 funcG = func;
133             } else if (funcBridge instanceof SVGFeFuncBElementBridge) {
134                 funcB = func;
135             } else if (funcBridge instanceof SVGFeFuncAElementBridge) {
136                 funcA = func;
137             }
138         }
139
140         Filter filter = new ComponentTransferRable8Bit
141             (in, funcA, funcR, funcG, funcB);
142
143         // handle the 'color-interpolation-filters' property
144
handleColorInterpolationFilters(filter, filterElement);
145
146         filter = new PadRable8Bit(filter, primitiveRegion, PadMode.ZERO_PAD);
147
148         // update the filter Map
149
updateFilterMap(filterElement, filter, filterMap);
150
151         return filter;
152     }
153
154     /**
155      * Bridge class for the &lt;feFuncA> element.
156      */

157     public static class SVGFeFuncAElementBridge extends SVGFeFuncElementBridge {
158
159         /**
160          * Constructs a new bridge for the <tt>feFuncA</tt> element.
161          */

162         public SVGFeFuncAElementBridge() {}
163
164         /**
165          * Returns 'feFuncA'.
166          */

167         public String JavaDoc getLocalName() {
168             return SVG_FE_FUNC_A_TAG;
169         }
170     }
171
172     /**
173      * Bridge class for the &lt;feFuncR> element.
174      */

175     public static class SVGFeFuncRElementBridge extends SVGFeFuncElementBridge {
176
177         /**
178          * Constructs a new bridge for the <tt>feFuncR</tt> element.
179          */

180         public SVGFeFuncRElementBridge() {}
181
182         /**
183          * Returns 'feFuncR'.
184          */

185         public String JavaDoc getLocalName() {
186             return SVG_FE_FUNC_R_TAG;
187         }
188     }
189
190     /**
191      * Bridge class for the &lt;feFuncG> element.
192      */

193     public static class SVGFeFuncGElementBridge extends SVGFeFuncElementBridge {
194
195         /**
196          * Constructs a new bridge for the <tt>feFuncG</tt> element.
197          */

198         public SVGFeFuncGElementBridge() {}
199
200         /**
201          * Returns 'feFuncG'.
202          */

203         public String JavaDoc getLocalName() {
204             return SVG_FE_FUNC_G_TAG;
205         }
206     }
207
208     /**
209      * Bridge class for the &lt;feFuncB> element.
210      */

211     public static class SVGFeFuncBElementBridge extends SVGFeFuncElementBridge {
212
213         /**
214          * Constructs a new bridge for the <tt>feFuncB</tt> element.
215          */

216         public SVGFeFuncBElementBridge() {}
217
218         /**
219          * Returns 'feFuncB'.
220          */

221         public String JavaDoc getLocalName() {
222             return SVG_FE_FUNC_B_TAG;
223         }
224     }
225
226     /**
227      * The base bridge class for component transfer function.
228      */

229     protected static abstract class SVGFeFuncElementBridge
230         extends AbstractSVGBridge {
231
232         /**
233          * Constructs a new bridge for component transfer function.
234          */

235         protected SVGFeFuncElementBridge() {}
236
237         /**
238          * Creates a <tt>ComponentTransferFunction</tt> according to
239          * the specified parameters.
240          *
241          * @param filterElement the feComponentTransfer filter primitive element
242          * @param funcElement the feFuncX element
243          */

244         public ComponentTransferFunction createComponentTransferFunction
245             (Element JavaDoc filterElement, Element JavaDoc funcElement) {
246
247             int type = convertType(funcElement);
248             switch (type) {
249             case ComponentTransferFunction.DISCRETE: {
250                 float [] v = convertTableValues(funcElement);
251                 if (v == null) {
252                     return ConcreteComponentTransferFunction.getIdentityTransfer();
253                 } else {
254                     return ConcreteComponentTransferFunction.getDiscreteTransfer(v);
255                 }
256             }
257             case ComponentTransferFunction.IDENTITY: {
258                 return ConcreteComponentTransferFunction.getIdentityTransfer();
259             }
260             case ComponentTransferFunction.GAMMA: {
261                 // 'amplitude' attribute - default is 1
262
float amplitude
263                     = convertNumber(funcElement, SVG_AMPLITUDE_ATTRIBUTE, 1);
264                 // 'exponent' attribute - default is 1
265
float exponent
266                     = convertNumber(funcElement, SVG_EXPONENT_ATTRIBUTE, 1);
267                 // 'offset' attribute - default is 0
268
float offset
269                     = convertNumber(funcElement, SVG_OFFSET_ATTRIBUTE, 0);
270
271                 return ConcreteComponentTransferFunction.getGammaTransfer
272                     (amplitude, exponent, offset);
273             }
274             case ComponentTransferFunction.LINEAR: {
275                 // 'slope' attribute - default is 1
276
float slope
277                     = convertNumber(funcElement, SVG_SLOPE_ATTRIBUTE, 1);
278                 // 'intercept' attribute - default is 0
279
float intercept
280                     = convertNumber(funcElement, SVG_INTERCEPT_ATTRIBUTE, 0);
281
282                 return ConcreteComponentTransferFunction.getLinearTransfer
283                     (slope, intercept);
284             }
285             case ComponentTransferFunction.TABLE: {
286                 float [] v = convertTableValues(funcElement);
287                 if (v == null) {
288                     return ConcreteComponentTransferFunction.getIdentityTransfer();
289                 } else {
290                     return ConcreteComponentTransferFunction.getTableTransfer(v);
291                 }
292             }
293             default:
294                 throw new Error JavaDoc(); // can't be reached
295
}
296
297         }
298
299         /**
300          * Converts the 'tableValues' attribute of the specified component
301          * transfer function element.
302          *
303          * @param e the element that represents a component transfer function
304          */

305         protected static float [] convertTableValues(Element JavaDoc e) {
306             String JavaDoc s = e.getAttributeNS(null, SVG_TABLE_VALUES_ATTRIBUTE);
307             if (s.length() == 0) {
308                 return null;
309             }
310             StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(s, " ,");
311             float [] v = new float[tokens.countTokens()];
312             try {
313                 for (int i = 0; tokens.hasMoreTokens(); ++i) {
314                     v[i] = SVGUtilities.convertSVGNumber(tokens.nextToken());
315                 }
316             } catch (NumberFormatException JavaDoc ex) {
317                 throw new BridgeException
318                     (e, ERR_ATTRIBUTE_VALUE_MALFORMED,
319                      new Object JavaDoc[] {SVG_TABLE_VALUES_ATTRIBUTE, s});
320         }
321             return v;
322         }
323
324         /**
325          * Converts the type of the specified component transfert
326          * function element.
327          *
328          * @param e the element that represents a component transfer function
329          */

330         protected static int convertType(Element JavaDoc e) {
331             String JavaDoc s = e.getAttributeNS(null, SVG_TYPE_ATTRIBUTE);
332             if (s.length() == 0) {
333                 throw new BridgeException(e, ERR_ATTRIBUTE_MISSING,
334                                           new Object JavaDoc[] {SVG_TYPE_ATTRIBUTE});
335             }
336             if (SVG_DISCRETE_VALUE.equals(s)) {
337                 return ComponentTransferFunction.DISCRETE;
338             }
339             if (SVG_IDENTITY_VALUE.equals(s)) {
340                 return ComponentTransferFunction.IDENTITY;
341             }
342             if (SVG_GAMMA_VALUE.equals(s)) {
343                 return ComponentTransferFunction.GAMMA;
344             }
345             if (SVG_LINEAR_VALUE.equals(s)) {
346                 return ComponentTransferFunction.LINEAR;
347             }
348             if (SVG_TABLE_VALUE.equals(s)) {
349                 return ComponentTransferFunction.TABLE;
350             }
351             throw new BridgeException(e, ERR_ATTRIBUTE_VALUE_MALFORMED,
352                                       new Object JavaDoc[] {SVG_TYPE_ATTRIBUTE, s});
353         }
354     }
355 }
356
Popular Tags