[英]Passing array to c++ .wasm module. Emscripten
I have an array consisting of mask data for a corresponding image i need to pass to a c++ function compiled with emscripten.我有一个包含相应图像的掩码数据的数组,我需要传递给用 emscripten 编译的 c++ function。 The mask array consists of values ranging from -1 to 255, so i guess an Int16Array must be used to represent the values.
掩码数组由 -1 到 255 的值组成,所以我猜必须使用 Int16Array 来表示这些值。 Is it possible to pass this array as a pointer to the wasm module?
是否可以将此数组作为指向 wasm 模块的指针传递? I know I have to allocate the memory and pass the pointer, but I am stuck on the following code.
我知道我必须分配 memory 并传递指针,但我被困在下面的代码上。 How do i correctly allocate and set the pointer in order for the c++ function to see the correct data?
我如何正确分配和设置指针以便 c++ function 看到正确的数据?
The maskData is obtained as: maskData 获得如下:
const mData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var data = Array.prototype.slice.call(mData.data);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
let data = imageData.data;
const dataLength = data.length;
const maskDataLength = maskData.length;
var dataPointer = Module._malloc(dataLength * data.BYTES_PER_ELEMENT);
var maskDataPointer = Module._malloc(maskDataLength * 2);
Module.HEAPU8.set(data, dataPointer);
Module.HEAP16.set(maskData, maskDataPointer);
let result = Module.ccall(
"process",
"number",
["number", "number", "number", "number", "number"],
[dataPointer, dataLength, maskDataPointer, pos, neg]
);
data = new Int16Array(Module.HEAP16.buffer, result, dataLength);
Module._free(dataPointer);
Module._free(maskDataPointer);
Module._free(result);
maskPointer
represents index of HEAP8, so divide by 2 to access HEAP16. maskPointer
表示 HEAP8 的索引,因此除以 2 即可访问 HEAP16。
Module.HEAP16.set(maskData, maskDataPointer >> 1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.