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