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

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

fix #21175 - fix NPE

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