Changeset 4712 in josm for trunk/src


Ignore:
Timestamp:
2011-12-26T15:13:52+01:00 (13 years ago)
Author:
bastiK
Message:

limit the maximum size of the image, but keep the aspect ratio (see #7182)

Location:
trunk/src/org/openstreetmap/josm/tools
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r4506 r4712  
    7777
    7878    /**
    79      * Return an image from the specified location.
     79     * Return an image from the specified location. Throws a RuntimeException if
     80     * the image cannot be located.
    8081     *
    8182     * @param subdir The position of the directory, e.g. 'layer'
     
    8788        if (icon == null) {
    8889            String ext = name.indexOf('.') != -1 ? "" : ".???";
    89             throw new NullPointerException(tr(
     90            throw new RuntimeException(tr(
    9091                    "Fatal: failed to locate image ''{0}''. This is a serious configuration problem. JOSM will stop working.",
    9192                    name+ext));
     
    128129    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, boolean sanitize) {
    129130        return getIfAvailable(dirs, id, subdir, name, archive, null, sanitize);
     131    }
     132
     133    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, Dimension dim, boolean sanitize) {
     134        return getIfAvailable(dirs, id, subdir, name, archive, dim, null, sanitize);
    130135    }
    131136
     
    144149     *                  part of the dimension can be -1. Then it will scale the width
    145150     *                  in the same way as the height. (And the other way around.)
     151     * @param maxSize   The maximum size of the image. It will shrink the image if necessary, and
     152     *                  keep the aspect ratio. The given width or height can be -1 which means this
     153     *                  direction is not bounded.
     154     *                  If this parameter has a non-null value, the parameter 'dim' will be ignored.
    146155     * @param sanitize  If the image should be repainted to a new BufferedImage to work
    147156     *                  around certain issues.
    148157     */
    149     public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, Dimension dim, boolean sanitize) {
     158    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name,
     159            File archive, Dimension dim, Dimension maxSize, boolean sanitize) {
    150160        ImageResource ir = getIfAvailableImpl(dirs, id, subdir, name, archive);
    151161        if (ir == null)
    152162            return null;
    153         return ir.getImageIcon(dim == null ? ImageResource.DEFAULT_DIMENSION : dim, sanitize);
    154     }
    155 
    156     private static ImageResource getIfAvailableImpl(Collection<String> dirs, String id, String subdir, String name, File archive) {
     163        if (maxSize != null)
     164            return ir.getImageIconBounded(maxSize, sanitize);
     165        else
     166            return ir.getImageIcon(dim == null ? ImageResource.DEFAULT_DIMENSION : dim, sanitize);
     167    }
     168
     169    static ImageResource getIfAvailableImpl(Collection<String> dirs, String id, String subdir, String name, File archive) {
    157170        if (name == null)
    158171            return null;
  • trunk/src/org/openstreetmap/josm/tools/ImageRequest.java

    r4271 r4712  
    2727    protected int width = -1;
    2828    protected int height = -1;
     29    protected int maxWidth = -1;
     30    protected int maxHeight = -1;
    2931    protected boolean sanitize;
    3032    protected boolean required = true;
     
    6567    }
    6668
     69    public ImageRequest setMaxWidth(int maxWidth) {
     70        this.maxWidth = maxWidth;
     71        return this;
     72    }
     73
     74    public ImageRequest setMaxHeight(int maxHeight) {
     75        this.maxHeight = maxHeight;
     76        return this;
     77    }
     78
    6779    public ImageRequest setSanitize(boolean sanitize) {
    6880        this.sanitize = sanitize;
     
    7688
    7789    public ImageIcon get() {
    78         ImageIcon icon = ImageProvider.getIfAvailable(dirs, id, subdir, name, archive, new Dimension(width, height), sanitize);
    79         if (required && icon == null) {
    80             String ext = name.indexOf('.') != -1 ? "" : ".???";
    81             throw new NullPointerException(tr("Fatal: failed to locate image ''{0}''. This is a serious configuration problem. JOSM will stop working.", name + ext));
     90        ImageResource ir = ImageProvider.getIfAvailableImpl(dirs, id, subdir, name, archive);
     91        if (ir == null) {
     92            if (required) {
     93                String ext = name.indexOf('.') != -1 ? "" : ".???";
     94                throw new RuntimeException(tr("Fatal: failed to locate image ''{0}''. This is a serious configuration problem. JOSM will stop working.", name + ext));
     95            } else
     96                return null;
    8297        }
    83         return icon;
     98        if (maxWidth != -1 || maxHeight != -1)
     99            return ir.getImageIconBounded(new Dimension(maxWidth, maxHeight), sanitize);
     100        else
     101            return ir.getImageIcon(new Dimension(width, height), sanitize);
    84102    }
    85103   
  • trunk/src/org/openstreetmap/josm/tools/ImageResource.java

    r4271 r4712  
    6262     */
    6363    public ImageIcon getImageIcon(Dimension dim, boolean sanitized) {
     64        if (dim.width < -1 || dim.width == 0 || dim.height < -1 || dim.height == 0)
     65            throw new IllegalArgumentException();
    6466        ImageWrapper iw = imgCache.get(dim);
    6567        if (iw != null) {
     
    9496        }
    9597    }
     98
     99    /**
     100     * Get image icon with a certain maximum size. The image is scaled down
     101     * to fit maximum dimensions. (Keeps aspect ratio)
     102     *
     103     * @param maxSize The maximum size. One of the dimensions (widht or height) can be -1,
     104     * which means it is not bounded.
     105     */
     106    public ImageIcon getImageIconBounded(Dimension maxSize, boolean sanitized) {
     107        if (maxSize.width < -1 || maxSize.width == 0 || maxSize.height < -1 || maxSize.height == 0)
     108            throw new IllegalArgumentException();
     109        float realWidth;
     110        float realHeight;
     111        if (svg != null) {
     112            realWidth = svg.getWidth();
     113            realHeight = svg.getHeight();
     114        } else {
     115            ImageWrapper base = imgCache.get(DEFAULT_DIMENSION);
     116            if (base == null) throw new AssertionError();
     117            ImageIcon icon = new ImageIcon(base.img);
     118            realWidth = icon.getIconWidth();
     119            realHeight = icon.getIconHeight();
     120        }
     121        int maxWidth = maxSize.width;
     122        int maxHeight = maxSize.height;
     123
     124        if (realWidth <= maxWidth) {
     125            maxWidth = -1;
     126        }
     127        if (realHeight <= maxHeight) {
     128            maxHeight = -1;
     129        }
     130
     131        if (maxWidth == -1 && maxHeight == -1)
     132            return getImageIcon(DEFAULT_DIMENSION, sanitized);
     133        else if (maxWidth == -1)
     134            return getImageIcon(new Dimension(-1, maxHeight), sanitized);
     135        else if (maxHeight == -1)
     136            return getImageIcon(new Dimension(maxWidth, -1), sanitized);
     137        else
     138            if (realWidth / maxWidth > realHeight / maxHeight)
     139                return getImageIcon(new Dimension(maxWidth, -1), sanitized);
     140            else
     141                return getImageIcon(new Dimension(-1, maxHeight), sanitized);
     142   }
    96143}
Note: See TracChangeset for help on using the changeset viewer.