1 package org.simantics.maps.internal;
3 import java.awt.Graphics2D;
4 import java.awt.GraphicsConfiguration;
5 import java.awt.GraphicsEnvironment;
6 import java.awt.image.BufferedImage;
9 * @author Tuukka Lehtonen
12 public class ImageUtil {
14 public static BufferedImage createScreenCompatibleImage(int width, int height, int transparency) {
15 return createCompatibleImage(
17 .getLocalGraphicsEnvironment()
18 .getDefaultScreenDevice()
19 .getDefaultConfiguration(),
20 width, height, transparency);
23 public static BufferedImage createCompatibleImage(GraphicsConfiguration conf, int width, int height, int transparency) {
24 return conf.createCompatibleImage(width, height, transparency);
27 public static BufferedImage toScreenCompatibleImage(BufferedImage img) {
28 return toCompatibleImage(
30 .getLocalGraphicsEnvironment()
31 .getDefaultScreenDevice()
32 .getDefaultConfiguration(),
36 public static BufferedImage toCompatibleImage(GraphicsConfiguration conf, BufferedImage img) {
37 // TODO: add checks to prevent new image creation if the provided image is already compatible with the given GraphicsConfiguration
38 if (img.getColorModel().equals(conf.getColorModel()))
41 // BufferedImage result = null;
42 // if (img.getAlphaRaster() != null) {
43 // result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
45 // result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
48 BufferedImage result = createCompatibleImage(conf, img.getWidth(), img.getHeight(), img.getTransparency());
49 Graphics2D g = result.createGraphics();
51 g.drawImage(img, 0, 0, null);