Changeset 4271 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2011-07-27T21:23:35+02:00 (13 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/tools
- Files:
-
- 2 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/ImageProvider.java
r4261 r4271 10 10 import java.awt.Component; 11 11 import java.awt.Cursor; 12 import java.awt.Dimension; 12 13 import java.awt.Graphics; 13 14 import java.awt.Graphics2D; … … 42 43 43 44 /** 44 * Helper class to support the application with images.45 * Helper class to support the application with images. 45 46 * @author imi 46 47 */ … … 59 60 public static enum ImageType { 60 61 SVG, // scalable vector graphics 61 OTHER // everything else, e.g. png, gif 62 // must be supported by Java 63 } 64 65 /** 66 * remember whether the image has been sanitized 67 */ 68 private static class ImageWrapper { 69 Image img; 70 boolean sanitized; 71 72 public ImageWrapper(Image img, boolean sanitized) { 73 this.img = img; 74 this.sanitized = sanitized; 75 } 62 OTHER // everything else, e.g. png, gif (must be supported by Java) 76 63 } 77 64 … … 79 66 * The icon cache 80 67 */ 81 private static Map<String, Image Wrapper> cache = new HashMap<String, ImageWrapper>();68 private static Map<String, ImageResource> cache = new HashMap<String, ImageResource>(); 82 69 83 70 /** … … 127 114 } 128 115 116 public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, boolean sanitize) { 117 return getIfAvailable(dirs, id, subdir, name, archive, null, sanitize); 118 } 119 129 120 /** 130 121 * The full path of the image is either a url (starting with http://) … … 137 128 * it will try both extensions. 138 129 * @param archive A zip file where the image is located (may be null). 130 * @param dim The dimensions of the image if it should be scaled. null if the 131 * original size of the image should be returned. The width 132 * part of the dimension can be -1. Then it will scale the width 133 * in the same way as the height. (And the other way around.) 139 134 * @param sanitize If the image should be repainted to a new BufferedImage to work 140 135 * around certain issues. 141 136 */ 142 public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, boolean sanitize) {143 Image Wrapper iw= getIfAvailableImpl(dirs, id, subdir, name, archive);144 if (i w== null)137 public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, Dimension dim, boolean sanitize) { 138 ImageResource ir = getIfAvailableImpl(dirs, id, subdir, name, archive); 139 if (ir == null) 145 140 return null; 146 if (sanitize && !iw.sanitized) { 147 iw.img = sanitize(iw.img); 148 iw.sanitized = true; 149 } 150 return new ImageIcon(iw.img); 151 } 152 153 private static ImageWrapper getIfAvailableImpl(Collection<String> dirs, String id, String subdir, String name, File archive) { 141 return ir.getImageIcon(dim == null ? ImageResource.DEFAULT_DIMENSION : dim, sanitize); 142 } 143 144 private static ImageResource getIfAvailableImpl(Collection<String> dirs, String id, String subdir, String name, File archive) { 154 145 if (name == null) 155 146 return null; … … 158 149 if (name.startsWith("http://")) { 159 150 String url = name; 160 Image Wrapper iw= cache.get(url);161 if (i w != null) return iw;162 i w= getIfAvailableHttp(url, type);163 if (i w!= null) {164 cache.put(url, i w);165 } 166 return i w;151 ImageResource ir = cache.get(url); 152 if (ir != null) return ir; 153 ir = getIfAvailableHttp(url, type); 154 if (ir != null) { 155 cache.put(url, ir); 156 } 157 return ir; 167 158 } 168 159 … … 198 189 } 199 190 200 Image Wrapper iw= cache.get(cache_name);201 if (i w != null) return iw;191 ImageResource ir = cache.get(cache_name); 192 if (ir != null) return ir; 202 193 203 194 switch (place) { 204 195 case ARCHIVE: 205 196 if (archive != null) { 206 i w= getIfAvailableZip(full_name, archive, type);207 if (i w!= null) {208 cache.put(cache_name, i w);209 return i w;197 ir = getIfAvailableZip(full_name, archive, type); 198 if (ir != null) { 199 cache.put(cache_name, ir); 200 return ir; 210 201 } 211 202 } … … 220 211 if (path == null) 221 212 continue; 222 i w= getIfAvailableLocalURL(path, type);223 if (i w!= null) {224 cache.put(cache_name, i w);225 return i w;213 ir = getIfAvailableLocalURL(path, type); 214 if (ir != null) { 215 cache.put(cache_name, ir); 216 return ir; 226 217 } 227 218 break; … … 232 223 } 233 224 234 private static ImageWrapper getIfAvailableHttp(String url, ImageType type) { 235 Image img = null; 225 private static ImageResource getIfAvailableHttp(String url, ImageType type) { 236 226 try { 237 227 MirroredInputStream is = new MirroredInputStream(url, … … 240 230 case SVG: 241 231 URI uri = getSvgUniverse().loadSVG(is, is.getFile().toURI().toURL().toString()); 242 img = createImageFromSvgUri(uri);243 break;232 SVGDiagram svg = getSvgUniverse().getDiagram(uri); 233 return svg == null ? null : new ImageResource(svg); 244 234 case OTHER: 245 img = Toolkit.getDefaultToolkit().createImage(is.getFile().toURI().toURL()); 246 break; 235 Image img = Toolkit.getDefaultToolkit().createImage(is.getFile().toURI().toURL()); 236 return img == null ? null : new ImageResource(img, false); 237 default: 238 throw new AssertionError(); 247 239 } 248 240 } catch (IOException e) { 249 }250 return img == null ? null : new ImageWrapper(img, false);251 } 252 253 private static Image WrappergetIfAvailableZip(String full_name, File archive, ImageType type) {241 return null; 242 } 243 } 244 245 private static ImageResource getIfAvailableZip(String full_name, File archive, ImageType type) { 254 246 ZipFile zipFile = null; 255 Image img = null;256 247 try 257 248 { … … 269 260 case SVG: 270 261 URI uri = getSvgUniverse().loadSVG(is, full_name); 271 img = createImageFromSvgUri(uri);272 break;262 SVGDiagram svg = getSvgUniverse().getDiagram(uri); 263 return svg == null ? null : new ImageResource(svg); 273 264 case OTHER: 274 265 while(size > 0) … … 278 269 size -= l; 279 270 } 280 img = Toolkit.getDefaultToolkit().createImage(buf); 281 break; 271 Image img = Toolkit.getDefaultToolkit().createImage(buf); 272 return img == null ? null : new ImageResource(img, false); 273 default: 274 throw new AssertionError(); 282 275 } 283 276 } finally { … … 297 290 } 298 291 } 299 return img == null ? null : new ImageWrapper(img, false); 300 } 301 302 private static ImageWrapper getIfAvailableLocalURL(URL path, ImageType type) { 303 Image img = null; 292 return null; 293 } 294 295 private static ImageResource getIfAvailableLocalURL(URL path, ImageType type) { 304 296 switch (type) { 305 297 case SVG: 306 298 URI uri = getSvgUniverse().loadSVG(path); 307 img = createImageFromSvgUri(uri);308 break;299 SVGDiagram svg = getSvgUniverse().getDiagram(uri); 300 return svg == null ? null : new ImageResource(svg); 309 301 case OTHER: 310 img = Toolkit.getDefaultToolkit().createImage(path); 311 break; 312 } 313 return img == null ? null : new ImageWrapper(img, false); 302 Image img = Toolkit.getDefaultToolkit().createImage(path); 303 return img == null ? null : new ImageResource(img, false); 304 default: 305 throw new AssertionError(); 306 } 314 307 } 315 308 … … 521 514 } 522 515 523 private static Image createImageFromSvgUri(URI uri) { 524 SVGDiagram dia = getSvgUniverse().getDiagram(uri); 525 int w = (int)dia.getWidth(); 526 int h = (int)dia.getHeight(); 527 Image img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 516 public static Image createImageFromSvg(SVGDiagram svg, Dimension dim) { 517 float realWidth = svg.getWidth(); 518 float realHeight = svg.getHeight(); 519 int width = Math.round(realWidth); 520 int height = Math.round(realHeight); 521 Double scaleX = null, scaleY = null; 522 if (dim.width != -1) { 523 width = dim.width; 524 scaleX = (double) width / realWidth; 525 if (dim.height == -1) { 526 scaleY = scaleX; 527 height = (int) Math.round(realHeight * scaleY); 528 } else { 529 height = dim.height; 530 scaleY = (double) height / realHeight; 531 } 532 } else if (dim.height != -1) { 533 height = dim.height; 534 scaleX = scaleY = (double) height / realHeight; 535 width = (int) Math.round(realWidth * scaleX); 536 } 537 Image img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 528 538 Graphics2D g = ((BufferedImage) img).createGraphics(); 529 g.setClip(0, 0, w, h); 539 g.setClip(0, 0, width, height); 540 if (scaleX != null) { 541 g.scale(scaleX, scaleY); 542 } 530 543 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 531 544 try { 532 dia.render(g);545 svg.render(g); 533 546 } catch (SVGException ex) { 534 547 return null;
Note:
See TracChangeset
for help on using the changeset viewer.