Changeset 4822 in josm


Ignore:
Timestamp:
19.01.2012 19:58:08 (4 months ago)
Author:
bastiK
Message:

make identity of map icons depend on the name and not the image data (see #6797)

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/MapPainter.java

    r4724 r4822  
    4646import org.openstreetmap.josm.gui.mappaint.BoxTextElemStyle.HorizontalTextAlignment; 
    4747import org.openstreetmap.josm.gui.mappaint.BoxTextElemStyle.VerticalTextAlignment; 
     48import org.openstreetmap.josm.gui.mappaint.MapImage; 
    4849import org.openstreetmap.josm.gui.mappaint.NodeElemStyle; 
    4950import org.openstreetmap.josm.gui.mappaint.NodeElemStyle.Symbol; 
     
    505506    } 
    506507 
    507     public void drawLinePattern(Way way, ImageIcon pattern) { 
    508         final int width = pattern.getIconWidth(); 
    509         final int height = pattern.getIconHeight(); 
     508    public void drawLinePattern(Way way, Image pattern) { 
     509        final int width = pattern.getWidth(null); 
     510        final int height = pattern.getHeight(null); 
    510511 
    511512        Point lastP = null; 
     
    530531 
    531532                if (dist > 0) { 
    532                     g.drawImage(pattern.getImage(), 0, 0, (int) dist, height, 
     533                    g.drawImage(pattern, 0, 0, (int) dist, height, 
    533534                            width - (int) dist, 0, width, height, null); 
    534535                } 
    535536                while (dist < segmentLength) { 
    536537                    if (dist + width > segmentLength) { 
    537                         g.drawImage(pattern.getImage(), (int) dist, 0, (int) segmentLength, height, 
     538                        g.drawImage(pattern, (int) dist, 0, (int) segmentLength, height, 
    538539                                0, 0, (int) segmentLength - (int) dist, height, null); 
    539540                    } else { 
    540                         pattern.paintIcon(nc, g, (int) dist, 0); 
     541                        g.drawImage(pattern, (int) dist, 0, nc); 
    541542                    } 
    542543                    dist += width; 
     
    550551    } 
    551552 
    552     public void drawNodeIcon(Node n, ImageIcon icon, float iconAlpha, boolean selected, boolean member) { 
     553    public void drawNodeIcon(Node n, Image img, float alpha, boolean selected, boolean member) { 
    553554        Point p = nc.getPoint(n); 
    554555 
    555         final int w = icon.getIconWidth(), h=icon.getIconHeight(); 
     556        final int w = img.getWidth(null), h=img.getHeight(null); 
    556557        if(n.isHighlighted()) { 
    557558            drawPointHighlight(p, Math.max(w, h)); 
    558559        } 
    559560 
    560         if (iconAlpha != 1f) { 
    561             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, iconAlpha)); 
    562         } 
    563         icon.paintIcon ( nc, g, p.x-w/2, p.y-h/2 ); 
     561        if (alpha != 1f) { 
     562            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); 
     563        } 
     564        g.drawImage(img, p.x-w/2, p.y-h/2, nc); 
    564565        g.setPaintMode(); 
    565566        if (selected || member) 
     
    802803    } 
    803804 
    804     public void drawArea(Way w, Color color, BufferedImage fillImage, float fillImageAlpha, TextElement text) { 
    805         drawArea(w, getPath(w), color, fillImage, fillImageAlpha, text); 
    806     } 
    807  
    808     protected void drawArea(OsmPrimitive osm, Path2D.Double path, Color color, BufferedImage fillImage, float fillImageAlpha, TextElement text) { 
     805    public void drawArea(Way w, Color color, MapImage<BufferedImage> fillImage, TextElement text) { 
     806        drawArea(w, getPath(w), color, fillImage, text); 
     807    } 
     808 
     809    protected void drawArea(OsmPrimitive osm, Path2D.Double path, Color color, MapImage<BufferedImage> fillImage, TextElement text) { 
    809810 
    810811        Shape area = path.createTransformedShape(nc.getAffineTransform()); 
     
    815816                g.fill(area); 
    816817            } else { 
    817                 TexturePaint texture = new TexturePaint(fillImage, 
     818                TexturePaint texture = new TexturePaint(fillImage.img, 
    818819                        //                        new Rectangle(polygon.xpoints[0], polygon.ypoints[0], fillImage.getWidth(), fillImage.getHeight())); 
    819                         new Rectangle(0, 0, fillImage.getWidth(), fillImage.getHeight())); 
     820                        new Rectangle(0, 0, fillImage.img.getWidth(null), fillImage.img.getHeight(null))); 
    820821                g.setPaint(texture); 
    821                 if (fillImageAlpha != 1f) { 
    822                     g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, fillImageAlpha)); 
     822                if (fillImage.alpha != 1f) { 
     823                    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, fillImage.alpha)); 
    823824                } 
    824825                g.fill(area); 
     
    871872    } 
    872873 
    873     public void drawArea(Relation r, Color color, BufferedImage fillImage, float fillImageAlpha, TextElement text) { 
     874    public void drawArea(Relation r, Color color, MapImage<BufferedImage> fillImage, TextElement text) { 
    874875        Multipolygon multipolygon = MultipolygonCache.getInstance().get(nc, r); 
    875876        if (!r.isDisabled() && !multipolygon.getOuterWays().isEmpty()) { 
     
    881882                drawArea(r, p, 
    882883                        pd.selected ? settings.getRelationSelectedColor(color.getAlpha()) : color, 
    883                                 fillImage, fillImageAlpha, text); 
     884                                fillImage, text); 
    884885            } 
    885886        } 
     
    898899    } 
    899900 
    900     public void drawRestriction(ImageIcon icon, Point pVia, double vx, double vx2, double vy, double vy2, double iconAngle, boolean selected) { 
    901         /* rotate icon with direction last node in from to */ 
    902         ImageIcon rotatedIcon = ImageProvider.createRotatedImage(null /*icon2*/, icon, iconAngle); 
    903  
    904         /* scale down icon to 16*16 pixels */ 
    905         ImageIcon smallIcon = new ImageIcon(rotatedIcon.getImage().getScaledInstance(16 , 16, Image.SCALE_SMOOTH)); 
    906         int w = smallIcon.getIconWidth(), h=smallIcon.getIconHeight(); 
    907         smallIcon.paintIcon (nc, g, (int)(pVia.x+vx+vx2)-w/2, (int)(pVia.y+vy+vy2)-h/2 ); 
     901    public void drawRestriction(Image img, Point pVia, double vx, double vx2, double vy, double vy2, double angle, boolean selected) { 
     902        /* rotate image with direction last node in from to */ 
     903        Image rotatedImg = ImageProvider.createRotatedImage(null , img, angle); 
     904 
     905        /* scale down image to 16*16 pixels */ 
     906        Image smallImg = new ImageIcon(rotatedImg.getScaledInstance(16 , 16, Image.SCALE_SMOOTH)).getImage(); 
     907        int w = smallImg.getWidth(null), h=smallImg.getHeight(null); 
     908        g.drawImage(smallImg, (int)(pVia.x+vx+vx2)-w/2, (int)(pVia.y+vy+vy2)-h/2, nc); 
    908909 
    909910        if (selected) { 
     
    913914    } 
    914915 
    915     public void drawRestriction(Relation r, NodeElemStyle icon) { 
     916    public void drawRestriction(Relation r, MapImage<Image> icon) { 
    916917        Way fromWay = null; 
    917918        Way toWay = null; 
     
    10841085        } 
    10851086 
    1086         drawRestriction(inactive || r.isDisabled() ? icon.getDisabledIcon() : icon.icon, 
     1087        drawRestriction(inactive || r.isDisabled() ? icon.getDisabled() : icon.img, 
    10871088                pVia, vx, vx2, vy, vy2, iconAngle, r.isSelected()); 
    10881089    } 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/AreaElemStyle.java

    r4820 r4822  
    11// License: GPL. For details, see LICENSE file. 
    22package org.openstreetmap.josm.gui.mappaint; 
     3 
     4import static org.openstreetmap.josm.tools.Utils.equal; 
    35 
    46import java.awt.Color; 
     
    2628     */ 
    2729    public Color color; 
    28     public BufferedImage fillImage; 
    29     public float fillImageAlpha; 
     30    public MapImage<BufferedImage> fillImage; 
    3031    public TextElement text; 
    3132 
    32     protected AreaElemStyle(Cascade c, Color color, BufferedImage fillImage, float fillImageAlpha, TextElement text) { 
     33    protected AreaElemStyle(Cascade c, Color color, MapImage<BufferedImage> fillImage, TextElement text) { 
    3334        super(c, -1000f); 
    3435        CheckParameterUtil.ensureParameterNotNull(color); 
    3536        this.color = color; 
    3637        this.fillImage = fillImage; 
    37         this.fillImageAlpha = fillImageAlpha; 
    3838        this.text = text; 
    3939    } 
    4040 
    4141    public static AreaElemStyle create(Cascade c) { 
    42         BufferedImage fillImage = null; 
     42        MapImage<BufferedImage> fillImage = null; 
    4343        Color color = null; 
    44         float fillImageAlpha = 1f; 
    4544 
    4645        IconReference iconRef = c.get("fill-image", null, IconReference.class); 
     
    5049                if (!(icon.getImage() instanceof BufferedImage)) 
    5150                    throw new RuntimeException(); 
    52                 fillImage = (BufferedImage) icon.getImage(); 
     51                fillImage = new MapImage<BufferedImage>(iconRef.iconName, iconRef.source); 
     52                fillImage.img = (BufferedImage) icon.getImage(); 
    5353 
    54                 color = new Color(fillImage.getRGB(fillImage.getWidth() / 2, fillImage.getHeight() / 2)); 
     54                color = new Color(fillImage.img.getRGB( 
     55                        fillImage.img.getWidth() / 2, fillImage.img.getHeight() / 2) 
     56                ); 
    5557 
    56                 fillImageAlpha = Utils.color_int2float(Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fill-image-alpha", 255))))); 
    57                 Float pAlpha = c.get("fill-opacity", null, Float.class); 
     58                fillImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fill-image-alpha", 255)))); 
     59                Integer pAlpha = Utils.color_float2int(c.get("fill-opacity", null, float.class)); 
    5860                if (pAlpha != null) { 
    59                     if (pAlpha < 0f || pAlpha > 1f) { 
    60                         pAlpha= 1f; 
    61                     } 
    62                     fillImageAlpha = pAlpha; 
     61                    fillImage.alpha = pAlpha; 
    6362                } 
    6463            } 
     
    8281         
    8382        if (color != null) 
    84             return new AreaElemStyle(c, color, fillImage, fillImageAlpha, text); 
     83            return new AreaElemStyle(c, color, fillImage, text); 
    8584        else 
    8685            return null; 
     
    9796                } 
    9897            } 
    99             painter.drawArea((Way) osm, myColor, fillImage, fillImageAlpha, text); 
     98            painter.drawArea((Way) osm, myColor, fillImage, text); 
    10099        } else if (osm instanceof Relation) 
    101100        { 
     
    106105                } 
    107106            } 
    108             painter.drawArea((Relation) osm, myColor, fillImage, fillImageAlpha, text); 
     107            painter.drawArea((Relation) osm, myColor, fillImage, text); 
    109108        } 
    110109    } 
     
    118117        AreaElemStyle other = (AreaElemStyle) obj; 
    119118        // we should get the same image object due to caching 
    120         if (fillImage != other.fillImage) 
     119        if (!equal(fillImage, other.fillImage)) 
    121120            return false; 
    122         if (!Utils.equal(color, other.color)) 
     121        if (!equal(color, other.color)) 
    123122            return false; 
    124         if (fillImageAlpha != other.fillImageAlpha) 
    125             return false; 
    126         if (!Utils.equal(text, other.text)) 
     123        if (!equal(text, other.text)) 
    127124            return false; 
    128125        return true; 
     
    134131        hash = 61 * hash + color.hashCode(); 
    135132        hash = 61 * hash + (fillImage != null ? fillImage.hashCode() : 0); 
    136         hash = 61 * hash + Float.floatToIntBits(fillImageAlpha); 
    137133        hash = 61 * hash + (text != null ? text.hashCode() : 0); 
    138134        return hash; 
     
    142138    public String toString() { 
    143139        return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) + 
    144                 " fillImageAlpha=" + fillImageAlpha + " fillImage=[" + fillImage + "]}"; 
     140                " fillImage=[" + fillImage + "]}"; 
    145141    } 
    146142} 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/LinePatternElemStyle.java

    r4820 r4822  
    11// License: GPL. For details, see LICENSE file. 
    22package org.openstreetmap.josm.gui.mappaint; 
     3 
     4import java.awt.Image; 
    35 
    46import javax.swing.ImageIcon; 
     
    1517public class LinePatternElemStyle extends ElemStyle { 
    1618 
    17     public ImageIcon pattern; 
     19    public MapImage<Image> pattern; 
    1820 
    19     public LinePatternElemStyle(Cascade c, ImageIcon pattern) { 
     21    public LinePatternElemStyle(Cascade c, MapImage<Image> pattern) { 
    2022        super(c, -1f); 
    2123        this.pattern = pattern; 
     
    3133        if (icon == null) 
    3234            return null; 
    33         return new LinePatternElemStyle(c, icon); 
     35        MapImage<Image> pattern = new MapImage<Image>(iconRef.iconName, iconRef.source); 
     36        pattern.img = icon.getImage(); 
     37        return new LinePatternElemStyle(c, pattern); 
    3438    } 
    3539 
     
    3741    public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member) { 
    3842        Way w = (Way)primitive; 
    39         painter.drawLinePattern(w, pattern); 
     43        painter.drawLinePattern(w, pattern.img); 
    4044    } 
    4145 
     
    5256            return false; 
    5357        final LinePatternElemStyle other = (LinePatternElemStyle) obj; 
    54         return pattern.getImage() == other.pattern.getImage(); 
     58        return pattern.equals(other.pattern); 
    5559    } 
    5660 
     
    6266    @Override 
    6367    public String toString() { 
    64         return "LinePatternElemStyle{" + super.toString() + "pattern=" + pattern + '}'; 
     68        return "LinePatternElemStyle{" + super.toString() + "pattern=[" + pattern + "]}"; 
    6569    } 
    6670} 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/NodeElemStyle.java

    r4820 r4822  
    66import java.awt.BasicStroke; 
    77import java.awt.Color; 
     8import java.awt.Image; 
    89import java.awt.Rectangle; 
    910import java.awt.Stroke; 
    1011 
    11 import javax.swing.GrayFilter; 
    1212import javax.swing.ImageIcon; 
    1313 
     
    2727 */ 
    2828public class NodeElemStyle extends ElemStyle { 
    29     public ImageIcon icon; 
    30     public int iconAlpha; 
     29    public MapImage<Image> mapImage; 
    3130    public Symbol symbol; 
    3231 
     
    9695    public static final StyleList DEFAULT_NODE_STYLELIST_TEXT = new StyleList(NodeElemStyle.SIMPLE_NODE_ELEMSTYLE, BoxTextElemStyle.SIMPLE_NODE_TEXT_ELEMSTYLE); 
    9796 
    98     protected NodeElemStyle(Cascade c, ImageIcon icon, Integer iconAlpha, Symbol symbol) { 
     97    protected NodeElemStyle(Cascade c, MapImage<Image> mapImage, Symbol symbol) { 
    9998        super(c, 1000f); 
    100         this.icon = icon; 
    101         this.iconAlpha = iconAlpha == null ? 0 : iconAlpha; 
     99        this.mapImage = mapImage; 
    102100        this.symbol = symbol; 
    103101    } 
     
    110108        Cascade c = env.mc.getCascade(env.layer); 
    111109 
    112         Pair<ImageIcon, Integer> icon = createIcon(env); 
     110        MapImage<Image> mapImage = createIcon(env); 
    113111        Symbol symbol = null; 
    114         if (icon == null) { 
     112        if (mapImage == null) { 
    115113            symbol = createSymbol(env); 
    116114        } 
    117115 
    118         // optimization: if we neither have a symbol, nor an icon 
     116        // optimization: if we neither have a symbol, nor a mapImage 
    119117        // we don't have to check for the remaining style properties and we don't 
    120118        // have to allocate a node element style. 
    121         if (!allowDefault && symbol == null && icon == null) return null; 
    122  
    123         return new NodeElemStyle(c, 
    124                 icon == null ? null : icon.a, 
    125                 icon == null ? null : icon.b, 
    126                 symbol); 
    127     } 
    128  
    129     private static Pair<ImageIcon, Integer> createIcon(Environment env) { 
     119        if (!allowDefault && symbol == null && mapImage == null) return null; 
     120 
     121        return new NodeElemStyle(c, mapImage, symbol); 
     122    } 
     123 
     124    private static MapImage<Image> createIcon(Environment env) { 
    130125        Cascade c = env.mc.getCascade(env.layer); 
    131126        Cascade c_def = env.mc.getCascade("default"); 
     
    150145        int height = heightF == null ? -1 : Math.round(heightF); 
    151146 
     147        MapImage<Image> mapImage = new MapImage<Image>(iconRef.iconName, iconRef.source); 
     148 
    152149        ImageIcon icon = MapPaintStyles.getIcon(iconRef, width, height); 
    153         if (icon == null) 
    154             return new Pair<ImageIcon, Integer>(MapPaintStyles.getNoIcon_Icon(iconRef.source), 255); 
    155         int iconAlpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.icon-image-alpha", 255)))); 
    156         Integer pAlpha = Utils.color_float2int(c.get("icon-opacity", null, float.class)); 
    157         if (pAlpha != null) { 
    158             iconAlpha = pAlpha; 
    159         } 
    160  
    161         return new Pair<ImageIcon, Integer>(icon, iconAlpha); 
     150        if (icon == null) { 
     151            mapImage.img = MapPaintStyles.getNoIcon_Icon(iconRef.source).getImage(); 
     152        } else { 
     153            mapImage.img = icon.getImage(); 
     154            mapImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.icon-image-alpha", 255)))); 
     155            Integer pAlpha = Utils.color_float2int(c.get("icon-opacity", null, float.class)); 
     156            if (pAlpha != null) { 
     157                mapImage.alpha = pAlpha; 
     158            } 
     159            mapImage.width = width; 
     160            mapImage.height = height; 
     161        } 
     162        return mapImage; 
    162163    } 
    163164 
     
    241242        if (primitive instanceof Node) { 
    242243            Node n = (Node) primitive; 
    243             if (icon != null && painter.isShowIcons()) { 
    244                 painter.drawNodeIcon(n, (painter.isInactiveMode() || n.isDisabled()) ? getDisabledIcon() : icon, 
    245                         Utils.color_int2float(iconAlpha), selected, member); 
     244            if (mapImage != null && painter.isShowIcons()) { 
     245                painter.drawNodeIcon(n, (painter.isInactiveMode() || n.isDisabled()) ? mapImage.getDisabled() : mapImage.img, 
     246                        Utils.color_int2float(mapImage.alpha), selected, member); 
    246247            } else if (symbol != null) { 
    247248                Color fillColor = symbol.fillColor; 
     
    303304 
    304305            } 
    305         } else if (primitive instanceof Relation && icon != null) { 
    306             painter.drawRestriction((Relation) primitive, this); 
    307         } 
    308     } 
    309  
    310     public ImageIcon getDisabledIcon() { 
    311         if (disabledIcon != null) 
    312             return disabledIcon; 
    313         if (icon == null) 
    314             return null; 
    315         return disabledIcon = new ImageIcon(GrayFilter.createDisabledImage(icon.getImage())); 
     306        } else if (primitive instanceof Relation && mapImage != null) { 
     307            painter.drawRestriction((Relation) primitive, mapImage); 
     308        } 
    316309    } 
    317310 
    318311    public Rectangle getBox() { 
    319         if (icon != null) { 
    320             int w = icon.getIconWidth(), h=icon.getIconHeight(); 
     312        if (mapImage != null) { 
     313            int w = mapImage.img.getWidth(null), h = mapImage.img.getHeight(null); 
    321314            return new Rectangle(-w/2, -h/2, w, h); 
    322315        } else if (symbol != null) { 
     
    338331    public int hashCode() { 
    339332        int hash = super.hashCode(); 
    340         hash = 17 * hash + (icon != null ? icon.getImage().hashCode() : 0); 
    341         hash = 17 * hash + iconAlpha; 
     333        hash = 17 * hash + (mapImage != null ? mapImage.hashCode() : 0); 
    342334        hash = 17 * hash + (symbol != null ? symbol.hashCode() : 0); 
    343335        return hash; 
     
    353345        final NodeElemStyle other = (NodeElemStyle) obj; 
    354346        // we should get the same image object due to caching 
    355         if (icon != other.icon && (icon == null || other.icon == null || icon.getImage() != other.icon.getImage())) 
    356             return false; 
    357         if (this.iconAlpha != other.iconAlpha) 
     347        if (!equal(mapImage, other.mapImage)) 
    358348            return false; 
    359349        if (!equal(symbol, other.symbol)) 
     
    362352    } 
    363353 
    364  
    365354    @Override 
    366355    public String toString() { 
    367356        StringBuilder s = new StringBuilder("NodeElemStyle{"); 
    368357        s.append(super.toString()); 
    369         if (icon != null) { 
    370             s.append(" icon=" + icon + " iconAlpha=" + iconAlpha); 
     358        if (mapImage != null) { 
     359            s.append(" icon=[" + mapImage + "]"); 
    371360        } 
    372361        if (symbol != null) { 
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r4820 r4822  
    760760     * @return the image after rotating. 
    761761     */ 
    762     public static ImageIcon createRotatedImage(Component c, Icon icon, double rotatedAngle) { 
     762    public static Image createRotatedImage(Component c, Image img, double rotatedAngle) { 
    763763        // convert rotatedAngle to a value from 0 to 360 
    764764        double originalAngle = rotatedAngle % 360; 
     
    775775        double radian = Math.toRadians(angle); 
    776776 
    777         int iw = icon.getIconWidth(); 
    778         int ih = icon.getIconHeight(); 
     777        int iw = img.getWidth(null); 
     778        int ih = img.getHeight(null); 
    779779        int w; 
    780780        int h; 
     
    802802 
    803803        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); 
    804         icon.paintIcon(c, g2d, -cx, -cy); 
     804        g2d.drawImage(img, -cx, -cy, c); 
    805805 
    806806        g2d.dispose(); 
    807         return new ImageIcon(image); 
     807        new ImageIcon(image); // load completely 
     808        return image; 
    808809    } 
    809810 
Note: See TracChangeset for help on using the changeset viewer.