-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathimageutils.cpp
415 lines (356 loc) · 14 KB
/
imageutils.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "imageutils.h"
#include "include/cglobals.h"
#include "Image2d.h"
#define TINYEXR_IMPLEMENTATION
#include "tinyexr.h"
bool SaveImage4fToEXR(const float* rgb, int width, int height, const char* outfilename, float a_normConst = 1.0f, bool a_invertY = false)
{
EXRHeader header;
InitEXRHeader(&header);
EXRImage image;
InitEXRImage(&image);
image.num_channels = 3;
std::vector<float> images[3];
images[0].resize(width * height);
images[1].resize(width * height);
images[2].resize(width * height);
// Split RGBARGBARGBA... into R, G and B layer
if(a_invertY) {
for(int y=0;y<height;y++) {
const int offsetY1 = y*width*4;
const int offsetY2 = (height-y-1)*width*4;
for(int x=0;x<width;x++) {
images[0][(offsetY1 >> 2) + x] = rgb[offsetY2 + x*4 + 0]*a_normConst;
images[1][(offsetY1 >> 2) + x] = rgb[offsetY2 + x*4 + 1]*a_normConst;
images[2][(offsetY1 >> 2) + x] = rgb[offsetY2 + x*4 + 2]*a_normConst;
}
}
}
else {
for (size_t i = 0; i < size_t(width * height); i++) {
images[0][i] = rgb[4*i+0]*a_normConst;
images[1][i] = rgb[4*i+1]*a_normConst;
images[2][i] = rgb[4*i+2]*a_normConst;
}
}
float* image_ptr[3];
image_ptr[0] = images[2].data(); // B
image_ptr[1] = images[1].data(); // G
image_ptr[2] = images[0].data(); // R
image.images = (unsigned char**)image_ptr;
image.width = width;
image.height = height;
header.num_channels = 3;
header.channels = (EXRChannelInfo *)malloc(sizeof(EXRChannelInfo) * header.num_channels);
// Must be (A)BGR order, since most of EXR viewers expect this channel order.
strncpy(header.channels[0].name, "B", 255); header.channels[0].name[strlen("B")] = '\0';
strncpy(header.channels[1].name, "G", 255); header.channels[1].name[strlen("G")] = '\0';
strncpy(header.channels[2].name, "R", 255); header.channels[2].name[strlen("R")] = '\0';
header.pixel_types = (int *)malloc(sizeof(int) * header.num_channels);
header.requested_pixel_types = (int *)malloc(sizeof(int) * header.num_channels);
for (int i = 0; i < header.num_channels; i++) {
header.pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT; // pixel type of input image
header.requested_pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT; // pixel type of output image to be stored in .EXR
}
const char* err = nullptr;
int ret = SaveEXRImageToFile(&image, &header, outfilename, &err);
if (ret != TINYEXR_SUCCESS) {
fprintf(stderr, "Save EXR err: %s\n", err);
FreeEXRErrorMessage(err); // free's buffer for an error message
return false;
}
//printf("Saved exr file. [%s] \n", outfilename);
free(header.channels);
free(header.pixel_types);
free(header.requested_pixel_types);
return true;
}
bool SaveImage3DToEXR(const float* data, int width, int height, int channels, const char* outfilename)
{
EXRHeader header;
InitEXRHeader(&header);
EXRImage image;
InitEXRImage(&image);
std::vector<const float*> image_ptr(channels);
for(int c=0;c<channels;c++)
image_ptr[c] = data + c*width*height;
std::vector<EXRChannelInfo> channelsVec(channels);
std::vector<int> auxIntData(2*channels);
image.images = (unsigned char**)image_ptr.data();
image.width = width;
image.height = height;
image.num_channels = channels;
header.num_channels = channels;
header.channels = channelsVec.data();
header.pixel_types = auxIntData.data();
header.requested_pixel_types = auxIntData.data() + channels;
constexpr float IMG_LAMBDA_MIN = 360.0f;
constexpr float IMG_LAMBDA_MAX = 830.0f;
if(channels == 1)
{
std::string name = "Y";
memset (header.channels[0].name, 0, 256);
strncpy(header.channels[0].name, name.c_str(), 255);
header.pixel_types[0] = TINYEXR_PIXELTYPE_FLOAT;
header.requested_pixel_types[0] = TINYEXR_PIXELTYPE_FLOAT;
}
else
{
for (int i = 0; i < channels; i++)
{
const float t0 = float(i)/float(channels);
const float t1 = float(i+1)/float(channels);
const float lamdba0 = IMG_LAMBDA_MIN + t0*(IMG_LAMBDA_MAX - IMG_LAMBDA_MIN);
const float lamdba1 = IMG_LAMBDA_MIN + t1*(IMG_LAMBDA_MAX - IMG_LAMBDA_MIN);
std::stringstream strout;
strout << int(lamdba0) << "-" << int(lamdba1)-1 << ".Y";
std::string tmp = strout.str();
//std::cout << tmp.c_str() << std::endl;
memset (header.channels[i].name, 0, 256);
strncpy(header.channels[i].name, tmp.c_str(), 255);
header.pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT; // pixel type of input image
header.requested_pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT; // pixel type of output image to be stored in .EXR
}
}
const char* err = nullptr;
int ret = SaveEXRImageToFile(&image, &header, outfilename, &err);
if (ret != TINYEXR_SUCCESS) {
fprintf(stderr, "Save EXR err: %s\n", err);
FreeEXRErrorMessage(err); // free's buffer for an error message
return false;
}
//printf("Saved exr file. [%s] \n", outfilename);
return true;
}
void SaveImage3DToImage3D1f(const float* data, int width, int height, int channels, const char* outfilename)
{
std::ofstream fout(outfilename, std::ios::binary);
int xyz[3] = {width, height, channels};
fout.write((const char*)xyz, sizeof(int)*3);
fout.write((const char*)data, sizeof(float)*size_t(width*height*channels));
fout.close();
}
void FlipYAndNormalizeImage2D1f(float* data, int width, int height, float a_normConst = 1.0f)
{
const int halfHeight = height / 2;
for (int y = 0; y < halfHeight; ++y) {
const int offsetY1 = y * width;
const int offsetY2 = (height - y - 1) * width;
for (int x = 0; x < width; ++x) {
const float tmp = a_normConst*data[offsetY1 + x];
data[offsetY1 + x] = a_normConst*data[offsetY2 + x];
data[offsetY2 + x] = tmp;
}
}
}
void SaveFrameBufferToEXR(float* data, int width, int height, int channels, const char* outfilename, float a_normConst)
{
if(channels == 4)
SaveImage4fToEXR(data, width, height, outfilename, a_normConst, true);
else
{
for(int c=0;c<channels;c++)
FlipYAndNormalizeImage2D1f(data + width*height*c, width, height, a_normConst*float(channels));
if(std::string(outfilename).find(".image3d1f") != std::string::npos)
SaveImage3DToImage3D1f(data, width, height, channels, outfilename);
else
SaveImage3DToEXR(data, width, height, channels, outfilename);
}
}
inline float linearToSRGB(float l)
{
if(l <= 0.00313066844250063f)
return l * 12.92f;
else
return 1.055f * std::pow(l, 1.0f/2.4f) - 0.055f;
}
void FrameBufferColorToLDRImage(const float* rgb, int width, int height, float a_normConst, float a_gamma, std::vector<uint32_t>& pixelData, bool a_flip)
{
if(std::abs(a_gamma - 2.4f) < 1e-5f)
{
#pragma omp parallel for if (width*height > 512*512)
for(int y=0;y<height;y++) // flip image and extract pixel data
{
int offset1 = y*width;
int offset2 = a_flip ? (height-y-1)*width : offset1;
for (int x = 0; x < width; x++)
{
float color[4];
color[0] = linearToSRGB(clamp(rgb[4*(offset1+x) + 0]*a_normConst, 0.0f, 1.0f));
color[1] = linearToSRGB(clamp(rgb[4*(offset1+x) + 1]*a_normConst, 0.0f, 1.0f));
color[2] = linearToSRGB(clamp(rgb[4*(offset1+x) + 2]*a_normConst, 0.0f, 1.0f));
color[3] = 1.0f;
pixelData[offset2 + x] = RealColorToUint32(color);
}
}
}
else
{
const float invGamma = 1.0f/a_gamma;
#pragma omp parallel for if (width*height > 512*512)
for(int y=0;y<height;y++) // flip image and extract pixel data
{
int offset1 = y*width;
int offset2 = a_flip ? (height-y-1)*width : offset1;
for (int x = 0; x < width; x++)
{
float color[4];
color[0] = clamp(std::pow(rgb[4*(offset1+x) + 0]*a_normConst, invGamma), 0.0f, 1.0f);
color[1] = clamp(std::pow(rgb[4*(offset1+x) + 1]*a_normConst, invGamma), 0.0f, 1.0f);
color[2] = clamp(std::pow(rgb[4*(offset1+x) + 2]*a_normConst, invGamma), 0.0f, 1.0f);
color[3] = 1.0f;
pixelData[offset2 + x] = RealColorToUint32(color);
}
}
}
}
void MonoFrameBufferToLDRImage(const float* mono, int width, int height, float a_normConst, float a_gamma, std::vector<uint32_t>& pixelData, bool a_flip)
{
if(std::abs(a_gamma - 2.4f) < 1e-5f)
{
#pragma omp parallel for if (width*height > 512*512)
for(int y=0;y<height;y++) // flip image and extract pixel data
{
int offset1 = y*width;
int offset2 = a_flip ? (height-y-1)*width : offset1;
for (int x = 0; x < width; x++)
{
float color[4];
color[0] = linearToSRGB(clamp(mono[(offset1+x)]*a_normConst, 0.0f, 1.0f));
color[1] = linearToSRGB(clamp(mono[(offset1+x)]*a_normConst, 0.0f, 1.0f));
color[2] = linearToSRGB(clamp(mono[(offset1+x)]*a_normConst, 0.0f, 1.0f));
color[3] = 1.0f;
pixelData[offset2 + x] = RealColorToUint32(color);
}
}
}
else
{
const float invGamma = 1.0f/a_gamma;
#pragma omp parallel for if (width*height > 512*512)
for(int y=0;y<height;y++) // flip image and extract pixel data
{
int offset1 = y*width;
int offset2 = a_flip ? (height-y-1)*width : offset1;
for (int x = 0; x < width; x++)
{
float color[4];
color[0] = clamp(std::pow(mono[(offset1+x)]*a_normConst, invGamma), 0.0f, 1.0f);
color[1] = clamp(std::pow(mono[(offset1+x)]*a_normConst, invGamma), 0.0f, 1.0f);
color[2] = clamp(std::pow(mono[(offset1+x)]*a_normConst, invGamma), 0.0f, 1.0f);
color[3] = 1.0f;
pixelData[offset2 + x] = RealColorToUint32(color);
}
}
}
}
std::vector<uint32_t> FrameBufferColorToLDRImage(const float* rgb, int width, int height, float a_normConst, float a_gamma, bool a_flip)
{
std::vector<uint32_t> pixelData(width*height);
FrameBufferColorToLDRImage(rgb, width, height, a_normConst, a_gamma, pixelData, a_flip);
return pixelData;
}
bool SaveImage4fToBMP(const float* rgb, int width, int height, int channels, const char* outfilename, float a_normConst, float a_gamma)
{
auto pixelData = FrameBufferColorToLDRImage(rgb, width, height, a_normConst, a_gamma, false);
LiteImage::SaveBMP(outfilename, pixelData.data(), width, height);
return true;
}
bool SaveImage4fByExtension(const float* data, int width, int height, int channels, const char* outfilename, float a_normConst, float a_gamma)
{
LiteImage::Image2D<uint32_t> tmp(width, height);
if(channels == 1)
{
MonoFrameBufferToLDRImage(data, width, height, a_normConst, a_gamma, const_cast< std::vector<uint32_t>& >(tmp.vector()), true);
}
else
{
FrameBufferColorToLDRImage(data, width, height, a_normConst, a_gamma, const_cast< std::vector<uint32_t>& >(tmp.vector()), true);
}
return LiteImage::SaveImage(outfilename, tmp);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::vector<float> LoadImage1fFromEXR(const char* infilename, int* pW, int* pH)
{
float* out; // width * height * RGBA
int width = 0;
int height = 0;
const char* err = nullptr;
int ret = LoadEXR(&out, &width, &height, infilename, &err);
if (ret != TINYEXR_SUCCESS) {
if (err) {
fprintf(stderr, "[LoadImage1fFromEXR] : %s\n", err);
std::cerr << "[LoadImage1fFromEXR] : " << err;
std::cerr << " from path : " << infilename << std::endl;
delete err;
}
return std::vector<float>();
}
const int imgSize = width * height;
std::vector<float> result(imgSize);
*pW = uint32_t(width);
*pH = uint32_t(height);
#pragma omp parallel for
for(int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
size_t idx = (x + (height - y - 1) * width) * 4;
size_t out_idx = x + y * width;
if (std::isinf(out[idx]))
result[out_idx] = 65504.0f; // max half float according to ieee
else
result[out_idx] = clamp(out[idx], 0.0f, 65504.0f); // max half float according to ieee
}
}
free(out);
return result;
}
std::vector<float> LoadImage4fFromEXR(const char* infilename, int* pW, int* pH)
{
float* out; // width * height * RGBA
int width = 0;
int height = 0;
const char* err = nullptr;
int ret = LoadEXR(&out, &width, &height, infilename, &err);
if (ret != TINYEXR_SUCCESS) {
if (err) {
fprintf(stderr, "[LoadImage4fFromEXR] : %s\n", err);
std::cerr << "[LoadImage4fFromEXR] : " << err << std::endl;
delete err;
}
return std::vector<float>();
}
std::vector<float> result(width * height * 4);
*pW = uint32_t(width);
*pH = uint32_t(height);
#pragma omp parallel for
for(int y = 0; y < height; y++)
{
const int offset1 = (height - y - 1) * width * 4;
const int offset2 = y * width * 4;
memcpy((void*)(result.data() + offset1), (void*)(out + offset2), width * sizeof(float) * 4);
}
free(out);
return result;
}
float* LoadImage4fFromEXRUnsafe(const char* infilename, int* pW, int* pH)
{
float* out; // width * height * RGBA
int width = 0;
int height = 0;
const char* err = nullptr;
int ret = LoadEXR(&out, &width, &height, infilename, &err);
if (ret != TINYEXR_SUCCESS) {
if (err) {
fprintf(stderr, "[LoadImage4fFromEXR] : %s\n", err);
std::cerr << "[LoadImage4fFromEXR] : " << err << std::endl;
delete err;
}
return nullptr;
}
*pW = uint32_t(width);
*pH = uint32_t(height);
return out;
}