-
Notifications
You must be signed in to change notification settings - Fork 0
/
PixelBuffer.cpp
167 lines (143 loc) · 3.98 KB
/
PixelBuffer.cpp
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
#include "PixelBuffer.hpp"
// PixelBuffer
//
PixelBuffer::PixelBuffer(uint32_t bufferWidth, uint32_t bufferHeight)
: bufferWidth(bufferWidth), bufferHeight(bufferHeight)
{}
PixelBuffer::~PixelBuffer()
{}
uint32_t PixelBuffer::GetWidth() const
{
return bufferWidth;
}
uint32_t PixelBuffer::GetHeight() const
{
return bufferHeight;
}
void PixelBuffer::WriteToFile(std::string filename) const
{
png::image<png::rgb_pixel> image(GetWidth(), GetHeight());
for(uint32_t x = 0; x < GetWidth(); x++)
{
for(uint32_t y = 0; y < GetHeight(); y++)
{
Pixel const &p = GetPixel(x, y);
image[y][x] = png::rgb_pixel(p.red, p.green, p.blue);
}
}
image.write(filename.c_str());
}
// InternalPixelBuffer
//
InternalPixelBuffer::InternalPixelBuffer(uint32_t bufferWidth, uint32_t bufferHeight)
: PixelBuffer(bufferWidth, bufferHeight)
{
buffer = new Pixel*[bufferWidth];
for(uint32_t x = 0; x < bufferWidth; x++)
{
buffer[x] = new Pixel[bufferHeight];
}
}
InternalPixelBuffer::InternalPixelBuffer(InternalPixelBuffer const &other)
: InternalPixelBuffer(other.GetWidth(), other.GetHeight())
{
for(uint32_t x = 0; x < GetWidth(); x++)
{
for(uint32_t y = 0; y < GetHeight(); y++)
{
buffer[x][y] = other.buffer[x][y];
}
}
}
InternalPixelBuffer::~InternalPixelBuffer()
{
for(uint32_t x = 0; x < this->GetWidth(); x++)
{
delete[] buffer[x];
}
delete[] buffer;
}
PixelBuffer *InternalPixelBuffer::clone() const
{
return new InternalPixelBuffer(*this);
}
Pixel const &InternalPixelBuffer::GetPixel(uint32_t x, uint32_t y) const
{
return buffer[x][y];
}
void InternalPixelBuffer::SetPixel(uint32_t x, uint32_t y, Pixel const &pixel)
{
buffer[x][y] = pixel;
}
// SupersampledPixelBuffer
//
SupersampledPixelBuffer::SupersampledPixelBuffer(uint32_t bufferWidth, uint32_t bufferHeight, float samplingFactor)
: InternalPixelBuffer(static_cast<uint32_t>(samplingFactor*bufferWidth), static_cast<uint32_t>(samplingFactor*bufferHeight))
, samplingFactor(samplingFactor)
, originalBufferWidth(bufferWidth)
, originalBufferHeight(bufferHeight)
{
}
SupersampledPixelBuffer::SupersampledPixelBuffer(SupersampledPixelBuffer const &other)
: InternalPixelBuffer(other)
, samplingFactor(other.samplingFactor)
, originalBufferWidth(other.originalBufferWidth)
, originalBufferHeight(other.originalBufferHeight)
{}
SupersampledPixelBuffer::~SupersampledPixelBuffer()
{}
PixelBuffer *SupersampledPixelBuffer::clone() const
{
return new SupersampledPixelBuffer(*this);
}
float SupersampledPixelBuffer::GetSamplingFactor() const
{
return samplingFactor;
}
InternalPixelBuffer SupersampledPixelBuffer::GetSampledPixelBuffer() const
{
InternalPixelBuffer buffer(originalBufferWidth, originalBufferHeight);
// supersampled sections
for(uint32_t x = 0; x < originalBufferWidth; x++)
{
float xs_from = x * samplingFactor;
float xs_to = (x+1) * samplingFactor;
for(uint32_t y = 0; y < originalBufferHeight; y++)
{
float ys_from = y * samplingFactor;
float ys_to = (y+1) * samplingFactor;
float r = 0.f;
float g = 0.f;
float b = 0.f;
for(uint32_t xs = static_cast<uint32_t>(xs_from); xs < xs_to; xs++)
{
float x_weight;
if(xs < xs_from)
x_weight = static_cast<float>(1 - (xs_from - xs)) / (xs_to - xs_from);
else if(xs > xs_to)
x_weight = static_cast<float>(xs - xs_to) / (xs_to - xs_from);
else
x_weight = 1.f;
for(uint32_t ys = static_cast<uint32_t>(ys_from); ys < ys_to; ys++)
{
float y_weight;
if(ys < ys_from)
y_weight = static_cast<float>(1 - (ys_from - ys)) / (ys_to - ys_from);
else if(ys > ys_to)
y_weight = static_cast<float>(ys - ys_to) / (ys_to - ys_from);
else
y_weight = 1.f;
Pixel const &p = this->GetPixel(xs, ys);
r += p.red * x_weight * y_weight;
g += p.green * x_weight * y_weight;
b += p.blue * x_weight * y_weight;
}
}
Pixel p_sampled(static_cast<uint8_t>(r),
static_cast<uint8_t>(g),
static_cast<uint8_t>(b));
buffer.SetPixel(x, y, p_sampled);
}
}
return buffer;
}