all files / src/imageLoader/wadouri/ unpackBinaryFrame.js

0% Statements 0/8
0% Branches 0/2
0% Functions 0/2
0% Lines 0/8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32                                                               
/* eslint no-bitwise: 0 */
 
function isBitSet (byte, bitPos) {
  return byte & (1 << bitPos);
}
 
/**
 * Function to deal with unpacking a binary frame
 */
function unpackBinaryFrame (byteArray, frameOffset, pixelsPerFrame) {
  // Create a new pixel array given the image size
  const pixelData = new Uint8Array(pixelsPerFrame);
 
  for (let i = 0; i < pixelsPerFrame; i++) {
    // Compute byte position
    const bytePos = Math.floor(i / 8);
 
    // Get the current byte
    const byte = byteArray[bytePos + frameOffset];
 
    // Bit position (0-7) within byte
    const bitPos = (i % 8);
 
    // Check whether bit at bitpos is set
    pixelData[i] = isBitSet(byte, bitPos) ? 1 : 0;
  }
 
  return pixelData;
}
 
export default unpackBinaryFrame;