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