-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuffer.java
More file actions
135 lines (107 loc) · 4.03 KB
/
Copy pathBuffer.java
File metadata and controls
135 lines (107 loc) · 4.03 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
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
package ptjava;
import java.awt.image.BufferedImage;
import java.util.concurrent.atomic.AtomicInteger;
import java.awt.Point;
public class Buffer {
public final int W, H;
public final Pixel[] Pixels;
private final byte[] imageBuffer;
public enum Channel {
ColorChannel,
VarianceChannel,
StandardDeviationChannel,
SamplesChannel
}
public Buffer(int width, int height, Pixel[] pixels) {
this.W = width;
this.H = height;
this.imageBuffer = new byte[256 * 4 * height];
this.Pixels = pixels;
}
public Buffer(int width, int height) {
this.W = width;
this.H = height;
this.imageBuffer = new byte[256 * 4 * height];
this.Pixels = new Pixel[width * height];
for (int i = 0; i < Pixels.length; i++) {
Pixels[i] = new Pixel();
}
}
public Buffer Copy() {
Pixel[] pixCopy = java.util.Arrays.stream(Pixels)
.parallel() // Parallel processing for large buffers
.map(Pixel::new) // Calls Pixel(Pixel other) constructor
.toArray(Pixel[]::new);
return new Buffer(this.W, this.H, pixCopy);
}
public void AddSample(int x, int y, Colour sample) {
Pixels[y * W + x].AddSample(sample);
}
public void AddSample(Point p, Colour sample) {
AddSample(p.x, p.y, sample);
}
public int Samples(int x, int y) {
return Pixels[y * W + x].Samples.get();
}
public Colour Color(int x, int y) {
return Pixels[y * W + x].Color();
}
public Colour Variance(int x, int y) {
return Pixels[y * W + x].Variance();
}
public Colour StandardDeviation(int x, int y) {
return Pixels[y * W + x].StandardDeviation();
}
public BufferedImage Image(Channel channel) {
BufferedImage renderedImage = new BufferedImage(this.W, this.H, BufferedImage.TYPE_INT_RGB);
double maxSamples = (channel == Channel.SamplesChannel) ? findMaxSamples() : 0;
for (int i = 0; i < Pixels.length; i++) {
int x = i % W;
int y = i / W;
Colour pixelColor = switch (channel) {
case ColorChannel -> Pixels[i].Color().Pow(1 / 2.2);
case VarianceChannel -> Pixels[i].Variance();
case StandardDeviationChannel -> Pixels[i].StandardDeviation();
case SamplesChannel -> new Colour(Pixels[i].Samples.get() / maxSamples, Pixels[i].Samples.get() / maxSamples, Pixels[i].Samples.get() / maxSamples);
};
renderedImage.setRGB(x, y, Colour.getIntFromColor(pixelColor.r, pixelColor.g, pixelColor.b));
}
return renderedImage;
}
private double findMaxSamples() {
return java.util.Arrays.stream(Pixels)
.mapToDouble(p -> p.Samples.get())
.max()
.orElse(1);
}
public static class Pixel {
public final AtomicInteger Samples = new AtomicInteger();
private Colour M = new Colour(0, 0, 0);
private Colour V = new Colour(0, 0, 0);
public Pixel() {}
public Pixel(Pixel other) {
this.Samples.set(other.Samples.get());
this.M = other.M;
this.V = other.V;
}
public void AddSample(Colour sample) {
int sampleCount = Samples.incrementAndGet();
if (sampleCount == 1) {
M = sample;
return;
}
Colour oldM = M;
M = M.Add(sample.Sub(M).DivScalar(sampleCount));
V = V.Add(sample.Sub(oldM).Mul(sample.Sub(M)));
}
public Colour Color() {
return M;
}
public Colour Variance() {
return (Samples.get() < 2) ? new Colour(0, 0, 0) : V.DivScalar(Samples.get() - 1);
}
public Colour StandardDeviation() {
return Variance().Pow(0.5);
}
}
}