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

0% Statements 0/19
0% Branches 0/5
0% Functions 0/6
0% Lines 0/19

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                                                                                                                                                                                                               
import { getVolumeInfo, splitImageIdsBy4DTags } from './helpers';
import StreamingDynamicImageVolume from './StreamingDynamicImageVolume';
 
interface IVolumeLoader {
  promise: Promise<StreamingDynamicImageVolume>;
  cancel: () => void;
  decache: () => void;
}
 
function get4DVolumeInfo(imageIds: string[]) {
  const { imageIdsGroups, splittingTag } = splitImageIdsBy4DTags(imageIds);
  return {
    volumesInfo: imageIdsGroups.map((imageIds) => getVolumeInfo(imageIds)),
    splittingTag,
  };
}
 
/**
 * 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 StreamingDynamicImageVolume instance.
 *
 * In order to use the cornerstoneStreamingDynamicImageVolumeLoader 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 StreamingDynamicImageVolume
 */
function cornerstoneStreamingDynamicImageVolumeLoader(
  volumeId: string,
  options: {
    imageIds: string[];
  }
): IVolumeLoader {
  if (!options || !options.imageIds || !options.imageIds.length) {
    throw new Error(
      'ImageIds must be provided to create a 4D streaming image volume'
    );
  }
 
  const { imageIds } = options;
  const { volumesInfo, splittingTag } = get4DVolumeInfo(imageIds);
 
  const {
    metadata: volumeMetadata,
    dimensions,
    spacing,
    origin,
    direction,
    sizeInBytes,
  } = volumesInfo[0];
 
  const sortedImageIdsArrays = [];
  const scalarDataArrays = [];
 
  volumesInfo.forEach((volumeInfo) => {
    sortedImageIdsArrays.push(volumeInfo.sortedImageIds);
    scalarDataArrays.push(volumeInfo.scalarData);
  });
 
  const sortedImageIds = sortedImageIdsArrays.flat();
  let streamingImageVolume = new StreamingDynamicImageVolume(
    // ImageVolume properties
    {
      volumeId,
      metadata: volumeMetadata,
      dimensions,
      spacing,
      origin,
      direction,
      scalarData: scalarDataArrays,
      sizeInBytes,
      imageIds: sortedImageIds,
      splittingTag,
    },
    // Streaming properties
    {
      imageIds: sortedImageIds,
      loadStatus: {
        // todo: loading and loaded should be on ImageVolume
        loaded: false,
        loading: false,
        cancelled: false,
        cachedFrames: [],
        callbacks: [],
      },
    }
  );
 
  return {
    promise: Promise.resolve(streamingImageVolume),
    decache: () => {
      streamingImageVolume.destroy();
      streamingImageVolume = null;
    },
    cancel: () => {
      streamingImageVolume.cancelLoading();
    },
  };
}
 
export default cornerstoneStreamingDynamicImageVolumeLoader;