All files / dicomImageLoader/src/imageLoader/wadouri loadImage.ts

53.44% Statements 31/58
46.66% Branches 7/15
53.84% Functions 7/13
53.44% Lines 31/58

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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231                              428x       180x                                                                 180x 180x         180x 180x   180x 180x 180x 180x             180x   180x   180x 180x 180x 180x   180x 180x 180x 180x           180x                                       180x                                                                                                             180x 180x                   180x   180x               180x     180x         180x                                         180x           180x                    
import type { DataSet } from 'dicom-parser';
import type { Types } from '@cornerstonejs/core';
import { Enums } from '@cornerstonejs/core';
import createImage from '../createImage';
import { xhrRequest } from '../internal/index';
import dataSetCacheManager from './dataSetCacheManager';
import type {
  LoadRequestFunction,
  DICOMLoaderIImage,
  DICOMLoaderImageOptions,
} from '../../types';
import getPixelData from './getPixelData';
import loadFileRequest from './loadFileRequest';
import parseImageId from './parseImageId';
 
const { ImageQualityStatus } = Enums;
 
// add a decache callback function to clear out our dataSetCacheManager
function addDecache(imageLoadObject: Types.IImageLoadObject, imageId: string) {
  imageLoadObject.decache = function () {
    // console.log('decache');
    const parsedImageId = parseImageId(imageId);
 
    dataSetCacheManager.unload(parsedImageId.url);
  };
}
 
/**
 * Given the dataSetPromise and imageId this will return a promise to be
 * resolved with an image object containing the loaded image.
 *
 * @param dataSetPromise - A promise that resolves to a DataSet object.
 * @param imageId - The imageId of the image to be loaded.
 * @param frame - The frame number to be loaded in case of multiframe. it should
 * be noted that this is used to extract the pixelData from dicomParser and
 * dicomParser is 0-based index (the first pixelData is frame 0); however,
 * in metadata and imageId frame is 1-based index (the first frame is frame 1).
 * @param sharedCacheKey -  A key to be used to cache the loaded image.
 * @param options - Options to be used when loading the image.
 * @param callbacks - Callbacks to be called when the image is loaded.
 * @returns An object containing a promise to be resolved with the loaded image
 */
function loadImageFromPromise(
  dataSetPromise: Promise<DataSet>,
  imageId: string,
  frame = 0,
  sharedCacheKey: string,
  options: DICOMLoaderImageOptions,
  callbacks?: {
    imageDoneCallback: (image: DICOMLoaderIImage) => void;
  }
): Types.IImageLoadObject {
  const start = new Date().getTime();
  const imageLoadObject: Types.IImageLoadObject = {
    cancelFn: undefined,
    promise: undefined,
  };
 
  imageLoadObject.promise = new Promise((resolve, reject) => {
    dataSetPromise.then(
      (dataSet /* , xhr*/) => {
        const pixelData = getPixelData(dataSet, frame);
        const transferSyntax = dataSet.string('x00020010');
        const loadEnd = new Date().getTime();
        const imagePromise = createImage(
          imageId,
          pixelData,
          transferSyntax,
          options
        );
 
        addDecache(imageLoadObject, imageId);
 
        imagePromise.then(
          (image) => {
            image = image as DICOMLoaderIImage;
            image.data = dataSet;
            image.sharedCacheKey = sharedCacheKey;
            const end = new Date().getTime();
 
            image.loadTimeInMS = loadEnd - start;
            image.totalTimeInMS = end - start;
            image.imageQualityStatus = ImageQualityStatus.FULL_RESOLUTION;
            Iif (
              callbacks !== undefined &&
              callbacks.imageDoneCallback !== undefined
            ) {
              callbacks.imageDoneCallback(image);
            }
            resolve(image);
          },
          function (error) {
            // Reject the error, and the dataSet
            reject({
              error,
              dataSet,
            });
          }
        );
      },
      function (error) {
        // Reject the error
        reject({
          error,
        });
      }
    );
  });
 
  return imageLoadObject;
}
 
function loadImageFromDataSet(
  dataSet,
  imageId: string,
  frame = 0,
  sharedCacheKey: string,
  options
): Types.IImageLoadObject {
  const start = new Date().getTime();
 
  const promise = new Promise<DICOMLoaderIImage | Types.IImageFrame>(
    (resolve, reject) => {
      const loadEnd = new Date().getTime();
 
      let imagePromise: Promise<DICOMLoaderIImage | Types.IImageFrame>;
 
      try {
        const pixelData = getPixelData(dataSet, frame);
        const transferSyntax = dataSet.string('x00020010');
 
        imagePromise = createImage(imageId, pixelData, transferSyntax, options);
      } catch (error) {
        // Reject the error, and the dataSet
        reject({
          error,
          dataSet,
        });
 
        return;
      }
 
      imagePromise.then((image) => {
        image = image as DICOMLoaderIImage;
 
        image.data = dataSet;
        // image.sharedCacheKey = sharedCacheKey;
        const end = new Date().getTime();
 
        image.loadTimeInMS = loadEnd - start;
        image.totalTimeInMS = end - start;
        image.imageQualityStatus = ImageQualityStatus.FULL_RESOLUTION;
        resolve(image);
      }, reject);
    }
  );
 
  return {
    promise: promise as Promise<Types.IImage>,
    cancelFn: undefined,
  };
}
 
function getLoaderForScheme(scheme: string): LoadRequestFunction {
  if (scheme === 'dicomweb' || scheme === 'wadouri') {
    return xhrRequest as LoadRequestFunction;
  } else Eif (scheme === 'dicomfile') {
    return loadFileRequest as LoadRequestFunction;
  }
}
 
function loadImage(
  imageId: string,
  options: DICOMLoaderImageOptions = {}
): Types.IImageLoadObject {
  const parsedImageId = parseImageId(imageId);
 
  options = Object.assign({}, options);
 
  // IMPORTANT: if you have a custom loader that you want to use for a specific
  // scheme, you should create your own loader and register it with the scheme
  // in the image loader, and NOT just pass it in as an option. This is because
  // the scheme is used to determine the loader to use and is more maintainable
 
  // The loader isn't transferable, so ensure it is deleted
  delete options.loader;
  // The options might have a loader above, but it is a loader into the cache,
  // so not the scheme loader, which is separate and defined by the scheme here
  const schemeLoader = getLoaderForScheme(parsedImageId.scheme);
 
  // if the dataset for this url is already loaded, use it, in case of multiframe
  // images, we need to extract the frame pixelData from the dataset although the
  // image is loaded
  Iif (dataSetCacheManager.isLoaded(parsedImageId.url)) {
    /**
     * @todo The arguments to the dataSetCacheManager below are incorrect.
     */
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const dataSet: DataSet = (dataSetCacheManager as any).get(
      parsedImageId.url,
      schemeLoader,
      imageId
    );
 
    return loadImageFromDataSet(
      dataSet,
      imageId,
      parsedImageId.pixelDataFrame,
      parsedImageId.url,
      options
    );
  }
 
  // load the dataSet via the dataSetCacheManager
  const dataSetPromise = dataSetCacheManager.load(
    parsedImageId.url,
    schemeLoader,
    imageId
  );
 
  return loadImageFromPromise(
    dataSetPromise,
    imageId,
    parsedImageId.frame,
    parsedImageId.url,
    options
  );
}
 
export { loadImageFromPromise, getLoaderForScheme, loadImage };