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