001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.bridge;
020:
021: import java.awt.geom.Rectangle2D;
022: import java.util.Map;
023: import java.util.StringTokenizer;
024:
025: import org.apache.batik.ext.awt.image.PadMode;
026: import org.apache.batik.ext.awt.image.renderable.Filter;
027: import org.apache.batik.ext.awt.image.renderable.GaussianBlurRable8Bit;
028: import org.apache.batik.ext.awt.image.renderable.PadRable;
029: import org.apache.batik.ext.awt.image.renderable.PadRable8Bit;
030: import org.apache.batik.gvt.GraphicsNode;
031: import org.w3c.dom.Element;
032:
033: /**
034: * Bridge class for the <feGaussianBlur> element.
035: *
036: * @author <a href="mailto:tkormann@apache.org">Thierry Kormann</a>
037: * @version $Id: SVGFeGaussianBlurElementBridge.java 501922 2007-01-31 17:47:47Z dvholten $
038: */
039: public class SVGFeGaussianBlurElementBridge extends
040: AbstractSVGFilterPrimitiveElementBridge {
041:
042: /**
043: * Constructs a new bridge for the <feGaussianBlur> element.
044: */
045: public SVGFeGaussianBlurElementBridge() {
046: }
047:
048: /**
049: * Returns 'feGaussianBlur'.
050: */
051: public String getLocalName() {
052: return SVG_FE_GAUSSIAN_BLUR_TAG;
053: }
054:
055: /**
056: * Creates a <tt>Filter</tt> primitive according to the specified
057: * parameters.
058: *
059: * @param ctx the bridge context to use
060: * @param filterElement the element that defines a filter
061: * @param filteredElement the element that references the filter
062: * @param filteredNode the graphics node to filter
063: *
064: * @param inputFilter the <tt>Filter</tt> that represents the current
065: * filter input if the filter chain.
066: * @param filterRegion the filter area defined for the filter chain
067: * the new node will be part of.
068: * @param filterMap a map where the mediator can map a name to the
069: * <tt>Filter</tt> it creates. Other <tt>FilterBridge</tt>s
070: * can then access a filter node from the filterMap if they
071: * know its name.
072: */
073: public Filter createFilter(BridgeContext ctx,
074: Element filterElement, Element filteredElement,
075: GraphicsNode filteredNode, Filter inputFilter,
076: Rectangle2D filterRegion, Map filterMap) {
077:
078: // 'stdDeviation' attribute - default is [0, 0]
079: float[] stdDeviationXY = convertStdDeviation(filterElement, ctx);
080: if (stdDeviationXY[0] < 0 || stdDeviationXY[1] < 0) {
081: throw new BridgeException(ctx, filterElement,
082: ERR_ATTRIBUTE_VALUE_MALFORMED, new Object[] {
083: SVG_STD_DEVIATION_ATTRIBUTE,
084: String.valueOf(stdDeviationXY[0])
085: + stdDeviationXY[1] });
086: }
087:
088: // 'in' attribute
089: Filter in = getIn(filterElement, filteredElement, filteredNode,
090: inputFilter, filterMap, ctx);
091: if (in == null) {
092: return null; // disable the filter
093: }
094:
095: // Default region is the size of in (if in is SourceGraphic or
096: // SourceAlpha it will already include a pad/crop to the
097: // proper filter region size).
098: Rectangle2D defaultRegion = in.getBounds2D();
099: Rectangle2D primitiveRegion = SVGUtilities
100: .convertFilterPrimitiveRegion(filterElement,
101: filteredElement, filteredNode, defaultRegion,
102: filterRegion, ctx);
103:
104: // Take the filter primitive region into account, we need to
105: // pad/crop the input and output.
106: PadRable pad = new PadRable8Bit(in, primitiveRegion,
107: PadMode.ZERO_PAD);
108:
109: // build filter
110: Filter blur = new GaussianBlurRable8Bit(pad, stdDeviationXY[0],
111: stdDeviationXY[1]);
112:
113: // handle the 'color-interpolation-filters' property
114: handleColorInterpolationFilters(blur, filterElement);
115:
116: PadRable filter = new PadRable8Bit(blur, primitiveRegion,
117: PadMode.ZERO_PAD);
118:
119: // update the filter Map
120: updateFilterMap(filterElement, filter, filterMap);
121:
122: return filter;
123: }
124:
125: /**
126: * Returns the standard deviation of the specified feGaussianBlur
127: * filter primitive element.
128: *
129: * @param filterElement the feGaussianBlur filter primitive element
130: * @param ctx the BridgeContext to use for error information
131: */
132: protected static float[] convertStdDeviation(Element filterElement,
133: BridgeContext ctx) {
134: String s = filterElement.getAttributeNS(null,
135: SVG_STD_DEVIATION_ATTRIBUTE);
136: if (s.length() == 0) {
137: return new float[] { 0, 0 };
138: }
139: float[] stdDevs = new float[2];
140: StringTokenizer tokens = new StringTokenizer(s, " ,");
141: try {
142: stdDevs[0] = SVGUtilities.convertSVGNumber(tokens
143: .nextToken());
144: if (tokens.hasMoreTokens()) {
145: stdDevs[1] = SVGUtilities.convertSVGNumber(tokens
146: .nextToken());
147: } else {
148: stdDevs[1] = stdDevs[0];
149: }
150: } catch (NumberFormatException nfEx) {
151: throw new BridgeException(ctx, filterElement, nfEx,
152: ERR_ATTRIBUTE_VALUE_MALFORMED, new Object[] {
153: SVG_STD_DEVIATION_ATTRIBUTE, s, nfEx });
154: }
155: if (tokens.hasMoreTokens()) {
156: throw new BridgeException(ctx, filterElement,
157: ERR_ATTRIBUTE_VALUE_MALFORMED, new Object[] {
158: SVG_STD_DEVIATION_ATTRIBUTE, s });
159: }
160: return stdDevs;
161: }
162: }
|