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
|
#include <vector>
#include <emscripten/bind.h>
#define TINYEXR_IMPLEMENTATION
#include "tinyexr.h"
using namespace emscripten;
///
/// Simple C++ wrapper class for Emscripten
///
class EXRLoader {
public:
///
/// `binary` is the buffer for EXR binary(e.g. buffer read by fs.readFileSync)
/// std::string can be used as UInt8Array in JS layer.
///
EXRLoader(const std::string &binary) {
const float *ptr = reinterpret_cast<const float *>(binary.data());
float *rgba = nullptr;
width_ = -1;
height_ = -1;
const char *err = nullptr;
error_.clear();
result_ = LoadEXRFromMemory(
&rgba, &width_, &height_,
reinterpret_cast<const unsigned char *>(binary.data()), binary.size(),
&err);
if (TINYEXR_SUCCESS == result_) {
image_.resize(size_t(width_ * height_ * 4));
memcpy(image_.data(), rgba, sizeof(float) * size_t(width_ * height_ * 4));
free(rgba);
} else {
if (err) {
error_ = std::string(err);
}
}
}
~EXRLoader() {}
// Return as memory views
emscripten::val getBytes() const {
return emscripten::val(
emscripten::typed_memory_view(image_.size(), image_.data()));
}
bool ok() const { return (TINYEXR_SUCCESS == result_); }
const std::string error() const { return error_; }
int width() const { return width_; }
int height() const { return height_; }
private:
std::vector<float> image_; // RGBA
int width_;
int height_;
int result_;
std::string error_;
};
// Register STL
EMSCRIPTEN_BINDINGS(stl_wrappters) { register_vector<float>("VectorFloat"); }
EMSCRIPTEN_BINDINGS(tinyexr_module) {
class_<EXRLoader>("EXRLoader")
.constructor<const std::string &>()
.function("getBytes", &EXRLoader::getBytes)
.function("ok", &EXRLoader::ok)
.function("error", &EXRLoader::error)
.function("width", &EXRLoader::width)
.function("height", &EXRLoader::height);
}
|