]> gerrit.simantics Code Review - simantics/district.git/blob - 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
1 package org.simantics.maps.internal;
2
3 import java.awt.Graphics2D;
4 import java.awt.GraphicsConfiguration;
5 import java.awt.GraphicsEnvironment;
6 import java.awt.image.BufferedImage;
7
8 /**
9  * @author Tuukka Lehtonen
10  * @since 1.35.0
11  */
12 public class ImageUtil {
13
14         public static BufferedImage createScreenCompatibleImage(int width, int height, int transparency) {
15                 return createCompatibleImage(
16                                 GraphicsEnvironment
17                                 .getLocalGraphicsEnvironment()
18                                 .getDefaultScreenDevice()
19                                 .getDefaultConfiguration(),
20                                 width, height, transparency);
21         }
22
23         public static BufferedImage createCompatibleImage(GraphicsConfiguration conf, int width, int height, int transparency) {
24                 return conf.createCompatibleImage(width, height, transparency);
25         }
26
27         public static BufferedImage toScreenCompatibleImage(BufferedImage img) {
28                 return toCompatibleImage(
29                                 GraphicsEnvironment
30                                 .getLocalGraphicsEnvironment()
31                                 .getDefaultScreenDevice()
32                                 .getDefaultConfiguration(),
33                                 img);
34         }
35
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()))
39                         return img;
40
41 //              BufferedImage result = null;
42 //              if (img.getAlphaRaster() != null) {
43 //                      result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
44 //              } else {
45 //                      result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
46 //              }
47
48                 BufferedImage result = createCompatibleImage(conf, img.getWidth(), img.getHeight(), img.getTransparency());
49                 Graphics2D g = result.createGraphics();
50                 try {
51                         g.drawImage(img, 0, 0, null);
52                         return result;
53                 } finally {
54                         g.dispose();
55                 }
56         }
57
58 }