001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.ext.awt.image;
020:
021: /**
022: * This is a typesafe enumeration of the standard Composite rules for
023: * the CompositeRable operation. (over, in, out, atop, xor, arith)
024: *
025: * @author <a href="mailto:Thomas.DeWeeese@Kodak.com">Thomas DeWeese</a>
026: * @version $Id: CompositeRule.java 478249 2006-11-22 17:29:37Z dvholten $
027: */
028: public final class CompositeRule implements java.io.Serializable {
029:
030: /** Porter-Duff src over rule, also used for feBlend <tt>normal</tt>. */
031: public static final int RULE_OVER = 1;
032:
033: /** Porter-Duff src in rule. */
034: public static final int RULE_IN = 2;
035:
036: /** Porter-Duff src out rule. */
037: public static final int RULE_OUT = 3;
038:
039: /** Porter-Duff src atop rule. */
040: public static final int RULE_ATOP = 4;
041:
042: /** Porter-Duff src xor rule. */
043: public static final int RULE_XOR = 5;
044:
045: /** Arithmatic rule 'out = k1*i1*i2 + k2*i1 + k3*i2 + k4'. */
046: public static final int RULE_ARITHMETIC = 6;
047:
048: /** SVG feBlend Multiply rule */
049: public static final int RULE_MULTIPLY = 7;
050:
051: /** SVG feBlend Screen rule */
052: public static final int RULE_SCREEN = 8;
053:
054: /** SVG feBlend Darken rule */
055: public static final int RULE_DARKEN = 9;
056:
057: /** SVG feBlend Lighten rule */
058: public static final int RULE_LIGHTEN = 10;
059:
060: /**
061: * Porter-Duff Source Over Destination rule. The source is
062: * composited over the destination.<pre>
063: *
064: * Fs = 1 and Fd = (1-As), thus:
065: *
066: * Cd = Cs + Cd*(1-As)
067: * Ad = As + Ad*(1-As)</pre>
068: * </pre>
069: */
070: public static final CompositeRule OVER = new CompositeRule(
071: RULE_OVER);
072:
073: /**
074: * Porter-Duff Source In Destination rule. The part of the
075: * source lying inside of the destination replaces the destination.<pre>
076: *
077: * Fs = Ad and Fd = 0, thus:
078: *
079: * Cd = Cs*Ad
080: * Ad = As*Ad
081: * </pre>
082: */
083: public static final CompositeRule IN = new CompositeRule(RULE_IN);
084:
085: /**
086: * Porter-Duff Source Out Destination rule. The part of the
087: * source lying outside of the destination replaces the destination.<pre>
088: *
089: * Fs = (1-Ad) and Fd = 0, thus:
090: *
091: * Cd = Cs*(1-Ad)
092: * Ad = As*(1-Ad)
093: * </pre>
094: */
095: public static final CompositeRule OUT = new CompositeRule(RULE_OUT);
096:
097: /**
098: * Porter-Duff Source Atop Destination rule. The part of the
099: * source lying inside of the destination replaces the destination,
100: * destination remains outside of source.<pre>
101: *
102: * Fs = Ad and Fd = (1-As), thus:
103: *
104: * Cd = Cs*Ad + Cd*(1-As)
105: * Ad = As*Ad + Ad*(1-As)
106: * </pre>
107: */
108: public static final CompositeRule ATOP = new CompositeRule(
109: RULE_ATOP);
110:
111: /**
112: * Xor rule. The source and destination are Xor'ed togeather.<pre>
113: *
114: * Fs = (1-Ad) and Fd = (1-As), thus:
115: *
116: * Cd = Cs*(1-Ad) + Cd*(1-As)
117: * Ad = As*(1-Ad) + Ad*(1-As)
118: * </pre>
119: */
120: public static final CompositeRule XOR = new CompositeRule(RULE_XOR);
121:
122: /**
123: * Factory to create artithmatic CompositeRules.
124: * 'out = k1*i1*i2 + k2*i1 + k3*i2 + k4'
125: * Note that arithmatic CompositeRules are not singletons.
126: */
127: public static CompositeRule ARITHMETIC(float k1, float k2,
128: float k3, float k4) {
129: return new CompositeRule(k1, k2, k3, k4);
130: }
131:
132: /**
133: * FeBlend Multiply rule. <pre>
134: *
135: * Cd = Cs*(1-Ad) + Cd*(1-As) + Cs*Cd
136: * Ad = 1 - (1-Ad)*(1-As)
137: * </pre>
138: */
139: public static final CompositeRule MULTIPLY = new CompositeRule(
140: RULE_MULTIPLY);
141:
142: /**
143: * FeBlend Screen rule. <pre>
144: *
145: * Cd = Cs + Cd - Cs*Cd
146: * Ad = 1 - (1-Ad)*(1-As)
147: * </pre>
148: */
149: public static final CompositeRule SCREEN = new CompositeRule(
150: RULE_SCREEN);
151:
152: /**
153: * FeBlend Darken rule. <pre>
154: *
155: * Cd = Min(Cs*(1-Ad) + Cd,
156: * Cd*(1-As) + Cs)
157: * Ad = 1 - (1-Ad)*(1-As)
158: * </pre>
159: */
160: public static final CompositeRule DARKEN = new CompositeRule(
161: RULE_DARKEN);
162:
163: /**
164: * FeBlend Lighten rule. <pre>
165: *
166: * Cd = Max(Cs*(1-Ad) + Cd,
167: * Cd*(1-As) + Cs)
168: * Ad = 1 - (1-Ad)*(1-As)
169: * </pre>
170: */
171: public static final CompositeRule LIGHTEN = new CompositeRule(
172: RULE_LIGHTEN);
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 readResolve() throws java.io.ObjectStreamException {
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("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 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:" + k1 + " k2: "
263: + 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("Unknown Composite Rule type");
274: }
275: }
276:
277: }
|