1 /*
   2  * Copyright (c) 1997, 2002, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.java2d;
  27 
  28 import java.awt.Composite;
  29 import java.awt.CompositeContext;
  30 import java.awt.AlphaComposite;
  31 import java.awt.image.ColorModel;
  32 import java.awt.image.BufferedImage;
  33 import java.awt.image.Raster;
  34 import java.awt.image.WritableRaster;
  35 import sun.awt.image.BufImgSurfaceData;
  36 import sun.java2d.loops.XORComposite;
  37 import sun.java2d.loops.CompositeType;
  38 import sun.java2d.loops.Blit;
  39 
  40 public class SunCompositeContext implements CompositeContext {
  41     ColorModel srcCM;
  42     ColorModel dstCM;
  43     Composite composite;
  44     CompositeType comptype;
  45 
  46     public SunCompositeContext(AlphaComposite ac,
  47                                ColorModel s, ColorModel d)
  48     {
  49         if (s == null) {
  50             throw new NullPointerException("Source color model cannot be null");
  51         }
  52         if (d == null) {
  53             throw new NullPointerException("Destination color model cannot be null");
  54         }
  55         srcCM = s;
  56         dstCM = d;
  57         this.composite = ac;
  58         this.comptype = CompositeType.forAlphaComposite(ac);
  59     }
  60 
  61     public SunCompositeContext(XORComposite xc,
  62                                ColorModel s, ColorModel d)
  63     {
  64         if (s == null) {
  65             throw new NullPointerException("Source color model cannot be null");
  66         }
  67         if (d == null) {
  68             throw new NullPointerException("Destination color model cannot be null");
  69         }
  70         srcCM = s;
  71         dstCM = d;
  72         this.composite = xc;
  73         this.comptype = CompositeType.Xor;
  74     }
  75 
  76     /**
  77      * Release resources allocated for context.
  78      */
  79     public void dispose() {
  80     }
  81 
  82     /**
  83      * This method composes the two source tiles
  84      * and places the result in the destination tile. Note that
  85      * the destination can be the same object as either
  86      * the first or second source.
  87      * @param src1 The first source tile for the compositing operation.
  88      * @param src2 The second source tile for the compositing operation.
  89      * @param dst The tile where the result of the operation is stored.
  90      */
  91     public void compose(Raster src1, Raster src2, WritableRaster dst) {
  92         WritableRaster src;
  93         int w;
  94         int h;
  95 
  96         if (src2 != dst) {
  97             dst.setDataElements(0, 0, src2);
  98         }
  99 
 100         // REMIND: We should be able to create a SurfaceData from just
 101         // a non-writable Raster and a ColorModel.  Since we need to
 102         // create a SurfaceData from a BufferedImage then we need to
 103         // make a WritableRaster since it is needed to construct a
 104         // BufferedImage.
 105         if (src1 instanceof WritableRaster) {
 106             src = (WritableRaster) src1;
 107         } else {
 108             src = src1.createCompatibleWritableRaster();
 109             src.setDataElements(0, 0, src1);
 110         }
 111 
 112         w = Math.min(src.getWidth(), src2.getWidth());
 113         h = Math.min(src.getHeight(), src2.getHeight());
 114 
 115         BufferedImage srcImg = new BufferedImage(srcCM, src,
 116                                                  srcCM.isAlphaPremultiplied(),
 117                                                  null);
 118         BufferedImage dstImg = new BufferedImage(dstCM, dst,
 119                                                  dstCM.isAlphaPremultiplied(),
 120                                                  null);
 121 
 122         SurfaceData srcData = BufImgSurfaceData.createData(srcImg);
 123         SurfaceData dstData = BufImgSurfaceData.createData(dstImg);
 124         Blit blit = Blit.getFromCache(srcData.getSurfaceType(),
 125                                       comptype,
 126                                       dstData.getSurfaceType());
 127         blit.Blit(srcData, dstData, composite, null, 0, 0, 0, 0, w, h);
 128     }
 129 }