1 18 package org.apache.batik.svggen; 19 20 import java.awt.Rectangle ; 21 import java.awt.image.BufferedImageOp ; 22 import java.awt.image.ConvolveOp ; 23 import java.awt.image.Kernel ; 24 25 import org.w3c.dom.Document ; 26 import org.w3c.dom.Element ; 27 28 36 public class SVGConvolveOp extends AbstractSVGFilterConverter { 37 40 public SVGConvolveOp(SVGGeneratorContext generatorContext) { 41 super(generatorContext); 42 } 43 44 56 public SVGFilterDescriptor toSVG(BufferedImageOp filter, 57 Rectangle filterRect){ 58 if(filter instanceof ConvolveOp ) 59 return toSVG((ConvolveOp )filter); 60 else 61 return null; 62 } 63 64 70 public SVGFilterDescriptor toSVG(ConvolveOp convolveOp){ 71 SVGFilterDescriptor filterDesc = 73 (SVGFilterDescriptor)descMap.get(convolveOp); 74 Document domFactory = generatorContext.domFactory; 75 76 if (filterDesc == null) { 77 Kernel kernel = convolveOp.getKernel(); 82 Element filterDef = 83 domFactory.createElementNS(SVG_NAMESPACE_URI, SVG_FILTER_TAG); 84 Element feConvolveMatrixDef = 85 domFactory.createElementNS(SVG_NAMESPACE_URI, 86 SVG_FE_CONVOLVE_MATRIX_TAG); 87 88 feConvolveMatrixDef.setAttributeNS(null, SVG_ORDER_ATTRIBUTE, 90 kernel.getWidth() + SPACE + 91 kernel.getHeight()); 92 93 StringBuffer kernelMatrixBuf = new StringBuffer (); 95 float data[] = kernel.getKernelData(null); 96 for(int i=0; i<data.length; i++){ 97 kernelMatrixBuf.append(doubleString(data[i])); 98 kernelMatrixBuf.append(SPACE); 99 } 100 101 feConvolveMatrixDef. 102 setAttributeNS(null, SVG_KERNEL_MATRIX_ATTRIBUTE, 103 kernelMatrixBuf.toString().trim()); 104 105 filterDef.appendChild(feConvolveMatrixDef); 106 107 filterDef.setAttributeNS(null, ATTR_ID, 108 generatorContext.idGenerator. 109 generateID(ID_PREFIX_FE_CONVOLVE_MATRIX)); 110 111 if(convolveOp.getEdgeCondition() == ConvolveOp.EDGE_NO_OP) 113 feConvolveMatrixDef.setAttributeNS(null, SVG_EDGE_MODE_ATTRIBUTE, 114 SVG_DUPLICATE_VALUE); 115 else 116 feConvolveMatrixDef.setAttributeNS(null, SVG_EDGE_MODE_ATTRIBUTE, 117 SVG_NONE_VALUE); 118 119 123 StringBuffer filterAttrBuf = new StringBuffer (URL_PREFIX); 125 filterAttrBuf.append(SIGN_POUND); 126 filterAttrBuf.append(filterDef.getAttributeNS(null, ATTR_ID)); 127 filterAttrBuf.append(URL_SUFFIX); 128 129 filterDesc = new SVGFilterDescriptor(filterAttrBuf.toString(), 130 filterDef); 131 132 defSet.add(filterDef); 133 descMap.put(convolveOp, filterDesc); 134 } 135 136 return filterDesc; 137 } 138 } 139 | Popular Tags |