KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > CompositeRule


1 /*
2
3    Copyright 2001 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;
19
20 /**
21  * This is a typesafe enumeration of the standard Composite rules for
22  * the CompositeRable operation. (over, in, out, atop, xor, arith)
23  *
24  * @author <a HREF="mailto:Thomas.DeWeeese@Kodak.com">Thomas DeWeese</a>
25  * @version $Id: CompositeRule.java,v 1.5 2004/08/18 07:13:48 vhardy Exp $
26  */

27 public final class CompositeRule implements java.io.Serializable JavaDoc {
28
29     /** Porter-Duff src over rule, also used for feBlend <tt>normal</tt>. */
30     public static final int RULE_OVER = 1;
31
32     /** Porter-Duff src in rule. */
33     public static final int RULE_IN = 2;
34
35     /** Porter-Duff src out rule. */
36     public static final int RULE_OUT = 3;
37
38     /** Porter-Duff src atop rule. */
39     public static final int RULE_ATOP = 4;
40
41     /** Porter-Duff src xor rule. */
42     public static final int RULE_XOR = 5;
43
44     /** Arithmatic rule 'out = k1*i1*i2 + k2*i1 + k3*i2 + k4'. */
45     public static final int RULE_ARITHMETIC = 6;
46
47     /** SVG feBlend Multiply rule */
48     public static final int RULE_MULTIPLY = 7;
49         
50     /** SVG feBlend Screen rule */
51     public static final int RULE_SCREEN = 8;
52         
53     /** SVG feBlend Darken rule */
54     public static final int RULE_DARKEN = 9;
55         
56     /** SVG feBlend Lighten rule */
57     public static final int RULE_LIGHTEN = 10;
58         
59
60       /**
61        * Porter-Duff Source Over Destination rule. The source is
62        * composited over the destination.<pre>
63        *
64        * Fs = 1 and Fd = (1-As), thus:
65        *
66        * Cd = Cs + Cd*(1-As)
67        * Ad = As + Ad*(1-As)</pre>
68        * </pre>
69        */

70     public static final CompositeRule OVER = new CompositeRule(RULE_OVER);
71
72       /**
73        * Porter-Duff Source In Destination rule. The part of the
74        * source lying inside of the destination replaces the destination.<pre>
75        *
76        * Fs = Ad and Fd = 0, thus:
77        *
78        * Cd = Cs*Ad
79        * Ad = As*Ad
80        * </pre>
81        */

82     public static final CompositeRule IN = new CompositeRule(RULE_IN);
83
84       /**
85        * Porter-Duff Source Out Destination rule. The part of the
86        * source lying outside of the destination replaces the destination.<pre>
87        *
88        * Fs = (1-Ad) and Fd = 0, thus:
89        *
90        * Cd = Cs*(1-Ad)
91        * Ad = As*(1-Ad)
92        * </pre>
93        */

94     public static final CompositeRule OUT = new CompositeRule(RULE_OUT);
95
96       /**
97        * Porter-Duff Source Atop Destination rule. The part of the
98        * source lying inside of the destination replaces the destination,
99        * destination remains outside of source.<pre>
100        *
101        * Fs = Ad and Fd = (1-As), thus:
102        *
103        * Cd = Cs*Ad + Cd*(1-As)
104        * Ad = As*Ad + Ad*(1-As)
105        * </pre>
106        */

107     public static final CompositeRule ATOP = new CompositeRule(RULE_ATOP);
108
109       /**
110        * Xor rule. The source and destination are Xor'ed togeather.<pre>
111        *
112        * Fs = (1-Ad) and Fd = (1-As), thus:
113        *
114        * Cd = Cs*(1-Ad) + Cd*(1-As)
115        * Ad = As*(1-Ad) + Ad*(1-As)
116        * </pre>
117        */

118     public static final CompositeRule XOR = new CompositeRule(RULE_XOR);
119
120       /**
121        * Factory to create artithmatic CompositeRules.
122        * 'out = k1*i1*i2 + k2*i1 + k3*i2 + k4'
123        * Note that arithmatic CompositeRules are not singletons.
124        */

125     public static CompositeRule ARITHMETIC
126         (float k1, float k2, float k3, float k4) {
127         return new CompositeRule(k1, k2, k3, k4);
128     }
129
130       /**
131        * FeBlend Multiply rule. <pre>
132        *
133        * Cd = Cs*(1-Ad) + Cd*(1-As) + Cs*Cd
134        * Ad = 1 - (1-Ad)*(1-As)
135        * </pre>
136        */

137     public static final CompositeRule MULTIPLY =
138         new CompositeRule(RULE_MULTIPLY);
139
140       /**
141        * FeBlend Screen rule. <pre>
142        *
143        * Cd = Cs + Cd - Cs*Cd
144        * Ad = 1 - (1-Ad)*(1-As)
145        * </pre>
146        */

147     public static final CompositeRule SCREEN =
148         new CompositeRule(RULE_SCREEN);
149
150       /**
151        * FeBlend Darken rule. <pre>
152        *
153        * Cd = Min(Cs*(1-Ad) + Cd,
154        * Cd*(1-As) + Cs)
155        * Ad = 1 - (1-Ad)*(1-As)
156        * </pre>
157        */

158     public static final CompositeRule DARKEN =
159         new CompositeRule(RULE_DARKEN);
160
161
162       /**
163        * FeBlend Lighten rule. <pre>
164        *
165        * Cd = Max(Cs*(1-Ad) + Cd,
166        * Cd*(1-As) + Cs)
167        * Ad = 1 - (1-Ad)*(1-As)
168        * </pre>
169        */

170     public static final CompositeRule LIGHTEN =
171         new CompositeRule(RULE_LIGHTEN);
172
173
174     /**
175      * Returns the type of this composite rule
176      */

177     public int getRule() {
178         return rule;
179     }
180
181       /**
182        * The composite rule for this object.
183        */

184     private int rule;
185
186       /* Arithmatic constants, only used for RULE_ARITHMETIC */
187     private float k1, k2, k3, k4;
188
189     private CompositeRule(int rule) {
190         this.rule = rule;
191     }
192
193     private CompositeRule(float k1, float k2, float k3, float k4) {
194         rule = RULE_ARITHMETIC;
195         this.k1 = k1;
196         this.k2 = k2;
197         this.k3 = k3;
198         this.k4 = k4;
199     }
200
201     public float [] getCoefficients() {
202         if (rule != RULE_ARITHMETIC)
203             return null;
204
205         return new float[] {k1, k2, k3, k4};
206     }
207
208     /**
209      * This is called by the serialization code before it returns
210      * an unserialized object. To provide for unicity of
211      * instances, the instance that was read is replaced by its
212      * static equivalent. See the serialiazation specification for
213      * further details on this method's logic.
214      */

215     private Object JavaDoc readResolve() throws java.io.ObjectStreamException JavaDoc {
216         switch(rule){
217         case RULE_OVER:
218             return OVER;
219         case RULE_IN:
220             return IN;
221         case RULE_OUT:
222             return OUT;
223         case RULE_ATOP:
224             return ATOP;
225         case RULE_XOR:
226             return XOR;
227         case RULE_ARITHMETIC:
228             return this;
229         case RULE_MULTIPLY:
230             return MULTIPLY;
231         case RULE_SCREEN:
232             return SCREEN;
233         case RULE_DARKEN:
234             return DARKEN;
235         case RULE_LIGHTEN:
236             return LIGHTEN;
237         default:
238             throw new Error JavaDoc("Unknown Composite Rule type");
239         }
240     }
241
242     /**
243      * This is called by the serialization code before it returns
244      * an unserialized object. To provide for unicity of
245      * instances, the instance that was read is replaced by its
246      * static equivalent. See the serialiazation specification for
247      * further details on this method's logic.
248      */

249     public String JavaDoc toString() {
250         switch(rule){
251         case RULE_OVER:
252             return "[CompositeRule: OVER]";
253         case RULE_IN:
254             return "[CompositeRule: IN]";
255         case RULE_OUT:
256             return "[CompositeRule: OUT]";
257         case RULE_ATOP:
258             return "[CompositeRule: ATOP]";
259         case RULE_XOR:
260             return "[CompositeRule: XOR]";
261         case RULE_ARITHMETIC:
262             return ("[CompositeRule: ARITHMATIC k1:" +
263                     k1 + " k2: " + k2 + " k3: " + k3 + " k4: " + k4 + "]");
264         case RULE_MULTIPLY:
265             return "[CompositeRule: MULTIPLY]";
266         case RULE_SCREEN:
267             return "[CompositeRule: SCREEN]";
268         case RULE_DARKEN:
269             return "[CompositeRule: DARKEN]";
270         case RULE_LIGHTEN:
271             return "[CompositeRule: LIGHTEN]";
272         default:
273             throw new Error JavaDoc("Unknown Composite Rule type");
274         }
275     }
276
277 }
278
Popular Tags