source: josm/trunk/src/org/openstreetmap/josm/gui/layer/geoimage/ThumbsLoader.java@ 12340

Last change on this file since 12340 was 12340, checked in by michael2402, 7 years ago

Fix #14893: Invalidate geo image / marker layer instead of repainting map view

  • Property svn:eol-style set to native
File size: 5.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.geoimage;
3
4import java.awt.Graphics2D;
5import java.awt.Image;
6import java.awt.MediaTracker;
7import java.awt.Rectangle;
8import java.awt.Toolkit;
9import java.awt.geom.AffineTransform;
10import java.awt.image.BufferedImage;
11import java.io.ByteArrayOutputStream;
12import java.io.File;
13import java.io.IOException;
14import java.util.ArrayList;
15import java.util.Collection;
16
17import javax.imageio.ImageIO;
18
19import org.apache.commons.jcs.access.behavior.ICacheAccess;
20import org.openstreetmap.josm.Main;
21import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
22import org.openstreetmap.josm.data.cache.JCSCacheManager;
23import org.openstreetmap.josm.tools.ExifReader;
24
25public 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.updateBufferAndRepaint();
85 }
86 }
87 }
88 if (layer != null) {
89 layer.thumbsLoaded();
90 layer.updateBufferAndRepaint();
91 }
92 }
93
94 private BufferedImage loadThumb(ImageEntry entry) {
95 final String cacheIdent = entry.getFile().toString()+':'+maxSize;
96
97 if (!cacheOff && cache != null) {
98 try {
99 BufferedImageCacheEntry cacheEntry = cache.get(cacheIdent);
100 if (cacheEntry != null && cacheEntry.getImage() != null) {
101 Main.debug(" from cache");
102 return cacheEntry.getImage();
103 }
104 } catch (IOException e) {
105 Main.warn(e);
106 }
107 }
108
109 Image img = Toolkit.getDefaultToolkit().createImage(entry.getFile().getPath());
110 tracker.addImage(img, 0);
111 try {
112 tracker.waitForID(0);
113 } catch (InterruptedException e) {
114 Main.error(" InterruptedException while loading thumb");
115 Thread.currentThread().interrupt();
116 return null;
117 }
118 if (tracker.isErrorID(1) || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) {
119 Main.error(" Invalid image");
120 return null;
121 }
122
123 final int w = img.getWidth(null);
124 final int h = img.getHeight(null);
125 final int hh, ww;
126 final Integer exifOrientation = entry.getExifOrientation();
127 if (exifOrientation != null && ExifReader.orientationSwitchesDimensions(exifOrientation)) {
128 ww = h;
129 hh = w;
130 } else {
131 ww = w;
132 hh = h;
133 }
134
135 Rectangle targetSize = ImageDisplay.calculateDrawImageRectangle(
136 new Rectangle(0, 0, ww, hh),
137 new Rectangle(0, 0, maxSize, maxSize));
138 BufferedImage scaledBI = new BufferedImage(targetSize.width, targetSize.height, BufferedImage.TYPE_INT_RGB);
139 Graphics2D g = scaledBI.createGraphics();
140
141 final AffineTransform scale = AffineTransform.getScaleInstance((double) targetSize.width / ww, (double) targetSize.height / hh);
142 if (exifOrientation != null) {
143 final AffineTransform restoreOrientation = ExifReader.getRestoreOrientationTransform(exifOrientation, w, h);
144 scale.concatenate(restoreOrientation);
145 }
146
147 while (!g.drawImage(img, scale, null)) {
148 try {
149 Thread.sleep(10);
150 } catch (InterruptedException e) {
151 Main.warn("InterruptedException while drawing thumb");
152 Thread.currentThread().interrupt();
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}
Note: See TracBrowser for help on using the repository browser.