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