Path: blob/master/src/java.desktop/share/classes/sun/java2d/SunCompositeContext.java
41152 views
/*1* Copyright (c) 1997, 2002, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d;2627import java.awt.Composite;28import java.awt.CompositeContext;29import java.awt.AlphaComposite;30import java.awt.image.ColorModel;31import java.awt.image.BufferedImage;32import java.awt.image.Raster;33import java.awt.image.WritableRaster;34import sun.awt.image.BufImgSurfaceData;35import sun.java2d.loops.XORComposite;36import sun.java2d.loops.CompositeType;37import sun.java2d.loops.Blit;3839public class SunCompositeContext implements CompositeContext {40ColorModel srcCM;41ColorModel dstCM;42Composite composite;43CompositeType comptype;4445public SunCompositeContext(AlphaComposite ac,46ColorModel s, ColorModel d)47{48if (s == null) {49throw new NullPointerException("Source color model cannot be null");50}51if (d == null) {52throw new NullPointerException("Destination color model cannot be null");53}54srcCM = s;55dstCM = d;56this.composite = ac;57this.comptype = CompositeType.forAlphaComposite(ac);58}5960public SunCompositeContext(XORComposite xc,61ColorModel s, ColorModel d)62{63if (s == null) {64throw new NullPointerException("Source color model cannot be null");65}66if (d == null) {67throw new NullPointerException("Destination color model cannot be null");68}69srcCM = s;70dstCM = d;71this.composite = xc;72this.comptype = CompositeType.Xor;73}7475/**76* Release resources allocated for context.77*/78public void dispose() {79}8081/**82* This method composes the two source tiles83* and places the result in the destination tile. Note that84* the destination can be the same object as either85* the first or second source.86* @param src1 The first source tile for the compositing operation.87* @param src2 The second source tile for the compositing operation.88* @param dst The tile where the result of the operation is stored.89*/90public void compose(Raster src1, Raster src2, WritableRaster dst) {91WritableRaster src;92int w;93int h;9495if (src2 != dst) {96dst.setDataElements(0, 0, src2);97}9899// REMIND: We should be able to create a SurfaceData from just100// a non-writable Raster and a ColorModel. Since we need to101// create a SurfaceData from a BufferedImage then we need to102// make a WritableRaster since it is needed to construct a103// BufferedImage.104if (src1 instanceof WritableRaster) {105src = (WritableRaster) src1;106} else {107src = src1.createCompatibleWritableRaster();108src.setDataElements(0, 0, src1);109}110111w = Math.min(src.getWidth(), src2.getWidth());112h = Math.min(src.getHeight(), src2.getHeight());113114BufferedImage srcImg = new BufferedImage(srcCM, src,115srcCM.isAlphaPremultiplied(),116null);117BufferedImage dstImg = new BufferedImage(dstCM, dst,118dstCM.isAlphaPremultiplied(),119null);120121SurfaceData srcData = BufImgSurfaceData.createData(srcImg);122SurfaceData dstData = BufImgSurfaceData.createData(dstImg);123Blit blit = Blit.getFromCache(srcData.getSurfaceType(),124comptype,125dstData.getSurfaceType());126blit.Blit(srcData, dstData, composite, null, 0, 0, 0, 0, w, h);127}128}129130131