X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.maps%2Fsrc%2Forg%2Fsimantics%2Fmaps%2Finternal%2FImageUtil.java;fp=org.simantics.district.maps%2Fsrc%2Forg%2Fsimantics%2Fmaps%2Finternal%2FImageUtil.java;h=162097b3b181f6777c766c1f7d0b3d20a49ef864;hb=d3de128e6d55f9f6a1325985695b925a1056c469;hp=0000000000000000000000000000000000000000;hpb=29914be09d4a237840e5c793bdb562ec83093b8d;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.maps/src/org/simantics/maps/internal/ImageUtil.java b/org.simantics.district.maps/src/org/simantics/maps/internal/ImageUtil.java new file mode 100644 index 00000000..162097b3 --- /dev/null +++ b/org.simantics.district.maps/src/org/simantics/maps/internal/ImageUtil.java @@ -0,0 +1,58 @@ +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(); + } + } + +}