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.ColorMatrixRable; 26 import org.apache.batik.ext.awt.image.renderable.ColorMatrixRable8Bit; 27 import org.apache.batik.ext.awt.image.renderable.Filter; 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 SVGFeColorMatrixElementBridge 39 extends AbstractSVGFilterPrimitiveElementBridge { 40 41 44 public SVGFeColorMatrixElementBridge() {} 45 46 49 public String getLocalName() { 50 return SVG_FE_COLOR_MATRIX_TAG; 51 } 52 53 71 public Filter createFilter(BridgeContext ctx, 72 Element filterElement, 73 Element filteredElement, 74 GraphicsNode filteredNode, 75 Filter inputFilter, 76 Rectangle2D filterRegion, 77 Map filterMap) { 78 79 Filter in = getIn(filterElement, 81 filteredElement, 82 filteredNode, 83 inputFilter, 84 filterMap, 85 ctx); 86 if (in == null) { 87 return null; } 89 90 Rectangle2D defaultRegion = in.getBounds2D(); 94 Rectangle2D primitiveRegion 95 = SVGUtilities.convertFilterPrimitiveRegion(filterElement, 96 filteredElement, 97 filteredNode, 98 defaultRegion, 99 filterRegion, 100 ctx); 101 102 int type = convertType(filterElement); 103 ColorMatrixRable colorMatrix; 104 switch (type) { 105 case ColorMatrixRable.TYPE_HUE_ROTATE: 106 float a = convertValuesToHueRotate(filterElement); 107 colorMatrix = ColorMatrixRable8Bit.buildHueRotate(a); 108 break; 109 case ColorMatrixRable.TYPE_LUMINANCE_TO_ALPHA: 110 colorMatrix = ColorMatrixRable8Bit.buildLuminanceToAlpha(); 111 break; 112 case ColorMatrixRable.TYPE_MATRIX: 113 float [][] matrix = convertValuesToMatrix(filterElement); 114 colorMatrix = ColorMatrixRable8Bit.buildMatrix(matrix); 115 break; 116 case ColorMatrixRable.TYPE_SATURATE: 117 float s = convertValuesToSaturate(filterElement); 118 colorMatrix = ColorMatrixRable8Bit.buildSaturate(s); 119 break; 120 default: 121 throw new Error (); } 123 colorMatrix.setSource(in); 124 125 handleColorInterpolationFilters(colorMatrix, filterElement); 127 128 Filter filter 129 = new PadRable8Bit(colorMatrix, primitiveRegion, PadMode.ZERO_PAD); 130 131 updateFilterMap(filterElement, filter, filterMap); 133 134 return filter; 135 } 136 137 143 protected static float[][] convertValuesToMatrix(Element filterElement) { 144 String s = filterElement.getAttributeNS(null, SVG_VALUES_ATTRIBUTE); 145 float [][] matrix = new float[4][5]; 146 if (s.length() == 0) { 147 matrix[0][0] = 1; 148 matrix[1][1] = 1; 149 matrix[2][2] = 1; 150 matrix[3][3] = 1; 151 return matrix; 152 } 153 StringTokenizer tokens = new StringTokenizer (s, " ,"); 154 int n = 0; 155 try { 156 while (n < 20 && tokens.hasMoreTokens()) { 157 matrix[n/5][n%5] 158 = SVGUtilities.convertSVGNumber(tokens.nextToken()); 159 n++; 160 } 161 } catch (NumberFormatException ex) { 162 throw new BridgeException 163 (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 164 new Object [] {SVG_VALUES_ATTRIBUTE, s, ex}); 165 } 166 if (n != 20 || tokens.hasMoreTokens()) { 167 throw new BridgeException 168 (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 169 new Object [] {SVG_VALUES_ATTRIBUTE, s}); 170 } 171 172 for (int i = 0; i < 4; ++i) { 173 matrix[i][4] *= 255; 174 } 175 return matrix; 176 } 177 178 184 protected static float convertValuesToSaturate(Element filterElement) { 185 String s = filterElement.getAttributeNS(null, SVG_VALUES_ATTRIBUTE); 186 if (s.length() == 0) 187 return 1; try { 189 return SVGUtilities.convertSVGNumber(s); 190 } catch (NumberFormatException ex) { 191 throw new BridgeException 192 (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 193 new Object [] {SVG_VALUES_ATTRIBUTE, s}); 194 } 195 } 196 197 203 protected static float convertValuesToHueRotate(Element filterElement) { 204 String s = filterElement.getAttributeNS(null, SVG_VALUES_ATTRIBUTE); 205 if (s.length() == 0) 206 return 0; try { 208 return (float)(SVGUtilities.convertSVGNumber(s)*Math.PI)/180f; 209 } catch (NumberFormatException ex) { 210 throw new BridgeException 211 (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 212 new Object [] {SVG_VALUES_ATTRIBUTE, s}); 213 } 214 } 215 216 221 protected static int convertType(Element filterElement) { 222 String s = filterElement.getAttributeNS(null, SVG_TYPE_ATTRIBUTE); 223 if (s.length() == 0) { 224 return ColorMatrixRable.TYPE_MATRIX; 225 } 226 if (SVG_HUE_ROTATE_VALUE.equals(s)) { 227 return ColorMatrixRable.TYPE_HUE_ROTATE; 228 } 229 if (SVG_LUMINANCE_TO_ALPHA_VALUE.equals(s)) { 230 return ColorMatrixRable.TYPE_LUMINANCE_TO_ALPHA; 231 } 232 if (SVG_MATRIX_VALUE.equals(s)) { 233 return ColorMatrixRable.TYPE_MATRIX; 234 } 235 if (SVG_SATURATE_VALUE.equals(s)) { 236 return ColorMatrixRable.TYPE_SATURATE; 237 } 238 throw new BridgeException(filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 239 new Object [] {SVG_TYPE_ATTRIBUTE, s}); 240 } 241 } 242 | Popular Tags |