All files / extensions/cornerstone-dicom-seg/src/utils segmentationConfig.ts

43.75% Statements 7/16
8.33% Branches 1/12
25% Functions 1/4
43.75% Lines 7/16

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 101178x 178x   178x   178x                                                                                                             24x       24x 24x                                                                      
export const LABELMAP_SEG_SOP_CLASS_UID = '1.2.840.10008.5.1.4.1.1.66.7';
export const BITMAP_SEG_SOP_CLASS_UID = '1.2.840.10008.5.1.4.1.1.66.4';
/** RLE Lossless — OHIF default SEG store transfer syntax. */
export const DEFAULT_SEG_STORE_TRANSFER_SYNTAX_UID = '1.2.840.10008.1.2.5';
/** OHIF default SEG store mode (Label Map Segmentation SOP Class). */
export const DEFAULT_SEG_STORE_MODE = 'labelmap' as const;
 
export type SegmentationMode = 'labelmap' | 'bitmap';
 
/**
 * Per-data-source overrides for the SEG store defaults. A data source may set
 * these under `configuration.segmentation.store` to override the values coming
 * from the `segmentation.store.*` customizations. Different back ends support
 * different SEG encodings, so the data source is allowed to win over the
 * app-wide customization default.
 */
export type SegmentationStoreOverride = {
  defaultMode?: SegmentationMode;
  transferSyntaxUID?: string;
};
 
type SegmentationCustomizationReader = {
  getCustomization: (customizationId: string) => unknown;
};
 
function getStoreDefaultMode(
  customizationService?: SegmentationCustomizationReader,
  override?: SegmentationStoreOverride
): SegmentationMode {
  const mode =
    override?.defaultMode ??
    (customizationService?.getCustomization('segmentation.store.defaultMode') as
      | SegmentationMode
      | undefined) ??
    DEFAULT_SEG_STORE_MODE;
 
  return mode === 'bitmap' ? 'bitmap' : 'labelmap';
}
 
function getStoreTransferSyntaxUID(
  customizationService?: SegmentationCustomizationReader,
  override?: SegmentationStoreOverride
): string {
  return (
    override?.transferSyntaxUID ??
    (customizationService?.getCustomization(
      'segmentation.store.transferSyntaxUID'
    ) as string | undefined) ??
    DEFAULT_SEG_STORE_TRANSFER_SYNTAX_UID
  );
}
 
/**
 * Resolves the parser type for loading a DICOM SEG instance.
 * Uses the instance SOP Class UID when it is a known SEG class; otherwise falls back to store defaultMode.
 */
export function getSegmentationParserType(
  sopClassUID: string | undefined,
  customizationService?: SegmentationCustomizationReader
): SegmentationMode {
  Iif (sopClassUID === LABELMAP_SEG_SOP_CLASS_UID) {
    return 'labelmap';
  }
 
  if (sopClassUID === BITMAP_SEG_SOP_CLASS_UID) {
    return 'bitmap';
  }
 
  return getStoreDefaultMode(customizationService);
}
 
/**
 * Options passed to @cornerstonejs/adapters generateSegmentation when exporting or storing SEG.
 *
 * Defaults to **Label Map + RLE Lossless**. Customizations (or per-data-source
 * `configuration.segmentation.store`) are only needed to opt into bitmap and/or
 * uncompressed Explicit VR Little Endian.
 */
export function getSegmentationSaveOptions(
  customizationService?: SegmentationCustomizationReader,
  override?: SegmentationStoreOverride
): {
  sopClassUID: string;
  transferSyntaxUID: string;
  transferSyntaxUid: string;
} {
  const defaultMode = getStoreDefaultMode(customizationService, override);
  const sopClassUID =
    defaultMode === 'bitmap' ? BITMAP_SEG_SOP_CLASS_UID : LABELMAP_SEG_SOP_CLASS_UID;
  const transferSyntaxUID = getStoreTransferSyntaxUID(
    customizationService,
    override
  );
 
  return {
    sopClassUID,
    transferSyntaxUID,
    transferSyntaxUid: transferSyntaxUID,
  };
}