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.List;
|
---|
9 | import java.util.Map;
|
---|
10 |
|
---|
11 | import javax.swing.AbstractAction;
|
---|
12 | import javax.swing.Action;
|
---|
13 | import javax.swing.Icon;
|
---|
14 | import javax.swing.ImageIcon;
|
---|
15 | import javax.swing.JPanel;
|
---|
16 | import javax.swing.UIManager;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.gui.util.GuiSizesHelper;
|
---|
19 |
|
---|
20 | import com.kitfox.svg.SVGDiagram;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Holds data for one particular image.
|
---|
24 | * It can be backed by a svg or raster image.
|
---|
25 | *
|
---|
26 | * In the first case, <code>svg</code> is not <code>null</code> and in the latter case,
|
---|
27 | * <code>baseImage</code> is not <code>null</code>.
|
---|
28 | * @since 4271
|
---|
29 | */
|
---|
30 | public class ImageResource {
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Caches the image data for resized versions of the same image.
|
---|
34 | */
|
---|
35 | private final Map<Dimension, Image> imgCache = new HashMap<>();
|
---|
36 | /**
|
---|
37 | * SVG diagram information in case of SVG vector image.
|
---|
38 | */
|
---|
39 | private SVGDiagram svg;
|
---|
40 | /**
|
---|
41 | * Use this dimension to request original file dimension.
|
---|
42 | */
|
---|
43 | public static final Dimension DEFAULT_DIMENSION = new Dimension(-1, -1);
|
---|
44 | /**
|
---|
45 | * ordered list of overlay images
|
---|
46 | */
|
---|
47 | protected List<ImageOverlay> overlayInfo;
|
---|
48 | /**
|
---|
49 | * <code>true</code> if icon must be grayed out
|
---|
50 | */
|
---|
51 | protected boolean isDisabled = false;
|
---|
52 | /**
|
---|
53 | * The base raster image for the final output
|
---|
54 | */
|
---|
55 | private Image baseImage;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Constructs a new {@code ImageResource} from an image.
|
---|
59 | * @param img the image
|
---|
60 | */
|
---|
61 | public ImageResource(Image img) {
|
---|
62 | CheckParameterUtil.ensureParameterNotNull(img);
|
---|
63 | baseImage = scaleBaseImageIfNeeded(img);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /** Scale image according to screen DPI if needed.
|
---|
67 | *
|
---|
68 | * @param img an image loaded from file (it's width and height are virtual pixels)
|
---|
69 | * @return original img if virtual size is the same as real size or new image resized to real pixels
|
---|
70 | */
|
---|
71 | private static Image scaleBaseImageIfNeeded(Image img) {
|
---|
72 | int imgWidth = img.getWidth(null);
|
---|
73 | int imgHeight = img.getHeight(null);
|
---|
74 | int realWidth = GuiSizesHelper.getSizeDpiAdjusted(imgWidth);
|
---|
75 | int realHeight = GuiSizesHelper.getSizeDpiAdjusted(imgHeight);
|
---|
76 | if (realWidth != -1 && realHeight != -1 && imgWidth != realWidth && imgHeight != realHeight) {
|
---|
77 | Image realImage = img.getScaledInstance(realWidth, realHeight, Image.SCALE_SMOOTH);
|
---|
78 | BufferedImage bimg = new BufferedImage(realWidth, realHeight, BufferedImage.TYPE_INT_ARGB);
|
---|
79 | bimg.getGraphics().drawImage(realImage, 0, 0, null);
|
---|
80 | return bimg;
|
---|
81 | }
|
---|
82 | return img;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Constructs a new {@code ImageResource} from SVG data.
|
---|
87 | * @param svg SVG data
|
---|
88 | */
|
---|
89 | public ImageResource(SVGDiagram svg) {
|
---|
90 | CheckParameterUtil.ensureParameterNotNull(svg);
|
---|
91 | this.svg = svg;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Constructs a new {@code ImageResource} from another one and sets overlays.
|
---|
96 | * @param res the existing resource
|
---|
97 | * @param overlayInfo the overlay to apply
|
---|
98 | * @since 8095
|
---|
99 | */
|
---|
100 | public ImageResource(ImageResource res, List<ImageOverlay> overlayInfo) {
|
---|
101 | this.svg = res.svg;
|
---|
102 | this.baseImage = res.baseImage;
|
---|
103 | this.overlayInfo = overlayInfo;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Set, if image must be filtered to grayscale so it will look like disabled icon.
|
---|
108 | *
|
---|
109 | * @param disabled true, if image must be grayed out for disabled state
|
---|
110 | * @return the current object, for convenience
|
---|
111 | * @since 10428
|
---|
112 | */
|
---|
113 | public ImageResource setDisabled(boolean disabled) {
|
---|
114 | this.isDisabled = disabled;
|
---|
115 | return this;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Set both icons of an Action
|
---|
120 | * @param a The action for the icons
|
---|
121 | * @since 10369
|
---|
122 | */
|
---|
123 | public void attachImageIcon(AbstractAction a) {
|
---|
124 | Dimension iconDimension = ImageProvider.ImageSizes.SMALLICON.getImageDimension();
|
---|
125 | ImageIcon icon = getImageIconBounded(iconDimension);
|
---|
126 | a.putValue(Action.SMALL_ICON, icon);
|
---|
127 |
|
---|
128 | iconDimension = ImageProvider.ImageSizes.LARGEICON.getImageDimension();
|
---|
129 | icon = getImageIconBounded(iconDimension);
|
---|
130 | a.putValue(Action.LARGE_ICON_KEY, icon);
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Set both icons of an Action
|
---|
135 | * @param a The action for the icons
|
---|
136 | * @param addresource Adds an resource named "ImageResource" if <code>true</code>
|
---|
137 | * @since 10369
|
---|
138 | */
|
---|
139 | public void attachImageIcon(AbstractAction a, boolean addresource) {
|
---|
140 | attachImageIcon(a);
|
---|
141 | if (addresource) {
|
---|
142 | a.putValue("ImageResource", this);
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Returns the image icon at default dimension.
|
---|
148 | * @return the image icon at default dimension
|
---|
149 | */
|
---|
150 | public ImageIcon getImageIcon() {
|
---|
151 | return getImageIcon(DEFAULT_DIMENSION);
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Get an ImageIcon object for the image of this resource
|
---|
156 | * @param dim The requested dimensions. Use (-1,-1) for the original size and (width, -1)
|
---|
157 | * to set the width, but otherwise scale the image proportionally.
|
---|
158 | * @return ImageIcon object for the image of this resource, scaled according to dim
|
---|
159 | */
|
---|
160 | public ImageIcon getImageIcon(Dimension dim) {
|
---|
161 | if (dim.width < -1 || dim.width == 0 || dim.height < -1 || dim.height == 0)
|
---|
162 | throw new IllegalArgumentException(dim+" is invalid");
|
---|
163 | Image img = imgCache.get(dim);
|
---|
164 | if (img != null) {
|
---|
165 | return new ImageIcon(img);
|
---|
166 | }
|
---|
167 | BufferedImage bimg;
|
---|
168 | if (svg != null) {
|
---|
169 | Dimension realDim = GuiSizesHelper.getDimensionDpiAdjusted(dim);
|
---|
170 | bimg = ImageProvider.createImageFromSvg(svg, realDim);
|
---|
171 | if (bimg == null) {
|
---|
172 | return null;
|
---|
173 | }
|
---|
174 | } else {
|
---|
175 | if (baseImage == null) throw new AssertionError();
|
---|
176 |
|
---|
177 | int realWidth = GuiSizesHelper.getSizeDpiAdjusted(dim.width);
|
---|
178 | int realHeight = GuiSizesHelper.getSizeDpiAdjusted(dim.height);
|
---|
179 | ImageIcon icon = new ImageIcon(baseImage);
|
---|
180 | if (realWidth == -1 && realHeight == -1) {
|
---|
181 | realWidth = GuiSizesHelper.getSizeDpiAdjusted(icon.getIconWidth());
|
---|
182 | realHeight = GuiSizesHelper.getSizeDpiAdjusted(icon.getIconHeight());
|
---|
183 | } else if (realWidth == -1) {
|
---|
184 | realWidth = Math.max(1, icon.getIconWidth() * realHeight / icon.getIconHeight());
|
---|
185 | } else if (realHeight == -1) {
|
---|
186 | realHeight = Math.max(1, icon.getIconHeight() * realWidth / icon.getIconWidth());
|
---|
187 | }
|
---|
188 | Image i = icon.getImage().getScaledInstance(realWidth, realHeight, Image.SCALE_SMOOTH);
|
---|
189 | bimg = new BufferedImage(realWidth, realHeight, BufferedImage.TYPE_INT_ARGB);
|
---|
190 | bimg.getGraphics().drawImage(i, 0, 0, null);
|
---|
191 | }
|
---|
192 | if (overlayInfo != null) {
|
---|
193 | for (ImageOverlay o : overlayInfo) {
|
---|
194 | o.process(bimg);
|
---|
195 | }
|
---|
196 | }
|
---|
197 | if (isDisabled) {
|
---|
198 | //Use default Swing functionality to make icon look disabled by applying grayscaling filter.
|
---|
199 | Icon disabledIcon = UIManager.getLookAndFeel().getDisabledIcon(null, new ImageIcon(bimg));
|
---|
200 | if (disabledIcon == null) {
|
---|
201 | return null;
|
---|
202 | }
|
---|
203 |
|
---|
204 | //Convert Icon to ImageIcon with BufferedImage inside
|
---|
205 | bimg = new BufferedImage(bimg.getWidth(), bimg.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
|
---|
206 | disabledIcon.paintIcon(new JPanel(), bimg.getGraphics(), 0, 0);
|
---|
207 | }
|
---|
208 | imgCache.put(dim, bimg);
|
---|
209 | return new ImageIcon(bimg);
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Get image icon with a certain maximum size. The image is scaled down
|
---|
214 | * to fit maximum dimensions. (Keeps aspect ratio)
|
---|
215 | *
|
---|
216 | * @param maxSize The maximum size. One of the dimensions (width or height) can be -1,
|
---|
217 | * which means it is not bounded.
|
---|
218 | * @return ImageIcon object for the image of this resource, scaled down if needed, according to maxSize
|
---|
219 | */
|
---|
220 | public ImageIcon getImageIconBounded(Dimension maxSize) {
|
---|
221 | if (maxSize.width < -1 || maxSize.width == 0 || maxSize.height < -1 || maxSize.height == 0)
|
---|
222 | throw new IllegalArgumentException(maxSize+" is invalid");
|
---|
223 | float sourceWidth;
|
---|
224 | float sourceHeight;
|
---|
225 | int maxWidth = maxSize.width;
|
---|
226 | int maxHeight = maxSize.height;
|
---|
227 | if (svg != null) {
|
---|
228 | sourceWidth = svg.getWidth();
|
---|
229 | sourceHeight = svg.getHeight();
|
---|
230 | } else {
|
---|
231 | if (baseImage == null) throw new AssertionError();
|
---|
232 | ImageIcon icon = new ImageIcon(baseImage);
|
---|
233 | sourceWidth = icon.getIconWidth();
|
---|
234 | sourceHeight = icon.getIconHeight();
|
---|
235 | if (sourceWidth <= maxWidth) {
|
---|
236 | maxWidth = -1;
|
---|
237 | }
|
---|
238 | if (sourceHeight <= maxHeight) {
|
---|
239 | maxHeight = -1;
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | if (maxWidth == -1 && maxHeight == -1)
|
---|
244 | return getImageIcon(DEFAULT_DIMENSION);
|
---|
245 | else if (maxWidth == -1)
|
---|
246 | return getImageIcon(new Dimension(-1, maxHeight));
|
---|
247 | else if (maxHeight == -1)
|
---|
248 | return getImageIcon(new Dimension(maxWidth, -1));
|
---|
249 | else if (sourceWidth / maxWidth > sourceHeight / maxHeight)
|
---|
250 | return getImageIcon(new Dimension(maxWidth, -1));
|
---|
251 | else
|
---|
252 | return getImageIcon(new Dimension(-1, maxHeight));
|
---|
253 | }
|
---|
254 | }
|
---|