@voltstack/headless-rasterizer 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,75 @@
1
+ #include <node_api.h>
2
+ #include "rasterizer/rasterizer.h"
3
+
4
+ namespace NapiBinding {
5
+
6
+ static std::string napiGetString(napi_env env, napi_value v) {
7
+ size_t len = 0;
8
+ napi_get_value_string_utf8(env, v, nullptr, 0, &len);
9
+ std::string s(len, '\0');
10
+ napi_get_value_string_utf8(env, v, &s[0], len + 1, &len);
11
+ return s;
12
+ }
13
+
14
+ static napi_value Rasterize(napi_env env, napi_callback_info info) {
15
+ size_t argc = 7;
16
+ napi_value args[7];
17
+ napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
18
+
19
+ if(argc < 7){
20
+ napi_value res;
21
+ napi_get_boolean(env, false, &res);
22
+ return res;
23
+ }
24
+
25
+ std::string glbPath = napiGetString(env, args[0]);
26
+ std::string pngPath = napiGetString(env, args[1]);
27
+
28
+ int32_t width = 0, height = 0;
29
+ napi_get_value_int32(env, args[2], &width);
30
+ napi_get_value_int32(env, args[3], &height);
31
+
32
+ double az = 0, el = 0;
33
+ napi_get_value_double(env, args[4], &az);
34
+ napi_get_value_double(env, args[5], &el);
35
+
36
+ napi_value opts = args[6], v;
37
+ Rasterizer::Options ropts{};
38
+ double fov = ropts.fovDeg, distScale = ropts.distScale;
39
+ bool zUp = ropts.zUp;
40
+
41
+ if(napi_get_named_property(env, opts, "fov", &v) == napi_ok) napi_get_value_double(env, v, &fov);
42
+ if(napi_get_named_property(env, opts, "distScale", &v) == napi_ok) napi_get_value_double(env, v, &distScale);
43
+ if(napi_get_named_property(env, opts, "zUp", &v) == napi_ok) napi_get_value_bool(env, v, &zUp);
44
+
45
+ ropts.fovDeg = (float)fov;
46
+ ropts.distScale = (float)distScale;
47
+ ropts.zUp = zUp;
48
+
49
+ if(width <= 0 || height <= 0){
50
+ napi_value res;
51
+ napi_get_boolean(env, false, &res);
52
+ return res;
53
+ }
54
+
55
+ Rasterizer raster;
56
+ bool ok = raster.rasterize(glbPath.c_str(), pngPath.c_str(),
57
+ width, height,
58
+ (float)az, (float)el,
59
+ ropts);
60
+
61
+ napi_value res;
62
+ napi_get_boolean(env, ok, &res);
63
+ return res;
64
+ }
65
+
66
+ static napi_value Init(napi_env env, napi_value exports) {
67
+ napi_value fn;
68
+ napi_create_function(env, nullptr, 0, Rasterize, nullptr, &fn);
69
+ napi_set_named_property(env, exports, "rasterize", fn);
70
+ return exports;
71
+ }
72
+
73
+ }
74
+
75
+ NAPI_MODULE(NODE_GYP_MODULE_NAME, NapiBinding::Init)