Changeset 4271 in josm


Ignore:
Timestamp:
Jul 27, 2011 9:23:35 PM (22 months ago)
Author:
bastiK
Message:

extend image caching and add support for scaled svg images (see #6560)

Location:
trunk/src/org/openstreetmap/josm/tools
Files:
2 added
1 edited

Legend:

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

    r4261 r4271  
    1010import java.awt.Component; 
    1111import java.awt.Cursor; 
     12import java.awt.Dimension; 
    1213import java.awt.Graphics; 
    1314import java.awt.Graphics2D; 
     
    4243 
    4344/** 
    44  * Helperclass to support the application with images. 
     45 * Helper class to support the application with images. 
    4546 * @author imi 
    4647 */ 
     
    5960    public static enum ImageType { 
    6061        SVG,    // scalable vector graphics 
    61         OTHER   // everything else, e.g. png, gif 
    62                 // must be supported by Java 
    63     } 
    64  
    65     /** 
    66      * remember whether the image has been sanitized 
    67      */ 
    68     private static class ImageWrapper { 
    69         Image img; 
    70         boolean sanitized; 
    71  
    72         public ImageWrapper(Image img, boolean sanitized) { 
    73             this.img = img; 
    74             this.sanitized = sanitized; 
    75         } 
     62        OTHER   // everything else, e.g. png, gif (must be supported by Java) 
    7663    } 
    7764 
     
    7966     * The icon cache 
    8067     */ 
    81     private static Map<String, ImageWrapper> cache = new HashMap<String, ImageWrapper>(); 
     68    private static Map<String, ImageResource> cache = new HashMap<String, ImageResource>(); 
    8269 
    8370    /** 
     
    127114    } 
    128115 
     116    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, boolean sanitize) { 
     117        return getIfAvailable(dirs, id, subdir, name, archive, null, sanitize); 
     118    } 
     119     
    129120    /** 
    130121     * The full path of the image is either a url (starting with http://) 
     
    137128     *                  it will try both extensions. 
    138129     * @param archive   A zip file where the image is located (may be null). 
     130     * @param dim       The dimensions of the image if it should be scaled. null if the 
     131     *                  original size of the image should be returned. The width  
     132     *                  part of the dimension can be -1. Then it will scale the width 
     133     *                  in the same way as the height. (And the other way around.) 
    139134     * @param sanitize  If the image should be repainted to a new BufferedImage to work 
    140135     *                  around certain issues. 
    141136     */ 
    142     public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, boolean sanitize) { 
    143         ImageWrapper iw = getIfAvailableImpl(dirs, id, subdir, name, archive); 
    144         if (iw == null) 
     137    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, Dimension dim, boolean sanitize) { 
     138        ImageResource ir = getIfAvailableImpl(dirs, id, subdir, name, archive); 
     139        if (ir == null) 
    145140            return null; 
    146         if (sanitize && !iw.sanitized) { 
    147             iw.img = sanitize(iw.img); 
    148             iw.sanitized = true; 
    149         } 
    150         return new ImageIcon(iw.img); 
    151     } 
    152  
    153     private static ImageWrapper getIfAvailableImpl(Collection<String> dirs, String id, String subdir, String name, File archive) { 
     141        return ir.getImageIcon(dim == null ? ImageResource.DEFAULT_DIMENSION : dim, sanitize); 
     142    } 
     143 
     144    private static ImageResource getIfAvailableImpl(Collection<String> dirs, String id, String subdir, String name, File archive) { 
    154145        if (name == null) 
    155146            return null; 
     
    158149        if (name.startsWith("http://")) { 
    159150            String url = name; 
    160             ImageWrapper iw = cache.get(url); 
    161             if (iw != null) return iw; 
    162             iw = getIfAvailableHttp(url, type); 
    163             if (iw != null) { 
    164                 cache.put(url, iw); 
    165             } 
    166             return iw; 
     151            ImageResource ir = cache.get(url); 
     152            if (ir != null) return ir; 
     153            ir = getIfAvailableHttp(url, type); 
     154            if (ir != null) { 
     155                cache.put(url, ir); 
     156            } 
     157            return ir; 
    167158        } 
    168159 
     
    198189                } 
    199190 
    200                 ImageWrapper iw = cache.get(cache_name); 
    201                 if (iw != null) return iw; 
     191                ImageResource ir = cache.get(cache_name); 
     192                if (ir != null) return ir; 
    202193 
    203194                switch (place) { 
    204195                    case ARCHIVE: 
    205196                        if (archive != null) { 
    206                             iw = getIfAvailableZip(full_name, archive, type); 
    207                             if (iw != null) { 
    208                                 cache.put(cache_name, iw); 
    209                                 return iw; 
     197                            ir = getIfAvailableZip(full_name, archive, type); 
     198                            if (ir != null) { 
     199                                cache.put(cache_name, ir); 
     200                                return ir; 
    210201                            } 
    211202                        } 
     
    220211                        if (path == null) 
    221212                            continue; 
    222                         iw = getIfAvailableLocalURL(path, type); 
    223                         if (iw != null) { 
    224                             cache.put(cache_name, iw); 
    225                             return iw; 
     213                        ir = getIfAvailableLocalURL(path, type); 
     214                        if (ir != null) { 
     215                            cache.put(cache_name, ir); 
     216                            return ir; 
    226217                        } 
    227218                        break; 
     
    232223    } 
    233224 
    234     private static ImageWrapper getIfAvailableHttp(String url, ImageType type) { 
    235         Image img = null; 
     225    private static ImageResource getIfAvailableHttp(String url, ImageType type) { 
    236226        try { 
    237227            MirroredInputStream is = new MirroredInputStream(url, 
     
    240230                case SVG: 
    241231                    URI uri = getSvgUniverse().loadSVG(is, is.getFile().toURI().toURL().toString()); 
    242                     img = createImageFromSvgUri(uri); 
    243                     break; 
     232                    SVGDiagram svg = getSvgUniverse().getDiagram(uri); 
     233                    return svg == null ? null : new ImageResource(svg); 
    244234                case OTHER: 
    245                     img = Toolkit.getDefaultToolkit().createImage(is.getFile().toURI().toURL()); 
    246                     break; 
     235                    Image img = Toolkit.getDefaultToolkit().createImage(is.getFile().toURI().toURL()); 
     236                    return img == null ? null : new ImageResource(img, false); 
     237                default: 
     238                    throw new AssertionError(); 
    247239            } 
    248240        } catch (IOException e) { 
    249         } 
    250         return img == null ? null : new ImageWrapper(img, false); 
    251     } 
    252  
    253     private static ImageWrapper getIfAvailableZip(String full_name, File archive, ImageType type) { 
     241            return null; 
     242        } 
     243    } 
     244 
     245    private static ImageResource getIfAvailableZip(String full_name, File archive, ImageType type) { 
    254246        ZipFile zipFile = null; 
    255         Image img = null; 
    256247        try 
    257248        { 
     
    269260                        case SVG: 
    270261                            URI uri = getSvgUniverse().loadSVG(is, full_name); 
    271                             img = createImageFromSvgUri(uri); 
    272                             break; 
     262                            SVGDiagram svg = getSvgUniverse().getDiagram(uri); 
     263                            return svg == null ? null : new ImageResource(svg); 
    273264                        case OTHER: 
    274265                            while(size > 0) 
     
    278269                                size -= l; 
    279270                            } 
    280                             img = Toolkit.getDefaultToolkit().createImage(buf); 
    281                             break; 
     271                            Image img = Toolkit.getDefaultToolkit().createImage(buf); 
     272                            return img == null ? null : new ImageResource(img, false); 
     273                        default: 
     274                            throw new AssertionError(); 
    282275                    } 
    283276                } finally { 
     
    297290            } 
    298291        } 
    299         return img == null ? null : new ImageWrapper(img, false); 
    300     } 
    301  
    302     private static ImageWrapper getIfAvailableLocalURL(URL path, ImageType type) { 
    303         Image img = null; 
     292        return null; 
     293    } 
     294 
     295    private static ImageResource getIfAvailableLocalURL(URL path, ImageType type) { 
    304296        switch (type) { 
    305297            case SVG: 
    306298                URI uri = getSvgUniverse().loadSVG(path); 
    307                 img = createImageFromSvgUri(uri); 
    308                 break; 
     299                SVGDiagram svg = getSvgUniverse().getDiagram(uri); 
     300                return svg == null ? null : new ImageResource(svg); 
    309301            case OTHER: 
    310                 img = Toolkit.getDefaultToolkit().createImage(path); 
    311                 break; 
    312         } 
    313         return img == null ? null : new ImageWrapper(img, false); 
     302                Image img = Toolkit.getDefaultToolkit().createImage(path); 
     303                return img == null ? null : new ImageResource(img, false); 
     304            default: 
     305                throw new AssertionError(); 
     306        } 
    314307    } 
    315308 
     
    521514    } 
    522515 
    523     private static Image createImageFromSvgUri(URI uri) { 
    524         SVGDiagram dia = getSvgUniverse().getDiagram(uri); 
    525         int w = (int)dia.getWidth(); 
    526         int h = (int)dia.getHeight(); 
    527         Image img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 
     516    public static Image createImageFromSvg(SVGDiagram svg, Dimension dim) { 
     517        float realWidth = svg.getWidth(); 
     518        float realHeight = svg.getHeight(); 
     519        int width = Math.round(realWidth); 
     520        int height = Math.round(realHeight); 
     521        Double scaleX = null, scaleY = null; 
     522        if (dim.width != -1) { 
     523            width = dim.width; 
     524            scaleX = (double) width / realWidth; 
     525            if (dim.height == -1) { 
     526                scaleY = scaleX; 
     527                height = (int) Math.round(realHeight * scaleY); 
     528            } else { 
     529                height = dim.height; 
     530                scaleY = (double) height / realHeight; 
     531            } 
     532        } else if (dim.height != -1) { 
     533            height = dim.height; 
     534            scaleX = scaleY = (double) height / realHeight; 
     535            width = (int) Math.round(realWidth * scaleX); 
     536        } 
     537        Image img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 
    528538        Graphics2D g = ((BufferedImage) img).createGraphics(); 
    529         g.setClip(0, 0, w, h); 
     539        g.setClip(0, 0, width, height); 
     540        if (scaleX != null) { 
     541            g.scale(scaleX, scaleY); 
     542        } 
    530543        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
    531544        try { 
    532             dia.render(g); 
     545            svg.render(g); 
    533546        } catch (SVGException ex) { 
    534547            return null; 
Note: See TracChangeset for help on using the changeset viewer.