dwv 0.31.0-beta.10 → 0.31.0-beta.13

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/dist/dwv.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! dwv 0.31.0-beta.10 2022-08-03 14:54:29 */
1
+ /*! dwv 0.31.0-beta.13 2022-09-07 12:27:46 */
2
2
  // Inspired from umdjs
3
3
  // See https://github.com/umdjs/umd/blob/master/templates/returnExports.js
4
4
  (function (root, factory) {
@@ -80,6 +80,17 @@ var dwv = dwv || {};
80
80
  *
81
81
  * @class
82
82
  * @tutorial examples
83
+ * @example
84
+ * // create the dwv app
85
+ * var app = new dwv.App();
86
+ * // initialise
87
+ * app.init({
88
+ * dataViewConfigs: {'*': [{divId: 'layerGroup0'}]}
89
+ * });
90
+ * // load dicom data
91
+ * app.loadURLs([
92
+ * 'https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323851.dcm'
93
+ * ]);
83
94
  */
84
95
  dwv.App = function () {
85
96
  // closure to self
@@ -357,6 +368,31 @@ dwv.App = function () {
357
368
  * after the first loaded data or not
358
369
  * - `defaultCharacterSet`: the default chraracter set string used for DICOM
359
370
  * parsing
371
+ * @example
372
+ * // create the dwv app
373
+ * var app = new dwv.App();
374
+ * // initialise
375
+ * app.init({
376
+ * dataViewConfigs: {'*': [{divId: 'layerGroup0'}]},
377
+ * viewOnFirstLoadItem: false
378
+ * });
379
+ * // render button
380
+ * var button = document.createElement('button');
381
+ * button.id = 'render';
382
+ * button.appendChild(document.createTextNode('render'));
383
+ * document.body.appendChild(button);
384
+ * app.addEventListener('load', function () {
385
+ * var button = document.getElementById('render');
386
+ * button.disabled = false;
387
+ * button.onclick = function () {
388
+ * // render data #0
389
+ * app.render(0);
390
+ * };
391
+ * });
392
+ * // load dicom data
393
+ * app.loadURLs([
394
+ * 'https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323851.dcm'
395
+ * ]);
360
396
  */
361
397
  this.init = function (opt) {
362
398
  // store
@@ -4533,7 +4569,7 @@ dwv.dicom = dwv.dicom || {};
4533
4569
  * @returns {string} The version of the library.
4534
4570
  */
4535
4571
  dwv.getVersion = function () {
4536
- return '0.31.0-beta.10';
4572
+ return '0.31.0-beta.13';
4537
4573
  };
4538
4574
 
4539
4575
  /**
@@ -18256,6 +18292,508 @@ dwv.image.lut.test = {
18256
18292
  var dwv = dwv || {};
18257
18293
  dwv.image = dwv.image || {};
18258
18294
 
18295
+ /**
18296
+ * Check two position patients for equality.
18297
+ *
18298
+ * @param {*} pos1 The first position patient.
18299
+ * @param {*} pos2 The second position patient.
18300
+ * @returns {boolean} True is equal.
18301
+ */
18302
+ dwv.dicom.equalPosPat = function (pos1, pos2) {
18303
+ return JSON.stringify(pos1) === JSON.stringify(pos2);
18304
+ };
18305
+
18306
+ /**
18307
+ * Compare two position patients.
18308
+ *
18309
+ * @param {*} pos1 The first position patient.
18310
+ * @param {*} pos2 The second position patient.
18311
+ * @returns {number|null} A number used to sort elements.
18312
+ */
18313
+ dwv.dicom.comparePosPat = function (pos1, pos2) {
18314
+ var diff = null;
18315
+ var posLen = pos1.length;
18316
+ var index = posLen;
18317
+ for (var i = 0; i < posLen; ++i) {
18318
+ --index;
18319
+ diff = pos2[index] - pos1[index];
18320
+ if (diff !== 0) {
18321
+ return diff;
18322
+ }
18323
+ }
18324
+ return diff;
18325
+ };
18326
+
18327
+ /**
18328
+ * Check that a DICOM tag definition is present in a parsed element.
18329
+ *
18330
+ * @param {object} rootElement The root dicom element.
18331
+ * @param {object} tagDefinition The tag definition as {name, tag, type, enum}.
18332
+ */
18333
+ dwv.dicom.checkTag = function (rootElement, tagDefinition) {
18334
+ var tagValue = rootElement.getFromKey(tagDefinition.tag);
18335
+ // check null and undefined
18336
+ if (tagDefinition.type === 1 || tagDefinition.type === 2) {
18337
+ if (tagValue === null || typeof tagValue === 'undefined') {
18338
+ throw new Error('Missing or empty ' + tagDefinition.name);
18339
+ }
18340
+ } else {
18341
+ if (tagValue === null || typeof tagValue === 'undefined') {
18342
+ // non mandatory value, exit
18343
+ return;
18344
+ }
18345
+ }
18346
+ var includes = false;
18347
+ if (Array.isArray(tagValue)) {
18348
+ // trim
18349
+ tagValue = tagValue.map(function (item) {
18350
+ return dwv.dicom.cleanString(item);
18351
+ });
18352
+ for (var i = 0; i < tagDefinition.enum.length; ++i) {
18353
+ if (!Array.isArray(tagDefinition.enum[i])) {
18354
+ throw new Error('Cannot compare array and non array tag value.');
18355
+ }
18356
+ if (dwv.utils.arrayEquals(tagDefinition.enum[i], tagValue)) {
18357
+ includes = true;
18358
+ break;
18359
+ }
18360
+ }
18361
+ } else {
18362
+ // trim
18363
+ if (typeof tagValue === 'string') {
18364
+ tagValue = dwv.dicom.cleanString(tagValue);
18365
+ }
18366
+
18367
+ includes = tagDefinition.enum.includes(tagValue);
18368
+ }
18369
+ if (!includes) {
18370
+ throw new Error(
18371
+ 'Unsupported ' + tagDefinition.name + ' value: ' + tagValue);
18372
+ }
18373
+ };
18374
+
18375
+ /**
18376
+ * List of DICOM Seg required tags.
18377
+ */
18378
+ dwv.dicom.requiredSegDicomTags = [
18379
+ {
18380
+ name: 'Transfer Syntax UID',
18381
+ tag: 'x00020010',
18382
+ type: '1',
18383
+ enum: ['1.2.840.10008.1.2.1']
18384
+ },
18385
+ {
18386
+ name: 'Media Storage SOP Class UID',
18387
+ tag: 'x00020002',
18388
+ type: '1',
18389
+ enum: ['1.2.840.10008.5.1.4.1.1.66.4']
18390
+ },
18391
+ {
18392
+ name: 'SOP Class UID',
18393
+ tag: 'x00020002',
18394
+ type: '1',
18395
+ enum: ['1.2.840.10008.5.1.4.1.1.66.4']
18396
+ },
18397
+ {
18398
+ name: 'Modality',
18399
+ tag: 'x00080060',
18400
+ type: '1',
18401
+ enum: ['SEG']
18402
+ },
18403
+ {
18404
+ name: 'Segmentation Type',
18405
+ tag: 'x00620001',
18406
+ type: '1',
18407
+ enum: ['BINARY']
18408
+ },
18409
+ {
18410
+ name: 'Dimension Organization Type',
18411
+ tag: 'x00209311',
18412
+ type: '3',
18413
+ enum: ['3D']
18414
+ },
18415
+ {
18416
+ name: 'Image Type',
18417
+ tag: 'x00080008',
18418
+ type: '1',
18419
+ enum: [['DERIVED', 'PRIMARY']]
18420
+ },
18421
+ {
18422
+ name: 'Samples Per Pixel',
18423
+ tag: 'x00280002',
18424
+ type: '1',
18425
+ enum: [1]
18426
+ },
18427
+ {
18428
+ name: 'Photometric Interpretation',
18429
+ tag: 'x00280004',
18430
+ type: '1',
18431
+ enum: ['MONOCHROME2']
18432
+ },
18433
+ {
18434
+ name: 'Pixel Representation',
18435
+ tag: 'x00280103',
18436
+ type: '1',
18437
+ enum: [0]
18438
+ },
18439
+ {
18440
+ name: 'Bits Allocated',
18441
+ tag: 'x00280100',
18442
+ type: '1',
18443
+ enum: [1]
18444
+ },
18445
+ {
18446
+ name: 'Bits Stored',
18447
+ tag: 'x00280101',
18448
+ type: '1',
18449
+ enum: [1]
18450
+ },
18451
+ {
18452
+ name: 'High Bit',
18453
+ tag: 'x00280102',
18454
+ type: '1',
18455
+ enum: [0]
18456
+ },
18457
+ ];
18458
+
18459
+ /**
18460
+ * Check the dimension organization from a dicom element.
18461
+ *
18462
+ * @param {object} rootElement The root dicom element.
18463
+ * @returns {object} The dimension organizations and indices.
18464
+ */
18465
+ dwv.dicom.getDimensionOrganization = function (rootElement) {
18466
+ // Dimension Organization Sequence (required)
18467
+ var orgSq = rootElement.getFromKey('x00209221', true);
18468
+ if (!orgSq || orgSq.length !== 1) {
18469
+ throw new Error('Unsupported dimension organization sequence length');
18470
+ }
18471
+ // Dimension Organization UID
18472
+ var orgs = [dwv.dicom.cleanString(orgSq[0].x00209164.value[0])];
18473
+
18474
+ // Dimension Index Sequence (conditionally required)
18475
+ var indices = [];
18476
+ var indexSq = rootElement.getFromKey('x00209222', true);
18477
+ if (indexSq) {
18478
+ // expecting 2D index
18479
+ if (indexSq.length !== 2) {
18480
+ throw new Error('Unsupported dimension index sequence length');
18481
+ }
18482
+ for (var i = 0; i < indexSq.length; ++i) {
18483
+ // Dimension Organization UID (required)
18484
+ var indexOrg = dwv.dicom.cleanString(indexSq[i].x00209164.value[0]);
18485
+ if (indexOrg !== orgs[0]) {
18486
+ throw new Error(
18487
+ 'Dimension Index Sequence contains a unknown Dimension Organization');
18488
+ }
18489
+ // Dimension Index Pointer (required)
18490
+ var indexPointer =
18491
+ dwv.dicom.cleanString(indexSq[i].x00209165.value[0]);
18492
+
18493
+ var index = {
18494
+ organization: indexOrg,
18495
+ pointer: indexPointer
18496
+ };
18497
+ // Dimension Description Label (optional)
18498
+ if (typeof indexSq[i].x00209421 !== 'undefined') {
18499
+ index.label =
18500
+ dwv.dicom.cleanString(indexSq[i].x00209421.value[0]);
18501
+ }
18502
+ // store
18503
+ indices.push(index);
18504
+ }
18505
+ // expecting Image Position at last position
18506
+ if (indices[1].pointer !== '(0020,0032)') {
18507
+ throw new Error('Unsupported non image position as last index');
18508
+ }
18509
+ }
18510
+
18511
+ return {
18512
+ organizations: orgs,
18513
+ indices: indices
18514
+ };
18515
+ };
18516
+
18517
+ /**
18518
+ * Get a code object from a dicom element.
18519
+ *
18520
+ * @param {object} element The dicom element.
18521
+ * @returns {object} A code object.
18522
+ */
18523
+ dwv.dicom.getCode = function (element) {
18524
+ // meaning -> CodeMeaning (type1)
18525
+ var code = {
18526
+ meaning: dwv.dicom.cleanString(element.x00080104.value[0])
18527
+ };
18528
+ // value -> CodeValue (type1C)
18529
+ // longValue -> LongCodeValue (type1C)
18530
+ // urnValue -> URNCodeValue (type1C)
18531
+ if (element.x00080100) {
18532
+ code.value = element.x00080100.value[0];
18533
+ } else if (element.x00080119) {
18534
+ code.longValue = element.x00080119.value[0];
18535
+ } else if (element.x00080120) {
18536
+ code.urnValue = element.x00080120.value[0];
18537
+ } else {
18538
+ throw Error('Invalid code with no value, no long value and no urn value.');
18539
+ }
18540
+ // schemeDesignator -> CodingSchemeDesignator (type1C)
18541
+ if (typeof code.value !== 'undefined' ||
18542
+ typeof code.longValue !== 'undefined') {
18543
+ if (element.x00080102) {
18544
+ code.schemeDesignator = element.x00080102.value[0];
18545
+ } else {
18546
+ throw Error(
18547
+ 'No coding sheme designator when code value or long value is present');
18548
+ }
18549
+ }
18550
+ return code;
18551
+ };
18552
+
18553
+ /**
18554
+ * Get a segment object from a dicom element.
18555
+ *
18556
+ * @param {object} element The dicom element.
18557
+ * @returns {object} A segment object.
18558
+ */
18559
+ dwv.dicom.getSegment = function (element) {
18560
+ // number -> SegmentNumber (type1)
18561
+ // label -> SegmentLabel (type1)
18562
+ // algorithmType -> SegmentAlgorithmType (type1)
18563
+ var segment = {
18564
+ number: element.x00620004.value[0],
18565
+ label: dwv.dicom.cleanString(element.x00620005.value[0]),
18566
+ algorithmType: dwv.dicom.cleanString(element.x00620008.value[0])
18567
+ };
18568
+ // algorithmName -> SegmentAlgorithmName (type1C)
18569
+ if (element.x00620009) {
18570
+ segment.algorithmName =
18571
+ dwv.dicom.cleanString(element.x00620009.value[0]);
18572
+ }
18573
+ // // required if type is not MANUAL
18574
+ // if (segment.algorithmType !== 'MANUAL' &&
18575
+ // (typeof segment.algorithmName === 'undefined' ||
18576
+ // segment.algorithmName.length === 0)) {
18577
+ // throw new Error('Empty algorithm name for non MANUAL algorithm type.');
18578
+ // }
18579
+ // displayValue ->
18580
+ // - RecommendedDisplayGrayscaleValue
18581
+ // - RecommendedDisplayCIELabValue converted to RGB
18582
+ if (typeof element.x0062000C !== 'undefined') {
18583
+ segment.displayValue = element.x006200C.value;
18584
+ } else if (typeof element.x0062000D !== 'undefined') {
18585
+ var cielabElement = element.x0062000D.value;
18586
+ var rgb = dwv.utils.cielabToSrgb(dwv.utils.uintLabToLab({
18587
+ l: cielabElement[0],
18588
+ a: cielabElement[1],
18589
+ b: cielabElement[2]
18590
+ }));
18591
+ segment.displayValue = rgb;
18592
+ }
18593
+ // Segmented Property Category Code Sequence (type1, only one)
18594
+ if (element.x00620003) {
18595
+ segment.propertyCategoryCode =
18596
+ dwv.dicom.getCode(element.x00620003.value[0]);
18597
+ } else {
18598
+ throw Error('Missing Segmented Property Category Code Sequence.');
18599
+ }
18600
+ // Segmented Property Type Code Sequence (type1)
18601
+ if (element.x0062000F) {
18602
+ segment.propertyTypeCode =
18603
+ dwv.dicom.getCode(element.x0062000F.value[0]);
18604
+ } else {
18605
+ throw Error('Missing Segmented Property Type Code Sequence.');
18606
+ }
18607
+
18608
+ return segment;
18609
+ };
18610
+
18611
+ /**
18612
+ * Check if two segment objects are equal.
18613
+ *
18614
+ * @param {object} seg1 The first segment.
18615
+ * @param {object} seg2 The second segment.
18616
+ * @returns {boolean} True if both segment are equal.
18617
+ */
18618
+ dwv.dicom.isEqualSegment = function (seg1, seg2) {
18619
+ // basics
18620
+ if (typeof seg1 === 'undefined' ||
18621
+ typeof seg2 === 'undefined' ||
18622
+ seg1 === null ||
18623
+ seg2 === null) {
18624
+ return false;
18625
+ }
18626
+ var isEqual = seg1.number === seg2.number &&
18627
+ seg1.label === seg2.label &&
18628
+ seg1.algorithmType === seg2.algorithmType;
18629
+ // rgb
18630
+ if (typeof seg1.displayValue.r !== 'undefined') {
18631
+ if (typeof seg2.displayValue.r === 'undefined') {
18632
+ isEqual = false;
18633
+ } else {
18634
+ isEqual = isEqual &&
18635
+ dwv.utils.isEqualRgb(seg1.displayValue, seg2.displayValue);
18636
+ }
18637
+ } else {
18638
+ isEqual = isEqual &&
18639
+ seg1.displayValue === seg2.displayValue;
18640
+ }
18641
+ // algorithmName
18642
+ if (typeof seg1.algorithmName !== 'undefined') {
18643
+ if (typeof seg2.algorithmName === 'undefined') {
18644
+ isEqual = false;
18645
+ } else {
18646
+ isEqual = isEqual &&
18647
+ seg1.algorithmName === seg2.algorithmName;
18648
+ }
18649
+ }
18650
+
18651
+ return isEqual;
18652
+ };
18653
+
18654
+ /**
18655
+ * Check if two segment objects are similar: either the
18656
+ * number or the displayValue are equal.
18657
+ *
18658
+ * @param {object} seg1 The first segment.
18659
+ * @param {object} seg2 The second segment.
18660
+ * @returns {boolean} True if both segment are similar.
18661
+ */
18662
+ dwv.dicom.isSimilarSegment = function (seg1, seg2) {
18663
+ // basics
18664
+ if (typeof seg1 === 'undefined' ||
18665
+ typeof seg2 === 'undefined' ||
18666
+ seg1 === null ||
18667
+ seg2 === null) {
18668
+ return false;
18669
+ }
18670
+ var isSimilar = seg1.number === seg2.number;
18671
+ // rgb
18672
+ if (typeof seg1.displayValue.r !== 'undefined') {
18673
+ if (typeof seg2.displayValue.r === 'undefined') {
18674
+ isSimilar = false;
18675
+ } else {
18676
+ isSimilar = isSimilar ||
18677
+ dwv.utils.isEqualRgb(seg1.displayValue, seg2.displayValue);
18678
+ }
18679
+ } else {
18680
+ isSimilar = isSimilar ||
18681
+ seg1.displayValue === seg2.displayValue;
18682
+ }
18683
+
18684
+ return isSimilar;
18685
+ };
18686
+
18687
+ /**
18688
+ * Get a spacing object from a dicom measure element.
18689
+ *
18690
+ * @param {object} measure The dicom element.
18691
+ * @returns {dwv.image.Spacing} A spacing object.
18692
+ */
18693
+ dwv.dicom.getSpacingFromMeasure = function (measure) {
18694
+ // Pixel Spacing
18695
+ if (typeof measure.x00280030 === 'undefined') {
18696
+ return null;
18697
+ }
18698
+ var pixelSpacing = measure.x00280030;
18699
+ var spacingValues = [
18700
+ parseFloat(pixelSpacing.value[0]),
18701
+ parseFloat(pixelSpacing.value[1])
18702
+ ];
18703
+ // Spacing Between Slices
18704
+ if (typeof measure.x00180088 !== 'undefined') {
18705
+ var sliceThickness = measure.x00180088;
18706
+ spacingValues.push(parseFloat(sliceThickness.value[0]));
18707
+ }
18708
+ return new dwv.image.Spacing(spacingValues);
18709
+ };
18710
+
18711
+ /**
18712
+ * Get a frame information object from a dicom element.
18713
+ *
18714
+ * @param {object} groupItem The dicom element.
18715
+ * @returns {object} A frame information object.
18716
+ */
18717
+ dwv.dicom.getSegmentFrameInfo = function (groupItem) {
18718
+ // Derivation Image Sequence
18719
+ var derivationImages = [];
18720
+ if (typeof groupItem.x00089124 !== 'undefined') {
18721
+ var derivationImageSq = groupItem.x00089124.value;
18722
+ // Source Image Sequence
18723
+ for (var i = 0; i < derivationImageSq.length; ++i) {
18724
+ var sourceImages = [];
18725
+ if (typeof derivationImageSq[i].x00082112 !== 'undefined') {
18726
+ var sourceImageSq = derivationImageSq[i].x00082112.value;
18727
+ for (var j = 0; j < sourceImageSq.length; ++j) {
18728
+ var sourceImage = {};
18729
+ // Referenced SOP Class UID
18730
+ if (typeof sourceImageSq[j].x00081150 !== 'undefined') {
18731
+ sourceImage.referencedSOPClassUID =
18732
+ sourceImageSq[j].x00081150.value[0];
18733
+ }
18734
+ // Referenced SOP Instance UID
18735
+ if (typeof sourceImageSq[j].x00081155 !== 'undefined') {
18736
+ sourceImage.referencedSOPInstanceUID =
18737
+ sourceImageSq[j].x00081155.value[0];
18738
+ }
18739
+ sourceImages.push(sourceImage);
18740
+ }
18741
+ }
18742
+ derivationImages.push(sourceImages);
18743
+ }
18744
+ }
18745
+ // Frame Content Sequence (required, only one)
18746
+ var frameContentSq = groupItem.x00209111.value;
18747
+ // Dimension Index Value
18748
+ var dimIndex = frameContentSq[0].x00209157.value;
18749
+ // Segment Identification Sequence (required, only one)
18750
+ var segmentIdSq = groupItem.x0062000A.value;
18751
+ // Referenced Segment Number
18752
+ var refSegmentNumber = segmentIdSq[0].x0062000B.value[0];
18753
+ // Plane Position Sequence (required, only one)
18754
+ var planePosSq = groupItem.x00209113.value;
18755
+ // Image Position (Patient) (conditionally required)
18756
+ var imagePosPat = planePosSq[0].x00200032.value;
18757
+ for (var p = 0; p < imagePosPat.length; ++p) {
18758
+ imagePosPat[p] = parseFloat(imagePosPat[p], 10);
18759
+ }
18760
+ var frameInfo = {
18761
+ dimIndex: dimIndex,
18762
+ imagePosPat: imagePosPat,
18763
+ derivationImages: derivationImages,
18764
+ refSegmentNumber: refSegmentNumber
18765
+ };
18766
+ // Plane Orientation Sequence
18767
+ if (typeof groupItem.x00209116 !== 'undefined') {
18768
+ var framePlaneOrientationSeq = groupItem.x00209116;
18769
+ if (framePlaneOrientationSeq.value.length !== 0) {
18770
+ // should only be one Image Orientation (Patient)
18771
+ var frameImageOrientation =
18772
+ framePlaneOrientationSeq.value[0].x00200037.value;
18773
+ if (typeof frameImageOrientation !== 'undefined') {
18774
+ frameInfo.imageOrientationPatient = frameImageOrientation;
18775
+ }
18776
+ }
18777
+ }
18778
+ // Pixel Measures Sequence
18779
+ if (typeof groupItem.x00289110 !== 'undefined') {
18780
+ var framePixelMeasuresSeq = groupItem.x00289110;
18781
+ if (framePixelMeasuresSeq.value.length !== 0) {
18782
+ // should only be one
18783
+ var frameSpacing =
18784
+ dwv.dicom.getSpacingFromMeasure(framePixelMeasuresSeq.value[0]);
18785
+ if (typeof frameSpacing !== 'undefined') {
18786
+ frameInfo.spacing = frameSpacing;
18787
+ }
18788
+ } else {
18789
+ dwv.logger.warn(
18790
+ 'No shared functional group pixel measure sequence items.');
18791
+ }
18792
+ }
18793
+
18794
+ return frameInfo;
18795
+ };
18796
+
18259
18797
  /**
18260
18798
  * Mask {@link dwv.image.Image} factory.
18261
18799
  *
@@ -18272,6 +18810,11 @@ dwv.image.MaskFactory = function () {};
18272
18810
  */
18273
18811
  dwv.image.MaskFactory.prototype.create = function (
18274
18812
  dicomElements, pixelBuffer) {
18813
+ // check required and supported tags
18814
+ for (var d = 0; d < dwv.dicom.requiredSegDicomTags.length; ++d) {
18815
+ dwv.dicom.checkTag(dicomElements, dwv.dicom.requiredSegDicomTags[d]);
18816
+ }
18817
+
18275
18818
  // columns
18276
18819
  var columns = dicomElements.getFromKey('x00280011');
18277
18820
  if (!columns) {
@@ -18294,87 +18837,40 @@ dwv.image.MaskFactory.prototype.create = function (
18294
18837
 
18295
18838
  if (frames !== pixelBuffer.length / sliceSize) {
18296
18839
  throw new Error(
18297
- 'Buffer and numberOfFrames meta are not equal.');
18840
+ 'Buffer and numberOfFrames meta are not equal.' +
18841
+ frames + ' ' + pixelBuffer.length / sliceSize);
18298
18842
  }
18299
18843
 
18300
- // Segmentation Type
18301
- var segType = dicomElements.getFromKey('x00620001');
18302
- if (!segType) {
18303
- throw new Error('Missing or empty DICOM segmentation type');
18304
- } else {
18305
- segType = dwv.dicom.cleanString(segType);
18306
- }
18307
- if (segType !== 'BINARY') {
18308
- throw new Error('Unsupported segmentation type: ' + segType);
18309
- }
18310
-
18311
- // check if compressed
18312
- var syntax = dwv.dicom.cleanString(dicomElements.getFromKey('x00020010'));
18313
- var algoName = dwv.dicom.getSyntaxDecompressionName(syntax);
18314
- if (algoName !== null) {
18315
- throw new Error('Unsupported compressed segmentation: ' + algoName);
18316
- }
18844
+ // Dimension Organization and Index
18845
+ var dimension = dwv.dicom.getDimensionOrganization(dicomElements);
18317
18846
 
18318
18847
  // Segment Sequence
18319
18848
  var segSequence = dicomElements.getFromKey('x00620002', true);
18320
18849
  if (!segSequence || typeof segSequence === 'undefined') {
18321
18850
  throw new Error('Missing or empty segmentation sequence');
18322
18851
  }
18323
- var labels = [];
18852
+ var segments = [];
18324
18853
  var storeAsRGB = false;
18325
18854
  for (var i = 0; i < segSequence.length; ++i) {
18326
- // value -> Segment Number
18327
- // name -> Segment Label
18328
- var label = {
18329
- value: segSequence[i].x00620004.value[0],
18330
- name: dwv.dicom.cleanString(segSequence[i].x00620005.value[0]),
18331
- algorithmType: dwv.dicom.cleanString(segSequence[i].x00620008.value[0])
18332
- };
18333
- // Segment Algorithm Name
18334
- if (segSequence[i].x00620009) {
18335
- label.algorithmName =
18336
- dwv.dicom.cleanString(segSequence[i].x00620009.value[0]);
18337
- }
18338
- // Recommended Display CIELab Value
18339
- if (typeof segSequence[i].x0062000D !== 'undefined') {
18340
- var cielabElement = segSequence[i].x0062000D.value;
18341
- var rgb = dwv.utils.cielabToSrgb(dwv.utils.uintLabToLab({
18342
- l: cielabElement[0],
18343
- a: cielabElement[1],
18344
- b: cielabElement[2]
18345
- }));
18346
- label.colour = rgb;
18855
+ var segment = dwv.dicom.getSegment(segSequence[i]);
18856
+ if (typeof segment.displayValue.r !== 'undefined' &&
18857
+ typeof segment.displayValue.g !== 'undefined' &&
18858
+ typeof segment.displayValue.b !== 'undefined') {
18347
18859
  // create rgb image
18348
18860
  storeAsRGB = true;
18349
18861
  }
18350
18862
  // store
18351
- labels.push(label);
18863
+ segments.push(segment);
18352
18864
  }
18353
18865
 
18354
18866
  // image size
18355
18867
  var size = dicomElements.getImageSize();
18356
18868
 
18357
- var getSpacingFromMeasure = function (measure) {
18358
- if (typeof measure.x00280030 === 'undefined') {
18359
- return null;
18360
- }
18361
- var pixelSpacing = measure.x00280030;
18362
- var spacingValues = [
18363
- parseFloat(pixelSpacing.value[0]),
18364
- parseFloat(pixelSpacing.value[1])
18365
- ];
18366
- if (typeof measure.x00180088 !== 'undefined') {
18367
- var sliceThickness = measure.x00180088;
18368
- spacingValues.push(parseFloat(sliceThickness.value[0]));
18369
- }
18370
- return new dwv.image.Spacing(spacingValues);
18371
- };
18372
-
18373
18869
  // Shared Functional Groups Sequence
18374
18870
  var spacing;
18375
18871
  var imageOrientationPatient;
18376
18872
  var sharedFunctionalGroupsSeq = dicomElements.getFromKey('x52009229', true);
18377
- if (sharedFunctionalGroupsSeq.length !== 0) {
18873
+ if (sharedFunctionalGroupsSeq && sharedFunctionalGroupsSeq.length !== 0) {
18378
18874
  // should be only one
18379
18875
  var funcGroup0 = sharedFunctionalGroupsSeq[0];
18380
18876
  // Plane Orientation Sequence
@@ -18382,7 +18878,12 @@ dwv.image.MaskFactory.prototype.create = function (
18382
18878
  var planeOrientationSeq = funcGroup0.x00209116;
18383
18879
  if (planeOrientationSeq.value.length !== 0) {
18384
18880
  // should be only one
18385
- imageOrientationPatient = planeOrientationSeq.value[0].x00200037.value;
18881
+ var orientArray = planeOrientationSeq.value[0].x00200037.value;
18882
+ imageOrientationPatient = orientArray.map(
18883
+ function (x) {
18884
+ return parseFloat(x);
18885
+ }
18886
+ );
18386
18887
  } else {
18387
18888
  dwv.logger.warn(
18388
18889
  'No shared functional group plane orientation sequence items.');
@@ -18393,7 +18894,7 @@ dwv.image.MaskFactory.prototype.create = function (
18393
18894
  var pixelMeasuresSeq = funcGroup0.x00289110;
18394
18895
  if (pixelMeasuresSeq.value.length !== 0) {
18395
18896
  // should be only one
18396
- spacing = getSpacingFromMeasure(pixelMeasuresSeq.value[0]);
18897
+ spacing = dwv.dicom.getSpacingFromMeasure(pixelMeasuresSeq.value[0]);
18397
18898
  } else {
18398
18899
  dwv.logger.warn(
18399
18900
  'No shared functional group pixel measure sequence items.');
@@ -18401,46 +18902,15 @@ dwv.image.MaskFactory.prototype.create = function (
18401
18902
  }
18402
18903
  }
18403
18904
 
18404
- // position patient utility functions
18405
- var equalPosPat = function (pos1, pos2) {
18406
- return JSON.stringify(pos1) === JSON.stringify(pos2);
18407
- };
18408
-
18409
- var comparePosPat = function (pos1, pos2) {
18410
- var diff = null;
18411
- var posLen = pos1.length;
18412
- var index = posLen;
18413
- for (var i = 0; i < posLen; ++i) {
18414
- --index;
18415
- diff = pos2[index] - pos1[index];
18416
- if (diff !== 0) {
18417
- return diff;
18418
- }
18419
- }
18420
- return diff;
18421
- };
18422
-
18423
18905
  var includesPosPat = function (arr, val) {
18424
18906
  return arr.some(function (arrVal) {
18425
- return equalPosPat(val, arrVal);
18907
+ return dwv.dicom.equalPosPat(val, arrVal);
18426
18908
  });
18427
18909
  };
18428
18910
 
18429
18911
  var findIndexPosPat = function (arr, val) {
18430
18912
  return arr.findIndex(function (arrVal) {
18431
- return equalPosPat(val, arrVal);
18432
- });
18433
- };
18434
-
18435
- var arrayEquals = function (arr0, arr1) {
18436
- if (arr0 === null || arr1 === null) {
18437
- return false;
18438
- }
18439
- if (arr0.length !== arr1.length) {
18440
- return false;
18441
- }
18442
- return arr0.every(function (element, index) {
18443
- return element === arr1[index];
18913
+ return dwv.dicom.equalPosPat(val, arrVal);
18444
18914
  });
18445
18915
  };
18446
18916
 
@@ -18456,79 +18926,49 @@ dwv.image.MaskFactory.prototype.create = function (
18456
18926
  }
18457
18927
  // create frame info object from per frame func
18458
18928
  var frameInfos = [];
18459
- var framePosPats = [];
18460
18929
  for (var j = 0; j < perFrameFuncGroupSequence.length; ++j) {
18461
- var frameFunc = perFrameFuncGroupSequence[j];
18462
- // Derivation Image Sequence
18463
- var referencedSOPInstanceUID;
18464
- if (typeof frameFunc.x00089124 !== 'undefined') {
18465
- var derivationImageSq = frameFunc.x00089124.value;
18466
- // Source Image Sequence
18467
- if (typeof derivationImageSq[0].x00082112 !== 'undefined') {
18468
- var sourceImageSq = derivationImageSq[0].x00082112.value;
18469
- // Referenced SOP Instance UID
18470
- if (typeof sourceImageSq[0].x00081155 !== 'undefined') {
18471
- referencedSOPInstanceUID = sourceImageSq[0].x00081155.value[0];
18472
- }
18473
- }
18474
- }
18475
- // Frame Content Sequence
18476
- var frameContentSq = frameFunc.x00209111.value;
18477
- // Dimension Index Value
18478
- var dimIndex = frameContentSq[0].x00209157.value;
18479
- // Plane Position Sequence
18480
- var planePosSq = frameFunc.x00209113.value;
18481
- // Image Position (Patient)
18482
- var imagePosPat = planePosSq[0].x00200032.value;
18483
- for (var p = 0; p < imagePosPat.length; ++p) {
18484
- imagePosPat[p] = parseFloat(imagePosPat[p], 10);
18485
- }
18486
- if (!includesPosPat(framePosPats, imagePosPat)) {
18487
- framePosPats.push(imagePosPat);
18488
- }
18489
- frameInfos.push({
18490
- dimIndex: dimIndex,
18491
- imagePosPat: imagePosPat,
18492
- referencedSOPInstanceUID: referencedSOPInstanceUID
18493
- });
18494
- // Plane Orientation Sequence
18495
- if (typeof frameFunc.x00209116 !== 'undefined') {
18496
- var framePlaneOrientationSeq = frameFunc.x00209116;
18497
- if (framePlaneOrientationSeq.value.length !== 0) {
18498
- // should only be one
18499
- var frameImageOrientation =
18500
- framePlaneOrientationSeq.value[0].x00200037.value;
18501
- if (typeof imageOrientationPatient === 'undefined') {
18502
- imageOrientationPatient = frameImageOrientation;
18503
- } else {
18504
- if (!arrayEquals(frameImageOrientation, imageOrientationPatient)) {
18505
- throw new Error('Unsupported multi orientation dicom seg.');
18506
- }
18930
+ frameInfos.push(
18931
+ dwv.dicom.getSegmentFrameInfo(perFrameFuncGroupSequence[j]));
18932
+ }
18933
+
18934
+ // check frame infos
18935
+ var framePosPats = [];
18936
+ for (var ii = 0; ii < frameInfos.length; ++ii) {
18937
+ if (!includesPosPat(framePosPats, frameInfos[ii].imagePosPat)) {
18938
+ framePosPats.push(frameInfos[ii].imagePosPat);
18939
+ }
18940
+ // store orientation if needed, avoid multi
18941
+ if (typeof frameInfos[ii].imageOrientationPatient !== 'undefined') {
18942
+ if (typeof imageOrientationPatient === 'undefined') {
18943
+ imageOrientationPatient = frameInfos[ii].imageOrientationPatient;
18944
+ } else {
18945
+ if (!dwv.utils.arrayEquals(
18946
+ imageOrientationPatient, frameInfos[ii].imageOrientationPatient)) {
18947
+ throw new Error('Unsupported multi orientation dicom seg.');
18507
18948
  }
18508
18949
  }
18509
18950
  }
18510
- // Pixel Measures Sequence
18511
- if (typeof frameFunc.x00289110 !== 'undefined') {
18512
- var framePixelMeasuresSeq = frameFunc.x00289110;
18513
- if (framePixelMeasuresSeq.value.length !== 0) {
18514
- // should only be one
18515
- var frameSpacing =
18516
- getSpacingFromMeasure(framePixelMeasuresSeq.value[0]);
18517
- if (typeof spacing === 'undefined') {
18518
- spacing = frameSpacing;
18519
- } else {
18520
- if (!spacing.equals(frameSpacing)) {
18521
- throw new Error('Unsupported multi resolution dicom seg.');
18522
- }
18523
- }
18951
+ // store spacing if needed, avoid multi
18952
+ if (typeof frameInfos[ii].spacing !== 'undefined') {
18953
+ if (typeof spacing === 'undefined') {
18954
+ spacing = frameInfos[ii].spacing;
18524
18955
  } else {
18525
- dwv.logger.warn(
18526
- 'No shared functional group pixel measure sequence items.');
18956
+ if (!spacing.equals(frameInfos[ii].spacing)) {
18957
+ throw new Error('Unsupported multi resolution dicom seg.');
18958
+ }
18527
18959
  }
18528
18960
  }
18529
18961
  }
18530
18962
  // sort positions patient
18531
- framePosPats.sort(comparePosPat);
18963
+ framePosPats.sort(dwv.dicom.comparePosPat);
18964
+
18965
+ // check spacing and orientation
18966
+ if (typeof spacing === 'undefined') {
18967
+ throw new Error('No spacing found for DICOM SEG');
18968
+ }
18969
+ if (typeof imageOrientationPatient === 'undefined') {
18970
+ throw new Error('No imageOrientationPatient found for DICOM SEG');
18971
+ }
18532
18972
 
18533
18973
  // add missing posPats
18534
18974
  var posPats = [];
@@ -18537,7 +18977,7 @@ dwv.image.MaskFactory.prototype.create = function (
18537
18977
  posPats.push(framePosPats[g]);
18538
18978
  var nextZ = framePosPats[g][2] - sliceSpacing;
18539
18979
  var diff = Math.abs(nextZ - framePosPats[g + 1][2]);
18540
- while (diff > sliceSpacing) {
18980
+ while (diff >= sliceSpacing) {
18541
18981
  posPats.push([framePosPats[g][0], framePosPats[g][1], nextZ]);
18542
18982
  nextZ -= sliceSpacing;
18543
18983
  diff = Math.abs(nextZ - framePosPats[g + 1][2]);
@@ -18545,8 +18985,14 @@ dwv.image.MaskFactory.prototype.create = function (
18545
18985
  }
18546
18986
  posPats.push(framePosPats[framePosPats.length - 1]);
18547
18987
 
18988
+ var getFindSegmentFunc = function (number) {
18989
+ return function (item) {
18990
+ return item.number === number;
18991
+ };
18992
+ };
18993
+
18548
18994
  // create output buffer
18549
- // as many slices as posPats -> gap slices between groups are not represented
18995
+ // as many slices as posPats
18550
18996
  var numberOfSlices = posPats.length;
18551
18997
  var mul = storeAsRGB ? 3 : 1;
18552
18998
  var buffer = new pixelBuffer.constructor(mul * sliceSize * numberOfSlices);
@@ -18555,19 +19001,16 @@ dwv.image.MaskFactory.prototype.create = function (
18555
19001
  var sliceOffset = null;
18556
19002
  var sliceIndex = null;
18557
19003
  var frameOffset = null;
18558
- var labelIndex = null;
18559
19004
  for (var f = 0; f < frameInfos.length; ++f) {
18560
19005
  // get the slice index from the position in the posPat array
18561
19006
  sliceIndex = findIndexPosPat(posPats, frameInfos[f].imagePosPat);
18562
19007
  frameOffset = sliceSize * f;
18563
- labelIndex = frameInfos[f].dimIndex[0] - 1;
18564
19008
  sliceOffset = sliceSize * sliceIndex;
18565
- var pixelValue;
18566
- if (storeAsRGB) {
18567
- pixelValue = labels[labelIndex].colour;
18568
- } else {
18569
- pixelValue = labels[labelIndex].value;
18570
- }
19009
+ // get the frame display value
19010
+ var frameSegment = segments.find(
19011
+ getFindSegmentFunc(frameInfos[f].refSegmentNumber)
19012
+ );
19013
+ var pixelValue = frameSegment.displayValue;
18571
19014
  for (var l = 0; l < sliceSize; ++l) {
18572
19015
  if (pixelBuffer[frameOffset + l] !== 0) {
18573
19016
  var offset = mul * (sliceOffset + l);
@@ -18607,12 +19050,23 @@ dwv.image.MaskFactory.prototype.create = function (
18607
19050
  // image meta
18608
19051
  var meta = {
18609
19052
  Modality: 'SEG',
18610
- BitsStored: 8,
19053
+ SegmentationType: 'BINARY',
19054
+ DimensionOrganizationType: '3D',
19055
+ DimensionOrganizations: dimension.organizations,
19056
+ DimensionIndices: dimension.indices,
19057
+ BitsStored: 1,
19058
+ PatientName: dwv.dicom.cleanString(dicomElements.getFromKey('x00100010')),
19059
+ PatientID: dwv.dicom.cleanString(dicomElements.getFromKey('x00100020')),
19060
+ PatientBirthDate: dicomElements.getFromKey('x00100030'),
19061
+ PatientSex: dwv.dicom.cleanString(dicomElements.getFromKey('x00100040')),
19062
+ StudyInstanceUID: dicomElements.getFromKey('x0020000D'),
18611
19063
  SeriesInstanceUID: dicomElements.getFromKey('x0020000E'),
19064
+ SeriesNumber: dicomElements.getFromKey('x00200011'),
18612
19065
  ImageOrientationPatient: imageOrientationPatient,
18613
19066
  custom: {
18614
- labels: labels,
18615
- frameInfos: frameInfos
19067
+ segments: segments,
19068
+ frameInfos: frameInfos,
19069
+ SOPInstanceUID: dicomElements.getFromKey('x00080018')
18616
19070
  }
18617
19071
  };
18618
19072
  image.setMeta(meta);
@@ -20475,7 +20929,7 @@ dwv.image.validateWindowWidth = function (value) {
20475
20929
  * WindowLevel class.
20476
20930
  * <br>Pseudo-code:
20477
20931
  * <pre>
20478
- * if (x <= c - 0.5 - (w-1)/2), then y = ymin
20932
+ * if (x &lt;= c - 0.5 - (w-1)/2), then y = ymin
20479
20933
  * else if (x > c - 0.5 + (w-1)/2), then y = ymax,
20480
20934
  * else y = ((x - (c - 0.5)) / (w-1) + 0.5) * (ymax - ymin) + ymin
20481
20935
  * </pre>
@@ -32832,6 +33286,65 @@ dwv.tool = dwv.tool || {};
32832
33286
  *
32833
33287
  * @class
32834
33288
  * @param {dwv.App} app The associated application.
33289
+ * @example
33290
+ * // create the dwv app
33291
+ * var app = new dwv.App();
33292
+ * // initialise
33293
+ * app.init({
33294
+ * dataViewConfigs: {'*': [{divId: 'layerGroup0'}]},
33295
+ * tools: {Scroll: {}}
33296
+ * });
33297
+ * // activate tool
33298
+ * app.addEventListener('load', function () {
33299
+ * app.setTool('Scroll');
33300
+ * });
33301
+ * // load dicom data
33302
+ * app.loadURLs([
33303
+ * 'https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323851.dcm',
33304
+ * 'https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323707.dcm',
33305
+ * 'https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323563.dcm'
33306
+ * ]);
33307
+ * @example <caption>Example with slider</caption>
33308
+ * // create the dwv app
33309
+ * var app = new dwv.App();
33310
+ * // initialise
33311
+ * app.init({
33312
+ * dataViewConfigs: {'*': [{divId: 'layerGroup0'}]},
33313
+ * tools: {Scroll: {}}
33314
+ * });
33315
+ * // create range
33316
+ * var range = document.createElement('input');
33317
+ * range.type = 'range';
33318
+ * range.min = 0;
33319
+ * range.id = 'sliceRange';
33320
+ * document.body.appendChild(range);
33321
+ * // update app on slider change
33322
+ * range.oninput = function () {
33323
+ * var lg = app.getLayerGroupById(0);
33324
+ * var vc = lg.getActiveViewLayer().getViewController();
33325
+ * var index = vc.getCurrentIndex();
33326
+ * var values = index.getValues();
33327
+ * values[2] = this.value;
33328
+ * vc.setCurrentIndex(new dwv.math.Index(values));
33329
+ * }
33330
+ * // activate tool and update range max on load
33331
+ * app.addEventListener('load', function () {
33332
+ * app.setTool('Scroll');
33333
+ * var size = app.getImage(0).getGeometry().getSize();
33334
+ * range.max = size.get(2) - 1;
33335
+ * });
33336
+ * // update slider on slice change (for ex via mouse wheel)
33337
+ * app.addEventListener('positionchange', function () {
33338
+ * var lg = app.getLayerGroupById(0);
33339
+ * var vc = lg.getActiveViewLayer().getViewController();
33340
+ * range.value = vc.getCurrentIndex().get(2);
33341
+ * });
33342
+ * // load dicom data
33343
+ * app.loadURLs([
33344
+ * 'https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323851.dcm',
33345
+ * 'https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323707.dcm',
33346
+ * 'https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323563.dcm'
33347
+ * ]);
32835
33348
  */
32836
33349
  dwv.tool.Scroll = function (app) {
32837
33350
  /**
@@ -33712,6 +34225,30 @@ dwv.tool.ZoomAndPan.prototype.init = function () {
33712
34225
  var dwv = dwv || {};
33713
34226
  dwv.utils = dwv.utils || {};
33714
34227
 
34228
+ /**
34229
+ * Check for array equality.
34230
+ *
34231
+ * @param {Array} arr0 First array.
34232
+ * @param {*} arr1 Second array.
34233
+ * @returns {boolean} True if both array are defined and contain same values.
34234
+ */
34235
+ dwv.utils.arrayEquals = function (arr0, arr1) {
34236
+ if (arr0 === null ||
34237
+ arr1 === null ||
34238
+ typeof arr0 === 'undefined' ||
34239
+ typeof arr1 === 'undefined') {
34240
+ return false;
34241
+ }
34242
+ if (arr0.length !== arr1.length) {
34243
+ return false;
34244
+ }
34245
+ var arr0sorted = arr0.slice().sort();
34246
+ var arr1sorted = arr1.slice().sort();
34247
+ return arr0sorted.every(function (element, index) {
34248
+ return element === arr1sorted[index];
34249
+ });
34250
+ };
34251
+
33715
34252
  /**
33716
34253
  * Convert a Uint8Array to a string.
33717
34254
  *
@@ -33936,6 +34473,23 @@ dwv.utils = dwv.utils || {};
33936
34473
  // example implementation: dcmtk/dcmiod/libsrc/cielabutil.cc
33937
34474
  // https://github.com/DCMTK/dcmtk/blob/DCMTK-3.6.6/dcmiod/libsrc/cielabutil.cc
33938
34475
 
34476
+ /**
34477
+ * Check if two rgb objects are equal.
34478
+ *
34479
+ * @param {object} c1 The first colour.
34480
+ * @param {object} c2 The second colour.
34481
+ * @returns {boolean} True if both colour are equal.
34482
+ */
34483
+ dwv.utils.isEqualRgb = function (c1, c2) {
34484
+ return c1 !== null &&
34485
+ c2 !== null &&
34486
+ typeof c1 !== 'undefined' &&
34487
+ typeof c2 !== 'undefined' &&
34488
+ c1.r === c2.r &&
34489
+ c1.g === c2.g &&
34490
+ c1.b === c2.b;
34491
+ };
34492
+
33939
34493
  /**
33940
34494
  * Convert YBR to RGB.
33941
34495
  *