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

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

fix #8577 - Exception with empty SVG as image

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