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

Last change on this file since 2662 was 2662, checked in by bastiK, 15 years ago

geoimage: reworked image correlation dialog. Might still have some quirks here and there. New: displays and updates the number of matched images in the status bar while you type.

  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1// License: GPL. See LICENSE file for details.
2
3package org.openstreetmap.josm.gui.layer.geoimage;
4
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.Graphics2D;
8import java.awt.Image;
9import java.awt.MediaTracker;
10import java.awt.Rectangle;
11import java.awt.Toolkit;
12import java.awt.image.BufferedImage;
13import java.io.File;
14import java.util.ArrayList;
15import java.util.List;
16
17import org.openstreetmap.josm.io.CacheFiles;
18import org.openstreetmap.josm.Main;
19
20public class ThumbsLoader implements Runnable {
21 public static final int maxSize = 120;
22 public static final int minSize = 22;
23 volatile boolean stop = false;
24 List<ImageEntry> data;
25 GeoImageLayer layer;
26 MediaTracker tracker;
27 CacheFiles cache;
28 boolean cacheOff = Main.pref.getBoolean("geoimage.noThumbnailCache", false);
29
30 public ThumbsLoader(GeoImageLayer layer) {
31 this.layer = layer;
32 this.data = new ArrayList<ImageEntry>(layer.data);
33 if (!cacheOff) {
34 cache = new CacheFiles("geoimage-thumbnails", false);
35 cache.setExpire(CacheFiles.EXPIRE_NEVER, false);
36 cache.setMaxSize(120, false);
37 }
38 }
39
40 public void run() {
41 System.err.println("Load Thumbnails");
42 tracker = new MediaTracker(Main.map.mapView);
43 for (int i = 0; i < data.size(); i++) {
44 if (stop) return;
45
46 System.err.print("fetching image "+i);
47
48 data.get(i).thumbnail = loadThumb(data.get(i));
49
50 if (Main.map != null && Main.map.mapView != null) {
51 try {
52 layer.updateOffscreenBuffer = true;
53 } catch (Exception e) {}
54 Main.map.mapView.repaint();
55 }
56 }
57 try {
58 layer.updateOffscreenBuffer = true;
59 } catch (Exception e) {}
60 Main.map.mapView.repaint();
61 (new Thread() { // clean up the garbage - shouldn't hurt
62 public void run() {
63 try {
64 Thread.sleep(200);
65 }
66 catch (InterruptedException ie) {}
67 System.gc();
68 }
69 }).start();
70
71 }
72
73 private BufferedImage loadThumb(ImageEntry entry) {
74 final String cacheIdent = entry.file.toString()+":"+maxSize;
75
76 if (!cacheOff) {
77 BufferedImage cached = cache.getImg(cacheIdent);
78 if(cached != null) {
79 System.err.println(" from cache");
80 return cached;
81 }
82 }
83
84 Image img = Toolkit.getDefaultToolkit().createImage(entry.file.getPath());
85 tracker.addImage(img, 0);
86 try {
87 tracker.waitForID(0);
88 } catch (InterruptedException e) {
89 System.err.println(" InterruptedException");
90 return null;
91 }
92 if (tracker.isErrorID(1) || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) {
93 System.err.println(" Invalid image");
94 return null;
95 }
96 Rectangle targetSize = ImageDisplay.calculateDrawImageRectangle(
97 new Rectangle(0, 0, img.getWidth(null), img.getHeight(null)),
98 new Rectangle(0, 0, maxSize, maxSize));
99 BufferedImage scaledBI = new BufferedImage(targetSize.width, targetSize.height, BufferedImage.TYPE_INT_RGB);
100 Graphics2D g = scaledBI.createGraphics();
101 while (!g.drawImage(img, 0, 0, targetSize.width, targetSize.height, null))
102 {
103 try {
104 Thread.sleep(10);
105 } catch(InterruptedException ie) {}
106 }
107 g.dispose();
108 tracker.removeImage(img);
109
110 if (scaledBI == null || scaledBI.getWidth() <= 0 || scaledBI.getHeight() <= 0) {
111 System.err.println(" Invalid image");
112 return null;
113 }
114
115 if (!cacheOff) {
116 cache.saveImg(cacheIdent, scaledBI);
117 }
118
119 System.err.println("");
120 return scaledBI;
121 }
122
123 }
Note: See TracBrowser for help on using the repository browser.