Changeset 3862 in josm for trunk/src/org/openstreetmap/josm/tools
- Timestamp:
- 2011-02-06T15:48:58+01:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/ImageProvider.java
r3512 r3862 52 52 53 53 /** 54 * remember whether the image has been sanitized 55 */ 56 private static class ImageWrapper { 57 Image img; 58 boolean sanitized; 59 60 public ImageWrapper(Image img, boolean sanitized) { 61 this.img = img; 62 this.sanitized = sanitized; 63 } 64 } 65 66 /** 54 67 * The icon cache 55 68 */ 56 private static Map<String, Image> cache = new HashMap<String, Image>(); 69 private static Map<String, ImageWrapper> cache = new HashMap<String, ImageWrapper>(); 57 70 58 71 /** … … 84 97 } 85 98 86 public static finalImageIcon getIfAvailable(String[] dirs, String id, String subdir, String name) {99 public static ImageIcon getIfAvailable(String[] dirs, String id, String subdir, String name) { 87 100 return getIfAvailable(Arrays.asList(dirs), id, subdir, name); 88 101 } … … 95 108 public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name) { 96 109 return getIfAvailable(dirs, id, subdir, name, null); 110 } 111 112 public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive) { 113 return getIfAvailable(dirs, id, subdir, name, archive, false); 97 114 } 98 115 … … 106 123 * @param name The name of the image. If it contains no '.', a png extension is added. 107 124 * @param archive A zip file where the image is located. 108 */ 109 public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive) { 125 * @param sanitize If the image should be repainted to a new BufferedImage to work 126 * around certain issues. 127 */ 128 public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, boolean sanitize) { 129 ImageWrapper iw = getIfAvailableImpl(dirs, id, subdir, name, archive); 130 if (iw == null) 131 return null; 132 if (sanitize && !iw.sanitized) { 133 iw.img = sanitize(iw.img); 134 iw.sanitized = true; 135 } 136 return new ImageIcon(iw.img); 137 } 138 139 private static ImageWrapper getIfAvailableImpl(Collection<String> dirs, String id, String subdir, String name, File archive) { 110 140 if (name == null) 111 141 return null; 112 142 if (name.startsWith("http://")) { 113 Image img= cache.get(name);114 if (i mg== null) {143 ImageWrapper iw = cache.get(name); 144 if (iw == null) { 115 145 try { 116 146 MirroredInputStream is = new MirroredInputStream(name, new File(Main.pref.getPreferencesDir(), 117 147 "images").toString()); 118 img = Toolkit.getDefaultToolkit().createImage(is.getFile().toURI().toURL()); 119 cache.put(name, img); 148 Image img = Toolkit.getDefaultToolkit().createImage(is.getFile().toURI().toURL()); 149 iw = new ImageWrapper(img, false); 150 cache.put(name, iw); 120 151 } catch (IOException e) { 121 152 } 122 153 } 123 return i mg == null ? null : new ImageIcon(img);154 return iw; 124 155 } 125 156 if (subdir == null) { … … 139 170 } 140 171 141 Image img= cache.get(cache_name);142 if (i mg== null) {172 ImageWrapper iw = cache.get(cache_name); 173 if (iw == null) { 143 174 if(archive != null) 144 175 { … … 162 193 size -= l; 163 194 } 164 img = Toolkit.getDefaultToolkit().createImage(buf); 195 Image img = Toolkit.getDefaultToolkit().createImage(buf); 196 iw = new ImageWrapper(img, false); 165 197 } finally { 166 198 if (is != null) { … … 185 217 // and don't bother to create a URL unless we're actually 186 218 // creating the image. 187 if(i mg== null)219 if(iw == null) 188 220 { 189 221 URL path = getImageUrl(full_name, dirs); 190 222 if (path == null) 191 223 return null; 192 img = Toolkit.getDefaultToolkit().createImage(path); 193 } 194 cache.put(cache_name, img); 195 } 196 197 return new ImageIcon(img); 224 Image img = Toolkit.getDefaultToolkit().createImage(path); 225 iw = new ImageWrapper(img, false); 226 } 227 cache.put(cache_name, iw); 228 } 229 230 return iw; 198 231 } 199 232 … … 405 438 return get("data", type.getAPIName()); 406 439 } 440 441 public static BufferedImage sanitize(Image img) { 442 (new ImageIcon(img)).getImage(); // load competely 443 int width = img.getWidth(null); 444 int height = img.getHeight(null); 445 BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 446 result.getGraphics().drawImage(img, 0, 0, null); 447 return result; 448 } 407 449 }
Note:
See TracChangeset
for help on using the changeset viewer.