jdk/src/java.desktop/share/classes/sun/awt/image/BufferedImageGraphicsConfig.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 5506 jdk/src/share/classes/sun/awt/image/BufferedImageGraphicsConfig.java@202f599c92aa
child 33254 2ce30cca1503
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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.awt.image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.AWTException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Graphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.GraphicsConfiguration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.GraphicsDevice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.ImageCapabilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.Transparency;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.image.DirectColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.image.Raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.image.VolatileImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class BufferedImageGraphicsConfig
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    extends GraphicsConfiguration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final int numconfigs = BufferedImage.TYPE_BYTE_BINARY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static BufferedImageGraphicsConfig configs[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        new BufferedImageGraphicsConfig[numconfigs];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static BufferedImageGraphicsConfig getConfig(BufferedImage bImg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        BufferedImageGraphicsConfig ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        int type = bImg.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (type > 0 && type < numconfigs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            ret = configs[type];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            if (ret != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        ret = new BufferedImageGraphicsConfig(bImg, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (type > 0 && type < numconfigs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            configs[type] = ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    GraphicsDevice gd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    ColorModel model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    Raster raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    int width, height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public BufferedImageGraphicsConfig(BufferedImage bufImg, Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (comp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            this.gd = new BufferedImageDevice(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            Graphics2D g2d = (Graphics2D)comp.getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            this.gd = g2d.getDeviceConfiguration().getDevice();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        this.model = bufImg.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        this.raster = bufImg.getRaster().createCompatibleWritableRaster(1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        this.width = bufImg.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        this.height = bufImg.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Return the graphics device associated with this configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public GraphicsDevice getDevice() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return gd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Returns a BufferedImage with channel layout and color model
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * compatible with this graphics configuration.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * has nothing to do with memory-mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * a device.  This BufferedImage has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * a layout and color model
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * that is closest to this native device configuration and thus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * can be optimally blitted to this device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public BufferedImage createCompatibleImage(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        WritableRaster wr = raster.createCompatibleWritableRaster(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return new BufferedImage(model, wr, model.isAlphaPremultiplied(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Returns the color model associated with this configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public ColorModel getColorModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Returns the color model associated with this configuration that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * supports the specified transparency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public ColorModel getColorModel(int transparency) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (model.getTransparency() == transparency) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        switch (transparency) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        case Transparency.OPAQUE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        case Transparency.BITMASK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        case Transparency.TRANSLUCENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            return ColorModel.getRGBdefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Returns the default Transform for this configuration.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Transform is typically the Identity transform for most normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * screens.  Device coordinates for screen and printer devices will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * have the origin in the upper left-hand corner of the target region of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * the device, with X coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * increasing to the right and Y coordinates increasing downwards.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * For image buffers, this Transform will be the Identity transform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public AffineTransform getDefaultTransform() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return new AffineTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Returns a Transform that can be composed with the default Transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * of a Graphics2D so that 72 units in user space will equal 1 inch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * in device space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Given a Graphics2D, g, one can reset the transformation to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * such a mapping by using the following pseudocode:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *      GraphicsConfiguration gc = g.getGraphicsConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *      g.setTransform(gc.getDefaultTransform());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *      g.transform(gc.getNormalizingTransform());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Note that sometimes this Transform will be identity (e.g. for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * printers or metafile output) and that this Transform is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * as accurate as the information supplied by the underlying system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * For image buffers, this Transform will be the Identity transform,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * since there is no valid distance measurement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public AffineTransform getNormalizingTransform() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return new AffineTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public Rectangle getBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return new Rectangle(0, 0, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
}