All files / core/src/utilities buildMetadata.ts

74.28% Statements 26/35
65% Branches 13/20
75% Functions 3/4
74.28% Lines 26/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 160 161 162 163 164 165 166 167 168 169 170 171 172 173                                                                    544x         544x                     1704x   1704x       1704x 348x     1704x 348x     1704x 348x     1704x 348x     1704x 348x     1704x 348x         1704x                                                                                                 544x                 544x   544x   544x 544x 544x   544x   544x   544x                                        
import * as metaData from '../metaData';
import { MetadataModules, VOILUTFunctionType } from '../enums';
import type IImage from '../types/IImage';
import type { ImagePlaneModule, ImagePixelModule } from '../types';
import type IImageCalibration from '../types/IImageCalibration';
 
export interface BuildMetadataResult {
  scalingFactor: number;
  imagePlaneModule: ImagePlaneModule;
  voiLUTFunction: VOILUTFunctionType;
  modality: string;
  calibration: IImageCalibration;
  imagePixelModule: {
    bitsAllocated: number;
    bitsStored: number;
    samplesPerPixel: number;
    highBit: number;
    photometricInterpretation: string;
    pixelRepresentation: number;
    windowWidth: number;
    windowCenter: number;
    modality: string;
    voiLUTFunction: VOILUTFunctionType;
  };
}
 
/**
 * Gets a valid VOI LUT function from the provided value, defaulting to LINEAR if invalid
 * @param voiLUTFunction - The VOI LUT function to validate
 * @returns A valid VOI LUT function
 */
export function getValidVOILUTFunction(
  voiLUTFunction: VOILUTFunctionType | unknown
): VOILUTFunctionType {
  Eif (
    !Object.values(VOILUTFunctionType).includes(
      voiLUTFunction as VOILUTFunctionType
    )
  ) {
    return VOILUTFunctionType.LINEAR;
  }
  return voiLUTFunction as VOILUTFunctionType;
}
 
/**
 * Creates default values for imagePlaneModule if values are undefined
 * @param imageId - The image ID to get the plane module for
 * @returns The image plane module with default values if needed
 */
export function getImagePlaneModule(imageId: string): ImagePlaneModule {
  const imagePlaneModule = metaData.get(MetadataModules.IMAGE_PLANE, imageId);
 
  const newImagePlaneModule: ImagePlaneModule = {
    ...imagePlaneModule,
  };
 
  if (!newImagePlaneModule.columnPixelSpacing) {
    newImagePlaneModule.columnPixelSpacing = 1;
  }
 
  if (!newImagePlaneModule.rowPixelSpacing) {
    newImagePlaneModule.rowPixelSpacing = 1;
  }
 
  if (!newImagePlaneModule.columnCosines) {
    newImagePlaneModule.columnCosines = [0, 1, 0];
  }
 
  if (!newImagePlaneModule.rowCosines) {
    newImagePlaneModule.rowCosines = [1, 0, 0];
  }
 
  if (!newImagePlaneModule.imagePositionPatient) {
    newImagePlaneModule.imagePositionPatient = [0, 0, 0];
  }
 
  if (!newImagePlaneModule.imageOrientationPatient) {
    newImagePlaneModule.imageOrientationPatient = new Float32Array([
      1, 0, 0, 0, 1, 0,
    ]);
  }
 
  return newImagePlaneModule;
}
 
/**
 * Calibrates the image plane module if necessary
 * @param imageId - The image ID
 * @param imagePlaneModule - The image plane module to calibrate
 * @param currentCalibration - The current calibration
 * @returns The calibrated image plane module and calibration event if updated
 */
export function calibrateImagePlaneModule(
  imageId: string,
  imagePlaneModule: ImagePlaneModule,
  currentCalibration: IImageCalibration
): {
  imagePlaneModule: ImagePlaneModule;
  hasPixelSpacing: boolean;
  calibrationEvent?: {
    scale: number;
    calibration: IImageCalibration;
  };
} {
  const calibration = metaData.get('calibratedPixelSpacing', imageId);
  const isUpdated = currentCalibration !== calibration;
  const { scale } = calibration || {};
  const hasPixelSpacing = scale > 0 || imagePlaneModule.rowPixelSpacing > 0;
  imagePlaneModule.calibration = calibration;
 
  if (!isUpdated) {
    return { imagePlaneModule, hasPixelSpacing };
  }
 
  return {
    imagePlaneModule,
    hasPixelSpacing,
    calibrationEvent: {
      scale,
      calibration,
    },
  };
}
 
/**
 * Builds metadata for an image, including image plane and pixel modules
 * @param image - The image to build metadata for
 * @param options - Options for building metadata
 * @returns The built metadata
 */
export function buildMetadata(image: IImage): BuildMetadataResult {
  const imageId = image.imageId;
 
  const {
    pixelRepresentation,
    bitsAllocated,
    bitsStored,
    highBit,
    photometricInterpretation,
    samplesPerPixel,
  } = metaData.get('imagePixelModule', imageId);
 
  const { windowWidth, windowCenter, voiLUTFunction } = image;
 
  const { modality } = metaData.get('generalSeriesModule', imageId);
  const imageIdScalingFactor = metaData.get('scalingModule', imageId);
  const calibration = metaData.get(MetadataModules.CALIBRATION, imageId);
 
  const voiLUTFunctionEnum = getValidVOILUTFunction(voiLUTFunction);
 
  const imagePlaneModule = getImagePlaneModule(imageId);
 
  return {
    calibration,
    scalingFactor: imageIdScalingFactor,
    voiLUTFunction: voiLUTFunctionEnum,
    modality,
    imagePlaneModule,
    imagePixelModule: {
      bitsAllocated,
      bitsStored,
      samplesPerPixel,
      highBit,
      photometricInterpretation,
      pixelRepresentation,
      windowWidth: windowWidth as number,
      windowCenter: windowCenter as number,
      modality,
      voiLUTFunction: voiLUTFunctionEnum,
    },
  };
}