Uint8Array based Image: Fastest way to copy sub image

200 views Asked by At

I have an RGBA image represented as an Uint8Array and I want to copy a sub rectangle of that image into a new array in the fastest possible way.

Currently I do something like this:

let sourceImage = { width: 640, height: 400, data: new Uint8Array( 640 * 480 * 4 ) };
let destImage = { width: 100, height: 100, data: new Uint8Array( 100 * 100 * 4 ) };
let offX = 100, offY = 50;

for( y=0; y < destImage.height; ++y )
{
  let toCopyArray = sourceImage.data.subarray( (offY+y) * sourceImage.width * 4 + offX * 4, (offY+y) * sourceImage.width * 4 + offX * 4 + image.width * 4 );
  destImage.data.set( toCopyArray, y * destImage.width * 4 );
}

Is there a faster way ?

0

There are 0 answers