All files / packages/streaming-image-volume-loader/src cornerstoneStreamingImageVolumeLoader.ts

82.35% Statements 42/51
56.25% Branches 9/16
66.66% Functions 2/3
78.04% Lines 32/41

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            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 { cache, utilities, Enums } from '@cornerstonejs/core';
import type { Types } from '@cornerstonejs/core';
import { vec3 } from 'gl-matrix';
import { makeVolumeMetadata, sortImageIdsAndGetSpacing } from './helpers';
import StreamingImageVolume from './StreamingImageVolume';
 
const { createUint8SharedArray, createFloat32SharedArray } = utilities;
 
interface IVolumeLoader {
  promise: Promise<StreamingImageVolume>;
  cancel: () => void;
  decache: () => void;
}
 
/**
 * It handles loading of a image by streaming in its imageIds. It will be the
 * volume loader if the schema for the volumeID is `cornerstoneStreamingImageVolume`.
 * This function returns a promise that resolves to the StreamingImageVolume instance.
 *
 * In order to use the cornerstoneStreamingImageVolumeLoader you should use
 * createAndCacheVolume helper from the cornerstone-core volumeLoader module.
 *
 * @param volumeId - The ID of the volume
 * @param options - options for loading, imageIds
 * @returns a promise that resolves to a StreamingImageVolume
 */
function cornerstoneStreamingImageVolumeLoader(
  volumeId: string,
  options: {
    imageIds: string[];
  }
): IVolumeLoader {
  Iif (!options || !options.imageIds || !options.imageIds.length) {
    throw new Error(
      'ImageIds must be provided to create a streaming image volume'
    );
  }
 
  const { imageIds } = options;
 
  const volumeMetadata = makeVolumeMetadata(imageIds);
 
  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 = <Types.Point3>[PixelSpacing[1], PixelSpacing[0], zSpacing];
  const dimensions = <Types.Point3>[Columns, Rows, numFrames];
  const direction = [
    ...rowCosineVec,
    ...colCosineVec,
    ...scanAxisNormal,
  ] as Types.Mat3;
  const signed = PixelRepresentation === 1;
 
  // Check if it fits in the cache before we allocate data
  // TODO Improve this when we have support for more types
  // NOTE: We use 4 bytes per voxel as we are using Float32.
  const bytesPerVoxel = BitsAllocated === 16 ? 4 : 1;
  const sizeInBytesPerComponent =
    bytesPerVoxel * dimensions[0] * dimensions[1] * dimensions[2];
 
  let numComponents = 1;
  Iif (PhotometricInterpretation === 'RGB') {
    numComponents = 3;
  }
 
  const sizeInBytes = sizeInBytesPerComponent * numComponents;
 
  // check if there is enough space in unallocated + image Cache
  const isCacheable = cache.isCacheable(sizeInBytes);
  Iif (!isCacheable) {
    throw new Error(Enums.Events.CACHE_SIZE_EXCEEDED);
  }
 
  cache.decacheIfNecessaryUntilBytesAvailable(sizeInBytes);
 
  let scalarData;
 
  switch (BitsAllocated) {
    case 8:
      Iif (signed) {
        throw new Error(
          '8 Bit signed images are not yet supported by this plugin.'
        );
      } else {
        scalarData = createUint8SharedArray(
          dimensions[0] * dimensions[1] * dimensions[2]
        );
      }
 
      break;
 
    case 16:
      scalarData = createFloat32SharedArray(
        dimensions[0] * dimensions[1] * dimensions[2]
      );
 
      break;
 
    case 24:
      // hacky because we don't support alpha channel in dicom
      scalarData = createUint8SharedArray(
        dimensions[0] * dimensions[1] * dimensions[2] * numComponents
      );
 
      break;
  }
 
  let streamingImageVolume = new StreamingImageVolume(
    // ImageVolume properties
    {
      volumeId,
      metadata: volumeMetadata,
      dimensions,
      spacing,
      origin,
      direction,
      scalarData,
      sizeInBytes,
    },
    // Streaming properties
    {
      imageIds: sortedImageIds,
      loadStatus: {
        // todo: loading and loaded should be on ImageVolume
        loaded: false,
        loading: false,
        cachedFrames: [],
        callbacks: [],
      },
    }
  );
 
  return {
    promise: Promise.resolve(streamingImageVolume),
    decache: () => {
      streamingImageVolume.vtkOpenGLTexture.delete();
      streamingImageVolume.scalarData = null;
      streamingImageVolume = null;
    },
    cancel: () => {
      streamingImageVolume.cancelLoading();
    },
  };
}
 
export default cornerstoneStreamingImageVolumeLoader;