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

Last change on this file since 17363 was 17363, checked in by simon04, 4 years ago

see #20141 - Extract BufferedImageCacheEntry.pngEncoded

  • Property svn:eol-style set to native
File size: 6.1 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.File;
12import java.io.IOException;
13import java.io.UncheckedIOException;
14import java.util.ArrayList;
15import java.util.Collection;
16
17import org.apache.commons.jcs3.access.behavior.ICacheAccess;
18import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
19import org.openstreetmap.josm.data.cache.JCSCacheManager;
20import org.openstreetmap.josm.gui.MainApplication;
21import org.openstreetmap.josm.gui.layer.geoimage.ImageDisplay.VisRect;
22import org.openstreetmap.josm.spi.preferences.Config;
23import org.openstreetmap.josm.tools.ExifReader;
24import org.openstreetmap.josm.tools.Logging;
25
26/**
27 * Loads thumbnail previews for a list of images from a {@link GeoImageLayer}.
28 *
29 * Thumbnails are loaded in the background and cached on disk for the next session.
30 */
31public class ThumbsLoader implements Runnable {
32 public static final int maxSize = 120;
33 public static final int minSize = 22;
34 public volatile boolean stop;
35 private final Collection<ImageEntry> data;
36 private final GeoImageLayer layer;
37 private MediaTracker tracker;
38 private ICacheAccess<String, BufferedImageCacheEntry> cache;
39 private final boolean cacheOff = Config.getPref().getBoolean("geoimage.noThumbnailCache", false);
40
41 private ThumbsLoader(Collection<ImageEntry> data, GeoImageLayer layer) {
42 this.data = data;
43 this.layer = layer;
44 initCache();
45 }
46
47 /**
48 * Constructs a new thumbnail loader that operates on a geoimage layer.
49 * @param layer geoimage layer
50 */
51 public ThumbsLoader(GeoImageLayer layer) {
52 this(new ArrayList<>(layer.getImageData().getImages()), layer);
53 }
54
55 /**
56 * Constructs a new thumbnail loader that operates on the image entries
57 * @param entries image entries
58 */
59 public ThumbsLoader(Collection<ImageEntry> entries) {
60 this(entries, null);
61 }
62
63 /**
64 * Initialize the thumbnail cache.
65 */
66 private void initCache() {
67 if (!cacheOff) {
68 cache = JCSCacheManager.getCache("geoimage-thumbnails", 0, 120,
69 Config.getDirs().getCacheDirectory(true).getPath() + File.separator + "geoimage-thumbnails");
70 }
71 }
72
73 @Override
74 public void run() {
75 Logging.debug("Load Thumbnails");
76 tracker = new MediaTracker(MainApplication.getMap().mapView);
77 for (ImageEntry entry : data) {
78 if (stop) return;
79
80 // Do not load thumbnails that were loaded before.
81 if (!entry.hasThumbnail()) {
82 entry.setThumbnail(loadThumb(entry));
83
84 if (layer != null && MainApplication.isDisplayingMapView()) {
85 layer.updateBufferAndRepaint();
86 }
87 }
88 }
89 if (layer != null) {
90 layer.thumbsLoaded();
91 layer.updateBufferAndRepaint();
92 }
93 }
94
95 private BufferedImage loadThumb(ImageEntry entry) {
96 final String cacheIdent = entry.getFile().toString()+':'+maxSize;
97
98 if (!cacheOff && cache != null) {
99 try {
100 BufferedImageCacheEntry cacheEntry = cache.get(cacheIdent);
101 if (cacheEntry != null && cacheEntry.getImage() != null) {
102 Logging.debug(" from cache");
103 return cacheEntry.getImage();
104 }
105 } catch (IOException e) {
106 Logging.warn(e);
107 }
108 }
109
110 Image img = Toolkit.getDefaultToolkit().createImage(entry.getFile().getPath());
111 tracker.addImage(img, 0);
112 try {
113 tracker.waitForID(0);
114 } catch (InterruptedException e) {
115 Logging.error(" InterruptedException while loading thumb");
116 Thread.currentThread().interrupt();
117 return null;
118 }
119 if (tracker.isErrorID(1) || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) {
120 Logging.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 VisRect(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 e) {
152 Logging.warn("InterruptedException while drawing thumb");
153 Thread.currentThread().interrupt();
154 }
155 }
156 g.dispose();
157 tracker.removeImage(img);
158
159 if (scaledBI.getWidth() <= 0 || scaledBI.getHeight() <= 0) {
160 Logging.error(" Invalid image");
161 return null;
162 }
163
164 if (!cacheOff && cache != null) {
165 try {
166 cache.put(cacheIdent, BufferedImageCacheEntry.pngEncoded(scaledBI));
167 } catch (UncheckedIOException e) {
168 Logging.warn("Failed to save geoimage thumb to cache");
169 Logging.warn(e);
170 }
171 }
172
173 return scaledBI;
174 }
175}
Note: See TracBrowser for help on using the repository browser.