Yes, you can create a new Uint8ClampedArray using the same backing buffer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays#Multiple_views_on_the_same_data
Be warned that IE 10 is missing Uint8ClampedArray if that's a compatibility issue for you. (10 should be pretty rare these days, 11 is fine iirc) -- brion On Nov 11, 2016 2:06 AM, "Philipp Gloor" <[email protected]> wrote: > I'm wondering if it is possible to convert memory location to an > ArrayBuffer. > > 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] = 255; > else > bitmap[i] = rand()%256; > } > > return buffer; > } > > > This is the C++ code where I just fill an array with random RGB values. > And I can display the whole thing without problems in javascript > > 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) > > > What I don't like here though is the fact that I have to loop through all > the bitmap components separately and assign them to the imgData.data array. > This array is a Uint8ClampedArray and I was wondering if there is a way to > convert this 'buffer' variable to an ArrayBuffer of type Uint8ClampedArray > which would save a lot of copying. Especially since there could be several > bitmaps passed per second. > > -- > You received this message because you are subscribed to the Google Groups > "emscripten-discuss" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "emscripten-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
