source: josm/trunk/src/org/openstreetmap/josm/tools/ImageResource.java @ 5241

Revision 4961, 4.4 KB checked in by stoecker, 3 months ago (diff)

see #1576 - switch ImageResource complete to BufferedImage, fix last accidential checkin

Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import com.kitfox.svg.SVGDiagram;
5
6import java.awt.Dimension;
7import java.awt.Image;
8import java.awt.image.BufferedImage;
9import java.util.HashMap;
10import javax.swing.ImageIcon;
11
12/**
13 * Holds data for one particular image.
14 * It can be backed by a svg or raster image.
15 *
16 * In the first case, 'svg' is not null and in the latter case, 'imgCache' has
17 * at least one entry for the key DEFAULT_DIMENSION.
18 */
19class ImageResource {
20   
21    /**
22     * Caches the image data for resized versions of the same image.
23     */
24    private HashMap<Dimension, BufferedImage> imgCache = new HashMap<Dimension, BufferedImage>();
25    private SVGDiagram svg;
26    public static final Dimension DEFAULT_DIMENSION = new Dimension(-1, -1);
27 
28    public ImageResource(BufferedImage img) {
29        CheckParameterUtil.ensureParameterNotNull(img);
30        imgCache.put(DEFAULT_DIMENSION, img);
31    }
32
33    public ImageResource(SVGDiagram svg) {
34        CheckParameterUtil.ensureParameterNotNull(svg);
35        this.svg = svg;
36    }
37
38    public ImageIcon getImageIcon() {
39        return getImageIcon(DEFAULT_DIMENSION);
40    }
41
42    /**
43     * Get an ImageIcon object for the image of this resource
44     * @param   dim The requested dimensions. Use (-1,-1) for the original size
45     *          and (width, -1) to set the width, but otherwise scale the image
46     *          proportionally.
47     */
48    public ImageIcon getImageIcon(Dimension dim) {
49        if (dim.width < -1 || dim.width == 0 || dim.height < -1 || dim.height == 0)
50            throw new IllegalArgumentException();
51        BufferedImage img = imgCache.get(dim);
52        if (img != null) {
53            return new ImageIcon(img);
54        }
55        if (svg != null) {
56            img = ImageProvider.createImageFromSvg(svg, dim);
57            imgCache.put(dim, img);
58            return new ImageIcon(img);
59        } else {
60            BufferedImage base = imgCache.get(DEFAULT_DIMENSION);
61            if (base == null) throw new AssertionError();
62           
63            int width = dim.width;
64            int height = dim.height;
65            ImageIcon icon = new ImageIcon(base);
66            if (width == -1) {
67                width = icon.getIconWidth() * height / icon.getIconHeight();
68            } else if (height == -1) {
69                height = icon.getIconHeight() * width / icon.getIconWidth();
70            }
71            Image i = icon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
72            img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
73            img.getGraphics().drawImage(i, 0, 0, null);
74            imgCache.put(dim, img);
75            return new ImageIcon(img);
76        }
77    }
78
79    /**
80     * Get image icon with a certain maximum size. The image is scaled down
81     * to fit maximum dimensions. (Keeps aspect ratio)
82     *
83     * @param maxSize The maximum size. One of the dimensions (widht or height) can be -1,
84     * which means it is not bounded.
85     */
86    public ImageIcon getImageIconBounded(Dimension maxSize) {
87        if (maxSize.width < -1 || maxSize.width == 0 || maxSize.height < -1 || maxSize.height == 0)
88            throw new IllegalArgumentException();
89        float realWidth;
90        float realHeight;
91        if (svg != null) {
92            realWidth = svg.getWidth();
93            realHeight = svg.getHeight();
94        } else {
95            BufferedImage base = imgCache.get(DEFAULT_DIMENSION);
96            if (base == null) throw new AssertionError();
97            ImageIcon icon = new ImageIcon(base);
98            realWidth = icon.getIconWidth();
99            realHeight = icon.getIconHeight();
100        }
101        int maxWidth = maxSize.width;
102        int maxHeight = maxSize.height;
103
104        if (realWidth <= maxWidth) {
105            maxWidth = -1;
106        }
107        if (realHeight <= maxHeight) {
108            maxHeight = -1;
109        }
110
111        if (maxWidth == -1 && maxHeight == -1)
112            return getImageIcon(DEFAULT_DIMENSION);
113        else if (maxWidth == -1)
114            return getImageIcon(new Dimension(-1, maxHeight));
115        else if (maxHeight == -1)
116            return getImageIcon(new Dimension(maxWidth, -1));
117        else
118            if (realWidth / maxWidth > realHeight / maxHeight)
119                return getImageIcon(new Dimension(maxWidth, -1));
120            else
121                return getImageIcon(new Dimension(-1, maxHeight));
122   }
123}
Note: See TracBrowser for help on using the repository browser.