newbie to emscripten, image processing error

41 views
Skip to first unread message

Davide Baltieri

unread,
May 27, 2021, 7:19:35 AM5/27/21
to emscripten-discuss
Hi,
 I'm porting parts of my computer vision code from C/C++ to js with emscripten.

I have an html page that opens a webcam, grabs frames and send them to the converted c functions, below part of the js code i use to grab frames and call converted functions:

let processor = {
timerCallback: function() {
this.computeFrame();
let self = this;
setTimeout(function () {
self.timerCallback();
}, 0);
},
computeFrame: function() {
if(canvas.width>0) {
//ctx.clearRect(0,0, 640, 480);
ctx.fillRect(0,0, 640, 480);
ctx.drawImage(video, 0, 0, 640, 480);
let frame = ctx.getImageData(0, 0, 640, 480);
_dbx_set_process_size(640, 480);
Module.HEAPU8.set(frame.data, frame.data.byteOffset);
_dbx_upload_frame(frame.data.byteOffset, 640, 480, 640*4, 0);
frame.data.set(new Uint8ClampedArray(Module.HEAPU8.buffer , frame.data.byteOffset , 640*480*4));
ctx.putImageData(frame, 0, 0);
}
return;
}
};

this seems to work,  _dbx_set_process_size and _dbx_upload_frame are c functions converted to js with emscripten in my .js/.wasm, their C code is:

void dbx_set_process_size(int width, int height) {
printf("dbx_set_process_size %d %d\n", width, height);
DBX::Params::prm_process_height = height;
DBX::Params::prm_process_width = width;
}

int dbx_upload_frame(char* image_data, int width, int height, int step, int format) {
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
image_data[i * width * 4 + j * 4] = 0;
}
}
return 0;
}

which works, as I correctly see the webcam frames with the red channel set to 0. However if I modify dbx_upload_frame, even just by adding a printf, like:

int dbx_upload_frame(char* image_data, int width, int height, int step, int format) {
printf("dbx_upload_frame\n");
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
image_data[i * width * 4 + j * 4] = 0;
}
}
return 0;
}

i get Uncaught (in promise) RuntimeError: memory access out of bounds. Adding DBX::Processor::m_process_image.resize(width * height);
instead of the printf gives the same error (m_process_image is just an std vector of uint8_t)

Why?
Reply all
Reply to author
Forward
0 new messages