Changeset 10358 in josm for trunk/src/org/openstreetmap/josm/tools
- Timestamp:
- 2016-06-12T12:00:38+02:00 (8 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/ImageProvider.java
r10308 r10358 69 69 import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset; 70 70 import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets; 71 import org.openstreetmap.josm.gui.util.GuiSizesHelper; 71 72 import org.openstreetmap.josm.io.CachedFile; 72 73 import org.openstreetmap.josm.plugins.PluginHandler; … … 145 146 CURSOR(Main.pref.getInteger("iconsize.cursor", 32)), 146 147 /** cursor overlay icon size */ 147 CURSOROVERLAY(CURSOR .imageSize),148 CURSOROVERLAY(CURSOR), 148 149 /** menu icon size */ 149 MENU(SMALLICON .imageSize),150 MENU(SMALLICON), 150 151 /** menu icon size in popup menus 151 152 * @since 8323 152 153 */ 153 POPUPMENU(LARGEICON .imageSize),154 POPUPMENU(LARGEICON), 154 155 /** Layer list icon size 155 156 * @since 8323 … … 159 160 * @since 9253 160 161 */ 161 TOOLBAR(LARGEICON .imageSize),162 TOOLBAR(LARGEICON), 162 163 /** Side button maximum height 163 164 * @since 9253 164 165 */ 165 166 SIDEBUTTON(Main.pref.getInteger("iconsize.sidebutton", 20)), 167 /** Settings tab icon size 168 * @since 9253 169 */ 170 SETTINGS_TAB(Main.pref.getInteger("iconsize.settingstab", 48)), 166 171 /** 167 172 * The default image size 168 173 * @since 9705 169 174 */ 170 DEFAULT(Main.pref.getInteger("iconsize.default", 24)); 171 172 private final int imageSize; 175 DEFAULT(Main.pref.getInteger("iconsize.default", 24)), 176 /** 177 * Splash dialog logo size 178 * @since 10358 179 */ 180 SPLASH_LOGO(128, 129), 181 /** 182 * About dialog logo size 183 * @since 10358 184 */ 185 ABOUT_LOGO(256, 258); 186 187 private final int virtualWidth; 188 private final int virtualHeight; 173 189 174 190 ImageSizes(int imageSize) { 175 this.imageSize = imageSize; 191 this.virtualWidth = imageSize; 192 this.virtualHeight = imageSize; 193 } 194 195 ImageSizes(int width, int height) { 196 this.virtualWidth = width; 197 this.virtualHeight = height; 198 } 199 200 ImageSizes(ImageSizes that) { 201 this.virtualWidth = that.virtualWidth; 202 this.virtualHeight = that.virtualHeight; 176 203 } 177 204 178 205 /** 179 * Returns the image size in pixels 180 * @return the image size in pixels 206 * Returns the image width in virtual pixels 207 * @return the image width in virtual pixels 208 */ 209 public int getVirtualWidth() { 210 return virtualWidth; 211 } 212 213 /** 214 * Returns the image height in virtual pixels 215 * @return the image height in virtual pixels 181 216 * @since 9705 182 217 */ 183 public int get ImageSize() {184 return imageSize;218 public int getVirtualHeight() { 219 return virtualHeight; 185 220 } 186 221 … … 191 226 */ 192 227 public Dimension getImageDimension() { 193 return new Dimension( imageSize, imageSize);228 return new Dimension(virtualWidth, virtualHeight); 194 229 } 195 230 } … … 219 254 /** directory inside the archive */ 220 255 protected String inArchiveDir; 221 /** width of the resulting image, -1 when original image data should be used */222 protected int width = -1;223 /** height of the resulting image, -1 when original image data should be used */224 protected int height = -1;225 /** maximum width of the resulting image, -1 for no restriction */226 protected int maxWidth = -1;227 /** maximum height of the resulting image, -1 for no restriction */228 protected int maxHeight = -1;256 /** virtual width of the resulting image, -1 when original image data should be used */ 257 protected int virtualWidth = -1; 258 /** virtual height of the resulting image, -1 when original image data should be used */ 259 protected int virtualHeight = -1; 260 /** virtual maximum width of the resulting image, -1 for no restriction */ 261 protected int virtualMaxWidth = -1; 262 /** virtual maximum height of the resulting image, -1 for no restriction */ 263 protected int virtualMaxHeight = -1; 229 264 /** In case of errors do not throw exception but return <code>null</code> for missing image */ 230 265 protected boolean optional; … … 306 341 this.archive = image.archive; 307 342 this.inArchiveDir = image.inArchiveDir; 308 this. width = image.width;309 this. height = image.height;310 this. maxWidth = image.maxWidth;311 this. maxHeight = image.maxHeight;343 this.virtualWidth = image.virtualWidth; 344 this.virtualHeight = image.virtualHeight; 345 this.virtualMaxWidth = image.virtualMaxWidth; 346 this.virtualMaxHeight = image.virtualMaxHeight; 312 347 this.optional = image.optional; 313 348 this.suppressWarnings = image.suppressWarnings; … … 389 424 */ 390 425 public ImageProvider setSize(Dimension size) { 391 this. width = size.width;392 this. height = size.height;426 this.virtualWidth = size.width; 427 this.virtualHeight = size.height; 393 428 return this; 394 429 } … … 407 442 408 443 /** 444 * Set the dimensions of the image. 445 * 446 * @param width final width of the image 447 * @param height final height of the image 448 * @return the current object, for convenience 449 * @since 10358 450 */ 451 public ImageProvider setSize(int width, int height) { 452 this.virtualWidth = width; 453 this.virtualHeight = height; 454 return this; 455 } 456 457 /** 409 458 * Set image width 410 459 * @param width final width of the image … … 413 462 */ 414 463 public ImageProvider setWidth(int width) { 415 this. width = width;464 this.virtualWidth = width; 416 465 return this; 417 466 } … … 424 473 */ 425 474 public ImageProvider setHeight(int height) { 426 this. height = height;475 this.virtualHeight = height; 427 476 return this; 428 477 } … … 439 488 */ 440 489 public ImageProvider setMaxSize(Dimension maxSize) { 441 this. maxWidth = maxSize.width;442 this. maxHeight = maxSize.height;490 this.virtualMaxWidth = maxSize.width; 491 this.virtualMaxHeight = maxSize.height; 443 492 return this; 444 493 } … … 458 507 */ 459 508 public ImageProvider resetMaxSize(Dimension maxSize) { 460 if (this. maxWidth == -1 || maxSize.width < this.maxWidth) {461 this. maxWidth = maxSize.width;462 } 463 if (this. maxHeight == -1 || maxSize.height < this.maxHeight) {464 this. maxHeight = maxSize.height;509 if (this.virtualMaxWidth == -1 || maxSize.width < this.virtualMaxWidth) { 510 this.virtualMaxWidth = maxSize.width; 511 } 512 if (this.virtualMaxHeight == -1 || maxSize.height < this.virtualMaxHeight) { 513 this.virtualMaxHeight = maxSize.height; 465 514 } 466 515 return this; … … 498 547 */ 499 548 public ImageProvider setMaxWidth(int maxWidth) { 500 this. maxWidth = maxWidth;549 this.virtualMaxWidth = maxWidth; 501 550 return this; 502 551 } … … 509 558 */ 510 559 public ImageProvider setMaxHeight(int maxHeight) { 511 this. maxHeight = maxHeight;560 this.virtualMaxHeight = maxHeight; 512 561 return this; 513 562 } … … 557 606 if (ir == null) 558 607 return null; 559 if ( maxWidth != -1 || maxHeight != -1)560 return ir.getImageIconBounded(new Dimension( maxWidth, maxHeight));608 if (virtualMaxWidth != -1 || virtualMaxHeight != -1) 609 return ir.getImageIconBounded(new Dimension(virtualMaxWidth, virtualMaxHeight)); 561 610 else 562 return ir.getImageIcon(new Dimension( width, height));611 return ir.getImageIcon(new Dimension(virtualWidth, virtualHeight)); 563 612 } 564 613 … … 656 705 657 706 /** 707 * Load an empty image with a given size. 708 * 709 * @param size Target icon size 710 * @return The requested Image. 711 * @since 10358 712 */ 713 public static ImageIcon getEmpty(ImageSizes size) { 714 Dimension iconRealSize = GuiSizesHelper.getDimensionDpiAdjusted(size.getImageDimension()); 715 return new ImageIcon(new BufferedImage(iconRealSize.width, iconRealSize.height, 716 BufferedImage.TYPE_INT_ARGB)); 717 } 718 719 /** 658 720 * Load an image with a given file name. 659 721 * … … 689 751 public static ImageIcon getIfAvailable(String name) { 690 752 return new ImageProvider(name).setOptional(true).get(); 753 } 754 755 /** 756 * Scale image to virtual dimensions. This util method is used to hide real sizes calculations. 757 * All other classes should use this method to resize images. 758 * 759 * @param im image to be resized 760 * @param virtualWidth target width of image in virtual pixels 761 * @param virtualHeight target height of image in virtual pixels 762 * @return new scaled image in real dimensions 763 */ 764 public static ImageIcon getScaledIcon(Image im, int virtualWidth, int virtualHeight) { 765 int realWidth = GuiSizesHelper.getSizeDpiAdjusted(virtualWidth); 766 int realHeight = GuiSizesHelper.getSizeDpiAdjusted(virtualHeight); 767 768 return new ImageIcon(im.getScaledInstance(realWidth, realHeight, Image.SCALE_SMOOTH)); 691 769 } 692 770 … … 1333 1411 MapImage icon = nodeStyle.mapImage; 1334 1412 if (icon != null) { 1335 int background Width = iconSize.width;1336 int background Height = iconSize.height;1337 int icon Width = icon.getWidth();1338 int icon Height = icon.getHeight();1339 BufferedImage image = new BufferedImage(background Width, backgroundHeight,1413 int backgroundRealWidth = GuiSizesHelper.getSizeDpiAdjusted(iconSize.width); 1414 int backgroundRealHeight = GuiSizesHelper.getSizeDpiAdjusted(iconSize.height); 1415 int iconRealWidth = icon.getWidth(); 1416 int iconRealHeight = icon.getHeight(); 1417 BufferedImage image = new BufferedImage(backgroundRealWidth, backgroundRealHeight, 1340 1418 BufferedImage.TYPE_INT_ARGB); 1341 double scaleFactor = Math.min(background Width / (double) iconWidth, backgroundHeight1342 / (double) icon Height);1419 double scaleFactor = Math.min(backgroundRealWidth / (double) iconRealWidth, backgroundRealHeight 1420 / (double) iconRealHeight); 1343 1421 BufferedImage iconImage = icon.getImage(false); 1344 1422 Image scaledIcon; … … 1347 1425 if (scaleFactor < 1) { 1348 1426 // Scale icon such that it fits on background. 1349 scaledWidth = (int) (icon Width * scaleFactor);1350 scaledHeight = (int) (icon Height * scaleFactor);1427 scaledWidth = (int) (iconRealWidth * scaleFactor); 1428 scaledHeight = (int) (iconRealHeight * scaleFactor); 1351 1429 scaledIcon = iconImage.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_SMOOTH); 1352 1430 } else { 1353 1431 // Use original size, don't upscale. 1354 scaledWidth = icon Width;1355 scaledHeight = icon Height;1432 scaledWidth = iconRealWidth; 1433 scaledHeight = iconRealHeight; 1356 1434 scaledIcon = iconImage; 1357 1435 } 1358 image.getGraphics().drawImage(scaledIcon, (background Width - scaledWidth) / 2,1359 (background Height - scaledHeight) / 2, null);1436 image.getGraphics().drawImage(scaledIcon, (backgroundRealWidth - scaledWidth) / 2, 1437 (backgroundRealHeight - scaledHeight) / 2, null); 1360 1438 1361 1439 return new ImageIcon(image); … … 1394 1472 */ 1395 1473 public static BufferedImage createImageFromSvg(SVGDiagram svg, Dimension dim) { 1396 float realWidth = svg.getWidth();1397 float realHeight = svg.getHeight();1398 int width = Math.round(realWidth);1399 int height = Math.round(realHeight);1400 Double scaleX = null, scaleY = null;1474 float sourceWidth = svg.getWidth(); 1475 float sourceHeight = svg.getHeight(); 1476 int realWidth = Math.round(GuiSizesHelper.getSizeDpiAdjusted(sourceWidth)); 1477 int realHeight = Math.round(GuiSizesHelper.getSizeDpiAdjusted(sourceHeight)); 1478 Double scaleX, scaleY; 1401 1479 if (dim.width != -1) { 1402 width = dim.width;1403 scaleX = (double) width / realWidth;1480 realWidth = dim.width; 1481 scaleX = (double) realWidth / sourceWidth; 1404 1482 if (dim.height == -1) { 1405 1483 scaleY = scaleX; 1406 height = (int) Math.round(realHeight * scaleY);1484 realHeight = (int) Math.round(sourceHeight * scaleY); 1407 1485 } else { 1408 height = dim.height;1409 scaleY = (double) height / realHeight;1486 realHeight = dim.height; 1487 scaleY = (double) realHeight / sourceHeight; 1410 1488 } 1411 1489 } else if (dim.height != -1) { 1412 height = dim.height; 1413 scaleX = scaleY = (double) height / realHeight; 1414 width = (int) Math.round(realWidth * scaleX); 1415 } 1416 if (width == 0 || height == 0) { 1490 realHeight = dim.height; 1491 scaleX = scaleY = (double) realHeight / sourceHeight; 1492 realWidth = (int) Math.round(sourceWidth * scaleX); 1493 } 1494 else { 1495 scaleX = scaleY = (double) realHeight / sourceHeight; 1496 } 1497 1498 if (realWidth == 0 || realHeight == 0) { 1417 1499 return null; 1418 1500 } 1419 BufferedImage img = new BufferedImage( width, height, BufferedImage.TYPE_INT_ARGB);1501 BufferedImage img = new BufferedImage(realWidth, realHeight, BufferedImage.TYPE_INT_ARGB); 1420 1502 Graphics2D g = img.createGraphics(); 1421 g.setClip(0, 0, width, height); 1422 if (scaleX != null && scaleY != null) { 1423 g.scale(scaleX, scaleY); 1424 } 1503 g.setClip(0, 0, realWidth, realHeight); 1504 g.scale(scaleX, scaleY); 1425 1505 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 1426 1506 try { -
trunk/src/org/openstreetmap/josm/tools/ImageResource.java
r10356 r10358 14 14 15 15 import com.kitfox.svg.SVGDiagram; 16 import org.openstreetmap.josm.gui.util.GuiSizesHelper; 16 17 17 18 /** … … 50 51 CheckParameterUtil.ensureParameterNotNull(img); 51 52 this.baseImage = img; 53 54 img = scaleBaseImageIfNeeded(img); 55 52 56 imgCache.put(DEFAULT_DIMENSION, img); 57 } 58 59 /** Scale image according to screen DPI if needed. 60 * 61 * @param img an image loaded from file (it's width and height are virtual pixels) 62 * @return original img if virtual size is the same as real size or new image resized to real pixels 63 */ 64 private static Image scaleBaseImageIfNeeded(Image img) { 65 int imgWidth = img.getWidth(null); 66 int imgHeight = img.getHeight(null); 67 int realWidth = GuiSizesHelper.getSizeDpiAdjusted(imgWidth); 68 int realHeight = GuiSizesHelper.getSizeDpiAdjusted(imgHeight); 69 if (realWidth != -1 && realHeight != -1 && imgWidth != realWidth && imgHeight != realHeight) { 70 Image realImage = img.getScaledInstance(realWidth, realHeight, Image.SCALE_SMOOTH); 71 BufferedImage bimg = new BufferedImage(realWidth, realHeight, BufferedImage.TYPE_INT_ARGB); 72 bimg.getGraphics().drawImage(realImage, 0, 0, null); 73 img = bimg; 74 } 75 return img; 53 76 } 54 77 … … 88 111 */ 89 112 public void getImageIcon(AbstractAction a) { 90 ImageIcon icon = getImageIconBounded(ImageProvider.ImageSizes.SMALLICON.getImageDimension()); 113 Dimension iconDimension = ImageProvider.ImageSizes.SMALLICON.getImageDimension(); 114 ImageIcon icon = getImageIconBounded(iconDimension); 91 115 a.putValue(Action.SMALL_ICON, icon); 92 icon = getImageIconBounded(ImageProvider.ImageSizes.LARGEICON.getImageDimension()); 116 117 iconDimension = ImageProvider.ImageSizes.LARGEICON.getImageDimension(); 118 icon = getImageIconBounded(iconDimension); 93 119 a.putValue(Action.LARGE_ICON_KEY, icon); 94 120 a.putValue("ImageResource", this); … … 123 149 } 124 150 if (svg != null) { 125 BufferedImage bimg = ImageProvider.createImageFromSvg(svg, dim); 151 Dimension realDim = GuiSizesHelper.getDimensionDpiAdjusted(dim); 152 BufferedImage bimg = ImageProvider.createImageFromSvg(svg, realDim); 126 153 if (bimg == null) { 127 154 return null; … … 137 164 if (baseImage == null) throw new AssertionError(); 138 165 139 int width = dim.width;140 int height = dim.height;166 int realWidth = GuiSizesHelper.getSizeDpiAdjusted(dim.width); 167 int realHeight = GuiSizesHelper.getSizeDpiAdjusted(dim.height); 141 168 ImageIcon icon = new ImageIcon(baseImage); 142 if ( width == -1 && height == -1) {143 width = icon.getIconWidth();144 height = icon.getIconHeight();145 } else if ( width == -1) {146 width = Math.max(1, icon.getIconWidth() * height / icon.getIconHeight());147 } else if ( height == -1) {148 height = Math.max(1, icon.getIconHeight() * width / icon.getIconWidth());149 } 150 Image i = icon.getImage().getScaledInstance( width, height, Image.SCALE_SMOOTH);151 BufferedImage bimg = new BufferedImage( width, height, BufferedImage.TYPE_INT_ARGB);169 if (realWidth == -1 && realHeight == -1) { 170 realWidth = GuiSizesHelper.getSizeDpiAdjusted(icon.getIconWidth()); 171 realHeight = GuiSizesHelper.getSizeDpiAdjusted(icon.getIconHeight()); 172 } else if (realWidth == -1) { 173 realWidth = Math.max(1, icon.getIconWidth() * realHeight / icon.getIconHeight()); 174 } else if (realHeight == -1) { 175 realHeight = Math.max(1, icon.getIconHeight() * realWidth / icon.getIconWidth()); 176 } 177 Image i = icon.getImage().getScaledInstance(realWidth, realHeight, Image.SCALE_SMOOTH); 178 BufferedImage bimg = new BufferedImage(realWidth, realHeight, BufferedImage.TYPE_INT_ARGB); 152 179 bimg.getGraphics().drawImage(i, 0, 0, null); 153 180 if (overlayInfo != null) { … … 172 199 if (maxSize.width < -1 || maxSize.width == 0 || maxSize.height < -1 || maxSize.height == 0) 173 200 throw new IllegalArgumentException(maxSize+" is invalid"); 174 float realWidth;175 float realHeight;201 float sourceWidth; 202 float sourceHeight; 176 203 int maxWidth = maxSize.width; 177 204 int maxHeight = maxSize.height; 178 205 if (svg != null) { 179 realWidth = svg.getWidth();180 realHeight = svg.getHeight();206 sourceWidth = svg.getWidth(); 207 sourceHeight = svg.getHeight(); 181 208 } else { 182 209 if (baseImage == null) throw new AssertionError(); 183 210 ImageIcon icon = new ImageIcon(baseImage); 184 realWidth = icon.getIconWidth();185 realHeight = icon.getIconHeight();186 if ( realWidth <= maxWidth) {211 sourceWidth = icon.getIconWidth(); 212 sourceHeight = icon.getIconHeight(); 213 if (sourceWidth <= maxWidth) { 187 214 maxWidth = -1; 188 215 } 189 if ( realHeight <= maxHeight) {216 if (sourceHeight <= maxHeight) { 190 217 maxHeight = -1; 191 218 } … … 198 225 else if (maxHeight == -1) 199 226 return getImageIcon(new Dimension(maxWidth, -1)); 200 else if ( realWidth / maxWidth > realHeight / maxHeight)227 else if (sourceWidth / maxWidth > sourceHeight / maxHeight) 201 228 return getImageIcon(new Dimension(maxWidth, -1)); 202 229 else
Note:
See TracChangeset
for help on using the changeset viewer.