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.File;
|
---|
12 | import java.io.IOException;
|
---|
13 | import java.io.UncheckedIOException;
|
---|
14 | import java.util.ArrayList;
|
---|
15 | import java.util.Collection;
|
---|
16 |
|
---|
17 | import org.apache.commons.jcs3.access.behavior.ICacheAccess;
|
---|
18 | import org.apache.commons.jcs3.engine.behavior.ICache;
|
---|
19 | import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
|
---|
20 | import org.openstreetmap.josm.data.cache.JCSCacheManager;
|
---|
21 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
22 | import org.openstreetmap.josm.gui.layer.geoimage.ImageDisplay.VisRect;
|
---|
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.getImageData().getImages()), 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 | cache = JCSCacheManager.getCache("geoimage-thumbnails", 0, 120,
|
---|
70 | Config.getDirs().getCacheDirectory(true).getPath() + File.separator + "geoimage-thumbnails");
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public void run() {
|
---|
76 | Logging.debug("Load Thumbnails");
|
---|
77 | tracker = new MediaTracker(MainApplication.getMap().mapView);
|
---|
78 | for (ImageEntry entry : data) {
|
---|
79 | if (stop) return;
|
---|
80 |
|
---|
81 | // Do not load thumbnails that were loaded before.
|
---|
82 | if (!entry.hasThumbnail()) {
|
---|
83 | entry.setThumbnail(loadThumb(entry));
|
---|
84 |
|
---|
85 | if (layer != null && MainApplication.isDisplayingMapView()) {
|
---|
86 | layer.updateBufferAndRepaint();
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | if (layer != null) {
|
---|
91 | layer.thumbsLoaded();
|
---|
92 | layer.updateBufferAndRepaint();
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | private BufferedImage loadThumb(ImageEntry entry) {
|
---|
97 | final String cacheIdent = entry.getFile().toString() + ICache.NAME_COMPONENT_DELIMITER + maxSize;
|
---|
98 |
|
---|
99 | if (!cacheOff && cache != null) {
|
---|
100 | try {
|
---|
101 | BufferedImageCacheEntry cacheEntry = cache.get(cacheIdent);
|
---|
102 | if (cacheEntry != null && cacheEntry.getImage() != null) {
|
---|
103 | Logging.debug(" from cache");
|
---|
104 | return cacheEntry.getImage();
|
---|
105 | }
|
---|
106 | } catch (IOException e) {
|
---|
107 | Logging.warn(e);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | Image img = Toolkit.getDefaultToolkit().createImage(entry.getFile().getPath());
|
---|
112 | tracker.addImage(img, 0);
|
---|
113 | try {
|
---|
114 | tracker.waitForID(0);
|
---|
115 | } catch (InterruptedException e) {
|
---|
116 | Logging.error(" InterruptedException while loading thumb");
|
---|
117 | Thread.currentThread().interrupt();
|
---|
118 | return null;
|
---|
119 | }
|
---|
120 | if (tracker.isErrorID(1) || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) {
|
---|
121 | Logging.error(" Invalid image");
|
---|
122 | return null;
|
---|
123 | }
|
---|
124 |
|
---|
125 | final int w = img.getWidth(null);
|
---|
126 | final int h = img.getHeight(null);
|
---|
127 | final int hh, ww;
|
---|
128 | final Integer exifOrientation = entry.getExifOrientation();
|
---|
129 | if (exifOrientation != null && ExifReader.orientationSwitchesDimensions(exifOrientation)) {
|
---|
130 | ww = h;
|
---|
131 | hh = w;
|
---|
132 | } else {
|
---|
133 | ww = w;
|
---|
134 | hh = h;
|
---|
135 | }
|
---|
136 |
|
---|
137 | Rectangle targetSize = ImageDisplay.calculateDrawImageRectangle(
|
---|
138 | new VisRect(0, 0, ww, hh),
|
---|
139 | new Rectangle(0, 0, maxSize, maxSize));
|
---|
140 | BufferedImage scaledBI = new BufferedImage(targetSize.width, targetSize.height, BufferedImage.TYPE_INT_RGB);
|
---|
141 | Graphics2D g = scaledBI.createGraphics();
|
---|
142 |
|
---|
143 | final AffineTransform scale = AffineTransform.getScaleInstance((double) targetSize.width / ww, (double) targetSize.height / hh);
|
---|
144 | if (exifOrientation != null) {
|
---|
145 | final AffineTransform restoreOrientation = ExifReader.getRestoreOrientationTransform(exifOrientation, w, h);
|
---|
146 | scale.concatenate(restoreOrientation);
|
---|
147 | }
|
---|
148 |
|
---|
149 | while (!g.drawImage(img, scale, null)) {
|
---|
150 | try {
|
---|
151 | Thread.sleep(10);
|
---|
152 | } catch (InterruptedException e) {
|
---|
153 | Logging.warn("InterruptedException while drawing thumb");
|
---|
154 | Thread.currentThread().interrupt();
|
---|
155 | }
|
---|
156 | }
|
---|
157 | g.dispose();
|
---|
158 | tracker.removeImage(img);
|
---|
159 |
|
---|
160 | if (scaledBI.getWidth() <= 0 || scaledBI.getHeight() <= 0) {
|
---|
161 | Logging.error(" Invalid image");
|
---|
162 | return null;
|
---|
163 | }
|
---|
164 |
|
---|
165 | if (!cacheOff && cache != null) {
|
---|
166 | try {
|
---|
167 | cache.put(cacheIdent, BufferedImageCacheEntry.pngEncoded(scaledBI));
|
---|
168 | } catch (UncheckedIOException e) {
|
---|
169 | Logging.warn("Failed to save geoimage thumb to cache");
|
---|
170 | Logging.warn(e);
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | return scaledBI;
|
---|
175 | }
|
---|
176 | }
|
---|