Ignore:
Timestamp:
2011-02-06T15:48:58+01:00 (14 years ago)
Author:
bastiK
Message:

mapcss: fill-image

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r3512 r3862  
    5252
    5353    /**
     54     * remember whether the image has been sanitized
     55     */
     56    private static class ImageWrapper {
     57        Image img;
     58        boolean sanitized;
     59
     60        public ImageWrapper(Image img, boolean sanitized) {
     61            this.img = img;
     62            this.sanitized = sanitized;
     63        }
     64    }
     65
     66    /**
    5467     * The icon cache
    5568     */
    56     private static Map<String, Image> cache = new HashMap<String, Image>();
     69    private static Map<String, ImageWrapper> cache = new HashMap<String, ImageWrapper>();
    5770
    5871    /**
     
    8497    }
    8598
    86     public static final ImageIcon getIfAvailable(String[] dirs, String id, String subdir, String name) {
     99    public static ImageIcon getIfAvailable(String[] dirs, String id, String subdir, String name) {
    87100        return getIfAvailable(Arrays.asList(dirs), id, subdir, name);
    88101    }
     
    95108    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name) {
    96109        return getIfAvailable(dirs, id, subdir, name, null);
     110    }
     111
     112    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive) {
     113        return getIfAvailable(dirs, id, subdir, name, archive, false);
    97114    }
    98115
     
    106123     * @param name      The name of the image. If it contains no '.', a png extension is added.
    107124     * @param archive   A zip file where the image is located.
    108      */
    109     public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive) {
     125     * @param sanitize  If the image should be repainted to a new BufferedImage to work
     126     *                  around certain issues.
     127     */
     128    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, boolean sanitize) {
     129        ImageWrapper iw = getIfAvailableImpl(dirs, id, subdir, name, archive);
     130        if (iw == null)
     131            return null;
     132        if (sanitize && !iw.sanitized) {
     133            iw.img = sanitize(iw.img);
     134            iw.sanitized = true;
     135        }
     136        return new ImageIcon(iw.img);
     137    }
     138
     139    private static ImageWrapper getIfAvailableImpl(Collection<String> dirs, String id, String subdir, String name, File archive) {
    110140        if (name == null)
    111141            return null;
    112142        if (name.startsWith("http://")) {
    113             Image img = cache.get(name);
    114             if (img == null) {
     143            ImageWrapper iw = cache.get(name);
     144            if (iw == null) {
    115145                try {
    116146                    MirroredInputStream is = new MirroredInputStream(name, new File(Main.pref.getPreferencesDir(),
    117147                    "images").toString());
    118                     img = Toolkit.getDefaultToolkit().createImage(is.getFile().toURI().toURL());
    119                     cache.put(name, img);
     148                    Image img = Toolkit.getDefaultToolkit().createImage(is.getFile().toURI().toURL());
     149                    iw = new ImageWrapper(img, false);
     150                    cache.put(name, iw);
    120151                } catch (IOException e) {
    121152                }
    122153            }
    123             return img == null ? null : new ImageIcon(img);
     154            return iw;
    124155        }
    125156        if (subdir == null) {
     
    139170        }
    140171
    141         Image img = cache.get(cache_name);
    142         if (img == null) {
     172        ImageWrapper iw = cache.get(cache_name);
     173        if (iw == null) {
    143174            if(archive != null)
    144175            {
     
    162193                                size -= l;
    163194                            }
    164                             img = Toolkit.getDefaultToolkit().createImage(buf);
     195                            Image img = Toolkit.getDefaultToolkit().createImage(buf);
     196                            iw = new ImageWrapper(img, false);
    165197                        } finally {
    166198                            if (is != null) {
     
    185217            // and don't bother to create a URL unless we're actually
    186218            // creating the image.
    187             if(img == null)
     219            if(iw == null)
    188220            {
    189221                URL path = getImageUrl(full_name, dirs);
    190222                if (path == null)
    191223                    return null;
    192                 img = Toolkit.getDefaultToolkit().createImage(path);
    193             }
    194             cache.put(cache_name, img);
    195         }
    196 
    197         return new ImageIcon(img);
     224                Image img = Toolkit.getDefaultToolkit().createImage(path);
     225                iw = new ImageWrapper(img, false);
     226            }
     227            cache.put(cache_name, iw);
     228        }
     229
     230        return iw;
    198231    }
    199232
     
    405438        return get("data", type.getAPIName());
    406439    }
     440
     441    public static BufferedImage sanitize(Image img) {
     442        (new ImageIcon(img)).getImage(); // load competely
     443        int width = img.getWidth(null);
     444        int height = img.getHeight(null);
     445        BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
     446        result.getGraphics().drawImage(img, 0, 0, null);
     447        return result;
     448    }
    407449}
Note: See TracChangeset for help on using the changeset viewer.