All files / nifti-volume-loader/src/helpers convert.ts

0% Statements 0/32
0% Branches 0/8
0% Functions 0/3
0% Lines 0/31

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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119                                                                                                                                                                                                                                             
import type { Types } from '@cornerstonejs/core';
import { parseAffineMatrix } from './affineUtilities';
 
/**
 * This converts scalar data: LPS to RAS and RAS to LPS
 */
const invertDataPerFrame = (dimensions, imageDataArray) => {
  let TypedArrayConstructor;
  let bytesPerVoxel;
 
  if (
    imageDataArray instanceof Uint8Array ||
    imageDataArray instanceof ArrayBuffer
  ) {
    TypedArrayConstructor = Uint8Array;
    bytesPerVoxel = 1;
  } else if (imageDataArray instanceof Int16Array) {
    TypedArrayConstructor = Int16Array;
    bytesPerVoxel = 2;
  } else if (imageDataArray instanceof Float32Array) {
    TypedArrayConstructor = Float32Array;
    bytesPerVoxel = 4;
  } else {
    throw new Error(
      'imageDataArray needs to be a Uint8Array, Int16Array or Float32Array.'
    );
  }
 
  // Make a copy of the data first using the browser native fast TypedArray.set().
  const newImageDataArray = new TypedArrayConstructor(
    imageDataArray.byteLength
  );
 
  const view = new TypedArrayConstructor(imageDataArray);
 
  newImageDataArray.set(view);
 
  // In order to switch from LP to RA within each slice, we just need to reverse each section.
  // We can do this in place using web api which is very fast, by taking views on different parts of a single buffer.
 
  const numFrames = dimensions[2];
  const frameLength = dimensions[0] * dimensions[1];
  const buffer = newImageDataArray.buffer;
 
  for (let frame = 0; frame < numFrames; frame++) {
    const byteOffset = frameLength * frame * bytesPerVoxel;
    // Get view of underlying buffer for this frame.
    const frameView = new TypedArrayConstructor(
      buffer,
      byteOffset,
      frameLength
    );
 
    frameView.reverse();
  }
 
  return newImageDataArray;
};
 
function rasToLps(niftiHeader) {
  const { affine } = niftiHeader;
 
  // RAS
  const { orientation, origin, spacing } = parseAffineMatrix(affine);
 
  // LPS
  const newOrigin = [-origin[0], -origin[1], origin[2]] as Types.Point3;
 
  // Change row-major to column-major for LPS orientation
  const newOrientation = [
    -orientation[0],
    -orientation[3],
    orientation[6],
    -orientation[1],
    -orientation[4],
    orientation[7],
    -orientation[2],
    -orientation[5],
    orientation[8],
  ];
 
  return {
    origin: newOrigin,
    orientation: newOrientation,
    spacing,
  };
}
 
function lpsToRas(header) {
  const { origin, orientation, spacing, dimensions, dataType } = header;
 
  const newOrigin = [-origin[0], -origin[1], origin[2]];
  const newOrientation = [
    -orientation[0],
    -orientation[3],
    orientation[6],
    -orientation[1],
    -orientation[4],
    orientation[7],
    -orientation[2],
    -orientation[5],
    orientation[8],
  ];
 
  return {
    orientation: newOrientation,
    origin: [newOrigin[0], newOrigin[1], newOrigin[2]],
    dataType,
    dimensions,
    spacing,
    slope: header.slope,
    inter: header.inter,
    max: header.max,
    min: header.min,
  };
}
 
export { lpsToRas, rasToLps, invertDataPerFrame };