All files / dicomImageLoader/src/imageLoader/wadouri unpackBinaryFrame.ts

0% Statements 0/9
0% Branches 0/2
0% Functions 0/2
0% Lines 0/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 33 34 35 36 37 38                                                                           
/* eslint no-bitwise: 0 */
 
import type { ByteArray } from 'dicom-parser';
 
function isBitSet(byte: number, bitPos: number) {
  return byte & (1 << bitPos);
}
 
/**
 * Function to deal with unpacking a binary frame
 */
function unpackBinaryFrame(
  byteArray: ByteArray,
  frameOffset: number,
  pixelsPerFrame: number
): Uint8Array {
  // 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;