All files / dicomImageLoader/src/shared/decoders getPixelData.ts

0% Statements 0/8
0% Branches 0/8
0% Functions 0/1
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 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                                                                                                                                     
import type { ByteArray } from 'dicom-parser';
import isSignedPixelData from './isSignedPixelData';
 
interface DecodedFrameInfo {
  bitsPerSample?: number;
  isSigned?: boolean | number;
  componentCount?: number;
}
 
/**
 * Wraps a codec's decoded output in the typed array its samples should be read
 * as.
 *
 * Every WASM codec decodes into a flat byte buffer and then has to pick a view
 * over it, and they all pick it the same way: 16 bit samples when bitsPerSample
 * is above 8, 8 bit otherwise, signed or unsigned per {@link isSignedPixelData}.
 * This lived as four separate copies - JPEG 2000, HTJ2K, JPEG-LS and JPEG
 * baseline - which is how the signed color bug got fixed in one of them while
 * the other three kept rendering the same file wrong.
 *
 * @param frameInfo - decoded frame info, as the codecs report it
 * @param decodedBuffer - the codec's decoded output buffer
 * @param signedOverride - signedness taken from DICOM metadata, for codecs whose
 *   frame info carries no signed flag of its own (JPEG-LS). When undefined the
 *   flag on frameInfo is used instead.
 */
export default function getPixelData(
  frameInfo: DecodedFrameInfo,
  decodedBuffer: ByteArray,
  signedOverride?: boolean
) {
  const signed = isSignedPixelData({
    isSigned: signedOverride ?? frameInfo?.isSigned,
    componentCount: frameInfo?.componentCount,
  });
 
  if (frameInfo?.bitsPerSample > 8) {
    if (signed) {
      return new Int16Array(
        decodedBuffer.buffer,
        decodedBuffer.byteOffset,
        decodedBuffer.byteLength / 2
      );
    }
 
    return new Uint16Array(
      decodedBuffer.buffer,
      decodedBuffer.byteOffset,
      decodedBuffer.byteLength / 2
    );
  }
 
  if (signed) {
    return new Int8Array(
      decodedBuffer.buffer,
      decodedBuffer.byteOffset,
      decodedBuffer.byteLength
    );
  }
 
  return new Uint8Array(
    decodedBuffer.buffer,
    decodedBuffer.byteOffset,
    decodedBuffer.byteLength
  );
}