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 |
|
---|
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 | */
|
---|
25 | public 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 | }
|
---|
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 | int maxWidth = maxSize.width;
|
---|
163 | int maxHeight = maxSize.height;
|
---|
164 | if (svg != null) {
|
---|
165 | realWidth = svg.getWidth();
|
---|
166 | realHeight = svg.getHeight();
|
---|
167 | } else {
|
---|
168 | if (baseImage == null) throw new AssertionError();
|
---|
169 | ImageIcon icon = new ImageIcon(baseImage);
|
---|
170 | realWidth = icon.getIconWidth();
|
---|
171 | realHeight = icon.getIconHeight();
|
---|
172 | if (realWidth <= maxWidth) {
|
---|
173 | maxWidth = -1;
|
---|
174 | }
|
---|
175 | if (realHeight <= maxHeight) {
|
---|
176 | maxHeight = -1;
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | if (maxWidth == -1 && maxHeight == -1)
|
---|
181 | return getImageIcon(DEFAULT_DIMENSION);
|
---|
182 | else if (maxWidth == -1)
|
---|
183 | return getImageIcon(new Dimension(-1, maxHeight));
|
---|
184 | else if (maxHeight == -1)
|
---|
185 | return getImageIcon(new Dimension(maxWidth, -1));
|
---|
186 | else if (realWidth / maxWidth > realHeight / maxHeight)
|
---|
187 | return getImageIcon(new Dimension(maxWidth, -1));
|
---|
188 | else
|
---|
189 | return getImageIcon(new Dimension(-1, maxHeight));
|
---|
190 | }
|
---|
191 | }
|
---|