KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > rendered > ColorMatrixRed


1 /*
2
3    Copyright 2001,2003-2004 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.ext.awt.image.rendered;
19
20 import java.awt.color.ColorSpace JavaDoc;
21 import java.awt.image.ColorModel JavaDoc;
22 import java.awt.image.DataBufferInt JavaDoc;
23 import java.awt.image.SampleModel JavaDoc;
24 import java.awt.image.SinglePixelPackedSampleModel JavaDoc;
25 import java.awt.image.WritableRaster JavaDoc;
26
27 import org.apache.batik.ext.awt.image.GraphicsUtil;
28
29 /**
30  *
31  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
32  * @version $Id: ColorMatrixRed.java,v 1.7 2004/08/18 07:14:08 vhardy Exp $
33  */

34 public class ColorMatrixRed extends AbstractRed{
35     /**
36      * Matrix to apply to color components
37      */

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 JavaDoc();
49         }
50
51         if(tmp.length != 4){
52             throw new IllegalArgumentException JavaDoc();
53         }
54
55         for(int i=0; i<4; i++){
56             if(tmp[i].length != 5){
57                 throw new IllegalArgumentException JavaDoc("" + 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 JavaDoc srcCM = src.getColorModel();
83         ColorSpace JavaDoc srcCS = null;
84         if (srcCM != null)
85             srcCS = srcCM.getColorSpace();
86         ColorModel JavaDoc 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 JavaDoc 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 JavaDoc copyData(WritableRaster JavaDoc wr){
106         //System.out.println("Getting data for : " + wr.getWidth() + "/" + wr.getHeight() + "/" + wr.getMinX() + "/" + wr.getMinY());
107

108         //
109
// First, get source data
110
//
111
CachableRed src = (CachableRed)getSources().elementAt(0);
112         // System.out.println("Hello");
113
// System.out.println("src class : " + src.getClass().getName());
114
// System.out.println("this : " + this);
115
wr = src.copyData(wr);
116         // System.out.println("Hi");
117
//System.out.println("Source was : " + wr.getWidth() + "/" + wr.getHeight()+ "/" + wr.getMinX() + "/" + wr.getMinY());
118

119         // Unpremultiply data if required
120
ColorModel JavaDoc cm = src.getColorModel();
121         GraphicsUtil.coerceData(wr, cm, false);
122
123         //
124
// Now, process pixel values
125
//
126
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 JavaDoc dbf = (DataBufferInt JavaDoc)wr.getDataBuffer();
131         final int[] pixels = dbf.getBankData()[0];
132
133         SinglePixelPackedSampleModel JavaDoc sppsm;
134         sppsm = (SinglePixelPackedSampleModel JavaDoc)wr.getSampleModel();
135
136         final int offset =
137             (dbf.getOffset() +
138              sppsm.getOffset(minX-wr.getSampleModelTranslateX(),
139                              minY-wr.getSampleModelTranslateY()));
140
141         // final int offset = dbf.getOffset();
142

143         final int scanStride =
144             ((SinglePixelPackedSampleModel JavaDoc)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                 /*dr = dr > 255 ? 255 : dr < 0 ? 0 : dr;
172                 dg = dg > 255 ? 255 : dg < 0 ? 0 : dg;
173                 db = db > 255 ? 255 : db < 0 ? 0 : db;
174                 da = da > 255 ? 255 : da < 0 ? 0 : da;*/

175
176
177                 // If any high bits are set we are not in range.
178
// If the highest bit is set then we are negative so
179
// clamp to zero else we are > 255 so clamp to 255.
180
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         //System.out.println("Result is : " + wr.getWidth() + "/" + wr.getHeight()+ "/" + wr.getMinX() + "/" + wr.getMinY());
202
return wr;
203     }
204
205 }
206
Popular Tags