-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathblue_noise_export_util.cpp
266 lines (251 loc) · 7.73 KB
/
blue_noise_export_util.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
#include "blue_noise_export_util.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#include <iomanip>
// some utility functions
namespace
{
//===========================================================================================================================
inline uint32_t FloatAsByteUNorm(float value)
{
return uint32_t(255.f * value);
}
//===========================================================================================================================
double remap_tri(const double v)
{
double r2 = 0.5 * v;
double f1 = sqrt(r2);
double f2 = 1.0 - sqrt(r2 - 0.25);
return (v < 0.5) ? f1 : f2;
}
//===========================================================================================================================
size_t ComputeElementCount(const size_t dimensionSize[BlueNoiseExportUtil::max_N_dimensions], size_t dimCount)
{
size_t elemCount = 1;
for (size_t currDim = 0; currDim < dimCount; ++currDim)
{
elemCount *= dimensionSize[currDim];
}
return elemCount;
}
//===========================================================================================================================
//note: splats to 24b
uint8_t* FloatDataToBytes(const std::vector<float>& arr, size_t N_valuesPerItem, bool do_remap_tri)
{
uint8_t* bytes = new uint8_t[3 * arr.size() / N_valuesPerItem];
for (size_t i = 0, n = arr.size() / N_valuesPerItem; i < n; ++i)
{
uint8_t lastChar = 0;
for (size_t j = 0; j < N_valuesPerItem; j++)
{
double v_f = arr[i * N_valuesPerItem + j];
if (do_remap_tri)
v_f = remap_tri(v_f);
int v_i = static_cast<int>(v_f * 256.0);
v_i = (v_i < 0) ? 0 : v_i;
v_i = (v_i > 255) ? 255 : v_i;
lastChar = static_cast<char>(v_i);
bytes[3 * i + j] = lastChar;
}
for (size_t j = N_valuesPerItem; j < 3; j++)
{
// splat
if (N_valuesPerItem == 1)
{
bytes[3 * i + j] = lastChar;
}
else
{
bytes[3 * i + j] = 0;
}
}
}
return bytes;
}
//===========================================================================================================================
void PrintWebGLOutputRecursive(std::ofstream& outFile,
const std::vector<float>& arr,
const std::string& arrName,
size_t N_dimensions,
size_t N_valuesPerItem,
size_t lo,
size_t high)
{
if (high - lo == 1)
{
outFile << "return ";
if (N_valuesPerItem > 1)
{
outFile << "vec" << N_valuesPerItem << "(";
for (size_t v = 0; v < N_valuesPerItem; ++v)
{
outFile << arr[lo * N_valuesPerItem + v];
if (v < N_valuesPerItem - 1)
{
outFile << ", ";
}
}
outFile << ");";
}
else
{
outFile << arr[lo] << ";";
}
}
else
{
size_t mid = (lo + high) / 2;
outFile << "if(" << arrName << " < " << mid << ") " << std::endl << "{" << std::endl;
PrintWebGLOutputRecursive(outFile, arr, arrName, N_dimensions, N_valuesPerItem, lo, mid);
outFile << "} else {" << std::endl;
PrintWebGLOutputRecursive(outFile, arr, arrName, N_dimensions, N_valuesPerItem, mid, high);
outFile << std::endl << "}";
}
}
}
//===========================================================================================================================
void BlueNoiseExportUtil::PrintCodeOutput (const std::string& fileName,
const std::vector<float>& arr,
const std::string& arrName,
bool mathematica,
const size_t dimensionSize[max_N_dimensions],
size_t N_dimensions,
size_t N_valuesPerItem)
{
std::ofstream outFile;
outFile.open(fileName, std::ios::out | std::ios::trunc);
const size_t arrSize = arr.size();
if (!mathematica)
{
outFile << "static const float " << arrName;
for (size_t d = 0; d < N_dimensions; ++d)
{
outFile << "[" << dimensionSize[N_dimensions - 1 - d] << "]";
}
if (N_valuesPerItem > 1)
{
outFile << "[" << N_valuesPerItem << "]";
}
outFile << " = " << std::endl;
}
for (size_t i = 0; i < arrSize / N_valuesPerItem; ++i)
{
for (size_t d = 0; d < N_dimensions; ++d)
{
size_t dim = (i / ComputeElementCount(dimensionSize, d)) % dimensionSize[d];
if (dim == 0)
{
outFile << "{";
}
else
{
break;
}
}
if (N_valuesPerItem == 1)
{
outFile << std::setprecision(8) << std::fixed << arr[i] << "f";
}
else
{
outFile << "{";
for (size_t v = 0; v < N_valuesPerItem; ++v)
{
outFile << std::setprecision(8) << std::fixed << arr[i * N_valuesPerItem + v] << "f";
if (v < N_valuesPerItem - 1)
{
outFile << ", ";
}
}
outFile << "}";
}
for (size_t d = 0; d < N_dimensions; ++d)
{
size_t dim = (i / ComputeElementCount(dimensionSize, d)) % dimensionSize[d];
if (dim == dimensionSize[d] - 1)
{
outFile << "}";
}
else
{
outFile << ",";
if (d > 0)
{
outFile << std::endl;
}
break;
}
}
}
if (!mathematica)
{
outFile << ";";
}
outFile << std::endl << std::endl;
}
//===========================================================================================================================
void BlueNoiseExportUtil::PrintWebGLOutput(const std::string& fileName,
const std::vector<float>& arr,
const std::string& arrName,
size_t N_dimensions,
size_t N_valuesPerItem,
size_t lo,
size_t high)
{
std::ofstream outFile;
outFile.open(fileName, std::ios::out | std::ios::trunc);
PrintWebGLOutputRecursive(outFile, arr, arrName, N_dimensions, N_valuesPerItem, lo, high);
}
//===========================================================================================================================
void BlueNoiseExportUtil::SaveAsPPM(const std::vector<float>& arr,
const std::string& fileName,
const size_t dimensionSize[max_N_dimensions],
size_t N_dimensions,
size_t N_valuesPerItem,
uint32_t slice)
{
std::ofstream outfile(fileName);
assert(N_dimensions >= 2);
size_t srcOffset = 0;
if (N_dimensions == 3)
{
srcOffset += slice * (dimensionSize[0] * dimensionSize[1]);
}
N_dimensions = 2; // ignore depth
outfile << "P3" << std::endl << dimensionSize[0] << " " << dimensionSize[1] << std::endl << 255 << std::endl;
const uint32_t pixCount = uint32_t(dimensionSize[0] * dimensionSize[1]);
for (size_t i = srcOffset; i < pixCount + srcOffset; ++i)
{
switch (N_valuesPerItem)
{
case 1: // monichromatic values
outfile << FloatAsByteUNorm(arr[i]) << " "
<< FloatAsByteUNorm(arr[i]) << " "
<< FloatAsByteUNorm(arr[i]) << " ";
break;
case 2:
outfile << FloatAsByteUNorm(arr[i * 2]) << " "
<< FloatAsByteUNorm(arr[i * 2 + 1]) << " "
<< 0 << " ";
break;
case 3:
outfile << FloatAsByteUNorm(arr[i * 3]) << " "
<< FloatAsByteUNorm(arr[i * 3 + 1]) << " "
<< FloatAsByteUNorm(arr[i * 3 + 2]) << " ";
break;
default:
assert(0);
}
}
outfile.close();
}
//===========================================================================================================================
void BlueNoiseExportUtil::SaveAsBMP(const std::vector<float>& arr,
const std::string& fileName,
const size_t dimensionSize[2],
size_t N_valuesPerItem)
{
uint8_t* bytedata = FloatDataToBytes(arr, N_valuesPerItem, false);
stbi_write_bmp(fileName.c_str(), int(dimensionSize[0]), int(dimensionSize[1]), 3, bytedata);
delete[] bytedata;
}