extensions/xnat/src/utils/IO/classes/_tempDCMJSFork/Segmentation_4X_fork.js
/**
 * fillSegmentation - Fills a derived segmentation dataset with cornerstoneTools `LabelMap3D` data.
 *
 * @param  {object[]} segmentation An empty segmentation derived dataset.
 * @param  {Object|Object[]} inputLabelmaps3D The cornerstone `Labelmap3D` object, or an array of objects.
 * @param  {Object} userOptions Options object to override default options.
 * @returns {Blob}           description
 */
Fills a derived segmentation dataset with cornerstoneTools `LabelMap3D` data.
@param  {object[]} segmentation An empty segmentation derived dataset.
@param  {Object|Object[]} inputLabelmaps3D The cornerstone `Labelmap3D` object, or an array of objects.
@param  {Object} userOptions Options object to override default options.
@returns {Blob}           description
Fills a derived segmentation dataset with cornerstoneTools ` LabelMap3D ` data . @param { object [] } segmentation An empty segmentation derived dataset . @param { Object|Object [] } inputLabelmaps3D The cornerstone ` Labelmap3D ` object or an array of objects . @param { Object } userOptions Options object to override default options . @returns { Blob } description
function fillSegmentation(segmentation, inputLabelmaps3D, userOptions = {}) {
  const options = Object.assign(
    {},
    generateSegmentationDefaultOptions,
    userOptions
  );
  const labelmaps3D = Array.isArray(inputLabelmaps3D)
    ? inputLabelmaps3D
    : [inputLabelmaps3D];
  let numberOfFrames = 0;
  const referencedFramesPerLabelmap = [];
  for (
    let labelmapIndex = 0;
    labelmapIndex < labelmaps3D.length;
    labelmapIndex++
  ) {
    const labelmap3D = labelmaps3D[labelmapIndex];
    const { labelmaps2D, metadata } = labelmap3D;
    const referencedFramesPerSegment = [];
    for (let i = 1; i < metadata.length; i++) {
      if (metadata[i]) {
        referencedFramesPerSegment[i] = [];
      }
    }
    for (let i = 0; i < labelmaps2D.length; i++) {
      const labelmap2D = labelmaps2D[i];
      if (labelmaps2D[i]) {
        const { segmentsOnLabelmap } = labelmap2D;
        segmentsOnLabelmap.forEach(segmentIndex => {
          if (segmentIndex !== 0) {
            referencedFramesPerSegment[segmentIndex].push(i);
            numberOfFrames++;
          }
        });
      }
    }
    referencedFramesPerLabelmap[labelmapIndex] = referencedFramesPerSegment;
  }
  segmentation.setNumberOfFrames(numberOfFrames);
  for (
    let labelmapIndex = 0;
    labelmapIndex < labelmaps3D.length;
    labelmapIndex++
  ) {
    const referencedFramesPerSegment =
      referencedFramesPerLabelmap[labelmapIndex];
    const labelmap3D = labelmaps3D[labelmapIndex];
    const { metadata } = labelmap3D;
    for (
      let segmentIndex = 1;
      segmentIndex < referencedFramesPerSegment.length;
      segmentIndex++
    ) {
      const referencedFrameIndicies = referencedFramesPerSegment[segmentIndex];
      if (referencedFrameIndicies) {
        const referencedFrameNumbers = referencedFrameIndicies.map(element => {
          return element + 1;
        });
        const segmentMetadata = metadata[segmentIndex];
        const labelmaps = _getLabelmapsFromRefernecedFrameIndicies(
          labelmap3D,
          referencedFrameIndicies
        );
        segmentation.addSegmentFromLabelmap(
          segmentMetadata,
          labelmaps,
          segmentIndex,
          referencedFrameNumbers
        );
      }
    }
  }
  if (options.rleEncode) {
    const rleEncodedFrames = encode(
      segmentation.dataset.PixelData,
      numberOfFrames,
      segmentation.dataset.Rows,
      segmentation.dataset.Columns
    );
    segmentation.assignToDataset({
      BitsAllocated: '8',
      BitsStored: '8',
      HighBit: '7',
      SegmentationType: 'FRACTIONAL',
      SegmentationFractionalType: 'PROBABILITY',
      MaximumFractionalValue: '255',
    });
    segmentation.dataset._meta.TransferSyntaxUID = {
      Value: ['1.2.840.10008.1.2.5'],
      vr: 'UI',
    };
    segmentation.dataset._vrMap.PixelData = 'OB';
    segmentation.dataset.PixelData = rleEncodedFrames;
  } else {
    segmentation.bitPackPixelData();
  }
  const segBlob = datasetToBlob(segmentation.dataset);
  return segBlob;
}
function fillSegmentation ( segmentation , inputLabelmaps3D , userOptions = { } ) { const options = Object . assign ( { } , generateSegmentationDefaultOptions , userOptions ) ; const labelmaps3D = Array . isArray ( inputLabelmaps3D ) ? inputLabelmaps3D : [ inputLabelmaps3D ] ; let numberOfFrames = 0 ; const referencedFramesPerLabelmap = [ ] ; for ( let labelmapIndex = 0 ; labelmapIndex < labelmaps3D . length ; labelmapIndex ++ ) { const labelmap3D = labelmaps3D [ labelmapIndex ] ; const { labelmaps2D , metadata } = labelmap3D ; const referencedFramesPerSegment = [ ] ; for ( let i = 1 ; i < metadata . length ; i ++ ) { if ( metadata [ i ] ) { referencedFramesPerSegment [ i ] = [ ] ; } } for ( let i = 0 ; i < labelmaps2D . length ; i ++ ) { const labelmap2D = labelmaps2D [ i ] ; if ( labelmaps2D [ i ] ) { const { segmentsOnLabelmap } = labelmap2D ; segmentsOnLabelmap . forEach ( segmentIndex => { if ( segmentIndex !== 0 ) { referencedFramesPerSegment [ segmentIndex ] . push ( i ) ; numberOfFrames ++ ; } } ) ; } } referencedFramesPerLabelmap [ labelmapIndex ] = referencedFramesPerSegment ; } segmentation . setNumberOfFrames ( numberOfFrames ) ; for ( let labelmapIndex = 0 ; labelmapIndex < labelmaps3D . length ; labelmapIndex ++ ) { const referencedFramesPerSegment = referencedFramesPerLabelmap [ labelmapIndex ] ; const labelmap3D = labelmaps3D [ labelmapIndex ] ; const { metadata } = labelmap3D ; for ( let segmentIndex = 1 ; segmentIndex < referencedFramesPerSegment . length ; segmentIndex ++ ) { const referencedFrameIndicies = referencedFramesPerSegment [ segmentIndex ] ; if ( referencedFrameIndicies ) { const referencedFrameNumbers = referencedFrameIndicies . map ( element => { return element + 1 ; } ) ; const segmentMetadata = metadata [ segmentIndex ] ; const labelmaps = _getLabelmapsFromRefernecedFrameIndicies ( labelmap3D , referencedFrameIndicies ) ; segmentation . addSegmentFromLabelmap ( segmentMetadata , labelmaps , segmentIndex , referencedFrameNumbers ) ; } } } if ( options . rleEncode ) { const rleEncodedFrames = encode ( segmentation . dataset . PixelData , numberOfFrames , segmentation . dataset . Rows , segmentation . dataset . Columns ) ; segmentation . assignToDataset ( { BitsAllocated : '8' , BitsStored : '8' , HighBit : '7' , SegmentationType : 'FRACTIONAL' , SegmentationFractionalType : 'PROBABILITY' , MaximumFractionalValue : '255' , } ) ; segmentation . dataset . _meta . TransferSyntaxUID = { Value : [ '1.2.840.10008.1.2.5' ] , vr : 'UI' , } ; segmentation . dataset . _vrMap . PixelData = 'OB' ; segmentation . dataset . PixelData = rleEncodedFrames ; } else { segmentation . bitPackPixelData ( ) ; } const segBlob = datasetToBlob ( segmentation . dataset ) ; return segBlob ; }
fillSegmentation - Fills a derived segmentation dataset with cornerstoneTools `LabelMap3D` data.
fillSegmentation - Fills a derived segmentation dataset with cornerstoneTools ` LabelMap3D ` data .
// Use another variable so we don't redefine labelmaps3D. // Frame numbers start from 1. // Must use fractional now to RLE encode, as the DICOM standard only allows BitStored && BitsAllocated // to be 1 for BINARY. This is not ideal and there should be a better format for compression in this manner // added to the standard. // If no rleEncoding, at least bitpack the data.