-
Notifications
You must be signed in to change notification settings - Fork 0
/
pixel_processing.cpp
332 lines (264 loc) · 9.5 KB
/
pixel_processing.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <imagina/pixel_processing.h>
#include <imagina/interface/engine.h>
#include <imagina/platform_dependent.h>
#include <assert.h>
#include <cstring>
#include <cmath>
namespace Imagina {
const FieldInfo *PixelDataInfo::FindField(const char *name) const {
for (size_t i = 0; i < FieldCount; i++) {
//if (Fields[i].Name == name) {
if (strcmp(Fields[i].Name, name) == 0) {
return &Fields[i];
}
}
return nullptr;
}
void SerialCompositeProcessor2::SetInput(const PixelDataInfo *info) {
processors[0].SetInput(info);
const PixelDataInfo *intermediateData = processors[0].GetOutputInfo();
intermediateDataSize = intermediateData->Size;
processors[1].SetInput(intermediateData);
}
const PixelDataInfo *SerialCompositeProcessor2::GetOutputInfo() {
return processors[1].GetOutputInfo();
}
void SerialCompositeProcessor2::Process(void *output, void *input) const {
void *intermediateData = alloca(intermediateDataSize);
processors[0].Process(intermediateData, input);
processors[1].Process(output, intermediateData);
}
SerialCompositeProcessor::SerialCompositeProcessor(std::initializer_list<IPixelProcessor> processors) {
assert(std::find(processors.begin(), processors.end(), nullptr) == processors.end());
assert(processors.size() > 1);
processorCount = processors.size();
this->processors = new IPixelProcessor[processorCount];
std::copy(processors.begin(), processors.end(), this->processors);
//memcpy(this->processors, processors.begin(), processorCount * sizeof(IPixelProcessor *));
}
void SerialCompositeProcessor::SetInput(const PixelDataInfo *info) {
const PixelDataInfo *intermediateData;
processors[0].SetInput(info);
intermediateData = processors[0].GetOutputInfo();
for (size_t i = 1; i < processorCount; i++) {
intermediateDataSize = std::max(intermediateDataSize, intermediateData->Size);
processors[i].SetInput(info);
intermediateData = processors[i].GetOutputInfo();
}
}
const PixelDataInfo *SerialCompositeProcessor::GetOutputInfo() {
return processors[processorCount - 1].GetOutputInfo();
}
void SerialCompositeProcessor::Process(void *output, void *input) const {
void *intermediateData[2] = { alloca(intermediateDataSize * 2) };
intermediateData[1] = (char *)intermediateData[0] + intermediateDataSize;
processors[0].Process(intermediateData[0], input);
size_t i;
for (i = 1; i < processorCount - 1; i++) {
processors[i].Process(intermediateData[i & 1], intermediateData[~i & 1]);
}
processors[i].Process(output, intermediateData[~i & 1]);
}
void CopyProcessor::SetInput(const PixelDataInfo *info) {
output = info;
dataSize = info->Size;
}
const PixelDataInfo *CopyProcessor::GetOutputInfo() {
return output;
}
void CopyProcessor::Process(void *output, void *input) const {
memcpy(output, input, dataSize);
}
IPixelProcessor PixelPipeline::GetCompositeProcessor(Stage first, Stage last) {
assert(StageValid(first) && StageValid(last) && first <= last);
uint8_t First = (uint8_t)first;
uint8_t Last = (uint8_t)last;
while (!stages[First] && First < (uint8_t)Stage::Colorize) First++;
while (!stages[Last] && Last > (uint8_t)Stage::Preprocess) Last--;
if (Last < First) {
return nullptr;
} else if (First == Last) { // One
return stages[First];
} else if (First + 1 == Last || !stages[(uint8_t)Stage::Postprocess]) { // Two
IPixelProcessor &compositeProcessor = composite2[First - (uint8_t)Stage::Preprocess];
if (!compositeProcessor) compositeProcessor = new SerialCompositeProcessor2(stages[First], stages[Last]);
return compositeProcessor;
} else { // Three
if (!composite3) composite3 = new SerialCompositeProcessor({ stages[1], stages[2], stages[3] });
return composite3;
}
}
void PixelPipeline::UseEngine(IEngine engine) {
outputs[0] = engine.GetOutputInfo();
}
void PixelPipeline::UsePreprocessor(IPixelProcessor processor) {
stages[1] = processor;
linked = false;
}
void PixelPipeline::UsePostprocessor(IPixelProcessor processor) {
stages[2] = processor;
linked = false;
}
void PixelPipeline::UseColorizer(IPixelProcessor processor) {
stages[3] = processor;
linked = false;
}
void PixelPipeline::Link() {
const PixelDataInfo *pixelData = outputs[0];
if (stages[1]) {
stages[1].SetInput(pixelData);
pixelData = stages[1].GetOutputInfo();
}
outputs[1] = pixelData;
if (stages[2]) {
stages[2].SetInput(pixelData);
pixelData = stages[2].GetOutputInfo();
}
outputs[2] = pixelData;
if (stages[3]) {
stages[3].SetInput(pixelData);
pixelData = stages[3].GetOutputInfo();
}
outputs[3] = pixelData;
linked = true;
}
const PixelDataInfo *PixelPipeline::GetOutputOfStage(Stage stage) {
assert(linked);
assert(StageValid(stage));
return outputs[(size_t)stage];
}
using namespace std;
const PixelDataInfo TestProcessor::OutputInfo{
4, 1, TestProcessor::OutputFields
};
const FieldInfo TestProcessor::OutputFields[1]{
{ "Value", 0, PixelDataType::float32 }
};
void TestProcessor::SetInput(const PixelDataInfo *info) {
const FieldInfo *sourceField = info->FindField("Value");
assert(sourceField->Type == PixelDataType::float64);
sourceOffset = sourceField->Offset;
}
const PixelDataInfo *TestProcessor::GetOutputInfo() {
return &OutputInfo;
}
void TestProcessor::Process(void *output, void *input) const {
*(float *)output = (float)*(double *)((char *)input + sourceOffset);
}
void TestProcessor2::SetInput(const PixelDataInfo *info) {
const FieldInfo *iterationsField = info->FindField("Iterations");
const FieldInfo *finalZField = info->FindField("FinalZ");
assert(iterationsField->Type == PixelDataType::uint64);
assert(finalZField->Type == PixelDataType::complex_sr);
iterationsOffset = iterationsField->Offset;
finalZOffset = finalZField->Offset;
}
const PixelDataInfo *TestProcessor2::GetOutputInfo() {
using namespace std;
static const FieldInfo OutputFields[1]{
{ "Value", 0, PixelDataType::float64 }
};
static const PixelDataInfo OutputInfo{
8, 1, OutputFields
};
return &OutputInfo;
}
void TestProcessor2::Process(void *output, void *input) const {
const complex_sr &finalZ = GetField<complex_sr>(input, finalZOffset);
real_sr finalMagnitude = norm(finalZ);
double Iterations = GetField<uint64_t>(input, iterationsOffset);
if (finalMagnitude > 4096.0) {
Iterations -= log2(log2(finalMagnitude)) - log2(log2(4096.0));
}
*(double *)output = Iterations;
}
PaletteLookup::PaletteLookup(const rgba32f *palette, size_t paletteSize) : paletteSize(paletteSize) {
rgba32f *temp = new rgba32f[paletteSize];
memcpy(temp, palette, paletteSize * sizeof(rgba32f));
this->palette = temp;
}
PaletteLookup::~PaletteLookup() {
delete[]palette;
}
void PaletteLookup::SetInput(const PixelDataInfo *info) {
inputField = info->FindField("Value");
assert(inputField);
assert(inputField->IsScalar());
}
const PixelDataInfo *PaletteLookup::GetOutputInfo() {
static const FieldInfo OutputFields[1]{
{ "Color", 0, PixelDataType::rgba32f }
};
static const PixelDataInfo OutputInfo{
16, 1, OutputFields
};
return &OutputInfo;
}
void PaletteLookup::Process(void *output, void *input) const {
real_sr value = inputField->GetScalar<real_sr>(input);
if (!std::isfinite(value)) {
*(rgba32f *)output = rgba32f(0.0, 0.0, 0.0, 1.0);
return;
}
value *= valueMultiplier;
value += valueOffset;
value -= floor(value);
value *= paletteSize;
size_t index1 = floor(value);
size_t index2 = index1 + 1;
if (index2 == paletteSize) index2 = 0;
value -= floor(value);
*(rgba32f *)output = lerp(palette[index1], palette[index2], (real_gr)value);
((rgba32f *)output)->x = std::sqrt(((rgba32f *)output)->x);
((rgba32f *)output)->y = std::sqrt(((rgba32f *)output)->y);
((rgba32f *)output)->z = std::sqrt(((rgba32f *)output)->z);
}
BSplineInterpolator::BSplineInterpolator(const rgba32f *palette, size_t paletteSize) : paletteSize(paletteSize) {
rgba32f *temp = new rgba32f[paletteSize + 3];
memcpy(temp + 1, palette, paletteSize * sizeof(rgba32f));
temp[0] = temp[paletteSize];
temp[paletteSize + 1] = temp[1];
temp[paletteSize + 2] = temp[2];
this->palette = temp;
}
BSplineInterpolator::~BSplineInterpolator() {
delete[]palette;
}
void BSplineInterpolator::SetInput(const PixelDataInfo *info) {
inputField = info->FindField("Value");
assert(inputField);
assert(inputField->IsScalar());
}
const PixelDataInfo *BSplineInterpolator::GetOutputInfo() {
static const FieldInfo OutputFields[1]{
{ "Color", 0, PixelDataType::rgba32f }
};
static const PixelDataInfo OutputInfo{
16, 1, OutputFields
};
return &OutputInfo;
}
void BSplineInterpolator::Process(void *output, void *input) const {
real_sr value = inputField->GetScalar<real_sr>(input);
if (!std::isfinite(value)) {
*(rgba32f *)output = rgba32f(0.0, 0.0, 0.0, 1.0);
return;
}
value *= valueMultiplier;
value += valueOffset;
value -= floor(value);
value *= paletteSize;
size_t index = floor(value);
float t = value - floor(value);
float t2 = t * t;
float t3 = t2 * t;
*(rgba32f *)output = palette[index + 0] * ( -t3 + 3.0f * t2 - 3.0f * t + 1.0f)
+ palette[index + 1] * ( 3.0f * t3 - 6.0f * t2 + 4.0f)
+ palette[index + 2] * (-3.0f * t3 + 3.0f * t2 + 3.0f * t + 1.0f)
+ palette[index + 3] * ( t3 );
*(rgba32f *)output *= 1.0f / 6.0f;
((rgba32f *)output)->x = std::sqrt(((rgba32f *)output)->x);
((rgba32f *)output)->y = std::sqrt(((rgba32f *)output)->y);
((rgba32f *)output)->z = std::sqrt(((rgba32f *)output)->z);
}
}