All files / packages/core/src/utilities generateVolumePropsFromImageIds.ts

54.09% Statements 33/61
21.42% Branches 9/42
100% Functions 2/2
54.09% Lines 33/61

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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187                                              1x   1x 1x               1x 1x 1x   1x         1x 1x                   1x   1x         1x           1x   1x   1x         1x     1x 1x 1x         1x 1x 1x 1x 1x 1x     1x       1x   1x         1x 1x 1x     1x                                                                                                                           1x                            
import { vec3 } from 'gl-matrix';
import {
  canRenderFloatTextures,
  getConfiguration,
  getShouldUseSharedArrayBuffer,
} from '../init';
import createFloat32SharedArray from './createFloat32SharedArray';
import createInt16SharedArray from './createInt16SharedArray';
import createUint16SharedArray from './createUInt16SharedArray';
import createUint8SharedArray from './createUint8SharedArray';
import getScalingParameters from './getScalingParameters';
import makeVolumeMetadata from './makeVolumeMetadata';
import sortImageIdsAndGetSpacing from './sortImageIdsAndGetSpacing';
import { hasFloatScalingParameters } from './hasFloatScalingParameters';
import { ImageVolumeProps, Mat3, Point3 } from '../types';
import cache from '../cache';
import { Events } from '../enums';
 
function generateVolumePropsFromImageIds(
  imageIds: string[],
  volumeId: string
): ImageVolumeProps {
  const { useNorm16Texture, preferSizeOverAccuracy } =
    getConfiguration().rendering;
 
  const use16BitDataType = useNorm16Texture || preferSizeOverAccuracy;
  const volumeMetadata = makeVolumeMetadata(imageIds);
 
  // For a streaming volume, the data type cannot rely on CSWIL to load
  // the proper array buffer type. This is because the target buffer container
  // must be decided ahead of time.
  // TODO: move this logic into CSWIL to avoid logic duplication.
  // We check if scaling parameters are negative we choose Int16 instead of
  // Uint16 for cases where BitsAllocated is 16.
  const imageIdIndex = Math.floor(imageIds.length / 2);
  const imageId = imageIds[imageIdIndex];
  const scalingParameters = getScalingParameters(imageId);
  const hasNegativeRescale =
    scalingParameters.rescaleIntercept < 0 ||
    scalingParameters.rescaleSlope < 0;
 
  // The prescale is ALWAYS used with modality LUT, so we can assume that
  // if the rescale slope is not an integer, we need to use Float32
  const floatAfterScale = hasFloatScalingParameters(scalingParameters);
  const canRenderFloat = canRenderFloatTextures();
 
  const {
    BitsAllocated,
    PixelRepresentation,
    PhotometricInterpretation,
    ImageOrientationPatient,
    PixelSpacing,
    Columns,
    Rows,
  } = volumeMetadata;
 
  const rowCosineVec = vec3.fromValues(
    ImageOrientationPatient[0],
    ImageOrientationPatient[1],
    ImageOrientationPatient[2]
  );
  const colCosineVec = vec3.fromValues(
    ImageOrientationPatient[3],
    ImageOrientationPatient[4],
    ImageOrientationPatient[5]
  );
 
  const scanAxisNormal = vec3.create();
 
  vec3.cross(scanAxisNormal, rowCosineVec, colCosineVec);
 
  const { zSpacing, origin, sortedImageIds } = sortImageIdsAndGetSpacing(
    imageIds,
    scanAxisNormal
  );
 
  const numFrames = imageIds.length;
 
  // Spacing goes [1] then [0], as [1] is column spacing (x) and [0] is row spacing (y)
  const spacing = <Point3>[PixelSpacing[1], PixelSpacing[0], zSpacing];
  const dimensions = <Point3>[Columns, Rows, numFrames];
  const direction = [
    ...rowCosineVec,
    ...colCosineVec,
    ...scanAxisNormal,
  ] as Mat3;
  const signed = PixelRepresentation === 1;
  const numComponents = PhotometricInterpretation === 'RGB' ? 3 : 1;
  const useSharedArrayBuffer = getShouldUseSharedArrayBuffer();
  const length = dimensions[0] * dimensions[1] * dimensions[2];
  const handleCache = (sizeInBytes) => {
    Iif (!cache.isCacheable(sizeInBytes)) {
      throw new Error(Events.CACHE_SIZE_EXCEEDED);
    }
    cache.decacheIfNecessaryUntilBytesAvailable(sizeInBytes);
  };
 
  let scalarData, sizeInBytes;
  switch (BitsAllocated) {
    case 8:
      Iif (signed) {
        throw new Error(
          '8 Bit signed images are not yet supported by this plugin.'
        );
      }
      sizeInBytes = length * numComponents;
      handleCache(sizeInBytes);
      scalarData = useSharedArrayBuffer
        ? createUint8SharedArray(length * numComponents)
        : new Uint8Array(length * numComponents);
      break;
 
    case 16:
      // Temporary fix for 16 bit images to use Float32
      // until the new dicom image loader handler the conversion
      // correctly
      if (!use16BitDataType || (canRenderFloat && floatAfterScale)) {
        sizeInBytes = length * 4;
        scalarData = useSharedArrayBuffer
          ? createFloat32SharedArray(length)
          : new Float32Array(length);
 
        break;
      }
 
      sizeInBytes = length * 2;
      if (signed || hasNegativeRescale) {
        handleCache(sizeInBytes);
        scalarData = useSharedArrayBuffer
          ? createInt16SharedArray(length)
          : new Int16Array(length);
        break;
      }
 
      if (!signed && !hasNegativeRescale) {
        handleCache(sizeInBytes);
        scalarData = useSharedArrayBuffer
          ? createUint16SharedArray(length)
          : new Uint16Array(length);
        break;
      }
 
      // Default to Float32 again
      sizeInBytes = length * 4;
      handleCache(sizeInBytes);
      scalarData = useSharedArrayBuffer
        ? createFloat32SharedArray(length)
        : new Float32Array(length);
      break;
 
    case 24:
      sizeInBytes = length * numComponents;
      handleCache(sizeInBytes);
 
      // hacky because we don't support alpha channel in dicom
      scalarData = useSharedArrayBuffer
        ? createUint8SharedArray(length * numComponents)
        : new Uint8Array(length * numComponents);
      break;
    case 32:
      sizeInBytes = length * 4;
      handleCache(sizeInBytes);
      scalarData = useSharedArrayBuffer
        ? createFloat32SharedArray(length)
        : new Float32Array(length);
      break;
    default:
      throw new Error(
        `Bits allocated of ${BitsAllocated} is not defined to generate scalarData for the volume.`
      );
  }
 
  return {
    dimensions,
    spacing,
    origin,
    direction,
    scalarData,
    sizeInBytes,
    metadata: volumeMetadata,
    imageIds: sortedImageIds,
    volumeId,
  };
}
 
export { generateVolumePropsFromImageIds };