-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwasm_bindings.cpp
More file actions
294 lines (238 loc) · 9.54 KB
/
wasm_bindings.cpp
File metadata and controls
294 lines (238 loc) · 9.54 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
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
// Note: Requires C++17 or newer for Emscripten Embind
#include <emscripten/bind.h>
#include <emscripten/val.h>
#include <stdint.h>
#include <vector>
#include <cstring>
// Include all the decoder headers
#include "bcn.h"
#include "pvrtc.h"
#include "etc.h"
#include "atc.h"
#include "astc.h"
#include "crunch.h"
#include "unitycrunch.h"
using namespace emscripten;
// Helper function to validate dimensions
bool validate_dimensions(uint32_t width, uint32_t height) {
if (width == 0 || height == 0) {
return false;
}
// Prevent integer overflow in size calculation
if (width > UINT32_MAX / height || width * height > UINT32_MAX / 4) {
return false;
}
return true;
}
// Generic decoder wrapper for simple decoders - using typed array for binary-safe data transfer
template<typename DecodeFunc>
val decode_generic(val typedArray, uint32_t width, uint32_t height, DecodeFunc decode_func) {
// Validate input
if (typedArray.isNull() || typedArray.isUndefined()) {
return val::null();
}
if (!validate_dimensions(width, height)) {
return val::null();
}
// Get length from typed array
unsigned int length = typedArray["length"].as<unsigned int>();
if (length == 0) {
return val::null();
}
// Copy data from JavaScript typed array to C++ vector (binary-safe)
std::vector<uint8_t> data(length);
for (unsigned int i = 0; i < length; ++i) {
data[i] = typedArray[i].as<uint8_t>();
}
size_t output_size = static_cast<size_t>(width) * height * 4;
std::vector<uint8_t> output(output_size);
if (!decode_func(data.data(), width, height, reinterpret_cast<uint32_t*>(output.data()))) {
return val::null();
}
return val(typed_memory_view(output.size(), output.data()));
}
// BC decoders
val decode_bc1_wrapper(val typedArray, uint32_t width, uint32_t height) {
return decode_generic(typedArray, width, height, decode_bc1);
}
val decode_bc3_wrapper(val typedArray, uint32_t width, uint32_t height) {
return decode_generic(typedArray, width, height, decode_bc3);
}
val decode_bc4_wrapper(val typedArray, uint32_t width, uint32_t height) {
return decode_generic(typedArray, width, height, decode_bc4);
}
val decode_bc5_wrapper(val typedArray, uint32_t width, uint32_t height) {
return decode_generic(typedArray, width, height, decode_bc5);
}
val decode_bc6_wrapper(val typedArray, uint32_t width, uint32_t height) {
return decode_generic(typedArray, width, height, decode_bc6);
}
val decode_bc7_wrapper(val typedArray, uint32_t width, uint32_t height) {
return decode_generic(typedArray, width, height, decode_bc7);
}
// ETC decoders
val decode_etc1_wrapper(val typedArray, uint32_t width, uint32_t height) {
return decode_generic(typedArray, width, height, decode_etc1);
}
val decode_etc2_wrapper(val typedArray, uint32_t width, uint32_t height) {
return decode_generic(typedArray, width, height, decode_etc2);
}
val decode_etc2a1_wrapper(val typedArray, uint32_t width, uint32_t height) {
return decode_generic(typedArray, width, height, decode_etc2a1);
}
val decode_etc2a8_wrapper(val typedArray, uint32_t width, uint32_t height) {
return decode_generic(typedArray, width, height, decode_etc2a8);
}
// EAC decoders
val decode_eacr_wrapper(val typedArray, uint32_t width, uint32_t height) {
return decode_generic(typedArray, width, height, decode_eacr);
}
val decode_eacr_signed_wrapper(val typedArray, uint32_t width, uint32_t height) {
return decode_generic(typedArray, width, height, decode_eacr_signed);
}
val decode_eacrg_wrapper(val typedArray, uint32_t width, uint32_t height) {
return decode_generic(typedArray, width, height, decode_eacrg);
}
val decode_eacrg_signed_wrapper(val typedArray, uint32_t width, uint32_t height) {
return decode_generic(typedArray, width, height, decode_eacrg_signed);
}
// ATC decoders
val decode_atc_rgb4_wrapper(val typedArray, uint32_t width, uint32_t height) {
return decode_generic(typedArray, width, height, decode_atc_rgb4);
}
val decode_atc_rgba8_wrapper(val typedArray, uint32_t width, uint32_t height) {
return decode_generic(typedArray, width, height, decode_atc_rgba8);
}
// PVRTC decoder (special case with is2bpp parameter)
val decode_pvrtc_wrapper(val typedArray, uint32_t width, uint32_t height, bool is2bpp) {
// Validate input
if (typedArray.isNull() || typedArray.isUndefined()) {
return val::null();
}
if (!validate_dimensions(width, height)) {
return val::null();
}
// Get length from typed array
unsigned int length = typedArray["length"].as<unsigned int>();
if (length == 0) {
return val::null();
}
// Copy data from JavaScript typed array to C++ vector (binary-safe)
std::vector<uint8_t> data(length);
for (unsigned int i = 0; i < length; ++i) {
data[i] = typedArray[i].as<uint8_t>();
}
size_t output_size = static_cast<size_t>(width) * height * 4;
std::vector<uint8_t> output(output_size);
if (!decode_pvrtc(data.data(), width, height,
reinterpret_cast<uint32_t*>(output.data()), is2bpp ? 1 : 0)) {
return val::null();
}
return val(typed_memory_view(output.size(), output.data()));
}
// ASTC decoder (special case with block dimensions)
val decode_astc_wrapper(val typedArray, uint32_t width, uint32_t height,
uint32_t block_width, uint32_t block_height) {
// Validate input
if (typedArray.isNull() || typedArray.isUndefined()) {
return val::null();
}
if (!validate_dimensions(width, height)) {
return val::null();
}
if (block_width == 0 || block_height == 0 || block_width > 12 || block_height > 12) {
return val::null();
}
// Get length from typed array
unsigned int length = typedArray["length"].as<unsigned int>();
if (length == 0) {
return val::null();
}
// Copy data from JavaScript typed array to C++ vector (binary-safe)
std::vector<uint8_t> data(length);
for (unsigned int i = 0; i < length; ++i) {
data[i] = typedArray[i].as<uint8_t>();
}
size_t output_size = static_cast<size_t>(width) * height * 4;
std::vector<uint8_t> output(output_size);
if (!decode_astc(data.data(), width, height,
block_width, block_height, reinterpret_cast<uint32_t*>(output.data()))) {
return val::null();
}
return val(typed_memory_view(output.size(), output.data()));
}
// Crunch unpack - using typed array for binary-safe data transfer
val unpack_crunch_wrapper(val typedArray) {
// Validate input
if (typedArray.isNull() || typedArray.isUndefined()) {
return val::null();
}
// Get length from typed array
unsigned int length = typedArray["length"].as<unsigned int>();
if (length == 0) {
return val::null();
}
// Copy data from JavaScript typed array to C++ vector (binary-safe)
std::vector<uint8_t> data(length);
for (unsigned int i = 0; i < length; ++i) {
data[i] = typedArray[i].as<uint8_t>();
}
void* ret = nullptr;
uint32_t retSize = 0;
if (!crunch_unpack_level(data.data(), length, 0, &ret, &retSize)) {
return val::null();
}
std::vector<uint8_t> output(retSize);
std::memcpy(output.data(), ret, retSize);
delete[] static_cast<uint8_t*>(ret);
return val(typed_memory_view(output.size(), output.data()));
}
// Unity Crunch unpack - using typed array for binary-safe data transfer
val unpack_unity_crunch_wrapper(val typedArray) {
// Validate input
if (typedArray.isNull() || typedArray.isUndefined()) {
return val::null();
}
// Get length from typed array
unsigned int length = typedArray["length"].as<unsigned int>();
if (length == 0) {
return val::null();
}
// Copy data from JavaScript typed array to C++ vector (binary-safe)
std::vector<uint8_t> data(length);
for (unsigned int i = 0; i < length; ++i) {
data[i] = typedArray[i].as<uint8_t>();
}
void* ret = nullptr;
uint32_t retSize = 0;
if (!unity_crunch_unpack_level(data.data(), length, 0, &ret, &retSize)) {
return val::null();
}
std::vector<uint8_t> output(retSize);
std::memcpy(output.data(), ret, retSize);
delete[] static_cast<uint8_t*>(ret);
return val(typed_memory_view(output.size(), output.data()));
}
// Emscripten bindings
EMSCRIPTEN_BINDINGS(texture2ddecoder) {
function("decode_bc1", &decode_bc1_wrapper);
function("decode_bc3", &decode_bc3_wrapper);
function("decode_bc4", &decode_bc4_wrapper);
function("decode_bc5", &decode_bc5_wrapper);
function("decode_bc6", &decode_bc6_wrapper);
function("decode_bc7", &decode_bc7_wrapper);
function("decode_pvrtc", &decode_pvrtc_wrapper);
function("decode_etc1", &decode_etc1_wrapper);
function("decode_etc2", &decode_etc2_wrapper);
function("decode_etc2a1", &decode_etc2a1_wrapper);
function("decode_etc2a8", &decode_etc2a8_wrapper);
function("decode_eacr", &decode_eacr_wrapper);
function("decode_eacr_signed", &decode_eacr_signed_wrapper);
function("decode_eacrg", &decode_eacrg_wrapper);
function("decode_eacrg_signed", &decode_eacrg_signed_wrapper);
function("decode_atc_rgb4", &decode_atc_rgb4_wrapper);
function("decode_atc_rgba8", &decode_atc_rgba8_wrapper);
function("decode_astc", &decode_astc_wrapper);
function("unpack_crunch", &unpack_crunch_wrapper);
function("unpack_unity_crunch", &unpack_unity_crunch_wrapper);
}