Ticket #9995: hidpi_patch_1_images.patch

File hidpi_patch_1_images.patch, 36.1 KB (added by strump, 8 years ago)
  • gui/dialogs/NotesDialog.java

     
    6161    public static final ImageIcon ICON_OPEN = ImageProvider.get("dialogs/notes", "note_open");
    6262    /** 16x16 icon for unresolved notes */
    6363    public static final ImageIcon ICON_OPEN_SMALL =
    64             new ImageIcon(ICON_OPEN.getImage().getScaledInstance(ICON_SMALL_SIZE, ICON_SMALL_SIZE, Image.SCALE_SMOOTH));
     64            new ImageProvider("dialogs/notes", "note_open").setSize(ICON_SMALL_SIZE, ICON_SMALL_SIZE).get();
    6565    /** 24x24 icon for resolved notes */
    6666    public static final ImageIcon ICON_CLOSED = ImageProvider.get("dialogs/notes", "note_closed");
    6767    /** 16x16 icon for resolved notes */
    6868    public static final ImageIcon ICON_CLOSED_SMALL =
    69             new ImageIcon(ICON_CLOSED.getImage().getScaledInstance(ICON_SMALL_SIZE, ICON_SMALL_SIZE, Image.SCALE_SMOOTH));
     69            new ImageProvider("dialogs/notes", "note_closed").setSize(ICON_SMALL_SIZE, ICON_SMALL_SIZE).get();
    7070    /** 24x24 icon for new notes */
    7171    public static final ImageIcon ICON_NEW = ImageProvider.get("dialogs/notes", "note_new");
    7272    /** 16x16 icon for new notes */
    7373    public static final ImageIcon ICON_NEW_SMALL =
    74             new ImageIcon(ICON_NEW.getImage().getScaledInstance(ICON_SMALL_SIZE, ICON_SMALL_SIZE, Image.SCALE_SMOOTH));
     74            new ImageProvider("dialogs/notes", "note_new").setSize(ICON_SMALL_SIZE, ICON_SMALL_SIZE).get();
    7575    /** Icon for note comments */
    7676    public static final ImageIcon ICON_COMMENT = ImageProvider.get("dialogs/notes", "note_comment");
    7777
  • gui/dialogs/ToggleDialog.java

     
    3333import javax.swing.AbstractAction;
    3434import javax.swing.BorderFactory;
    3535import javax.swing.ButtonGroup;
     36import javax.swing.ImageIcon;
    3637import javax.swing.JButton;
    3738import javax.swing.JCheckBoxMenuItem;
    3839import javax.swing.JComponent;
     
    503504            add(lblMinimized);
    504505
    505506            // scale down the dialog icon
    506             lblTitle = new JLabel("", new ImageProvider("dialogs", iconName).setWidth(16).get(), JLabel.TRAILING);
     507            ImageIcon icon = new ImageProvider("dialogs", iconName).setSize(ImageProvider.ImageSizes.SMALLICON).get();
     508            lblTitle = new JLabel("", icon, JLabel.TRAILING);
    507509            lblTitle.setIconTextGap(8);
    508510
    509511            JPanel conceal = new JPanel();
     
    540542
    541543            // show the pref button if applicable
    542544            if (preferenceClass != null) {
    543                 JButton pref = new JButton(new ImageProvider("preference").setWidth(16).get());
     545                JButton pref = new JButton(new ImageProvider("preference").setSize(ImageProvider.ImageSizes.SMALLICON).get());
    544546                pref.setToolTipText(tr("Open preferences for this panel"));
    545547                pref.setBorder(BorderFactory.createEmptyBorder());
    546548                pref.addActionListener(
  • gui/io/SaveLayersDialog.java

     
    381381    }
    382382
    383383    final class SaveAndProceedAction extends AbstractAction implements PropertyChangeListener {
    384         private static final int ICON_SIZE = 24;
    385384        private static final String BASE_ICON = "BASE_ICON";
    386385        private final transient Image save = ImageProvider.get("save").getImage();
    387386        private final transient Image upld = ImageProvider.get("upload").getImage();
     387        private final transient int ICON_SIZE = save.getWidth(null);
    388388        private final transient Image saveDis = new BufferedImage(ICON_SIZE, ICON_SIZE, BufferedImage.TYPE_4BYTE_ABGR);
    389389        private final transient Image upldDis = new BufferedImage(ICON_SIZE, ICON_SIZE, BufferedImage.TYPE_4BYTE_ABGR);
    390390
  • gui/mappaint/MapPaintStyles.java

     
    146146     */
    147147    public static ImageIcon getIcon(IconReference ref, int width, int height) {
    148148        final String namespace = ref.source.getPrefName();
    149         ImageIcon i = getIconProvider(ref, false).setWidth(width).setHeight(height).get();
     149        ImageIcon i = getIconProvider(ref, false).setSize(width, height).get();
    150150        if (i == null) {
    151151            Main.warn("Mappaint style \""+namespace+"\" ("+ref.source.getDisplayString()+") icon \"" + ref.iconName + "\" not found.");
    152152            return null;
  • gui/preferences/PreferenceTabbedPane.java

     
    509509                if (expert || !tps.isExpert()) {
    510510                    // Get icon
    511511                    String iconName = tps.getIconName();
    512                     ImageIcon icon = iconName != null && !iconName.isEmpty() ? ImageProvider.get("preferences", iconName) : null;
    513                     // See #6985 - Force icons to be 48x48 pixels
    514                     if (icon != null && (icon.getIconHeight() != 48 || icon.getIconWidth() != 48)) {
    515                         icon = new ImageIcon(icon.getImage().getScaledInstance(48, 48, Image.SCALE_DEFAULT));
     512                    ImageIcon icon = null;
     513
     514                    if(iconName != null && !iconName.isEmpty()) {
     515                        icon = new ImageProvider("preferences", iconName).setSize(ImageProvider.ImageSizes.SETTINGS_TAB).get();
    516516                    }
    517517                    if (settingsInitialized.contains(tps)) {
    518518                        // If it has been initialized, add corresponding tab(s)
  • gui/SideButton.java

     
    2525 * @since 744
    2626 */
    2727public class SideButton extends JButton implements Destroyable {
    28     private static final int iconHeight = ImageProvider.ImageSizes.SIDEBUTTON.getImageSize();
     28    private static final int iconHeight = ImageProvider.ImageSizes.SIDEBUTTON.getVirtualHeight();
    2929
    3030    private transient PropertyChangeListener propertyChangeListener;
    3131
     
    6060     */
    6161    public SideButton(Action action, String imagename) {
    6262        super(action);
    63         setIcon(getScaledImage(ImageProvider.get("dialogs", imagename).getImage()));
     63        Icon icon = new ImageProvider("dialogs", imagename).setHeight(iconHeight).get();
     64        setIcon(icon);
    6465        doStyle();
    6566    }
    6667
     
    9192     */
    9293    private static ImageIcon getScaledImage(Image im) {
    9394        int newWidth = im.getWidth(null) *  iconHeight / im.getHeight(null);
    94         return new ImageIcon(im.getScaledInstance(newWidth, iconHeight, Image.SCALE_SMOOTH));
     95        return ImageProvider.getScaledIcon(im, newWidth, iconHeight);
    9596    }
    9697
    9798    private void doStyle() {
  • gui/SplashScreen.java

     
    88import java.awt.Dimension;
    99import java.awt.GridBagConstraints;
    1010import java.awt.GridBagLayout;
    11 import java.awt.Image;
    1211import java.awt.Insets;
    1312import java.awt.event.MouseAdapter;
    1413import java.awt.event.MouseEvent;
     
    1716import java.util.concurrent.CopyOnWriteArrayList;
    1817
    1918import javax.swing.BorderFactory;
    20 import javax.swing.ImageIcon;
    2119import javax.swing.JFrame;
    2220import javax.swing.JLabel;
    2321import javax.swing.JPanel;
     
    6967        contentPane.add(innerContentPane);
    7068
    7169        // Add the logo
    72         JLabel logo = new JLabel(new ImageIcon(ImageProvider.get("logo.svg").getImage().getScaledInstance(128, 129, Image.SCALE_SMOOTH)));
     70        JLabel logo = new JLabel(new ImageProvider("logo.svg").setSize(ImageProvider.ImageSizes.SPLASH_LOGO).get());
    7371        GridBagConstraints gbc = new GridBagConstraints();
    7472        gbc.gridheight = 2;
    7573        gbc.insets = new Insets(0, 0, 0, 70);
  • gui/widgets/TextContextualPopupMenu.java

     
    179179            JMenuItem mi = new JMenuItem(action);
    180180            mi.setText(label);
    181181            if (iconName != null && Main.pref.getBoolean("text.popupmenu.useicons", true)) {
    182                 ImageIcon icon = new ImageProvider(iconName).setWidth(16).get();
     182                ImageIcon icon = new ImageProvider(iconName).setSize(ImageProvider.ImageSizes.SMALLICON).get();
    183183                if (icon != null) {
    184184                    mi.setIcon(icon);
    185185                }
  • io/session/GenericSessionExporter.java

     
    7373         * Constructs a new {@code LayerSaveAction}.
    7474         */
    7575        LayerSaveAction() {
    76             putValue(SMALL_ICON, new ImageProvider("save").setWidth(16).get());
     76            putValue(SMALL_ICON, new ImageProvider("save").setSize(ImageProvider.ImageSizes.SMALLICON).get());
    7777            putValue(SHORT_DESCRIPTION, ((AbstractModifiableLayer) layer).requiresSaveToFile() ?
    7878                    tr("Layer contains unsaved data - save to file.") :
    7979                    tr("Layer does not contain unsaved data."));
  • plugins/PluginInformation.java

     
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.awt.Image;
     6import java.awt.Dimension;
    77import java.awt.image.BufferedImage;
    88import java.io.File;
    99import java.io.FileInputStream;
     
    2828
    2929import org.openstreetmap.josm.Main;
    3030import org.openstreetmap.josm.data.Version;
     31import org.openstreetmap.josm.gui.util.GuiSizesHelper;
    3132import org.openstreetmap.josm.tools.ImageProvider;
    3233import org.openstreetmap.josm.tools.LanguageInfo;
    3334import org.openstreetmap.josm.tools.Utils;
     
    8485    /** All manifest attributes. */
    8586    public final Map<String, String> attr = new TreeMap<>();
    8687
    87     private static final ImageIcon emptyIcon = new ImageIcon(new BufferedImage(24, 24, BufferedImage.TYPE_INT_ARGB));
     88    private static final Dimension iconRealSize =
     89            GuiSizesHelper.getDimensionDpiAdjusted(ImageProvider.ImageSizes.LARGEICON.getImageDimension());
     90    private static final ImageIcon emptyIcon =
     91            new ImageIcon(new BufferedImage(iconRealSize.width, iconRealSize.height, BufferedImage.TYPE_INT_ARGB));
    8892
    8993    /**
    9094     * Creates a plugin information object by reading the plugin information from
     
    243247        if (iconPath != null) {
    244248            if (file != null) {
    245249                // extract icon from the plugin jar file
    246                 icon = new ImageProvider(iconPath).setArchive(file).setMaxWidth(24).setMaxHeight(24).setOptional(true).get();
     250                icon = new ImageProvider(iconPath).setArchive(file).setMaxSize(ImageProvider.ImageSizes.LARGEICON).setOptional(true).get();
    247251            } else if (iconPath.startsWith("data:")) {
    248                 icon = new ImageProvider(iconPath).setMaxWidth(24).setMaxHeight(24).setOptional(true).get();
     252                icon = new ImageProvider(iconPath).setMaxSize(ImageProvider.ImageSizes.LARGEICON).setOptional(true).get();
    249253            }
    250254        }
    251255        canloadatruntime = Boolean.parseBoolean(attr.getValue("Plugin-Canloadatruntime"));
     
    499503    public ImageIcon getScaledIcon() {
    500504        if (icon == null)
    501505            return emptyIcon;
    502         return new ImageIcon(icon.getImage().getScaledInstance(24, 24, Image.SCALE_SMOOTH));
     506        return ImageProvider.getScaledIcon(icon, ImageProvider.ImageSizes.LARGEICON);
    503507    }
    504508
    505509    @Override
  • tools/ImageProvider.java

     
    6868import org.openstreetmap.josm.gui.mappaint.styleelement.StyleElement;
    6969import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
    7070import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
     71import org.openstreetmap.josm.gui.util.GuiSizesHelper;
    7172import org.openstreetmap.josm.io.CachedFile;
    7273import org.openstreetmap.josm.plugins.PluginHandler;
    7374import org.w3c.dom.Element;
     
    144145        /** cursor icon size */
    145146        CURSOR(Main.pref.getInteger("iconsize.cursor", 32)),
    146147        /** cursor overlay icon size */
    147         CURSOROVERLAY(CURSOR.imageSize),
     148        CURSOROVERLAY(CURSOR),
    148149        /** menu icon size */
    149         MENU(SMALLICON.imageSize),
     150        MENU(SMALLICON),
    150151        /** menu icon size in popup menus
    151152         * @since 8323
    152153         */
    153         POPUPMENU(LARGEICON.imageSize),
     154        POPUPMENU(LARGEICON),
    154155        /** Layer list icon size
    155156         * @since 8323
    156157         */
     
    158159        /** Toolbar button icon size
    159160         * @since 9253
    160161         */
    161         TOOLBAR(LARGEICON.imageSize),
     162        TOOLBAR(LARGEICON),
    162163        /** Side button maximum height
    163164         * @since 9253
    164165         */
    165166        SIDEBUTTON(Main.pref.getInteger("iconsize.sidebutton", 20)),
     167        /** Side button maximum height
     168         * @since 9253
     169         */
     170        SETTINGS_TAB(Main.pref.getInteger("iconsize.settingstab", 48)),
    166171        /**
    167172         * The default image size
    168173         * @since 9705
    169174         */
    170         DEFAULT(Main.pref.getInteger("iconsize.default", 24));
     175        DEFAULT(Main.pref.getInteger("iconsize.default", 24)),
    171176
    172         private final int imageSize;
     177        /**
     178         * Splash dialog logo size
     179         * @since XXXX
     180         */
     181        SPLASH_LOGO(128, 129),
    173182
     183        /**
     184         * About dialog logo size
     185         * @since XXXX
     186         */
     187        ABOUT_LOGO(256, 258);
     188
     189        private final int virtualWidth;
     190        private final int virtualHeight;
     191
    174192        ImageSizes(int imageSize) {
    175             this.imageSize = imageSize;
     193            this.virtualWidth = imageSize;
     194            this.virtualHeight = imageSize;
    176195        }
    177196
     197        ImageSizes(int width, int height) {
     198            this.virtualWidth = width;
     199            this.virtualHeight = height;
     200        }
     201
     202        ImageSizes(ImageSizes that) {
     203            this.virtualWidth = that.virtualWidth;
     204            this.virtualHeight = that.virtualHeight;
     205        }
     206
    178207        /**
    179          * Returns the image size in pixels
    180          * @return the image size in pixels
     208         * Returns the image width in virtual pixels
     209         * @return the image width in virtual pixels
     210         */
     211        public int getVirtualWidth() {
     212            return virtualWidth;
     213        }
     214
     215        /**
     216         * Returns the image height in virtual pixels
     217         * @return the image height in virtual pixels
    181218         * @since 9705
    182219         */
    183         public int getImageSize() {
    184             return imageSize;
     220        public int getVirtualHeight() {
     221            return virtualHeight;
    185222        }
    186223
    187224        /**
     
    190227         * @since 9705
    191228         */
    192229        public Dimension getImageDimension() {
    193             return new Dimension(imageSize, imageSize);
     230            return new Dimension(virtualWidth, virtualHeight);
    194231        }
    195232    }
    196233
     
    218255    protected File archive;
    219256    /** directory inside the archive */
    220257    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;
     258    /** virtual width of the resulting image, -1 when original image data should be used */
     259    protected int virtualWidth = -1;
     260    /** virtual height of the resulting image, -1 when original image data should be used */
     261    protected int virtualHeight = -1;
     262    /** virtual maximum width of the resulting image, -1 for no restriction */
     263    protected int virtualMaxWidth = -1;
     264    /** virtual maximum height of the resulting image, -1 for no restriction */
     265    protected int virtualMaxHeight = -1;
    229266    /** In case of errors do not throw exception but return <code>null</code> for missing image */
    230267    protected boolean optional;
    231268    /** <code>true</code> if warnings should be suppressed */
     
    305342        this.name = image.name;
    306343        this.archive = image.archive;
    307344        this.inArchiveDir = image.inArchiveDir;
    308         this.width = image.width;
    309         this.height = image.height;
    310         this.maxWidth = image.maxWidth;
    311         this.maxHeight = image.maxHeight;
     345        this.virtualWidth = image.virtualWidth;
     346        this.virtualHeight = image.virtualHeight;
     347        this.virtualMaxWidth = image.virtualMaxWidth;
     348        this.virtualMaxHeight = image.virtualMaxHeight;
    312349        this.optional = image.optional;
    313350        this.suppressWarnings = image.suppressWarnings;
    314351        this.additionalClassLoaders = image.additionalClassLoaders;
     
    388425     * @return the current object, for convenience
    389426     */
    390427    public ImageProvider setSize(Dimension size) {
    391         this.width = size.width;
    392         this.height = size.height;
     428        this.virtualWidth = size.width;
     429        this.virtualHeight = size.height;
    393430        return this;
    394431    }
    395432
     
    405442        return setSize(size.getImageDimension());
    406443    }
    407444
     445    public ImageProvider setSize(int width, int height) {
     446        this.virtualWidth = width;
     447        this.virtualHeight = height;
     448        return this;
     449    }
     450
    408451    /**
    409452     * Set image width
    410453     * @param width final width of the image
     
    412455     * @see #setSize
    413456     */
    414457    public ImageProvider setWidth(int width) {
    415         this.width = width;
     458        this.virtualWidth = width;
    416459        return this;
    417460    }
    418461
     
    423466     * @see #setSize
    424467     */
    425468    public ImageProvider setHeight(int height) {
    426         this.height = height;
     469        this.virtualHeight = height;
    427470        return this;
    428471    }
    429472
     
    438481     * @return the current object, for convenience
    439482     */
    440483    public ImageProvider setMaxSize(Dimension maxSize) {
    441         this.maxWidth = maxSize.width;
    442         this.maxHeight = maxSize.height;
     484        this.virtualMaxWidth = maxSize.width;
     485        this.virtualMaxHeight = maxSize.height;
    443486        return this;
    444487    }
    445488
     
    457500     * @see #setMaxSize(Dimension)
    458501     */
    459502    public ImageProvider resetMaxSize(Dimension maxSize) {
    460         if (this.maxWidth == -1 || maxSize.width < this.maxWidth) {
    461             this.maxWidth = maxSize.width;
     503        if (this.virtualMaxWidth == -1 || maxSize.width < this.virtualMaxWidth) {
     504            this.virtualMaxWidth = maxSize.width;
    462505        }
    463         if (this.maxHeight == -1 || maxSize.height < this.maxHeight) {
    464             this.maxHeight = maxSize.height;
     506        if (this.virtualMaxHeight == -1 || maxSize.height < this.virtualMaxHeight) {
     507            this.virtualMaxHeight = maxSize.height;
    465508        }
    466509        return this;
    467510    }
     
    497540     * @see #setMaxSize
    498541     */
    499542    public ImageProvider setMaxWidth(int maxWidth) {
    500         this.maxWidth = maxWidth;
     543        this.virtualMaxWidth = maxWidth;
    501544        return this;
    502545    }
    503546
     
    508551     * @see #setMaxSize
    509552     */
    510553    public ImageProvider setMaxHeight(int maxHeight) {
    511         this.maxHeight = maxHeight;
     554        this.virtualMaxHeight = maxHeight;
    512555        return this;
    513556    }
    514557
     
    556599        ImageResource ir = getResource();
    557600        if (ir == null)
    558601            return null;
    559         if (maxWidth != -1 || maxHeight != -1)
    560             return ir.getImageIconBounded(new Dimension(maxWidth, maxHeight));
     602        if (virtualMaxWidth != -1 || virtualMaxHeight != -1)
     603            return ir.getImageIconBounded(new Dimension(virtualMaxWidth, virtualMaxHeight));
    561604        else
    562             return ir.getImageIcon(new Dimension(width, height));
     605            return ir.getImageIcon(new Dimension(virtualWidth, virtualHeight));
    563606    }
    564607
    565608    /**
     
    691734    }
    692735
    693736    /**
     737     * Scale image to imgSize dimensions. This method resizes image with respect to actual screen DPI.
     738     *
     739     * @param icon image to be resized
     740     * @param imgSize target size of image
     741     * @return new scaled image in real dimensions
     742     */
     743    public static ImageIcon getScaledIcon(ImageIcon icon, ImageSizes imgSize) {
     744        Dimension size = imgSize.getImageDimension();
     745        size = GuiSizesHelper.getDimensionDpiAdjusted(size);
     746        return new ImageIcon(icon.getImage().getScaledInstance(size.width, size.height, Image.SCALE_SMOOTH));
     747    }
     748
     749    /**
     750     * Scale image to virtual dimensions. This util method is used to hide real sizes calculations.
     751     * All other classes should use this method to resize images.
     752     *
     753     * @param im image to be resized
     754     * @param virtualWidth target width of image in virtual pixels
     755     * @param virtualHeight target height of image in virtual pixels
     756     * @return new scaled image in real dimensions
     757     */
     758    public static ImageIcon getScaledIcon(Image im, int virtualWidth, int virtualHeight) {
     759        int realWidth = GuiSizesHelper.getSizeDpiAdjusted(virtualWidth);
     760        int realHeight = GuiSizesHelper.getSizeDpiAdjusted(virtualHeight);
     761
     762        return new ImageIcon(im.getScaledInstance(realWidth, realHeight, Image.SCALE_SMOOTH));
     763    }
     764
     765    /**
    694766     * {@code data:[<mediatype>][;base64],<data>}
    695767     * @see <a href="http://tools.ietf.org/html/rfc2397">RFC2397</a>
    696768     */
     
    13321404                    NodeElement nodeStyle = (NodeElement) style;
    13331405                    MapImage icon = nodeStyle.mapImage;
    13341406                    if (icon != null) {
    1335                         int backgroundWidth = iconSize.width;
    1336                         int backgroundHeight = iconSize.height;
    1337                         int iconWidth = icon.getWidth();
    1338                         int iconHeight = icon.getHeight();
    1339                         BufferedImage image = new BufferedImage(backgroundWidth, backgroundHeight,
     1407                        int backgroundRealWidth = GuiSizesHelper.getSizeDpiAdjusted(iconSize.width);
     1408                        int backgroundRealHeight = GuiSizesHelper.getSizeDpiAdjusted(iconSize.height);
     1409                        int iconRealWidth = icon.getWidth();
     1410                        int iconRealHeight = icon.getHeight();
     1411                        BufferedImage image = new BufferedImage(backgroundRealWidth, backgroundRealHeight,
    13401412                                BufferedImage.TYPE_INT_ARGB);
    1341                         double scaleFactor = Math.min(backgroundWidth / (double) iconWidth, backgroundHeight
    1342                                 / (double) iconHeight);
     1413                        double scaleFactor = Math.min(backgroundRealWidth / (double) iconRealWidth, backgroundRealHeight
     1414                                / (double) iconRealHeight);
    13431415                        BufferedImage iconImage = icon.getImage(false);
    13441416                        Image scaledIcon;
    13451417                        final int scaledWidth;
     
    13461418                        final int scaledHeight;
    13471419                        if (scaleFactor < 1) {
    13481420                            // Scale icon such that it fits on background.
    1349                             scaledWidth = (int) (iconWidth * scaleFactor);
    1350                             scaledHeight = (int) (iconHeight * scaleFactor);
     1421                            scaledWidth = (int) (iconRealWidth * scaleFactor);
     1422                            scaledHeight = (int) (iconRealHeight * scaleFactor);
    13511423                            scaledIcon = iconImage.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_SMOOTH);
    13521424                        } else {
    13531425                            // Use original size, don't upscale.
    1354                             scaledWidth = iconWidth;
    1355                             scaledHeight = iconHeight;
     1426                            scaledWidth = iconRealWidth;
     1427                            scaledHeight = iconRealHeight;
    13561428                            scaledIcon = iconImage;
    13571429                        }
    1358                         image.getGraphics().drawImage(scaledIcon, (backgroundWidth - scaledWidth) / 2,
    1359                                 (backgroundHeight - scaledHeight) / 2, null);
     1430                        image.getGraphics().drawImage(scaledIcon, (backgroundRealWidth - scaledWidth) / 2,
     1431                                (backgroundRealHeight - scaledHeight) / 2, null);
    13601432
    13611433                        return new ImageIcon(image);
    13621434                    }
     
    13931465     * @return an image from the given SVG data at the desired dimension.
    13941466     */
    13951467    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;
     1468        float sourceWidth = svg.getWidth();
     1469        float sourceHeight = svg.getHeight();
     1470        int realWidth = Math.round(GuiSizesHelper.getSizeDpiAdjusted(sourceWidth));
     1471        int realHeight = Math.round(GuiSizesHelper.getSizeDpiAdjusted(sourceHeight));
     1472        Double scaleX, scaleY;
    14011473        if (dim.width != -1) {
    1402             width = dim.width;
    1403             scaleX = (double) width / realWidth;
     1474            realWidth = dim.width;
     1475            scaleX = (double) realWidth / sourceWidth;
    14041476            if (dim.height == -1) {
    14051477                scaleY = scaleX;
    1406                 height = (int) Math.round(realHeight * scaleY);
     1478                realHeight = (int) Math.round(sourceHeight * scaleY);
    14071479            } else {
    1408                 height = dim.height;
    1409                 scaleY = (double) height / realHeight;
     1480                realHeight = dim.height;
     1481                scaleY = (double) realHeight / sourceHeight;
    14101482            }
    14111483        } else if (dim.height != -1) {
    1412             height = dim.height;
    1413             scaleX = scaleY = (double) height / realHeight;
    1414             width = (int) Math.round(realWidth * scaleX);
     1484            realHeight = dim.height;
     1485            scaleX = scaleY = (double) realHeight / sourceHeight;
     1486            realWidth = (int) Math.round(sourceWidth * scaleX);
    14151487        }
    1416         if (width == 0 || height == 0) {
     1488        else {
     1489            scaleX = scaleY = (double) realHeight / sourceHeight;
     1490        }
     1491
     1492        if (realWidth == 0 || realHeight == 0) {
    14171493            return null;
    14181494        }
    1419         BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
     1495        BufferedImage img = new BufferedImage(realWidth, realHeight, BufferedImage.TYPE_INT_ARGB);
    14201496        Graphics2D g = img.createGraphics();
    1421         g.setClip(0, 0, width, height);
    1422         if (scaleX != null && scaleY != null) {
    1423             g.scale(scaleX, scaleY);
    1424         }
     1497        g.setClip(0, 0, realWidth, realHeight);
     1498        g.scale(scaleX, scaleY);
    14251499        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    14261500        try {
    14271501            synchronized (getSvgUniverse()) {
  • tools/ImageResource.java

     
    1313import javax.swing.ImageIcon;
    1414
    1515import com.kitfox.svg.SVGDiagram;
     16import org.openstreetmap.josm.gui.util.GuiSizesHelper;
    1617
    1718/**
    1819 * Holds data for one particular image.
     
    4950    public ImageResource(Image img) {
    5051        CheckParameterUtil.ensureParameterNotNull(img);
    5152        this.baseImage = img;
     53
     54        img = scaleBaseImageIfNeeded(img);
     55
    5256        imgCache.put(DEFAULT_DIMENSION, img);
    5357    }
    5458
     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;
     76    }
     77
    5578    /**
    5679     * Constructs a new {@code ImageResource} from SVG data.
    5780     * @param svg SVG data
     
    87110     * @since 7693
    88111     */
    89112    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);
    91115        a.putValue(Action.SMALL_ICON, icon);
    92         icon = getImageIconBounded(ImageProvider.ImageSizes.LARGEICON.getImageDimension());
     116
     117        iconDimension = ImageProvider.ImageSizes.LARGEICON.getImageDimension();
     118        icon = getImageIconBounded(iconDimension);
    93119        a.putValue(Action.LARGE_ICON_KEY, icon);
    94120    }
    95121
     
    108134            return new ImageIcon(img);
    109135        }
    110136        if (svg != null) {
    111             BufferedImage bimg = ImageProvider.createImageFromSvg(svg, dim);
     137            Dimension realDim = GuiSizesHelper.getDimensionDpiAdjusted(dim);
     138            BufferedImage bimg = ImageProvider.createImageFromSvg(svg, realDim);
    112139            if (bimg == null) {
    113140                return null;
    114141            }
     
    122149        } else {
    123150            if (baseImage == null) throw new AssertionError();
    124151
    125             int width = dim.width;
    126             int height = dim.height;
     152            int realWidth = GuiSizesHelper.getSizeDpiAdjusted(dim.width);
     153            int realHeight = GuiSizesHelper.getSizeDpiAdjusted(dim.height);
    127154            ImageIcon icon = new ImageIcon(baseImage);
    128             if (width == -1 && height == -1) {
    129                 width = icon.getIconWidth();
    130                 height = icon.getIconHeight();
    131             } else if (width == -1) {
    132                 width = Math.max(1, icon.getIconWidth() * height / icon.getIconHeight());
    133             } else if (height == -1) {
    134                 height = Math.max(1, icon.getIconHeight() * width / icon.getIconWidth());
     155            if (realWidth == -1 && realHeight == -1) {
     156                realWidth = GuiSizesHelper.getSizeDpiAdjusted(icon.getIconWidth());
     157                realHeight = GuiSizesHelper.getSizeDpiAdjusted(icon.getIconHeight());
     158            } else if (realWidth == -1) {
     159                realWidth = Math.max(1, icon.getIconWidth() * realHeight / icon.getIconHeight());
     160            } else if (realHeight == -1) {
     161                realHeight = Math.max(1, icon.getIconHeight() * realWidth / icon.getIconWidth());
    135162            }
    136             Image i = icon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
    137             BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
     163            Image i = icon.getImage().getScaledInstance(realWidth, realHeight, Image.SCALE_SMOOTH);
     164            BufferedImage bimg = new BufferedImage(realWidth, realHeight, BufferedImage.TYPE_INT_ARGB);
    138165            bimg.getGraphics().drawImage(i, 0, 0, null);
    139166            if (overlayInfo != null) {
    140167                for (ImageOverlay o : overlayInfo) {
     
    157184    public ImageIcon getImageIconBounded(Dimension maxSize) {
    158185        if (maxSize.width < -1 || maxSize.width == 0 || maxSize.height < -1 || maxSize.height == 0)
    159186            throw new IllegalArgumentException(maxSize+" is invalid");
    160         float realWidth;
    161         float realHeight;
     187        float sourceWidth;
     188        float sourceHeight;
    162189        int maxWidth = maxSize.width;
    163190        int maxHeight = maxSize.height;
    164191        if (svg != null) {
    165             realWidth = svg.getWidth();
    166             realHeight = svg.getHeight();
     192            sourceWidth = svg.getWidth();
     193            sourceHeight = svg.getHeight();
    167194        } else {
    168195            if (baseImage == null) throw new AssertionError();
    169196            ImageIcon icon = new ImageIcon(baseImage);
    170             realWidth = icon.getIconWidth();
    171             realHeight = icon.getIconHeight();
    172             if (realWidth <= maxWidth) {
     197            sourceWidth = icon.getIconWidth();
     198            sourceHeight = icon.getIconHeight();
     199            if (sourceWidth <= maxWidth) {
    173200                maxWidth = -1;
    174201            }
    175             if (realHeight <= maxHeight) {
     202            if (sourceHeight <= maxHeight) {
    176203                maxHeight = -1;
    177204            }
    178205        }
     
    183210            return getImageIcon(new Dimension(-1, maxHeight));
    184211        else if (maxHeight == -1)
    185212            return getImageIcon(new Dimension(maxWidth, -1));
    186         else if (realWidth / maxWidth > realHeight / maxHeight)
     213        else if (sourceWidth / maxWidth > sourceHeight / maxHeight)
    187214            return getImageIcon(new Dimension(maxWidth, -1));
    188215        else
    189216            return getImageIcon(new Dimension(-1, maxHeight));
  • gui/util/GuiSizesHelper.java

     
     1package org.openstreetmap.josm.gui.util;
     2
     3import java.awt.*;
     4
     5public class GuiSizesHelper {
     6    private static int screenDPI = -1;
     7
     8    private static int getScreenDPI() {
     9        if (screenDPI == -1) {
     10            synchronized (GuiHelper.class) {
     11                if (screenDPI == -1) {
     12                    screenDPI = Toolkit.getDefaultToolkit().getScreenResolution();
     13                }
     14            }
     15        }
     16        return screenDPI;
     17    }
     18
     19    /**
     20     * Returns coefficient of monitor pixel density. All hardcoded sizes must be multiplied by this value.
     21     *
     22     * @return float value. 1 - means standard monitor, 2 and high - "retina" display.
     23     * @since XXX
     24     */
     25    public static float getPixelDensity() {
     26        int pixelPerInch = getScreenDPI();
     27        return (float) (pixelPerInch / 96.);
     28    }
     29
     30    public static boolean isHiDPI() {
     31        return getPixelDensity() >= 2f;
     32    }
     33
     34    public static int getSizeDpiAdjusted(int size) {
     35        if (size <= 0) return size;
     36        int pixelPerInch = getScreenDPI();
     37        return size * pixelPerInch / 96;
     38    }
     39
     40    public static float getSizeDpiAdjusted(float size) {
     41        if (size <= 0f) return size;
     42        int pixelPerInch = getScreenDPI();
     43        return size * pixelPerInch / 96;
     44    }
     45
     46    public static double getSizeDpiAdjusted(double size) {
     47        if (size <= 0d) return size;
     48        int pixelPerInch = getScreenDPI();
     49        return size * pixelPerInch / 96;
     50    }
     51
     52    public static Dimension getDimensionDpiAdjusted(Dimension dim) {
     53        int pixelPerInch = getScreenDPI();
     54        int width = dim.width, height = dim.height;
     55        if (dim.width > 0) {
     56            width = dim.width * pixelPerInch / 96;
     57        }
     58
     59        if (dim.height > 0) {
     60            height = dim.height * pixelPerInch / 96;
     61        }
     62
     63        return new Dimension(width, height);
     64    }
     65}