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

Last change on this file since 13127 was 13127, checked in by Don-vip, 8 years ago

see #15476, fix #15511 - fix image scaling regression and makes geoimage feature more configurable through prefs (adjustable max zoom, zoom-step, click zooming with mouse buttons (e.g. if a mouse wheel is not present). Patch by cmuelle8

  • Property svn:eol-style set to native
File size: 6.4 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.data.cache.BufferedImageCacheEntry;
21import org.openstreetmap.josm.data.cache.JCSCacheManager;
22import org.openstreetmap.josm.gui.MainApplication;
23import org.openstreetmap.josm.gui.layer.geoimage.ImageDisplay.VisRect;
24import org.openstreetmap.josm.spi.preferences.Config;
25import org.openstreetmap.josm.tools.ExifReader;
26import 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 */
33public 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.data), 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 try {
71 cache = JCSCacheManager.getCache("geoimage-thumbnails", 0, 120,
72 Config.getDirs().getCacheDirectory(true).getPath() + File.separator + "geoimage-thumbnails");
73 } catch (IOException e) {
74 Logging.warn("Failed to initialize cache for geoimage-thumbnails");
75 Logging.warn(e);
76 }
77 }
78 }
79
80 @Override
81 public void run() {
82 Logging.debug("Load Thumbnails");
83 tracker = new MediaTracker(MainApplication.getMap().mapView);
84 for (ImageEntry entry : data) {
85 if (stop) return;
86
87 // Do not load thumbnails that were loaded before.
88 if (!entry.hasThumbnail()) {
89 entry.setThumbnail(loadThumb(entry));
90
91 if (layer != null && MainApplication.isDisplayingMapView()) {
92 layer.updateBufferAndRepaint();
93 }
94 }
95 }
96 if (layer != null) {
97 layer.thumbsLoaded();
98 layer.updateBufferAndRepaint();
99 }
100 }
101
102 private BufferedImage loadThumb(ImageEntry entry) {
103 final String cacheIdent = entry.getFile().toString()+':'+maxSize;
104
105 if (!cacheOff && cache != null) {
106 try {
107 BufferedImageCacheEntry cacheEntry = cache.get(cacheIdent);
108 if (cacheEntry != null && cacheEntry.getImage() != null) {
109 Logging.debug(" from cache");
110 return cacheEntry.getImage();
111 }
112 } catch (IOException e) {
113 Logging.warn(e);
114 }
115 }
116
117 Image img = Toolkit.getDefaultToolkit().createImage(entry.getFile().getPath());
118 tracker.addImage(img, 0);
119 try {
120 tracker.waitForID(0);
121 } catch (InterruptedException e) {
122 Logging.error(" InterruptedException while loading thumb");
123 Thread.currentThread().interrupt();
124 return null;
125 }
126 if (tracker.isErrorID(1) || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) {
127 Logging.error(" Invalid image");
128 return null;
129 }
130
131 final int w = img.getWidth(null);
132 final int h = img.getHeight(null);
133 final int hh, ww;
134 final Integer exifOrientation = entry.getExifOrientation();
135 if (exifOrientation != null && ExifReader.orientationSwitchesDimensions(exifOrientation)) {
136 ww = h;
137 hh = w;
138 } else {
139 ww = w;
140 hh = h;
141 }
142
143 Rectangle targetSize = ImageDisplay.calculateDrawImageRectangle(
144 new VisRect(0, 0, ww, hh),
145 new Rectangle(0, 0, maxSize, maxSize));
146 BufferedImage scaledBI = new BufferedImage(targetSize.width, targetSize.height, BufferedImage.TYPE_INT_RGB);
147 Graphics2D g = scaledBI.createGraphics();
148
149 final AffineTransform scale = AffineTransform.getScaleInstance((double) targetSize.width / ww, (double) targetSize.height / hh);
150 if (exifOrientation != null) {
151 final AffineTransform restoreOrientation = ExifReader.getRestoreOrientationTransform(exifOrientation, w, h);
152 scale.concatenate(restoreOrientation);
153 }
154
155 while (!g.drawImage(img, scale, null)) {
156 try {
157 Thread.sleep(10);
158 } catch (InterruptedException e) {
159 Logging.warn("InterruptedException while drawing thumb");
160 Thread.currentThread().interrupt();
161 }
162 }
163 g.dispose();
164 tracker.removeImage(img);
165
166 if (scaledBI.getWidth() <= 0 || scaledBI.getHeight() <= 0) {
167 Logging.error(" Invalid image");
168 return null;
169 }
170
171 if (!cacheOff && cache != null) {
172 try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
173 ImageIO.write(scaledBI, "png", output);
174 cache.put(cacheIdent, new BufferedImageCacheEntry(output.toByteArray()));
175 } catch (IOException e) {
176 Logging.warn("Failed to save geoimage thumb to cache");
177 Logging.warn(e);
178 }
179 }
180
181 return scaledBI;
182 }
183}
Note: See TracBrowser for help on using the repository browser.