Ticket #1576: nosan.patch

File nosan.patch, 14.0 KB (added by stoecker, 13 years ago)

Patch to remove sanitize

  • src/org/openstreetmap/josm/tools/ImageProvider.java

     
    8484        OTHER   // everything else, e.g. png, gif (must be supported by Java)
    8585    }
    8686
    87     public static enum SanitizeMode {
    88         OFF,                // never sanitize the image
    89         ALWAYS,             // always copy to a new BufferedImage
    90         MAKE_BUFFEREDIMAGE  // make sure the returned image is instance of BufferedImage
    91     }
    92 
    9387    protected Collection<String> dirs;
    9488    protected String id;
    9589    protected String subdir;
     
    9993    protected int height = -1;
    10094    protected int maxWidth = -1;
    10195    protected int maxHeight = -1;
    102     protected SanitizeMode sanitize;
    10396    protected boolean optional;
    10497
    10598    private static SVGUniverse svgUniverse;
     
    207200    }
    208201
    209202    /**
    210      * Set true, if the image should be repainted to a new BufferedImage in order to work around certain issues.
    211      */
    212     public ImageProvider setSanitize(SanitizeMode sanitize) {
    213         this.sanitize = sanitize;
    214         return this;
    215     }
    216 
    217     /**
    218203     * The image URL comes from user data and the image may be missing.
    219204     *
    220205     * Set true, if JOSM should *not* throw a RuntimeException in case the image cannot be located.
     
    239224            }
    240225        }
    241226        if (maxWidth != -1 || maxHeight != -1)
    242             return ir.getImageIconBounded(new Dimension(maxWidth, maxHeight), sanitize);
     227            return ir.getImageIconBounded(new Dimension(maxWidth, maxHeight));
    243228        else
    244             return ir.getImageIcon(new Dimension(width, height), sanitize);
     229            return ir.getImageIcon(new Dimension(width, height));
    245230    }
    246231
    247232    /**
     
    291276                            : URLDecoder.decode(data, "utf-8").getBytes();
    292277                    if (mediatype != null && mediatype.contains("image/svg+xml")) {
    293278                        URI uri = getSvgUniverse().loadSVG(new StringReader(new String(bytes)), name);
    294                         SVGDiagram svg = getSvgUniverse().getDiagram(uri);
    295                         return new ImageResource(svg);
     279                        return new ImageResource(getSvgUniverse().getDiagram(uri));
    296280                    } else {
    297                         return new ImageResource(new ImageIcon(bytes).getImage(), true);
     281                        return new ImageResource(new ImageIcon(bytes).getImage());
    298282                    }
    299283                }
    300284            }
     
    405389                    try {
    406390                        img = ImageIO.read(is.getFile().toURI().toURL());
    407391                    } catch (IOException e) {}
    408                     return img == null ? null : new ImageResource(img, true);
     392                    return img == null ? null : new ImageResource(img);
    409393                default:
    410394                    throw new AssertionError();
    411395            }
     
    474458                            try {
    475459                                img = ImageIO.read(new ByteArrayInputStream(buf));
    476460                            } catch (IOException e) {}
    477                             return img == null ? null : new ImageResource(img, false);
     461                            return img == null ? null : new ImageResource(img);
    478462                        default:
    479463                            throw new AssertionError();
    480464                    }
     
    508492                try {
    509493                    img = ImageIO.read(path);
    510494                } catch (IOException e) {}
    511                 return img == null ? null : new ImageResource(img, true);
     495                return img == null ? null : new ImageResource(img);
    512496            default:
    513497                throw new AssertionError();
    514498        }
     
    766750        return get("data", type.getAPIName());
    767751    }
    768752
    769     public static BufferedImage sanitize(Image img) {
    770         (new ImageIcon(img)).getImage(); // load competely
    771         int width = img.getWidth(null);
    772         int height = img.getHeight(null);
    773         BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    774         result.getGraphics().drawImage(img, 0, 0, null);
    775         return result;
    776     }
    777 
    778753    public static Image createImageFromSvg(SVGDiagram svg, Dimension dim) {
    779754        float realWidth = svg.getWidth();
    780755        float realHeight = svg.getHeight();
  • src/org/openstreetmap/josm/tools/ImageResource.java

     
    99import java.util.HashMap;
    1010import javax.swing.ImageIcon;
    1111
    12 import org.openstreetmap.josm.tools.ImageProvider.SanitizeMode;
    13 
    1412/**
    1513 * Holds data for one particular image.
    1614 * It can be backed by a svg or raster image.
     
    2321    /**
    2422     * Caches the image data for resized versions of the same image.
    2523     */
    26     private HashMap<Dimension, ImageWrapper> imgCache = new HashMap<Dimension, ImageWrapper>();
     24    private HashMap<Dimension, Image> imgCache = new HashMap<Dimension, Image>();
    2725    private SVGDiagram svg;
    2826    public static final Dimension DEFAULT_DIMENSION = new Dimension(-1, -1);
    2927 
    30     /**
    31      * remember whether the image has been sanitized
    32      */
    33     private static class ImageWrapper {
    34         Image img;
    35         boolean sanitized;
    36 
    37         public ImageWrapper(Image img, boolean sanitized) {
    38             CheckParameterUtil.ensureParameterNotNull(img);
    39             this.img = img;
    40             this.sanitized = sanitized;
    41         }
    42     }
    43    
    44     public ImageResource(Image img, boolean sanitized) {
     28    public ImageResource(Image img) {
    4529        CheckParameterUtil.ensureParameterNotNull(img);
    46         imgCache.put(DEFAULT_DIMENSION, new ImageWrapper(img, sanitized));
     30        imgCache.put(DEFAULT_DIMENSION, img);
    4731    }
    4832
    4933    public ImageResource(SVGDiagram svg) {
     
    5236    }
    5337
    5438    public ImageIcon getImageIcon() {
    55         return getImageIcon(DEFAULT_DIMENSION, SanitizeMode.OFF);
     39        return getImageIcon(DEFAULT_DIMENSION);
    5640    }
    5741
    5842    /**
     
    6044     * @param   dim The requested dimensions. Use (-1,-1) for the original size
    6145     *          and (width, -1) to set the width, but otherwise scale the image
    6246     *          proportionally.
    63      * @param sanitize Whether the returned image should be copied to a BufferedImage
    64      *          to avoid certain problem with native image formats.
    6547     */
    66     public ImageIcon getImageIcon(Dimension dim, SanitizeMode sanitize) {
     48    public ImageIcon getImageIcon(Dimension dim) {
    6749        if (dim.width < -1 || dim.width == 0 || dim.height < -1 || dim.height == 0)
    6850            throw new IllegalArgumentException();
    69         ImageWrapper iw = imgCache.get(dim);
    70         if (iw != null) {
    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                 }
    76             }
    77             return new ImageIcon(iw.img);
     51        Image img = imgCache.get(dim);
     52        if (img != null) {
     53            return new ImageIcon(img);
    7854        }
    7955        if (svg != null) {
    80             Image img = ImageProvider.createImageFromSvg(svg, dim);
    81             imgCache.put(dim, new ImageWrapper(img, true));
     56            img = ImageProvider.createImageFromSvg(svg, dim);
     57            imgCache.put(dim, img);
    8258            return new ImageIcon(img);
    8359        } else {
    84             ImageWrapper base = imgCache.get(DEFAULT_DIMENSION);
     60            Image base = imgCache.get(DEFAULT_DIMENSION);
    8561            if (base == null) throw new AssertionError();
    8662           
    8763            int width = dim.width;
    8864            int height = dim.height;
    89             ImageIcon icon = new ImageIcon(base.img);
     65            ImageIcon icon = new ImageIcon(base);
    9066            if (width == -1) {
    9167                width = icon.getIconWidth() * height / icon.getIconHeight();
    9268            } else if (height == -1) {
    9369                height = icon.getIconHeight() * width / icon.getIconWidth();
    9470            }
    95             Image img;
    9671            img = icon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
    97             boolean sanitized = false;
    98             if (sanitize == SanitizeMode.ALWAYS || (sanitize == SanitizeMode.MAKE_BUFFEREDIMAGE && !(img instanceof BufferedImage))) {
    99                 img = ImageProvider.sanitize(img);
    100                 sanitized = true;
    101             }
    102             imgCache.put(dim, new ImageWrapper(img, sanitized));
     72            imgCache.put(dim, img);
    10373            return new ImageIcon(img);
    10474        }
    10575    }
     
    11181     * @param maxSize The maximum size. One of the dimensions (widht or height) can be -1,
    11282     * which means it is not bounded.
    11383     */
    114     public ImageIcon getImageIconBounded(Dimension maxSize, SanitizeMode sanitize) {
     84    public ImageIcon getImageIconBounded(Dimension maxSize) {
    11585        if (maxSize.width < -1 || maxSize.width == 0 || maxSize.height < -1 || maxSize.height == 0)
    11686            throw new IllegalArgumentException();
    11787        float realWidth;
     
    12090            realWidth = svg.getWidth();
    12191            realHeight = svg.getHeight();
    12292        } else {
    123             ImageWrapper base = imgCache.get(DEFAULT_DIMENSION);
     93            Image base = imgCache.get(DEFAULT_DIMENSION);
    12494            if (base == null) throw new AssertionError();
    125             ImageIcon icon = new ImageIcon(base.img);
     95            ImageIcon icon = new ImageIcon(base);
    12696            realWidth = icon.getIconWidth();
    12797            realHeight = icon.getIconHeight();
    12898        }
     
    137107        }
    138108
    139109        if (maxWidth == -1 && maxHeight == -1)
    140             return getImageIcon(DEFAULT_DIMENSION, sanitize);
     110            return getImageIcon(DEFAULT_DIMENSION);
    141111        else if (maxWidth == -1)
    142             return getImageIcon(new Dimension(-1, maxHeight), sanitize);
     112            return getImageIcon(new Dimension(-1, maxHeight));
    143113        else if (maxHeight == -1)
    144             return getImageIcon(new Dimension(maxWidth, -1), sanitize);
     114            return getImageIcon(new Dimension(maxWidth, -1));
    145115        else
    146116            if (realWidth / maxWidth > realHeight / maxHeight)
    147                 return getImageIcon(new Dimension(maxWidth, -1), sanitize);
     117                return getImageIcon(new Dimension(maxWidth, -1));
    148118            else
    149                 return getImageIcon(new Dimension(-1, maxHeight), sanitize);
     119                return getImageIcon(new Dimension(-1, maxHeight));
    150120   }
    151121}
  • src/org/openstreetmap/josm/gui/mappaint/AreaElemStyle.java

     
    1717import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
    1818import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
    1919import org.openstreetmap.josm.tools.CheckParameterUtil;
    20 import org.openstreetmap.josm.tools.ImageProvider.SanitizeMode;
    2120import org.openstreetmap.josm.tools.Utils;
    2221
    2322public class AreaElemStyle extends ElemStyle
     
    4443
    4544        IconReference iconRef = c.get("fill-image", null, IconReference.class);
    4645        if (iconRef != null) {
    47             ImageIcon icon = MapPaintStyles.getIcon(iconRef, -1, -1, SanitizeMode.MAKE_BUFFEREDIMAGE);
     46            ImageIcon icon = MapPaintStyles.getIcon(iconRef, -1, -1);
    4847            if (icon != null) {
    4948                if (!(icon.getImage() instanceof BufferedImage))
    5049                    throw new RuntimeException();
  • src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

     
    2626import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    2727import org.openstreetmap.josm.io.MirroredInputStream;
    2828import org.openstreetmap.josm.tools.ImageProvider;
    29 import org.openstreetmap.josm.tools.ImageProvider.SanitizeMode;
    3029
    3130/**
    3231 * This class manages the ElemStyles instance. The object you get with
     
    8887    }
    8988
    9089    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) {
    9590        final String namespace = ref.source.getPrefName();
    9691        ImageIcon i = new ImageProvider(ref.iconName)
    9792                .setDirs(getIconSourceDirs(ref.source))
     
    9994                .setArchive(ref.source.zipIcons)
    10095                .setWidth(width)
    10196                .setHeight(height)
    102                 .setSanitize(sanitize)
    10397                .setOptional(true).get();
    10498        if(i == null)
    10599        {
     
    109103        return i;
    110104    }
    111105
    112     public static ImageIcon getNoIcon_Icon(StyleSource source) {
    113         return getNoIcon_Icon(source, SanitizeMode.OFF);
    114     }
    115 
    116106    /**
    117107     * No icon with the given name was found, show a dummy icon instead
    118108     * @return the icon misc/no_icon.png, in descending priority:
     
    121111     *   - josm's default icon
    122112     *  can be null if the defaults are turned off by user
    123113     */
    124     public static ImageIcon getNoIcon_Icon(StyleSource source, SanitizeMode sanitize) {
     114    public static ImageIcon getNoIcon_Icon(StyleSource source) {
    125115        return new ImageProvider("misc/no_icon.png")
    126116                .setDirs(getIconSourceDirs(source))
    127117                .setId("mappaint."+source.getPrefName())
    128118                .setArchive(source.zipIcons)
    129                 .setSanitize(sanitize)
    130119                .setOptional(true).get();
    131120    }
    132121