KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > renderable > ColorMatrixRable8Bit


1 /*
2
3    Copyright 2001,2003 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.renderable;
19
20 import java.awt.image.RenderedImage JavaDoc;
21 import java.awt.image.renderable.RenderContext JavaDoc;
22
23 import org.apache.batik.ext.awt.image.rendered.ColorMatrixRed;
24
25 /**
26  * Implements the interface expected from a color matrix
27  * operation
28  *
29  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
30  * @version $Id: ColorMatrixRable8Bit.java,v 1.8 2004/08/18 07:13:58 vhardy Exp $
31  */

32 public class ColorMatrixRable8Bit
33     extends AbstractColorInterpolationRable
34     implements ColorMatrixRable {
35     /**
36      * Predefined luminanceToAlpha matrix
37      */

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     /**
47      * This matrix type
48      */

49     private int type;
50
51     /**
52      * The matrix
53      */

54     private float matrix[][];
55
56     /**
57      * Sets the source of the blur operation
58      */

59     public void setSource(Filter src){
60         init(src, null);
61     }
62
63     /**
64      * Returns the source of the blur operation
65      */

66     public Filter getSource(){
67         return (Filter)getSources().get(0);
68     }
69
70     /**
71      * Returns the type of this color matrix.
72      * @return one of TYPE_MATRIX, TYPE_SATURATE, TYPE_HUE_ROTATE,
73      * TYPE_LUMINANCE_TO_ALPHA
74      */

75     public int getType(){
76         return type;
77     }
78
79     /**
80      * Returns the rows of the color matrix. This uses
81      * the same convention as BandCombineOp.
82      */

83     public float[][] getMatrix(){
84         return matrix;
85     }
86
87     /**
88      * Instances should be built through the static
89      * factory methods
90      */

91     private ColorMatrixRable8Bit(){
92     }
93
94     /**
95      * Builds a TYPE_MATRIX instance
96      */

97     public static ColorMatrixRable buildMatrix(float matrix[][]){
98         if(matrix == null){
99             throw new IllegalArgumentException JavaDoc();
100         }
101
102         if(matrix.length != 4){
103             throw new IllegalArgumentException JavaDoc();
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 JavaDoc();
112             }
113             if(m.length != 5){
114                 throw new IllegalArgumentException JavaDoc();
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         /*for(int i=0; i<4; i++){
123             for(int j=0; j<5; j++)
124                 System.out.print(newMatrix[i][j] + " ");
125             System.out.println();
126             }*/

127
128         ColorMatrixRable8Bit filter
129             = new ColorMatrixRable8Bit();
130         filter.type = TYPE_MATRIX;
131         filter.matrix = newMatrix;
132         return filter;
133     }
134
135     /**
136      * Builds a TYPE_SATURATE instance
137      */

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     /**
152      * Builds a TYPE_HUE_ROTATE instance.
153      * @param a angle, in radian
154      */

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         // System.out.println("sin : " + sin + " cos : " + cos);
164

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         /*for(int i=0; i<4; i++){
184             for(int j=0; j<5; j++)
185                 System.out.print(filter.matrix[i][j] + " ");
186             System.out.println();
187             }*/

188
189         return filter;
190     }
191
192     /**
193      * Builds a TYPE_LUMINANCE_TO_ALPHA instance
194      */

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 JavaDoc createRendering(RenderContext JavaDoc rc) {
204         //
205
// Get source's rendered image
206
//
207
RenderedImage JavaDoc 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