source: josm/trunk/src/org/openstreetmap/josm/gui/layer/imagery/ReprojectionTile.java@ 11882

Last change on this file since 11882 was 11882, checked in by bastiK, 7 years ago

see #7427 - minor style change

  • Property svn:eol-style set to native
File size: 7.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.imagery;
3
4import java.awt.Dimension;
5import java.awt.geom.Point2D;
6import java.awt.image.BufferedImage;
7
8import org.openstreetmap.gui.jmapviewer.Tile;
9import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.data.ProjectionBounds;
12import org.openstreetmap.josm.data.coor.EastNorth;
13import org.openstreetmap.josm.data.projection.Projection;
14import org.openstreetmap.josm.data.projection.Projections;
15import org.openstreetmap.josm.tools.ImageWarp;
16import org.openstreetmap.josm.tools.Utils;
17
18/**
19 * Tile class that stores a reprojected version of the original tile.
20 * @since 11858
21 */
22public class ReprojectionTile extends Tile {
23
24 protected TileAnchor anchor;
25 private double nativeScale;
26 protected boolean maxZoomReached;
27
28 /**
29 * Constructs a new {@code ReprojectionTile}.
30 * @param source sourec tile
31 * @param xtile X coordinate
32 * @param ytile Y coordinate
33 * @param zoom zoom level
34 */
35 public ReprojectionTile(TileSource source, int xtile, int ytile, int zoom) {
36 super(source, xtile, ytile, zoom);
37 }
38
39 /**
40 * Get the position of the tile inside the image.
41 * @return the position of the tile inside the image
42 * @see #getImage()
43 */
44 public TileAnchor getAnchor() {
45 return anchor;
46 }
47
48 public double getNativeScale() {
49 return nativeScale;
50 }
51
52 public boolean needsUpdate(double currentScale) {
53 if (Utils.equalsEpsilon(nativeScale, currentScale))
54 return false;
55 if (maxZoomReached && currentScale < nativeScale)
56 // zoomed in even more - max zoom already reached, so no update
57 return false;
58 return true;
59 }
60
61 @Override
62 public void setImage(BufferedImage image) {
63 if (image == null) {
64 reset();
65 } else {
66 transform(image);
67 }
68 }
69
70 private synchronized void reset() {
71 this.image = null;
72 this.anchor = null;
73 this.maxZoomReached = false;
74 }
75
76 public void transform(BufferedImage imageIn) {
77 if (!Main.isDisplayingMapView()) {
78 reset();
79 return;
80 }
81 double scaleMapView = Main.map.mapView.getScale();
82 ImageWarp.Interpolation interpolation;
83 switch (Main.pref.get("imagery.warp.interpolation", "bilinear")) {
84 case "nearest_neighbor":
85 interpolation = ImageWarp.Interpolation.NEAREST_NEIGHBOR;
86 break;
87 default:
88 interpolation = ImageWarp.Interpolation.BILINEAR;
89 }
90 double margin = interpolation.getMargin();
91
92 Projection projCurrent = Main.getProjection();
93 Projection projServer = Projections.getProjectionByCode(source.getServerCRS());
94 EastNorth en00Server = new EastNorth(source.tileXYtoProjected(xtile, ytile, zoom));
95 EastNorth en11Server = new EastNorth(source.tileXYtoProjected(xtile + 1, ytile + 1, zoom));
96 ProjectionBounds pbServer = new ProjectionBounds(en00Server);
97 pbServer.extend(en11Server);
98 // find east-north rectangle in current projection, that will fully contain the tile
99 ProjectionBounds pbTarget = projCurrent.getEastNorthBoundsBox(pbServer, projServer);
100
101 // add margin and align to pixel grid
102 double minEast = Math.floor(pbTarget.minEast / scaleMapView - margin) * scaleMapView;
103 double minNorth = -Math.floor(-(pbTarget.minNorth / scaleMapView - margin)) * scaleMapView;
104 double maxEast = Math.ceil(pbTarget.maxEast / scaleMapView + margin) * scaleMapView;
105 double maxNorth = -Math.ceil(-(pbTarget.maxNorth / scaleMapView + margin)) * scaleMapView;
106 ProjectionBounds pbTargetAligned = new ProjectionBounds(minEast, minNorth, maxEast, maxNorth);
107
108 Dimension dim = getDimension(pbTargetAligned, scaleMapView);
109 Integer scaleFix = limitScale(source.getTileSize(), Math.sqrt(dim.getWidth() * dim.getHeight()));
110 double scale = scaleFix == null ? scaleMapView : (scaleMapView * scaleFix);
111
112 ImageWarp.PointTransform pointTransform = pt -> {
113 EastNorth target = new EastNorth(pbTargetAligned.minEast + pt.getX() * scale,
114 pbTargetAligned.maxNorth - pt.getY() * scale);
115 EastNorth sourceEN = projServer.latlon2eastNorth(projCurrent.eastNorth2latlon(target));
116 double x = source.getTileSize() *
117 (sourceEN.east() - pbServer.minEast) / (pbServer.maxEast - pbServer.minEast);
118 double y = source.getTileSize() *
119 (pbServer.maxNorth - sourceEN.north()) / (pbServer.maxNorth - pbServer.minNorth);
120 return new Point2D.Double(x, y);
121 };
122
123 // pixel coordinates of tile origin and opposite tile corner inside the target image
124 // (tile may be deformed / rotated by reprojection)
125 EastNorth en00Current = projCurrent.latlon2eastNorth(projServer.eastNorth2latlon(en00Server));
126 EastNorth en11Current = projCurrent.latlon2eastNorth(projServer.eastNorth2latlon(en11Server));
127 Point2D p00Img = new Point2D.Double(
128 (en00Current.east() - pbTargetAligned.minEast) / scale,
129 (pbTargetAligned.maxNorth - en00Current.north()) / scale);
130 Point2D p11Img = new Point2D.Double(
131 (en11Current.east() - pbTargetAligned.minEast) / scale,
132 (pbTargetAligned.maxNorth - en11Current.north()) / scale);
133
134 BufferedImage imageOut = ImageWarp.warp(
135 imageIn, getDimension(pbTargetAligned, scale), pointTransform,
136 interpolation);
137 synchronized (this) {
138 this.image = imageOut;
139 this.anchor = new TileAnchor(p00Img, p11Img);
140 this.nativeScale = scale;
141 this.maxZoomReached = scaleFix != null;
142 }
143 }
144
145 private Dimension getDimension(ProjectionBounds bounds, double scale) {
146 return new Dimension(
147 (int) Math.round((bounds.maxEast - bounds.minEast) / scale),
148 (int) Math.round((bounds.maxNorth - bounds.minNorth) / scale));
149 }
150
151 /**
152 * Make sure, the image is not scaled up too much.
153 *
154 * This would not give any significant improvement in image quality and may
155 * exceed the user's memory. The correction factor is a power of 2.
156 * @param lenOrig tile size of original image
157 * @param lenNow (averaged) tile size of warped image
158 * @return factor to shrink if limit is exceeded; 1 if it is already at the
159 * limit, but no change needed; null if it is well below the limit and can
160 * still be scaled up by at least a factor of 2.
161 */
162 protected Integer limitScale(double lenOrig, double lenNow) {
163 final double limit = 3;
164 if (lenNow > limit * lenOrig) {
165 int n = (int) Math.ceil((Math.log(lenNow) - Math.log(limit * lenOrig)) / Math.log(2));
166 int f = 1 << n;
167 double lenNowFixed = lenNow / f;
168 if (lenNowFixed > limit * lenOrig) throw new AssertionError();
169 if (lenNowFixed <= limit * lenOrig / 2) throw new AssertionError();
170 return f;
171 }
172 if (lenNow > limit * lenOrig / 2)
173 return 1;
174 return null;
175 }
176}
Note: See TracBrowser for help on using the repository browser.