1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.awt.Dimension;
|
---|
5 | import java.awt.Image;
|
---|
6 | import java.awt.image.BufferedImage;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import java.util.Map;
|
---|
9 |
|
---|
10 | import javax.swing.AbstractAction;
|
---|
11 | import javax.swing.Action;
|
---|
12 | import javax.swing.ImageIcon;
|
---|
13 |
|
---|
14 | import com.kitfox.svg.SVGDiagram;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Holds data for one particular image.
|
---|
18 | * It can be backed by a svg or raster image.
|
---|
19 | *
|
---|
20 | * In the first case, 'svg' is not null and in the latter case, 'imgCache' has
|
---|
21 | * at least one entry for the key DEFAULT_DIMENSION.
|
---|
22 | * @since 4271
|
---|
23 | */
|
---|
24 | public class ImageResource {
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Caches the image data for resized versions of the same image.
|
---|
28 | */
|
---|
29 | private Map<Dimension, Image> imgCache = new HashMap<>();
|
---|
30 | private SVGDiagram svg;
|
---|
31 | /**
|
---|
32 | * Use this dimension to request original file dimension.
|
---|
33 | */
|
---|
34 | public static final Dimension DEFAULT_DIMENSION = new Dimension(-1, -1);
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Constructs a new {@code ImageResource} from an image.
|
---|
38 | * @param img the image
|
---|
39 | */
|
---|
40 | public ImageResource(Image img) {
|
---|
41 | CheckParameterUtil.ensureParameterNotNull(img);
|
---|
42 | imgCache.put(DEFAULT_DIMENSION, img);
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Constructs a new {@code ImageResource} from SVG data.
|
---|
47 | * @param svg SVG data
|
---|
48 | */
|
---|
49 | public ImageResource(SVGDiagram svg) {
|
---|
50 | CheckParameterUtil.ensureParameterNotNull(svg);
|
---|
51 | this.svg = svg;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Returns the image icon at default dimension.
|
---|
56 | * @return the image icon at default dimension
|
---|
57 | */
|
---|
58 | public ImageIcon getImageIcon() {
|
---|
59 | return getImageIcon(DEFAULT_DIMENSION);
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Set both icons of an Action
|
---|
64 | * @param a The action for the icons
|
---|
65 | * @since 7693
|
---|
66 | */
|
---|
67 | public void getImageIcon(AbstractAction a) {
|
---|
68 | ImageIcon icon = getImageIconBounded(ImageProvider.getImageSizes(ImageProvider.ImageSizes.SMALLICON));
|
---|
69 | a.putValue(Action.SMALL_ICON, icon);
|
---|
70 | icon = getImageIconBounded(ImageProvider.getImageSizes(ImageProvider.ImageSizes.LARGEICON));
|
---|
71 | a.putValue(Action.LARGE_ICON_KEY, icon);
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Get an ImageIcon object for the image of this resource
|
---|
76 | * @param dim The requested dimensions. Use (-1,-1) for the original size
|
---|
77 | * and (width, -1) to set the width, but otherwise scale the image
|
---|
78 | * proportionally.
|
---|
79 | * @return ImageIcon object for the image of this resource, scaled according to dim
|
---|
80 | */
|
---|
81 | public ImageIcon getImageIcon(Dimension dim) {
|
---|
82 | if (dim.width < -1 || dim.width == 0 || dim.height < -1 || dim.height == 0)
|
---|
83 | throw new IllegalArgumentException();
|
---|
84 | Image img = imgCache.get(dim);
|
---|
85 | if (img != null) {
|
---|
86 | return new ImageIcon(img);
|
---|
87 | }
|
---|
88 | if (svg != null) {
|
---|
89 | img = ImageProvider.createImageFromSvg(svg, dim);
|
---|
90 | if (img == null) {
|
---|
91 | return null;
|
---|
92 | }
|
---|
93 | imgCache.put(dim, img);
|
---|
94 | return new ImageIcon(img);
|
---|
95 | } else {
|
---|
96 | Image base = imgCache.get(DEFAULT_DIMENSION);
|
---|
97 | if (base == null) throw new AssertionError();
|
---|
98 |
|
---|
99 | int width = dim.width;
|
---|
100 | int height = dim.height;
|
---|
101 | ImageIcon icon = new ImageIcon(base);
|
---|
102 | if (width == -1) {
|
---|
103 | width = Math.max(1, icon.getIconWidth() * height / icon.getIconHeight());
|
---|
104 | } else if (height == -1) {
|
---|
105 | height = Math.max(1, icon.getIconHeight() * width / icon.getIconWidth());
|
---|
106 | }
|
---|
107 | Image i = icon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
|
---|
108 | img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
---|
109 | img.getGraphics().drawImage(i, 0, 0, null);
|
---|
110 | imgCache.put(dim, img);
|
---|
111 | return new ImageIcon(img);
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Get image icon with a certain maximum size. The image is scaled down
|
---|
117 | * to fit maximum dimensions. (Keeps aspect ratio)
|
---|
118 | *
|
---|
119 | * @param maxSize The maximum size. One of the dimensions (widht or height) can be -1,
|
---|
120 | * which means it is not bounded.
|
---|
121 | * @return ImageIcon object for the image of this resource, scaled down if needed, according to maxSize
|
---|
122 | */
|
---|
123 | public ImageIcon getImageIconBounded(Dimension maxSize) {
|
---|
124 | if (maxSize.width < -1 || maxSize.width == 0 || maxSize.height < -1 || maxSize.height == 0)
|
---|
125 | throw new IllegalArgumentException();
|
---|
126 | float realWidth;
|
---|
127 | float realHeight;
|
---|
128 | if (svg != null) {
|
---|
129 | realWidth = svg.getWidth();
|
---|
130 | realHeight = svg.getHeight();
|
---|
131 | } else {
|
---|
132 | Image base = imgCache.get(DEFAULT_DIMENSION);
|
---|
133 | if (base == null) throw new AssertionError();
|
---|
134 | ImageIcon icon = new ImageIcon(base);
|
---|
135 | realWidth = icon.getIconWidth();
|
---|
136 | realHeight = icon.getIconHeight();
|
---|
137 | }
|
---|
138 | int maxWidth = maxSize.width;
|
---|
139 | int maxHeight = maxSize.height;
|
---|
140 |
|
---|
141 | if (realWidth <= maxWidth) {
|
---|
142 | maxWidth = -1;
|
---|
143 | }
|
---|
144 | if (realHeight <= maxHeight) {
|
---|
145 | maxHeight = -1;
|
---|
146 | }
|
---|
147 |
|
---|
148 | if (maxWidth == -1 && maxHeight == -1)
|
---|
149 | return getImageIcon(DEFAULT_DIMENSION);
|
---|
150 | else if (maxWidth == -1)
|
---|
151 | return getImageIcon(new Dimension(-1, maxHeight));
|
---|
152 | else if (maxHeight == -1)
|
---|
153 | return getImageIcon(new Dimension(maxWidth, -1));
|
---|
154 | else
|
---|
155 | if (realWidth / maxWidth > realHeight / maxHeight)
|
---|
156 | return getImageIcon(new Dimension(maxWidth, -1));
|
---|
157 | else
|
---|
158 | return getImageIcon(new Dimension(-1, maxHeight));
|
---|
159 | }
|
---|
160 | }
|
---|