[2593] | 1 | // License: GPL. See LICENSE file for details.
|
---|
| 2 | package org.openstreetmap.josm.gui.layer.geoimage;
|
---|
| 3 |
|
---|
| 4 | import java.awt.Graphics2D;
|
---|
| 5 | import java.awt.Image;
|
---|
[2617] | 6 | import java.awt.MediaTracker;
|
---|
| 7 | import java.awt.Rectangle;
|
---|
[2593] | 8 | import java.awt.Toolkit;
|
---|
[7956] | 9 | import java.awt.geom.AffineTransform;
|
---|
[2593] | 10 | import java.awt.image.BufferedImage;
|
---|
[2606] | 11 | import java.util.ArrayList;
|
---|
[2593] | 12 | import java.util.List;
|
---|
| 13 |
|
---|
[2986] | 14 | import org.openstreetmap.josm.Main;
|
---|
[2606] | 15 | import org.openstreetmap.josm.io.CacheFiles;
|
---|
[7956] | 16 | import org.openstreetmap.josm.tools.ExifReader;
|
---|
[2593] | 17 |
|
---|
| 18 | public class ThumbsLoader implements Runnable {
|
---|
[2986] | 19 | public static final int maxSize = 120;
|
---|
| 20 | public static final int minSize = 22;
|
---|
[7935] | 21 | public volatile boolean stop = false;
|
---|
[2986] | 22 | List<ImageEntry> data;
|
---|
| 23 | GeoImageLayer layer;
|
---|
| 24 | MediaTracker tracker;
|
---|
| 25 | CacheFiles cache;
|
---|
| 26 | boolean cacheOff = Main.pref.getBoolean("geoimage.noThumbnailCache", false);
|
---|
[2617] | 27 |
|
---|
[2986] | 28 | public ThumbsLoader(GeoImageLayer layer) {
|
---|
| 29 | this.layer = layer;
|
---|
[7005] | 30 | this.data = new ArrayList<>(layer.data);
|
---|
[2986] | 31 | if (!cacheOff) {
|
---|
| 32 | cache = new CacheFiles("geoimage-thumbnails", false);
|
---|
| 33 | cache.setExpire(CacheFiles.EXPIRE_NEVER, false);
|
---|
| 34 | cache.setMaxSize(120, false);
|
---|
[2593] | 35 | }
|
---|
[2986] | 36 | }
|
---|
[2593] | 37 |
|
---|
[6084] | 38 | @Override
|
---|
[2986] | 39 | public void run() {
|
---|
[6248] | 40 | Main.debug("Load Thumbnails");
|
---|
[2986] | 41 | tracker = new MediaTracker(Main.map.mapView);
|
---|
| 42 | for (int i = 0; i < data.size(); i++) {
|
---|
| 43 | if (stop) return;
|
---|
[2617] | 44 |
|
---|
[7784] | 45 | // Do not load thumbnails that were loaded before.
|
---|
| 46 | if (data.get(i).thumbnail == null) {
|
---|
| 47 | data.get(i).thumbnail = loadThumb(data.get(i));
|
---|
[2617] | 48 |
|
---|
[7784] | 49 | if (Main.isDisplayingMapView()) {
|
---|
| 50 | layer.updateOffscreenBuffer = true;
|
---|
| 51 | Main.map.mapView.repaint();
|
---|
| 52 | }
|
---|
[2986] | 53 | }
|
---|
| 54 | }
|
---|
[7935] | 55 | layer.thumbsLoaded();
|
---|
[3385] | 56 | layer.updateOffscreenBuffer = true;
|
---|
[2986] | 57 | Main.map.mapView.repaint();
|
---|
| 58 | }
|
---|
[2617] | 59 |
|
---|
[2986] | 60 | private BufferedImage loadThumb(ImageEntry entry) {
|
---|
| 61 | final String cacheIdent = entry.getFile().toString()+":"+maxSize;
|
---|
[2617] | 62 |
|
---|
[2986] | 63 | if (!cacheOff) {
|
---|
| 64 | BufferedImage cached = cache.getImg(cacheIdent);
|
---|
[6248] | 65 | if (cached != null) {
|
---|
| 66 | Main.debug(" from cache");
|
---|
[2986] | 67 | return cached;
|
---|
[2606] | 68 | }
|
---|
[2986] | 69 | }
|
---|
[2617] | 70 |
|
---|
[2986] | 71 | Image img = Toolkit.getDefaultToolkit().createImage(entry.getFile().getPath());
|
---|
| 72 | tracker.addImage(img, 0);
|
---|
| 73 | try {
|
---|
| 74 | tracker.waitForID(0);
|
---|
| 75 | } catch (InterruptedException e) {
|
---|
[6313] | 76 | Main.error(" InterruptedException while loading thumb");
|
---|
[2986] | 77 | return null;
|
---|
| 78 | }
|
---|
| 79 | if (tracker.isErrorID(1) || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) {
|
---|
[6248] | 80 | Main.error(" Invalid image");
|
---|
[2986] | 81 | return null;
|
---|
| 82 | }
|
---|
[7956] | 83 |
|
---|
| 84 | final int w = img.getWidth(null);
|
---|
| 85 | final int h = img.getHeight(null);
|
---|
| 86 | final int hh, ww;
|
---|
| 87 | if (ExifReader.orientationSwitchesDimensions(entry.getExifOrientation())) {
|
---|
| 88 | ww = h;
|
---|
| 89 | hh = w;
|
---|
| 90 | } else {
|
---|
| 91 | ww = w;
|
---|
| 92 | hh = h;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[2986] | 95 | Rectangle targetSize = ImageDisplay.calculateDrawImageRectangle(
|
---|
[7956] | 96 | new Rectangle(0, 0, ww, hh),
|
---|
[2617] | 97 | new Rectangle(0, 0, maxSize, maxSize));
|
---|
[2986] | 98 | BufferedImage scaledBI = new BufferedImage(targetSize.width, targetSize.height, BufferedImage.TYPE_INT_RGB);
|
---|
| 99 | Graphics2D g = scaledBI.createGraphics();
|
---|
[7956] | 100 |
|
---|
| 101 | final AffineTransform restoreOrientation = ExifReader.getRestoreOrientationTransform(entry.getExifOrientation(), w, h);
|
---|
| 102 | final AffineTransform scale = AffineTransform.getScaleInstance((double) targetSize.width / ww, (double) targetSize.height / hh);
|
---|
| 103 | scale.concatenate(restoreOrientation);
|
---|
| 104 |
|
---|
| 105 | while (!g.drawImage(img, scale, null)) {
|
---|
[2986] | 106 | try {
|
---|
| 107 | Thread.sleep(10);
|
---|
[6310] | 108 | } catch(InterruptedException ie) {
|
---|
[6313] | 109 | Main.warn("InterruptedException while drawing thumb");
|
---|
[6310] | 110 | }
|
---|
[2986] | 111 | }
|
---|
| 112 | g.dispose();
|
---|
| 113 | tracker.removeImage(img);
|
---|
[2617] | 114 |
|
---|
[2986] | 115 | if (scaledBI.getWidth() <= 0 || scaledBI.getHeight() <= 0) {
|
---|
[6248] | 116 | Main.error(" Invalid image");
|
---|
[2986] | 117 | return null;
|
---|
| 118 | }
|
---|
[2617] | 119 |
|
---|
[2986] | 120 | if (!cacheOff) {
|
---|
| 121 | cache.saveImg(cacheIdent, scaledBI);
|
---|
[2606] | 122 | }
|
---|
[2593] | 123 |
|
---|
[2986] | 124 | return scaledBI;
|
---|
[2593] | 125 | }
|
---|
[2986] | 126 |
|
---|
| 127 | }
|
---|