-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeightMap.java
More file actions
53 lines (44 loc) · 1.53 KB
/
Copy pathHeightMap.java
File metadata and controls
53 lines (44 loc) · 1.53 KB
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
/*
George Zhang
Encapsulate an image (used to get pixel values).
*/
package geetransit.minecraft05.engine;
import java.nio.ByteBuffer;
public class HeightMap implements AutoCloseable {
public static int CHANNELS = 4;
public final ByteBuffer buffer;
public final int width;
public final int length;
public HeightMap(ByteBuffer buffer, int width, int length) {
this.buffer = buffer;
this.width = width;
this.length = length;
}
public static HeightMap loadFromImage(String fileName) throws Exception {
int width[] = {0}, length[] = {0};
ByteBuffer buffer = Utils.loadImage(fileName, width, length);
return new HeightMap(buffer, width[0], length[0]);
}
@Override
public void close() {
Utils.freeImage(this.buffer);
}
// usage: compressExpand(heightAt(...), 0, MAX_COLOR, 0, 16)
public static float compressExpand(int f, float cMin, float cMax, float eMin, float eMax) {
return expand(compress(f, cMin, cMax), eMin, eMax);
}
public static float compress(int f, float min, float max) { return (f-min) / (max-min); }
public static float expand(float f, float min, float max) { return min + f*(max-min); }
public int heightAt(int x, int z) { return this.heightAt(x*CHANNELS + z*CHANNELS*this.width); }
public int heightAt(int i) {
byte r = this.buffer.get(i + 0);
byte g = this.buffer.get(i + 1);
byte b = this.buffer.get(i + 2);
byte a = this.buffer.get(i + 3);
return 0
// | ((0xFF & a) << 24) // removed cuz it turns overflows int
| ((0xFF & r) << 16)
| ((0xFF & g) << 8)
| ((0xFF & b) << 0);
}
}