<?xml version="1.0" encoding="UTF-8"?>
<classpath>
- <classpathentry kind="src" path="src"/>
- <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+ <classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
org.eclipse.jface,
org.eclipse.ui.ide,
org.eclipse.ui.workbench,
- org.simantics.district.geotools;bundle-version="1.0.0"
+ org.simantics.district.geotools;bundle-version="1.0.0",
+ org.slf4j.api;bundle-version="1.7.25"
Export-Package: org.simantics.maps,
org.simantics.maps.debug,
org.simantics.maps.eclipse,
org.simantics.maps.tile,
org.simantics.maps.wms
Import-Package: org.simantics.scenegraph.g2d
-Bundle-RequiredExecutionEnvironment: JavaSE-1.6,
- JavaSE-1.7
+Bundle-RequiredExecutionEnvironment: JavaSE-1.8
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
+import java.awt.Transparency;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.net.URI;
import java.net.URISyntaxException;
import org.simantics.maps.ProvisionException;
+import org.simantics.maps.internal.ImageUtil;
import org.simantics.maps.tile.ITileProvider;
import org.simantics.maps.tile.TileKey;
);
// System.out.println(yTiles + ", " + key.y + " => " + r);
- BufferedImage image = new BufferedImage(tileSize, tileSize, BufferedImage.TYPE_3BYTE_BGR);
+ BufferedImage image = ImageUtil.createScreenCompatibleImage(tileSize, tileSize, Transparency.OPAQUE);
Graphics2D g = image.createGraphics();
try {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
- g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 32));
+ g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
g.setColor(Color.BLACK);
g.fillRect(0, 0, tileSize, tileSize);
import java.io.File;
import java.io.IOException;
import java.net.URI;
+import java.util.concurrent.ForkJoinPool;
import javax.imageio.ImageIO;
import org.eclipse.core.runtime.Platform;
import org.osgi.framework.Bundle;
import org.simantics.maps.ProvisionException;
+import org.simantics.maps.internal.ImageUtil;
import org.simantics.maps.tile.ITileProvider;
import org.simantics.maps.tile.TileKey;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* @author Tuukka Lehtonen
*/
public class DiskCachingTileProvider implements ITileProvider {
+ private static final Logger LOGGER = LoggerFactory.getLogger(DiskCachingTileProvider.class);
+
private final String CACHED_IMAGE_FORMAT = "png";
ITileProvider provider;
boolean supportAlpha;
-// private ImageWriter writer;
-//
-// private ImageReader reader;
-//
-// private ImageTypeSpecifier noAlphaType = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_3BYTE_BGR);
-//
-// private ImageTypeSpecifier alphaType = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_4BYTE_ABGR);
-
-
public DiskCachingTileProvider(ITileProvider provider, Boolean supportAlpha) {
this.provider = provider;
this.supportAlpha = supportAlpha;
if (!f.exists()) {
f.mkdirs();
} else if (!f.isDirectory()) {
-// FIXME
-// ErrorLogger.defaultLogError("Cache directory " + f.toString() + " already exists as a file, cannot use it for cache", new Exception());
+ LOGGER.error("Tile cache directory " + f.toString() + " already exists as a file, cannot use it as a cache directory", new Exception());
return;
}
IPath cachePath = toTileCachePath(key).addFileExtension(CACHED_IMAGE_FORMAT);
File f = cachePath.toFile();
if (f.exists()) {
-// ImageInputStream iis = null;
try {
-// iis = ImageIO.createImageInputStream(f);
-// ImageReadParam param = reader.getDefaultReadParam();
-// param.setDestinationType(supportAlpha ? alphaType : noAlphaType);
-// reader.setInput(iis, true, true);
-// BufferedImage img = reader.read(0, param);
BufferedImage img = ImageIO.read(f);
if (img != null) {
//System.out.println("[" + provider + "] Disk cache hit: " + key);
//System.out.println("[" + provider + "] image: " + img);
// Need to make sure that the type of the loaded image is
// something that can be rendered in an accelerated fashion.
- if (img.getType() != BufferedImage.TYPE_CUSTOM)
- return img;
-
- BufferedImage img2 = null;
- if (img.getAlphaRaster() != null) {
- img2 = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
- } else {
- img2 = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
- }
- //System.out.println("[" + provider + "] image2: " + img2);
-
- img.copyData(img2.getRaster());
- return img2;
+ return ImageUtil.toScreenCompatibleImage(img);
}
} catch (IOException e) {
- e.printStackTrace();
+ LOGGER.warn("Failed to load cached tile from {}", f.toString(), e);
} finally {
-// try {
-// iis.close();
-// } catch (IOException e) {
-// }
}
// Data is most likely corrupted, just destroy it.
f.delete();
}
@Override
- public Image get(final TileKey key) throws ProvisionException {
+ public Image get(TileKey key) throws ProvisionException {
Image img = lookupFromDisk(key);
if (img == null) {
img = provider.get(key);
final Image image = img;
-
- // Write image to disk in the background
- // TODO: use somekind of worker executor system instead of just threads.
- new Thread() {
- @Override
- public void run() {
- cacheToDisk(key, image);
- }
- }.start();
+ if (cachePath != null) {
+ // Write image to disk in the background
+ ForkJoinPool.commonPool().submit(() -> cacheToDisk(key, image));
+ }
}
return img;
}
--- /dev/null
+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();
+ }
+ }
+
+}
import org.simantics.maps.ProvisionException;
import org.simantics.maps.WebService;
+import org.simantics.maps.internal.ImageUtil;
import org.simantics.maps.tile.ITileProvider;
import org.simantics.maps.tile.TileKey;
import org.simantics.maps.wms.ServiceException;
if (img == null) {
throw new IIOException("ImageIO returned null, unable to decode stream as image data.");
}
- return img;
+ return ImageUtil.toScreenCompatibleImage(img);
}
}
import java.awt.geom.Rectangle2D;
import java.awt.image.ImageObserver;
import java.awt.image.VolatileImage;
-import java.net.MalformedURLException;
-import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import org.simantics.maps.tile.TileKey;
import org.simantics.scenegraph.g2d.G2DNode;
import org.simantics.scenegraph.g2d.G2DRenderingHints;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* @author Tuukka Lehtonen
private static final long serialVersionUID = 2490944880914577411L;
+ private static final Logger LOGGER = LoggerFactory.getLogger(MapNode.class);
+
private final double MAP_SCALE = 1;
private final int MAX_TILE_LEVEL = 19;
private final int TILE_PIXEL_SIZE = 256;
this.tileCache = new TileCache(job);
this.tileCache.addTileListener(this);
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (URISyntaxException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
} catch(Exception e ) {
- e.printStackTrace();
+ LOGGER.error("Failed to initialize MapNode", e);
}
}
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
+import java.awt.Transparency;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.net.URI;
import java.util.List;
import org.simantics.maps.ProvisionException;
+import org.simantics.maps.internal.ImageUtil;
/**
* @author Tuukka Lehtonen
}
if (result == null) {
- result = new BufferedImage(tileSize, tileSize, BufferedImage.TYPE_3BYTE_BGR);
+ result = ImageUtil.createScreenCompatibleImage(tileSize, tileSize, Transparency.OPAQUE);
g = result.createGraphics();
}
import javax.imageio.ImageIO;
import org.simantics.maps.ProvisionException;
+import org.simantics.maps.internal.ImageUtil;
/**
* @author Tuukka Lehtonen
return outOfBoundsImage;
final int outOfBoundsImageSize = 1;
- BufferedImage image = new BufferedImage(outOfBoundsImageSize, outOfBoundsImageSize, BufferedImage.TYPE_3BYTE_BGR);
+ BufferedImage image = ImageUtil.createScreenCompatibleImage(outOfBoundsImageSize, outOfBoundsImageSize, BufferedImage.OPAQUE);
Graphics2D g = image.createGraphics();
try {
g.setColor(Color.PINK);
//System.out.println("img = " + img);
if (img == null)
throw new ProvisionException("Failed to provide image for rectangle: " + r);
-
- if (BufferedImage.TYPE_4BYTE_ABGR != img.getType()) {
- BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
- img.copyData(img2.getRaster());
- return img2;
- }
- return img;
+ return ImageUtil.toScreenCompatibleImage(img);
} finally {
if (tempFile.exists()) {
// System.out.println("DELETING TEMP FILE: " + tempFile);
import org.simantics.maps.ProvisionException;
import org.simantics.maps.WebService;
+import org.simantics.maps.internal.ImageUtil;
import org.simantics.maps.tile.ITileProvider;
import org.simantics.maps.tile.TileKey;
if (img == null) {
throw new IIOException("ImageIO returned null, unable to decode stream as image data.");
}
- return img;
+ return ImageUtil.toScreenCompatibleImage(img);
}
}