I work with emscripten where I create a bitmap in C++ which I pass to my javascript code
const int COMP = 4;
long createBitmap(int DIM)
{
srand(NULL);
// create buffer
long buffer = EM_ASM_INT(
{
var buffer = Module._malloc($0 * 1);
return buffer;
}, DIM*DIM*COMP);
uint8_t* bitmap = (uint8_t*)buffer;
//just randomly fill the buffer
for (int i = 0; i < DIM*DIM*COMP; i++)
{
if (i%4==3)
bitmap[i] = 128;
else
bitmap[i] = rand()%256;
}
return buffer;
}
and in javascript I have the following code:
var dim = 1080;
var buffer = Module.createBitmap(dim);
var c = document.getElementById("bitmap");
var ctx = c.getContext("2d");
var imgData = ctx.createImageData(dim,dim);
for (var i = 0; i < dim*dim*4; i++) {
imgData.data[i] = Module.HEAPU8[buffer+i];
}
ctx.putImageData(imgData, 0, 0)
This works well but I don't like the loop to assign all the elements of the buffer to the imgData.data array. I know that the uint8_t data type that I use in C++ corresponds to the Uint8ClampedArray for imgData.data. To me this seems like a good opportunity to just assign the beginning of the buffer to this imgData.data and I won't have to copy anything - is this possible?