1 18 package org.apache.batik.bridge; 19 20 import java.awt.geom.Rectangle2D ; 21 import java.util.Map ; 22 import java.util.StringTokenizer ; 23 24 import org.apache.batik.ext.awt.image.PadMode; 25 import org.apache.batik.ext.awt.image.renderable.Filter; 26 import org.apache.batik.ext.awt.image.renderable.MorphologyRable8Bit; 27 import org.apache.batik.ext.awt.image.renderable.PadRable; 28 import org.apache.batik.ext.awt.image.renderable.PadRable8Bit; 29 import org.apache.batik.gvt.GraphicsNode; 30 import org.w3c.dom.Element ; 31 32 38 public class SVGFeMorphologyElementBridge 39 extends AbstractSVGFilterPrimitiveElementBridge { 40 41 42 45 public SVGFeMorphologyElementBridge() {} 46 47 50 public String getLocalName() { 51 return SVG_FE_MORPHOLOGY_TAG; 52 } 53 54 72 public Filter createFilter(BridgeContext ctx, 73 Element filterElement, 74 Element filteredElement, 75 GraphicsNode filteredNode, 76 Filter inputFilter, 77 Rectangle2D filterRegion, 78 Map filterMap) { 79 80 float [] radii = convertRadius(filterElement); 82 if (radii[0] == 0 || radii[1] == 0) { 83 return null; } 85 86 boolean isDilate = convertOperator(filterElement); 88 89 Filter in = getIn(filterElement, 91 filteredElement, 92 filteredNode, 93 inputFilter, 94 filterMap, 95 ctx); 96 if (in == null) { 97 return null; } 99 100 Rectangle2D defaultRegion = in.getBounds2D(); 104 Rectangle2D primitiveRegion 105 = SVGUtilities.convertFilterPrimitiveRegion(filterElement, 106 filteredElement, 107 filteredNode, 108 defaultRegion, 109 filterRegion, 110 ctx); 111 112 PadRable pad = new PadRable8Bit(in, primitiveRegion, PadMode.ZERO_PAD); 115 116 Filter morphology 118 = new MorphologyRable8Bit(pad, radii[0], radii[1], isDilate); 119 120 handleColorInterpolationFilters(morphology, filterElement); 122 123 PadRable filter = new PadRable8Bit 124 (morphology, primitiveRegion, PadMode.ZERO_PAD); 125 126 updateFilterMap(filterElement, filter, filterMap); 128 129 return filter; 130 } 131 132 138 protected static float [] convertRadius(Element filterElement) { 139 String s = filterElement.getAttributeNS(null, SVG_RADIUS_ATTRIBUTE); 140 if (s.length() == 0) { 141 return new float[] {0, 0}; 142 } 143 float [] radii = new float[2]; 144 StringTokenizer tokens = new StringTokenizer (s, " ,"); 145 try { 146 radii[0] = SVGUtilities.convertSVGNumber(tokens.nextToken()); 147 if (tokens.hasMoreTokens()) { 148 radii[1] = SVGUtilities.convertSVGNumber(tokens.nextToken()); 149 } else { 150 radii[1] = radii[0]; 151 } 152 } catch (NumberFormatException ex) { 153 throw new BridgeException 154 (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 155 new Object [] {SVG_RADIUS_ATTRIBUTE, s, ex}); 156 } 157 if (tokens.hasMoreTokens() || radii[0] < 0 || radii[1] < 0) { 158 throw new BridgeException 159 (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 160 new Object [] {SVG_RADIUS_ATTRIBUTE, s}); 161 } 162 return radii; 163 } 164 165 171 protected static boolean convertOperator(Element filterElement) { 172 String s = filterElement.getAttributeNS(null, SVG_OPERATOR_ATTRIBUTE); 173 if (s.length() == 0) { 174 return false; 175 } 176 if (SVG_ERODE_VALUE.equals(s)) { 177 return false; 178 } 179 if (SVG_DILATE_VALUE.equals(s)) { 180 return true; 181 } 182 throw new BridgeException(filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 183 new Object [] {SVG_OPERATOR_ATTRIBUTE, s}); 184 } 185 186 } 187 | Popular Tags |