Binary mask to RGBA conversion

Benchmark created on


Description

Converting a binary mask to RGBA where RGB are 0xff and the alpha channel is 0xff for foreground pixels and 0x0 for background pixels (data has 63 items)

Setup

const data = new Uint8Array([
  0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff,
  0x00, 0x00, 0xff, 0xff, 0x00, 0xff, 0x00,
  0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00,
  0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff,
  0x00, 0x00, 0xff, 0xff, 0x00, 0xff, 0x00,
  0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00,
  0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff,
  0x00, 0x00, 0xff, 0xff, 0x00, 0xff, 0x00,
  0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00
]);

Test runner

Ready to run.

Testing in
TestOps/sec
Assigning pixels
const len = data.length;
const destLen = len * 4;
const destData = new Uint8Array(destLen);
const RGA = 0x00ffffff;
for (let i = 0; i < destLen; i++) {
    const ci = i * 4;
    destData[ci] = 255; // R value
    destData[ci + 1] = 255; // G value
    destData[ci + 2] = 255; // B value
    destData[ci + 3] = data[i] > 0 ? 255 : 0; // A value
}
ready
Shifting pixels
const len = data.length;
const destData = new Uint32Array(len);
const RGA = 0x00ffffff;
for (let i = 0; i < len; i++) {
    const alpha = data[i] > 0 ? 0xff000000 : 0x0; // alpha is the high byte. Bits 24-31
    destData[i] = alpha + RGA;
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.