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

53.12% Statements 17/32
35% Branches 7/20
100% Functions 1/1
53.12% Lines 17/32

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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99                        128x 128x 128x 128x   128x                                       128x   128x               128x 128x       128x 12x 12x       12x     116x 116x 116x       116x                                                              
import type { DataSet } from 'dicom-parser';
import unpackBinaryFrame from './unpackBinaryFrame';
 
/**
 * Function to deal with extracting an image frame from an encapsulated data set.
 */
 
function getUncompressedImageFrame(
  dataSet: DataSet,
  frameIndex: number
): Uint8Array {
  const pixelDataElement =
    dataSet.elements.x7fe00010 || dataSet.elements.x7fe00008;
  const bitsAllocated = dataSet.uint16('x00280100');
  const rows = dataSet.uint16('x00280010');
  const columns = dataSet.uint16('x00280011');
 
  let samplesPerPixel = dataSet.uint16('x00280002');
 
  /**
   * From: http://dicom.nema.org/medical/dicom/current/output/chtml/part03/sect_C.7.6.3.html
   *
   * Though the chrominance channels are downsampled, there are still nominally
   * three channels, hence Samples per Pixel (0028,0002) has a value of 3, not
   * 2. I.e., for pixel data in a Native (uncompressed) format, the Value Length
   * of Pixel Data (7FE0,0010) is not:
   *
   * Rows (0028,0010) * Columns (0028,0011) * Number of Frames (0028,0008) *
   * Samples per Pixel (0028,0002) * (⌊(Bits Allocated (0028,0100)-1)/8⌋+1)
   *
   * padded to an even length, as it would otherwise be, but rather is:
   *
   * Rows (0028,0010) * Columns (0028,0011) * Number of Frames (0028,0008) * 2 *
   * (⌊(Bits Allocated (0028,0100)-1)/8⌋+1)
   *
   * padded to an even length.
   */
  const photometricInterpretation = dataSet.string('x00280004');
 
  Iif (photometricInterpretation === 'YBR_FULL_422') {
    samplesPerPixel = 2;
    console.warn(
      `Using SamplesPerPixel of 2 for YBR_FULL_422 photometric interpretation.
      See http://dicom.nema.org/medical/dicom/current/output/chtml/part03/sect_C.7.6.3.html for more information.`
    );
  }
 
  const pixelDataOffset = pixelDataElement.dataOffset;
  const pixelsPerFrame = rows * columns * samplesPerPixel;
 
  let frameOffset;
 
  if (bitsAllocated === 8) {
    frameOffset = pixelDataOffset + frameIndex * pixelsPerFrame;
    Iif (frameOffset >= dataSet.byteArray.length) {
      throw new Error('frame exceeds size of pixelData');
    }
 
    return new Uint8Array(
      dataSet.byteArray.buffer.slice(frameOffset, frameOffset + pixelsPerFrame)
    );
  } else if (bitsAllocated === 16) {
    frameOffset = pixelDataOffset + frameIndex * pixelsPerFrame * 2;
    Iif (frameOffset >= dataSet.byteArray.length) {
      throw new Error('frame exceeds size of pixelData');
    }
 
    return new Uint8Array(
      dataSet.byteArray.buffer.slice(
        frameOffset,
        frameOffset + pixelsPerFrame * 2
      )
    );
  } else Eif (bitsAllocated === 1) {
    frameOffset = pixelDataOffset + frameIndex * pixelsPerFrame * 0.125;
    if (frameOffset >= dataSet.byteArray.length) {
      throw new Error('frame exceeds size of pixelData');
    }
 
    return unpackBinaryFrame(dataSet.byteArray, frameOffset, pixelsPerFrame);
  } else if (bitsAllocated === 32) {
    frameOffset = pixelDataOffset + frameIndex * pixelsPerFrame * 4;
    if (frameOffset >= dataSet.byteArray.length) {
      throw new Error('frame exceeds size of pixelData');
    }
 
    return new Uint8Array(
      dataSet.byteArray.buffer.slice(
        frameOffset,
        frameOffset + pixelsPerFrame * 4
      )
    );
  }
 
  throw new Error('unsupported pixel format');
}
 
export default getUncompressedImageFrame;