]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.maps/src/org/simantics/maps/internal/ImageUtil.java
Ensure ITileProviders return BufferedImages with compatible ColorModel
[simantics/district.git] / org.simantics.district.maps / src / org / simantics / maps / internal / ImageUtil.java
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 (file)
index 0000000..162097b
--- /dev/null
@@ -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();
+               }
+       }
+
+}