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

Last change on this file since 10356 was 10356, checked in by stoecker, 8 years ago

see #9995, see #10684 - remove more hardcoded places of images

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