2

Trying to display a uint8_t* rgb image data buffer to an HTML canvas that was process in C via WASM.

In C I have the following external method:

extern void JS_DisplayRenderData(uint8_t* data, int dataLength);

Then I call the extrnal method like so:

int size = 1280 * 720 * 3;
uint8_t data[size];
memset(data, 255, size);
JS_DisplayRenderData(data, size);

In javaScript I then try to display the buffer like so:

if (typeof mergeInto !== 'undefined') mergeInto(LibraryManager.library,
{
    JS_DisplayRenderData: function(data, dataLength)
    {
        alert("Data Length: " + dataLength);
        var c = document.getElementById("canvas");
        var ctx = c.getContext("2d");
        var imgdata = ctx.getImageData(0, 0, c.width, c.height);
        var imgdatalen = imgdata.data.length;
        var i2 = 0;
        for (var i = 0; i < (imgdatalen / 4); i++)
        {
            imgdata.data[4*i] = data[i2];    // RED (0-255)
            imgdata.data[4*i+1] = data[i2+1];    // GREEN (0-255)
            imgdata.data[4*i+2] = data[i2+2];    // BLUE (0-255)
            imgdata.data[4*i+3] = 255;  // APLHA (0-255)
            i2 += 3;
        }
        ctx.putImageData(imgdata, 0, 0);
    }
});

However all I get are black pixels even though it should all be white.

2
  • extern...and accessing data via index directly in JS side? Could you attach a complete source, including your emcc commands, the use Emscripten macros in C files, and JS file? To me this code should not able to run nor should not even able to compile in the first place. Commented Mar 31, 2019 at 23:39
  • Found the issue. Just needed "var a = HEAPU8.subarray(data);". Will post more info in an answer. Commented Apr 1, 2019 at 0:05

1 Answer 1

1

Found the answer tnx to: Struct operations in Javascript through Emscripten

Just needed to state what the buffer type was via: "var a = HEAPU8.subarray(data);"

The method I'm using to invoke JS via C can be found: Webassembly calling JavaScript methods from wasm i.e. whithin c++ code

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.