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