-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatasetExporter.cpp
More file actions
208 lines (171 loc) · 5.95 KB
/
Copy pathDatasetExporter.cpp
File metadata and controls
208 lines (171 loc) · 5.95 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
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
#include "template.h"
#ifdef ML_DATASET
#ifdef _WIN32
// Manually define the specific constants TinyEXR needs
#ifndef CP_UTF8
#define CP_UTF8 65001
#endif
// Forward declare the Windows function TinyEXR is looking for
extern "C" __declspec(dllimport) int __stdcall MultiByteToWideChar(
unsigned int CodePage,
unsigned long dwFlags,
const char* lpMultiByteStr,
int cbMultiByte,
wchar_t* lpWideCharStr,
int cchWideChar);
#endif
#define TINYEXR_USE_MINIZ 0
#include "zlib.h"
#define TINYEXR_IMPLEMENTATION
#include "tinyexr.h"
#include "DatasetExporter.h"
#include <vector>
#include <string>
#include <iostream>
bool DatasetExporter::SaveNoisyFrame(const std::string& filename, const FrameData& data)
{
EXRHeader header;
InitEXRHeader(&header);
EXRImage image;
InitEXRImage(&image);
// Total channels: Distance(1), Illum_RGB(3), Normal_XYZ(3) = 7
constexpr int num_channels = 7;
image.num_channels = num_channels;
image.width = data.width;
image.height = data.height;
// 1. MUST BE STRICTLY ALPHABETICAL for mrv2 and other viewers to map correctly
const char* names[] = {
"Distance", // 1
"Illumination_B", // 2
"Illumination_G", // 3
"Illumination_R", // 4
"Nx", // 5
"Ny", // 6
"Nz", // 7
};
// 2. Prepare pixel arrays
std::vector<float> channels[num_channels];
for (int i = 0; i < num_channels; ++i) channels[i].resize(data.width * data.height);
const float scale = 1.f / (data.spp - 1);
for (int i = 0; i < data.width * data.height; ++i)
{
float4 rgb = RGB8_to_RGBAF32(data.rgb[i]);
channels[0][i] = data.distance[i] * scale; // Distance
// Illumination RGB (indices 4, 5, 6)
channels[1][i] = data.illumination[i * 3 + 2] * scale; // Illum B
channels[2][i] = data.illumination[i * 3 + 1] * scale; // Illum G
channels[3][i] = data.illumination[i * 3 + 0] * scale; // Illum R
// Normals (indices 7, 8, 9)
channels[4][i] = data.normal[i * 3 + 0]; // Nx
channels[5][i] = data.normal[i * 3 + 1]; // Ny
channels[6][i] = data.normal[i * 3 + 2]; // Nz
}
// 3. Link pointers to the EXR structure
float* image_ptr[num_channels];
EXRChannelInfo channel_infos[num_channels];
for (int i = 0; i < num_channels; ++i)
{
image_ptr[i] = channels[i].data();
strncpy(channel_infos[i].name, names[i], 255);
channel_infos[i].name[255] = '\0';
}
image.images = (unsigned char**)image_ptr;
header.num_channels = num_channels;
header.channels = channel_infos;
// 4. Set pixel types
header.pixel_types = new int[num_channels];
header.requested_pixel_types = new int[num_channels];
for (int i = 0; i < num_channels; ++i)
{
header.pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT;
header.requested_pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT;
}
// 5. Save to disk
const char* err = nullptr;
int ret = SaveEXRImageToFile(&image, &header, filename.c_str(), &err);
// Cleanup
delete[] header.pixel_types;
delete[] header.requested_pixel_types;
if (ret != TINYEXR_SUCCESS)
{
std::cerr << "EXR Error: " << err << std::endl;
FreeEXRErrorMessage(err);
return false;
}
return true;
}
bool DatasetExporter::SaveHDFrame(const std::string& filename, const FrameData& data)
{
EXRHeader header;
InitEXRHeader(&header);
EXRImage image;
InitEXRImage(&image);
// Total channels: Illum_RGB(3) = 3
constexpr int num_channels = 3;
image.num_channels = num_channels;
image.width = data.width;
image.height = data.height;
// 1. MUST BE STRICTLY ALPHABETICAL for mrv2 and other viewers to map correctly
const char* names[] = {
"Illumination_B", // 1
"Illumination_G", // 2
"Illumination_R", // 3
};
// 2. Prepare pixel arrays
std::vector<float> channels[num_channels];
for (int i = 0; i < num_channels; ++i) channels[i].resize(data.width * data.height);
const float scale = 1.f / (data.spp - 1);
for (int i = 0; i < data.width * data.height; ++i)
{
// Illumination RGB (indices 4, 5, 6)
channels[0][i] = data.illumination[i * 3 + 2] * scale; // Illum B
channels[1][i] = data.illumination[i * 3 + 1] * scale; // Illum G
channels[2][i] = data.illumination[i * 3 + 0] * scale; // Illum R
}
// 3. Link pointers to the EXR structure
float* image_ptr[num_channels];
EXRChannelInfo channel_infos[num_channels];
for (int i = 0; i < num_channels; ++i)
{
image_ptr[i] = channels[i].data();
strncpy(channel_infos[i].name, names[i], 255);
channel_infos[i].name[255] = '\0';
}
image.images = (unsigned char**)image_ptr;
header.num_channels = num_channels;
header.channels = channel_infos;
// 4. Set pixel types
header.pixel_types = new int[num_channels];
header.requested_pixel_types = new int[num_channels];
for (int i = 0; i < num_channels; ++i)
{
header.pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT;
header.requested_pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT;
}
// 5. Save to disk
const char* err = nullptr;
int ret = SaveEXRImageToFile(&image, &header, filename.c_str(), &err);
// Cleanup
delete[] header.pixel_types;
delete[] header.requested_pixel_types;
if (ret != TINYEXR_SUCCESS)
{
std::cerr << "EXR Error: " << err << std::endl;
FreeEXRErrorMessage(err);
return false;
}
return true;
}
void DatasetExporter::PushNoisy(const FrameData& data)
{
m_lowq = data;
std::string filename = m_path + std::to_string(data.frame) + "_lq_.exr";
SaveNoisyFrame(filename, data);
}
void DatasetExporter::PushHD(const FrameData& data)
{
m_highq = data;
std::string filename = m_path + std::to_string(data.frame) + "_hq_.exr";
SaveHDFrame(filename, data);
}
#endif // ML_DATASET