Ticket #19751: 19751.patch

File 19751.patch, 11.2 KB (added by simon04, 15 months ago)
  • src/org/openstreetmap/josm/gui/mappaint/styleelement/MapImage.java

    diff --git a/src/org/openstreetmap/josm/gui/mappaint/styleelement/MapImage.java b/src/org/openstreetmap/josm/gui/mappaint/styleelement/MapImage.java
    index 963b42bab..c921a2217 100644
    a b private Image getImage() { 
    189189                    ImageIcon noIcon = MapPaintStyles.getNoIconIcon(source);
    190190                    img = noIcon == null ? null : noIcon.getImage();
    191191                } else {
    192                     img = rescale(result.getImageIcon(new Dimension(width, height)).getImage());
     192                    img = result.getImageIcon(new Dimension(width, height)).getImage();
     193                    if (img != null && mustRescale(img)) {
     194                        // Scale down large images to 16x16 pixels if no size is explicitly specified
     195                        img = result.getImageIconBounded(ImageProvider.ImageSizes.MAP.getImageDimension()).getImage();
     196                    }
    193197                }
    194198                if (temporary) {
    195199                    disabledImgCache = null;
    public BoxProvider getBoxProvider() { 
    300304        return new MapImageBoxProvider();
    301305    }
    302306
    303     /**
    304      * Rescale excessively large images.
    305      * @param image the unscaled image
    306      * @return The scaled down version to 16x16 pixels if the image height and width exceeds 48 pixels and no size has been explicitly specified
    307      */
    308     private Image rescale(Image image) {
    309         if (image == null) return null;
    310         // Scale down large (.svg) images to 16x16 pixels if no size is explicitly specified
    311         if (mustRescale(image)) {
    312             return ImageProvider.createBoundedImage(image, 16);
    313         } else {
    314             return image;
    315         }
    316     }
    317 
    318307    private boolean mustRescale(Image image) {
    319308        return autoRescale && width == -1 && image.getWidth(null) > MAX_SIZE
    320309             && height == -1 && image.getHeight(null) > MAX_SIZE;
  • src/org/openstreetmap/josm/tools/ImageProvider.java

    diff --git a/src/org/openstreetmap/josm/tools/ImageProvider.java b/src/org/openstreetmap/josm/tools/ImageProvider.java
    index 29a55dbe6..67d424bcd 100644
    a b public ImageIcon get() { 
    660660            Logging.trace("get {0} from {1}", this, Thread.currentThread());
    661661        }
    662662        if (virtualMaxWidth != -1 || virtualMaxHeight != -1)
    663             return ir.getImageIcon(new Dimension(virtualMaxWidth, virtualMaxHeight), multiResolution, ImageResizeMode.BOUNDED);
     663            return ir.getImageIcon(new Dimension(virtualMaxWidth, virtualMaxHeight), multiResolution, null);
    664664        else
    665665            return ir.getImageIcon(new Dimension(virtualWidth, virtualHeight), multiResolution, ImageResizeMode.AUTO);
    666666    }
    static BufferedImage createImageFromSvg(SVGDiagram svg, Dimension dim, ImageResi 
    14891489            Logging.error("createImageFromSvg: {0} {1} sourceWidth={2} sourceHeight={3}", svg.getXMLBase(), dim, sourceWidth, sourceHeight);
    14901490            return null;
    14911491        }
    1492         if (resizeMode == ImageResizeMode.BOUNDED) {
    1493             resizeMode = ImageResizeMode.BOUNDED_UPSCALE;
    1494         }
    14951492        return resizeMode.createBufferedImage(dim, new Dimension((int) sourceWidth, (int) sourceHeight), g -> {
    14961493            try {
    14971494                synchronized (getSvgUniverse()) {
  • src/org/openstreetmap/josm/tools/ImageResizeMode.java

    diff --git a/src/org/openstreetmap/josm/tools/ImageResizeMode.java b/src/org/openstreetmap/josm/tools/ImageResizeMode.java
    index 2ea8d1e68..d1bcabc2a 100644
    a b  
    1313 */
    1414enum ImageResizeMode {
    1515
     16    /**
     17     * Calculate proportional dimensions that best fit into the target width and height, retain aspect ratio
     18     */
    1619    AUTO {
    1720        @Override
    1821        Dimension computeDimension(Dimension dim, Dimension icon) {
    Dimension computeDimension(Dimension dim, Dimension icon) { 
    2427                return new Dimension(Math.max(1, icon.width * dim.height / icon.height), dim.height);
    2528            } else if (dim.height == -1) {
    2629                return new Dimension(dim.width, Math.max(1, icon.height * dim.width / icon.width));
     30            } else if (icon.getWidth() / dim.getWidth() > icon.getHeight() / dim.getHeight()) {
     31                return computeDimension(new Dimension(dim.width, -1), icon);
    2732            } else {
    28                 return dim;
     33                return computeDimension(new Dimension(-1, dim.height), icon);
    2934            }
    3035        }
    3136    },
    3237
     38    /**
     39     * Calculate dimensions for the largest image that fit within the bounding box, retain aspect ratio
     40     */
    3341    BOUNDED {
    34         @Override
    35         Dimension computeDimension(Dimension dim, Dimension icon) {
    36             final int maxWidth = Math.min(dim.width, icon.width);
    37             final int maxHeight = Math.min(dim.height, icon.height);
    38             return BOUNDED_UPSCALE.computeDimension(new Dimension(maxWidth, maxHeight), icon);
    39         }
    40     },
    41 
    42     BOUNDED_UPSCALE {
    4342        @Override
    4443        Dimension computeDimension(Dimension dim, Dimension icon) {
    4544            CheckParameterUtil.ensureThat((dim.width > 0 || dim.width == -1) && (dim.height > 0 || dim.height == -1),
    4645                    () -> dim + " is invalid");
    47             final int maxWidth = dim.width;
    48             final int maxHeight = dim.height;
    49             final Dimension spec;
    50             if (maxWidth == -1 || maxHeight == -1) {
    51                 spec = dim;
    52             } else if (icon.getWidth() / maxWidth > icon.getHeight() / maxHeight) {
    53                 spec = new Dimension(maxWidth, -1);
    54             } else {
    55                 spec = new Dimension(-1, maxHeight);
    56             }
    57             return AUTO.computeDimension(spec, icon);
     46            final int maxWidth = Math.min(dim.width, icon.width);
     47            final int maxHeight = Math.min(dim.height, icon.height);
     48            return AUTO.computeDimension(new Dimension(maxWidth, maxHeight), icon);
    5849        }
    5950    },
    6051
     52    /**
     53     * Position an appropriately scaled image within the bounding box, retain aspect ratio
     54     */
    6155    PADDED {
    6256        @Override
    6357        Dimension computeDimension(Dimension dim, Dimension icon) {
  • src/org/openstreetmap/josm/tools/ImageResource.java

    diff --git a/src/org/openstreetmap/josm/tools/ImageResource.java b/src/org/openstreetmap/josm/tools/ImageResource.java
    index 59d9834d1..f710b7f30 100644
    a b public ImageResource setDisabled(boolean disabled) { 
    101101     */
    102102    public void attachImageIcon(AbstractAction a) {
    103103        Dimension iconDimension = ImageProvider.ImageSizes.SMALLICON.getImageDimension();
    104         ImageIcon icon = getImageIconBounded(iconDimension);
     104        ImageIcon icon = getImageIcon(iconDimension);
    105105        a.putValue(Action.SMALL_ICON, icon);
    106106
    107107        iconDimension = ImageProvider.ImageSizes.LARGEICON.getImageDimension();
    108         icon = getImageIconBounded(iconDimension);
     108        icon = getImageIcon(iconDimension);
    109109        a.putValue(Action.LARGE_ICON_KEY, icon);
    110110    }
    111111
    public ImageIcon getImageIcon() { 
    140140     * @see #getImageIconBounded(java.awt.Dimension)
    141141     */
    142142    public ImageIcon getImageIcon(Dimension dim) {
    143         return getImageIcon(dim, true, ImageResizeMode.AUTO);
     143        return getImageIcon(dim, true, null);
    144144    }
    145145
    146146    /**
    ImageIcon getImageIconAlreadyScaled(Dimension dim, boolean multiResolution, bool 
    174174        CheckParameterUtil.ensureThat((dim.width > 0 || dim.width == -1) && (dim.height > 0 || dim.height == -1),
    175175                () -> dim + " is invalid");
    176176
     177        if (resizeMode == null && svg != null) {
     178            // upscale SVG icons
     179            resizeMode = ImageResizeMode.AUTO;
     180        } else if (resizeMode == null) {
     181            resizeMode = ImageResizeMode.BOUNDED;
     182        }
    177183        final int cacheKey = resizeMode.cacheKey(dim);
    178184        BufferedImage img = imgCache.get(cacheKey);
    179185        if (img == null) {
  • test/functional/org/openstreetmap/josm/tools/ImageProviderTest.java

    diff --git a/test/functional/org/openstreetmap/josm/tools/ImageProviderTest.java b/test/functional/org/openstreetmap/josm/tools/ImageProviderTest.java
    index e4b0bd20e..a318f11c2 100644
    a b public void testImageIcon() throws IOException { 
    170170        ImageResource resource = new ImageProvider("presets/misc/housenumber_small").getResource();
    171171        testImage(12, 9, "housenumber_small-AUTO-null", resource.getImageIcon());
    172172        testImage(12, 9, "housenumber_small-AUTO-default", resource.getImageIcon(ImageResource.DEFAULT_DIMENSION));
    173         testImage(8, 8, "housenumber_small-AUTO-08x08", resource.getImageIcon(new Dimension(8, 8)));
    174         testImage(16, 16, "housenumber_small-AUTO-16x16", resource.getImageIcon(new Dimension(16, 16)));
    175         testImage(24, 24, "housenumber_small-AUTO-24x24", resource.getImageIcon(new Dimension(24, 24)));
     173        testImage(8, 6, "housenumber_small-AUTO-08x08", resource.getImageIcon(new Dimension(8, 8)));
     174        testImage(16, 12, "housenumber_small-AUTO-16x16", resource.getImageIcon(new Dimension(16, 16)));
     175        testImage(24, 18, "housenumber_small-AUTO-24x24", resource.getImageIcon(new Dimension(24, 24)));
    176176        testImage(36, 27, "housenumber_small-AUTO-36x27", resource.getImageIcon(new Dimension(36, 27)));
    177177    }
    178178
    public void testImageIcon() throws IOException { 
    184184    public void testImageIconBounded() throws IOException {
    185185        ImageResource resource = new ImageProvider("presets/misc/housenumber_small").getResource();
    186186        testImage(8, 6, "housenumber_small-BOUNDED-08x08", resource.getImageIconBounded(new Dimension(8, 8)));
    187         testImage(16, 12, "housenumber_small-BOUNDED-16x16", resource.getImageIconBounded(new Dimension(16, 16)));
    188         testImage(24, 18, "housenumber_small-BOUNDED-24x24", resource.getImageIconBounded(new Dimension(24, 24)));
     187        testImage(12, 9, "housenumber_small-BOUNDED-16x16", resource.getImageIconBounded(new Dimension(16, 16)));
     188        testImage(12, 9, "housenumber_small-BOUNDED-24x24", resource.getImageIconBounded(new Dimension(24, 24)));
    189189    }
    190190
    191191    /**
  • test/unit/org/openstreetmap/josm/tools/ImageResizeModeTest.java

    diff --git a/test/unit/org/openstreetmap/josm/tools/ImageResizeModeTest.java b/test/unit/org/openstreetmap/josm/tools/ImageResizeModeTest.java
    index 2f014281c..9b606ce16 100644
    a b public void testComputeDimensionAuto() { 
    3939        assertEquals(new Dimension(64, 48), ImageResizeMode.AUTO.computeDimension(new Dimension(64, 48), image));
    4040        assertEquals(new Dimension(32, 24), ImageResizeMode.AUTO.computeDimension(new Dimension(32, -1), image));
    4141        assertEquals(new Dimension(21, 16), ImageResizeMode.AUTO.computeDimension(new Dimension(-1, 16), image));
    42         assertEquals(new Dimension(24, 32), ImageResizeMode.AUTO.computeDimension(new Dimension(24, 32), image));
     42        assertEquals(new Dimension(24, 18), ImageResizeMode.AUTO.computeDimension(new Dimension(24, 32), image));
    4343    }
    4444
    4545    /**