1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trc;
|
---|
6 |
|
---|
7 | import java.awt.Color;
|
---|
8 | import java.awt.Component;
|
---|
9 | import java.awt.Font;
|
---|
10 | import java.awt.Graphics;
|
---|
11 | import java.awt.event.ActionEvent;
|
---|
12 | import java.awt.image.BufferedImage;
|
---|
13 | import java.awt.image.BufferedImageOp;
|
---|
14 | import java.awt.image.ConvolveOp;
|
---|
15 | import java.awt.image.Kernel;
|
---|
16 | import java.util.List;
|
---|
17 |
|
---|
18 | import javax.swing.AbstractAction;
|
---|
19 | import javax.swing.Icon;
|
---|
20 | import javax.swing.JCheckBoxMenuItem;
|
---|
21 | import javax.swing.JMenu;
|
---|
22 | import javax.swing.JMenuItem;
|
---|
23 | import javax.swing.JSeparator;
|
---|
24 |
|
---|
25 | import org.openstreetmap.josm.Main;
|
---|
26 | import org.openstreetmap.josm.actions.ImageryAdjustAction;
|
---|
27 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
28 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
29 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
30 | import org.openstreetmap.josm.data.imagery.OffsetBookmark;
|
---|
31 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
32 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
33 |
|
---|
34 | public abstract class ImageryLayer extends Layer {
|
---|
35 | protected static final Icon icon = ImageProvider.get("imagery_small");
|
---|
36 |
|
---|
37 | public static final IntegerProperty PROP_FADE_AMOUNT = new IntegerProperty("imagery.fade_amount", 0);
|
---|
38 | public static final IntegerProperty PROP_SHARPEN_LEVEL = new IntegerProperty("imagery.sharpen_level", 0);
|
---|
39 |
|
---|
40 | public static Color getFadeColor() {
|
---|
41 | return Main.pref.getColor("imagery.fade", Color.white);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public static Color getFadeColorWithAlpha() {
|
---|
45 | Color c = getFadeColor();
|
---|
46 | return new Color(c.getRed(),c.getGreen(),c.getBlue(),PROP_FADE_AMOUNT.get()*255/100);
|
---|
47 | }
|
---|
48 |
|
---|
49 | public static void setFadeColor(Color color) {
|
---|
50 | Main.pref.putColor("imagery.fade", color);
|
---|
51 | }
|
---|
52 |
|
---|
53 | protected final ImageryInfo info;
|
---|
54 |
|
---|
55 | protected double dx = 0.0;
|
---|
56 | protected double dy = 0.0;
|
---|
57 |
|
---|
58 | protected int sharpenLevel;
|
---|
59 |
|
---|
60 | public ImageryLayer(ImageryInfo info) {
|
---|
61 | super(info.getName());
|
---|
62 | this.info = info;
|
---|
63 | this.sharpenLevel = PROP_SHARPEN_LEVEL.get();
|
---|
64 | }
|
---|
65 |
|
---|
66 | public double getPPD(){
|
---|
67 | if (Main.map == null || Main.map.mapView == null) return Main.proj.getDefaultZoomInPPD();
|
---|
68 | ProjectionBounds bounds = Main.map.mapView.getProjectionBounds();
|
---|
69 | return Main.map.mapView.getWidth() / (bounds.max.east() - bounds.min.east());
|
---|
70 | }
|
---|
71 |
|
---|
72 | public double getDx() {
|
---|
73 | return dx;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public double getDy() {
|
---|
77 | return dy;
|
---|
78 | }
|
---|
79 |
|
---|
80 | public void setOffset(double dx, double dy) {
|
---|
81 | this.dx = dx;
|
---|
82 | this.dy = dy;
|
---|
83 | }
|
---|
84 |
|
---|
85 | public void displace(double dx, double dy) {
|
---|
86 | setOffset(this.dx += dx, this.dy += dy);
|
---|
87 | }
|
---|
88 |
|
---|
89 | public ImageryInfo getInfo() {
|
---|
90 | return info;
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public Icon getIcon() {
|
---|
95 | return icon;
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | public boolean isMergable(Layer other) {
|
---|
100 | return false;
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | public void mergeFrom(Layer from) {
|
---|
105 | }
|
---|
106 |
|
---|
107 | @Override
|
---|
108 | public Object getInfoComponent() {
|
---|
109 | return getToolTipText();
|
---|
110 | }
|
---|
111 |
|
---|
112 | public static ImageryLayer create(ImageryInfo info) {
|
---|
113 | if (info.getImageryType() == ImageryType.WMS || info.getImageryType() == ImageryType.HTML)
|
---|
114 | return new WMSLayer(info);
|
---|
115 | else if (info.getImageryType() == ImageryType.TMS || info.getImageryType() == ImageryType.BING)
|
---|
116 | return new TMSLayer(info);
|
---|
117 | else throw new AssertionError();
|
---|
118 | }
|
---|
119 |
|
---|
120 | class ApplyOffsetAction extends AbstractAction {
|
---|
121 | private OffsetBookmark b;
|
---|
122 | ApplyOffsetAction(OffsetBookmark b) {
|
---|
123 | super(b.name);
|
---|
124 | this.b = b;
|
---|
125 | }
|
---|
126 |
|
---|
127 | @Override
|
---|
128 | public void actionPerformed(ActionEvent ev) {
|
---|
129 | setOffset(b.dx, b.dy);
|
---|
130 | Main.main.menu.imageryMenuUpdater.refreshOffsetMenu();
|
---|
131 | Main.map.repaint();
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | public class OffsetAction extends AbstractAction implements LayerAction {
|
---|
136 | @Override
|
---|
137 | public void actionPerformed(ActionEvent e) {
|
---|
138 | }
|
---|
139 |
|
---|
140 | @Override
|
---|
141 | public Component createMenuComponent() {
|
---|
142 | return getOffsetMenuItem();
|
---|
143 | }
|
---|
144 |
|
---|
145 | @Override
|
---|
146 | public boolean supportLayers(List<Layer> layers) {
|
---|
147 | return false;
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | ImageryAdjustAction adjustAction = new ImageryAdjustAction(this);
|
---|
152 |
|
---|
153 | public JMenuItem getOffsetMenuItem() {
|
---|
154 | JMenuItem adjustMenuItem = new JMenuItem(adjustAction);
|
---|
155 | if (OffsetBookmark.allBookmarks.isEmpty()) return adjustMenuItem;
|
---|
156 |
|
---|
157 | JMenu subMenu = new JMenu(trc("layer", "Offset"));
|
---|
158 | subMenu.setIcon(ImageProvider.get("mapmode", "adjustimg"));
|
---|
159 | subMenu.add(adjustMenuItem);
|
---|
160 | subMenu.add(new JSeparator());
|
---|
161 | boolean hasBookmarks = false;
|
---|
162 | for (OffsetBookmark b : OffsetBookmark.allBookmarks) {
|
---|
163 | if (!b.isUsable(this)) {
|
---|
164 | continue;
|
---|
165 | }
|
---|
166 | JCheckBoxMenuItem item = new JCheckBoxMenuItem(new ApplyOffsetAction(b));
|
---|
167 | if (b.dx == dx && b.dy == dy) {
|
---|
168 | item.setSelected(true);
|
---|
169 | }
|
---|
170 | subMenu.add(item);
|
---|
171 | hasBookmarks = true;
|
---|
172 | }
|
---|
173 | return hasBookmarks ? subMenu : adjustMenuItem;
|
---|
174 | }
|
---|
175 |
|
---|
176 | public BufferedImage sharpenImage(BufferedImage img) {
|
---|
177 | if (sharpenLevel <= 0) return img;
|
---|
178 | int width = img.getWidth(null);
|
---|
179 | int height = img.getHeight(null);
|
---|
180 | BufferedImage tmp = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
---|
181 | tmp.getGraphics().drawImage(img, 0, 0, null);
|
---|
182 | Kernel kernel;
|
---|
183 | if (sharpenLevel == 1) {
|
---|
184 | kernel = new Kernel(3, 3, new float[] { -0.25f, -0.5f, -0.25f, -0.5f, 4, -0.5f, -0.25f, -0.5f, -0.25f});
|
---|
185 | } else {
|
---|
186 | kernel = new Kernel(3, 3, new float[] { -0.5f, -1, -0.5f, -1, 7, -1, -0.5f, -1, -0.5f});
|
---|
187 | }
|
---|
188 | BufferedImageOp op = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
|
---|
189 | return op.filter(tmp, null);
|
---|
190 | }
|
---|
191 |
|
---|
192 | public void drawErrorTile(BufferedImage img) {
|
---|
193 | Graphics g = img.getGraphics();
|
---|
194 | g.setColor(Color.RED);
|
---|
195 | g.fillRect(0, 0, img.getWidth(), img.getHeight());
|
---|
196 | g.setFont(g.getFont().deriveFont(Font.PLAIN).deriveFont(36.0f));
|
---|
197 | g.setColor(Color.BLACK);
|
---|
198 | g.drawString(tr("ERROR"), 30, img.getHeight()/2);
|
---|
199 | }
|
---|
200 | }
|
---|