All files / utils/test testUtilsImageLoader.js

91.89% Statements 34/37
91.17% Branches 31/34
100% Functions 5/5
91.42% Lines 32/35

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                                                1x 91x   731x       91x 3x   88x       91x                             215x               91x                                               3116x       3116x     3116x         3116x   24990x   3116x 3116x 3116x 95x                     95x 3021x 103x     103x 2918x 127x         127x 2791x 2460x                           2460x 331x 1x       330x 6x                
import {
  getVerticalBarImage,
  getVerticalBarRGBImage,
} from './testUtilsPixelData';
 
/**
 * It creates an image based on the imageId name for testing purposes. It splits the imageId
 * based on "_" and deciphers each field of scheme, rows, columns, barStart, barWidth, x_spacing, y_spacing, rgb, and PT.
 * fakeLoader: myImage_64_64_10_20_1_1_0 will load a grayscale test image of size 64 by
 * 64 and with a vertical bar which starts at 10th pixel and span 20 pixels
 * width, with pixel spacing of 1 mm and 1 mm in x and y direction.
 *
 * fakeImageLoader should be registered for each test image:
 *
 * @example
 * ```javascript
 * imageLoader.registerImageLoader('fakeImageLoader', imageLoader)
 * ```
 *
 * then you can use imageId like: 'fakeImageLoader: myImage_64_64_10_20_1_1_0'
 *
 * @param {imageId} imageId
 * @returns Promise that resolves to the image
 */
const fakeImageLoader = (imageId) => {
  const imageURI = imageId.split(':')[1];
  const [_, rows, columns, barStart, barWidth, x_spacing, y_spacing, rgb, PT] =
    imageURI.split('_').map((v) => parseFloat(v));
 
  let pixelData;
 
  if (rgb) {
    pixelData = getVerticalBarRGBImage(rows, columns, barStart, barWidth);
  } else {
    pixelData = getVerticalBarImage(rows, columns, barStart, barWidth);
  }
 
  // Todo: separated fakeImageLoader for cpu and gpu
  const image = {
    rows,
    columns,
    width: columns,
    height: rows,
    imageId,
    intercept: 0,
    slope: 1,
    invert: false,
    windowCenter: 40,
    windowWidth: 400,
    maxPixelValue: 255,
    minPixelValue: 0,
    rowPixelSpacing: y_spacing,
    columnPixelSpacing: x_spacing,
    getPixelData: () => pixelData,
    sizeInBytes: rows * columns * 1, // 1 byte for now
    FrameOfReferenceUID: 'Stack_Frame_Of_Reference',
    imageFrame: {
      photometricInterpretation: rgb ? 'RGB' : 'MONOCHROME2',
    },
  };
 
  return {
    promise: Promise.resolve(image),
  };
};
 
/**
 * Returns the requested metadata for the imageId
 *
 * Note: fakeMetadataLoader should be added as a provider for each test
 *
 * ```javascript
 * metaData.addProvider(fakeMetaDataProvider, 10000)
 * ```
 *
 *
 *
 * @param {string} type - metadata type
 * @param {string} imageId - the imageId
 * @returns metadata based on the imageId and type
 */
function fakeMetaDataProvider(type, imageId) {
  // don't try to provide incorrect information for derived
  // images, as it will cause errors, rather let the rest of providers
  // handle it
  Iif (imageId.startsWith('derived')) {
    return;
  }
 
  Iif (Array.isArray(imageId)) {
    return;
  }
  Iif (typeof imageId !== 'string') {
    throw new Error(
      `Expected imageId to be of type string, but received ${imageId}`
    );
  }
  const imageURI = imageId.split(':')[1];
  const [_, rows, columns, barStart, barWidth, x_spacing, y_spacing, rgb, PT] =
    imageURI.split('_').map((v) => parseFloat(v));
 
  const modality = PT ? 'PT' : 'MR';
  const photometricInterpretation = rgb ? 'RGB' : 'MONOCHROME2';
  if (type === 'imagePixelModule') {
    const imagePixelModule = {
      photometricInterpretation,
      rows,
      columns,
      samplesPerPixel: rgb ? 3 : 1,
      bitsAllocated: rgb ? 24 : 8,
      bitsStored: rgb ? 24 : 8,
      highBit: rgb ? 24 : 8,
      pixelRepresentation: 0,
    };
 
    return imagePixelModule;
  } else if (type === 'generalSeriesModule') {
    const generalSeriesModule = {
      modality: modality,
    };
    return generalSeriesModule;
  } else if (type === 'scalingModule') {
    const scalingModule = {
      suvbw: 100,
      suvlbm: 100,
      suvbsa: 100,
    };
    return scalingModule;
  } else if (type === 'imagePlaneModule') {
    const imagePlaneModule = {
      rows,
      columns,
      width: rows,
      height: columns,
      imageOrientationPatient: [1, 0, 0, 0, 1, 0],
      rowCosines: [1, 0, 0],
      columnCosines: [0, 1, 0],
      imagePositionPatient: [0, 0, 0],
      pixelSpacing: [x_spacing, y_spacing],
      rowPixelSpacing: y_spacing,
      columnPixelSpacing: x_spacing,
    };
 
    return imagePlaneModule;
  } else if (type === 'voiLutModule') {
    return {
      windowWidth: undefined,
      windowCenter: undefined,
    };
  } else if (type === 'modalityLutModule') {
    return {
      rescaleSlope: undefined,
      rescaleIntercept: undefined,
    };
  }
}
 
export { fakeImageLoader, fakeMetaDataProvider };