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

Last change on this file since 8734 was 8723, checked in by simon04, 9 years ago

see #11713 - use new interface ImageProcessor where applicable

  • Property svn:eol-style set to native
File size: 6.6 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 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 = null;
43 private Image baseImage = null;
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.getImageSizes(ImageProvider.ImageSizes.SMALLICON));
91 a.putValue(Action.SMALL_ICON, icon);
92 icon = getImageIconBounded(ImageProvider.getImageSizes(ImageProvider.ImageSizes.LARGEICON));
93 a.putValue(Action.LARGE_ICON_KEY, icon);
94 }
95
96 /**
97 * Get an ImageIcon object for the image of this resource
98 * @param dim The requested dimensions. Use (-1,-1) for the original size
99 * and (width, -1) to set the width, but otherwise scale the image
100 * proportionally.
101 * @return ImageIcon object for the image of this resource, scaled according to dim
102 */
103 public ImageIcon getImageIcon(Dimension dim) {
104 if (dim.width < -1 || dim.width == 0 || dim.height < -1 || dim.height == 0)
105 throw new IllegalArgumentException(dim+" is invalid");
106 Image img = imgCache.get(dim);
107 if (img != null) {
108 return new ImageIcon(img);
109 }
110 if (svg != null) {
111 BufferedImage bimg = ImageProvider.createImageFromSvg(svg, dim);
112 if (bimg == null) {
113 return null;
114 }
115 if (overlayInfo != null) {
116 for (ImageOverlay o : overlayInfo) {
117 o.process(bimg);
118 }
119 }
120 imgCache.put(dim, bimg);
121 return new ImageIcon(bimg);
122 } else {
123 if (baseImage == null) throw new AssertionError();
124
125 int width = dim.width;
126 int height = dim.height;
127 ImageIcon icon = new ImageIcon(baseImage);
128 if (width == -1 && height == -1) {
129 width = icon.getIconWidth();
130 height = icon.getIconHeight();
131 } else if (width == -1) {
132 width = Math.max(1, icon.getIconWidth() * height / icon.getIconHeight());
133 } else if (height == -1) {
134 height = Math.max(1, icon.getIconHeight() * width / icon.getIconWidth());
135 }
136 Image i = icon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
137 BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
138 bimg.getGraphics().drawImage(i, 0, 0, null);
139 if (overlayInfo != null) {
140 for (ImageOverlay o : overlayInfo) {
141 o.process(bimg);
142 }
143 }
144 imgCache.put(dim, bimg);
145 return new ImageIcon(bimg);
146 }
147 }
148
149 /**
150 * Get image icon with a certain maximum size. The image is scaled down
151 * to fit maximum dimensions. (Keeps aspect ratio)
152 *
153 * @param maxSize The maximum size. One of the dimensions (width or height) can be -1,
154 * which means it is not bounded.
155 * @return ImageIcon object for the image of this resource, scaled down if needed, according to maxSize
156 */
157 public ImageIcon getImageIconBounded(Dimension maxSize) {
158 if (maxSize.width < -1 || maxSize.width == 0 || maxSize.height < -1 || maxSize.height == 0)
159 throw new IllegalArgumentException(maxSize+" is invalid");
160 float realWidth;
161 float realHeight;
162 if (svg != null) {
163 realWidth = svg.getWidth();
164 realHeight = svg.getHeight();
165 } else {
166 if (baseImage == null) throw new AssertionError();
167 ImageIcon icon = new ImageIcon(baseImage);
168 realWidth = icon.getIconWidth();
169 realHeight = icon.getIconHeight();
170 }
171 int maxWidth = maxSize.width;
172 int maxHeight = maxSize.height;
173
174 if (realWidth <= maxWidth) {
175 maxWidth = -1;
176 }
177 if (realHeight <= maxHeight) {
178 maxHeight = -1;
179 }
180
181 if (maxWidth == -1 && maxHeight == -1)
182 return getImageIcon(DEFAULT_DIMENSION);
183 else if (maxWidth == -1)
184 return getImageIcon(new Dimension(-1, maxHeight));
185 else if (maxHeight == -1)
186 return getImageIcon(new Dimension(maxWidth, -1));
187 else if (realWidth / maxWidth > realHeight / maxHeight)
188 return getImageIcon(new Dimension(maxWidth, -1));
189 else
190 return getImageIcon(new Dimension(-1, maxHeight));
191 }
192}
Note: See TracBrowser for help on using the repository browser.