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

Last change on this file was 18108, checked in by Don-vip, 3 years ago

fix #21175 - fix NPE

  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.geoimage;
3
4import java.awt.Dimension;
5import java.awt.image.BufferedImage;
6import java.io.File;
7import java.io.IOException;
8import java.io.UncheckedIOException;
9import java.util.ArrayList;
10import java.util.Collection;
11
12import org.apache.commons.jcs3.access.behavior.ICacheAccess;
13import org.apache.commons.jcs3.engine.behavior.ICache;
14import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
15import org.openstreetmap.josm.data.cache.JCSCacheManager;
16import org.openstreetmap.josm.gui.MainApplication;
17import org.openstreetmap.josm.spi.preferences.Config;
18import org.openstreetmap.josm.tools.Logging;
19import org.openstreetmap.josm.tools.Stopwatch;
20
21/**
22 * Loads thumbnail previews for a list of images from a {@link GeoImageLayer}.
23 *
24 * Thumbnails are loaded in the background and cached on disk for the next session.
25 */
26public class ThumbsLoader implements Runnable {
27 public static final int maxSize = 120;
28 public static final int minSize = 22;
29 public volatile boolean stop;
30 private final Collection<ImageEntry> data;
31 private final GeoImageLayer layer;
32 private ICacheAccess<String, BufferedImageCacheEntry> cache;
33 private final boolean cacheOff = Config.getPref().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.getImageData().getImages()), 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 cache = JCSCacheManager.getCache("geoimage-thumbnails", 0, 120,
63 Config.getDirs().getCacheDirectory(true).getPath() + File.separator + "geoimage-thumbnails");
64 }
65 }
66
67 @Override
68 public void run() {
69 int count = 0;
70 Stopwatch stopwatch = Stopwatch.createStarted();
71 Logging.debug("Loading {0} thumbnails", data.size());
72 for (ImageEntry entry : data) {
73 if (stop) return;
74
75 // Do not load thumbnails that were loaded before.
76 if (!entry.hasThumbnail()) {
77 entry.setThumbnail(loadThumb(entry));
78
79 if (layer != null && MainApplication.isDisplayingMapView()) {
80 layer.updateBufferAndRepaint();
81 }
82 }
83 count++;
84 }
85 Logging.debug("Loaded {0} thumbnails in {1}", count, stopwatch);
86 if (layer != null) {
87 layer.thumbsLoaded();
88 layer.updateBufferAndRepaint();
89 }
90 }
91
92 private BufferedImage loadThumb(ImageEntry entry) {
93 final String cacheIdent = entry.getFile().toString() + ICache.NAME_COMPONENT_DELIMITER + maxSize;
94
95 if (!cacheOff && cache != null) {
96 try {
97 BufferedImageCacheEntry cacheEntry = cache.get(cacheIdent);
98 if (cacheEntry != null && cacheEntry.getImage() != null) {
99 Logging.debug("{0} from cache", cacheIdent);
100 return cacheEntry.getImage();
101 }
102 } catch (IOException e) {
103 Logging.warn(e);
104 }
105 }
106
107 BufferedImage img;
108 try {
109 img = entry.read(new Dimension(maxSize, maxSize));
110 } catch (IOException e) {
111 Logging.warn("Failed to load geoimage thumb");
112 Logging.warn(e);
113 return null;
114 }
115
116 if (img == null || img.getWidth() <= 0 || img.getHeight() <= 0) {
117 Logging.error(" Invalid image");
118 return null;
119 }
120
121 if (!cacheOff && cache != null) {
122 try {
123 cache.put(cacheIdent, BufferedImageCacheEntry.pngEncoded(img));
124 } catch (UncheckedIOException e) {
125 Logging.warn("Failed to save geoimage thumb to cache");
126 Logging.warn(e);
127 }
128 }
129
130 return img;
131 }
132}
Note: See TracBrowser for help on using the repository browser.