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

68.18% Statements 45/66
35.41% Branches 17/48
85.71% Functions 6/7
68.18% Lines 45/66

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 232 233 234 235 236 237 238 239                      428x 428x 428x                               9454x 9454x         9454x 9454x   9454x   18908x   18908x 9454x     9454x   9454x           9454x                         9454x 9454x                                   9454x                                                                     428x           9454x   9454x   9454x       9454x   9454x     9454x 9454x             9454x 9454x     9454x       9454x             9454x         9454x 9454x       9454x               9278x   9278x 9278x 9278x   9278x 9278x                       9454x 9454x 9454x 9454x   9454x             9454x                                                    
import {
  Enums,
  imageRetrievalPoolManager,
  utilities,
} from '@cornerstonejs/core';
import { Enums as csCoreEnums, type Types } from '@cornerstonejs/core';
 
import createImage from '../createImage';
import getPixelData from './getPixelData';
import type { DICOMLoaderIImage, DICOMLoaderImageOptions } from '../../types';
 
const { ProgressiveIterator } = utilities;
const { ImageQualityStatus } = Enums;
const streamableTransferSyntaxes = new Set<string>([
  // Private HTJ2K
  '3.2.840.10008.1.2.4.96',
  // Released HTJ2K - only the RPCL one is definitely streamable.
  '1.2.840.10008.1.2.4.202',
  // HTJ2K lossy might be streamable, so try it.  If it fails it is ok as it will
  // proceed and eventually work.
  '1.2.840.10008.1.2.4.203',
]);
 
/**
 * Helper method to extract the transfer-syntax from the response of the server.
 * @param {string} contentType The value of the content-type header as returned by the WADO-RS server.
 * @return The transfer-syntax as announced by the server, or Implicit Little Endian by default.
 */
export function getTransferSyntaxForContentType(contentType: string): string {
  const defaultTransferSyntax = '1.2.840.10008.1.2'; // Default is Implicit Little Endian.
  Iif (!contentType) {
    return defaultTransferSyntax;
  }
 
  // Browse through the content type parameters
  const parameters = contentType.split(';');
  const params: Record<string, string> = {};
 
  parameters.forEach((parameter) => {
    // Look for a transfer-syntax=XXXX pair
    const parameterValues = parameter.split('=');
 
    if (parameterValues.length !== 2) {
      return;
    }
 
    const value = parameterValues[1].trim().replace(/"/g, '');
 
    params[parameterValues[0].trim()] = value;
  });
 
  // This is useful if the PACS doesn't respond with a syntax
  // in the content type.
  // http://dicom.nema.org/medical/dicom/current/output/chtml/part18/chapter_6.html#table_6.1.1.8-3b
  const defaultTransferSyntaxByType = {
    'image/jpeg': '1.2.840.10008.1.2.4.50',
    'image/x-dicom-rle': '1.2.840.10008.1.2.5',
    'image/x-jls': '1.2.840.10008.1.2.4.80',
    'image/jls': '1.2.840.10008.1.2.4.80',
    'image/jll': '1.2.840.10008.1.2.4.70',
    'image/jp2': '1.2.840.10008.1.2.4.90',
    'image/jpx': '1.2.840.10008.1.2.4.92',
    // Temporary types, until ratified by DICOM committed - TODO
    'image/jphc': '3.2.840.10008.1.2.4.96',
    'image/jxl': '1.2.840.10008.1.2.4.140',
  };
 
  if (params['transfer-syntax']) {
    return params['transfer-syntax'];
  } else Eif (
    contentType &&
    !Object.keys(params).length &&
    defaultTransferSyntaxByType[contentType]
  ) {
    // dcm4che seems to be reporting the content type as just 'image/jp2'?
    return defaultTransferSyntaxByType[contentType];
  } else if (params.type && defaultTransferSyntaxByType[params.type]) {
    return defaultTransferSyntaxByType[params.type];
  } else if (defaultTransferSyntaxByType[contentType]) {
    return defaultTransferSyntaxByType[contentType];
  }
 
  return defaultTransferSyntax;
}
 
function getImageRetrievalPool() {
  return imageRetrievalPoolManager;
}
 
export interface StreamingData {
  url: string;
  encodedData?: Uint8Array;
  // Some values used by instances of streaming data for range
  totalBytes?: number;
  chunkSize?: number;
  totalRanges?: number;
  rangesFetched?: number;
}
 
export interface CornerstoneWadoRsLoaderOptions
  extends DICOMLoaderImageOptions {
  requestType?: csCoreEnums.RequestType;
  additionalDetails?: {
    imageId: string;
  };
  priority?: number;
  addToBeginning?: boolean;
  retrieveType?: string;
  transferSyntaxUID?: string;
  // Retrieve options are stored to provide sub-options for nested calls
  retrieveOptions?: Types.RangeRetrieveOptions;
  // Streaming data adds information about already streamed results.
  streamingData?: StreamingData;
}
 
// TODO: load bulk data items that we might need
 
// Uncomment this on to test jpegls codec in OHIF
// const mediaType = 'multipart/related; type="image/x-jls"';
// const mediaType = 'multipart/related; type="application/octet-stream"; transfer-syntax="image/x-jls"';
const mediaType =
  'multipart/related; type=application/octet-stream; transfer-syntax=*';
 
function loadImage(
  imageId: string,
  options: CornerstoneWadoRsLoaderOptions = {}
): Types.IImageLoadObject {
  const imageRetrievalPool = getImageRetrievalPool();
 
  const start = new Date().getTime();
 
  const uncompressedIterator = new ProgressiveIterator<DICOMLoaderIImage>(
    'decompress'
  );
  async function sendXHR(imageURI: string, imageId: string, mediaType: string) {
    uncompressedIterator.generate(async (it) => {
      // get the pixel data from the server
      const compressedIt = ProgressiveIterator.as(
        getPixelData(imageURI, imageId, mediaType, options)
      );
      let lastDecodeLevel = 10;
      for await (const result of compressedIt) {
        const {
          pixelData,
          imageQualityStatus = ImageQualityStatus.FULL_RESOLUTION,
          percentComplete,
          done = true,
          extractDone = true,
        } = result;
        const transferSyntax = getTransferSyntaxForContentType(
          result.contentType
        );
        Iif (!extractDone && !streamableTransferSyntaxes.has(transferSyntax)) {
          continue;
        }
        const decodeLevel =
          result.decodeLevel ??
          (imageQualityStatus === ImageQualityStatus.FULL_RESOLUTION
            ? 0
            : decodeLevelFromComplete(
                percentComplete,
                options.retrieveOptions?.decodeLevel
              ));
        Iif (!done && lastDecodeLevel <= decodeLevel) {
          // No point trying again yet
          continue;
        }
 
        try {
          const useOptions = {
            ...options,
            decodeLevel,
          };
          const image = (await createImage(
            imageId,
            pixelData,
            transferSyntax,
            useOptions
          )) as DICOMLoaderIImage;
 
          // add the loadTimeInMS property
          const end = new Date().getTime();
 
          image.loadTimeInMS = end - start;
          image.transferSyntaxUID = transferSyntax;
          image.imageQualityStatus = imageQualityStatus;
          // The iteration is done even if the image itself isn't done yet
          it.add(image, done);
          lastDecodeLevel = decodeLevel;
        } catch (e) {
          if (extractDone) {
            console.warn("Couldn't decode", e);
            throw e;
          }
        }
      }
    });
  }
 
  const requestType =
    options.requestType || csCoreEnums.RequestType.Interaction;
  const additionalDetails = options.additionalDetails || { imageId };
  const priority = options.priority === undefined ? 5 : options.priority;
  const uri = imageId.substring(7);
 
  imageRetrievalPool.addRequest(
    sendXHR.bind(this, uri, imageId, mediaType),
    requestType,
    additionalDetails,
    priority
  );
 
  return {
    promise: uncompressedIterator.getDonePromise(),
    cancelFn: undefined,
  };
}
 
/** The decode level is based on how much of hte data is needed for
 * each level.  It is a square function, so
 * level 4 only needs 1/25 of the data (eg (4+1)^2).  Add 2% to ensure
 * there is enough space
 */
function decodeLevelFromComplete(percent: number, retrieveDecodeLevel = 4) {
  const testSize = percent / 100 - 0.02;
  if (testSize > 1 / 4) {
    return Math.min(retrieveDecodeLevel, 0);
  }
  if (testSize > 1 / 16) {
    return Math.min(retrieveDecodeLevel, 1);
  }
  if (testSize > 1 / 64) {
    return Math.min(retrieveDecodeLevel, 2);
  }
  return Math.min(retrieveDecodeLevel, 3);
}
 
export default loadImage;