Ticket #1576: nosan.patch
File nosan.patch, 14.0 KB (added by , 13 years ago) |
---|
-
src/org/openstreetmap/josm/tools/ImageProvider.java
84 84 OTHER // everything else, e.g. png, gif (must be supported by Java) 85 85 } 86 86 87 public static enum SanitizeMode {88 OFF, // never sanitize the image89 ALWAYS, // always copy to a new BufferedImage90 MAKE_BUFFEREDIMAGE // make sure the returned image is instance of BufferedImage91 }92 93 87 protected Collection<String> dirs; 94 88 protected String id; 95 89 protected String subdir; … … 99 93 protected int height = -1; 100 94 protected int maxWidth = -1; 101 95 protected int maxHeight = -1; 102 protected SanitizeMode sanitize;103 96 protected boolean optional; 104 97 105 98 private static SVGUniverse svgUniverse; … … 207 200 } 208 201 209 202 /** 210 * Set true, if the image should be repainted to a new BufferedImage in order to work around certain issues.211 */212 public ImageProvider setSanitize(SanitizeMode sanitize) {213 this.sanitize = sanitize;214 return this;215 }216 217 /**218 203 * The image URL comes from user data and the image may be missing. 219 204 * 220 205 * Set true, if JOSM should *not* throw a RuntimeException in case the image cannot be located. … … 239 224 } 240 225 } 241 226 if (maxWidth != -1 || maxHeight != -1) 242 return ir.getImageIconBounded(new Dimension(maxWidth, maxHeight) , sanitize);227 return ir.getImageIconBounded(new Dimension(maxWidth, maxHeight)); 243 228 else 244 return ir.getImageIcon(new Dimension(width, height) , sanitize);229 return ir.getImageIcon(new Dimension(width, height)); 245 230 } 246 231 247 232 /** … … 291 276 : URLDecoder.decode(data, "utf-8").getBytes(); 292 277 if (mediatype != null && mediatype.contains("image/svg+xml")) { 293 278 URI uri = getSvgUniverse().loadSVG(new StringReader(new String(bytes)), name); 294 SVGDiagram svg = getSvgUniverse().getDiagram(uri); 295 return new ImageResource(svg); 279 return new ImageResource(getSvgUniverse().getDiagram(uri)); 296 280 } else { 297 return new ImageResource(new ImageIcon(bytes).getImage() , true);281 return new ImageResource(new ImageIcon(bytes).getImage()); 298 282 } 299 283 } 300 284 } … … 405 389 try { 406 390 img = ImageIO.read(is.getFile().toURI().toURL()); 407 391 } catch (IOException e) {} 408 return img == null ? null : new ImageResource(img , true);392 return img == null ? null : new ImageResource(img); 409 393 default: 410 394 throw new AssertionError(); 411 395 } … … 474 458 try { 475 459 img = ImageIO.read(new ByteArrayInputStream(buf)); 476 460 } catch (IOException e) {} 477 return img == null ? null : new ImageResource(img , false);461 return img == null ? null : new ImageResource(img); 478 462 default: 479 463 throw new AssertionError(); 480 464 } … … 508 492 try { 509 493 img = ImageIO.read(path); 510 494 } catch (IOException e) {} 511 return img == null ? null : new ImageResource(img , true);495 return img == null ? null : new ImageResource(img); 512 496 default: 513 497 throw new AssertionError(); 514 498 } … … 766 750 return get("data", type.getAPIName()); 767 751 } 768 752 769 public static BufferedImage sanitize(Image img) {770 (new ImageIcon(img)).getImage(); // load competely771 int width = img.getWidth(null);772 int height = img.getHeight(null);773 BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);774 result.getGraphics().drawImage(img, 0, 0, null);775 return result;776 }777 778 753 public static Image createImageFromSvg(SVGDiagram svg, Dimension dim) { 779 754 float realWidth = svg.getWidth(); 780 755 float realHeight = svg.getHeight(); -
src/org/openstreetmap/josm/tools/ImageResource.java
9 9 import java.util.HashMap; 10 10 import javax.swing.ImageIcon; 11 11 12 import org.openstreetmap.josm.tools.ImageProvider.SanitizeMode;13 14 12 /** 15 13 * Holds data for one particular image. 16 14 * It can be backed by a svg or raster image. … … 23 21 /** 24 22 * Caches the image data for resized versions of the same image. 25 23 */ 26 private HashMap<Dimension, Image Wrapper> imgCache = new HashMap<Dimension, ImageWrapper>();24 private HashMap<Dimension, Image> imgCache = new HashMap<Dimension, Image>(); 27 25 private SVGDiagram svg; 28 26 public static final Dimension DEFAULT_DIMENSION = new Dimension(-1, -1); 29 27 30 /** 31 * remember whether the image has been sanitized 32 */ 33 private static class ImageWrapper { 34 Image img; 35 boolean sanitized; 36 37 public ImageWrapper(Image img, boolean sanitized) { 38 CheckParameterUtil.ensureParameterNotNull(img); 39 this.img = img; 40 this.sanitized = sanitized; 41 } 42 } 43 44 public ImageResource(Image img, boolean sanitized) { 28 public ImageResource(Image img) { 45 29 CheckParameterUtil.ensureParameterNotNull(img); 46 imgCache.put(DEFAULT_DIMENSION, new ImageWrapper(img, sanitized));30 imgCache.put(DEFAULT_DIMENSION, img); 47 31 } 48 32 49 33 public ImageResource(SVGDiagram svg) { … … 52 36 } 53 37 54 38 public ImageIcon getImageIcon() { 55 return getImageIcon(DEFAULT_DIMENSION , SanitizeMode.OFF);39 return getImageIcon(DEFAULT_DIMENSION); 56 40 } 57 41 58 42 /** … … 60 44 * @param dim The requested dimensions. Use (-1,-1) for the original size 61 45 * and (width, -1) to set the width, but otherwise scale the image 62 46 * proportionally. 63 * @param sanitize Whether the returned image should be copied to a BufferedImage64 * to avoid certain problem with native image formats.65 47 */ 66 public ImageIcon getImageIcon(Dimension dim , SanitizeMode sanitize) {48 public ImageIcon getImageIcon(Dimension dim) { 67 49 if (dim.width < -1 || dim.width == 0 || dim.height < -1 || dim.height == 0) 68 50 throw new IllegalArgumentException(); 69 ImageWrapper iw = imgCache.get(dim); 70 if (iw != null) { 71 if (!iw.sanitized) { 72 if (sanitize == SanitizeMode.ALWAYS || (sanitize == SanitizeMode.MAKE_BUFFEREDIMAGE && !(iw.img instanceof BufferedImage))) { 73 iw.img = ImageProvider.sanitize(iw.img); 74 iw.sanitized = true; 75 } 76 } 77 return new ImageIcon(iw.img); 51 Image img = imgCache.get(dim); 52 if (img != null) { 53 return new ImageIcon(img); 78 54 } 79 55 if (svg != null) { 80 Imageimg = ImageProvider.createImageFromSvg(svg, dim);81 imgCache.put(dim, new ImageWrapper(img, true));56 img = ImageProvider.createImageFromSvg(svg, dim); 57 imgCache.put(dim, img); 82 58 return new ImageIcon(img); 83 59 } else { 84 Image Wrapperbase = imgCache.get(DEFAULT_DIMENSION);60 Image base = imgCache.get(DEFAULT_DIMENSION); 85 61 if (base == null) throw new AssertionError(); 86 62 87 63 int width = dim.width; 88 64 int height = dim.height; 89 ImageIcon icon = new ImageIcon(base .img);65 ImageIcon icon = new ImageIcon(base); 90 66 if (width == -1) { 91 67 width = icon.getIconWidth() * height / icon.getIconHeight(); 92 68 } else if (height == -1) { 93 69 height = icon.getIconHeight() * width / icon.getIconWidth(); 94 70 } 95 Image img;96 71 img = icon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH); 97 boolean sanitized = false; 98 if (sanitize == SanitizeMode.ALWAYS || (sanitize == SanitizeMode.MAKE_BUFFEREDIMAGE && !(img instanceof BufferedImage))) { 99 img = ImageProvider.sanitize(img); 100 sanitized = true; 101 } 102 imgCache.put(dim, new ImageWrapper(img, sanitized)); 72 imgCache.put(dim, img); 103 73 return new ImageIcon(img); 104 74 } 105 75 } … … 111 81 * @param maxSize The maximum size. One of the dimensions (widht or height) can be -1, 112 82 * which means it is not bounded. 113 83 */ 114 public ImageIcon getImageIconBounded(Dimension maxSize , SanitizeMode sanitize) {84 public ImageIcon getImageIconBounded(Dimension maxSize) { 115 85 if (maxSize.width < -1 || maxSize.width == 0 || maxSize.height < -1 || maxSize.height == 0) 116 86 throw new IllegalArgumentException(); 117 87 float realWidth; … … 120 90 realWidth = svg.getWidth(); 121 91 realHeight = svg.getHeight(); 122 92 } else { 123 Image Wrapperbase = imgCache.get(DEFAULT_DIMENSION);93 Image base = imgCache.get(DEFAULT_DIMENSION); 124 94 if (base == null) throw new AssertionError(); 125 ImageIcon icon = new ImageIcon(base .img);95 ImageIcon icon = new ImageIcon(base); 126 96 realWidth = icon.getIconWidth(); 127 97 realHeight = icon.getIconHeight(); 128 98 } … … 137 107 } 138 108 139 109 if (maxWidth == -1 && maxHeight == -1) 140 return getImageIcon(DEFAULT_DIMENSION , sanitize);110 return getImageIcon(DEFAULT_DIMENSION); 141 111 else if (maxWidth == -1) 142 return getImageIcon(new Dimension(-1, maxHeight) , sanitize);112 return getImageIcon(new Dimension(-1, maxHeight)); 143 113 else if (maxHeight == -1) 144 return getImageIcon(new Dimension(maxWidth, -1) , sanitize);114 return getImageIcon(new Dimension(maxWidth, -1)); 145 115 else 146 116 if (realWidth / maxWidth > realHeight / maxHeight) 147 return getImageIcon(new Dimension(maxWidth, -1) , sanitize);117 return getImageIcon(new Dimension(maxWidth, -1)); 148 118 else 149 return getImageIcon(new Dimension(-1, maxHeight) , sanitize);119 return getImageIcon(new Dimension(-1, maxHeight)); 150 120 } 151 121 } -
src/org/openstreetmap/josm/gui/mappaint/AreaElemStyle.java
17 17 import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors; 18 18 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference; 19 19 import org.openstreetmap.josm.tools.CheckParameterUtil; 20 import org.openstreetmap.josm.tools.ImageProvider.SanitizeMode;21 20 import org.openstreetmap.josm.tools.Utils; 22 21 23 22 public class AreaElemStyle extends ElemStyle … … 44 43 45 44 IconReference iconRef = c.get("fill-image", null, IconReference.class); 46 45 if (iconRef != null) { 47 ImageIcon icon = MapPaintStyles.getIcon(iconRef, -1, -1 , SanitizeMode.MAKE_BUFFEREDIMAGE);46 ImageIcon icon = MapPaintStyles.getIcon(iconRef, -1, -1); 48 47 if (icon != null) { 49 48 if (!(icon.getImage() instanceof BufferedImage)) 50 49 throw new RuntimeException(); -
src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java
26 26 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 27 27 import org.openstreetmap.josm.io.MirroredInputStream; 28 28 import org.openstreetmap.josm.tools.ImageProvider; 29 import org.openstreetmap.josm.tools.ImageProvider.SanitizeMode;30 29 31 30 /** 32 31 * This class manages the ElemStyles instance. The object you get with … … 88 87 } 89 88 90 89 public static ImageIcon getIcon(IconReference ref, int width, int height) { 91 return getIcon(ref, width, height, SanitizeMode.OFF);92 }93 94 public static ImageIcon getIcon(IconReference ref, int width, int height, SanitizeMode sanitize) {95 90 final String namespace = ref.source.getPrefName(); 96 91 ImageIcon i = new ImageProvider(ref.iconName) 97 92 .setDirs(getIconSourceDirs(ref.source)) … … 99 94 .setArchive(ref.source.zipIcons) 100 95 .setWidth(width) 101 96 .setHeight(height) 102 .setSanitize(sanitize)103 97 .setOptional(true).get(); 104 98 if(i == null) 105 99 { … … 109 103 return i; 110 104 } 111 105 112 public static ImageIcon getNoIcon_Icon(StyleSource source) {113 return getNoIcon_Icon(source, SanitizeMode.OFF);114 }115 116 106 /** 117 107 * No icon with the given name was found, show a dummy icon instead 118 108 * @return the icon misc/no_icon.png, in descending priority: … … 121 111 * - josm's default icon 122 112 * can be null if the defaults are turned off by user 123 113 */ 124 public static ImageIcon getNoIcon_Icon(StyleSource source , SanitizeMode sanitize) {114 public static ImageIcon getNoIcon_Icon(StyleSource source) { 125 115 return new ImageProvider("misc/no_icon.png") 126 116 .setDirs(getIconSourceDirs(source)) 127 117 .setId("mappaint."+source.getPrefName()) 128 118 .setArchive(source.zipIcons) 129 .setSanitize(sanitize)130 119 .setOptional(true).get(); 131 120 } 132 121