forked from syoyo/tinyexr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tinyexr.cc
469 lines (395 loc) · 15.2 KB
/
test_tinyexr.cc
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#if defined(_WIN32)
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <tchar.h>
#include <windows.h>
#endif
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
// Uncomment if you want to use system provided zlib.
// #define TINYEXR_USE_MINIZ (0)
// #include <zlib.h>
#define TINYEXR_IMPLEMENTATION
#include "tinyexr.h"
#ifdef __clang__
#if __has_warning("-Wzero-as-null-pointer-constant")
#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
#endif
#endif
#define SIMPLE_API_EXAMPLE
//#define TEST_ZFP_COMPRESSION
#ifdef SIMPLE_API_EXAMPLE
#if 0
static void
SaveAsPFM(const char* filename, int width, int height, float* data)
{
#ifdef _WIN32
FILE* fp = NULL;
fopen_s(&fp, filename, "wb");
#else
FILE* fp = fopen(filename, "wb");
#endif
if (!fp) {
fprintf(stderr, "failed to write a PFM file.\n");
return;
}
fprintf(fp, "PF\n");
fprintf(fp, "%d %d\n", width, height);
fprintf(fp, "-1\n"); // -1: little endian, 1: big endian
// RGBA -> RGB
std::vector<float> rgb(static_cast<size_t>(width*height*3));
for (size_t i = 0; i < static_cast<size_t>(width * height); i++) {
rgb[3*i+0] = data[4*i+0];
rgb[3*i+1] = data[4*i+1];
rgb[3*i+2] = data[4*i+2];
}
fwrite(&rgb.at(0), sizeof(float), static_cast<size_t>(width * height * 3), fp);
fclose(fp);
}
#endif
#else
static const char* GetPixelType(int id) {
if (id == TINYEXR_PIXELTYPE_HALF) {
return "HALF";
} else if (id == TINYEXR_PIXELTYPE_FLOAT) {
return "FLOAT";
} else if (id == TINYEXR_PIXELTYPE_UINT) {
return "UINT";
}
return "???";
}
// Simple tile -> scanline converter. Assumes FLOAT pixel type for all channels.
static void TiledImageToScanlineImage(EXRImage* src, const EXRHeader* header) {
assert(header->data_window[2] - header->data_window[0] + 1 >= 0);
assert(header->data_window[3] - header->data_window[1] + 1 >= 0);
size_t data_width =
static_cast<size_t>(header->data_window[2] - header->data_window[0] + 1);
size_t data_height =
static_cast<size_t>(header->data_window[3] - header->data_window[1] + 1);
src->images = static_cast<unsigned char**>(
malloc(sizeof(float*) * static_cast<size_t>(header->num_channels)));
for (size_t c = 0; c < static_cast<size_t>(header->num_channels); c++) {
assert(header->pixel_types[c] == TINYEXR_PIXELTYPE_FLOAT);
src->images[c] = static_cast<unsigned char*>(
malloc(sizeof(float) * data_width * data_height));
memset(src->images[c], 0, sizeof(float) * data_width * data_height);
}
for (size_t tile_idx = 0; tile_idx < static_cast<size_t>(src->num_tiles);
tile_idx++) {
size_t sx = static_cast<size_t>(src->tiles[tile_idx].offset_x *
header->tile_size_x);
size_t sy = static_cast<size_t>(src->tiles[tile_idx].offset_y *
header->tile_size_y);
size_t ex = static_cast<size_t>(src->tiles[tile_idx].offset_x *
header->tile_size_x +
src->tiles[tile_idx].width);
size_t ey = static_cast<size_t>(src->tiles[tile_idx].offset_y *
header->tile_size_y +
src->tiles[tile_idx].height);
for (size_t c = 0; c < static_cast<size_t>(header->num_channels); c++) {
float* dst_image = reinterpret_cast<float*>(src->images[c]);
const float* src_image =
reinterpret_cast<const float*>(src->tiles[tile_idx].images[c]);
for (size_t y = 0; y < static_cast<size_t>(ey - sy); y++) {
for (size_t x = 0; x < static_cast<size_t>(ex - sx); x++) {
dst_image[(y + sy) * data_width + (x + sx)] =
src_image[y * static_cast<size_t>(header->tile_size_x) + x];
}
}
}
}
}
#endif
#if defined(_WIN32)
#if defined(__MINGW32__)
// __wgetmainargs is not defined in windows.h
extern "C" int __wgetmainargs(int*, wchar_t***, wchar_t***, int, int*);
#endif
// https://gist.github.com/trueroad/fb4d0c3f67285bf66804
namespace {
std::vector<char> utf16_to_utf8(const wchar_t* wc) {
int size = WideCharToMultiByte(CP_UTF8, 0, wc, -1, NULL, 0, NULL, NULL);
std::vector<char> retval(size);
if (size) {
WideCharToMultiByte(CP_UTF8, 0, wc, -1, retval.data(), retval.size(), NULL,
NULL);
} else
retval.push_back('\0');
return retval;
}
} // namespace
#endif
static int test_main(int argc, char** argv);
#if defined(_WIN32)
#if defined(__MINGW32__)
int main() {
wchar_t** wargv;
wchar_t** wenpv;
int argc = 0, si = 0;
__wgetmainargs(&argc, &wargv, &wenpv, 1, &si);
std::vector<std::vector<char> > argv_vvc(argc);
std::vector<char*> argv_vc(argc);
for (int i = 0; i < argc; i++) {
argv_vvc.at(i) = utf16_to_utf8(wargv[i]);
argv_vc.at(i) = argv_vvc.at(i).data();
}
// TODO(syoyo): envp
return test_main(argc, argv_vc.data());
}
#else // Assume MSVC
int _tmain(int argc, _TCHAR** wargv) {
std::vector<std::vector<char> > argv_vvc(argc);
std::vector<char*> argv_vc(argc);
for (int i = 0; i < argc; i++) {
#if defined(UNICODE) || defined(_UNICODE)
argv_vvc.at(i) = utf16_to_utf8(wargv[i]);
#else
size_t slen = _tcslen(wargv[i]);
std::vector<char> buf(slen + 1);
memcpy(buf.data(), wargv[i], slen);
buf[slen] = '\0';
argv_vvc.at(i) = buf;
#endif
argv_vc.at(i) = argv_vvc.at(i).data();
}
return test_main(argc, argv_vc.data());
}
#endif
#else
int main(int argc, char** argv) { return test_main(argc, argv); }
#endif
int test_main(int argc, char** argv) {
const char* outfilename = "output_test.exr";
const char* err = NULL;
if (argc < 2) {
fprintf(stderr, "Needs input.exr.\n");
exit(-1);
}
if (argc > 2) {
outfilename = argv[2];
}
const char* input_filename = argv[1];
#ifdef SIMPLE_API_EXAMPLE
(void)outfilename;
int width, height;
float* image;
int ret = IsEXR(input_filename);
if (ret != TINYEXR_SUCCESS) {
fprintf(stderr, "Header err. code %d\n", ret);
exit(-1);
}
ret = LoadEXR(&image, &width, &height, input_filename, &err);
if (ret != TINYEXR_SUCCESS) {
if (err) {
fprintf(stderr, "Load EXR err: %s(code %d)\n", err, ret);
} else {
fprintf(stderr, "Load EXR err: code = %d\n", ret);
}
FreeEXRErrorMessage(err);
return ret;
}
// SaveAsPFM("output.pfm", width, height, image);
ret = SaveEXR(image, width, height, 4 /* =RGBA*/,
1 /* = save as fp16 format */, "output.exr", &err);
if (ret != TINYEXR_SUCCESS) {
if (err) {
fprintf(stderr, "Save EXR err: %s(code %d)\n", err, ret);
} else {
fprintf(stderr, "Failed to save EXR image. code = %d\n", ret);
}
}
free(image);
std::cout << "Wrote output.exr." << std::endl;
#else
EXRVersion exr_version;
int ret = ParseEXRVersionFromFile(&exr_version, input_filename);
if (ret != 0) {
fprintf(stderr, "Invalid EXR file: %s\n", input_filename);
return -1;
}
printf(
"version: tiled = %d, long_name = %d, non_image = %d, multipart = %d\n",
exr_version.tiled, exr_version.long_name, exr_version.non_image,
exr_version.multipart);
if (exr_version.multipart) {
EXRHeader** exr_headers; // list of EXRHeader pointers.
int num_exr_headers;
ret = ParseEXRMultipartHeaderFromFile(&exr_headers, &num_exr_headers,
&exr_version, argv[1], &err);
if (ret != 0) {
fprintf(stderr, "Parse EXR err: %s\n", err);
return ret;
}
printf("num parts = %d\n", num_exr_headers);
for (size_t i = 0; i < static_cast<size_t>(num_exr_headers); i++) {
const EXRHeader& exr_header = *(exr_headers[i]);
printf("Part: %lu\n", static_cast<unsigned long>(i));
printf("dataWindow = %d, %d, %d, %d\n", exr_header.data_window[0],
exr_header.data_window[1], exr_header.data_window[2],
exr_header.data_window[3]);
printf("displayWindow = %d, %d, %d, %d\n", exr_header.display_window[0],
exr_header.display_window[1], exr_header.display_window[2],
exr_header.display_window[3]);
printf("screenWindowCenter = %f, %f\n",
static_cast<double>(exr_header.screen_window_center[0]),
static_cast<double>(exr_header.screen_window_center[1]));
printf("screenWindowWidth = %f\n",
static_cast<double>(exr_header.screen_window_width));
printf("pixelAspectRatio = %f\n",
static_cast<double>(exr_header.pixel_aspect_ratio));
printf("lineOrder = %d\n", exr_header.line_order);
if (exr_header.num_custom_attributes > 0) {
printf("# of custom attributes = %d\n",
exr_header.num_custom_attributes);
for (int a = 0; a < exr_header.num_custom_attributes; a++) {
printf(" [%d] name = %s, type = %s, size = %d\n", a,
exr_header.custom_attributes[a].name,
exr_header.custom_attributes[a].type,
exr_header.custom_attributes[a].size);
// if (strcmp(exr_header.custom_attributes[i].type, "float") == 0) {
// printf(" value = %f\n", *reinterpret_cast<float
// *>(exr_header.custom_attributes[i].value));
//}
}
}
}
std::vector<EXRImage> images(static_cast<size_t>(num_exr_headers));
for (size_t i = 0; i < static_cast<size_t>(num_exr_headers); i++) {
InitEXRImage(&images[i]);
}
ret = LoadEXRMultipartImageFromFile(
&images.at(0), const_cast<const EXRHeader**>(exr_headers),
static_cast<unsigned int>(num_exr_headers), input_filename, &err);
if (ret != 0) {
fprintf(stderr, "Load EXR err: %s\n", err);
FreeEXRErrorMessage(err);
return ret;
}
printf("Loaded %d part images\n", num_exr_headers);
printf(
"There is no saving feature for multi-part images, thus just exit an "
"application...\n");
for (size_t i = 0; i < static_cast<size_t>(num_exr_headers); i++) {
FreeEXRImage(&images.at(i));
}
for (size_t i = 0; i < static_cast<size_t>(num_exr_headers); i++) {
FreeEXRHeader(exr_headers[i]);
free(exr_headers[i]);
}
free(exr_headers);
} else { // single-part EXR
EXRHeader exr_header;
InitEXRHeader(&exr_header);
ret =
ParseEXRHeaderFromFile(&exr_header, &exr_version, input_filename, &err);
if (ret != 0) {
fprintf(stderr, "Parse single-part EXR err: %s\n", err);
FreeEXRErrorMessage(err);
return ret;
}
printf("dataWindow = %d, %d, %d, %d\n", exr_header.data_window[0],
exr_header.data_window[1], exr_header.data_window[2],
exr_header.data_window[3]);
printf("displayWindow = %d, %d, %d, %d\n", exr_header.display_window[0],
exr_header.display_window[1], exr_header.display_window[2],
exr_header.display_window[3]);
printf("screenWindowCenter = %f, %f\n",
static_cast<double>(exr_header.screen_window_center[0]),
static_cast<double>(exr_header.screen_window_center[1]));
printf("screenWindowWidth = %f\n",
static_cast<double>(exr_header.screen_window_width));
printf("pixelAspectRatio = %f\n",
static_cast<double>(exr_header.pixel_aspect_ratio));
printf("lineOrder = %d\n", exr_header.line_order);
if (exr_header.num_custom_attributes > 0) {
printf("# of custom attributes = %d\n", exr_header.num_custom_attributes);
for (int i = 0; i < exr_header.num_custom_attributes; i++) {
printf(" [%d] name = %s, type = %s, size = %d\n", i,
exr_header.custom_attributes[i].name,
exr_header.custom_attributes[i].type,
exr_header.custom_attributes[i].size);
// if (strcmp(exr_header.custom_attributes[i].type, "float") == 0) {
// printf(" value = %f\n", *reinterpret_cast<float
// *>(exr_header.custom_attributes[i].value));
//}
}
}
// Read HALF channel as FLOAT.
for (int i = 0; i < exr_header.num_channels; i++) {
if (exr_header.pixel_types[i] == TINYEXR_PIXELTYPE_HALF) {
exr_header.requested_pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT;
}
}
EXRImage exr_image;
InitEXRImage(&exr_image);
ret = LoadEXRImageFromFile(&exr_image, &exr_header, input_filename, &err);
if (ret != 0) {
fprintf(stderr, "Load EXR err: %s\n", err);
FreeEXRHeader(&exr_header);
FreeEXRErrorMessage(err);
return ret;
}
printf("EXR: %d x %d\n", exr_image.width, exr_image.height);
for (int i = 0; i < exr_header.num_channels; i++) {
printf("pixelType[%d]: %s\n", i, GetPixelType(exr_header.pixel_types[i]));
printf("chan[%d] = %s\n", i, exr_header.channels[i].name);
printf("requestedPixelType[%d]: %s\n", i,
GetPixelType(exr_header.requested_pixel_types[i]));
}
#if 0 // example to write custom attribute
int version_minor = 3;
exr_header.num_custom_attributes = 1;
exr_header.custom_attributes = reinterpret_cast<EXRAttribute *>(malloc(sizeof(EXRAttribute) * exr_header.custom_attributes));
strcpy(exr_header.custom_attributes[0].name, "tinyexr_version_minor");
exr_header.custom_attributes[0].name[strlen("tinyexr_version_minor")] = '\0';
strcpy(exr_header.custom_attributes[0].type, "int");
exr_header.custom_attributes[0].type[strlen("int")] = '\0';
exr_header.custom_attributes[0].size = sizeof(int);
exr_header.custom_attributes[0].value = (unsigned char*)malloc(sizeof(int));
memcpy(exr_header.custom_attributes[0].value, &version_minor, sizeof(int));
#endif
if (exr_header.tiled) {
TiledImageToScanlineImage(&exr_image, &exr_header);
}
exr_header.compression_type = TINYEXR_COMPRESSIONTYPE_NONE;
#ifdef TEST_ZFP_COMPRESSION
// Assume input image is FLOAT pixel type.
for (int i = 0; i < exr_header.num_channels; i++) {
exr_header.channels[i].pixel_type = TINYEXR_PIXELTYPE_FLOAT;
exr_header.requested_pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT;
}
unsigned char zfp_compression_type = TINYEXR_ZFP_COMPRESSIONTYPE_RATE;
double zfp_compression_rate = 4;
exr_header.num_custom_attributes = 2;
strcpy(exr_header.custom_attributes[0].name, "zfpCompressionType");
exr_header.custom_attributes[0].name[strlen("zfpCompressionType")] = '\0';
exr_header.custom_attributes[0].size = 1;
exr_header.custom_attributes[0].value =
(unsigned char*)malloc(sizeof(unsigned char));
exr_header.custom_attributes[0].value[0] = zfp_compression_type;
strcpy(exr_header.custom_attributes[1].name, "zfpCompressionRate");
exr_header.custom_attributes[1].name[strlen("zfpCompressionRate")] = '\0';
exr_header.custom_attributes[1].size = sizeof(double);
exr_header.custom_attributes[1].value =
(unsigned char*)malloc(sizeof(double));
memcpy(exr_header.custom_attributes[1].value, &zfp_compression_rate,
sizeof(double));
exr_header.compression_type = TINYEXR_COMPRESSIONTYPE_ZFP;
#endif
ret = SaveEXRImageToFile(&exr_image, &exr_header, outfilename, &err);
if (ret != 0) {
fprintf(stderr, "Save EXR err: %s\n", err);
FreeEXRHeader(&exr_header);
FreeEXRErrorMessage(err);
return ret;
}
printf("Saved exr file. [ %s ] \n", outfilename);
FreeEXRHeader(&exr_header);
FreeEXRImage(&exr_image);
}
#endif
return ret;
}