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