File: FieldWarpFilter.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 (176 lines) | stat: -rw-r--r-- 4,354 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
/*
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 class which warps an image using a field Warp algorithm.
 */
public class FieldWarpFilter extends TransformFilter {

	public static class Line {
		public int x1, y1, x2, y2;
		public int dx, dy;
		public float length, lengthSquared;
		
		public Line(int x1, int y1, int x2, int y2) {
			this.x1 = x1;
			this.y1 = y1;
			this.x2 = x2;
			this.y2 = y2;
		}
		
		public void setup() {
			dx = x2-x1;
			dy = y2-y1;
			lengthSquared = dx*dx + dy*dy;
			length = (float)Math.sqrt(lengthSquared);
		}
	}

	private float amount = 1.0f;
	private float power = 1.0f;
	private float strength = 2.0f;
	private Line[] inLines;
	private Line[] outLines;
	private Line[] intermediateLines;
	private float width, height;

	public FieldWarpFilter() {
	}

	public void setAmount(float amount) {
		this.amount = amount;
	}
	
	public float getAmount() {
		return amount;
	}
	
	public void setPower(float power) {
		this.power = power;
	}
	
	public float getPower() {
		return power;
	}
	
	public void setStrength(float strength) {
		this.strength = strength;
	}
	
	public float getStrength() {
		return strength;
	}
	
	public void setInLines( Line[] inLines ) {
		this.inLines = inLines;
	}
	
	public Line[] getInLines() {
		return inLines;
	}
	
	public void setOutLines( Line[] outLines ) {
		this.outLines = outLines;
	}
	
	public Line[] getOutLines() {
		return outLines;
	}
	
	protected void transform(int x, int y, Point out) {
	}

	protected void transformInverse(int x, int y, float[] out) {
		float u = 0, v = 0;
		float fraction = 0;
		float distance;
		float fdist;
		float weight;
		float a = 0.001f;
		float b = 1.5f*strength + 0.5f;
		float p = power;

		float totalWeight = 0.0f;
		float sumX = 0.0f;
		float sumY = 0.0f;

		for (int line = 0; line < inLines.length; line++) {
			Line l1 = inLines[line];
			Line l = intermediateLines[line];
			float dx = x - l.x1;
			float dy = y - l.y1;

			fraction = (dx * l.dx + dy * l.dy) / l.lengthSquared;
			fdist = (dy * l.dx - dx * l.dy) / l.length;
			if (fraction <= 0)
				distance = (float)Math.sqrt(dx*dx + dy*dy);
			else if (fraction >= 1) {
				dx = x - l.x2;
				dy = y - l.y2;
				distance = (float)Math.sqrt(dx*dx + dy*dy);
			} else if (fdist >= 0)
				distance = fdist;
			else
				distance = -fdist;
			u = l1.x1 + fraction * l1.dx - fdist * l1.dy / l1.length;
			v = l1.y1 + fraction * l1.dy + fdist * l1.dx / l1.length;

			weight = (float)Math.pow(Math.pow(l.length, p) / (a + distance), b);

			sumX += (u - x) * weight;
			sumY += (v - y) * weight;
//if (x % 10 == 0&&y == 20)System.out.println("distance="+distance+" weight="+weight+" sumX="+sumX+" sumY="+sumY+" u="+u+" v="+v);
			totalWeight += weight;
		}

//		out[0] = ImageMath.clamp(x + sumX / totalWeight + 0.5f, 0, width-1);
//		out[1] = ImageMath.clamp(y + sumY / totalWeight + 0.5f, 0, height-1);
		out[0] = x + sumX / totalWeight + 0.5f;
		out[1] = y + sumY / totalWeight + 0.5f;
	}

    public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
		this.width = width;
		this.height = height;
		if ( inLines != null && outLines != null ) {
			intermediateLines = new Line[inLines.length];
			for (int line = 0; line < inLines.length; line++) {
				Line l = intermediateLines[line] = new Line(
					ImageMath.lerp(amount, inLines[line].x1, outLines[line].x1),
					ImageMath.lerp(amount, inLines[line].y1, outLines[line].y1),
					ImageMath.lerp(amount, inLines[line].x2, outLines[line].x2),
					ImageMath.lerp(amount, inLines[line].y2, outLines[line].y2)
				);
				l.setup();
				inLines[line].setup();
			}
			dst = super.filter( src, dst );
			intermediateLines = null;
			return dst;
		}
		return src;
	}

	public String toString() {
		return "Distort/Field Warp...";
	}

}