dcmjs 0.19.1 → 0.19.5

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
@@ -2642,6 +2642,13 @@ function () {
2642
2642
  value: function isPixelDataTag() {
2643
2643
  return this.is(0x7fe00010);
2644
2644
  }
2645
+ }, {
2646
+ key: "isPrivateCreator",
2647
+ value: function isPrivateCreator() {
2648
+ var group = this.group();
2649
+ var element = this.element();
2650
+ return group % 2 === 1 && element < 0x100 && element > 0x00;
2651
+ }
2645
2652
  }, {
2646
2653
  key: "write",
2647
2654
  value: function write(stream, vrType, values, syntax, writeOptions) {
@@ -3271,6 +3278,8 @@ function () {
3271
3278
  vrType = "OW";
3272
3279
  } else if (vrType == "xs") {
3273
3280
  vrType = "US";
3281
+ } else if (tag.isPrivateCreator()) {
3282
+ vrType = "LO";
3274
3283
  } else {
3275
3284
  vrType = "UN";
3276
3285
  }
@@ -12180,28 +12189,28 @@ function fillSegmentation(segmentation, inputLabelmaps3D) {
12180
12189
  metadata = labelmap3D.metadata;
12181
12190
  var referencedFramesPerSegment = [];
12182
12191
 
12183
- for (var i = 1; i < metadata.length; i++) {
12184
- if (metadata[i]) {
12185
- referencedFramesPerSegment[i] = [];
12192
+ for (var _i = 1; _i < metadata.length; _i++) {
12193
+ if (metadata[_i]) {
12194
+ referencedFramesPerSegment[_i] = [];
12186
12195
  }
12187
12196
  }
12188
12197
 
12189
- var _loop2 = function _loop2(_i) {
12190
- var labelmap2D = labelmaps2D[_i];
12198
+ var _loop2 = function _loop2(_i2) {
12199
+ var labelmap2D = labelmaps2D[_i2];
12191
12200
 
12192
- if (labelmaps2D[_i]) {
12201
+ if (labelmaps2D[_i2]) {
12193
12202
  var segmentsOnLabelmap = labelmap2D.segmentsOnLabelmap;
12194
12203
  segmentsOnLabelmap.forEach(function (segmentIndex) {
12195
12204
  if (segmentIndex !== 0) {
12196
- referencedFramesPerSegment[segmentIndex].push(_i);
12205
+ referencedFramesPerSegment[segmentIndex].push(_i2);
12197
12206
  numberOfFrames++;
12198
12207
  }
12199
12208
  });
12200
12209
  }
12201
12210
  };
12202
12211
 
12203
- for (var _i = 0; _i < labelmaps2D.length; _i++) {
12204
- _loop2(_i);
12212
+ for (var _i2 = 0; _i2 < labelmaps2D.length; _i2++) {
12213
+ _loop2(_i2);
12205
12214
  }
12206
12215
 
12207
12216
  referencedFramesPerLabelmap[labelmapIndex] = referencedFramesPerSegment;
@@ -12267,8 +12276,8 @@ function _getLabelmapsFromRefernecedFrameIndicies(labelmap3D, referencedFrameInd
12267
12276
  var labelmaps2D = labelmap3D.labelmaps2D;
12268
12277
  var labelmaps = [];
12269
12278
 
12270
- for (var i = 0; i < referencedFrameIndicies.length; i++) {
12271
- var frame = referencedFrameIndicies[i];
12279
+ for (var _i3 = 0; _i3 < referencedFrameIndicies.length; _i3++) {
12280
+ var frame = referencedFrameIndicies[_i3];
12272
12281
  labelmaps.push(labelmaps2D[frame].pixelData);
12273
12282
  }
12274
12283
 
@@ -12294,8 +12303,8 @@ function _createSegFromImages$1(images, isMultiframe, options) {
12294
12303
  dataset._meta = DicomMetaDictionary.namifyDataset(dicomData.meta);
12295
12304
  datasets.push(dataset);
12296
12305
  } else {
12297
- for (var i = 0; i < images.length; i++) {
12298
- var _image = images[i];
12306
+ for (var _i4 = 0; _i4 < images.length; _i4++) {
12307
+ var _image = images[_i4];
12299
12308
  var _arrayBuffer = _image.data.byteArray.buffer;
12300
12309
 
12301
12310
  var _dicomData = DicomMessage.readFile(_arrayBuffer);
@@ -12317,7 +12326,8 @@ function _createSegFromImages$1(images, isMultiframe, options) {
12317
12326
  * @param {string[]} imageIds - An array of the imageIds.
12318
12327
  * @param {ArrayBuffer} arrayBuffer - The SEG arrayBuffer.
12319
12328
  * @param {*} metadataProvider.
12320
- * @param {number} tolerance - checks tolerance, default value 1.e-3.
12329
+ * @param {bool} skipOverlapping - skip checks for overlapping segs, default value false.
12330
+ * @param {number} tolerance - default value 1.e-3.
12321
12331
  *
12322
12332
  * @return {[]ArrayBuffer}a list of array buffer for each labelMap
12323
12333
  * @return {Object} an object from which the segment metadata can be derived
@@ -12415,17 +12425,40 @@ function generateToolState$1(imageIds, arrayBuffer, metadataProvider) {
12415
12425
  };
12416
12426
  }
12417
12427
  /**
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
- *
12428
+ * Find the reference frame of the segmentation frame in the source data.
12429
+ * @returns {string} Returns the imageId
12430
+ */
12431
+
12432
+
12433
+ function findReferenceSourceImageId(SourceImageSequence, imageIds, metadataProvider, PerFrameFunctionalGroups, frameOfReferenceUID, tolerance) {
12434
+ var imageId = getImageIdOfSourceImage$1(SourceImageSequence, imageIds, metadataProvider);
12435
+
12436
+ if (!imageId) {
12437
+ /** not found, we can do a check with the PlanePositionSequence.
12438
+ * (WARNING!) However, if more than a source series is present,
12439
+ * this logic can find the wrong frame
12440
+ * (i.e. two source series, with the same frameOfReferenceUID,
12441
+ * that have each a frame with the same ImagePositionPatient of PlanePositionSequence).
12442
+ */
12443
+ if (PerFrameFunctionalGroups.PlanePositionSequence !== undefined && PerFrameFunctionalGroups.PlanePositionSequence[0] !== undefined && PerFrameFunctionalGroups.PlanePositionSequence[0].ImagePositionPatient !== undefined) {
12444
+ for (var _i5 = 0; _i5 < imageIds.length; ++_i5) {
12445
+ var sourceImageMetadata = cornerstone.metaData.get("instance", imageIds[_i5]);
12446
+
12447
+ if (sourceImageMetadata === undefined || sourceImageMetadata.ImagePositionPatient === undefined || sourceImageMetadata.FrameOfReferenceUID !== frameOfReferenceUID) {
12448
+ continue;
12449
+ }
12450
+
12451
+ if (compareArrays(PerFrameFunctionalGroups.PlanePositionSequence[0].ImagePositionPatient, sourceImageMetadata.ImagePositionPatient, tolerance)) {
12452
+ return imageIds[_i5];
12453
+ }
12454
+ }
12455
+ }
12456
+ }
12457
+
12458
+ return imageId;
12459
+ }
12460
+ /**
12461
+ * Checks if there is any overlapping segmentations.
12429
12462
  * @returns {boolean} Returns a flag if segmentations overlapping
12430
12463
  */
12431
12464
 
@@ -12433,135 +12466,126 @@ function generateToolState$1(imageIds, arrayBuffer, metadataProvider) {
12433
12466
  function checkSEGsOverlapping(pixelData, multiframe, imageIds, validOrientations, metadataProvider, tolerance) {
12434
12467
  var SharedFunctionalGroupsSequence = multiframe.SharedFunctionalGroupsSequence,
12435
12468
  PerFrameFunctionalGroupsSequence = multiframe.PerFrameFunctionalGroupsSequence,
12469
+ SegmentSequence = multiframe.SegmentSequence,
12436
12470
  Rows = multiframe.Rows,
12437
12471
  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;
12472
+ var numberOfSegs = SegmentSequence.length;
12446
12473
 
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.");
12474
+ if (numberOfSegs < 2) {
12449
12475
  return false;
12450
12476
  }
12451
12477
 
12452
- var refSegFrame0 = getSegmentIndex(multiframe, 0);
12453
- var refSegFrame1 = getSegmentIndex(multiframe, 1);
12454
-
12455
- if (refSegFrame0 === undefined || refSegFrame1 === undefined || refSegFrame0 !== refSegFrame1) {
12456
- console.warn("Failed to check for overlap of segments: frames in PerFrameFunctionalGroupsSequence are not sorted.");
12457
- return false;
12458
- }
12478
+ var sharedImageOrientationPatient = SharedFunctionalGroupsSequence.PlaneOrientationSequence ? SharedFunctionalGroupsSequence.PlaneOrientationSequence.ImageOrientationPatient : undefined;
12479
+ var sliceLength = Columns * Rows;
12480
+ var groupsLen = PerFrameFunctionalGroupsSequence.length;
12481
+ /** sort groupsLen to have all the segments for each frame in an array
12482
+ * frame 2 : 1, 2
12483
+ * frame 4 : 1, 3
12484
+ * frame 5 : 4
12485
+ */
12459
12486
 
12460
- var i = 0;
12487
+ var frameSegmentsMapping = new Map();
12461
12488
 
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);
12467
-
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
- }
12474
-
12475
- var segmentIndex = getSegmentIndex(multiframe, i);
12489
+ var _loop3 = function _loop3(frameSegment) {
12490
+ var PerFrameFunctionalGroups = PerFrameFunctionalGroupsSequence[frameSegment];
12491
+ var segmentIndex = getSegmentIndex(multiframe, frameSegment);
12476
12492
 
12477
12493
  if (segmentIndex === undefined) {
12478
- console.warn("Could not retrieve the segment index, skipping this frame.");
12479
- return {
12480
- v: false
12481
- };
12494
+ console.warn("Could not retrieve the segment index for frame segment " + frameSegment + ", skipping this frame.");
12495
+ return "continue";
12482
12496
  }
12483
12497
 
12484
12498
  var SourceImageSequence = void 0;
12485
12499
 
12486
12500
  if (multiframe.SourceImageSequence) {
12487
- SourceImageSequence = multiframe.SourceImageSequence[i];
12501
+ SourceImageSequence = multiframe.SourceImageSequence[frameSegment];
12488
12502
  } else {
12489
12503
  SourceImageSequence = PerFrameFunctionalGroups.DerivationImageSequence.SourceImageSequence;
12490
12504
  }
12491
12505
 
12492
12506
  if (!SourceImageSequence) {
12493
12507
  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
- };
12508
+ return "continue";
12497
12509
  }
12498
12510
 
12499
- var imageId = getImageIdOfSourceImage$1(SourceImageSequence, imageIds, metadataProvider);
12511
+ var imageId = findReferenceSourceImageId(SourceImageSequence, imageIds, metadataProvider, PerFrameFunctionalGroups, multiframe.FrameOfReferenceUID, tolerance);
12500
12512
 
12501
12513
  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
-
12514
+ console.warn("Image not present in stack, can't import frame : " + i + ".");
12513
12515
  return "continue";
12514
12516
  }
12515
12517
 
12516
- var data = alignedPixelDataI.data;
12517
12518
  var imageIdIndex = imageIds.findIndex(function (element) {
12518
12519
  return element === imageId;
12519
12520
  });
12520
12521
 
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]++;
12522
+ if (frameSegmentsMapping.has(imageIdIndex)) {
12523
+ var segmentArray = frameSegmentsMapping.get(imageIdIndex);
12533
12524
 
12534
- if (temp2DArray[j] > 1) {
12535
- return {
12536
- v: true
12537
- };
12538
- }
12525
+ if (!segmentArray.includes(frameSegment)) {
12526
+ segmentArray.push(frameSegment);
12527
+ frameSegmentsMapping.set(imageIdIndex, segmentArray);
12539
12528
  }
12529
+ } else {
12530
+ frameSegmentsMapping.set(imageIdIndex, [frameSegment]);
12540
12531
  }
12532
+ };
12541
12533
 
12542
- i = i + numberOfFrames;
12534
+ for (var frameSegment = 0; frameSegment < groupsLen; ++frameSegment) {
12535
+ var _ret = _loop3(frameSegment);
12543
12536
 
12544
- if (i >= groupsLen) {
12545
- i = i - numberOfFrames * numberOfSegs + 1;
12537
+ if (_ret === "continue") continue;
12538
+ }
12546
12539
 
12547
- if (i >= numberOfFrames) {
12548
- return "break";
12549
- }
12550
- }
12551
- };
12540
+ var _iteratorNormalCompletion = true;
12541
+ var _didIteratorError = false;
12542
+ var _iteratorError = undefined;
12552
12543
 
12553
- _loop3: while (i < groupsLen) {
12554
- var _ret = _loop4();
12544
+ try {
12545
+ for (var _iterator = frameSegmentsMapping.entries()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
12546
+ var _step$value = _slicedToArray(_step.value, 2),
12547
+ user = _step$value[0],
12548
+ role = _step$value[1];
12549
+
12550
+ var temp2DArray = new Uint16Array(sliceLength).fill(0);
12551
+
12552
+ for (var _i6 = 0; _i6 < role.length; ++_i6) {
12553
+ var _frameSegment = role[_i6];
12554
+ var PerFrameFunctionalGroups = PerFrameFunctionalGroupsSequence[_frameSegment];
12555
+ var ImageOrientationPatientI = sharedImageOrientationPatient || PerFrameFunctionalGroups.PlaneOrientationSequence.ImageOrientationPatient;
12556
+ var pixelDataI2D = ndarray(new Uint8Array(pixelData.buffer, _frameSegment * sliceLength, sliceLength), [Rows, Columns]);
12557
+ var alignedPixelDataI = alignPixelDataWithSourceData$1(pixelDataI2D, ImageOrientationPatientI, validOrientations, tolerance);
12558
+
12559
+ if (!alignedPixelDataI) {
12560
+ console.warn("Individual SEG frames are out of plane with respect to the first SEG frame, this is not yet supported, skipping this frame.");
12561
+ continue;
12562
+ }
12555
12563
 
12556
- switch (_ret) {
12557
- case "break":
12558
- break _loop3;
12564
+ var data = alignedPixelDataI.data;
12559
12565
 
12560
- case "continue":
12561
- continue;
12566
+ for (var j = 0, len = data.length; j < len; ++j) {
12567
+ if (data[j] !== 0) {
12568
+ temp2DArray[j]++;
12562
12569
 
12563
- default:
12564
- if (_typeof(_ret) === "object") return _ret.v;
12570
+ if (temp2DArray[j] > 1) {
12571
+ return true;
12572
+ }
12573
+ }
12574
+ }
12575
+ }
12576
+ }
12577
+ } catch (err) {
12578
+ _didIteratorError = true;
12579
+ _iteratorError = err;
12580
+ } finally {
12581
+ try {
12582
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
12583
+ _iterator.return();
12584
+ }
12585
+ } finally {
12586
+ if (_didIteratorError) {
12587
+ throw _iteratorError;
12588
+ }
12565
12589
  }
12566
12590
  }
12567
12591
 
@@ -12585,42 +12609,41 @@ function insertOverlappingPixelDataPlanar(segmentsOnFrame, segmentsOnFrameArray,
12585
12609
  var tempBuffer = labelmapBufferArray[m].slice(0); // temp list for checking overlaps
12586
12610
 
12587
12611
  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
- */
12612
+ /** split overlapping SEGs algorithm for each segment:
12613
+ * A) copy the labelmapBuffer in the array with index 0
12614
+ * B) add the segment pixel per pixel on the copied buffer from (A)
12615
+ * C) if no overlap, copy the results back on the orignal array from (A)
12616
+ * D) if overlap, repeat increasing the index m up to M (if out of memory, add new buffer in the array and M++);
12617
+ */
12594
12618
 
12595
12619
  var numberOfSegs = multiframe.SegmentSequence.length;
12596
12620
 
12597
12621
  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);
12622
+ var _loop4 = function _loop4(groupsLen, _i8) {
12623
+ var PerFrameFunctionalGroups = PerFrameFunctionalGroupsSequence[_i8];
12624
+ var segmentIndex = getSegmentIndex(multiframe, _i8);
12601
12625
 
12602
12626
  if (segmentIndex === undefined) {
12603
12627
  throw new Error("Could not retrieve the segment index. Aborting segmentation loading.");
12604
12628
  }
12605
12629
 
12606
12630
  if (segmentIndex !== segmentIndexToProcess) {
12607
- i = _i2;
12631
+ _i7 = _i8;
12608
12632
  return "continue";
12609
12633
  }
12610
12634
 
12611
12635
  var ImageOrientationPatientI = sharedImageOrientationPatient || PerFrameFunctionalGroups.PlaneOrientationSequence.ImageOrientationPatient;
12612
- var pixelDataI2D = ndarray(new Uint8Array(pixelData.buffer, _i2 * sliceLength, sliceLength), [Rows, Columns]);
12636
+ var pixelDataI2D = ndarray(new Uint8Array(pixelData.buffer, _i8 * sliceLength, sliceLength), [Rows, Columns]);
12613
12637
  var alignedPixelDataI = alignPixelDataWithSourceData$1(pixelDataI2D, ImageOrientationPatientI, validOrientations, tolerance);
12614
12638
 
12615
12639
  if (!alignedPixelDataI) {
12616
12640
  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
12641
  }
12618
12642
 
12619
- var imageId = undefined;
12620
12643
  var SourceImageSequence = undefined;
12621
12644
 
12622
12645
  if (multiframe.SourceImageSequence) {
12623
- SourceImageSequence = multiframe.SourceImageSequence[_i2];
12646
+ SourceImageSequence = multiframe.SourceImageSequence[_i8];
12624
12647
  } else {
12625
12648
  SourceImageSequence = PerFrameFunctionalGroups.DerivationImageSequence.SourceImageSequence;
12626
12649
  }
@@ -12629,11 +12652,11 @@ function insertOverlappingPixelDataPlanar(segmentsOnFrame, segmentsOnFrameArray,
12629
12652
  throw new Error("Source Image Sequence information missing: individual SEG frames are out of plane. " + "This is not yet supported. Aborting segmentation loading.");
12630
12653
  }
12631
12654
 
12632
- imageId = getImageIdOfSourceImage$1(SourceImageSequence, imageIds, metadataProvider);
12655
+ var imageId = findReferenceSourceImageId(SourceImageSequence, imageIds, metadataProvider, PerFrameFunctionalGroups, multiframe.FrameOfReferenceUID, tolerance);
12633
12656
 
12634
12657
  if (!imageId) {
12635
- i = _i2;
12636
- // Image not present in stack, can't import this frame.
12658
+ console.warn("Image not present in stack, can't import frame : " + _i8 + ".");
12659
+ _i7 = _i8;
12637
12660
  return "continue";
12638
12661
  }
12639
12662
 
@@ -12665,7 +12688,7 @@ function insertOverlappingPixelDataPlanar(segmentsOnFrame, segmentsOnFrameArray,
12665
12688
 
12666
12689
  tempBuffer = labelmapBufferArray[m].slice(0);
12667
12690
  tempSegmentsOnFrame = lodash_clonedeep(segmentsOnFrameArray[m]);
12668
- _i2 = 0;
12691
+ _i8 = 0;
12669
12692
  break;
12670
12693
  } else {
12671
12694
  labelmap2DView[j] = segmentIndex;
@@ -12688,11 +12711,11 @@ function insertOverlappingPixelDataPlanar(segmentsOnFrame, segmentsOnFrameArray,
12688
12711
  segmentsOnFrame[imageIdIndex].push(segmentIndex);
12689
12712
  }
12690
12713
 
12691
- i = _i2;
12714
+ _i7 = _i8;
12692
12715
  };
12693
12716
 
12694
- for (var i = 0, groupsLen = PerFrameFunctionalGroupsSequence.length; i < groupsLen; ++i) {
12695
- var _ret2 = _loop5(i, groupsLen);
12717
+ for (var _i7 = 0, groupsLen = PerFrameFunctionalGroupsSequence.length; _i7 < groupsLen; ++_i7) {
12718
+ var _ret2 = _loop4(groupsLen, _i7);
12696
12719
 
12697
12720
  if (_ret2 === "continue") continue;
12698
12721
  }
@@ -12721,27 +12744,26 @@ function insertPixelDataPlanar(segmentsOnFrame, segmentsOnFrameArray, labelmapBu
12721
12744
  var sharedImageOrientationPatient = SharedFunctionalGroupsSequence.PlaneOrientationSequence ? SharedFunctionalGroupsSequence.PlaneOrientationSequence.ImageOrientationPatient : undefined;
12722
12745
  var sliceLength = Columns * Rows;
12723
12746
 
12724
- var _loop6 = function _loop6(groupsLen, _i3) {
12725
- var PerFrameFunctionalGroups = PerFrameFunctionalGroupsSequence[_i3];
12747
+ var _loop5 = function _loop5(groupsLen, _i9) {
12748
+ var PerFrameFunctionalGroups = PerFrameFunctionalGroupsSequence[_i9];
12726
12749
  var ImageOrientationPatientI = sharedImageOrientationPatient || PerFrameFunctionalGroups.PlaneOrientationSequence.ImageOrientationPatient;
12727
- var pixelDataI2D = ndarray(new Uint8Array(pixelData.buffer, _i3 * sliceLength, sliceLength), [Rows, Columns]);
12750
+ var pixelDataI2D = ndarray(new Uint8Array(pixelData.buffer, _i9 * sliceLength, sliceLength), [Rows, Columns]);
12728
12751
  var alignedPixelDataI = alignPixelDataWithSourceData$1(pixelDataI2D, ImageOrientationPatientI, validOrientations, tolerance);
12729
12752
 
12730
12753
  if (!alignedPixelDataI) {
12731
12754
  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
12755
  }
12733
12756
 
12734
- var segmentIndex = getSegmentIndex(multiframe, _i3);
12757
+ var segmentIndex = getSegmentIndex(multiframe, _i9);
12735
12758
 
12736
12759
  if (segmentIndex === undefined) {
12737
12760
  throw new Error("Could not retrieve the segment index. Aborting segmentation loading.");
12738
12761
  }
12739
12762
 
12740
- var imageId = undefined;
12741
12763
  var SourceImageSequence = undefined;
12742
12764
 
12743
12765
  if (multiframe.SourceImageSequence) {
12744
- SourceImageSequence = multiframe.SourceImageSequence[_i3];
12766
+ SourceImageSequence = multiframe.SourceImageSequence[_i9];
12745
12767
  } else {
12746
12768
  SourceImageSequence = PerFrameFunctionalGroups.DerivationImageSequence.SourceImageSequence;
12747
12769
  }
@@ -12750,10 +12772,10 @@ function insertPixelDataPlanar(segmentsOnFrame, segmentsOnFrameArray, labelmapBu
12750
12772
  throw new Error("Source Image Sequence information missing: individual SEG frames are out of plane. " + "This is not yet supported. Aborting segmentation loading.");
12751
12773
  }
12752
12774
 
12753
- imageId = getImageIdOfSourceImage$1(SourceImageSequence, imageIds, metadataProvider);
12775
+ var imageId = findReferenceSourceImageId(SourceImageSequence, imageIds, metadataProvider, PerFrameFunctionalGroups, multiframe.FrameOfReferenceUID, tolerance);
12754
12776
 
12755
12777
  if (!imageId) {
12756
- // Image not present in stack, can't import this frame.
12778
+ console.warn("Image not present in stack, can't import frame : " + _i9 + ".");
12757
12779
  return "continue";
12758
12780
  }
12759
12781
 
@@ -12769,7 +12791,7 @@ function insertPixelDataPlanar(segmentsOnFrame, segmentsOnFrameArray, labelmapBu
12769
12791
  var byteOffset = sliceLength * 2 * imageIdIndex; // 2 bytes/pixel
12770
12792
 
12771
12793
  var labelmap2DView = new Uint16Array(labelmapBufferArray[0], byteOffset, sliceLength);
12772
- var data = alignedPixelDataI.data; //
12794
+ var data = alignedPixelDataI.data;
12773
12795
 
12774
12796
  for (var j = 0, len = alignedPixelDataI.data.length; j < len; ++j) {
12775
12797
  if (data[j]) {
@@ -12789,8 +12811,8 @@ function insertPixelDataPlanar(segmentsOnFrame, segmentsOnFrameArray, labelmapBu
12789
12811
  }
12790
12812
  };
12791
12813
 
12792
- for (var _i3 = 0, groupsLen = PerFrameFunctionalGroupsSequence.length; _i3 < groupsLen; ++_i3) {
12793
- var _ret3 = _loop6(groupsLen, _i3);
12814
+ for (var _i9 = 0, groupsLen = PerFrameFunctionalGroupsSequence.length; _i9 < groupsLen; ++_i9) {
12815
+ var _ret3 = _loop5(groupsLen, _i9);
12794
12816
 
12795
12817
  if (_ret3 === "continue") continue;
12796
12818
  }
@@ -12804,7 +12826,7 @@ function checkOrientation(multiframe, validOrientations, sourceDataDimensions, t
12804
12826
  var PerFrameFunctionalGroups = PerFrameFunctionalGroupsSequence[0];
12805
12827
  var iop = sharedImageOrientationPatient || PerFrameFunctionalGroups.PlaneOrientationSequence.ImageOrientationPatient;
12806
12828
  var inPlane = validOrientations.some(function (operation) {
12807
- return compareIOP$1(iop, operation, tolerance);
12829
+ return compareArrays(iop, operation, tolerance);
12808
12830
  });
12809
12831
 
12810
12832
  if (inPlane) {
@@ -12844,12 +12866,23 @@ function checkIfPerpendicular(iop1, iop2, tolerance) {
12844
12866
 
12845
12867
  function unpackPixelData$1(multiframe) {
12846
12868
  var segType = multiframe.SegmentationType;
12869
+ var data;
12870
+
12871
+ if (Array.isArray(multiframe.PixelData)) {
12872
+ data = multiframe.PixelData[0];
12873
+ } else {
12874
+ data = multiframe.PixelData;
12875
+ }
12876
+
12877
+ if (data === undefined) {
12878
+ log.error("This segmentation pixeldata is undefined.");
12879
+ }
12847
12880
 
12848
12881
  if (segType === "BINARY") {
12849
- return BitArray.unpack(multiframe.PixelData);
12882
+ return BitArray.unpack(data);
12850
12883
  }
12851
12884
 
12852
- var pixelData = new Uint8Array(multiframe.PixelData);
12885
+ var pixelData = new Uint8Array(data);
12853
12886
  var max = multiframe.MaximumFractionalValue;
12854
12887
  var onlyMaxAndZero = pixelData.find(function (element) {
12855
12888
  return element !== 0 && element !== max;
@@ -12966,50 +12999,60 @@ function getValidOrientations$1(iop) {
12966
12999
 
12967
13000
 
12968
13001
  function alignPixelDataWithSourceData$1(pixelData2D, iop, orientations, tolerance) {
12969
- if (compareIOP$1(iop, orientations[0], tolerance)) {
13002
+ if (compareArrays(iop, orientations[0], tolerance)) {
12970
13003
  return pixelData2D;
12971
- } else if (compareIOP$1(iop, orientations[1], tolerance)) {
13004
+ } else if (compareArrays(iop, orientations[1], tolerance)) {
12972
13005
  // Flipped vertically.
12973
13006
  // Undo Flip
12974
13007
  return flipMatrix2D.v(pixelData2D);
12975
- } else if (compareIOP$1(iop, orientations[2], tolerance)) {
13008
+ } else if (compareArrays(iop, orientations[2], tolerance)) {
12976
13009
  // Flipped horizontally.
12977
13010
  // Unfo flip
12978
13011
  return flipMatrix2D.h(pixelData2D);
12979
- } else if (compareIOP$1(iop, orientations[3], tolerance)) {
13012
+ } else if (compareArrays(iop, orientations[3], tolerance)) {
12980
13013
  //Rotated 90 degrees
12981
13014
  // Rotate back
12982
13015
  return rotateMatrix902D(pixelData2D);
12983
- } else if (compareIOP$1(iop, orientations[4], tolerance)) {
13016
+ } else if (compareArrays(iop, orientations[4], tolerance)) {
12984
13017
  //Rotated 90 degrees and fliped horizontally.
12985
13018
  // Undo flip and rotate back.
12986
13019
  return rotateMatrix902D(flipMatrix2D.h(pixelData2D));
12987
- } else if (compareIOP$1(iop, orientations[5], tolerance)) {
13020
+ } else if (compareArrays(iop, orientations[5], tolerance)) {
12988
13021
  // Rotated 90 degrees and fliped vertically
12989
13022
  // Unfo flip and rotate back.
12990
13023
  return rotateMatrix902D(flipMatrix2D.v(pixelData2D));
12991
- } else if (compareIOP$1(iop, orientations[6], tolerance)) {
13024
+ } else if (compareArrays(iop, orientations[6], tolerance)) {
12992
13025
  // Rotated 180 degrees. // TODO -> Do this more effeciently, there is a 1:1 mapping like 90 degree rotation.
12993
13026
  return rotateMatrix902D(rotateMatrix902D(pixelData2D));
12994
- } else if (compareIOP$1(iop, orientations[7], tolerance)) {
13027
+ } else if (compareArrays(iop, orientations[7], tolerance)) {
12995
13028
  // Rotated 270 degrees
12996
13029
  // Rotate back.
12997
13030
  return rotateMatrix902D(rotateMatrix902D(rotateMatrix902D(pixelData2D)));
12998
13031
  }
12999
13032
  }
13000
13033
  /**
13001
- * compareIOP - Returns true if iop1 and iop2 are equal
13034
+ * compareArrays - Returns true if array1 and array2 are equal
13002
13035
  * within a tolerance.
13003
13036
  *
13004
- * @param {Number[6]} iop1 - An ImageOrientationPatient array.
13005
- * @param {Number[6]} iop2 - An ImageOrientationPatient array.
13037
+ * @param {Number[]} array1 - An array.
13038
+ * @param {Number[]} array2 - An array.
13006
13039
  * @param {Number} tolerance.
13007
- * @return {Boolean} True if iop1 and iop2 are equal.
13040
+ * @return {Boolean} True if array1 and array2 are equal.
13008
13041
  */
13009
13042
 
13010
13043
 
13011
- function compareIOP$1(iop1, iop2, tolerance) {
13012
- 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);
13044
+ function compareArrays(array1, array2, tolerance) {
13045
+ if (array1.length != array2.length) {
13046
+ return false;
13047
+ }
13048
+
13049
+ for (var _i10 = 0; _i10 < array1.length; ++_i10) {
13050
+ if (!nearlyEqual(array1[_i10], array2[_i10], tolerance)) {
13051
+ return false;
13052
+ }
13053
+ }
13054
+
13055
+ return true;
13013
13056
  }
13014
13057
 
13015
13058
  function getSegmentMetadata$1(multiframe, seriesInstanceUid) {
@@ -13067,6 +13110,10 @@ function generateSegmentation$2(images, labelmaps3DorBrushData) {
13067
13110
  * @param {string[]} imageIds An array of the imageIds.
13068
13111
  * @param {ArrayBuffer} arrayBuffer The SEG arrayBuffer.
13069
13112
  * @param {*} metadataProvider
13113
+ * @param {bool} skipOverlapping - skip checks for overlapping segs, default value false.
13114
+ * @param {number} tolerance - default value 1.e-3.
13115
+ * @param {number} cornerstoneToolsVersion - default value 4.
13116
+ *
13070
13117
  * @returns {Object} The toolState and an object from which the
13071
13118
  * segment metadata can be derived.
13072
13119
  */
@@ -13074,14 +13121,15 @@ function generateSegmentation$2(images, labelmaps3DorBrushData) {
13074
13121
 
13075
13122
  function generateToolState$2(imageIds, arrayBuffer, metadataProvider) {
13076
13123
  var skipOverlapping = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
13077
- var cornerstoneToolsVersion = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 4;
13124
+ var tolerance = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1e-3;
13125
+ var cornerstoneToolsVersion = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 4;
13078
13126
 
13079
13127
  if (cornerstoneToolsVersion === 4) {
13080
- return Segmentation$2.generateToolState(imageIds, arrayBuffer, metadataProvider, skipOverlapping);
13128
+ return Segmentation$2.generateToolState(imageIds, arrayBuffer, metadataProvider, skipOverlapping, tolerance);
13081
13129
  }
13082
13130
 
13083
13131
  if (cornerstoneToolsVersion === 3) {
13084
- return Segmentation$1.generateToolState(imageIds, arrayBuffer, metadataProvider, skipOverlapping);
13132
+ return Segmentation$1.generateToolState(imageIds, arrayBuffer, metadataProvider);
13085
13133
  }
13086
13134
 
13087
13135
  console.warn("No generateToolState adapater for cornerstone version ".concat(cornerstoneToolsVersion, ", exiting."));