Changeset 17873 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2021-05-06T21:47:04+02:00 (4 years ago)
Author:
simon04
Message:

see #20813 - Modernize ThumbsLoader using ImageIO and subsampling

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/layer/geoimage/ThumbsLoader.java

    r17856 r17873  
    22package org.openstreetmap.josm.gui.layer.geoimage;
    33
    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;
     4import java.awt.Dimension;
    105import java.awt.image.BufferedImage;
    116import java.io.File;
     
    2015import org.openstreetmap.josm.data.cache.JCSCacheManager;
    2116import org.openstreetmap.josm.gui.MainApplication;
    22 import org.openstreetmap.josm.gui.layer.geoimage.ImageDisplay.VisRect;
    2317import org.openstreetmap.josm.spi.preferences.Config;
    24 import org.openstreetmap.josm.tools.ExifReader;
    2518import org.openstreetmap.josm.tools.Logging;
     19import org.openstreetmap.josm.tools.Stopwatch;
    2620
    2721/**
     
    3630    private final Collection<ImageEntry> data;
    3731    private final GeoImageLayer layer;
    38     private MediaTracker tracker;
    3932    private ICacheAccess<String, BufferedImageCacheEntry> cache;
    4033    private final boolean cacheOff = Config.getPref().getBoolean("geoimage.noThumbnailCache", false);
     
    7467    @Override
    7568    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());
    7872        for (ImageEntry entry : data) {
    7973            if (stop) return;
     
    8781                }
    8882            }
     83            count++;
    8984        }
     85        Logging.debug("Loaded {0} thumbnails in {1}", count, stopwatch);
    9086        if (layer != null) {
    9187            layer.thumbsLoaded();
     
    10197                BufferedImageCacheEntry cacheEntry = cache.get(cacheIdent);
    10298                if (cacheEntry != null && cacheEntry.getImage() != null) {
    103                     Logging.debug(" from cache");
     99                    Logging.debug("{0} from cache", cacheIdent);
    104100                    return cacheEntry.getImage();
    105101                }
     
    109105        }
    110106
    111         Image img = Toolkit.getDefaultToolkit().createImage(entry.getFile().getPath());
    112         tracker.addImage(img, 0);
     107        BufferedImage img;
    113108        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);
    122113            return null;
    123114        }
    124115
    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) {
    161117            Logging.error(" Invalid image");
    162118            return null;
     
    165121        if (!cacheOff && cache != null) {
    166122            try {
    167                 cache.put(cacheIdent, BufferedImageCacheEntry.pngEncoded(scaledBI));
     123                cache.put(cacheIdent, BufferedImageCacheEntry.pngEncoded(img));
    168124            } catch (UncheckedIOException e) {
    169125                Logging.warn("Failed to save geoimage thumb to cache");
     
    172128        }
    173129
    174         return scaledBI;
     130        return img;
    175131    }
    176132}
Note: See TracChangeset for help on using the changeset viewer.