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

Last change on this file since 12711 was 12682, checked in by Don-vip, 7 years ago

see #15182 - move GuiSizesHelper from gui.util to tools (only called from there)

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