dcmjs 0.19.2 → 0.19.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/dcmjs.es.js CHANGED
@@ -12180,28 +12180,28 @@ function fillSegmentation(segmentation, inputLabelmaps3D) {
12180
12180
  metadata = labelmap3D.metadata;
12181
12181
  var referencedFramesPerSegment = [];
12182
12182
 
12183
- for (var i = 1; i < metadata.length; i++) {
12184
- if (metadata[i]) {
12185
- referencedFramesPerSegment[i] = [];
12183
+ for (var _i = 1; _i < metadata.length; _i++) {
12184
+ if (metadata[_i]) {
12185
+ referencedFramesPerSegment[_i] = [];
12186
12186
  }
12187
12187
  }
12188
12188
 
12189
- var _loop2 = function _loop2(_i) {
12190
- var labelmap2D = labelmaps2D[_i];
12189
+ var _loop2 = function _loop2(_i2) {
12190
+ var labelmap2D = labelmaps2D[_i2];
12191
12191
 
12192
- if (labelmaps2D[_i]) {
12192
+ if (labelmaps2D[_i2]) {
12193
12193
  var segmentsOnLabelmap = labelmap2D.segmentsOnLabelmap;
12194
12194
  segmentsOnLabelmap.forEach(function (segmentIndex) {
12195
12195
  if (segmentIndex !== 0) {
12196
- referencedFramesPerSegment[segmentIndex].push(_i);
12196
+ referencedFramesPerSegment[segmentIndex].push(_i2);
12197
12197
  numberOfFrames++;
12198
12198
  }
12199
12199
  });
12200
12200
  }
12201
12201
  };
12202
12202
 
12203
- for (var _i = 0; _i < labelmaps2D.length; _i++) {
12204
- _loop2(_i);
12203
+ for (var _i2 = 0; _i2 < labelmaps2D.length; _i2++) {
12204
+ _loop2(_i2);
12205
12205
  }
12206
12206
 
12207
12207
  referencedFramesPerLabelmap[labelmapIndex] = referencedFramesPerSegment;
@@ -12267,8 +12267,8 @@ function _getLabelmapsFromRefernecedFrameIndicies(labelmap3D, referencedFrameInd
12267
12267
  var labelmaps2D = labelmap3D.labelmaps2D;
12268
12268
  var labelmaps = [];
12269
12269
 
12270
- for (var i = 0; i < referencedFrameIndicies.length; i++) {
12271
- var frame = referencedFrameIndicies[i];
12270
+ for (var _i3 = 0; _i3 < referencedFrameIndicies.length; _i3++) {
12271
+ var frame = referencedFrameIndicies[_i3];
12272
12272
  labelmaps.push(labelmaps2D[frame].pixelData);
12273
12273
  }
12274
12274
 
@@ -12294,8 +12294,8 @@ function _createSegFromImages$1(images, isMultiframe, options) {
12294
12294
  dataset._meta = DicomMetaDictionary.namifyDataset(dicomData.meta);
12295
12295
  datasets.push(dataset);
12296
12296
  } else {
12297
- for (var i = 0; i < images.length; i++) {
12298
- var _image = images[i];
12297
+ for (var _i4 = 0; _i4 < images.length; _i4++) {
12298
+ var _image = images[_i4];
12299
12299
  var _arrayBuffer = _image.data.byteArray.buffer;
12300
12300
 
12301
12301
  var _dicomData = DicomMessage.readFile(_arrayBuffer);
@@ -12415,17 +12415,40 @@ function generateToolState$1(imageIds, arrayBuffer, metadataProvider) {
12415
12415
  };
12416
12416
  }
12417
12417
  /**
12418
- * Checks if there is any overlapping segmentations. The check is performed frame by frame.
12419
- * Two assumptions are used in the loop:
12420
- * 1) numberOfSegs * numberOfFrames = groupsLen,
12421
- * i.e. for each frame we have a N PerFrameFunctionalGroupsSequence, where N is the number of segmentations (numberOfSegs).
12422
- * 2) the order of the group sequence is = numberOfFrames of segmentation 1 + numberOfFrames of segmentation 2 + ... + numberOfFrames of segmentation numberOfSegs
12423
- *
12424
- * -------------------
12425
- *
12426
- * TO DO: We could check the ImagePositionPatient and working in 3D coordinates (instead of indexes) and remove the assumptions,
12427
- * but this would greatly increase the computation time (i.e. we would have to sort the data before running checkSEGsOverlapping).
12428
- *
12418
+ * Find the reference frame of the segmentation frame in the source data.
12419
+ * @returns {string} Returns the imageId
12420
+ */
12421
+
12422
+
12423
+ function findReferenceSourceImageId(SourceImageSequence, imageIds, metadataProvider, PerFrameFunctionalGroups, frameOfReferenceUID, tolerance) {
12424
+ var imageId = getImageIdOfSourceImage$1(SourceImageSequence, imageIds, metadataProvider);
12425
+
12426
+ if (!imageId) {
12427
+ /** not found, we can do a check with the PlanePositionSequence.
12428
+ * (WARNING!) However, if more than a source series is present,
12429
+ * this logic can find the wrong frame
12430
+ * (i.e. two source series, with the same frameOfReferenceUID,
12431
+ * that have each a frame with the same ImagePositionPatient of PlanePositionSequence).
12432
+ */
12433
+ if (PerFrameFunctionalGroups.PlanePositionSequence !== undefined && PerFrameFunctionalGroups.PlanePositionSequence[0] !== undefined && PerFrameFunctionalGroups.PlanePositionSequence[0].ImagePositionPatient !== undefined) {
12434
+ for (var _i5 = 0; _i5 < imageIds.length; ++_i5) {
12435
+ var sourceImageMetadata = cornerstone.metaData.get("instance", imageIds[_i5]);
12436
+
12437
+ if (sourceImageMetadata === undefined || sourceImageMetadata.ImagePositionPatient === undefined || sourceImageMetadata.FrameOfReferenceUID !== frameOfReferenceUID) {
12438
+ continue;
12439
+ }
12440
+
12441
+ if (compareArrays(PerFrameFunctionalGroups.PlanePositionSequence[0].ImagePositionPatient, sourceImageMetadata.ImagePositionPatient, tolerance)) {
12442
+ return imageIds[_i5];
12443
+ }
12444
+ }
12445
+ }
12446
+ }
12447
+
12448
+ return imageId;
12449
+ }
12450
+ /**
12451
+ * Checks if there is any overlapping segmentations.
12429
12452
  * @returns {boolean} Returns a flag if segmentations overlapping
12430
12453
  */
12431
12454
 
@@ -12433,135 +12456,126 @@ function generateToolState$1(imageIds, arrayBuffer, metadataProvider) {
12433
12456
  function checkSEGsOverlapping(pixelData, multiframe, imageIds, validOrientations, metadataProvider, tolerance) {
12434
12457
  var SharedFunctionalGroupsSequence = multiframe.SharedFunctionalGroupsSequence,
12435
12458
  PerFrameFunctionalGroupsSequence = multiframe.PerFrameFunctionalGroupsSequence,
12459
+ SegmentSequence = multiframe.SegmentSequence,
12436
12460
  Rows = multiframe.Rows,
12437
12461
  Columns = multiframe.Columns;
12438
- var sharedImageOrientationPatient = SharedFunctionalGroupsSequence.PlaneOrientationSequence ? SharedFunctionalGroupsSequence.PlaneOrientationSequence.ImageOrientationPatient : undefined;
12439
- var sliceLength = Columns * Rows;
12440
- var firstSegIndex = -1;
12441
- var previousimageIdIndex = -1;
12442
- var temp2DArray = new Uint16Array(sliceLength).fill(0);
12443
- var groupsLen = PerFrameFunctionalGroupsSequence.length;
12444
- var numberOfSegs = multiframe.SegmentSequence.length;
12445
- var numberOfFrames = imageIds.length;
12446
-
12447
- if (numberOfSegs * numberOfFrames !== groupsLen) {
12448
- console.warn("Failed to check for overlap of segments: " + "missing frames in PerFrameFunctionalGroupsSequence " + "or the segmentation has different geometry respect to the source image.");
12449
- return false;
12450
- }
12451
-
12452
- var refSegFrame0 = getSegmentIndex(multiframe, 0);
12453
- var refSegFrame1 = getSegmentIndex(multiframe, 1);
12462
+ var numberOfSegs = SegmentSequence.length;
12454
12463
 
12455
- if (refSegFrame0 === undefined || refSegFrame1 === undefined || refSegFrame0 !== refSegFrame1) {
12456
- console.warn("Failed to check for overlap of segments: frames in PerFrameFunctionalGroupsSequence are not sorted.");
12464
+ if (numberOfSegs < 2) {
12457
12465
  return false;
12458
12466
  }
12459
12467
 
12460
- var i = 0;
12461
-
12462
- var _loop4 = function _loop4() {
12463
- var PerFrameFunctionalGroups = PerFrameFunctionalGroupsSequence[i];
12464
- var ImageOrientationPatientI = sharedImageOrientationPatient || PerFrameFunctionalGroups.PlaneOrientationSequence.ImageOrientationPatient;
12465
- var pixelDataI2D = ndarray(new Uint8Array(pixelData.buffer, i * sliceLength, sliceLength), [Rows, Columns]);
12466
- var alignedPixelDataI = alignPixelDataWithSourceData$1(pixelDataI2D, ImageOrientationPatientI, validOrientations, tolerance);
12468
+ var sharedImageOrientationPatient = SharedFunctionalGroupsSequence.PlaneOrientationSequence ? SharedFunctionalGroupsSequence.PlaneOrientationSequence.ImageOrientationPatient : undefined;
12469
+ var sliceLength = Columns * Rows;
12470
+ var groupsLen = PerFrameFunctionalGroupsSequence.length;
12471
+ /** sort groupsLen to have all the segments for each frame in an array
12472
+ * frame 2 : 1, 2
12473
+ * frame 4 : 1, 3
12474
+ * frame 5 : 4
12475
+ */
12467
12476
 
12468
- if (!alignedPixelDataI) {
12469
- console.warn("Individual SEG frames are out of plane with respect to the first SEG frame, this is not yet supported, skipping this frame.");
12470
- return {
12471
- v: false
12472
- };
12473
- }
12477
+ var frameSegmentsMapping = new Map();
12474
12478
 
12475
- var segmentIndex = getSegmentIndex(multiframe, i);
12479
+ var _loop3 = function _loop3(frameSegment) {
12480
+ var PerFrameFunctionalGroups = PerFrameFunctionalGroupsSequence[frameSegment];
12481
+ var segmentIndex = getSegmentIndex(multiframe, frameSegment);
12476
12482
 
12477
12483
  if (segmentIndex === undefined) {
12478
- console.warn("Could not retrieve the segment index, skipping this frame.");
12479
- return {
12480
- v: false
12481
- };
12484
+ console.warn("Could not retrieve the segment index for frame segment " + frameSegment + ", skipping this frame.");
12485
+ return "continue";
12482
12486
  }
12483
12487
 
12484
12488
  var SourceImageSequence = void 0;
12485
12489
 
12486
12490
  if (multiframe.SourceImageSequence) {
12487
- SourceImageSequence = multiframe.SourceImageSequence[i];
12491
+ SourceImageSequence = multiframe.SourceImageSequence[frameSegment];
12488
12492
  } else {
12489
12493
  SourceImageSequence = PerFrameFunctionalGroups.DerivationImageSequence.SourceImageSequence;
12490
12494
  }
12491
12495
 
12492
12496
  if (!SourceImageSequence) {
12493
12497
  console.warn("Source Image Sequence information missing: individual SEG frames are out of plane, this is not yet supported, skipping this frame.");
12494
- return {
12495
- v: false
12496
- };
12498
+ return "continue";
12497
12499
  }
12498
12500
 
12499
- var imageId = getImageIdOfSourceImage$1(SourceImageSequence, imageIds, metadataProvider);
12501
+ var imageId = findReferenceSourceImageId(SourceImageSequence, imageIds, metadataProvider, PerFrameFunctionalGroups, multiframe.FrameOfReferenceUID, tolerance);
12500
12502
 
12501
12503
  if (!imageId) {
12502
- // Image not present in stack, can't import this frame.
12503
- i = i + numberOfFrames;
12504
-
12505
- if (i >= groupsLen) {
12506
- i = i - numberOfFrames * numberOfSegs + 1;
12507
-
12508
- if (i >= numberOfFrames) {
12509
- return "break";
12510
- }
12511
- }
12512
-
12504
+ console.warn("Image not present in stack, can't import frame : " + i + ".");
12513
12505
  return "continue";
12514
12506
  }
12515
12507
 
12516
- var data = alignedPixelDataI.data;
12517
12508
  var imageIdIndex = imageIds.findIndex(function (element) {
12518
12509
  return element === imageId;
12519
12510
  });
12520
12511
 
12521
- if (i === 0) {
12522
- firstSegIndex = segmentIndex;
12523
- }
12524
-
12525
- if (segmentIndex === firstSegIndex && imageIdIndex > previousimageIdIndex) {
12526
- temp2DArray.fill(0);
12527
- previousimageIdIndex = imageIdIndex;
12528
- }
12529
-
12530
- for (var j = 0, len = alignedPixelDataI.data.length; j < len; ++j) {
12531
- if (data[j]) {
12532
- temp2DArray[j]++;
12512
+ if (frameSegmentsMapping.has(imageIdIndex)) {
12513
+ var segmentArray = frameSegmentsMapping.get(imageIdIndex);
12533
12514
 
12534
- if (temp2DArray[j] > 1) {
12535
- return {
12536
- v: true
12537
- };
12538
- }
12515
+ if (!segmentArray.includes(frameSegment)) {
12516
+ segmentArray.push(frameSegment);
12517
+ frameSegmentsMapping.set(imageIdIndex, segmentArray);
12539
12518
  }
12519
+ } else {
12520
+ frameSegmentsMapping.set(imageIdIndex, [frameSegment]);
12540
12521
  }
12522
+ };
12541
12523
 
12542
- i = i + numberOfFrames;
12524
+ for (var frameSegment = 0; frameSegment < groupsLen; ++frameSegment) {
12525
+ var _ret = _loop3(frameSegment);
12543
12526
 
12544
- if (i >= groupsLen) {
12545
- i = i - numberOfFrames * numberOfSegs + 1;
12527
+ if (_ret === "continue") continue;
12528
+ }
12546
12529
 
12547
- if (i >= numberOfFrames) {
12548
- return "break";
12549
- }
12550
- }
12551
- };
12530
+ var _iteratorNormalCompletion = true;
12531
+ var _didIteratorError = false;
12532
+ var _iteratorError = undefined;
12552
12533
 
12553
- _loop3: while (i < groupsLen) {
12554
- var _ret = _loop4();
12534
+ try {
12535
+ for (var _iterator = frameSegmentsMapping.entries()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
12536
+ var _step$value = _slicedToArray(_step.value, 2),
12537
+ user = _step$value[0],
12538
+ role = _step$value[1];
12539
+
12540
+ var temp2DArray = new Uint16Array(sliceLength).fill(0);
12541
+
12542
+ for (var _i6 = 0; _i6 < role.length; ++_i6) {
12543
+ var _frameSegment = role[_i6];
12544
+ var PerFrameFunctionalGroups = PerFrameFunctionalGroupsSequence[_frameSegment];
12545
+ var ImageOrientationPatientI = sharedImageOrientationPatient || PerFrameFunctionalGroups.PlaneOrientationSequence.ImageOrientationPatient;
12546
+ var pixelDataI2D = ndarray(new Uint8Array(pixelData.buffer, _frameSegment * sliceLength, sliceLength), [Rows, Columns]);
12547
+ var alignedPixelDataI = alignPixelDataWithSourceData$1(pixelDataI2D, ImageOrientationPatientI, validOrientations, tolerance);
12548
+
12549
+ if (!alignedPixelDataI) {
12550
+ console.warn("Individual SEG frames are out of plane with respect to the first SEG frame, this is not yet supported, skipping this frame.");
12551
+ continue;
12552
+ }
12555
12553
 
12556
- switch (_ret) {
12557
- case "break":
12558
- break _loop3;
12554
+ var data = alignedPixelDataI.data;
12559
12555
 
12560
- case "continue":
12561
- continue;
12556
+ for (var j = 0, len = data.length; j < len; ++j) {
12557
+ if (data[j] !== 0) {
12558
+ temp2DArray[j]++;
12562
12559
 
12563
- default:
12564
- if (_typeof(_ret) === "object") return _ret.v;
12560
+ if (temp2DArray[j] > 1) {
12561
+ return true;
12562
+ }
12563
+ }
12564
+ }
12565
+ }
12566
+ }
12567
+ } catch (err) {
12568
+ _didIteratorError = true;
12569
+ _iteratorError = err;
12570
+ } finally {
12571
+ try {
12572
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
12573
+ _iterator.return();
12574
+ }
12575
+ } finally {
12576
+ if (_didIteratorError) {
12577
+ throw _iteratorError;
12578
+ }
12565
12579
  }
12566
12580
  }
12567
12581
 
@@ -12585,42 +12599,41 @@ function insertOverlappingPixelDataPlanar(segmentsOnFrame, segmentsOnFrameArray,
12585
12599
  var tempBuffer = labelmapBufferArray[m].slice(0); // temp list for checking overlaps
12586
12600
 
12587
12601
  var tempSegmentsOnFrame = lodash_clonedeep(segmentsOnFrameArray[m]);
12588
- /* split overlapping SEGs algorithm for each segment:
12589
- A) copy the labelmapBuffer in the array with index 0
12590
- B) add the segment pixel per pixel on the copied buffer from (A)
12591
- C) if no overlap, copy the results back on the orignal array from (A)
12592
- D) if overlap, repeat increasing the index m up to M (if out of memory, add new buffer in the array and M++);
12593
- */
12602
+ /** split overlapping SEGs algorithm for each segment:
12603
+ * A) copy the labelmapBuffer in the array with index 0
12604
+ * B) add the segment pixel per pixel on the copied buffer from (A)
12605
+ * C) if no overlap, copy the results back on the orignal array from (A)
12606
+ * D) if overlap, repeat increasing the index m up to M (if out of memory, add new buffer in the array and M++);
12607
+ */
12594
12608
 
12595
12609
  var numberOfSegs = multiframe.SegmentSequence.length;
12596
12610
 
12597
12611
  for (var segmentIndexToProcess = 1; segmentIndexToProcess <= numberOfSegs; ++segmentIndexToProcess) {
12598
- var _loop5 = function _loop5(_i2, groupsLen) {
12599
- var PerFrameFunctionalGroups = PerFrameFunctionalGroupsSequence[_i2];
12600
- var segmentIndex = getSegmentIndex(multiframe, _i2);
12612
+ var _loop4 = function _loop4(groupsLen, _i8) {
12613
+ var PerFrameFunctionalGroups = PerFrameFunctionalGroupsSequence[_i8];
12614
+ var segmentIndex = getSegmentIndex(multiframe, _i8);
12601
12615
 
12602
12616
  if (segmentIndex === undefined) {
12603
12617
  throw new Error("Could not retrieve the segment index. Aborting segmentation loading.");
12604
12618
  }
12605
12619
 
12606
12620
  if (segmentIndex !== segmentIndexToProcess) {
12607
- i = _i2;
12621
+ _i7 = _i8;
12608
12622
  return "continue";
12609
12623
  }
12610
12624
 
12611
12625
  var ImageOrientationPatientI = sharedImageOrientationPatient || PerFrameFunctionalGroups.PlaneOrientationSequence.ImageOrientationPatient;
12612
- var pixelDataI2D = ndarray(new Uint8Array(pixelData.buffer, _i2 * sliceLength, sliceLength), [Rows, Columns]);
12626
+ var pixelDataI2D = ndarray(new Uint8Array(pixelData.buffer, _i8 * sliceLength, sliceLength), [Rows, Columns]);
12613
12627
  var alignedPixelDataI = alignPixelDataWithSourceData$1(pixelDataI2D, ImageOrientationPatientI, validOrientations, tolerance);
12614
12628
 
12615
12629
  if (!alignedPixelDataI) {
12616
12630
  throw new Error("Individual SEG frames are out of plane with respect to the first SEG frame. " + "This is not yet supported. Aborting segmentation loading.");
12617
12631
  }
12618
12632
 
12619
- var imageId = undefined;
12620
12633
  var SourceImageSequence = undefined;
12621
12634
 
12622
12635
  if (multiframe.SourceImageSequence) {
12623
- SourceImageSequence = multiframe.SourceImageSequence[_i2];
12636
+ SourceImageSequence = multiframe.SourceImageSequence[_i8];
12624
12637
  } else {
12625
12638
  SourceImageSequence = PerFrameFunctionalGroups.DerivationImageSequence.SourceImageSequence;
12626
12639
  }
@@ -12629,11 +12642,11 @@ function insertOverlappingPixelDataPlanar(segmentsOnFrame, segmentsOnFrameArray,
12629
12642
  throw new Error("Source Image Sequence information missing: individual SEG frames are out of plane. " + "This is not yet supported. Aborting segmentation loading.");
12630
12643
  }
12631
12644
 
12632
- imageId = getImageIdOfSourceImage$1(SourceImageSequence, imageIds, metadataProvider);
12645
+ var imageId = findReferenceSourceImageId(SourceImageSequence, imageIds, metadataProvider, PerFrameFunctionalGroups, multiframe.FrameOfReferenceUID, tolerance);
12633
12646
 
12634
12647
  if (!imageId) {
12635
- i = _i2;
12636
- // Image not present in stack, can't import this frame.
12648
+ console.warn("Image not present in stack, can't import frame : " + _i8 + ".");
12649
+ _i7 = _i8;
12637
12650
  return "continue";
12638
12651
  }
12639
12652
 
@@ -12665,7 +12678,7 @@ function insertOverlappingPixelDataPlanar(segmentsOnFrame, segmentsOnFrameArray,
12665
12678
 
12666
12679
  tempBuffer = labelmapBufferArray[m].slice(0);
12667
12680
  tempSegmentsOnFrame = lodash_clonedeep(segmentsOnFrameArray[m]);
12668
- _i2 = 0;
12681
+ _i8 = 0;
12669
12682
  break;
12670
12683
  } else {
12671
12684
  labelmap2DView[j] = segmentIndex;
@@ -12688,11 +12701,11 @@ function insertOverlappingPixelDataPlanar(segmentsOnFrame, segmentsOnFrameArray,
12688
12701
  segmentsOnFrame[imageIdIndex].push(segmentIndex);
12689
12702
  }
12690
12703
 
12691
- i = _i2;
12704
+ _i7 = _i8;
12692
12705
  };
12693
12706
 
12694
- for (var i = 0, groupsLen = PerFrameFunctionalGroupsSequence.length; i < groupsLen; ++i) {
12695
- var _ret2 = _loop5(i, groupsLen);
12707
+ for (var _i7 = 0, groupsLen = PerFrameFunctionalGroupsSequence.length; _i7 < groupsLen; ++_i7) {
12708
+ var _ret2 = _loop4(groupsLen, _i7);
12696
12709
 
12697
12710
  if (_ret2 === "continue") continue;
12698
12711
  }
@@ -12721,27 +12734,26 @@ function insertPixelDataPlanar(segmentsOnFrame, segmentsOnFrameArray, labelmapBu
12721
12734
  var sharedImageOrientationPatient = SharedFunctionalGroupsSequence.PlaneOrientationSequence ? SharedFunctionalGroupsSequence.PlaneOrientationSequence.ImageOrientationPatient : undefined;
12722
12735
  var sliceLength = Columns * Rows;
12723
12736
 
12724
- var _loop6 = function _loop6(groupsLen, _i3) {
12725
- var PerFrameFunctionalGroups = PerFrameFunctionalGroupsSequence[_i3];
12737
+ var _loop5 = function _loop5(groupsLen, _i9) {
12738
+ var PerFrameFunctionalGroups = PerFrameFunctionalGroupsSequence[_i9];
12726
12739
  var ImageOrientationPatientI = sharedImageOrientationPatient || PerFrameFunctionalGroups.PlaneOrientationSequence.ImageOrientationPatient;
12727
- var pixelDataI2D = ndarray(new Uint8Array(pixelData.buffer, _i3 * sliceLength, sliceLength), [Rows, Columns]);
12740
+ var pixelDataI2D = ndarray(new Uint8Array(pixelData.buffer, _i9 * sliceLength, sliceLength), [Rows, Columns]);
12728
12741
  var alignedPixelDataI = alignPixelDataWithSourceData$1(pixelDataI2D, ImageOrientationPatientI, validOrientations, tolerance);
12729
12742
 
12730
12743
  if (!alignedPixelDataI) {
12731
12744
  throw new Error("Individual SEG frames are out of plane with respect to the first SEG frame. " + "This is not yet supported. Aborting segmentation loading.");
12732
12745
  }
12733
12746
 
12734
- var segmentIndex = getSegmentIndex(multiframe, _i3);
12747
+ var segmentIndex = getSegmentIndex(multiframe, _i9);
12735
12748
 
12736
12749
  if (segmentIndex === undefined) {
12737
12750
  throw new Error("Could not retrieve the segment index. Aborting segmentation loading.");
12738
12751
  }
12739
12752
 
12740
- var imageId = undefined;
12741
12753
  var SourceImageSequence = undefined;
12742
12754
 
12743
12755
  if (multiframe.SourceImageSequence) {
12744
- SourceImageSequence = multiframe.SourceImageSequence[_i3];
12756
+ SourceImageSequence = multiframe.SourceImageSequence[_i9];
12745
12757
  } else {
12746
12758
  SourceImageSequence = PerFrameFunctionalGroups.DerivationImageSequence.SourceImageSequence;
12747
12759
  }
@@ -12750,10 +12762,10 @@ function insertPixelDataPlanar(segmentsOnFrame, segmentsOnFrameArray, labelmapBu
12750
12762
  throw new Error("Source Image Sequence information missing: individual SEG frames are out of plane. " + "This is not yet supported. Aborting segmentation loading.");
12751
12763
  }
12752
12764
 
12753
- imageId = getImageIdOfSourceImage$1(SourceImageSequence, imageIds, metadataProvider);
12765
+ var imageId = findReferenceSourceImageId(SourceImageSequence, imageIds, metadataProvider, PerFrameFunctionalGroups, multiframe.FrameOfReferenceUID, tolerance);
12754
12766
 
12755
12767
  if (!imageId) {
12756
- // Image not present in stack, can't import this frame.
12768
+ console.warn("Image not present in stack, can't import frame : " + _i9 + ".");
12757
12769
  return "continue";
12758
12770
  }
12759
12771
 
@@ -12769,7 +12781,7 @@ function insertPixelDataPlanar(segmentsOnFrame, segmentsOnFrameArray, labelmapBu
12769
12781
  var byteOffset = sliceLength * 2 * imageIdIndex; // 2 bytes/pixel
12770
12782
 
12771
12783
  var labelmap2DView = new Uint16Array(labelmapBufferArray[0], byteOffset, sliceLength);
12772
- var data = alignedPixelDataI.data; //
12784
+ var data = alignedPixelDataI.data;
12773
12785
 
12774
12786
  for (var j = 0, len = alignedPixelDataI.data.length; j < len; ++j) {
12775
12787
  if (data[j]) {
@@ -12789,8 +12801,8 @@ function insertPixelDataPlanar(segmentsOnFrame, segmentsOnFrameArray, labelmapBu
12789
12801
  }
12790
12802
  };
12791
12803
 
12792
- for (var _i3 = 0, groupsLen = PerFrameFunctionalGroupsSequence.length; _i3 < groupsLen; ++_i3) {
12793
- var _ret3 = _loop6(groupsLen, _i3);
12804
+ for (var _i9 = 0, groupsLen = PerFrameFunctionalGroupsSequence.length; _i9 < groupsLen; ++_i9) {
12805
+ var _ret3 = _loop5(groupsLen, _i9);
12794
12806
 
12795
12807
  if (_ret3 === "continue") continue;
12796
12808
  }
@@ -12804,7 +12816,7 @@ function checkOrientation(multiframe, validOrientations, sourceDataDimensions, t
12804
12816
  var PerFrameFunctionalGroups = PerFrameFunctionalGroupsSequence[0];
12805
12817
  var iop = sharedImageOrientationPatient || PerFrameFunctionalGroups.PlaneOrientationSequence.ImageOrientationPatient;
12806
12818
  var inPlane = validOrientations.some(function (operation) {
12807
- return compareIOP$1(iop, operation, tolerance);
12819
+ return compareArrays(iop, operation, tolerance);
12808
12820
  });
12809
12821
 
12810
12822
  if (inPlane) {
@@ -12977,50 +12989,60 @@ function getValidOrientations$1(iop) {
12977
12989
 
12978
12990
 
12979
12991
  function alignPixelDataWithSourceData$1(pixelData2D, iop, orientations, tolerance) {
12980
- if (compareIOP$1(iop, orientations[0], tolerance)) {
12992
+ if (compareArrays(iop, orientations[0], tolerance)) {
12981
12993
  return pixelData2D;
12982
- } else if (compareIOP$1(iop, orientations[1], tolerance)) {
12994
+ } else if (compareArrays(iop, orientations[1], tolerance)) {
12983
12995
  // Flipped vertically.
12984
12996
  // Undo Flip
12985
12997
  return flipMatrix2D.v(pixelData2D);
12986
- } else if (compareIOP$1(iop, orientations[2], tolerance)) {
12998
+ } else if (compareArrays(iop, orientations[2], tolerance)) {
12987
12999
  // Flipped horizontally.
12988
13000
  // Unfo flip
12989
13001
  return flipMatrix2D.h(pixelData2D);
12990
- } else if (compareIOP$1(iop, orientations[3], tolerance)) {
13002
+ } else if (compareArrays(iop, orientations[3], tolerance)) {
12991
13003
  //Rotated 90 degrees
12992
13004
  // Rotate back
12993
13005
  return rotateMatrix902D(pixelData2D);
12994
- } else if (compareIOP$1(iop, orientations[4], tolerance)) {
13006
+ } else if (compareArrays(iop, orientations[4], tolerance)) {
12995
13007
  //Rotated 90 degrees and fliped horizontally.
12996
13008
  // Undo flip and rotate back.
12997
13009
  return rotateMatrix902D(flipMatrix2D.h(pixelData2D));
12998
- } else if (compareIOP$1(iop, orientations[5], tolerance)) {
13010
+ } else if (compareArrays(iop, orientations[5], tolerance)) {
12999
13011
  // Rotated 90 degrees and fliped vertically
13000
13012
  // Unfo flip and rotate back.
13001
13013
  return rotateMatrix902D(flipMatrix2D.v(pixelData2D));
13002
- } else if (compareIOP$1(iop, orientations[6], tolerance)) {
13014
+ } else if (compareArrays(iop, orientations[6], tolerance)) {
13003
13015
  // Rotated 180 degrees. // TODO -> Do this more effeciently, there is a 1:1 mapping like 90 degree rotation.
13004
13016
  return rotateMatrix902D(rotateMatrix902D(pixelData2D));
13005
- } else if (compareIOP$1(iop, orientations[7], tolerance)) {
13017
+ } else if (compareArrays(iop, orientations[7], tolerance)) {
13006
13018
  // Rotated 270 degrees
13007
13019
  // Rotate back.
13008
13020
  return rotateMatrix902D(rotateMatrix902D(rotateMatrix902D(pixelData2D)));
13009
13021
  }
13010
13022
  }
13011
13023
  /**
13012
- * compareIOP - Returns true if iop1 and iop2 are equal
13024
+ * compareArrays - Returns true if array1 and array2 are equal
13013
13025
  * within a tolerance.
13014
13026
  *
13015
- * @param {Number[6]} iop1 - An ImageOrientationPatient array.
13016
- * @param {Number[6]} iop2 - An ImageOrientationPatient array.
13027
+ * @param {Number[]} array1 - An array.
13028
+ * @param {Number[]} array2 - An array.
13017
13029
  * @param {Number} tolerance.
13018
- * @return {Boolean} True if iop1 and iop2 are equal.
13030
+ * @return {Boolean} True if array1 and array2 are equal.
13019
13031
  */
13020
13032
 
13021
13033
 
13022
- function compareIOP$1(iop1, iop2, tolerance) {
13023
- return nearlyEqual(iop1[0], iop2[0], tolerance) && nearlyEqual(iop1[1], iop2[1], tolerance) && nearlyEqual(iop1[2], iop2[2], tolerance) && nearlyEqual(iop1[3], iop2[3], tolerance) && nearlyEqual(iop1[4], iop2[4], tolerance) && nearlyEqual(iop1[5], iop2[5], tolerance);
13034
+ function compareArrays(array1, array2, tolerance) {
13035
+ if (array1.length != array2.length) {
13036
+ return false;
13037
+ }
13038
+
13039
+ for (var _i10 = 0; _i10 < array1.length; ++_i10) {
13040
+ if (!nearlyEqual(array1[_i10], array2[_i10], tolerance)) {
13041
+ return false;
13042
+ }
13043
+ }
13044
+
13045
+ return true;
13024
13046
  }
13025
13047
 
13026
13048
  function getSegmentMetadata$1(multiframe, seriesInstanceUid) {