1 /*
   2  * Copyright (c) 1997, 2005, 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.awt.image;
  27 
  28 import java.awt.AWTException;
  29 import java.awt.Component;
  30 import java.awt.Graphics2D;
  31 import java.awt.GraphicsConfiguration;
  32 import java.awt.GraphicsDevice;
  33 import java.awt.ImageCapabilities;
  34 import java.awt.Rectangle;
  35 import java.awt.Transparency;
  36 import java.awt.geom.AffineTransform;
  37 import java.awt.image.BufferedImage;
  38 import java.awt.image.ColorModel;
  39 import java.awt.image.DirectColorModel;
  40 import java.awt.image.Raster;
  41 import java.awt.image.VolatileImage;
  42 import java.awt.image.WritableRaster;
  43 
  44 public class BufferedImageGraphicsConfig
  45     extends GraphicsConfiguration
  46 {
  47     private static final int numconfigs = BufferedImage.TYPE_BYTE_BINARY;
  48     private static BufferedImageGraphicsConfig configs[][] =
  49         new BufferedImageGraphicsConfig[numconfigs][2];
  50 
  51     public static BufferedImageGraphicsConfig getConfig(BufferedImage bImg) {
  52         return getConfig(bImg, 1, 1);
  53     }
  54 
  55     public static BufferedImageGraphicsConfig getConfig(BufferedImage bImg,
  56                                                         double scaleX,
  57                                                         double scaleY)
  58     {
  59         BufferedImageGraphicsConfig ret;
  60         int type = bImg.getType();
  61         int index = (scaleX == 1 && scaleY == 1) ? 0 : 1;
  62         if (type > 0 && type < numconfigs) {
  63             ret = configs[type][index];
  64             if (ret != null && ret.scaleX == scaleX && ret.scaleY == scaleY) {
  65                 return ret;
  66             }
  67         }
  68         ret = new BufferedImageGraphicsConfig(bImg, null, scaleX, scaleY);
  69         if (type > 0 && type < numconfigs) {
  70             configs[type][index] = ret;
  71         }
  72         return ret;
  73     }
  74 
  75     GraphicsDevice gd;
  76     ColorModel model;
  77     Raster raster;
  78     int width, height;
  79     private final double scaleX;
  80     private final double scaleY;
  81 
  82     public BufferedImageGraphicsConfig(BufferedImage bufImg, Component comp) {
  83         this(bufImg, comp, 1, 1);
  84     }
  85 
  86     public BufferedImageGraphicsConfig(BufferedImage bufImg, Component comp,
  87                                        double scaleX, double scaleY)
  88     {
  89         if (comp == null) {
  90             this.gd = new BufferedImageDevice(this);
  91         } else {
  92             Graphics2D g2d = (Graphics2D)comp.getGraphics();
  93             this.gd = g2d.getDeviceConfiguration().getDevice();
  94         }
  95         this.model = bufImg.getColorModel();
  96         this.raster = bufImg.getRaster().createCompatibleWritableRaster(1, 1);
  97         this.width = bufImg.getWidth();
  98         this.height = bufImg.getHeight();
  99         this.scaleX = scaleX;
 100         this.scaleY = scaleY;
 101     }
 102 
 103     /**
 104      * Return the graphics device associated with this configuration.
 105      */
 106     public GraphicsDevice getDevice() {
 107         return gd;
 108     }
 109 
 110     /**
 111      * Returns a BufferedImage with channel layout and color model
 112      * compatible with this graphics configuration.  This method
 113      * has nothing to do with memory-mapping
 114      * a device.  This BufferedImage has
 115      * a layout and color model
 116      * that is closest to this native device configuration and thus
 117      * can be optimally blitted to this device.
 118      */
 119     public BufferedImage createCompatibleImage(int width, int height) {
 120         WritableRaster wr = raster.createCompatibleWritableRaster(width, height);
 121         return new BufferedImage(model, wr, model.isAlphaPremultiplied(), null);
 122     }
 123 
 124     /**
 125      * Returns the color model associated with this configuration.
 126      */
 127     public ColorModel getColorModel() {
 128         return model;
 129     }
 130 
 131     /**
 132      * Returns the color model associated with this configuration that
 133      * supports the specified transparency.
 134      */
 135     public ColorModel getColorModel(int transparency) {
 136 
 137         if (model.getTransparency() == transparency) {
 138             return model;
 139         }
 140         switch (transparency) {
 141         case Transparency.OPAQUE:
 142             return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
 143         case Transparency.BITMASK:
 144             return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
 145         case Transparency.TRANSLUCENT:
 146             return ColorModel.getRGBdefault();
 147         default:
 148             return null;
 149         }
 150     }
 151 
 152     /**
 153      * Returns the default Transform for this configuration.  This
 154      * Transform is typically the Identity transform for most normal
 155      * screens.  Device coordinates for screen and printer devices will
 156      * have the origin in the upper left-hand corner of the target region of
 157      * the device, with X coordinates
 158      * increasing to the right and Y coordinates increasing downwards.
 159      * For image buffers, this Transform will be the Identity transform.
 160      */
 161     public AffineTransform getDefaultTransform() {
 162         return AffineTransform.getScaleInstance(scaleX, scaleY);
 163     }
 164 
 165     /**
 166      *
 167      * Returns a Transform that can be composed with the default Transform
 168      * of a Graphics2D so that 72 units in user space will equal 1 inch
 169      * in device space.
 170      * Given a Graphics2D, g, one can reset the transformation to create
 171      * such a mapping by using the following pseudocode:
 172      * <pre>
 173      *      GraphicsConfiguration gc = g.getGraphicsConfiguration();
 174      *
 175      *      g.setTransform(gc.getDefaultTransform());
 176      *      g.transform(gc.getNormalizingTransform());
 177      * </pre>
 178      * Note that sometimes this Transform will be identity (e.g. for
 179      * printers or metafile output) and that this Transform is only
 180      * as accurate as the information supplied by the underlying system.
 181      * For image buffers, this Transform will be the Identity transform,
 182      * since there is no valid distance measurement.
 183      */
 184     public AffineTransform getNormalizingTransform() {
 185         return new AffineTransform();
 186     }
 187 
 188     public Rectangle getBounds() {
 189         return new Rectangle(0, 0, width, height);
 190     }
 191 }