File: WarpGrid.java

package info (click to toggle)
libjhlabs-filters-java 2.0.235-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,132 kB
  • sloc: java: 16,740; xml: 17; sh: 11; makefile: 5
file content (356 lines) | stat: -rw-r--r-- 8,919 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
Copyright 2006 Jerry Huxtable

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.jhlabs.image;

import java.awt.*;
import java.awt.image.*;

/**
 * A warp grid.
 * From "A simplified approach to Image Processing" by Randy Crane
 */
public class WarpGrid implements java.io.Serializable {

	static final long serialVersionUID = 4312410199770201968L;
	
	public float[] xGrid = null;
	public float[] yGrid = null;
	public int rows, cols;

	public WarpGrid(int rows, int cols, int w, int h) {
		this.rows = rows;
		this.cols = cols;
		xGrid = new float[rows*cols];
		yGrid = new float[rows*cols];
		int index = 0;
		for (int row = 0; row < rows; row++) {
			for (int col = 0; col < cols; col++) {
				xGrid[index] = (float)col*(w-1)/(cols-1);
				yGrid[index] = (float)row*(h-1)/(rows-1);
				index++;
			}
		}
	}

	/**
	 * Add a new row to the grid. "before" must be in the range 1..rows-1. i.e. you can only add rows inside the grid.
	 */
	public void addRow( int before ) {
		int size = (rows+1) * cols;
		float[] x = new float[size];
		float[] y = new float[size];

		rows++;
		int i = 0;
		int j = 0;
		for (int row = 0; row < rows; row++) {
			for (int col = 0; col < cols; col++) {
				int k = j+col;
				int l = i+col;
				if ( row == before ) {
					x[k] = (xGrid[l]+xGrid[k])/2;
					y[k] = (yGrid[l]+yGrid[k])/2;
				} else {
					x[k] = xGrid[l];
					y[k] = yGrid[l];
				}
			}
			if ( row != before-1 )
				i += cols;
			j += cols;
		}
		xGrid = x;
		yGrid = y;
	}

	/**
	 * Add a new column to the grid. "before" must be in the range 1..cols-1. i.e. you can only add columns inside the grid.
	 */
	public void addCol( int before ) {
		int size = rows * (cols+1);
		float[] x = new float[size];
		float[] y = new float[size];

		cols++;
int i = 0;
int j = 0;
		for (int row = 0; row < rows; row++) {
//			int i = row*(cols-1);
//			int j = row*cols;
			for (int col = 0; col < cols; col++) {
				if ( col == before ) {
					x[j] = (xGrid[i]+xGrid[i-1])/2;
					y[j] = (yGrid[i]+yGrid[i-1])/2;
				} else {
					x[j] = xGrid[i];
					y[j] = yGrid[i];
					i++;
				}
				j++;
			}
		}
		xGrid = x;
		yGrid = y;
	}

	/**
	 * Remove a row from the grid.
	 */
	public void removeRow( int r ) {
		int size = (rows-1) * cols;
		float[] x = new float[size];
		float[] y = new float[size];

		rows--;
		int i = 0;
		int j = 0;
		for (int row = 0; row < rows; row++) {
			for (int col = 0; col < cols; col++) {
				int k = j+col;
				int l = i+col;
				x[k] = xGrid[l];
				y[k] = yGrid[l];
			}
			if ( row == r-1 )
				i += cols;
			i += cols;
			j += cols;
		}
		xGrid = x;
		yGrid = y;
	}

	/**
	 * Remove a column from the grid.
	 */
	public void removeCol( int r ) {
		int size = rows * (cols+1);
		float[] x = new float[size];
		float[] y = new float[size];

		cols--;
		for (int row = 0; row < rows; row++) {
			int i = row*(cols+1);
			int j = row*cols;
			for (int col = 0; col < cols; col++) {
				x[j] = xGrid[i];
				y[j] = yGrid[i];
				if ( col == r-1 )
					i++;
				i++;
				j++;
			}
		}
		xGrid = x;
		yGrid = y;
	}

	public void lerp(float t, WarpGrid destination, WarpGrid intermediate) {
		if (rows != destination.rows || cols != destination.cols)
			throw new IllegalArgumentException("source and destination are different sizes");
		if (rows != intermediate.rows || cols != intermediate.cols)
			throw new IllegalArgumentException("source and intermediate are different sizes");
		int index = 0;
		for (int row = 0; row < rows; row++) {
			for (int col = 0; col < cols; col++) {
				intermediate.xGrid[index] = (float)ImageMath.lerp(t, xGrid[index], destination.xGrid[index]);
				intermediate.yGrid[index] = (float)ImageMath.lerp(t, yGrid[index], destination.yGrid[index]);
				index++;
			}
		}
	}
	
	public void warp(int[] inPixels, int cols, int rows, WarpGrid sourceGrid, WarpGrid destGrid, int[] outPixels) {
try {
		int x, y;
		int u, v;
		int[] intermediate;
		WarpGrid splines;

		if (sourceGrid.rows != destGrid.rows || sourceGrid.cols != destGrid.cols)
			throw new IllegalArgumentException("source and destination grids are different sizes");

		int size = Math.max(cols, rows);
		float[] xrow = new float[size];
		float[] yrow = new float[size];
		float[] scale  = new float[size + 1];
		float[] interpolated = new float[size + 1];

		int gridCols = sourceGrid.cols;
		int gridRows = sourceGrid.rows;

		splines = new WarpGrid(rows, gridCols, 1, 1);

		for (u = 0; u < gridCols;u++) {
			int i = u;

			for (v = 0; v < gridRows;v++) {
				xrow[v] = sourceGrid.xGrid[i];
				yrow[v] = sourceGrid.yGrid[i];
				i += gridCols;
			}

			interpolateSpline(yrow, xrow, 0, gridRows, interpolated, 0, rows);

			i = u;
			for (y = 0;y < rows;y++) {
				splines.xGrid[i] = interpolated[y];
				i += gridCols;
			}
		}

		for (u = 0; u < gridCols;u++) {
			int i = u;

			for (v = 0; v < gridRows;v++) {
				xrow[v] = destGrid.xGrid[i];
				yrow[v] = destGrid.yGrid[i];
				i += gridCols;
			}

			interpolateSpline(yrow, xrow, 0, gridRows, interpolated, 0, rows);

			i = u;
			for (y = 0;y < rows; y++) {
				splines.yGrid[i] = interpolated[y];
				i += gridCols;
			}
		}

		/* first pass: warp x using splines */
		intermediate = new int[rows*cols];

		int offset = 0;
		for (y = 0; y < rows; y++) {
			/* fit spline to x-intercepts;resample over all cols */
			interpolateSpline(splines.xGrid, splines.yGrid, offset, gridCols, scale, 0, cols);
			scale[cols] = cols;
			ImageMath.resample(inPixels, intermediate, cols, y*cols, 1, scale);
			offset += gridCols;
		}
		/* create table of y-intercepts for intermediate mesh's hor splines */

		splines = new WarpGrid(gridRows, cols, 1, 1);

		offset = 0;
		int offset2 = 0;
		for (v = 0; v < gridRows; v++) {
			interpolateSpline(sourceGrid.xGrid, sourceGrid.yGrid, offset, gridCols, splines.xGrid, offset2, cols);
			offset += gridCols;
			offset2 += cols;
		}

		offset = 0;
		offset2 = 0;
		for (v = 0; v < gridRows; v++) {
			interpolateSpline(destGrid.xGrid, destGrid.yGrid, offset, gridCols, splines.yGrid, offset2, cols);
			offset += gridCols;
			offset2 += cols;
		}

		/* second pass: warp y */

		for (x = 0; x < cols; x++) {
			int i = x;
			
			for (v = 0; v < gridRows; v++) {
				xrow[v] = splines.xGrid[i];;
				yrow[v] = splines.yGrid[i];;
				i += cols;
			}

			interpolateSpline(xrow, yrow, 0, gridRows, scale, 0, rows);
			scale[rows] = rows;
			ImageMath.resample(intermediate, outPixels, rows, x, cols, scale);
		}
}
catch (Exception e) {
	e.printStackTrace();
}
	}

	private final static float m00 = -0.5f;
	private final static float m01 =  1.5f;
	private final static float m02 = -1.5f;
	private final static float m03 =  0.5f;
	private final static float m10 =  1.0f;
	private final static float m11 = -2.5f;
	private final static float m12 =  2.0f;
	private final static float m13 = -0.5f;
	private final static float m20 = -0.5f;
	private final static float m22 =  0.5f;
	private final static float m31 =  1.0f;

	protected void interpolateSpline(float[] xKnots, float[] yKnots, int offset, int length, float[] splineY, int splineOffset, int splineLength) {
		int index = offset;
		int end = offset+length-1;
		float x0, x1;
		float k0, k1, k2, k3;
		float c0, c1, c2, c3;

		x0 = xKnots[index];
		k0 = k1 = k2 = yKnots[index];
		x1 = xKnots[index+1];
		k3 = yKnots[index+1];

		for (int i = 0;i < splineLength;i++) {
			if (index <= end && i > xKnots[index]) {
				k0 = k1;
				k1 = k2;
				k2 = k3;
				x0 = xKnots[index];
				index++;
				if ( index <= end )
					x1 = xKnots[index];
				if ( index < end )
					k3 = yKnots[index+1];
				else
					k3 = k2;
			}
			float t = (i - x0) / (x1 - x0);
			c3 = m00*k0 + m01*k1 + m02*k2 + m03*k3;
			c2 = m10*k0 + m11*k1 + m12*k2 + m13*k3;
			c1 = m20*k0 + m22*k2;
			c0 = m31*k1;
			
			splineY[splineOffset+i] = ((c3*t + c2)*t + c1)*t + c0;
		}
	}

	protected void interpolateSpline2(float[] xKnots, float[] yKnots, int offset, float[] splineY, int splineOffset, int splineLength) {
		int index = offset;
		float leftX, rightX;
		float leftY, rightY;

		leftX = xKnots[index];
		leftY = yKnots[index];
		rightX = xKnots[index+1];
		rightY = yKnots[index+1];

		for (int i = 0;i < splineLength;i++) {
			if (i > xKnots[index]) {
				leftX = xKnots[index];
				leftY = yKnots[index];
				index++;
				rightX = xKnots[index];
				rightY = yKnots[index];
			}
			float f = (i - leftX) / (rightX - leftX);
			splineY[splineOffset+i] = leftY + f * (rightY - leftY);
		}
	}
}