Changeset 4820 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2012-01-19T12:49:55+01:00 (8 years ago)
Author:
bastiK
Message:

extend image sanitize options

Location:
trunk/src/org/openstreetmap/josm
Files:
7 edited

Legend:

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

    r4272 r4820  
    1616import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
    1717import org.openstreetmap.josm.tools.CheckParameterUtil;
     18import org.openstreetmap.josm.tools.ImageProvider.SanitizeMode;
    1819import org.openstreetmap.josm.tools.Utils;
    1920
     
    4546        IconReference iconRef = c.get("fill-image", null, IconReference.class);
    4647        if (iconRef != null) {
    47             ImageIcon icon = MapPaintStyles.getIcon(iconRef, -1, -1, false);
     48            ImageIcon icon = MapPaintStyles.getIcon(iconRef, -1, -1, SanitizeMode.MAKE_BUFFEREDIMAGE);
    4849            if (icon != null) {
    49                 if (!(icon.getImage() instanceof BufferedImage)) {
    50                     icon = MapPaintStyles.getIcon(iconRef, -1, -1, true);
    51                 }
    5250                if (!(icon.getImage() instanceof BufferedImage))
    5351                    throw new RuntimeException();
  • trunk/src/org/openstreetmap/josm/gui/mappaint/LinePatternElemStyle.java

    r4272 r4820  
    2828        if (iconRef == null)
    2929            return null;
    30         ImageIcon icon = MapPaintStyles.getIcon(iconRef, -1, -1, false);
     30        ImageIcon icon = MapPaintStyles.getIcon(iconRef, -1, -1);
    3131        if (icon == null)
    3232            return null;
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r4721 r4820  
    2727import org.openstreetmap.josm.io.MirroredInputStream;
    2828import org.openstreetmap.josm.tools.ImageProvider;
     29import org.openstreetmap.josm.tools.ImageProvider.SanitizeMode;
    2930
    3031/**
     
    8788    }
    8889
    89     public static ImageIcon getIcon(IconReference ref, int width, int height, boolean sanitize) {
     90    public static ImageIcon getIcon(IconReference ref, int width, int height) {
     91        return getIcon(ref, width, height, SanitizeMode.OFF);
     92    }
     93
     94    public static ImageIcon getIcon(IconReference ref, int width, int height, SanitizeMode sanitize) {
    9095        final String namespace = ref.source.getPrefName();
    9196        ImageIcon i = new ImageProvider(ref.iconName)
     
    105110    }
    106111
     112    public static ImageIcon getNoIcon_Icon(StyleSource source) {
     113        return getNoIcon_Icon(source, SanitizeMode.OFF);
     114    }
     115
    107116    /**
    108117     * No icon with the given name was found, show a dummy icon instead
     
    113122     *  can be null if the defaults are turned off by user
    114123     */
    115     public static ImageIcon getNoIcon_Icon(StyleSource source, boolean sanitize) {
     124    public static ImageIcon getNoIcon_Icon(StyleSource source, SanitizeMode sanitize) {
    116125        return new ImageProvider("misc/no_icon.png")
    117126                .setDirs(getIconSourceDirs(source))
  • trunk/src/org/openstreetmap/josm/gui/mappaint/NodeElemStyle.java

    r4415 r4820  
    150150        int height = heightF == null ? -1 : Math.round(heightF);
    151151
    152         ImageIcon icon = MapPaintStyles.getIcon(iconRef, width, height, false);
     152        ImageIcon icon = MapPaintStyles.getIcon(iconRef, width, height);
    153153        if (icon == null)
    154             return new Pair<ImageIcon, Integer>(MapPaintStyles.getNoIcon_Icon(iconRef.source, false), 255);
     154            return new Pair<ImageIcon, Integer>(MapPaintStyles.getNoIcon_Icon(iconRef.source), 255);
    155155        int iconAlpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.icon-image-alpha", 255))));
    156156        Integer pAlpha = Utils.color_float2int(c.get("icon-opacity", null, float.class));
  • trunk/src/org/openstreetmap/josm/gui/mappaint/StyleSource.java

    r4272 r4820  
    7474        if (imageIcon == null) {
    7575            if (icon != null) {
    76                 imageIcon = MapPaintStyles.getIcon(new IconReference(icon, this), -1, -1, false);
     76                imageIcon = MapPaintStyles.getIcon(new IconReference(icon, this), -1, -1);
    7777            }
    7878            if (imageIcon == null) {
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r4818 r4820  
    3535import java.util.zip.ZipEntry;
    3636import java.util.zip.ZipFile;
     37
    3738import javax.swing.Icon;
    3839import javax.swing.ImageIcon;
     40
    3941import org.apache.commons.codec.binary.Base64;
    4042import org.openstreetmap.josm.Main;
     
    8183    }
    8284
     85    public static enum SanitizeMode {
     86        OFF,                // never sanitize the image
     87        ALWAYS,             // always copy to a new BufferedImage
     88        MAKE_BUFFEREDIMAGE  // make sure the returned image is instance of BufferedImage
     89    }
     90
    8391    protected Collection<String> dirs;
    8492    protected String id;
     
    9098    protected int maxWidth = -1;
    9199    protected int maxHeight = -1;
    92     protected boolean sanitize;
     100    protected SanitizeMode sanitize;
    93101    protected boolean optional;
    94102
     
    200208     * Set true, if the image should be repainted to a new BufferedImage in order to work around certain issues.
    201209     */
    202     public ImageProvider setSanitize(boolean sanitize) {
     210    public ImageProvider setSanitize(SanitizeMode sanitize) {
    203211        this.sanitize = sanitize;
    204212        return this;
     
    276284    @Deprecated
    277285    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive) {
    278         return getIfAvailable(dirs, id, subdir, name, archive, false);
     286        return getIfAvailable(dirs, id, subdir, name, archive, SanitizeMode.OFF);
    279287    }
    280288
    281289    @Deprecated
    282     public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, boolean sanitize) {
     290    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, SanitizeMode sanitize) {
    283291        return getIfAvailable(dirs, id, subdir, name, archive, null, sanitize);
    284292    }
    285293
    286294    @Deprecated
    287     public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, Dimension dim, boolean sanitize) {
     295    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, Dimension dim, SanitizeMode sanitize) {
    288296        return getIfAvailable(dirs, id, subdir, name, archive, dim, null, sanitize);
    289297    }
     
    312320    @Deprecated
    313321    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name,
    314             File archive, Dimension dim, Dimension maxSize, boolean sanitize) {
     322            File archive, Dimension dim, Dimension maxSize, SanitizeMode sanitize) {
    315323        ImageProvider p = new ImageProvider(subdir, name).setDirs(dirs).setId(id).setArchive(archive).setSanitize(sanitize).setOptional(true);
    316324        if (dim != null) {
  • trunk/src/org/openstreetmap/josm/tools/ImageResource.java

    r4712 r4820  
    66import java.awt.Dimension;
    77import java.awt.Image;
     8import java.awt.image.BufferedImage;
    89import java.util.HashMap;
    910import javax.swing.ImageIcon;
     11
     12import org.openstreetmap.josm.tools.ImageProvider.SanitizeMode;
    1013
    1114/**
     
    5053
    5154    public ImageIcon getImageIcon() {
    52         return getImageIcon(DEFAULT_DIMENSION, false);
     55        return getImageIcon(DEFAULT_DIMENSION, SanitizeMode.OFF);
    5356    }
    5457
     
    5861     *          and (width, -1) to set the width, but otherwise scale the image
    5962     *          proportionally.
    60      * @param sanitized Whether the returned image should be copied to a BufferedImage
     63     * @param sanitize Whether the returned image should be copied to a BufferedImage
    6164     *          to avoid certain problem with native image formats.
    6265     */
    63     public ImageIcon getImageIcon(Dimension dim, boolean sanitized) {
     66    public ImageIcon getImageIcon(Dimension dim, SanitizeMode sanitize) {
    6467        if (dim.width < -1 || dim.width == 0 || dim.height < -1 || dim.height == 0)
    6568            throw new IllegalArgumentException();
    6669        ImageWrapper iw = imgCache.get(dim);
    6770        if (iw != null) {
    68             if (sanitized && !iw.sanitized) {
    69                 iw.img = ImageProvider.sanitize(iw.img);
    70                 iw.sanitized = true;
     71            if (!iw.sanitized) {
     72                if (sanitize == SanitizeMode.ALWAYS || (sanitize == SanitizeMode.MAKE_BUFFEREDIMAGE && !(iw.img instanceof BufferedImage))) {
     73                    iw.img = ImageProvider.sanitize(iw.img);
     74                    iw.sanitized = true;
     75                }
    7176            }
    7277            return new ImageIcon(iw.img);
     
    8893                height = icon.getIconHeight() * width / icon.getIconWidth();
    8994            }
    90             Image img = icon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
    91             if (sanitized) {
     95            Image img;
     96            if (width == dim.width && height == dim.height) {
     97                img = icon.getImage();
     98            } else {
     99                img = icon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
     100            }
     101            boolean sanitized = false;
     102            if (sanitize == SanitizeMode.ALWAYS || (sanitize == SanitizeMode.MAKE_BUFFEREDIMAGE && !(img instanceof BufferedImage))) {
    92103                img = ImageProvider.sanitize(img);
     104                sanitized = true;
    93105            }
    94106            imgCache.put(dim, new ImageWrapper(img, sanitized));
     
    104116     * which means it is not bounded.
    105117     */
    106     public ImageIcon getImageIconBounded(Dimension maxSize, boolean sanitized) {
     118    public ImageIcon getImageIconBounded(Dimension maxSize, SanitizeMode sanitize) {
    107119        if (maxSize.width < -1 || maxSize.width == 0 || maxSize.height < -1 || maxSize.height == 0)
    108120            throw new IllegalArgumentException();
     
    130142
    131143        if (maxWidth == -1 && maxHeight == -1)
    132             return getImageIcon(DEFAULT_DIMENSION, sanitized);
     144            return getImageIcon(DEFAULT_DIMENSION, sanitize);
    133145        else if (maxWidth == -1)
    134             return getImageIcon(new Dimension(-1, maxHeight), sanitized);
     146            return getImageIcon(new Dimension(-1, maxHeight), sanitize);
    135147        else if (maxHeight == -1)
    136             return getImageIcon(new Dimension(maxWidth, -1), sanitized);
     148            return getImageIcon(new Dimension(maxWidth, -1), sanitize);
    137149        else
    138150            if (realWidth / maxWidth > realHeight / maxHeight)
    139                 return getImageIcon(new Dimension(maxWidth, -1), sanitized);
     151                return getImageIcon(new Dimension(maxWidth, -1), sanitize);
    140152            else
    141                 return getImageIcon(new Dimension(-1, maxHeight), sanitized);
     153                return getImageIcon(new Dimension(-1, maxHeight), sanitize);
    142154   }
    143155}
Note: See TracChangeset for help on using the changeset viewer.