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.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 ; 32 import org.w3c.dom.Node ; 33 34 40 public class SVGFeComponentTransferElementBridge 41 extends AbstractSVGFilterPrimitiveElementBridge { 42 43 46 public SVGFeComponentTransferElementBridge() {} 47 48 51 public String getLocalName() { 52 return SVG_FE_COMPONENT_TRANSFER_TAG; 53 } 54 55 73 public Filter createFilter(BridgeContext ctx, 74 Element filterElement, 75 Element filteredElement, 76 GraphicsNode filteredNode, 77 Filter inputFilter, 78 Rectangle2D filterRegion, 79 Map filterMap) { 80 81 Filter in = getIn(filterElement, 83 filteredElement, 84 filteredNode, 85 inputFilter, 86 filterMap, 87 ctx); 88 if (in == null) { 89 return null; } 91 92 Rectangle2D defaultRegion = in.getBounds2D(); 96 Rectangle2D primitiveRegion 97 = SVGUtilities.convertFilterPrimitiveRegion(filterElement, 98 filteredElement, 99 filteredNode, 100 defaultRegion, 101 filterRegion, 102 ctx); 103 104 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 e = (Element )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 handleColorInterpolationFilters(filter, filterElement); 145 146 filter = new PadRable8Bit(filter, primitiveRegion, PadMode.ZERO_PAD); 147 148 updateFilterMap(filterElement, filter, filterMap); 150 151 return filter; 152 } 153 154 157 public static class SVGFeFuncAElementBridge extends SVGFeFuncElementBridge { 158 159 162 public SVGFeFuncAElementBridge() {} 163 164 167 public String getLocalName() { 168 return SVG_FE_FUNC_A_TAG; 169 } 170 } 171 172 175 public static class SVGFeFuncRElementBridge extends SVGFeFuncElementBridge { 176 177 180 public SVGFeFuncRElementBridge() {} 181 182 185 public String getLocalName() { 186 return SVG_FE_FUNC_R_TAG; 187 } 188 } 189 190 193 public static class SVGFeFuncGElementBridge extends SVGFeFuncElementBridge { 194 195 198 public SVGFeFuncGElementBridge() {} 199 200 203 public String getLocalName() { 204 return SVG_FE_FUNC_G_TAG; 205 } 206 } 207 208 211 public static class SVGFeFuncBElementBridge extends SVGFeFuncElementBridge { 212 213 216 public SVGFeFuncBElementBridge() {} 217 218 221 public String getLocalName() { 222 return SVG_FE_FUNC_B_TAG; 223 } 224 } 225 226 229 protected static abstract class SVGFeFuncElementBridge 230 extends AbstractSVGBridge { 231 232 235 protected SVGFeFuncElementBridge() {} 236 237 244 public ComponentTransferFunction createComponentTransferFunction 245 (Element filterElement, Element 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 float amplitude 263 = convertNumber(funcElement, SVG_AMPLITUDE_ATTRIBUTE, 1); 264 float exponent 266 = convertNumber(funcElement, SVG_EXPONENT_ATTRIBUTE, 1); 267 float offset 269 = convertNumber(funcElement, SVG_OFFSET_ATTRIBUTE, 0); 270 271 return ConcreteComponentTransferFunction.getGammaTransfer 272 (amplitude, exponent, offset); 273 } 274 case ComponentTransferFunction.LINEAR: { 275 float slope 277 = convertNumber(funcElement, SVG_SLOPE_ATTRIBUTE, 1); 278 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 (); } 296 297 } 298 299 305 protected static float [] convertTableValues(Element e) { 306 String s = e.getAttributeNS(null, SVG_TABLE_VALUES_ATTRIBUTE); 307 if (s.length() == 0) { 308 return null; 309 } 310 StringTokenizer tokens = new StringTokenizer (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 ex) { 317 throw new BridgeException 318 (e, ERR_ATTRIBUTE_VALUE_MALFORMED, 319 new Object [] {SVG_TABLE_VALUES_ATTRIBUTE, s}); 320 } 321 return v; 322 } 323 324 330 protected static int convertType(Element e) { 331 String s = e.getAttributeNS(null, SVG_TYPE_ATTRIBUTE); 332 if (s.length() == 0) { 333 throw new BridgeException(e, ERR_ATTRIBUTE_MISSING, 334 new Object [] {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 [] {SVG_TYPE_ATTRIBUTE, s}); 353 } 354 } 355 } 356 | Popular Tags |