001: /*
002: * Copyright 1997-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;
027:
028: import java.awt.Composite;
029: import java.awt.CompositeContext;
030: import java.awt.AlphaComposite;
031: import java.awt.image.ColorModel;
032: import java.awt.image.BufferedImage;
033: import java.awt.image.Raster;
034: import java.awt.image.WritableRaster;
035: import sun.awt.image.BufImgSurfaceData;
036: import sun.java2d.loops.XORComposite;
037: import sun.java2d.loops.CompositeType;
038: import sun.java2d.loops.Blit;
039:
040: public class SunCompositeContext implements CompositeContext {
041: ColorModel srcCM;
042: ColorModel dstCM;
043: Composite composite;
044: CompositeType comptype;
045:
046: public SunCompositeContext(AlphaComposite ac, ColorModel s,
047: ColorModel d) {
048: if (s == null) {
049: throw new NullPointerException(
050: "Source color model cannot be null");
051: }
052: if (d == null) {
053: throw new NullPointerException(
054: "Destination color model cannot be null");
055: }
056: srcCM = s;
057: dstCM = d;
058: this .composite = ac;
059: this .comptype = CompositeType.forAlphaComposite(ac);
060: }
061:
062: public SunCompositeContext(XORComposite xc, ColorModel s,
063: ColorModel d) {
064: if (s == null) {
065: throw new NullPointerException(
066: "Source color model cannot be null");
067: }
068: if (d == null) {
069: throw new NullPointerException(
070: "Destination color model cannot be null");
071: }
072: srcCM = s;
073: dstCM = d;
074: this .composite = xc;
075: this .comptype = CompositeType.Xor;
076: }
077:
078: /**
079: * Release resources allocated for context.
080: */
081: public void dispose() {
082: }
083:
084: /**
085: * This method composes the two source tiles
086: * and places the result in the destination tile. Note that
087: * the destination can be the same object as either
088: * the first or second source.
089: * @param src1 The first source tile for the compositing operation.
090: * @param src2 The second source tile for the compositing operation.
091: * @param dst The tile where the result of the operation is stored.
092: */
093: public void compose(Raster srcArg, Raster dstIn,
094: WritableRaster dstOut) {
095: WritableRaster src;
096: int w;
097: int h;
098:
099: if (dstIn != dstOut) {
100: dstOut.setDataElements(0, 0, dstIn);
101: }
102:
103: // REMIND: We should be able to create a SurfaceData from just
104: // a non-writable Raster and a ColorModel. Since we need to
105: // create a SurfaceData from a BufferedImage then we need to
106: // make a WritableRaster since it is needed to construct a
107: // BufferedImage.
108: if (srcArg instanceof WritableRaster) {
109: src = (WritableRaster) srcArg;
110: } else {
111: src = srcArg.createCompatibleWritableRaster();
112: src.setDataElements(0, 0, srcArg);
113: }
114:
115: w = Math.min(src.getWidth(), dstIn.getWidth());
116: h = Math.min(src.getHeight(), dstIn.getHeight());
117:
118: BufferedImage srcImg = new BufferedImage(srcCM, src, srcCM
119: .isAlphaPremultiplied(), null);
120: BufferedImage dstImg = new BufferedImage(dstCM, dstOut, dstCM
121: .isAlphaPremultiplied(), null);
122:
123: SurfaceData srcData = BufImgSurfaceData.createData(srcImg);
124: SurfaceData dstData = BufImgSurfaceData.createData(dstImg);
125: Blit blit = Blit.getFromCache(srcData.getSurfaceType(),
126: comptype, dstData.getSurfaceType());
127: blit.Blit(srcData, dstData, composite, null, 0, 0, 0, 0, w, h);
128: }
129: }
|