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

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