extensions/xnat/src/utils/IO/classes/_tempDCMJSFork/Segmentation_4X_fork.js
/**
 * generateToolState - Given a set of cornrstoneTools imageIds and a Segmentation buffer,
 * derive cornerstoneTools toolState and brush metadata.
 *
 * @param  {string[]} imageIds    An array of the imageIds.
 * @param  {ArrayBuffer} arrayBuffer The SEG arrayBuffer.
 * @param {*} metadataProvider
 * @returns {Object}  The toolState and an object from which the
 *                    segment metadata can be derived.
 */
Given a set of cornrstoneTools imageIds and a Segmentation buffer,
derive cornerstoneTools toolState and brush metadata.

@param  {string[]} imageIds    An array of the imageIds.
@param  {ArrayBuffer} arrayBuffer The SEG arrayBuffer.
@param {*} metadataProvider
@returns {Object}  The toolState and an object from which the
segment metadata can be derived.
Given a set of cornrstoneTools imageIds and a Segmentation buffer derive cornerstoneTools toolState and brush metadata . @param { string [] } imageIds An array of the imageIds . @param { ArrayBuffer } arrayBuffer The SEG arrayBuffer . @param { * } metadataProvider @returns { Object } The toolState and an object from which the segment metadata can be derived .
function generateToolState(imageIds, arrayBuffer, metadataProvider) {
  const dicomData = DicomMessage.readFile(arrayBuffer);
  const dataset = DicomMetaDictionary.naturalizeDataset(dicomData.dict);
  dataset._meta = DicomMetaDictionary.namifyDataset(dicomData.meta);
  const multiframe = Normalizer.normalizeToDataset([dataset]);
  const imagePlaneModule = metadataProvider.get(
    'imagePlaneModule',
    imageIds[0]
  );
  const generalSeriesModule = metadataProvider.get(
    'generalSeriesModule',
    imageIds[0]
  );
  const SeriesInstanceUID = generalSeriesModule.seriesInstanceUID;
  console.warn(
    'Note the cornerstoneTools 4.0 currently assumes the labelmaps are non-overlapping. Overlapping segments will allocate incorrectly. Feel free to submit a PR to improve this behaviour!'
  );
  if (!imagePlaneModule) {
    console.warn('Insufficient metadata, imagePlaneModule missing.');
  }
  const ImageOrientationPatient = Array.isArray(imagePlaneModule.rowCosines)
    ? [...imagePlaneModule.rowCosines, ...imagePlaneModule.columnCosines]
    : [
        imagePlaneModule.rowCosines.x,
        imagePlaneModule.rowCosines.y,
        imagePlaneModule.rowCosines.z,
        imagePlaneModule.columnCosines.x,
        imagePlaneModule.columnCosines.y,
        imagePlaneModule.columnCosines.z,
      ];
  const validOrientations = getValidOrientations(ImageOrientationPatient);
  const sliceLength = multiframe.Columns * multiframe.Rows;
  const segMetadata = getSegmentMetadata(multiframe, SeriesInstanceUID);
  const TransferSyntaxUID = multiframe._meta.TransferSyntaxUID.Value[0];
  let pixelData;
  let isFractional = false;
  if (TransferSyntaxUID === '1.2.840.10008.1.2.5') {
    const rleEncodedFrames = Array.isArray(multiframe.PixelData)
      ? multiframe.PixelData
      : [multiframe.PixelData];
    pixelData = decode(rleEncodedFrames, multiframe.Rows, multiframe.Columns);
    if (multiframe.BitsStored === 1) {
      console.warn('No implementation for rle + bitbacking.');
      return;
    }
  } else {
    const results = unpackPixelData(multiframe);
    pixelData = results.pixelData;
    isFractional = results.isFractional;
    if (!pixelData) {
      throw new Error('Fractional segmentations are not yet supported');
    }
  }
  const orientation = checkOrientation(multiframe, validOrientations, [
    imagePlaneModule.rows,
    imagePlaneModule.columns,
    imageIds.length,
  ]);
  const segmentsOnFrame = [];
  let insertFunction;
  switch (orientation) {
    case 'Planar':
      insertFunction = insertPixelDataPlanar;
      break;
    case 'Perpendicular':
      throw new Error(
        'Segmentations orthogonal to the acquisition plane of the source data are not yet supported.'
      );
    case 'Oblique':
      throw new Error(
        'Segmentations oblique to the acquisition plane of the source data are not yet supported.'
      );
  }
  const arrayBufferLength = sliceLength * imageIds.length * 2; 
  const labelmapBuffer = new ArrayBuffer(arrayBufferLength);
  let probabilityMapBuffer;
  if (isFractional) {
    probabilityMapBuffer = new ArrayBuffer(sliceLength * imageIds.length);
  }
  insertFunction(
    segmentsOnFrame,
    labelmapBuffer,
    pixelData,
    multiframe,
    imageIds,
    validOrientations,
    metadataProvider,
    isFractional,
    probabilityMapBuffer
  );
  return {
    labelmapBuffer,
    probabilityMapBuffer,
    isFractional,
    segMetadata,
    segmentsOnFrame,
  };
}
function generateToolState ( imageIds , arrayBuffer , metadataProvider ) { const dicomData = DicomMessage . readFile ( arrayBuffer ) ; const dataset = DicomMetaDictionary . naturalizeDataset ( dicomData . dict ) ; dataset . _meta = DicomMetaDictionary . namifyDataset ( dicomData . meta ) ; const multiframe = Normalizer . normalizeToDataset ( [ dataset ] ) ; const imagePlaneModule = metadataProvider . get ( 'imagePlaneModule' , imageIds [ 0 ] ) ; const generalSeriesModule = metadataProvider . get ( 'generalSeriesModule' , imageIds [ 0 ] ) ; const SeriesInstanceUID = generalSeriesModule . seriesInstanceUID ; console . warn ( 'Note the cornerstoneTools 4.0 currently assumes the labelmaps are non-overlapping. Overlapping segments will allocate incorrectly. Feel free to submit a PR to improve this behaviour!' ) ; if ( ! imagePlaneModule ) { console . warn ( 'Insufficient metadata, imagePlaneModule missing.' ) ; } const ImageOrientationPatient = Array . isArray ( imagePlaneModule . rowCosines ) ? [ ... imagePlaneModule . rowCosines , ... imagePlaneModule . columnCosines ] : [ imagePlaneModule . rowCosines . x , imagePlaneModule . rowCosines . y , imagePlaneModule . rowCosines . z , imagePlaneModule . columnCosines . x , imagePlaneModule . columnCosines . y , imagePlaneModule . columnCosines . z , ] ; const validOrientations = getValidOrientations ( ImageOrientationPatient ) ; const sliceLength = multiframe . Columns * multiframe . Rows ; const segMetadata = getSegmentMetadata ( multiframe , SeriesInstanceUID ) ; const TransferSyntaxUID = multiframe . _meta . TransferSyntaxUID . Value [ 0 ] ; let pixelData ; let isFractional = false ; if ( TransferSyntaxUID === '1.2.840.10008.1.2.5' ) { const rleEncodedFrames = Array . isArray ( multiframe . PixelData ) ? multiframe . PixelData : [ multiframe . PixelData ] ; pixelData = decode ( rleEncodedFrames , multiframe . Rows , multiframe . Columns ) ; if ( multiframe . BitsStored === 1 ) { console . warn ( 'No implementation for rle + bitbacking.' ) ; return ; } } else { const results = unpackPixelData ( multiframe ) ; pixelData = results . pixelData ; isFractional = results . isFractional ; if ( ! pixelData ) { throw new Error ( 'Fractional segmentations are not yet supported' ) ; } } const orientation = checkOrientation ( multiframe , validOrientations , [ imagePlaneModule . rows , imagePlaneModule . columns , imageIds . length , ] ) ; const segmentsOnFrame = [ ] ; let insertFunction ; switch ( orientation ) { case 'Planar' : insertFunction = insertPixelDataPlanar ; break ; case 'Perpendicular' : throw new Error ( 'Segmentations orthogonal to the acquisition plane of the source data are not yet supported.' ) ; case 'Oblique' : throw new Error ( 'Segmentations oblique to the acquisition plane of the source data are not yet supported.' ) ; } const arrayBufferLength = sliceLength * imageIds . length * 2 ; const labelmapBuffer = new ArrayBuffer ( arrayBufferLength ) ; let probabilityMapBuffer ; if ( isFractional ) { probabilityMapBuffer = new ArrayBuffer ( sliceLength * imageIds . length ) ; } insertFunction ( segmentsOnFrame , labelmapBuffer , pixelData , multiframe , imageIds , validOrientations , metadataProvider , isFractional , probabilityMapBuffer ) ; return { labelmapBuffer , probabilityMapBuffer , isFractional , segMetadata , segmentsOnFrame , } ; }
generateToolState - Given a set of cornrstoneTools imageIds and a Segmentation buffer,
 derive cornerstoneTools toolState and brush metadata.
generateToolState - Given a set of cornrstoneTools imageIds and a Segmentation buffer derive cornerstoneTools toolState and brush metadata .
// Get IOP from ref series, compute supported orientations: //insertFunction = insertPixelDataPerpendicular; // 2 bytes per label voxel in cst4.