[4271] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
| 4 | import com.kitfox.svg.SVGDiagram;
|
---|
| 5 |
|
---|
| 6 | import java.awt.Dimension;
|
---|
| 7 | import java.awt.Image;
|
---|
[4820] | 8 | import java.awt.image.BufferedImage;
|
---|
[4271] | 9 | import java.util.HashMap;
|
---|
| 10 | import javax.swing.ImageIcon;
|
---|
| 11 |
|
---|
| 12 | /**
|
---|
| 13 | * Holds data for one particular image.
|
---|
| 14 | * It can be backed by a svg or raster image.
|
---|
[6070] | 15 | *
|
---|
| 16 | * In the first case, 'svg' is not null and in the latter case, 'imgCache' has
|
---|
[4271] | 17 | * at least one entry for the key DEFAULT_DIMENSION.
|
---|
| 18 | */
|
---|
| 19 | class ImageResource {
|
---|
[6070] | 20 |
|
---|
[4271] | 21 | /**
|
---|
| 22 | * Caches the image data for resized versions of the same image.
|
---|
| 23 | */
|
---|
[4961] | 24 | private HashMap<Dimension, BufferedImage> imgCache = new HashMap<Dimension, BufferedImage>();
|
---|
[4271] | 25 | private SVGDiagram svg;
|
---|
| 26 | public static final Dimension DEFAULT_DIMENSION = new Dimension(-1, -1);
|
---|
[6070] | 27 |
|
---|
[4961] | 28 | public ImageResource(BufferedImage img) {
|
---|
[4271] | 29 | CheckParameterUtil.ensureParameterNotNull(img);
|
---|
[4960] | 30 | imgCache.put(DEFAULT_DIMENSION, img);
|
---|
[4271] | 31 | }
|
---|
| 32 |
|
---|
| 33 | public ImageResource(SVGDiagram svg) {
|
---|
| 34 | CheckParameterUtil.ensureParameterNotNull(svg);
|
---|
| 35 | this.svg = svg;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | public ImageIcon getImageIcon() {
|
---|
[4960] | 39 | return getImageIcon(DEFAULT_DIMENSION);
|
---|
[4271] | 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 | */
|
---|
[4960] | 48 | public ImageIcon getImageIcon(Dimension dim) {
|
---|
[4712] | 49 | if (dim.width < -1 || dim.width == 0 || dim.height < -1 || dim.height == 0)
|
---|
| 50 | throw new IllegalArgumentException();
|
---|
[4961] | 51 | BufferedImage img = imgCache.get(dim);
|
---|
[4960] | 52 | if (img != null) {
|
---|
| 53 | return new ImageIcon(img);
|
---|
[4271] | 54 | }
|
---|
| 55 | if (svg != null) {
|
---|
[4960] | 56 | img = ImageProvider.createImageFromSvg(svg, dim);
|
---|
[5830] | 57 | if (img == null) {
|
---|
| 58 | return null;
|
---|
| 59 | }
|
---|
[4960] | 60 | imgCache.put(dim, img);
|
---|
[4271] | 61 | return new ImageIcon(img);
|
---|
| 62 | } else {
|
---|
[4961] | 63 | BufferedImage base = imgCache.get(DEFAULT_DIMENSION);
|
---|
[4271] | 64 | if (base == null) throw new AssertionError();
|
---|
[6070] | 65 |
|
---|
[4271] | 66 | int width = dim.width;
|
---|
| 67 | int height = dim.height;
|
---|
[4960] | 68 | ImageIcon icon = new ImageIcon(base);
|
---|
[4271] | 69 | if (width == -1) {
|
---|
| 70 | width = icon.getIconWidth() * height / icon.getIconHeight();
|
---|
| 71 | } else if (height == -1) {
|
---|
| 72 | height = icon.getIconHeight() * width / icon.getIconWidth();
|
---|
| 73 | }
|
---|
[4961] | 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);
|
---|
[4960] | 77 | imgCache.put(dim, img);
|
---|
[4271] | 78 | return new ImageIcon(img);
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
[4712] | 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 | */
|
---|
[4960] | 89 | public ImageIcon getImageIconBounded(Dimension maxSize) {
|
---|
[4712] | 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 {
|
---|
[4961] | 98 | BufferedImage base = imgCache.get(DEFAULT_DIMENSION);
|
---|
[4712] | 99 | if (base == null) throw new AssertionError();
|
---|
[4960] | 100 | ImageIcon icon = new ImageIcon(base);
|
---|
[4712] | 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)
|
---|
[4960] | 115 | return getImageIcon(DEFAULT_DIMENSION);
|
---|
[4712] | 116 | else if (maxWidth == -1)
|
---|
[4960] | 117 | return getImageIcon(new Dimension(-1, maxHeight));
|
---|
[4712] | 118 | else if (maxHeight == -1)
|
---|
[4960] | 119 | return getImageIcon(new Dimension(maxWidth, -1));
|
---|
[4712] | 120 | else
|
---|
| 121 | if (realWidth / maxWidth > realHeight / maxHeight)
|
---|
[4960] | 122 | return getImageIcon(new Dimension(maxWidth, -1));
|
---|
[4712] | 123 | else
|
---|
[4960] | 124 | return getImageIcon(new Dimension(-1, maxHeight));
|
---|
[4712] | 125 | }
|
---|
[4271] | 126 | }
|
---|