All files / extensions/cornerstone/src/utils segmentationExportUtils.ts

85.29% Statements 29/34
45.45% Branches 5/11
100% Functions 3/3
84.84% Lines 28/33

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                        67x       690x 176x     514x 514x       514x 9820x 9820x       9820x 2196360388x 498x         514x 16x     498x   498x     498x 498x   498x       498x 498x 498x         498x                     67x   192x 16x     176x   176x       176x    
import { cache, metaData } from '@cornerstonejs/core';
import { Types as cstTypes } from '@cornerstonejs/tools';
 
/**
 * Checks if a labelmap segmentation has exportable data
 * A label map is exportable if it has any pixel data and
 * it is referenced by a display set that is reconstructable.
 *
 * @param labelmap - The labelmap representation data
 * @param displaySetService - The display set service instance
 * @returns boolean - Whether the labelmap has exportable data
 */
export const hasExportableLabelMapData = (
  labelmap: cstTypes.LabelmapSegmentationData | undefined,
  displaySetService: any
): boolean => {
  if (!labelmap) {
    return false;
  }
 
  const imageIds = labelmap?.imageIds;
  Iif (!imageIds?.length) {
    return false;
  }
 
  const hasPixelData = imageIds.some(imageId => {
    const pixelData = cache.getImage(imageId)?.getPixelData();
    Iif (!pixelData) {
      return false;
    }
 
    for (let i = 0; i < pixelData.length; i++) {
      if (pixelData[i] !== 0) {
        return true;
      }
    }
  });
 
  if (!hasPixelData) {
    return false;
  }
 
  const referencedImageIds = labelmap?.referencedImageIds;
 
  Iif (!referencedImageIds) {
    return false;
  }
  const firstImageId = referencedImageIds[0];
  const instance = metaData.get('instance', firstImageId);
 
  Iif (!instance) {
    return false;
  }
 
  const SOPInstanceUID = instance.SOPInstanceUID || instance.SopInstanceUID;
  const SeriesInstanceUID = instance.SeriesInstanceUID;
  const displaySet = displaySetService.getDisplaySetForSOPInstanceUID(
    SOPInstanceUID,
    SeriesInstanceUID
  );
 
  return displaySet?.isReconstructable;
};
 
/**
 * Checks if a contour segmentation has exportable data
 * A contour is exportable if it has any contour annotation/geometry data.
 *
 * @param contour - The contour representation data
 * @returns boolean - Whether the contour has exportable data
 */
export const hasExportableContourData = (
  contour: cstTypes.ContourSegmentationData | undefined
): boolean => {
  if (!contour) {
    return false;
  }
 
  const contourAnnotationUIDsMap = contour?.annotationUIDsMap;
 
  Iif (!contourAnnotationUIDsMap) {
    return false;
  }
 
  return contourAnnotationUIDsMap.size > 0;
};