KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > extension > svg > BatikHistogramNormalizationElementBridge


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.extension.svg;
19
20 import java.awt.geom.Rectangle2D JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.batik.bridge.AbstractSVGFilterPrimitiveElementBridge;
24 import org.apache.batik.bridge.Bridge;
25 import org.apache.batik.bridge.BridgeContext;
26 import org.apache.batik.bridge.BridgeException;
27 import org.apache.batik.bridge.SVGUtilities;
28 import org.apache.batik.ext.awt.image.PadMode;
29 import org.apache.batik.ext.awt.image.renderable.Filter;
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 a histogram normalization element.
36  *
37  * @author <a HREF="mailto:thomas.deweese@kodak.com">Thomas Deweese</a>
38  */

39 public class BatikHistogramNormalizationElementBridge
40     extends AbstractSVGFilterPrimitiveElementBridge
41     implements BatikExtConstants {
42
43     /**
44      * Constructs a new bridge for the &lt;histogramNormalization> element.
45      */

46     public BatikHistogramNormalizationElementBridge() { /* nothing */ }
47
48     /**
49      * Returns the SVG namespace URI.
50      */

51     public String JavaDoc getNamespaceURI() {
52         return BATIK_EXT_NAMESPACE_URI;
53     }
54
55     /**
56      * Returns 'histogramNormalization'.
57      */

58     public String JavaDoc getLocalName() {
59         return BATIK_EXT_HISTOGRAM_NORMALIZATION_TAG;
60     }
61
62     /**
63      * Returns a new instance of this bridge.
64      */

65     public Bridge getInstance() {
66         return new BatikHistogramNormalizationElementBridge();
67     }
68
69     /**
70      * Creates a <tt>Filter</tt> primitive according to the specified
71      * parameters.
72      *
73      * @param ctx the bridge context to use
74      * @param filterElement the element that defines a filter
75      * @param filteredElement the element that references the filter
76      * @param filteredNode the graphics node to filter
77      *
78      * @param inputFilter the <tt>Filter</tt> that represents the current
79      * filter input if the filter chain.
80      * @param filterRegion the filter area defined for the filter chain
81      * the new node will be part of.
82      * @param filterMap a map where the mediator can map a name to the
83      * <tt>Filter</tt> it creates. Other <tt>FilterBridge</tt>s
84      * can then access a filter node from the filterMap if they
85      * know its name.
86      */

87     public Filter createFilter(BridgeContext ctx,
88                                Element JavaDoc filterElement,
89                                Element JavaDoc filteredElement,
90                                GraphicsNode filteredNode,
91                                Filter inputFilter,
92                                Rectangle2D JavaDoc filterRegion,
93                                Map JavaDoc filterMap) {
94
95         // 'in' attribute
96
Filter in = getIn(filterElement,
97                           filteredElement,
98                           filteredNode,
99                           inputFilter,
100                           filterMap,
101                           ctx);
102         if (in == null) {
103             return null; // disable the filter
104
}
105
106         // The default region is the union of the input sources
107
// regions unless 'in' is 'SourceGraphic' in which case the
108
// default region is the filterChain's region
109
Filter sourceGraphics = (Filter)filterMap.get(SVG_SOURCE_GRAPHIC_VALUE);
110         Rectangle2D JavaDoc defaultRegion;
111         if (in == sourceGraphics) {
112             defaultRegion = filterRegion;
113         } else {
114             defaultRegion = in.getBounds2D();
115         }
116
117         Rectangle2D JavaDoc primitiveRegion
118             = SVGUtilities.convertFilterPrimitiveRegion(filterElement,
119                                                         filteredElement,
120                                                         filteredNode,
121                                                         defaultRegion,
122                                                         filterRegion,
123                                                         ctx);
124
125         float trim = 1;
126         String JavaDoc s = filterElement.getAttributeNS
127             (null, BATIK_EXT_TRIM_ATTRIBUTE);
128
129         if (s.length() != 0) {
130             try {
131                 trim = SVGUtilities.convertSVGNumber(s);
132             } catch (NumberFormatException JavaDoc ex) {
133                 throw new BridgeException
134                     (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
135                      new Object JavaDoc[] {BATIK_EXT_TRIM_ATTRIBUTE, s});
136             }
137         }
138
139         if (trim < 0) trim =0;
140         else if (trim > 100) trim=100;
141
142         Filter filter = in;
143         filter = new BatikHistogramNormalizationFilter8Bit(filter, trim/100);
144         
145         filter = new PadRable8Bit(filter, primitiveRegion, PadMode.ZERO_PAD);
146
147         // update the filter Map
148
updateFilterMap(filterElement, filter, filterMap);
149
150         // handle the 'color-interpolation-filters' property
151
handleColorInterpolationFilters(filter, filterElement);
152
153         return filter;
154     }
155
156     /**
157      * Stolen from AbstractSVGFilterPrimitiveElementBridge.
158      * Converts on the specified filter primitive element, the specified
159      * attribute that represents an integer and with the specified
160      * default value.
161      *
162      * @param filterElement the filter primitive element
163      * @param attrName the name of the attribute
164      * @param defaultValue the default value of the attribute
165      */

166     protected static int convertSides(Element JavaDoc filterElement,
167                                         String JavaDoc attrName,
168                                         int defaultValue) {
169         String JavaDoc s = filterElement.getAttributeNS(null, attrName);
170         if (s.length() == 0) {
171             return defaultValue;
172         } else {
173             int ret = 0;
174             try {
175                 ret = SVGUtilities.convertSVGInteger(s);
176             } catch (NumberFormatException JavaDoc ex) {
177                 throw new BridgeException
178                     (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
179                      new Object JavaDoc[] {attrName, s});
180             }
181
182             if (ret <3)
183                 throw new BridgeException
184                     (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
185                      new Object JavaDoc[] {attrName, s});
186             return ret;
187         }
188     }
189 }
190
Popular Tags