Changeset 17144 in josm for trunk/src


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

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

Location:
trunk/src/org/openstreetmap/josm
Files:
4 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);
Note: See TracChangeset for help on using the changeset viewer.