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