1 18 package org.apache.batik.ext.awt.image.rendered; 19 20 import java.awt.color.ColorSpace ; 21 import java.awt.image.ColorModel ; 22 import java.awt.image.DataBufferInt ; 23 import java.awt.image.SampleModel ; 24 import java.awt.image.SinglePixelPackedSampleModel ; 25 import java.awt.image.WritableRaster ; 26 27 import org.apache.batik.ext.awt.image.GraphicsUtil; 28 29 34 public class ColorMatrixRed extends AbstractRed{ 35 38 private float[][] matrix; 39 40 public float[][] getMatrix(){ 41 return copyMatrix(matrix); 42 } 43 44 public void setMatrix(float[][] matrix){ 45 float[][] tmp = copyMatrix(matrix); 46 47 if(tmp == null){ 48 throw new IllegalArgumentException (); 49 } 50 51 if(tmp.length != 4){ 52 throw new IllegalArgumentException (); 53 } 54 55 for(int i=0; i<4; i++){ 56 if(tmp[i].length != 5){ 57 throw new IllegalArgumentException ("" + i + " : " + tmp[i].length); 58 } 59 } 60 this.matrix = matrix; 61 } 62 63 private float[][] copyMatrix(float[][] m){ 64 if(m == null){ 65 return null; 66 } 67 68 float[][] cm = new float[m.length][]; 69 for(int i=0; i<m.length; i++){ 70 if(m[i] != null){ 71 cm[i] = new float[m[i].length]; 72 System.arraycopy(m[i], 0, cm[i], 0, m[i].length); 73 } 74 } 75 76 return cm; 77 } 78 79 public ColorMatrixRed(CachableRed src, float[][] matrix){ 80 setMatrix(matrix); 81 82 ColorModel srcCM = src.getColorModel(); 83 ColorSpace srcCS = null; 84 if (srcCM != null) 85 srcCS = srcCM.getColorSpace(); 86 ColorModel cm; 87 if (srcCS == null) 88 cm = GraphicsUtil.Linear_sRGB_Unpre; 89 else { 90 if (srcCS == ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB)) 91 cm = GraphicsUtil.Linear_sRGB_Unpre; 92 else 93 cm = GraphicsUtil.sRGB_Unpre; 94 } 95 96 SampleModel sm = 97 cm.createCompatibleSampleModel(src.getWidth(), 98 src.getHeight()); 99 100 init(src, src.getBounds(), cm, sm, 101 src.getTileGridXOffset(), src.getTileGridYOffset(), null); 102 } 103 104 105 public WritableRaster copyData(WritableRaster wr){ 106 108 CachableRed src = (CachableRed)getSources().elementAt(0); 112 wr = src.copyData(wr); 116 119 ColorModel cm = src.getColorModel(); 121 GraphicsUtil.coerceData(wr, cm, false); 122 123 final int minX = wr.getMinX(); 127 final int minY = wr.getMinY(); 128 final int w = wr.getWidth(); 129 final int h = wr.getHeight(); 130 DataBufferInt dbf = (DataBufferInt )wr.getDataBuffer(); 131 final int[] pixels = dbf.getBankData()[0]; 132 133 SinglePixelPackedSampleModel sppsm; 134 sppsm = (SinglePixelPackedSampleModel )wr.getSampleModel(); 135 136 final int offset = 137 (dbf.getOffset() + 138 sppsm.getOffset(minX-wr.getSampleModelTranslateX(), 139 minY-wr.getSampleModelTranslateY())); 140 141 143 final int scanStride = 144 ((SinglePixelPackedSampleModel )wr.getSampleModel()) 145 .getScanlineStride(); 146 final int adjust = scanStride - w; 147 int p = offset; 148 int r=0, g=0, b=0, a=0, dr=0, dg=0, db=0, da=0; 149 int i=0, j=0; 150 int pel = 0; 151 152 final float a00=matrix[0][0]/255f, a01=matrix[0][1]/255f, a02=matrix[0][2]/255f, a03=matrix[0][3]/255f, a04=matrix[0][4]/255f; 153 final float a10=matrix[1][0]/255f, a11=matrix[1][1]/255f, a12=matrix[1][2]/255f, a13=matrix[1][3]/255f, a14=matrix[1][4]/255f; 154 final float a20=matrix[2][0]/255f, a21=matrix[2][1]/255f, a22=matrix[2][2]/255f, a23=matrix[2][3]/255f, a24=matrix[2][4]/255f; 155 final float a30=matrix[3][0]/255f, a31=matrix[3][1]/255f, a32=matrix[3][2]/255f, a33=matrix[3][3]/255f, a34=matrix[3][4]/255f; 156 157 for(i=0; i<h; i++){ 158 for(j=0; j<w; j++){ 159 pel = pixels[p]; 160 161 a = pel >>> 24; 162 r = (pel >> 16) & 0xff; 163 g = (pel >> 8 ) & 0xff; 164 b = pel & 0xff; 165 166 dr = (int)((a00*r + a01*g + a02*b + a03*a + a04)*255); 167 dg = (int)((a10*r + a11*g + a12*b + a13*a + a14)*255); 168 db = (int)((a20*r + a21*g + a22*b + a23*a + a24)*255); 169 da = (int)((a30*r + a31*g + a32*b + a33*a + a34)*255); 170 171 175 176 177 if ((dr & 0xFFFFFF00) != 0) 181 dr = ((dr & 0x80000000) != 0)?0:255; 182 if ((dg & 0xFFFFFF00) != 0) 183 dg = ((dg & 0x80000000) != 0)?0:255; 184 if ((db & 0xFFFFFF00) != 0) 185 db = ((db & 0x80000000) != 0)?0:255; 186 if ((da & 0xFFFFFF00) != 0) 187 da = ((da & 0x80000000) != 0)?0:255; 188 189 pixels[p++] = (da << 24 190 | 191 dr << 16 192 | 193 dg << 8 194 | 195 db); 196 197 } 198 p += adjust; 199 } 200 201 return wr; 203 } 204 205 } 206 | Popular Tags |