1 18 package org.apache.batik.ext.awt.image.renderable; 19 20 import java.awt.image.RenderedImage ; 21 import java.awt.image.renderable.RenderContext ; 22 23 import org.apache.batik.ext.awt.image.rendered.ColorMatrixRed; 24 25 32 public class ColorMatrixRable8Bit 33 extends AbstractColorInterpolationRable 34 implements ColorMatrixRable { 35 38 private static float MATRIX_LUMINANCE_TO_ALPHA[][] 39 = { 40 {0, 0, 0, 0, 0}, 41 {0, 0, 0, 0, 0}, 42 {0, 0, 0, 0, 0}, 43 {0.2125f, 0.7154f, 0.0721f, 0, 0} 44 }; 45 46 49 private int type; 50 51 54 private float matrix[][]; 55 56 59 public void setSource(Filter src){ 60 init(src, null); 61 } 62 63 66 public Filter getSource(){ 67 return (Filter)getSources().get(0); 68 } 69 70 75 public int getType(){ 76 return type; 77 } 78 79 83 public float[][] getMatrix(){ 84 return matrix; 85 } 86 87 91 private ColorMatrixRable8Bit(){ 92 } 93 94 97 public static ColorMatrixRable buildMatrix(float matrix[][]){ 98 if(matrix == null){ 99 throw new IllegalArgumentException (); 100 } 101 102 if(matrix.length != 4){ 103 throw new IllegalArgumentException (); 104 } 105 106 float newMatrix[][] = new float[4][]; 107 108 for(int i=0; i<4; i++){ 109 float m[] = matrix[i]; 110 if(m == null){ 111 throw new IllegalArgumentException (); 112 } 113 if(m.length != 5){ 114 throw new IllegalArgumentException (); 115 } 116 newMatrix[i] = new float[5]; 117 for(int j=0; j<5; j++){ 118 newMatrix[i][j] = m[j]; 119 } 120 } 121 122 127 128 ColorMatrixRable8Bit filter 129 = new ColorMatrixRable8Bit(); 130 filter.type = TYPE_MATRIX; 131 filter.matrix = newMatrix; 132 return filter; 133 } 134 135 138 public static ColorMatrixRable buildSaturate(float s){ 139 ColorMatrixRable8Bit filter 140 = new ColorMatrixRable8Bit(); 141 filter.type = TYPE_SATURATE; 142 filter.matrix = new float[][] { 143 { 0.213f+0.787f*s, 0.715f-0.715f*s, 0.072f-0.072f*s, 0, 0 }, 144 { 0.213f-0.213f*s, 0.715f+0.285f*s, 0.072f-0.072f*s, 0, 0 }, 145 { 0.213f-0.213f*s, 0.715f-0.715f*s, 0.072f+0.928f*s, 0, 0 }, 146 { 0, 0, 0, 1, 0 } 147 }; 148 return filter; 149 } 150 151 155 public static ColorMatrixRable buildHueRotate(float a){ 156 ColorMatrixRable8Bit filter 157 = new ColorMatrixRable8Bit(); 158 filter.type = TYPE_HUE_ROTATE; 159 160 float cos = (float)Math.cos(a); 161 float sin = (float)Math.sin(a); 162 163 165 float a00 = 0.213f + cos*0.787f - sin*0.213f; 166 float a10 = 0.213f - cos*0.212f + sin*0.143f; 167 float a20 = 0.213f - cos*0.213f - sin*0.787f; 168 169 float a01 = 0.715f - cos*0.715f - sin*0.715f; 170 float a11 = 0.715f + cos*0.285f + sin*0.140f; 171 float a21 = 0.715f - cos*0.715f + sin*0.715f; 172 173 float a02 = 0.072f - cos*0.072f + sin*0.928f; 174 float a12 = 0.072f - cos*0.072f - sin*0.283f; 175 float a22 = 0.072f + cos*0.928f + sin*0.072f; 176 177 filter.matrix = new float[][] { 178 { a00, a01, a02, 0, 0 }, 179 { a10, a11, a12, 0, 0 }, 180 { a20, a21, a22, 0, 0 }, 181 { 0, 0, 0, 1, 0 }}; 182 183 188 189 return filter; 190 } 191 192 195 public static ColorMatrixRable buildLuminanceToAlpha(){ 196 ColorMatrixRable8Bit filter 197 = new ColorMatrixRable8Bit(); 198 filter.type = TYPE_LUMINANCE_TO_ALPHA; 199 filter.matrix = MATRIX_LUMINANCE_TO_ALPHA; 200 return filter; 201 } 202 203 public RenderedImage createRendering(RenderContext rc) { 204 RenderedImage srcRI = getSource().createRendering(rc); 208 209 if(srcRI == null) 210 return null; 211 212 return new ColorMatrixRed(convertSourceCS(srcRI), matrix); 213 } 214 } 215 | Popular Tags |