001: /*
002: * Copyright 1999-2002 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.java2d.loops;
027:
028: import java.awt.image.BufferedImage;
029: import java.awt.AlphaComposite;
030:
031: /**
032: * A CompositeType object provides a chained description of a type of
033: * algorithm for color compositing. The object will provide a single
034: * String constant descriptor which is one way of describing a particular
035: * compositing algorithm as well as a pointer to another CompositeType
036: * which describes a more general algorithm for achieving the same result.
037: * <p>
038: * A description of a more specific algorithm is considered a "subtype"
039: * and a description of a more general algorithm is considered a "supertype".
040: * Thus, the deriveSubType method provides a way to create a new CompositeType
041: * that is related to but more specific than an existing CompositeType and
042: * the getSuperType method provides a way to ask a given CompositeType
043: * for a more general algorithm to achieve the same result.
044: * <p>
045: * Note that you cannot construct a brand new root for a chain since
046: * the constructor is private. Every chain of types must at some point
047: * derive from the Any node provided here using the deriveSubType()
048: * method. The presence of this common Any node on every chain
049: * ensures that all chains end with the DESC_ANY descriptor so that
050: * a suitable General GraphicsPrimitive object can be obtained for
051: * the indicated algorithm if all of the more specific searches fail.
052: */
053: public final class CompositeType {
054: /*
055: * CONSTANTS USED BY ALL PRIMITIVES TO DESCRIBE THE COMPOSITING
056: * ALGORITHMS THEY CAN PERFORM
057: */
058:
059: /**
060: * algorithm is a general algorithm that uses a CompositeContext
061: * to do the rendering.
062: */
063: public static final String DESC_ANY = "Any CompositeContext";
064:
065: /**
066: * constant used to describe the Graphics.setXORMode() algorithm
067: */
068: public static final String DESC_XOR = "XOR mode";
069:
070: /**
071: * constants used to describe the various AlphaComposite
072: * algorithms.
073: */
074: public static final String DESC_CLEAR = "Porter-Duff Clear";
075: public static final String DESC_SRC = "Porter-Duff Src";
076: public static final String DESC_DST = "Porter-Duff Dst";
077: public static final String DESC_SRC_OVER = "Porter-Duff Src Over Dst";
078: public static final String DESC_DST_OVER = "Porter-Duff Dst Over Src";
079: public static final String DESC_SRC_IN = "Porter-Duff Src In Dst";
080: public static final String DESC_DST_IN = "Porter-Duff Dst In Src";
081: public static final String DESC_SRC_OUT = "Porter-Duff Src HeldOutBy Dst";
082: public static final String DESC_DST_OUT = "Porter-Duff Dst HeldOutBy Src";
083: public static final String DESC_SRC_ATOP = "Porter-Duff Src Atop Dst";
084: public static final String DESC_DST_ATOP = "Porter-Duff Dst Atop Src";
085: public static final String DESC_ALPHA_XOR = "Porter-Duff Xor";
086:
087: /**
088: * constants used to describe the two common cases of
089: * AlphaComposite algorithms that are simpler if there
090: * is not extraAlpha.
091: */
092: public static final String DESC_SRC_NO_EA = "Porter-Duff Src, No Extra Alpha";
093: public static final String DESC_SRC_OVER_NO_EA = "Porter-Duff SrcOverDst, No Extra Alpha";
094:
095: /**
096: * constant used to describe an algorithm that implements all 8 of
097: * the Porter-Duff rules in one Primitive.
098: */
099: public static final String DESC_ANY_ALPHA = "Any AlphaComposite Rule";
100:
101: /*
102: * END OF COMPOSITE ALGORITHM TYPE CONSTANTS
103: */
104:
105: /**
106: * The root CompositeType object for all chains of algorithm descriptions.
107: */
108: public static final CompositeType Any = new CompositeType(null,
109: DESC_ANY);
110:
111: /*
112: * START OF CompositeeType OBJECTS FOR THE VARIOUS CONSTANTS
113: */
114:
115: public static final CompositeType General = Any;
116:
117: public static final CompositeType AnyAlpha = General
118: .deriveSubType(DESC_ANY_ALPHA);
119: public static final CompositeType Xor = General
120: .deriveSubType(DESC_XOR);
121:
122: public static final CompositeType Clear = AnyAlpha
123: .deriveSubType(DESC_CLEAR);
124: public static final CompositeType Src = AnyAlpha
125: .deriveSubType(DESC_SRC);
126: public static final CompositeType Dst = AnyAlpha
127: .deriveSubType(DESC_DST);
128: public static final CompositeType SrcOver = AnyAlpha
129: .deriveSubType(DESC_SRC_OVER);
130: public static final CompositeType DstOver = AnyAlpha
131: .deriveSubType(DESC_DST_OVER);
132: public static final CompositeType SrcIn = AnyAlpha
133: .deriveSubType(DESC_SRC_IN);
134: public static final CompositeType DstIn = AnyAlpha
135: .deriveSubType(DESC_DST_IN);
136: public static final CompositeType SrcOut = AnyAlpha
137: .deriveSubType(DESC_SRC_OUT);
138: public static final CompositeType DstOut = AnyAlpha
139: .deriveSubType(DESC_DST_OUT);
140: public static final CompositeType SrcAtop = AnyAlpha
141: .deriveSubType(DESC_SRC_ATOP);
142: public static final CompositeType DstAtop = AnyAlpha
143: .deriveSubType(DESC_DST_ATOP);
144: public static final CompositeType AlphaXor = AnyAlpha
145: .deriveSubType(DESC_ALPHA_XOR);
146:
147: public static final CompositeType SrcNoEa = Src
148: .deriveSubType(DESC_SRC_NO_EA);
149: public static final CompositeType SrcOverNoEa = SrcOver
150: .deriveSubType(DESC_SRC_OVER_NO_EA);
151:
152: /*
153: * END OF CompositeType OBJECTS FOR THE VARIOUS CONSTANTS
154: */
155:
156: /**
157: * Return a new CompositeType object which uses this object as its
158: * more general "supertype" descriptor. If no operation can be
159: * found that implements the algorithm described more exactly
160: * by desc, then this object will define the more general
161: * compositing algorithm that can be used instead.
162: */
163: public CompositeType deriveSubType(String desc) {
164: return new CompositeType(this , desc);
165: }
166:
167: /**
168: * Return a CompositeType object for the specified AlphaComposite
169: * rule.
170: */
171: public static CompositeType forAlphaComposite(AlphaComposite ac) {
172: switch (ac.getRule()) {
173: case AlphaComposite.CLEAR:
174: return Clear;
175: case AlphaComposite.SRC:
176: if (ac.getAlpha() >= 1.0f) {
177: return SrcNoEa;
178: } else {
179: return Src;
180: }
181: case AlphaComposite.DST:
182: return Dst;
183: case AlphaComposite.SRC_OVER:
184: if (ac.getAlpha() >= 1.0f) {
185: return SrcOverNoEa;
186: } else {
187: return SrcOver;
188: }
189: case AlphaComposite.DST_OVER:
190: return DstOver;
191: case AlphaComposite.SRC_IN:
192: return SrcIn;
193: case AlphaComposite.DST_IN:
194: return DstIn;
195: case AlphaComposite.SRC_OUT:
196: return SrcOut;
197: case AlphaComposite.DST_OUT:
198: return DstOut;
199: case AlphaComposite.SRC_ATOP:
200: return SrcAtop;
201: case AlphaComposite.DST_ATOP:
202: return DstAtop;
203: case AlphaComposite.XOR:
204: return AlphaXor;
205: default:
206: throw new InternalError("Unrecognized alpha rule");
207: }
208: }
209:
210: private static int unusedUID = 1;
211: private int uniqueID;
212: private String desc;
213: private CompositeType next;
214:
215: private CompositeType(CompositeType parent, String desc) {
216: next = parent;
217: this .desc = desc;
218: this .uniqueID = makeUniqueID();
219: }
220:
221: private synchronized static final int makeUniqueID() {
222: if (unusedUID > 255) {
223: throw new InternalError("composite type id overflow");
224: }
225: return unusedUID++;
226: }
227:
228: public int getUniqueID() {
229: return uniqueID;
230: }
231:
232: public String getDescriptor() {
233: return desc;
234: }
235:
236: public CompositeType getSuperType() {
237: return next;
238: }
239:
240: public int hashCode() {
241: return desc.hashCode();
242: }
243:
244: public boolean isDerivedFrom(CompositeType other) {
245: CompositeType comptype = this ;
246: do {
247: if (comptype.desc == other.desc) {
248: return true;
249: }
250: comptype = comptype.next;
251: } while (comptype != null);
252: return false;
253: }
254:
255: public boolean equals(Object o) {
256: if (o instanceof CompositeType) {
257: return (((CompositeType) o).uniqueID == this .uniqueID);
258: }
259: return false;
260: }
261:
262: public String toString() {
263: return desc;
264: }
265: }
|