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