1 18 package org.apache.batik.svggen; 19 20 import java.awt.Rectangle ; 21 import java.awt.image.BufferedImageOp ; 22 import java.awt.image.RescaleOp ; 23 24 import org.w3c.dom.Document ; 25 import org.w3c.dom.Element ; 26 27 39 public class SVGRescaleOp extends AbstractSVGFilterConverter { 40 41 44 public SVGRescaleOp(SVGGeneratorContext generatorContext) { 45 super(generatorContext); 46 } 47 48 60 public SVGFilterDescriptor toSVG(BufferedImageOp filter, 61 Rectangle filterRect) { 62 if(filter instanceof RescaleOp ) 63 return toSVG((RescaleOp )filter); 64 else 65 return null; 66 } 67 68 74 public SVGFilterDescriptor toSVG(RescaleOp rescaleOp) { 75 SVGFilterDescriptor filterDesc = 77 (SVGFilterDescriptor)descMap.get(rescaleOp); 78 79 Document domFactory = generatorContext.domFactory; 80 81 if (filterDesc == null) { 82 Element filterDef = domFactory.createElementNS(SVG_NAMESPACE_URI, 87 SVG_FILTER_TAG); 88 Element feComponentTransferDef = 89 domFactory.createElementNS(SVG_NAMESPACE_URI, 90 SVG_FE_COMPONENT_TRANSFER_TAG); 91 92 float offsets[] = rescaleOp.getOffsets(null); 103 float scaleFactors[] = rescaleOp.getScaleFactors(null); 104 if(offsets.length != scaleFactors.length) 105 throw new SVGGraphics2DRuntimeException(ERR_SCALE_FACTORS_AND_OFFSETS_MISMATCH); 106 107 if(offsets.length != 1 && 108 offsets.length != 3 && 109 offsets.length != 4) 110 throw new SVGGraphics2DRuntimeException(ERR_ILLEGAL_BUFFERED_IMAGE_RESCALE_OP); 111 112 Element feFuncR = domFactory.createElementNS(SVG_NAMESPACE_URI, 113 SVG_FE_FUNC_R_TAG); 114 Element feFuncG = domFactory.createElementNS(SVG_NAMESPACE_URI, 115 SVG_FE_FUNC_G_TAG); 116 Element feFuncB = domFactory.createElementNS(SVG_NAMESPACE_URI, 117 SVG_FE_FUNC_B_TAG); 118 Element feFuncA = null; 119 String type = SVG_LINEAR_VALUE; 120 121 if(offsets.length == 1){ 122 String slope = doubleString(scaleFactors[0]); 123 String intercept = doubleString(offsets[0]); 124 feFuncR.setAttributeNS(null, SVG_TYPE_ATTRIBUTE, type); 125 feFuncG.setAttributeNS(null, SVG_TYPE_ATTRIBUTE, type); 126 feFuncB.setAttributeNS(null, SVG_TYPE_ATTRIBUTE, type); 127 feFuncR.setAttributeNS(null, SVG_SLOPE_ATTRIBUTE, slope); 128 feFuncG.setAttributeNS(null, SVG_SLOPE_ATTRIBUTE, slope); 129 feFuncB.setAttributeNS(null, SVG_SLOPE_ATTRIBUTE, slope); 130 feFuncR.setAttributeNS(null, SVG_INTERCEPT_ATTRIBUTE, intercept); 131 feFuncG.setAttributeNS(null, SVG_INTERCEPT_ATTRIBUTE, intercept); 132 feFuncB.setAttributeNS(null, SVG_INTERCEPT_ATTRIBUTE, intercept); 133 } 134 else if(offsets.length >= 3){ 135 feFuncR.setAttributeNS(null, SVG_TYPE_ATTRIBUTE, type); 136 feFuncG.setAttributeNS(null, SVG_TYPE_ATTRIBUTE, type); 137 feFuncB.setAttributeNS(null, SVG_TYPE_ATTRIBUTE, type); 138 feFuncR.setAttributeNS(null, SVG_SLOPE_ATTRIBUTE, 139 doubleString(scaleFactors[0])); 140 feFuncG.setAttributeNS(null, SVG_SLOPE_ATTRIBUTE, 141 doubleString(scaleFactors[1])); 142 feFuncB.setAttributeNS(null, SVG_SLOPE_ATTRIBUTE, 143 doubleString(scaleFactors[2])); 144 feFuncR.setAttributeNS(null, SVG_INTERCEPT_ATTRIBUTE, 145 doubleString(offsets[0])); 146 feFuncG.setAttributeNS(null, SVG_INTERCEPT_ATTRIBUTE, 147 doubleString(offsets[1])); 148 feFuncB.setAttributeNS(null, SVG_INTERCEPT_ATTRIBUTE, 149 doubleString(offsets[2])); 150 151 if(offsets.length == 4){ 152 feFuncA = domFactory.createElementNS(SVG_NAMESPACE_URI, 153 SVG_FE_FUNC_A_TAG); 154 feFuncA.setAttributeNS(null, SVG_TYPE_ATTRIBUTE, type); 155 feFuncA.setAttributeNS(null, SVG_SLOPE_ATTRIBUTE, 156 doubleString(scaleFactors[3])); 157 feFuncA.setAttributeNS(null, SVG_INTERCEPT_ATTRIBUTE, 158 doubleString(offsets[3])); 159 } 160 } 161 162 feComponentTransferDef.appendChild(feFuncR); 163 feComponentTransferDef.appendChild(feFuncG); 164 feComponentTransferDef.appendChild(feFuncB); 165 if(feFuncA != null) 166 feComponentTransferDef.appendChild(feFuncA); 167 168 filterDef.appendChild(feComponentTransferDef); 169 170 filterDef. 171 setAttributeNS(null, ATTR_ID, 172 generatorContext.idGenerator. 173 generateID(ID_PREFIX_FE_COMPONENT_TRANSFER)); 174 175 179 StringBuffer filterAttrBuf = new StringBuffer (URL_PREFIX); 181 filterAttrBuf.append(SIGN_POUND); 182 filterAttrBuf.append(filterDef.getAttributeNS(null, ATTR_ID)); 183 filterAttrBuf.append(URL_SUFFIX); 184 185 filterDesc = new SVGFilterDescriptor(filterAttrBuf.toString(), 186 filterDef); 187 188 defSet.add(filterDef); 189 descMap.put(rescaleOp, filterDesc); 190 } 191 192 return filterDesc; 193 } 194 } 195 | Popular Tags |