File: CausticsFilter.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 (274 lines) | stat: -rw-r--r-- 6,178 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
/*
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.*;
import java.util.*;
import com.jhlabs.math.*;

/**
 * A filter which simulates underwater caustics. This can be animated to get a bottom-of-the-swimming-pool effect.
 */
public class CausticsFilter extends WholeImageFilter {

	private float scale = 32;
	private float angle = 0.0f;
	public int brightness = 10;
	public float amount = 1.0f;
	public float turbulence = 1.0f;
	public float dispersion = 0.0f;
	public float time = 0.0f;
	private int samples = 2;
	private int bgColor = 0xff799fff;

	private float s, c;

	public CausticsFilter() {
	}

	public void setScale(float scale) {
		this.scale = scale;
	}

	public float getScale() {
		return scale;
	}

	public void setBrightness(int brightness) {
		this.brightness = brightness;
	}

	public int getBrightness() {
		return brightness;
	}

	public void setTurbulence(float turbulence) {
		this.turbulence = turbulence;
	}

	public float getTurbulence() {
		return turbulence;
	}

	public void setAmount(float amount) {
		this.amount = amount;
	}
	
	public float getAmount() {
		return amount;
	}
	
	public void setDispersion(float dispersion) {
		this.dispersion = dispersion;
	}
	
	public float getDispersion() {
		return dispersion;
	}
	
	public void setTime(float time) {
		this.time = time;
	}
	
	public float getTime() {
		return time;
	}
	
	public void setSamples(int samples) {
		this.samples = samples;
	}
	
	public int getSamples() {
		return samples;
	}
	
	public void setBgColor(int c) {
		bgColor = c;
	}

	public int getBgColor() {
		return bgColor;
	}

	protected int[] filterPixels( int width, int height, int[] inPixels, Rectangle transformedSpace ) {
		Random random = new Random(0);

		s = (float)Math.sin(0.1);
		c = (float)Math.cos(0.1);

		int srcWidth = originalSpace.width;
		int srcHeight = originalSpace.height;
		int outWidth = transformedSpace.width;
		int outHeight = transformedSpace.height;
		int index = 0;
		int[] pixels = new int[outWidth * outHeight];

		for (int y = 0; y < outHeight; y++) {
			for (int x = 0; x < outWidth; x++) {
				pixels[index++] = bgColor;
			}
		}
		
		int v = brightness/samples;
		if (v == 0)
			v = 1;

		float rs = 1.0f/scale;
		float d = 0.95f;
		index = 0;
		for (int y = 0; y < outHeight; y++) {
			for (int x = 0; x < outWidth; x++) {
				for (int s = 0; s < samples; s++) {
					float sx = x+random.nextFloat();
					float sy = y+random.nextFloat();
					float nx = sx*rs;
					float ny = sy*rs;
					float xDisplacement, yDisplacement;
					float focus = 0.1f+amount;
					xDisplacement = evaluate(nx-d, ny) - evaluate(nx+d, ny);
					yDisplacement = evaluate(nx, ny+d) - evaluate(nx, ny-d);

					if (dispersion > 0) {
						for (int c = 0; c < 3; c++) {
							float ca = (1+c*dispersion);
							float srcX = sx + scale*focus * xDisplacement*ca;
							float srcY = sy + scale*focus * yDisplacement*ca;

							if (srcX < 0 || srcX >= outWidth-1 || srcY < 0 || srcY >= outHeight-1) {
							} else {
								int i = ((int)srcY)*outWidth+(int)srcX;
								int rgb = pixels[i];
								int r = (rgb >> 16) & 0xff;
								int g = (rgb >> 8) & 0xff;
								int b = rgb & 0xff;
								if (c == 2)
									r += v;
								else if (c == 1)
									g += v;
								else
									b += v;
								if (r > 255)
									r = 255;
								if (g > 255)
									g = 255;
								if (b > 255)
									b = 255;
								pixels[i] = 0xff000000 | (r << 16) | (g << 8) | b;
							}
						}
					} else {
						float srcX = sx + scale*focus * xDisplacement;
						float srcY = sy + scale*focus * yDisplacement;

						if (srcX < 0 || srcX >= outWidth-1 || srcY < 0 || srcY >= outHeight-1) {
						} else {
							int i = ((int)srcY)*outWidth+(int)srcX;
							int rgb = pixels[i];
							int r = (rgb >> 16) & 0xff;
							int g = (rgb >> 8) & 0xff;
							int b = rgb & 0xff;
							r += v;
							g += v;
							b += v;
							if (r > 255)
								r = 255;
							if (g > 255)
								g = 255;
							if (b > 255)
								b = 255;
							pixels[i] = 0xff000000 | (r << 16) | (g << 8) | b;
						}
					}
				}
			}
		}
		return pixels;
	}

	private static int add(int rgb, float brightness) {
		int r = (rgb >> 16) & 0xff;
		int g = (rgb >> 8) & 0xff;
		int b = rgb & 0xff;
		r += brightness;
		g += brightness;
		b += brightness;
		if (r > 255)
			r = 255;
		if (g > 255)
			g = 255;
		if (b > 255)
			b = 255;
		return 0xff000000 | (r << 16) | (g << 8) | b;
	}
	
	private static int add(int rgb, float brightness, int c) {
		int r = (rgb >> 16) & 0xff;
		int g = (rgb >> 8) & 0xff;
		int b = rgb & 0xff;
		if (c == 2)
			r += brightness;
		else if (c == 1)
			g += brightness;
		else
			b += brightness;
		if (r > 255)
			r = 255;
		if (g > 255)
			g = 255;
		if (b > 255)
			b = 255;
		return 0xff000000 | (r << 16) | (g << 8) | b;
	}
	
	public static float turbulence2(float x, float y, float time, float octaves) {
		float value = 0.0f;
		float remainder;
		float lacunarity = 2.0f;
		float f = 1.0f;
		int i;
		
		// to prevent "cascading" effects
		x += 371;
		y += 529;
		
		for (i = 0; i < (int)octaves; i++) {
			value += Noise.noise3(x, y, time) / f;
			x *= lacunarity;
			y *= lacunarity;
			f *= 2;
		}

		remainder = octaves - (int)octaves;
		if (remainder != 0)
			value += remainder * Noise.noise3(x, y, time) / f;

		return value;
	}

	protected float evaluate(float x, float y) {
		float xt = s*x + c*time;
		float tt = c*x - c*time;
		float f = turbulence == 0.0 ? Noise.noise3(xt, y, tt) : turbulence2(xt, y, tt, turbulence);
		return f;
	}
	
	public String toString() {
		return "Texture/Caustics...";
	}
	
}