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