package org.simantics.maps.internal; import java.awt.Graphics2D; import java.awt.GraphicsConfiguration; import java.awt.GraphicsEnvironment; import java.awt.image.BufferedImage; /** * @author Tuukka Lehtonen * @since 1.35.0 */ public class ImageUtil { public static BufferedImage createScreenCompatibleImage(int width, int height, int transparency) { return createCompatibleImage( GraphicsEnvironment .getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration(), width, height, transparency); } public static BufferedImage createCompatibleImage(GraphicsConfiguration conf, int width, int height, int transparency) { return conf.createCompatibleImage(width, height, transparency); } public static BufferedImage toScreenCompatibleImage(BufferedImage img) { return toCompatibleImage( GraphicsEnvironment .getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration(), img); } public static BufferedImage toCompatibleImage(GraphicsConfiguration conf, BufferedImage img) { // TODO: add checks to prevent new image creation if the provided image is already compatible with the given GraphicsConfiguration if (img.getColorModel().equals(conf.getColorModel())) return img; // BufferedImage result = null; // if (img.getAlphaRaster() != null) { // result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_4BYTE_ABGR); // } else { // result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_3BYTE_BGR); // } BufferedImage result = createCompatibleImage(conf, img.getWidth(), img.getHeight(), img.getTransparency()); Graphics2D g = result.createGraphics(); try { g.drawImage(img, 0, 0, null); return result; } finally { g.dispose(); } } }