Changeset 17144 in josm


Ignore:
Timestamp:
2020-10-10T18:41:23+02:00 (3 weeks ago)
Author:
simon04
Message:

fix #19751 - MapImage.rescale: use original SVG instead of rastered version

Location:
trunk
Files:
25 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/MapImage.java

    r16252 r17144  
    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) {
     
    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
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r16998 r17144  
    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);
     
    14891489            Logging.error("createImageFromSvg: {0} {1} sourceWidth={2} sourceHeight={3}", svg.getXMLBase(), dim, sourceWidth, sourceHeight);
    14901490            return null;
    1491         }
    1492         if (resizeMode == ImageResizeMode.BOUNDED) {
    1493             resizeMode = ImageResizeMode.BOUNDED_UPSCALE;
    14941491        }
    14951492        return resizeMode.createBufferedImage(dim, new Dimension((int) sourceWidth, (int) sourceHeight), g -> {
  • trunk/src/org/openstreetmap/josm/tools/ImageResizeMode.java

    r17074 r17144  
    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
     
    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
  • trunk/src/org/openstreetmap/josm/tools/ImageResource.java

    r16998 r17144  
    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    }
     
    141141     */
    142142    public ImageIcon getImageIcon(Dimension dim) {
    143         return getImageIcon(dim, true, ImageResizeMode.AUTO);
     143        return getImageIcon(dim, true, null);
    144144    }
    145145
     
    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);
  • trunk/test/functional/org/openstreetmap/josm/tools/ImageProviderTest.java

    r16997 r17144  
    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    }
     
    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
  • trunk/test/unit/org/openstreetmap/josm/tools/ImageResizeModeTest.java

    r17005 r17144  
    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
Note: See TracChangeset for help on using the changeset viewer.