KickJava   Java API By Example, From Geeks To Geeks.

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


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.ColorMatrixRable;
26 import org.apache.batik.ext.awt.image.renderable.ColorMatrixRable8Bit;
27 import org.apache.batik.ext.awt.image.renderable.Filter;
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 <feColorMatrix> element.
34  *
35  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
36  * @version $Id: SVGFeColorMatrixElementBridge.java,v 1.16 2004/08/18 07:12:33 vhardy Exp $
37  */

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

44     public SVGFeColorMatrixElementBridge() {}
45
46     /**
47      * Returns 'feColorMatrix'.
48      */

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

71     public Filter createFilter(BridgeContext ctx,
72                                Element JavaDoc filterElement,
73                                Element JavaDoc filteredElement,
74                                GraphicsNode filteredNode,
75                                Filter inputFilter,
76                                Rectangle2D JavaDoc filterRegion,
77                                Map JavaDoc filterMap) {
78
79         // 'in' attribute
80
Filter in = getIn(filterElement,
81                           filteredElement,
82                           filteredNode,
83                           inputFilter,
84                           filterMap,
85                           ctx);
86         if (in == null) {
87             return null; // disable the filter
88
}
89
90         // Default region is the size of in (if in is SourceGraphic or
91
// SourceAlpha it will already include a pad/crop to the
92
// proper filter region size).
93
Rectangle2D JavaDoc defaultRegion = in.getBounds2D();
94         Rectangle2D JavaDoc primitiveRegion
95             = SVGUtilities.convertFilterPrimitiveRegion(filterElement,
96                                                         filteredElement,
97                                                         filteredNode,
98                                                         defaultRegion,
99                                                         filterRegion,
100                                                         ctx);
101
102         int type = convertType(filterElement);
103         ColorMatrixRable colorMatrix;
104         switch (type) {
105         case ColorMatrixRable.TYPE_HUE_ROTATE:
106             float a = convertValuesToHueRotate(filterElement);
107             colorMatrix = ColorMatrixRable8Bit.buildHueRotate(a);
108             break;
109         case ColorMatrixRable.TYPE_LUMINANCE_TO_ALPHA:
110             colorMatrix = ColorMatrixRable8Bit.buildLuminanceToAlpha();
111             break;
112         case ColorMatrixRable.TYPE_MATRIX:
113             float [][] matrix = convertValuesToMatrix(filterElement);
114             colorMatrix = ColorMatrixRable8Bit.buildMatrix(matrix);
115             break;
116         case ColorMatrixRable.TYPE_SATURATE:
117             float s = convertValuesToSaturate(filterElement);
118             colorMatrix = ColorMatrixRable8Bit.buildSaturate(s);
119             break;
120         default:
121             throw new Error JavaDoc(); // can't be reached
122
}
123         colorMatrix.setSource(in);
124
125         // handle the 'color-interpolation-filters' property
126
handleColorInterpolationFilters(colorMatrix, filterElement);
127
128         Filter filter
129             = new PadRable8Bit(colorMatrix, primitiveRegion, PadMode.ZERO_PAD);
130
131         // update the filter Map
132
updateFilterMap(filterElement, filter, filterMap);
133
134         return filter;
135     }
136
137     /**
138      * Converts the 'values' attribute of the specified feColorMatrix
139      * filter primitive element for the 'matrix' type.
140      *
141      * @param filterElement the filter element
142      */

143     protected static float[][] convertValuesToMatrix(Element JavaDoc filterElement) {
144         String JavaDoc s = filterElement.getAttributeNS(null, SVG_VALUES_ATTRIBUTE);
145         float [][] matrix = new float[4][5];
146         if (s.length() == 0) {
147             matrix[0][0] = 1;
148             matrix[1][1] = 1;
149             matrix[2][2] = 1;
150             matrix[3][3] = 1;
151             return matrix;
152         }
153         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(s, " ,");
154         int n = 0;
155         try {
156             while (n < 20 && tokens.hasMoreTokens()) {
157                 matrix[n/5][n%5]
158                     = SVGUtilities.convertSVGNumber(tokens.nextToken());
159                 n++;
160             }
161         } catch (NumberFormatException JavaDoc ex) {
162             throw new BridgeException
163                 (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
164                  new Object JavaDoc[] {SVG_VALUES_ATTRIBUTE, s, ex});
165         }
166         if (n != 20 || tokens.hasMoreTokens()) {
167             throw new BridgeException
168                 (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
169                  new Object JavaDoc[] {SVG_VALUES_ATTRIBUTE, s});
170         }
171
172         for (int i = 0; i < 4; ++i) {
173             matrix[i][4] *= 255;
174         }
175         return matrix;
176     }
177
178     /**
179      * Converts the 'values' attribute of the specified feColorMatrix
180      * filter primitive element for the 'saturate' type.
181      *
182      * @param filterElement the filter element
183      */

184     protected static float convertValuesToSaturate(Element JavaDoc filterElement) {
185         String JavaDoc s = filterElement.getAttributeNS(null, SVG_VALUES_ATTRIBUTE);
186         if (s.length() == 0)
187             return 1; // default is 1
188
try {
189             return SVGUtilities.convertSVGNumber(s);
190         } catch (NumberFormatException JavaDoc ex) {
191             throw new BridgeException
192                 (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
193                  new Object JavaDoc [] {SVG_VALUES_ATTRIBUTE, s});
194         }
195     }
196
197     /**
198      * Converts the 'values' attribute of the specified feColorMatrix
199      * filter primitive element for the 'hueRotate' type.
200      *
201      * @param filterElement the filter element
202      */

203     protected static float convertValuesToHueRotate(Element JavaDoc filterElement) {
204         String JavaDoc s = filterElement.getAttributeNS(null, SVG_VALUES_ATTRIBUTE);
205         if (s.length() == 0)
206             return 0; // default is 0
207
try {
208             return (float)(SVGUtilities.convertSVGNumber(s)*Math.PI)/180f;
209         } catch (NumberFormatException JavaDoc ex) {
210             throw new BridgeException
211                 (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
212                  new Object JavaDoc [] {SVG_VALUES_ATTRIBUTE, s});
213         }
214     }
215
216     /**
217      * Converts the type of the specified color matrix filter primitive.
218      *
219      * @param filterElement the filter element
220      */

221     protected static int convertType(Element JavaDoc filterElement) {
222         String JavaDoc s = filterElement.getAttributeNS(null, SVG_TYPE_ATTRIBUTE);
223         if (s.length() == 0) {
224             return ColorMatrixRable.TYPE_MATRIX;
225         }
226         if (SVG_HUE_ROTATE_VALUE.equals(s)) {
227             return ColorMatrixRable.TYPE_HUE_ROTATE;
228         }
229         if (SVG_LUMINANCE_TO_ALPHA_VALUE.equals(s)) {
230             return ColorMatrixRable.TYPE_LUMINANCE_TO_ALPHA;
231         }
232         if (SVG_MATRIX_VALUE.equals(s)) {
233             return ColorMatrixRable.TYPE_MATRIX;
234         }
235         if (SVG_SATURATE_VALUE.equals(s)) {
236             return ColorMatrixRable.TYPE_SATURATE;
237         }
238         throw new BridgeException(filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
239                                   new Object JavaDoc[] {SVG_TYPE_ATTRIBUTE, s});
240     }
241 }
242
Popular Tags