Changeset 8095 in josm
- Timestamp:
- 2015-02-22T01:21:48+01:00 (10 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/tools
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/ImageProvider.java
r7834 r8095 39 39 import java.util.Hashtable; 40 40 import java.util.Iterator; 41 import java.util.LinkedList; 42 import java.util.List; 41 43 import java.util.Map; 42 44 import java.util.concurrent.ExecutorService; … … 80 82 * How to use: 81 83 * 82 * <code>ImageIcon icon = new ImageProvider(name).setMax Width(24).setMaxHeight(24).get();</code>84 * <code>ImageIcon icon = new ImageProvider(name).setMaxSize(ImageSizes.MAP).get();</code> 83 85 * (there are more options, see below) 84 86 * … … 127 129 /** LARGE_ICON_KEY value of on Action */ 128 130 LARGEICON, 129 /** MAPicon */131 /** map icon */ 130 132 MAP, 131 /** MAPicon maximum size */133 /** map icon maximum size */ 132 134 MAPMAX, 133 /** MENU icon size */ 135 /** menu icon size */ 136 CURSOR, 137 /** Cursor overlay icon size */ 138 CURSOROVERLAY, 139 /** Cursor icon size */ 134 140 MENU, 135 141 } … … 147 153 public static String PROP_TRANSPARENCY_COLOR = "josm.transparency.color"; 148 154 155 /** directories in which images are searched */ 149 156 protected Collection<String> dirs; 157 /** caching identifier */ 150 158 protected String id; 159 /** sub directory the image can be found in */ 151 160 protected String subdir; 161 /** image file name */ 152 162 protected String name; 163 /** archive file to take image from */ 153 164 protected File archive; 165 /** directory inside the archive */ 154 166 protected String inArchiveDir; 167 /** width of the resulting image, -1 when original image data should be used */ 155 168 protected int width = -1; 169 /** height of the resulting image, -1 when original image data should be used */ 156 170 protected int height = -1; 171 /** maximum width of the resulting image, -1 for no restriction */ 157 172 protected int maxWidth = -1; 173 /** maximum height of the resulting image, -1 for no restriction */ 158 174 protected int maxHeight = -1; 175 /** In case of errors do not throw exception but return <code>null</code> for missing image */ 159 176 protected boolean optional; 177 /** <code>true</code> if warnings should be suppressed */ 160 178 protected boolean suppressWarnings; 179 /** list of class loaders to take images from */ 161 180 protected Collection<ClassLoader> additionalClassLoaders; 181 /** ordered list of overlay images */ 182 protected List<ImageOverlay> overlayInfo = null; 162 183 163 184 private static SVGUniverse svgUniverse; … … 200 221 /** 201 222 * Constructs a new {@code ImageProvider} from a filename in a given directory. 202 * @param subdir 203 * @param name 204 * 223 * @param subdir subdirectory the image lies in 224 * @param name the name of the image. If it does not end with '.png' or '.svg', 225 * both extensions are tried. 205 226 */ 206 227 public ImageProvider(String subdir, String name) { … … 211 232 /** 212 233 * Constructs a new {@code ImageProvider} from a filename. 213 * @param name 214 * 234 * @param name the name of the image. If it does not end with '.png' or '.svg', 235 * both extensions are tried. 215 236 */ 216 237 public ImageProvider(String name) { … … 232 253 * If name starts with <tt>http://</tt> Id is not used for the cache. 233 254 * (A URL is unique anyway.) 255 * @param id the id for the cached image 234 256 * @return the current object, for convenience 235 257 */ … … 256 278 * 257 279 * (optional) 280 * @param inArchiveDir path inside the archive 258 281 * @return the current object, for convenience 259 282 */ 260 283 public ImageProvider setInArchiveDir(String inArchiveDir) { 261 284 this.inArchiveDir = inArchiveDir; 285 return this; 286 } 287 288 /** 289 * Add an overlay over the image. Multiple overlays are possible. 290 * 291 * @param overlay overlay image and placement specification 292 * @return the current object, for convenience 293 * @since 8095 294 */ 295 public ImageProvider addOverlay(ImageOverlay overlay) { 296 if (overlayInfo == null) { 297 overlayInfo = new LinkedList<ImageOverlay>(); 298 } 299 overlayInfo.add(overlay); 262 300 return this; 263 301 } … … 277 315 case MENU: /* MENU is SMALLICON - only provided in case of future changes */ 278 316 case SMALLICON: sizeval = Main.pref.getInteger("iconsize.smallicon", 16); break; 317 case CURSOROVERLAY: /* same as cursor - only provided in case of future changes */ 318 case CURSOR: sizeval = Main.pref.getInteger("iconsize.cursor", 32); break; 279 319 default: sizeval = Main.pref.getInteger("iconsize.default", 24); break; 280 320 } … … 288 328 * The width part of the dimension can be -1. Then it will only set the height but 289 329 * keep the aspect ratio. (And the other way around.) 330 * @param size final dimensions of the image 290 331 * @return the current object, for convenience 291 332 */ … … 300 341 * 301 342 * If not specified, the original size of the image is used. 343 * @param size final dimensions of the image 302 344 * @return the current object, for convenience 303 345 * @since 7687 … … 308 350 309 351 /** 352 * Set image width 353 * @param width final width of the image 310 354 * @see #setSize 311 355 * @return the current object, for convenience … … 317 361 318 362 /** 363 * Set image height 364 * @param height final height of the image 319 365 * @see #setSize 320 366 * @return the current object, for convenience … … 332 378 * 333 379 * 'size' and 'maxSize' are not compatible, you should set only one of them. 380 * @param maxSize maximum image size 334 381 * @return the current object, for convenience 335 382 */ … … 347 394 * 348 395 * 'size' and 'maxSize' are not compatible, you should set only one of them. 396 * @param size maximum image size 349 397 * @return the current object, for convenience 350 398 * @since 7687 … … 356 404 /** 357 405 * Convenience method, see {@link #setMaxSize(Dimension)}. 406 * @param maxSize maximum image size 358 407 * @return the current object, for convenience 359 408 */ … … 363 412 364 413 /** 414 * Limit the maximum width of the image. 415 * @param maxWidth maximum image width 365 416 * @see #setMaxSize 366 417 * @return the current object, for convenience … … 372 423 373 424 /** 425 * Limit the maximum height of the image. 426 * @param maxHeight maximum image height 374 427 * @see #setMaxSize 375 428 * @return the current object, for convenience … … 398 451 * 399 452 * In combination with setOptional(true); 453 * @param suppressWarnings if <code>true</code> warnings are suppressed 400 454 * @return the current object, for convenience 401 455 */ … … 407 461 /** 408 462 * Add a collection of additional class loaders to search image for. 463 * @param additionalClassLoaders class loaders to add to the internal list 409 464 * @return the current object, for convenience 410 465 */ … … 430 485 /** 431 486 * Execute the image request. 487 * 432 488 * @return the requested image or null if the request failed 433 489 * @since 7693 … … 446 502 } 447 503 } 504 if (overlayInfo != null) { 505 ir = new ImageResource(ir, overlayInfo); 506 } 448 507 return ir; 449 508 } … … 539 598 540 599 /** 600 * Load an image with a given file name, but do not throw an exception 601 * when the image cannot be found. 602 * 541 603 * @param name The icon name (base name with or without '.png' or '.svg' extension) 542 604 * @return the requested image or null if the request failed … … 554 616 "^data:([a-zA-Z]+/[a-zA-Z+]+)?(;base64)?,(.+)$"); 555 617 618 /** 619 * Internal implementation of the image request. 620 * 621 * @param additionalClassLoaders the list of class loaders to use 622 * @return the requested image or null if the request failed 623 */ 556 624 private ImageResource getIfAvailableImpl(Collection<ClassLoader> additionalClassLoaders) { 557 625 synchronized (cache) { … … 661 729 } 662 730 731 /** 732 * Internal implementation of the image request for URL's. 733 * 734 * @param url URL of the image 735 * @param type data type of the image 736 * @return the requested image or null if the request failed 737 */ 663 738 private static ImageResource getIfAvailableHttp(String url, ImageType type) { 664 739 CachedFile cf = new CachedFile(url) … … 689 764 } 690 765 766 /** 767 * Internal implementation of the image request for inline images (<b>data:</b> urls). 768 * 769 * @param url the data URL for image extraction 770 * @return the requested image or null if the request failed 771 */ 691 772 private static ImageResource getIfAvailableDataUrl(String url) { 692 773 try { … … 737 818 } 738 819 820 /** 821 * Internal implementation of the image request for wiki images. 822 * 823 * @param name image file name 824 * @param type data type of the image 825 * @return the requested image or null if the request failed 826 */ 739 827 private static ImageResource getIfAvailableWiki(String name, ImageType type) { 740 828 final Collection<String> defaultBaseUrls = Arrays.asList( … … 767 855 } 768 856 857 /** 858 * Internal implementation of the image request for images in Zip archives. 859 * 860 * @param fullName image file name 861 * @param archive the archive to get image from 862 * @param inArchiveDir directory of the image inside the archive or <code>null</code> 863 * @param type data type of the image 864 * @return the requested image or null if the request failed 865 */ 769 866 private static ImageResource getIfAvailableZip(String fullName, File archive, String inArchiveDir, ImageType type) { 770 867 try (ZipFile zipFile = new ZipFile(archive, StandardCharsets.UTF_8)) { … … 814 911 } 815 912 913 /** 914 * Internal implementation of the image request for local images. 915 * 916 * @param path image file path 917 * @param type data type of the image 918 * @return the requested image or null if the request failed 919 */ 816 920 private static ImageResource getIfAvailableLocalURL(URL path, ImageType type) { 817 921 switch (type) { … … 929 1033 930 1034 /** 931 * Reads the wiki page on a certain file in html format in order to find the real image URL. 1035 * Return URL of wiki image for an Wiki image described by and Wiki file info page 1036 * 1037 * @param base base URL for Wiki image 1038 * @param fn filename of the Wiki image 1039 * @return image URL for a Wiki image or null in case of error 932 1040 */ 933 1041 private static String getImgUrlFromWikiInfoPage(final String base, final String fn) { … … 977 1085 ImageIcon img = get("cursor", name); 978 1086 if (overlay != null) { 979 img = overlay(img, ImageProvider.get("cursor/modifier/" + overlay), OverlayPosition.SOUTHEAST); 1087 img = new ImageProvider("cursor", name).setMaxSize(ImageSizes.CURSOR) 1088 .addOverlay(new ImageOverlay(new ImageProvider("cursor/modifier/" + overlay) 1089 .setMaxSize(ImageSizes.CURSOROVERLAY))).get(); 980 1090 } 981 1091 if (GraphicsEnvironment.isHeadless()) { -
trunk/src/org/openstreetmap/josm/tools/ImageResource.java
r7937 r8095 6 6 import java.awt.image.BufferedImage; 7 7 import java.util.HashMap; 8 import java.util.List; 8 9 import java.util.Map; 9 10 … … 18 19 * It can be backed by a svg or raster image. 19 20 * 20 * In the first case, 'svg'is notnulland in the latter case,'imgCache' has21 * at least one entry for the key DEFAULT_DIMENSION.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>. 22 23 * @since 4271 23 24 */ … … 28 29 */ 29 30 private Map<Dimension, Image> imgCache = new HashMap<>(); 31 /** 32 * SVG diagram information in case of SVG vector image. 33 */ 30 34 private SVGDiagram svg; 31 35 /** … … 33 37 */ 34 38 public static final Dimension DEFAULT_DIMENSION = new Dimension(-1, -1); 39 /** 40 * ordered list of overlay images 41 */ 42 protected List<ImageOverlay> overlayInfo = null; 43 private Image baseImage = null; 35 44 36 45 /** … … 40 49 public ImageResource(Image img) { 41 50 CheckParameterUtil.ensureParameterNotNull(img); 51 this.baseImage = img; 42 52 imgCache.put(DEFAULT_DIMENSION, img); 43 53 } … … 50 60 CheckParameterUtil.ensureParameterNotNull(svg); 51 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; 52 74 } 53 75 … … 87 109 } 88 110 if (svg != null) { 89 img = ImageProvider.createImageFromSvg(svg, dim); 90 if (img == null) { 111 BufferedImage bimg = ImageProvider.createImageFromSvg(svg, dim); 112 if (bimg == null) { 91 113 return null; 92 114 } 93 imgCache.put(dim, img); 94 return new ImageIcon(img); 115 if (overlayInfo != null) { 116 for (ImageOverlay o : overlayInfo) { 117 o.apply(bimg); 118 } 119 } 120 imgCache.put(dim, bimg); 121 return new ImageIcon(bimg); 95 122 } else { 96 Image base = imgCache.get(DEFAULT_DIMENSION); 97 if (base == null) throw new AssertionError(); 123 if (baseImage == null) throw new AssertionError(); 98 124 99 125 int width = dim.width; 100 126 int height = dim.height; 101 ImageIcon icon = new ImageIcon(base); 102 if (width == -1) { 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) { 103 132 width = Math.max(1, icon.getIconWidth() * height / icon.getIconHeight()); 104 133 } else if (height == -1) { … … 106 135 } 107 136 Image i = icon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH); 108 img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 109 img.getGraphics().drawImage(i, 0, 0, null); 110 imgCache.put(dim, img); 111 return new ImageIcon(img); 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.apply(bimg); 142 } 143 } 144 imgCache.put(dim, bimg); 145 return new ImageIcon(bimg); 112 146 } 113 147 } … … 117 151 * to fit maximum dimensions. (Keeps aspect ratio) 118 152 * 119 * @param maxSize The maximum size. One of the dimensions (wid htor height) can be -1,153 * @param maxSize The maximum size. One of the dimensions (width or height) can be -1, 120 154 * which means it is not bounded. 121 155 * @return ImageIcon object for the image of this resource, scaled down if needed, according to maxSize … … 130 164 realHeight = svg.getHeight(); 131 165 } else { 132 Image base = imgCache.get(DEFAULT_DIMENSION); 133 if (base == null) throw new AssertionError(); 134 ImageIcon icon = new ImageIcon(base); 166 if (baseImage == null) throw new AssertionError(); 167 ImageIcon icon = new ImageIcon(baseImage); 135 168 realWidth = icon.getIconWidth(); 136 169 realHeight = icon.getIconHeight(); … … 152 185 else if (maxHeight == -1) 153 186 return getImageIcon(new Dimension(maxWidth, -1)); 187 else if (realWidth / maxWidth > realHeight / maxHeight) 188 return getImageIcon(new Dimension(maxWidth, -1)); 154 189 else 155 if (realWidth / maxWidth > realHeight / maxHeight) 156 return getImageIcon(new Dimension(maxWidth, -1)); 157 else 158 return getImageIcon(new Dimension(-1, maxHeight)); 190 return getImageIcon(new Dimension(-1, maxHeight)); 159 191 } 160 192 }
Note:
See TracChangeset
for help on using the changeset viewer.