source: josm/trunk/src/org/openstreetmap/josm/tools/ImageWarp.java@ 12808

Last change on this file since 12808 was 12798, checked in by Don-vip, 7 years ago

see #14794 - checkstyle

  • Property svn:eol-style set to native
File size: 7.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.awt.Dimension;
5import java.awt.geom.Point2D;
6import java.awt.geom.Rectangle2D;
7import java.awt.image.BufferedImage;
8import java.util.HashMap;
9import java.util.HashSet;
10import java.util.Map;
11import java.util.Set;
12
13/**
14 * Image warping algorithm.
15 *
16 * Deforms an image geometrically according to a given transformation formula.
17 * @since 11858
18 */
19public final class ImageWarp {
20
21 private ImageWarp() {
22 // Hide default constructor
23 }
24
25 /**
26 * Transformation that translates the pixel coordinates.
27 */
28 public interface PointTransform {
29 /**
30 * Translates pixel coordinates.
31 * @param pt pixel coordinates
32 * @return transformed pixel coordinates
33 */
34 Point2D transform(Point2D pt);
35 }
36
37 /**
38 * Wrapper that optimizes a given {@link ImageWarp.PointTransform}.
39 *
40 * It does so by spanning a grid with certain step size. It will invoke the
41 * potentially expensive master transform only at those grid points and use
42 * bilinear interpolation to approximate transformed values in between.
43 * <p>
44 * For memory optimization, this class assumes that rows are more or less scanned
45 * one-by-one as is done in {@link ImageWarp#warp}. I.e. this transform is <em>not</em>
46 * random access in the y coordinate.
47 */
48 public static class GridTransform implements ImageWarp.PointTransform {
49
50 private final double stride;
51 private final ImageWarp.PointTransform trfm;
52
53 private final Map<Integer, Map<Integer, Point2D>> cache;
54
55 private final boolean consistencyTest;
56 private final Set<Integer> deletedRows;
57
58 /**
59 * Create a new GridTransform.
60 * @param trfm the master transform, that needs to be optimized
61 * @param stride step size
62 */
63 public GridTransform(ImageWarp.PointTransform trfm, double stride) {
64 this.trfm = trfm;
65 this.stride = stride;
66 this.cache = new HashMap<>();
67 this.consistencyTest = Logging.isDebugEnabled();
68 if (consistencyTest) {
69 deletedRows = new HashSet<>();
70 } else {
71 deletedRows = null;
72 }
73 }
74
75 @Override
76 public Point2D transform(Point2D pt) {
77 int xIdx = (int) Math.floor(pt.getX() / stride);
78 int yIdx = (int) Math.floor(pt.getY() / stride);
79 double dx = pt.getX() / stride - xIdx;
80 double dy = pt.getY() / stride - yIdx;
81 Point2D value00 = getValue(xIdx, yIdx);
82 Point2D value01 = getValue(xIdx, yIdx + 1);
83 Point2D value10 = getValue(xIdx + 1, yIdx);
84 Point2D value11 = getValue(xIdx + 1, yIdx + 1);
85 double valueX = (value00.getX() * (1-dx) + value10.getX() * dx) * (1-dy) +
86 (value01.getX() * (1-dx) + value11.getX() * dx) * dy;
87 double valueY = (value00.getY() * (1-dx) + value10.getY() * dx) * (1-dy) +
88 (value01.getY() * (1-dx) + value11.getY() * dx) * dy;
89 return new Point2D.Double(valueX, valueY);
90 }
91
92 private Point2D getValue(int xIdx, int yIdx) {
93 Map<Integer, Point2D> row = getRow(yIdx);
94 Point2D val = row.get(xIdx);
95 if (val == null) {
96 val = trfm.transform(new Point2D.Double(xIdx * stride, yIdx * stride));
97 row.put(xIdx, val);
98 }
99 return val;
100 }
101
102 private Map<Integer, Point2D> getRow(int yIdx) {
103 cleanUp(yIdx - 3);
104 Map<Integer, Point2D> row = cache.get(yIdx);
105 if (row == null) {
106 row = new HashMap<>();
107 cache.put(yIdx, row);
108 if (consistencyTest) {
109 // should not create a row that has been deleted before
110 if (deletedRows.contains(yIdx)) throw new AssertionError();
111 // only ever cache 3 rows at once
112 if (cache.size() > 3) throw new AssertionError();
113 }
114 }
115 return row;
116 }
117
118 // remove rows from cache that will no longer be used
119 private void cleanUp(int yIdx) {
120 Map<Integer, Point2D> del = cache.remove(yIdx);
121 if (consistencyTest && del != null) {
122 // should delete each row only once
123 if (deletedRows.contains(yIdx)) throw new AssertionError();
124 deletedRows.add(yIdx);
125 }
126 }
127 }
128
129 /**
130 * Interpolation method.
131 */
132 public enum Interpolation {
133 /**
134 * Nearest neighbor.
135 *
136 * Simplest possible method. Faster, but not very good quality.
137 */
138 NEAREST_NEIGHBOR,
139
140 /**
141 * Bilinear.
142 *
143 * Decent quality.
144 */
145 BILINEAR;
146 }
147
148 /**
149 * Warp an image.
150 * @param srcImg the original image
151 * @param targetDim dimension of the target image
152 * @param invTransform inverse transformation (translates pixel coordinates
153 * of the target image to pixel coordinates of the original image)
154 * @param interpolation the interpolation method
155 * @return the warped image
156 */
157 public static BufferedImage warp(BufferedImage srcImg, Dimension targetDim, PointTransform invTransform, Interpolation interpolation) {
158 BufferedImage imgTarget = new BufferedImage(targetDim.width, targetDim.height, BufferedImage.TYPE_INT_ARGB);
159 Rectangle2D srcRect = new Rectangle2D.Double(0, 0, srcImg.getWidth(), srcImg.getHeight());
160 for (int j = 0; j < imgTarget.getHeight(); j++) {
161 for (int i = 0; i < imgTarget.getWidth(); i++) {
162 Point2D srcCoord = invTransform.transform(new Point2D.Double(i, j));
163 if (srcRect.contains(srcCoord)) {
164 int rgba;
165 switch (interpolation) {
166 case NEAREST_NEIGHBOR:
167 rgba = getColor((int) Math.round(srcCoord.getX()), (int) Math.round(srcCoord.getY()), srcImg);
168 break;
169 case BILINEAR:
170 int x0 = (int) Math.floor(srcCoord.getX());
171 double dx = srcCoord.getX() - x0;
172 int y0 = (int) Math.floor(srcCoord.getY());
173 double dy = srcCoord.getY() - y0;
174 int c00 = getColor(x0, y0, srcImg);
175 int c01 = getColor(x0, y0 + 1, srcImg);
176 int c10 = getColor(x0 + 1, y0, srcImg);
177 int c11 = getColor(x0 + 1, y0 + 1, srcImg);
178 rgba = 0;
179 // loop over color components: blue, green, red, alpha
180 for (int ch = 0; ch <= 3; ch++) {
181 int shift = 8 * ch;
182 int chVal = (int) Math.round(
183 (((c00 >> shift) & 0xff) * (1-dx) + ((c10 >> shift) & 0xff) * dx) * (1-dy) +
184 (((c01 >> shift) & 0xff) * (1-dx) + ((c11 >> shift) & 0xff) * dx) * dy);
185 rgba |= chVal << shift;
186 }
187 break;
188 default:
189 throw new AssertionError();
190 }
191 imgTarget.setRGB(i, j, rgba);
192 }
193 }
194 }
195 return imgTarget;
196 }
197
198 private static int getColor(int x, int y, BufferedImage img) {
199 // border strategy: continue with the color of the outermost pixel,
200 return img.getRGB(
201 Utils.clamp(x, 0, img.getWidth() - 1),
202 Utils.clamp(y, 0, img.getHeight() - 1));
203 }
204}
Note: See TracBrowser for help on using the repository browser.