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

Last change on this file since 16398 was 16398, checked in by simon04, 5 years ago

see #19208 - JCS: update package to org.apache.commons.jcs3

  • Property svn:eol-style set to native
File size: 6.2 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.jcs3.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.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}
Note: See TracBrowser for help on using the repository browser.