Changeset 17873 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2021-05-06T21:47:04+02:00 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/layer/geoimage/ThumbsLoader.java
r17856 r17873 2 2 package org.openstreetmap.josm.gui.layer.geoimage; 3 3 4 import java.awt.Graphics2D; 5 import java.awt.Image; 6 import java.awt.MediaTracker; 7 import java.awt.Rectangle; 8 import java.awt.Toolkit; 9 import java.awt.geom.AffineTransform; 4 import java.awt.Dimension; 10 5 import java.awt.image.BufferedImage; 11 6 import java.io.File; … … 20 15 import org.openstreetmap.josm.data.cache.JCSCacheManager; 21 16 import org.openstreetmap.josm.gui.MainApplication; 22 import org.openstreetmap.josm.gui.layer.geoimage.ImageDisplay.VisRect;23 17 import org.openstreetmap.josm.spi.preferences.Config; 24 import org.openstreetmap.josm.tools.ExifReader;25 18 import org.openstreetmap.josm.tools.Logging; 19 import org.openstreetmap.josm.tools.Stopwatch; 26 20 27 21 /** … … 36 30 private final Collection<ImageEntry> data; 37 31 private final GeoImageLayer layer; 38 private MediaTracker tracker;39 32 private ICacheAccess<String, BufferedImageCacheEntry> cache; 40 33 private final boolean cacheOff = Config.getPref().getBoolean("geoimage.noThumbnailCache", false); … … 74 67 @Override 75 68 public void run() { 76 Logging.debug("Load Thumbnails"); 77 tracker = new MediaTracker(MainApplication.isDisplayingMapView() ? MainApplication.getMap().mapView : MainApplication.getMainPanel()); 69 int count = 0; 70 Stopwatch stopwatch = Stopwatch.createStarted(); 71 Logging.debug("Loading {0} thumbnails", data.size()); 78 72 for (ImageEntry entry : data) { 79 73 if (stop) return; … … 87 81 } 88 82 } 83 count++; 89 84 } 85 Logging.debug("Loaded {0} thumbnails in {1}", count, stopwatch); 90 86 if (layer != null) { 91 87 layer.thumbsLoaded(); … … 101 97 BufferedImageCacheEntry cacheEntry = cache.get(cacheIdent); 102 98 if (cacheEntry != null && cacheEntry.getImage() != null) { 103 Logging.debug(" from cache");99 Logging.debug("{0} from cache", cacheIdent); 104 100 return cacheEntry.getImage(); 105 101 } … … 109 105 } 110 106 111 Image img = Toolkit.getDefaultToolkit().createImage(entry.getFile().getPath()); 112 tracker.addImage(img, 0); 107 BufferedImage img; 113 108 try { 114 tracker.waitForID(0); 115 } catch (InterruptedException e) { 116 Logging.error(" InterruptedException while loading thumb"); 117 Thread.currentThread().interrupt(); 118 return null; 119 } 120 if (tracker.isErrorID(1) || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) { 121 Logging.error(" Invalid image"); 109 img = entry.read(new Dimension(maxSize, maxSize)); 110 } catch (IOException e) { 111 Logging.warn("Failed to load geoimage thumb"); 112 Logging.warn(e); 122 113 return null; 123 114 } 124 115 125 final int w = img.getWidth(null); 126 final int h = img.getHeight(null); 127 final int hh, ww; 128 final Integer exifOrientation = entry.getExifOrientation(); 129 if (exifOrientation != null && ExifReader.orientationSwitchesDimensions(exifOrientation)) { 130 ww = h; 131 hh = w; 132 } else { 133 ww = w; 134 hh = h; 135 } 136 137 Rectangle targetSize = ImageDisplay.calculateDrawImageRectangle( 138 new VisRect(0, 0, ww, hh), 139 new Rectangle(0, 0, maxSize, maxSize)); 140 BufferedImage scaledBI = new BufferedImage(targetSize.width, targetSize.height, BufferedImage.TYPE_INT_RGB); 141 Graphics2D g = scaledBI.createGraphics(); 142 143 final AffineTransform scale = AffineTransform.getScaleInstance((double) targetSize.width / ww, (double) targetSize.height / hh); 144 if (exifOrientation != null) { 145 final AffineTransform restoreOrientation = ExifReader.getRestoreOrientationTransform(exifOrientation, w, h); 146 scale.concatenate(restoreOrientation); 147 } 148 149 while (!g.drawImage(img, scale, null)) { 150 try { 151 Thread.sleep(10); 152 } catch (InterruptedException e) { 153 Logging.warn("InterruptedException while drawing thumb"); 154 Thread.currentThread().interrupt(); 155 } 156 } 157 g.dispose(); 158 tracker.removeImage(img); 159 160 if (scaledBI.getWidth() <= 0 || scaledBI.getHeight() <= 0) { 116 if (img.getWidth() <= 0 || img.getHeight() <= 0) { 161 117 Logging.error(" Invalid image"); 162 118 return null; … … 165 121 if (!cacheOff && cache != null) { 166 122 try { 167 cache.put(cacheIdent, BufferedImageCacheEntry.pngEncoded( scaledBI));123 cache.put(cacheIdent, BufferedImageCacheEntry.pngEncoded(img)); 168 124 } catch (UncheckedIOException e) { 169 125 Logging.warn("Failed to save geoimage thumb to cache"); … … 172 128 } 173 129 174 return scaledBI;130 return img; 175 131 } 176 132 }
Note:
See TracChangeset
for help on using the changeset viewer.