1 /**
2  * Copyright (c) 2011, Novyon Events
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * - Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * @author Anthyon
29  */
30 package com.jme3.terrain.noise.filter;
31 
32 import java.nio.FloatBuffer;
33 
34 public class SmoothFilter extends AbstractFilter {
35 
36 	private int radius;
37 	private float effect;
38 
setRadius(int radius)39 	public void setRadius(int radius) {
40 		this.radius = radius;
41 	}
42 
getRadius()43 	public int getRadius() {
44 		return this.radius;
45 	}
46 
setEffect(float effect)47 	public void setEffect(float effect) {
48 		this.effect = effect;
49 	}
50 
getEffect()51 	public float getEffect() {
52 		return this.effect;
53 	}
54 
55 	@Override
getMargin(int size, int margin)56 	public int getMargin(int size, int margin) {
57 		return super.getMargin(size, margin) + this.radius;
58 	}
59 
60 	@Override
filter(float sx, float sy, float base, FloatBuffer buffer, int size)61 	public FloatBuffer filter(float sx, float sy, float base, FloatBuffer buffer, int size) {
62 		float[] data = buffer.array();
63 		float[] retval = new float[data.length];
64 
65 		for (int y = this.radius; y < size - this.radius; y++) {
66 			for (int x = this.radius; x < size - this.radius; x++) {
67 				int idx = y * size + x;
68 				float n = 0;
69 				for (int i = -this.radius; i < this.radius + 1; i++) {
70 					for (int j = -this.radius; j < this.radius + 1; j++) {
71 						n += data[(y + i) * size + x + j];
72 					}
73 				}
74 				retval[idx] = this.effect * n / (4 * this.radius * (this.radius + 1) + 1) + (1 - this.effect) * data[idx];
75 			}
76 		}
77 
78 		return FloatBuffer.wrap(retval);
79 	}
80 }
81