KickJava   Java API By Example, From Geeks To Geeks.

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


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.ComponentTransferFunction;
24 import org.apache.batik.ext.awt.image.DiscreteTransfer;
25 import org.apache.batik.ext.awt.image.GammaTransfer;
26 import org.apache.batik.ext.awt.image.IdentityTransfer;
27 import org.apache.batik.ext.awt.image.LinearTransfer;
28 import org.apache.batik.ext.awt.image.TableTransfer;
29 import org.apache.batik.ext.awt.image.TransferFunction;
30 import org.apache.batik.ext.awt.image.rendered.ComponentTransferRed;
31
32 /**
33  * This class implements the interface expected from a component
34  * transfer operation.
35  *
36  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
37  * @version $Id: ComponentTransferRable8Bit.java,v 1.8 2004/08/18 07:13:58 vhardy Exp $
38  */

39 public class ComponentTransferRable8Bit
40     extends AbstractColorInterpolationRable
41     implements ComponentTransferRable {
42
43     public static final int ALPHA = 0;
44     public static final int RED = 1;
45     public static final int GREEN = 2;
46     public static final int BLUE = 3;
47
48     /**
49      * Array of transfer functions. There are four
50      * elements. Elements may be null.
51      */

52     private ComponentTransferFunction
53         functions[] = new ComponentTransferFunction[4];
54
55     /**
56      * Array of transfer functions. Elements are computed
57      * lazily.
58      */

59     private TransferFunction
60         txfFunc[] = new TransferFunction[4];
61
62     public ComponentTransferRable8Bit(Filter src,
63                                       ComponentTransferFunction alphaFunction,
64                                       ComponentTransferFunction redFunction,
65                                       ComponentTransferFunction greenFunction,
66                                       ComponentTransferFunction blueFunction){
67         super(src, null);
68         setAlphaFunction(alphaFunction);
69         setRedFunction(redFunction);
70         setGreenFunction(greenFunction);
71         setBlueFunction(blueFunction);
72     }
73
74     /**
75      * Sets the source of the blur operation
76      */

77     public void setSource(Filter src){
78         init(src, null);
79     }
80
81     /**
82      * Returns the source of the blur operation
83      */

84     public Filter getSource(){
85         return (Filter)getSources().get(0);
86     }
87
88     /**
89      * Returns the transfer function for the alpha channel
90      */

91     public ComponentTransferFunction getAlphaFunction(){
92         return functions[ALPHA];
93     }
94
95     /**
96      * Sets the transfer function for the alpha channel
97      */

98     public void setAlphaFunction(ComponentTransferFunction alphaFunction){
99         touch();
100         functions[ALPHA] = alphaFunction;
101         txfFunc[ALPHA] = null;
102     }
103
104     /**
105      * Returns the transfer function for the red channel
106      */

107     public ComponentTransferFunction getRedFunction(){
108         return functions[RED];
109     }
110
111     /**
112      * Sets the transfer function for the red channel
113      */

114     public void setRedFunction(ComponentTransferFunction redFunction){
115         touch();
116         functions[RED] = redFunction;
117         txfFunc[RED] = null;
118     }
119
120     /**
121      * Returns the transfer function for the green channel
122      */

123     public ComponentTransferFunction getGreenFunction(){
124         return functions[GREEN];
125     }
126
127     /**
128      * Sets the transfer function for the green channel
129      */

130     public void setGreenFunction(ComponentTransferFunction greenFunction){
131         touch();
132         functions[GREEN] = greenFunction;
133         txfFunc[GREEN] = null;
134     }
135
136     /**
137      * Returns the transfer function for the blue channel
138      */

139     public ComponentTransferFunction getBlueFunction(){
140         return functions[BLUE];
141     }
142
143     /**
144      * Sets the transfer function for the blue channel
145      */

146     public void setBlueFunction(ComponentTransferFunction blueFunction){
147         touch();
148         functions[BLUE] = blueFunction;
149         txfFunc[BLUE] = null;
150     }
151
152     public RenderedImage JavaDoc createRendering(RenderContext JavaDoc rc){
153         //
154
// Get source's rendered image
155
//
156
RenderedImage JavaDoc srcRI = getSource().createRendering(rc);
157
158         if(srcRI == null)
159             return null;
160
161         return new ComponentTransferRed(convertSourceCS(srcRI),
162                                         getTransferFunctions(),
163                                         rc.getRenderingHints());
164     }
165
166     /**
167      * Builds an array of transfer functions for the
168      * ComponentTransferOp.
169      */

170     private TransferFunction[] getTransferFunctions(){
171         //
172
// Copy array to avoid multi-thread conflicts on
173
// array access.
174
//
175
TransferFunction txfFunc[] = new TransferFunction[4];
176         System.arraycopy(this.txfFunc, 0, txfFunc, 0, 4);
177
178         ComponentTransferFunction functions[];
179         functions = new ComponentTransferFunction[4];
180         System.arraycopy(this.functions, 0, functions, 0, 4);
181
182         for(int i=0; i<4; i++){
183             if(txfFunc[i] == null){
184                 txfFunc[i] = getTransferFunction(functions[i]);
185                 synchronized(this.functions){
186                     if(this.functions[i] == functions[i]){
187                         this.txfFunc[i] = txfFunc[i];
188                     }
189                 }
190             }
191         }
192
193         return txfFunc;
194     }
195
196     /**
197      * Converts a ComponentTransferFunction to a TransferFunction
198      */

199     private static TransferFunction getTransferFunction
200         (ComponentTransferFunction function){
201
202         TransferFunction txfFunc = null;
203         if(function == null){
204             txfFunc = new IdentityTransfer();
205         }
206         else{
207             switch(function.getType()){
208             case ComponentTransferFunction.IDENTITY:
209                 txfFunc = new IdentityTransfer();
210                 break;
211             case ComponentTransferFunction.TABLE:
212                 txfFunc = new TableTransfer(tableFloatToInt(function.getTableValues()));
213                 break;
214             case ComponentTransferFunction.DISCRETE:
215                 txfFunc = new DiscreteTransfer(tableFloatToInt(function.getTableValues()));
216                 break;
217             case ComponentTransferFunction.LINEAR:
218                 txfFunc = new LinearTransfer(function.getSlope(),
219                                              function.getIntercept());
220                 break;
221             case ComponentTransferFunction.GAMMA:
222                 txfFunc = new GammaTransfer(function.getAmplitude(),
223                                             function.getExponent(),
224                                             function.getOffset());
225                 break;
226             default:
227                 // Should never happen
228
throw new Error JavaDoc();
229             }
230         }
231
232         return txfFunc;
233     }
234
235     /**
236      * Converts a intensity values (0-1) to code values (0-255)
237      */

238     private static int[] tableFloatToInt(float tableValues[]){
239         int values[] = new int[tableValues.length];
240         for(int i=0; i<tableValues.length; i++){
241             values[i] = (int)(tableValues[i]*255f);
242         }
243
244         return values;
245     }
246
247 }
248
Popular Tags