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.Collection;
|
---|
16 |
|
---|
17 | import javax.imageio.ImageIO;
|
---|
18 |
|
---|
19 | import org.apache.commons.jcs.access.behavior.ICacheAccess;
|
---|
20 | import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
|
---|
21 | import org.openstreetmap.josm.data.cache.JCSCacheManager;
|
---|
22 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
23 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
24 | import org.openstreetmap.josm.tools.ExifReader;
|
---|
25 | import org.openstreetmap.josm.tools.Logging;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Loads thumbnail previews for a list of images from a {@link GeoImageLayer}.
|
---|
29 | *
|
---|
30 | * Thumbnails are loaded in the background and cached on disk for the next session.
|
---|
31 | */
|
---|
32 | public class ThumbsLoader implements Runnable {
|
---|
33 | public static final int maxSize = 120;
|
---|
34 | public static final int minSize = 22;
|
---|
35 | public volatile boolean stop;
|
---|
36 | private final Collection<ImageEntry> data;
|
---|
37 | private final GeoImageLayer layer;
|
---|
38 | private MediaTracker tracker;
|
---|
39 | private ICacheAccess<String, BufferedImageCacheEntry> cache;
|
---|
40 | private final boolean cacheOff = Config.getPref().getBoolean("geoimage.noThumbnailCache", false);
|
---|
41 |
|
---|
42 | private ThumbsLoader(Collection<ImageEntry> data, GeoImageLayer layer) {
|
---|
43 | this.data = data;
|
---|
44 | this.layer = layer;
|
---|
45 | initCache();
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Constructs a new thumbnail loader that operates on a geoimage layer.
|
---|
50 | * @param layer geoimage layer
|
---|
51 | */
|
---|
52 | public ThumbsLoader(GeoImageLayer layer) {
|
---|
53 | this(new ArrayList<>(layer.data), layer);
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Constructs a new thumbnail loader that operates on the image entries
|
---|
58 | * @param entries image entries
|
---|
59 | */
|
---|
60 | public ThumbsLoader(Collection<ImageEntry> entries) {
|
---|
61 | this(entries, null);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Initialize the thumbnail cache.
|
---|
66 | */
|
---|
67 | private void initCache() {
|
---|
68 | if (!cacheOff) {
|
---|
69 | try {
|
---|
70 | cache = JCSCacheManager.getCache("geoimage-thumbnails", 0, 120,
|
---|
71 | Config.getDirs().getCacheDirectory(true).getPath() + File.separator + "geoimage-thumbnails");
|
---|
72 | } catch (IOException e) {
|
---|
73 | Logging.warn("Failed to initialize cache for geoimage-thumbnails");
|
---|
74 | Logging.warn(e);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public void run() {
|
---|
81 | Logging.debug("Load Thumbnails");
|
---|
82 | tracker = new MediaTracker(MainApplication.getMap().mapView);
|
---|
83 | for (ImageEntry entry : data) {
|
---|
84 | if (stop) return;
|
---|
85 |
|
---|
86 | // Do not load thumbnails that were loaded before.
|
---|
87 | if (!entry.hasThumbnail()) {
|
---|
88 | entry.setThumbnail(loadThumb(entry));
|
---|
89 |
|
---|
90 | if (layer != null && MainApplication.isDisplayingMapView()) {
|
---|
91 | layer.updateBufferAndRepaint();
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 | if (layer != null) {
|
---|
96 | layer.thumbsLoaded();
|
---|
97 | layer.updateBufferAndRepaint();
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | private BufferedImage loadThumb(ImageEntry entry) {
|
---|
102 | final String cacheIdent = entry.getFile().toString()+':'+maxSize;
|
---|
103 |
|
---|
104 | if (!cacheOff && cache != null) {
|
---|
105 | try {
|
---|
106 | BufferedImageCacheEntry cacheEntry = cache.get(cacheIdent);
|
---|
107 | if (cacheEntry != null && cacheEntry.getImage() != null) {
|
---|
108 | Logging.debug(" from cache");
|
---|
109 | return cacheEntry.getImage();
|
---|
110 | }
|
---|
111 | } catch (IOException e) {
|
---|
112 | Logging.warn(e);
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | Image img = Toolkit.getDefaultToolkit().createImage(entry.getFile().getPath());
|
---|
117 | tracker.addImage(img, 0);
|
---|
118 | try {
|
---|
119 | tracker.waitForID(0);
|
---|
120 | } catch (InterruptedException e) {
|
---|
121 | Logging.error(" InterruptedException while loading thumb");
|
---|
122 | Thread.currentThread().interrupt();
|
---|
123 | return null;
|
---|
124 | }
|
---|
125 | if (tracker.isErrorID(1) || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) {
|
---|
126 | Logging.error(" Invalid image");
|
---|
127 | return null;
|
---|
128 | }
|
---|
129 |
|
---|
130 | final int w = img.getWidth(null);
|
---|
131 | final int h = img.getHeight(null);
|
---|
132 | final int hh, ww;
|
---|
133 | final Integer exifOrientation = entry.getExifOrientation();
|
---|
134 | if (exifOrientation != null && ExifReader.orientationSwitchesDimensions(exifOrientation)) {
|
---|
135 | ww = h;
|
---|
136 | hh = w;
|
---|
137 | } else {
|
---|
138 | ww = w;
|
---|
139 | hh = h;
|
---|
140 | }
|
---|
141 |
|
---|
142 | Rectangle targetSize = ImageDisplay.calculateDrawImageRectangle(
|
---|
143 | new Rectangle(0, 0, ww, hh),
|
---|
144 | new Rectangle(0, 0, maxSize, maxSize));
|
---|
145 | BufferedImage scaledBI = new BufferedImage(targetSize.width, targetSize.height, BufferedImage.TYPE_INT_RGB);
|
---|
146 | Graphics2D g = scaledBI.createGraphics();
|
---|
147 |
|
---|
148 | final AffineTransform scale = AffineTransform.getScaleInstance((double) targetSize.width / ww, (double) targetSize.height / hh);
|
---|
149 | if (exifOrientation != null) {
|
---|
150 | final AffineTransform restoreOrientation = ExifReader.getRestoreOrientationTransform(exifOrientation, w, h);
|
---|
151 | scale.concatenate(restoreOrientation);
|
---|
152 | }
|
---|
153 |
|
---|
154 | while (!g.drawImage(img, scale, null)) {
|
---|
155 | try {
|
---|
156 | Thread.sleep(10);
|
---|
157 | } catch (InterruptedException e) {
|
---|
158 | Logging.warn("InterruptedException while drawing thumb");
|
---|
159 | Thread.currentThread().interrupt();
|
---|
160 | }
|
---|
161 | }
|
---|
162 | g.dispose();
|
---|
163 | tracker.removeImage(img);
|
---|
164 |
|
---|
165 | if (scaledBI.getWidth() <= 0 || scaledBI.getHeight() <= 0) {
|
---|
166 | Logging.error(" Invalid image");
|
---|
167 | return null;
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (!cacheOff && cache != null) {
|
---|
171 | try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
|
---|
172 | ImageIO.write(scaledBI, "png", output);
|
---|
173 | cache.put(cacheIdent, new BufferedImageCacheEntry(output.toByteArray()));
|
---|
174 | } catch (IOException e) {
|
---|
175 | Logging.warn("Failed to save geoimage thumb to cache");
|
---|
176 | Logging.warn(e);
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | return scaledBI;
|
---|
181 | }
|
---|
182 | }
|
---|