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

Last change on this file since 6615 was 6316, checked in by Don-vip, 11 years ago

Sonar/FindBugs - Loose coupling

File size: 4.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.Map;
9
10import javax.swing.ImageIcon;
11
12import com.kitfox.svg.SVGDiagram;
13
14/**
15 * Holds data for one particular image.
16 * It can be backed by a svg or raster image.
17 *
18 * In the first case, 'svg' is not null and in the latter case, 'imgCache' has
19 * at least one entry for the key DEFAULT_DIMENSION.
20 * @since 4271
21 */
22class ImageResource {
23
24 /**
25 * Caches the image data for resized versions of the same image.
26 */
27 private Map<Dimension, Image> imgCache = new HashMap<Dimension, Image>();
28 private SVGDiagram svg;
29 public static final Dimension DEFAULT_DIMENSION = new Dimension(-1, -1);
30
31 public ImageResource(Image img) {
32 CheckParameterUtil.ensureParameterNotNull(img);
33 imgCache.put(DEFAULT_DIMENSION, img);
34 }
35
36 public ImageResource(SVGDiagram svg) {
37 CheckParameterUtil.ensureParameterNotNull(svg);
38 this.svg = svg;
39 }
40
41 public ImageIcon getImageIcon() {
42 return getImageIcon(DEFAULT_DIMENSION);
43 }
44
45 /**
46 * Get an ImageIcon object for the image of this resource
47 * @param dim The requested dimensions. Use (-1,-1) for the original size
48 * and (width, -1) to set the width, but otherwise scale the image
49 * proportionally.
50 * @return ImageIcon object for the image of this resource, scaled according to dim
51 */
52 public ImageIcon getImageIcon(Dimension dim) {
53 if (dim.width < -1 || dim.width == 0 || dim.height < -1 || dim.height == 0)
54 throw new IllegalArgumentException();
55 Image img = imgCache.get(dim);
56 if (img != null) {
57 return new ImageIcon(img);
58 }
59 if (svg != null) {
60 img = ImageProvider.createImageFromSvg(svg, dim);
61 if (img == null) {
62 return null;
63 }
64 imgCache.put(dim, img);
65 return new ImageIcon(img);
66 } else {
67 Image base = imgCache.get(DEFAULT_DIMENSION);
68 if (base == null) throw new AssertionError();
69
70 int width = dim.width;
71 int height = dim.height;
72 ImageIcon icon = new ImageIcon(base);
73 if (width == -1) {
74 width = Math.max(1, icon.getIconWidth() * height / icon.getIconHeight());
75 } else if (height == -1) {
76 height = Math.max(1, icon.getIconHeight() * width / icon.getIconWidth());
77 }
78 Image i = icon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
79 img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
80 img.getGraphics().drawImage(i, 0, 0, null);
81 imgCache.put(dim, img);
82 return new ImageIcon(img);
83 }
84 }
85
86 /**
87 * Get image icon with a certain maximum size. The image is scaled down
88 * to fit maximum dimensions. (Keeps aspect ratio)
89 *
90 * @param maxSize The maximum size. One of the dimensions (widht or height) can be -1,
91 * which means it is not bounded.
92 * @return ImageIcon object for the image of this resource, scaled down if needed, according to maxSize
93 */
94 public ImageIcon getImageIconBounded(Dimension maxSize) {
95 if (maxSize.width < -1 || maxSize.width == 0 || maxSize.height < -1 || maxSize.height == 0)
96 throw new IllegalArgumentException();
97 float realWidth;
98 float realHeight;
99 if (svg != null) {
100 realWidth = svg.getWidth();
101 realHeight = svg.getHeight();
102 } else {
103 Image base = imgCache.get(DEFAULT_DIMENSION);
104 if (base == null) throw new AssertionError();
105 ImageIcon icon = new ImageIcon(base);
106 realWidth = icon.getIconWidth();
107 realHeight = icon.getIconHeight();
108 }
109 int maxWidth = maxSize.width;
110 int maxHeight = maxSize.height;
111
112 if (realWidth <= maxWidth) {
113 maxWidth = -1;
114 }
115 if (realHeight <= maxHeight) {
116 maxHeight = -1;
117 }
118
119 if (maxWidth == -1 && maxHeight == -1)
120 return getImageIcon(DEFAULT_DIMENSION);
121 else if (maxWidth == -1)
122 return getImageIcon(new Dimension(-1, maxHeight));
123 else if (maxHeight == -1)
124 return getImageIcon(new Dimension(maxWidth, -1));
125 else
126 if (realWidth / maxWidth > realHeight / maxHeight)
127 return getImageIcon(new Dimension(maxWidth, -1));
128 else
129 return getImageIcon(new Dimension(-1, maxHeight));
130 }
131}
Note: See TracBrowser for help on using the repository browser.