1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.geoimage;
|
---|
3 |
|
---|
4 | import java.awt.Graphics2D;
|
---|
5 | import java.awt.Image;
|
---|
6 | import java.awt.MediaTracker;
|
---|
7 | import java.awt.Rectangle;
|
---|
8 | import java.awt.Toolkit;
|
---|
9 | import java.awt.geom.AffineTransform;
|
---|
10 | import java.awt.image.BufferedImage;
|
---|
11 | import java.io.ByteArrayOutputStream;
|
---|
12 | import java.io.File;
|
---|
13 | import java.io.IOException;
|
---|
14 | import java.util.ArrayList;
|
---|
15 | import java.util.List;
|
---|
16 |
|
---|
17 | import javax.imageio.ImageIO;
|
---|
18 |
|
---|
19 | import org.apache.commons.jcs.access.behavior.ICacheAccess;
|
---|
20 | import org.openstreetmap.josm.Main;
|
---|
21 | import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
|
---|
22 | import org.openstreetmap.josm.data.cache.JCSCacheManager;
|
---|
23 | import org.openstreetmap.josm.tools.ExifReader;
|
---|
24 |
|
---|
25 | public class ThumbsLoader implements Runnable {
|
---|
26 | public static final int maxSize = 120;
|
---|
27 | public static final int minSize = 22;
|
---|
28 | public volatile boolean stop = false;
|
---|
29 | private List<ImageEntry> data;
|
---|
30 | private GeoImageLayer layer;
|
---|
31 | private MediaTracker tracker;
|
---|
32 | private ICacheAccess<String, BufferedImageCacheEntry> cache;
|
---|
33 | private boolean cacheOff = Main.pref.getBoolean("geoimage.noThumbnailCache", false);
|
---|
34 |
|
---|
35 | public ThumbsLoader(GeoImageLayer layer) {
|
---|
36 | this.layer = layer;
|
---|
37 | this.data = new ArrayList<>(layer.data);
|
---|
38 | if (!cacheOff) {
|
---|
39 | try {
|
---|
40 | cache = JCSCacheManager.getCache("geoimage-thumbnails", 0, 120,
|
---|
41 | Main.pref.getCacheDirectory().getPath() + File.separator + "geoimage-thumbnails");
|
---|
42 | } catch (IOException e) {
|
---|
43 | Main.warn("Failed to initialize cache for geoimage-thumbnails");
|
---|
44 | Main.warn(e);
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public void run() {
|
---|
51 | Main.debug("Load Thumbnails");
|
---|
52 | tracker = new MediaTracker(Main.map.mapView);
|
---|
53 | for (int i = 0; i < data.size(); i++) {
|
---|
54 | if (stop) return;
|
---|
55 |
|
---|
56 | // Do not load thumbnails that were loaded before.
|
---|
57 | if (data.get(i).thumbnail == null) {
|
---|
58 | data.get(i).thumbnail = loadThumb(data.get(i));
|
---|
59 |
|
---|
60 | if (Main.isDisplayingMapView()) {
|
---|
61 | layer.updateOffscreenBuffer = true;
|
---|
62 | Main.map.mapView.repaint();
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|
66 | layer.thumbsLoaded();
|
---|
67 | layer.updateOffscreenBuffer = true;
|
---|
68 | Main.map.mapView.repaint();
|
---|
69 | }
|
---|
70 |
|
---|
71 | private BufferedImage loadThumb(ImageEntry entry) {
|
---|
72 | final String cacheIdent = entry.getFile()+":"+maxSize;
|
---|
73 |
|
---|
74 | if (!cacheOff && cache != null) {
|
---|
75 | try {
|
---|
76 | BufferedImageCacheEntry cacheEntry = cache.get(cacheIdent);
|
---|
77 | if (cacheEntry != null && cacheEntry.getImage() != null) {
|
---|
78 | Main.debug(" from cache");
|
---|
79 | return cacheEntry.getImage();
|
---|
80 | }
|
---|
81 | } catch (IOException e) {
|
---|
82 | Main.warn(e);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | Image img = Toolkit.getDefaultToolkit().createImage(entry.getFile().getPath());
|
---|
87 | tracker.addImage(img, 0);
|
---|
88 | try {
|
---|
89 | tracker.waitForID(0);
|
---|
90 | } catch (InterruptedException e) {
|
---|
91 | Main.error(" InterruptedException while loading thumb");
|
---|
92 | return null;
|
---|
93 | }
|
---|
94 | if (tracker.isErrorID(1) || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) {
|
---|
95 | Main.error(" Invalid image");
|
---|
96 | return null;
|
---|
97 | }
|
---|
98 |
|
---|
99 | final int w = img.getWidth(null);
|
---|
100 | final int h = img.getHeight(null);
|
---|
101 | final int hh, ww;
|
---|
102 | final Integer exifOrientation = entry.getExifOrientation();
|
---|
103 | if (exifOrientation != null && ExifReader.orientationSwitchesDimensions(exifOrientation)) {
|
---|
104 | ww = h;
|
---|
105 | hh = w;
|
---|
106 | } else {
|
---|
107 | ww = w;
|
---|
108 | hh = h;
|
---|
109 | }
|
---|
110 |
|
---|
111 | Rectangle targetSize = ImageDisplay.calculateDrawImageRectangle(
|
---|
112 | new Rectangle(0, 0, ww, hh),
|
---|
113 | new Rectangle(0, 0, maxSize, maxSize));
|
---|
114 | BufferedImage scaledBI = new BufferedImage(targetSize.width, targetSize.height, BufferedImage.TYPE_INT_RGB);
|
---|
115 | Graphics2D g = scaledBI.createGraphics();
|
---|
116 |
|
---|
117 | final AffineTransform scale = AffineTransform.getScaleInstance((double) targetSize.width / ww, (double) targetSize.height / hh);
|
---|
118 | if (exifOrientation != null) {
|
---|
119 | final AffineTransform restoreOrientation = ExifReader.getRestoreOrientationTransform(exifOrientation, w, h);
|
---|
120 | scale.concatenate(restoreOrientation);
|
---|
121 | }
|
---|
122 |
|
---|
123 | while (!g.drawImage(img, scale, null)) {
|
---|
124 | try {
|
---|
125 | Thread.sleep(10);
|
---|
126 | } catch (InterruptedException ie) {
|
---|
127 | Main.warn("InterruptedException while drawing thumb");
|
---|
128 | }
|
---|
129 | }
|
---|
130 | g.dispose();
|
---|
131 | tracker.removeImage(img);
|
---|
132 |
|
---|
133 | if (scaledBI.getWidth() <= 0 || scaledBI.getHeight() <= 0) {
|
---|
134 | Main.error(" Invalid image");
|
---|
135 | return null;
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (!cacheOff && cache != null) {
|
---|
139 | try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
|
---|
140 | ImageIO.write(scaledBI, "png", output);
|
---|
141 | cache.put(cacheIdent, new BufferedImageCacheEntry(output.toByteArray()));
|
---|
142 | } catch (IOException e) {
|
---|
143 | Main.warn("Failed to save geoimage thumb to cache");
|
---|
144 | Main.warn(e);
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | return scaledBI;
|
---|
149 | }
|
---|
150 | }
|
---|