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