dwv 0.31.0-beta.14 → 0.31.0-beta.16

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.14 2022-09-23 10:16:00 */
1
+ /*! dwv 0.31.0-beta.16 2022-11-08 14:56:16 */
2
2
  // Inspired from umdjs
3
3
  // See https://github.com/umdjs/umd/blob/master/templates/returnExports.js
4
4
  (function (root, factory) {
@@ -322,6 +322,17 @@ dwv.App = function () {
322
322
  return stage.getLayerGroup(groupId);
323
323
  };
324
324
 
325
+ /**
326
+ * Get a layer group by div id.
327
+ * The layer is available after the first loaded item.
328
+ *
329
+ * @param {string} divId The div id.
330
+ * @returns {dwv.gui.LayerGroup} The layer group.
331
+ */
332
+ this.getLayerGroupByDivId = function (divId) {
333
+ return stage.getLayerGroupByDivId(divId);
334
+ };
335
+
325
336
  /**
326
337
  * Get the number of layer groups.
327
338
  *
@@ -617,11 +628,8 @@ dwv.App = function () {
617
628
  * To be called once the image is loaded.
618
629
  */
619
630
  this.fitToContainer = function () {
620
- for (var i = 0; i < stage.getNumberOfLayerGroups(); ++i) {
621
- var layerGroup = stage.getLayerGroup(i);
622
- layerGroup.fitToContainer();
623
- layerGroup.draw();
624
- }
631
+ stage.syncLayerGroupScale();
632
+
625
633
  };
626
634
 
627
635
  /**
@@ -746,7 +754,7 @@ dwv.App = function () {
746
754
  for (var i = 0; i < viewConfigs.length; ++i) {
747
755
  var config = viewConfigs[i];
748
756
  var layerGroup =
749
- stage.getLayerGroupWithElementId(config.divId);
757
+ stage.getLayerGroupByDivId(config.divId);
750
758
  // layer group must exist
751
759
  if (!layerGroup) {
752
760
  throw new Error('No layer group for ' + config.divId);
@@ -1328,7 +1336,7 @@ dwv.App = function () {
1328
1336
  if (!data) {
1329
1337
  throw new Error('Cannot initialise layer with data id: ' + dataIndex);
1330
1338
  }
1331
- var layerGroup = stage.getLayerGroupWithElementId(dataViewConfig.divId);
1339
+ var layerGroup = stage.getLayerGroupByDivId(dataViewConfig.divId);
1332
1340
  if (!layerGroup) {
1333
1341
  throw new Error('Cannot initialise layer with group id: ' +
1334
1342
  dataViewConfig.divId);
@@ -2031,6 +2039,8 @@ dwv.ctrl.DrawController = function (konvaLayer) {
2031
2039
 
2032
2040
  // udpate current layer
2033
2041
  konvaLayer.draw();
2042
+
2043
+ return true;
2034
2044
  };
2035
2045
 
2036
2046
  /**
@@ -3240,33 +3250,6 @@ dwv.dicom.DataReader = function (buffer, isLittleEndian) {
3240
3250
  isLittleEndian = true;
3241
3251
  }
3242
3252
 
3243
- // Default text decoder
3244
- var defaultTextDecoder = {};
3245
- defaultTextDecoder.decode = function (buffer) {
3246
- var result = '';
3247
- for (var i = 0, leni = buffer.length; i < leni; ++i) {
3248
- result += String.fromCharCode(buffer[i]);
3249
- }
3250
- return result;
3251
- };
3252
-
3253
- // Text decoder
3254
- var textDecoder = defaultTextDecoder;
3255
- if (typeof window.TextDecoder !== 'undefined') {
3256
- textDecoder = new TextDecoder('iso-8859-1');
3257
- }
3258
-
3259
- /**
3260
- * Set the utfLabel used to construct the TextDecoder.
3261
- *
3262
- * @param {string} label The encoding label.
3263
- */
3264
- this.setUtfLabel = function (label) {
3265
- if (typeof window.TextDecoder !== 'undefined') {
3266
- textDecoder = new TextDecoder(label);
3267
- }
3268
- };
3269
-
3270
3253
  /**
3271
3254
  * Is the Native endianness Little Endian.
3272
3255
  *
@@ -3301,6 +3284,16 @@ dwv.dicom.DataReader = function (buffer, isLittleEndian) {
3301
3284
  return view.getUint16(byteOffset, isLittleEndian);
3302
3285
  };
3303
3286
 
3287
+ /**
3288
+ * Read Int16 (2 bytes) data.
3289
+ *
3290
+ * @param {number} byteOffset The offset to start reading from.
3291
+ * @returns {number} The read data.
3292
+ */
3293
+ this.readInt16 = function (byteOffset) {
3294
+ return view.getInt16(byteOffset, isLittleEndian);
3295
+ };
3296
+
3304
3297
  /**
3305
3298
  * Read Uint32 (4 bytes) data.
3306
3299
  *
@@ -3614,32 +3607,6 @@ dwv.dicom.DataReader = function (buffer, isLittleEndian) {
3614
3607
  }
3615
3608
  return data;
3616
3609
  };
3617
-
3618
- /**
3619
- * Read data as a string.
3620
- *
3621
- * @param {number} byteOffset The offset to start reading from.
3622
- * @param {number} nChars The number of characters to read.
3623
- * @returns {string} The read data.
3624
- */
3625
- this.readString = function (byteOffset, nChars) {
3626
- var data = this.readUint8Array(byteOffset, nChars);
3627
- return defaultTextDecoder.decode(data);
3628
- };
3629
-
3630
- /**
3631
- * Read data as a 'special' string, decoding it if the
3632
- * TextDecoder is available.
3633
- *
3634
- * @param {number} byteOffset The offset to start reading from.
3635
- * @param {number} nChars The number of characters to read.
3636
- * @returns {string} The read data.
3637
- */
3638
- this.readSpecialString = function (byteOffset, nChars) {
3639
- var data = this.readUint8Array(byteOffset, nChars);
3640
- return textDecoder.decode(data);
3641
- };
3642
-
3643
3610
  }; // class DataReader
3644
3611
 
3645
3612
  /**
@@ -3673,33 +3640,6 @@ dwv.dicom.DataWriter = function (buffer, isLittleEndian) {
3673
3640
  isLittleEndian = true;
3674
3641
  }
3675
3642
 
3676
- // Default text encoder
3677
- var defaultTextEncoder = {};
3678
- defaultTextEncoder.encode = function (buffer) {
3679
- var result = new Uint8Array(buffer.length);
3680
- for (var i = 0, leni = buffer.length; i < leni; ++i) {
3681
- result[i] = buffer.charCodeAt(i);
3682
- }
3683
- return result;
3684
- };
3685
-
3686
- // Text encoder
3687
- var textEncoder = defaultTextEncoder;
3688
- if (typeof window.TextEncoder !== 'undefined') {
3689
- textEncoder = new TextEncoder('iso-8859-1');
3690
- }
3691
-
3692
- /**
3693
- * Set the utfLabel used to construct the TextEncoder.
3694
- *
3695
- * @param {string} label The encoding label.
3696
- */
3697
- this.setUtfLabel = function (label) {
3698
- if (typeof window.TextEncoder !== 'undefined') {
3699
- textEncoder = new TextEncoder(label);
3700
- }
3701
- };
3702
-
3703
3643
  // private DataView
3704
3644
  var view = new DataView(buffer);
3705
3645
 
@@ -3841,31 +3781,6 @@ dwv.dicom.DataWriter = function (buffer, isLittleEndian) {
3841
3781
  return byteOffset + Uint16Array.BYTES_PER_ELEMENT;
3842
3782
  };
3843
3783
 
3844
- /**
3845
- * Write string data.
3846
- *
3847
- * @param {number} byteOffset The offset to start writing from.
3848
- * @param {number} str The data to write.
3849
- * @returns {number} The new offset position.
3850
- */
3851
- this.writeString = function (byteOffset, str) {
3852
- var data = defaultTextEncoder.encode(str);
3853
- return this.writeUint8Array(byteOffset, data);
3854
- };
3855
-
3856
- /**
3857
- * Write data as a 'special' string, encoding it if the
3858
- * TextEncoder is available.
3859
- *
3860
- * @param {number} byteOffset The offset to start reading from.
3861
- * @param {number} str The data to write.
3862
- * @returns {number} The new offset position.
3863
- */
3864
- this.writeSpecialString = function (byteOffset, str) {
3865
- var data = textEncoder.encode(str);
3866
- return this.writeUint8Array(byteOffset, data);
3867
- };
3868
-
3869
3784
  }; // class DataWriter
3870
3785
 
3871
3786
  /**
@@ -4105,10 +4020,9 @@ dwv.dicom.DicomElementsWrapper = function (dicomElements) {
4105
4020
  * @returns {string} The tag name.
4106
4021
  */
4107
4022
  this.getTagName = function (tag) {
4108
- var tagObj = new dwv.dicom.Tag(tag.group, tag.element);
4109
- var name = tagObj.getNameFromDictionary();
4023
+ var name = tag.getNameFromDictionary();
4110
4024
  if (name === null) {
4111
- name = tagObj.getKey2();
4025
+ name = tag.getKey2();
4112
4026
  }
4113
4027
  return name;
4114
4028
  };
@@ -4123,8 +4037,7 @@ dwv.dicom.DicomElementsWrapper = function (dicomElements) {
4123
4037
  // element value
4124
4038
  var value = null;
4125
4039
 
4126
- var isPixel = dicomElement.tag.group === '0x7FE0' &&
4127
- dicomElement.tag.element === '0x0010';
4040
+ var isPixel = dwv.dicom.isPixelDataTag(dicomElement.tag);
4128
4041
 
4129
4042
  var vr = dicomElement.vr;
4130
4043
  if (vr === 'SQ' &&
@@ -4153,8 +4066,8 @@ dwv.dicom.DicomElementsWrapper = function (dicomElements) {
4153
4066
  // return
4154
4067
  return {
4155
4068
  value: value,
4156
- group: dicomElement.tag.group,
4157
- element: dicomElement.tag.element,
4069
+ group: dicomElement.tag.getGroup(),
4070
+ element: dicomElement.tag.getElement(),
4158
4071
  vr: vr,
4159
4072
  vl: dicomElement.vl
4160
4073
  };
@@ -4181,7 +4094,7 @@ dwv.dicom.DicomElementsWrapper = function (dicomElements) {
4181
4094
  var checkHeader = true;
4182
4095
  for (var i = 0, leni = keys.length; i < leni; ++i) {
4183
4096
  dicomElement = dicomElements[keys[i]];
4184
- if (checkHeader && dicomElement.tag.group !== '0x0002') {
4097
+ if (checkHeader && dicomElement.tag.getGroup() !== '0x0002') {
4185
4098
  result += '\n';
4186
4099
  result += '# Dicom-Data-Set\n';
4187
4100
  result += '# Used TransferSyntax: ';
@@ -4230,9 +4143,8 @@ dwv.dicom.DicomElementsWrapper.prototype.getElementValueAsString = function (
4230
4143
  if (dicomElement.vr !== 'SQ' &&
4231
4144
  dicomElement.value.length === 1 && dicomElement.value[0] === '') {
4232
4145
  str += '(no value available)';
4233
- } else if (dicomElement.tag.group === '0x7FE0' &&
4234
- dicomElement.tag.element === '0x0010' &&
4235
- dicomElement.vl === 'u/l') {
4146
+ } else if (dwv.dicom.isPixelDataTag(dicomElement.tag) &&
4147
+ dicomElement.undefinedLength) {
4236
4148
  str = '(PixelSequence)';
4237
4149
  } else if (dicomElement.vr === 'DA' && pretty) {
4238
4150
  var daValue = dicomElement.value[0];
@@ -4332,8 +4244,7 @@ dwv.dicom.DicomElementsWrapper.prototype.getElementAsString = function (
4332
4244
  prefix = prefix || '';
4333
4245
 
4334
4246
  // get tag anme from dictionary
4335
- var tag = new dwv.dicom.Tag(
4336
- dicomElement.tag.group, dicomElement.tag.element);
4247
+ var tag = dicomElement.tag;
4337
4248
  var tagName = tag.getNameFromDictionary();
4338
4249
 
4339
4250
  var deSize = dicomElement.value.length;
@@ -4343,25 +4254,23 @@ dwv.dicom.DicomElementsWrapper.prototype.getElementAsString = function (
4343
4254
  }
4344
4255
 
4345
4256
  // no size for delimitations
4346
- if (dicomElement.tag.group === '0xFFFE' && (
4347
- dicomElement.tag.element === '0xE00D' ||
4348
- dicomElement.tag.element === '0xE0DD')) {
4257
+ if (dwv.dicom.isItemDelimitationItemTag(dicomElement.tag) ||
4258
+ dwv.dicom.isSequenceDelimitationItemTag(dicomElement.tag)) {
4349
4259
  deSize = 0;
4350
4260
  } else if (isOtherVR) {
4351
4261
  deSize = 1;
4352
4262
  }
4353
4263
 
4354
- var isPixSequence = (dicomElement.tag.group === '0x7FE0' &&
4355
- dicomElement.tag.element === '0x0010' &&
4356
- dicomElement.vl === 'u/l');
4264
+ var isPixSequence = (dwv.dicom.isPixelDataTag(dicomElement.tag) &&
4265
+ dicomElement.undefinedLength);
4357
4266
 
4358
4267
  var line = null;
4359
4268
 
4360
4269
  // (group,element)
4361
4270
  line = '(';
4362
- line += dicomElement.tag.group.substring(2).toLowerCase();
4271
+ line += dicomElement.tag.getGroup().substring(2).toLowerCase();
4363
4272
  line += ',';
4364
- line += dicomElement.tag.element.substring(2).toLowerCase();
4273
+ line += dicomElement.tag.getElement().substring(2).toLowerCase();
4365
4274
  line += ') ';
4366
4275
  // value representation
4367
4276
  line += dicomElement.vr;
@@ -4381,7 +4290,7 @@ dwv.dicom.DicomElementsWrapper.prototype.getElementAsString = function (
4381
4290
  line += ' (PixelSequence #=' + deSize + ')';
4382
4291
  } else if (dicomElement.vr === 'SQ') {
4383
4292
  line += ' (Sequence with';
4384
- if (dicomElement.vl === 'u/l') {
4293
+ if (dicomElement.undefinedLength) {
4385
4294
  line += ' undefined';
4386
4295
  } else {
4387
4296
  line += ' explicit';
@@ -4448,7 +4357,7 @@ dwv.dicom.DicomElementsWrapper.prototype.getElementAsString = function (
4448
4357
  // get the item element
4449
4358
  var itemElement = item.xFFFEE000;
4450
4359
  message = '(Item with';
4451
- if (itemElement.vl === 'u/l') {
4360
+ if (itemElement.undefinedLength) {
4452
4361
  message += ' undefined';
4453
4362
  } else {
4454
4363
  message += ' explicit';
@@ -4468,12 +4377,12 @@ dwv.dicom.DicomElementsWrapper.prototype.getElementAsString = function (
4468
4377
  }
4469
4378
 
4470
4379
  message = '(ItemDelimitationItem';
4471
- if (itemElement.vl !== 'u/l') {
4380
+ if (!itemElement.undefinedLength) {
4472
4381
  message += ' for re-encoding';
4473
4382
  }
4474
4383
  message += ')';
4475
4384
  var itemDelimElement = {
4476
- tag: {group: '0xFFFE', element: '0xE00D'},
4385
+ tag: dwv.dicom.getItemDelimitationItemTag(),
4477
4386
  vr: 'na',
4478
4387
  vl: '0',
4479
4388
  value: [message]
@@ -4484,12 +4393,12 @@ dwv.dicom.DicomElementsWrapper.prototype.getElementAsString = function (
4484
4393
  }
4485
4394
 
4486
4395
  message = '(SequenceDelimitationItem';
4487
- if (dicomElement.vl !== 'u/l') {
4396
+ if (!dicomElement.undefinedLength) {
4488
4397
  message += ' for re-encod.';
4489
4398
  }
4490
4399
  message += ')';
4491
4400
  var sqDelimElement = {
4492
- tag: {group: '0xFFFE', element: '0xE0DD'},
4401
+ tag: dwv.dicom.getSequenceDelimitationItemTag(),
4493
4402
  vr: 'na',
4494
4403
  vl: '0',
4495
4404
  value: [message]
@@ -4507,7 +4416,7 @@ dwv.dicom.DicomElementsWrapper.prototype.getElementAsString = function (
4507
4416
  }
4508
4417
 
4509
4418
  var pixDelimElement = {
4510
- tag: {group: '0xFFFE', element: '0xE0DD'},
4419
+ tag: dwv.dicom.getSequenceDelimitationItemTag(),
4511
4420
  vr: 'na',
4512
4421
  vl: '0',
4513
4422
  value: ['(SequenceDelimitationItem)']
@@ -4621,7 +4530,7 @@ dwv.dicom.DicomElementsWrapper.prototype.getTime = function () {
4621
4530
  * Get the file list from a DICOMDIR
4622
4531
  *
4623
4532
  * @param {object} data The buffer data of the DICOMDIR
4624
- * @returns {Array} The file list as an array ordered by
4533
+ * @returns {Array|undefined} The file list as an array ordered by
4625
4534
  * STUDY > SERIES > IMAGES.
4626
4535
  */
4627
4536
  dwv.dicom.getFileListFromDicomDir = function (data) {
@@ -4634,13 +4543,13 @@ dwv.dicom.getFileListFromDicomDir = function (data) {
4634
4543
  if (typeof elements.x00041220 === 'undefined' ||
4635
4544
  typeof elements.x00041220.value === 'undefined') {
4636
4545
  dwv.logger.warn('No Directory Record Sequence found in DICOMDIR.');
4637
- return;
4546
+ return undefined;
4638
4547
  }
4639
4548
  var dirSeq = elements.x00041220.value;
4640
4549
 
4641
4550
  if (dirSeq.length === 0) {
4642
4551
  dwv.logger.warn('The Directory Record Sequence of the DICOMDIR is empty.');
4643
- return;
4552
+ return undefined;
4644
4553
  }
4645
4554
 
4646
4555
  var records = [];
@@ -4693,7 +4602,7 @@ dwv.dicom = dwv.dicom || {};
4693
4602
  * @returns {string} The version of the library.
4694
4603
  */
4695
4604
  dwv.getVersion = function () {
4696
- return '0.31.0-beta.14';
4605
+ return '0.31.0-beta.16';
4697
4606
  };
4698
4607
 
4699
4608
  /**
@@ -4785,6 +4694,25 @@ dwv.dicom.getUtfLabel = function (charSetTerm) {
4785
4694
  return label;
4786
4695
  };
4787
4696
 
4697
+ /**
4698
+ * Default text decoder
4699
+ */
4700
+ dwv.dicom.DefaultTextDecoder = function () {
4701
+ /**
4702
+ * Decode an input string buffer.
4703
+ *
4704
+ * @param {Uint8Array} buffer The buffer to decode.
4705
+ * @returns {string} The decoded string.
4706
+ */
4707
+ this.decode = function (buffer) {
4708
+ var result = '';
4709
+ for (var i = 0, leni = buffer.length; i < leni; ++i) {
4710
+ result += String.fromCharCode(buffer[i]);
4711
+ }
4712
+ return result;
4713
+ };
4714
+ };
4715
+
4788
4716
  /**
4789
4717
  * Get patient orientation label in the reverse direction.
4790
4718
  *
@@ -5016,7 +4944,7 @@ dwv.dicom.guessTransferSyntax = function (firstDataElement) {
5016
4944
  var oEightGroupBigEndian = '0x0800';
5017
4945
  var oEightGroupLittleEndian = '0x0008';
5018
4946
  // check that group is 0x0008
5019
- var group = firstDataElement.tag.group;
4947
+ var group = firstDataElement.tag.getGroup();
5020
4948
  if (group !== oEightGroupBigEndian &&
5021
4949
  group !== oEightGroupLittleEndian) {
5022
4950
  throw new Error(
@@ -5054,12 +4982,7 @@ dwv.dicom.guessTransferSyntax = function (firstDataElement) {
5054
4982
  }
5055
4983
  // set transfer syntax data element
5056
4984
  var dataElement = {
5057
- tag: {
5058
- group: '0x0002',
5059
- element: '0x0010',
5060
- name: 'x00020010',
5061
- endOffset: 4
5062
- },
4985
+ tag: new dwv.dicom.Tag('0x0002', '0x0010'),
5063
4986
  vr: 'UI'
5064
4987
  };
5065
4988
  dataElement.value = [syntax + ' ']; // even length
@@ -5170,6 +5093,43 @@ dwv.dicom.DicomParser = function () {
5170
5093
  * @type {string}
5171
5094
  */
5172
5095
  var defaultCharacterSet;
5096
+
5097
+ /**
5098
+ * Default text decoder.
5099
+ *
5100
+ * @private
5101
+ * @type {dwv.dicom.DefaultTextDecoder}
5102
+ */
5103
+ var defaultTextDecoder = new dwv.dicom.DefaultTextDecoder();
5104
+
5105
+ /**
5106
+ * Special text decoder.
5107
+ *
5108
+ * @private
5109
+ * @type {dwv.dicom.DefaultTextDecoder|TextDecoder}
5110
+ */
5111
+ var textDecoder = defaultTextDecoder;
5112
+
5113
+ /**
5114
+ * Decode an input string buffer using the default text decoder.
5115
+ *
5116
+ * @param {Uint8Array} buffer The buffer to decode.
5117
+ * @returns {string} The decoded string.
5118
+ */
5119
+ this.decodeString = function (buffer) {
5120
+ return defaultTextDecoder.decode(buffer);
5121
+ };
5122
+
5123
+ /**
5124
+ * Decode an input string buffer using the 'special' text decoder.
5125
+ *
5126
+ * @param {Uint8Array} buffer The buffer to decode.
5127
+ * @returns {string} The decoded string.
5128
+ */
5129
+ this.decodeSpecialString = function (buffer) {
5130
+ return textDecoder.decode(buffer);
5131
+ };
5132
+
5173
5133
  /**
5174
5134
  * Get the default character set.
5175
5135
  *
@@ -5178,14 +5138,30 @@ dwv.dicom.DicomParser = function () {
5178
5138
  this.getDefaultCharacterSet = function () {
5179
5139
  return defaultCharacterSet;
5180
5140
  };
5141
+
5181
5142
  /**
5182
5143
  * Set the default character set.
5183
- * param {String} The character set.
5184
5144
  *
5185
5145
  * @param {string} characterSet The input character set.
5186
5146
  */
5187
5147
  this.setDefaultCharacterSet = function (characterSet) {
5188
5148
  defaultCharacterSet = characterSet;
5149
+ this.setCharacterSet(characterSet);
5150
+ };
5151
+
5152
+ /**
5153
+ * Set the text decoder character set.
5154
+ *
5155
+ * @param {string} characterSet The input character set.
5156
+ */
5157
+ this.setDecoderCharacterSet = function (characterSet) {
5158
+ /**
5159
+ * The text decoder.
5160
+ *
5161
+ * @external TextDecoder
5162
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
5163
+ */
5164
+ textDecoder = new TextDecoder(characterSet);
5189
5165
  };
5190
5166
  };
5191
5167
 
@@ -5212,8 +5188,7 @@ dwv.dicom.DicomParser.prototype.getDicomElements = function () {
5212
5188
  *
5213
5189
  * @param {dwv.dicom.DataReader} reader The raw data reader.
5214
5190
  * @param {number} offset The offset where to start to read.
5215
- * @returns {object} An object containing the tags 'group',
5216
- * 'element' and 'name'.
5191
+ * @returns {object} An object containing the tag and the end offset.
5217
5192
  */
5218
5193
  dwv.dicom.DicomParser.prototype.readTag = function (reader, offset) {
5219
5194
  // group
@@ -5222,59 +5197,22 @@ dwv.dicom.DicomParser.prototype.readTag = function (reader, offset) {
5222
5197
  // element
5223
5198
  var element = reader.readHex(offset);
5224
5199
  offset += Uint16Array.BYTES_PER_ELEMENT;
5225
- // name
5226
- var name = new dwv.dicom.Tag(group, element).getKey();
5227
5200
  // return
5228
5201
  return {
5229
- group: group,
5230
- element: element,
5231
- name: name,
5202
+ tag: new dwv.dicom.Tag(group, element),
5232
5203
  endOffset: offset
5233
5204
  };
5234
5205
  };
5235
5206
 
5236
5207
  /**
5237
- * Read an explicit item data element.
5238
- *
5239
- * @param {dwv.dicom.DataReader} reader The raw data reader.
5240
- * @param {number} offset The offset where to start to read.
5241
- * @param {boolean} implicit Is the DICOM VR implicit?
5242
- * @returns {object} The item data as a list of data elements.
5243
- */
5244
- dwv.dicom.DicomParser.prototype.readExplicitItemDataElement = function (
5245
- reader, offset, implicit) {
5246
- var itemData = {};
5247
-
5248
- // read the first item
5249
- var item = this.readDataElement(reader, offset, implicit);
5250
- offset = item.endOffset;
5251
- itemData[item.tag.name] = item;
5252
-
5253
- // read until the end offset
5254
- var endOffset = offset;
5255
- offset -= item.vl;
5256
- while (offset < endOffset) {
5257
- item = this.readDataElement(reader, offset, implicit);
5258
- offset = item.endOffset;
5259
- itemData[item.tag.name] = item;
5260
- }
5261
-
5262
- return {
5263
- data: itemData,
5264
- endOffset: offset,
5265
- isSeqDelim: false
5266
- };
5267
- };
5268
-
5269
- /**
5270
- * Read an implicit item data element.
5208
+ * Read an item data element.
5271
5209
  *
5272
5210
  * @param {dwv.dicom.DataReader} reader The raw data reader.
5273
5211
  * @param {number} offset The offset where to start to read.
5274
5212
  * @param {boolean} implicit Is the DICOM VR implicit?
5275
5213
  * @returns {object} The item data as a list of data elements.
5276
5214
  */
5277
- dwv.dicom.DicomParser.prototype.readImplicitItemDataElement = function (
5215
+ dwv.dicom.DicomParser.prototype.readItemDataElement = function (
5278
5216
  reader, offset, implicit) {
5279
5217
  var itemData = {};
5280
5218
 
@@ -5283,7 +5221,7 @@ dwv.dicom.DicomParser.prototype.readImplicitItemDataElement = function (
5283
5221
  offset = item.endOffset;
5284
5222
 
5285
5223
  // exit if it is a sequence delimitation item
5286
- if (item.tag.name === 'xFFFEE0DD') {
5224
+ if (dwv.dicom.isSequenceDelimitationItemTag(item.tag)) {
5287
5225
  return {
5288
5226
  data: itemData,
5289
5227
  endOffset: item.endOffset,
@@ -5291,17 +5229,33 @@ dwv.dicom.DicomParser.prototype.readImplicitItemDataElement = function (
5291
5229
  };
5292
5230
  }
5293
5231
 
5294
- // store item
5295
- itemData[item.tag.name] = item;
5232
+ // store item (mainly to keep vl)
5233
+ itemData[item.tag.getKey()] = {
5234
+ tag: item.tag,
5235
+ vr: 'NONE',
5236
+ vl: item.vl,
5237
+ undefinedLength: item.undefinedLength
5238
+ };
5296
5239
 
5297
- // read until the item delimitation item
5298
- var isItemDelim = false;
5299
- while (!isItemDelim) {
5300
- item = this.readDataElement(reader, offset, implicit);
5301
- offset = item.endOffset;
5302
- isItemDelim = item.tag.name === 'xFFFEE00D';
5303
- if (!isItemDelim) {
5304
- itemData[item.tag.name] = item;
5240
+ if (!item.undefinedLength) {
5241
+ // explicit VR item: read until the end offset
5242
+ var endOffset = offset;
5243
+ offset -= item.vl;
5244
+ while (offset < endOffset) {
5245
+ item = this.readDataElement(reader, offset, implicit);
5246
+ offset = item.endOffset;
5247
+ itemData[item.tag.getKey()] = item;
5248
+ }
5249
+ } else {
5250
+ // implicit VR item: read until the item delimitation item
5251
+ var isItemDelim = false;
5252
+ while (!isItemDelim) {
5253
+ item = this.readDataElement(reader, offset, implicit);
5254
+ offset = item.endOffset;
5255
+ isItemDelim = dwv.dicom.isItemDelimitationItemTag(item.tag);
5256
+ if (!isItemDelim) {
5257
+ itemData[item.tag.getKey()] = item;
5258
+ }
5305
5259
  }
5306
5260
  }
5307
5261
 
@@ -5335,8 +5289,10 @@ dwv.dicom.DicomParser.prototype.readPixelItemDataElement = function (
5335
5289
  while (!isSeqDelim) {
5336
5290
  item = this.readDataElement(reader, offset, implicit);
5337
5291
  offset = item.endOffset;
5338
- isSeqDelim = item.tag.name === 'xFFFEE0DD';
5292
+ isSeqDelim = dwv.dicom.isSequenceDelimitationItemTag(item.tag);
5339
5293
  if (!isSeqDelim) {
5294
+ // force pixel item vr to OB
5295
+ item.vr = 'OB';
5340
5296
  itemData.push(item);
5341
5297
  }
5342
5298
  }
@@ -5361,9 +5317,9 @@ dwv.dicom.DicomParser.prototype.readPixelItemDataElement = function (
5361
5317
  dwv.dicom.DicomParser.prototype.readDataElement = function (
5362
5318
  reader, offset, implicit) {
5363
5319
  // Tag: group, element
5364
- var tagData = this.readTag(reader, offset);
5365
- var tag = new dwv.dicom.Tag(tagData.group, tagData.element);
5366
- offset = tagData.endOffset;
5320
+ var readTagRes = this.readTag(reader, offset);
5321
+ var tag = readTagRes.tag;
5322
+ offset = readTagRes.endOffset;
5367
5323
 
5368
5324
  // Value Representation (VR)
5369
5325
  var vr = null;
@@ -5377,7 +5333,7 @@ dwv.dicom.DicomParser.prototype.readDataElement = function (
5377
5333
  }
5378
5334
  is32bitVLVR = true;
5379
5335
  } else {
5380
- vr = reader.readString(offset, 2);
5336
+ vr = this.decodeString(reader.readUint8Array(offset, 2));
5381
5337
  offset += 2 * Uint8Array.BYTES_PER_ELEMENT;
5382
5338
  is32bitVLVR = dwv.dicom.is32bitVLVR(vr);
5383
5339
  // reserved 2 bytes
@@ -5386,7 +5342,7 @@ dwv.dicom.DicomParser.prototype.readDataElement = function (
5386
5342
  }
5387
5343
  }
5388
5344
  } else {
5389
- vr = 'UN';
5345
+ vr = 'NONE';
5390
5346
  is32bitVLVR = true;
5391
5347
  }
5392
5348
 
@@ -5401,14 +5357,13 @@ dwv.dicom.DicomParser.prototype.readDataElement = function (
5401
5357
  }
5402
5358
 
5403
5359
  // check the value of VL
5404
- var vlString = vl;
5360
+ var undefinedLength = false;
5405
5361
  if (vl === 0xffffffff) {
5406
- vlString = 'u/l';
5362
+ undefinedLength = true;
5407
5363
  vl = 0;
5408
5364
  }
5409
5365
 
5410
5366
  // treat private tag with unknown VR and zero VL as a sequence (see #799)
5411
- //if (dwv.dicom.isPrivateGroup(tag.group) && vr === 'UN' && vl === 0) {
5412
5367
  if (tag.isPrivate() && vr === 'UN' && vl === 0) {
5413
5368
  vr = 'SQ';
5414
5369
  }
@@ -5418,35 +5373,35 @@ dwv.dicom.DicomParser.prototype.readDataElement = function (
5418
5373
 
5419
5374
  // read sequence elements
5420
5375
  var data = null;
5421
- if (dwv.dicom.isPixelDataTag(tag) && vlString === 'u/l') {
5376
+ if (dwv.dicom.isPixelDataTag(tag) && undefinedLength) {
5422
5377
  // pixel data sequence (implicit)
5423
5378
  var pixItemData = this.readPixelItemDataElement(reader, offset, implicit);
5424
5379
  offset = pixItemData.endOffset;
5425
5380
  startOffset += pixItemData.offsetTableVl;
5426
5381
  data = pixItemData.data;
5427
5382
  endOffset = offset;
5383
+ vl = offset - startOffset;
5428
5384
  } else if (vr === 'SQ') {
5429
5385
  // sequence
5430
5386
  data = [];
5431
5387
  var itemData;
5432
- if (vlString !== 'u/l') {
5433
- // explicit VR sequence
5388
+ if (!undefinedLength) {
5434
5389
  if (vl !== 0) {
5435
- // read until the end offset
5390
+ // explicit VR sequence: read until the end offset
5436
5391
  var sqEndOffset = offset + vl;
5437
5392
  while (offset < sqEndOffset) {
5438
- itemData = this.readExplicitItemDataElement(reader, offset, implicit);
5393
+ itemData = this.readItemDataElement(reader, offset, implicit);
5439
5394
  data.push(itemData.data);
5440
5395
  offset = itemData.endOffset;
5441
5396
  }
5442
5397
  endOffset = offset;
5398
+ vl = offset - startOffset;
5443
5399
  }
5444
5400
  } else {
5445
- // implicit VR sequence
5446
- // read until the sequence delimitation item
5401
+ // implicit VR sequence: read until the sequence delimitation item
5447
5402
  var isSeqDelim = false;
5448
5403
  while (!isSeqDelim) {
5449
- itemData = this.readImplicitItemDataElement(reader, offset, implicit);
5404
+ itemData = this.readItemDataElement(reader, offset, implicit);
5450
5405
  isSeqDelim = itemData.isSeqDelim;
5451
5406
  offset = itemData.endOffset;
5452
5407
  // do not store the delimitation item
@@ -5455,19 +5410,24 @@ dwv.dicom.DicomParser.prototype.readDataElement = function (
5455
5410
  }
5456
5411
  }
5457
5412
  endOffset = offset;
5413
+ vl = offset - startOffset;
5458
5414
  }
5459
5415
  }
5460
5416
 
5461
5417
  // return
5462
5418
  var element = {
5463
- tag: tagData,
5419
+ tag: tag,
5464
5420
  vr: vr,
5465
- vl: vlString,
5421
+ vl: vl,
5466
5422
  startOffset: startOffset,
5467
5423
  endOffset: endOffset
5468
5424
  };
5425
+ // only set if true (only for sequences and items)
5426
+ if (undefinedLength) {
5427
+ element.undefinedLength = undefinedLength;
5428
+ }
5469
5429
  if (data) {
5470
- element.elements = data;
5430
+ element.items = data;
5471
5431
  }
5472
5432
  return element;
5473
5433
  };
@@ -5492,56 +5452,46 @@ dwv.dicom.DicomParser.prototype.interpretElement = function (
5492
5452
 
5493
5453
  // data
5494
5454
  var data = null;
5495
- var isPixelDataTag = dwv.dicom.isPixelDataTag(
5496
- new dwv.dicom.Tag(tag.group, tag.element));
5455
+ var isPixelDataTag = dwv.dicom.isPixelDataTag(tag);
5497
5456
  var vrType = dwv.dicom.vrTypes[vr];
5498
- if (isPixelDataTag && vl === 'u/l') {
5499
- // implicit pixel data sequence
5500
- data = [];
5501
- for (var j = 0; j < element.elements.length; ++j) {
5502
- data.push(this.interpretElement(
5503
- element.elements[j], reader,
5504
- pixelRepresentation, bitsAllocated));
5505
- }
5506
- } else if (isPixelDataTag &&
5507
- (vr === 'OB' || vr === 'OW' || vr === 'ox')) {
5508
- // check bits allocated and VR
5509
- // https://dicom.nema.org/medical/dicom/2022a/output/chtml/part05/sect_A.2.html
5510
- if (bitsAllocated > 8 && vr !== 'OW') {
5511
- dwv.logger.warn(
5512
- 'Reading DICOM pixel data with bitsAllocated>8 and vr is not OW.'
5513
- );
5514
- }
5515
- // read
5516
- data = [];
5517
- if (bitsAllocated === 1) {
5518
- data.push(reader.readBinaryArray(offset, vl));
5519
- } else if (bitsAllocated === 8) {
5520
- if (pixelRepresentation === 0) {
5521
- data.push(reader.readUint8Array(offset, vl));
5522
- } else {
5523
- data.push(reader.readInt8Array(offset, vl));
5524
- }
5525
- } else if (bitsAllocated === 16) {
5526
- if (pixelRepresentation === 0) {
5527
- data.push(reader.readUint16Array(offset, vl));
5528
- } else {
5529
- data.push(reader.readInt16Array(offset, vl));
5530
- }
5531
- } else if (bitsAllocated === 32) {
5532
- if (pixelRepresentation === 0) {
5533
- data.push(reader.readUint32Array(offset, vl));
5534
- } else {
5535
- data.push(reader.readInt32Array(offset, vl));
5457
+ if (isPixelDataTag) {
5458
+ if (element.undefinedLength) {
5459
+ // implicit pixel data sequence
5460
+ data = [];
5461
+ for (var j = 0; j < element.items.length; ++j) {
5462
+ data.push(this.interpretElement(
5463
+ element.items[j], reader,
5464
+ pixelRepresentation, bitsAllocated));
5465
+ }
5466
+ // remove non parsed items
5467
+ delete element.items;
5468
+ } else {
5469
+ // check bits allocated and VR
5470
+ // https://dicom.nema.org/medical/dicom/2022a/output/chtml/part05/sect_A.2.html
5471
+ if (bitsAllocated > 8 && vr === 'OB') {
5472
+ dwv.logger.warn(
5473
+ 'Reading DICOM pixel data with bitsAllocated>8 and OB VR.'
5474
+ );
5536
5475
  }
5537
- } else if (bitsAllocated === 64) {
5538
- if (pixelRepresentation === 0) {
5539
- data.push(reader.readUint64Array(offset, vl));
5476
+ // read
5477
+ data = [];
5478
+ if (bitsAllocated === 1) {
5479
+ data.push(reader.readBinaryArray(offset, vl));
5480
+ } else if (bitsAllocated === 8) {
5481
+ if (pixelRepresentation === 0) {
5482
+ data.push(reader.readUint8Array(offset, vl));
5483
+ } else {
5484
+ data.push(reader.readInt8Array(offset, vl));
5485
+ }
5486
+ } else if (bitsAllocated === 16) {
5487
+ if (pixelRepresentation === 0) {
5488
+ data.push(reader.readUint16Array(offset, vl));
5489
+ } else {
5490
+ data.push(reader.readInt16Array(offset, vl));
5491
+ }
5540
5492
  } else {
5541
- data.push(reader.readInt64Array(offset, vl));
5493
+ throw new Error('Unsupported bits allocated: ' + bitsAllocated);
5542
5494
  }
5543
- } else {
5544
- throw new Error('Unsupported bits allocated: ' + bitsAllocated);
5545
5495
  }
5546
5496
  } else if (typeof vrType !== 'undefined') {
5547
5497
  if (vrType === 'Uint8') {
@@ -5563,10 +5513,11 @@ dwv.dicom.DicomParser.prototype.interpretElement = function (
5563
5513
  } else if (vrType === 'Float64') {
5564
5514
  data = reader.readFloat64Array(offset, vl);
5565
5515
  } else if (vrType === 'string') {
5516
+ var stream = reader.readUint8Array(offset, vl);
5566
5517
  if (dwv.dicom.charSetString.includes(vr)) {
5567
- data = reader.readSpecialString(offset, vl);
5518
+ data = this.decodeSpecialString(stream);
5568
5519
  } else {
5569
- data = reader.readString(offset, vl);
5520
+ data = this.decodeString(stream);
5570
5521
  }
5571
5522
  data = data.split('\\');
5572
5523
  } else {
@@ -5603,8 +5554,8 @@ dwv.dicom.DicomParser.prototype.interpretElement = function (
5603
5554
  } else if (vr === 'SQ') {
5604
5555
  // sequence
5605
5556
  data = [];
5606
- for (var k = 0; k < element.elements.length; ++k) {
5607
- var item = element.elements[k];
5557
+ for (var k = 0; k < element.items.length; ++k) {
5558
+ var item = element.items[k];
5608
5559
  var itemData = {};
5609
5560
  var keys = Object.keys(item);
5610
5561
  for (var l = 0; l < keys.length; ++l) {
@@ -5616,6 +5567,11 @@ dwv.dicom.DicomParser.prototype.interpretElement = function (
5616
5567
  }
5617
5568
  data.push(itemData);
5618
5569
  }
5570
+ // remove non parsed elements
5571
+ delete element.items;
5572
+ } else if (vr === 'NONE') {
5573
+ // no VR -> no data
5574
+ data = [];
5619
5575
  } else {
5620
5576
  dwv.logger.warn('Unknown VR: ' + vr);
5621
5577
  }
@@ -5643,6 +5599,9 @@ dwv.dicom.DicomParser.prototype.interpret = function (
5643
5599
  element.value = this.interpretElement(
5644
5600
  element, reader, pixelRepresentation, bitsAllocated);
5645
5601
  }
5602
+ // delete interpretation specific properties
5603
+ delete element.startOffset;
5604
+ delete element.endOffset;
5646
5605
  }
5647
5606
  };
5648
5607
 
@@ -5662,7 +5621,7 @@ dwv.dicom.DicomParser.prototype.parse = function (buffer) {
5662
5621
 
5663
5622
  // 128 -> 132: magic word
5664
5623
  offset = 128;
5665
- var magicword = metaReader.readString(offset, 4);
5624
+ var magicword = this.decodeString(metaReader.readUint8Array(offset, 4));
5666
5625
  offset += 4 * Uint8Array.BYTES_PER_ELEMENT;
5667
5626
  if (magicword === 'DICM') {
5668
5627
  // 0x0002, 0x0000: FileMetaInformationGroupLength
@@ -5671,7 +5630,7 @@ dwv.dicom.DicomParser.prototype.parse = function (buffer) {
5671
5630
  // increment offset
5672
5631
  offset = dataElement.endOffset;
5673
5632
  // store the data element
5674
- this.dicomElements[dataElement.tag.name] = dataElement;
5633
+ this.dicomElements[dataElement.tag.getKey()] = dataElement;
5675
5634
  // get meta length
5676
5635
  var metaLength = parseInt(dataElement.value[0], 10);
5677
5636
 
@@ -5682,7 +5641,7 @@ dwv.dicom.DicomParser.prototype.parse = function (buffer) {
5682
5641
  dataElement = this.readDataElement(metaReader, offset, false);
5683
5642
  offset = dataElement.endOffset;
5684
5643
  // store the data element
5685
- this.dicomElements[dataElement.tag.name] = dataElement;
5644
+ this.dicomElements[dataElement.tag.getKey()] = dataElement;
5686
5645
  }
5687
5646
 
5688
5647
  // check the TransferSyntaxUID (has to be there!)
@@ -5694,12 +5653,13 @@ dwv.dicom.DicomParser.prototype.parse = function (buffer) {
5694
5653
  syntax = dwv.dicom.cleanString(dataElement.value[0]);
5695
5654
 
5696
5655
  } else {
5656
+ dwv.logger.warn('No DICM prefix, trying to guess tansfer syntax.');
5697
5657
  // read first element
5698
5658
  dataElement = this.readDataElement(dataReader, 0, false);
5699
5659
  // guess transfer syntax
5700
5660
  var tsElement = dwv.dicom.guessTransferSyntax(dataElement);
5701
5661
  // store
5702
- this.dicomElements[tsElement.tag.name] = tsElement;
5662
+ this.dicomElements[tsElement.tag.getKey()] = tsElement;
5703
5663
  syntax = dwv.dicom.cleanString(tsElement.value[0]);
5704
5664
  // reset offset
5705
5665
  offset = 0;
@@ -5722,11 +5682,6 @@ dwv.dicom.DicomParser.prototype.parse = function (buffer) {
5722
5682
  dataReader = new dwv.dicom.DataReader(buffer, false);
5723
5683
  }
5724
5684
 
5725
- // default character set
5726
- if (typeof this.getDefaultCharacterSet() !== 'undefined') {
5727
- dataReader.setUtfLabel(this.getDefaultCharacterSet());
5728
- }
5729
-
5730
5685
  // DICOM data elements
5731
5686
  while (offset < buffer.byteLength) {
5732
5687
  // get the data element
@@ -5734,10 +5689,10 @@ dwv.dicom.DicomParser.prototype.parse = function (buffer) {
5734
5689
  // increment offset
5735
5690
  offset = dataElement.endOffset;
5736
5691
  // store the data element
5737
- if (typeof this.dicomElements[dataElement.tag.name] === 'undefined') {
5738
- this.dicomElements[dataElement.tag.name] = dataElement;
5692
+ if (typeof this.dicomElements[dataElement.tag.getKey()] === 'undefined') {
5693
+ this.dicomElements[dataElement.tag.getKey()] = dataElement;
5739
5694
  } else {
5740
- dwv.logger.warn('Not saving duplicate tag: ' + dataElement.tag.name);
5695
+ dwv.logger.warn('Not saving duplicate tag: ' + dataElement.tag.getKey());
5741
5696
  }
5742
5697
  }
5743
5698
 
@@ -5774,7 +5729,12 @@ dwv.dicom.DicomParser.prototype.parse = function (buffer) {
5774
5729
  dwv.logger.warn('Reading DICOM pixel data with default bitsAllocated.');
5775
5730
  }
5776
5731
 
5777
- // character set
5732
+ // default character set
5733
+ if (typeof this.getDefaultCharacterSet() !== 'undefined') {
5734
+ this.setDecoderCharacterSet(this.getDefaultCharacterSet());
5735
+ }
5736
+
5737
+ // SpecificCharacterSet
5778
5738
  dataElement = this.dicomElements.x00080005;
5779
5739
  if (typeof dataElement !== 'undefined') {
5780
5740
  dataElement.value = this.interpretElement(dataElement, dataReader);
@@ -5786,7 +5746,7 @@ dwv.dicom.DicomParser.prototype.parse = function (buffer) {
5786
5746
  dwv.logger.warn('Unsupported character set with code extensions: \'' +
5787
5747
  charSetTerm + '\'.');
5788
5748
  }
5789
- dataReader.setUtfLabel(dwv.dicom.getUtfLabel(charSetTerm));
5749
+ this.setDecoderCharacterSet(dwv.dicom.getUtfLabel(charSetTerm));
5790
5750
  }
5791
5751
 
5792
5752
  // interpret the dicom elements
@@ -5800,7 +5760,7 @@ dwv.dicom.DicomParser.prototype.parse = function (buffer) {
5800
5760
  // (third note, "Depending on the transfer syntax...")
5801
5761
  dataElement = this.dicomElements.x7FE00010;
5802
5762
  if (typeof dataElement !== 'undefined') {
5803
- if (dataElement.vl === 'u/l') {
5763
+ if (dataElement.undefinedLength) {
5804
5764
  var numberOfFrames = 1;
5805
5765
  if (typeof this.dicomElements.x00280008 !== 'undefined') {
5806
5766
  numberOfFrames = dwv.dicom.cleanString(
@@ -5851,6 +5811,18 @@ dwv.dicom = dwv.dicom || {};
5851
5811
  * @param {string} element The tag element as '0x####'.
5852
5812
  */
5853
5813
  dwv.dicom.Tag = function (group, element) {
5814
+ if (!group || typeof group === 'undefined') {
5815
+ throw new Error('Cannot create tag with no group.');
5816
+ }
5817
+ if (group.length !== 6 || !group.startsWith('0x')) {
5818
+ throw new Error('Cannot create tag with badly formed group.');
5819
+ }
5820
+ if (!element || typeof element === 'undefined') {
5821
+ throw new Error('Cannot create tag with no element.');
5822
+ }
5823
+ if (element.length !== 6 || !element.startsWith('0x')) {
5824
+ throw new Error('Cannot create tag with badly formed element.');
5825
+ }
5854
5826
  /**
5855
5827
  * Get the tag group.
5856
5828
  *
@@ -5877,23 +5849,28 @@ dwv.dicom.Tag = function (group, element) {
5877
5849
  */
5878
5850
  dwv.dicom.Tag.prototype.equals = function (rhs) {
5879
5851
  return rhs !== null &&
5852
+ typeof rhs !== 'undefined' &&
5880
5853
  this.getGroup() === rhs.getGroup() &&
5881
5854
  this.getElement() === rhs.getElement();
5882
5855
  };
5883
5856
 
5884
5857
  /**
5885
- * Check for Tag equality.
5858
+ * Tag compare function.
5886
5859
  *
5887
- * @param {object} rhs The other tag to compare to provided as a simple object.
5888
- * @returns {boolean} True if both tags are equal.
5860
+ * @param {dwv.dicom.Tag} a The first tag.
5861
+ * @param {dwv.dicom.Tag} b The second tag.
5862
+ * @returns {number} The result of the tag comparison,
5863
+ * positive for b before a, negative for a before b and
5864
+ * zero to keep same order.
5889
5865
  */
5890
- dwv.dicom.Tag.prototype.equals2 = function (rhs) {
5891
- if (rhs === null ||
5892
- typeof rhs.group === 'undefined' ||
5893
- typeof rhs.element === 'undefined') {
5894
- return false;
5866
+ dwv.dicom.tagCompareFunction = function (a, b) {
5867
+ // first by group
5868
+ var res = parseInt(a.getGroup()) - parseInt(b.getGroup());
5869
+ if (res === 0) {
5870
+ // by element if same group
5871
+ res = parseInt(a.getElement()) - parseInt(b.getElement());
5895
5872
  }
5896
- return this.equals(new dwv.dicom.Tag(rhs.group, rhs.element));
5873
+ return res;
5897
5874
  };
5898
5875
 
5899
5876
  /**
@@ -5935,7 +5912,9 @@ dwv.dicom.Tag.prototype.getGroupName = function () {
5935
5912
  * @returns {object} The DICOM tag.
5936
5913
  */
5937
5914
  dwv.dicom.getTagFromKey = function (key) {
5938
- return new dwv.dicom.Tag(key.substring(1, 5), key.substring(5, 9));
5915
+ return new dwv.dicom.Tag(
5916
+ '0x' + key.substring(1, 5),
5917
+ '0x' + key.substring(5, 9));
5939
5918
  };
5940
5919
 
5941
5920
  /**
@@ -6115,16 +6094,18 @@ dwv.dicom.isPixelDataTag = function (tag) {
6115
6094
  * Get a tag from the dictionary using a tag string name.
6116
6095
  *
6117
6096
  * @param {string} tagName The tag string name.
6118
- * @returns {object} The tag object.
6097
+ * @returns {object|null} The tag object or null if not found.
6119
6098
  */
6120
6099
  dwv.dicom.getTagFromDictionary = function (tagName) {
6100
+ if (typeof tagName === 'undefined' || tagName === null) {
6101
+ return null;
6102
+ }
6121
6103
  var group = null;
6122
6104
  var element = null;
6123
6105
  var dict = dwv.dicom.dictionary;
6124
6106
  var keys0 = Object.keys(dict);
6125
6107
  var keys1 = null;
6126
- // label for nested loop break
6127
- outLabel:
6108
+ var foundTag = false;
6128
6109
  // search through dictionary
6129
6110
  for (var k0 = 0, lenK0 = keys0.length; k0 < lenK0; ++k0) {
6130
6111
  group = keys0[k0];
@@ -6132,12 +6113,16 @@ dwv.dicom.getTagFromDictionary = function (tagName) {
6132
6113
  for (var k1 = 0, lenK1 = keys1.length; k1 < lenK1; ++k1) {
6133
6114
  element = keys1[k1];
6134
6115
  if (dict[group][element][2] === tagName) {
6135
- break outLabel;
6116
+ foundTag = true;
6117
+ break;
6136
6118
  }
6137
6119
  }
6120
+ if (foundTag) {
6121
+ break;
6122
+ }
6138
6123
  }
6139
6124
  var tag = null;
6140
- if (group !== null && element !== null) {
6125
+ if (foundTag) {
6141
6126
  tag = new dwv.dicom.Tag(group, element);
6142
6127
  }
6143
6128
  return tag;
@@ -6241,6 +6226,7 @@ dwv.dicom.isStringVr = function (vr) {
6241
6226
 
6242
6227
  /**
6243
6228
  * Is the input VR a VR that could need padding.
6229
+ * see http://dicom.nema.org/dicom/2013/output/chtml/part05/sect_6.2.html
6244
6230
  *
6245
6231
  * @param {string} vr The element VR.
6246
6232
  * @returns {boolean} True if the VR needs padding.
@@ -6268,60 +6254,52 @@ dwv.dicom.getVrPad = function (vr) {
6268
6254
  };
6269
6255
 
6270
6256
  /**
6271
- * Pad an input value according to its VR.
6272
- * see http://dicom.nema.org/dicom/2013/output/chtml/part05/sect_6.2.html
6273
- *
6274
- * @param {object} element The DICOM element to get the VR from.
6275
- * @param {object} value The value to pad.
6276
- * @returns {string} The padded value.
6277
- */
6278
- dwv.dicom.padElementValue = function (element, value) {
6279
- if (typeof value !== 'undefined' && typeof value.length !== 'undefined') {
6280
- if (dwv.dicom.isVrToPad(element.vr) && !dwv.dicom.isEven(value.length)) {
6281
- if (value instanceof Array) {
6282
- value[value.length - 1] += dwv.dicom.getVrPad(element.vr);
6283
- } else {
6284
- value += dwv.dicom.getVrPad(element.vr);
6285
- }
6286
- }
6287
- }
6288
- return value;
6289
- };
6290
-
6291
- /**
6292
- * Is this element an implicit length sequence?
6293
- *
6294
- * @param {object} element The element to check.
6295
- * @returns {boolean} True if it is.
6296
- */
6297
- dwv.dicom.isImplicitLengthSequence = function (element) {
6298
- // sequence with no length
6299
- return (element.vr === 'SQ') &&
6300
- (element.vl === 'u/l');
6301
- };
6302
-
6303
- /**
6304
- * Is this element an implicit length item?
6257
+ * Push a value at the end of an input Uint8Array.
6305
6258
  *
6306
- * @param {object} element The element to check.
6307
- * @returns {boolean} True if it is.
6259
+ * @param {Uint8Array} arr The input array.
6260
+ * @param {number} value The value to push.
6261
+ * @returns {Uint8Array} The new array.
6308
6262
  */
6309
- dwv.dicom.isImplicitLengthItem = function (element) {
6310
- // item with no length
6311
- return (element.tag.name === 'xFFFEE000') &&
6312
- (element.vl === 'u/l');
6263
+ dwv.dicom.uint8ArrayPush = function (arr, value) {
6264
+ var newArr = new Uint8Array(arr.length + 1);
6265
+ newArr.set(arr);
6266
+ newArr.set(value, arr.length);
6267
+ return newArr;
6313
6268
  };
6314
6269
 
6315
6270
  /**
6316
- * Is this element an implicit length pixel data?
6271
+ * Pad an input OB value.
6317
6272
  *
6318
- * @param {object} element The element to check.
6319
- * @returns {boolean} True if it is.
6273
+ * @param {Array|Uint8Array} value The input value.
6274
+ * @returns {Array|Uint8Array} The padded input.
6320
6275
  */
6321
- dwv.dicom.isImplicitLengthPixels = function (element) {
6322
- // pixel data with no length
6323
- return (element.tag.name === 'x7FE00010') &&
6324
- (element.vl === 'u/l');
6276
+ dwv.dicom.padOBValue = function (value) {
6277
+ if (value !== null &&
6278
+ typeof value !== 'undefined' &&
6279
+ typeof value.length !== 'undefined') {
6280
+ // calculate size and pad if needed
6281
+ if (value.length !== 0 &&
6282
+ typeof value[0].length !== 'undefined') {
6283
+ // handle array of array
6284
+ var size = 0;
6285
+ for (var i = 0; i < value.length; ++i) {
6286
+ size += value[i].length;
6287
+ }
6288
+ if (!dwv.dicom.isEven(size)) {
6289
+ value[value.length - 1] = dwv.dicom.uint8ArrayPush(
6290
+ value[value.length - 1], 0);
6291
+ }
6292
+ } else {
6293
+ if (!dwv.dicom.isEven(value.length)) {
6294
+ value = dwv.dicom.uint8ArrayPush(value, 0);
6295
+ }
6296
+ }
6297
+ } else {
6298
+ throw new Error('Cannot pad undefined or null OB value.');
6299
+ }
6300
+ // uint8ArrayPush may create a new array so we
6301
+ // need to return it
6302
+ return value;
6325
6303
  };
6326
6304
 
6327
6305
  /**
@@ -6349,6 +6327,25 @@ dwv.dicom.flattenArrayOfTypedArrays = function (initialArray) {
6349
6327
  return flattenedArray;
6350
6328
  };
6351
6329
 
6330
+ /**
6331
+ * Default text encoder.
6332
+ */
6333
+ dwv.dicom.DefaultTextEncoder = function () {
6334
+ /**
6335
+ * Encode an input string.
6336
+ *
6337
+ * @param {string} str The string to encode.
6338
+ * @returns {Uint8Array} The encoded string.
6339
+ */
6340
+ this.encode = function (str) {
6341
+ var result = new Uint8Array(str.length);
6342
+ for (var i = 0, leni = str.length; i < leni; ++i) {
6343
+ result[i] = str.charCodeAt(i);
6344
+ }
6345
+ return result;
6346
+ };
6347
+ };
6348
+
6352
6349
  /**
6353
6350
  * DICOM writer.
6354
6351
  *
@@ -6380,16 +6377,11 @@ dwv.dicom.DicomWriter = function () {
6380
6377
  return null;
6381
6378
  },
6382
6379
  clear: function (item) {
6383
- item.value[0] = '';
6384
- item.vl = 0;
6385
- item.endOffset = item.startOffset;
6380
+ item.value = [];
6386
6381
  return item;
6387
6382
  },
6388
6383
  replace: function (item, value) {
6389
- var paddedValue = dwv.dicom.padElementValue(item, value);
6390
- item.value[0] = paddedValue;
6391
- item.vl = paddedValue.length;
6392
- item.endOffset = item.startOffset + paddedValue.length;
6384
+ item.value = [value];
6393
6385
  return item;
6394
6386
  }
6395
6387
  };
@@ -6410,6 +6402,55 @@ dwv.dicom.DicomWriter = function () {
6410
6402
  */
6411
6403
  this.rules = defaultRules;
6412
6404
 
6405
+ /**
6406
+ * Default text encoder.
6407
+ *
6408
+ * @private
6409
+ * @type {dwv.dicom.DefaultTextEncoder}
6410
+ */
6411
+ var defaultTextEncoder = new dwv.dicom.DefaultTextEncoder();
6412
+
6413
+ /**
6414
+ * Special text encoder.
6415
+ *
6416
+ * @private
6417
+ * @type {dwv.dicom.DefaultTextEncoder|TextEncoder}
6418
+ */
6419
+ var textEncoder = defaultTextEncoder;
6420
+
6421
+ /**
6422
+ * Encode string data.
6423
+ *
6424
+ * @param {number} str The string to encode.
6425
+ * @returns {Uint8Array} The encoded string.
6426
+ */
6427
+ this.encodeString = function (str) {
6428
+ return defaultTextEncoder.encode(str);
6429
+ };
6430
+
6431
+ /**
6432
+ * Encode data as a UTF-8.
6433
+ *
6434
+ * @param {number} str The string to write.
6435
+ * @returns {Uint8Array} The encoded string.
6436
+ */
6437
+ this.encodeSpecialString = function (str) {
6438
+ return textEncoder.encode(str);
6439
+ };
6440
+
6441
+ /**
6442
+ * Use a TextEncoder instead of the default text decoder.
6443
+ */
6444
+ this.useSpecialTextEncoder = function () {
6445
+ /**
6446
+ * The text encoder.
6447
+ *
6448
+ * @external TextEncoder
6449
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder
6450
+ */
6451
+ textEncoder = new TextEncoder();
6452
+ };
6453
+
6413
6454
  /**
6414
6455
  * Example anonymisation rules.
6415
6456
  */
@@ -6432,15 +6473,14 @@ dwv.dicom.DicomWriter = function () {
6432
6473
  */
6433
6474
  this.getElementToWrite = function (element) {
6434
6475
  // get group and tag string name
6435
- var tag = new dwv.dicom.Tag(element.tag.group, element.tag.element);
6436
- var groupName = tag.getGroupName();
6437
- var tagName = tag.getNameFromDictionary();
6476
+ var groupName = element.tag.getGroupName();
6477
+ var tagName = element.tag.getNameFromDictionary();
6438
6478
 
6439
6479
  // apply rules:
6440
6480
  var rule;
6441
- if (typeof this.rules[element.tag.name] !== 'undefined') {
6481
+ if (typeof this.rules[element.tag.getKey()] !== 'undefined') {
6442
6482
  // 1. tag itself
6443
- rule = this.rules[element.tag.name];
6483
+ rule = this.rules[element.tag.getKey()];
6444
6484
  } else if (tagName !== null && typeof this.rules[tagName] !== 'undefined') {
6445
6485
  // 2. tag name
6446
6486
  rule = this.rules[tagName];
@@ -6475,11 +6515,14 @@ dwv.dicom.DicomWriter.prototype.writeDataElementItems = function (
6475
6515
  continue;
6476
6516
  }
6477
6517
  // item element (create new to not modify original)
6478
- var implicitLength = item.xFFFEE000.vl === 'u/l';
6518
+ var undefinedLength = false;
6519
+ if (typeof item.xFFFEE000.undefinedLength !== 'undefined') {
6520
+ undefinedLength = item.xFFFEE000.undefinedLength;
6521
+ }
6479
6522
  var itemElement = {
6480
- tag: item.xFFFEE000.tag,
6481
- vr: item.xFFFEE000.vr,
6482
- vl: implicitLength ? 0xffffffff : item.xFFFEE000.vl,
6523
+ tag: dwv.dicom.getItemTag(),
6524
+ vr: 'NONE',
6525
+ vl: undefinedLength ? 0xffffffff : item.xFFFEE000.vl,
6483
6526
  value: []
6484
6527
  };
6485
6528
  byteOffset = this.writeDataElement(
@@ -6492,13 +6535,9 @@ dwv.dicom.DicomWriter.prototype.writeDataElementItems = function (
6492
6535
  }
6493
6536
  }
6494
6537
  // item delimitation
6495
- if (implicitLength) {
6538
+ if (undefinedLength) {
6496
6539
  var itemDelimElement = {
6497
- tag: {
6498
- group: '0xFFFE',
6499
- element: '0xE00D',
6500
- name: 'ItemDelimitationItem'
6501
- },
6540
+ tag: dwv.dicom.getItemDelimitationItemTag(),
6502
6541
  vr: 'NONE',
6503
6542
  vl: 0,
6504
6543
  value: []
@@ -6516,21 +6555,22 @@ dwv.dicom.DicomWriter.prototype.writeDataElementItems = function (
6516
6555
  * Write data with a specific Value Representation (VR).
6517
6556
  *
6518
6557
  * @param {dwv.dicom.DataWriter} writer The raw data writer.
6519
- * @param {string} vr The data Value Representation (VR).
6520
- * @param {string} vl The data Value Length (VL).
6558
+ * @param {object} element The element to write.
6521
6559
  * @param {number} byteOffset The offset to start writing from.
6522
6560
  * @param {Array} value The array to write.
6523
6561
  * @param {boolean} isImplicit Is the DICOM VR implicit?
6524
6562
  * @returns {number} The new offset position.
6525
6563
  */
6526
6564
  dwv.dicom.DicomWriter.prototype.writeDataElementValue = function (
6527
- writer, vr, vl, byteOffset, value, isImplicit) {
6565
+ writer, element, byteOffset, value, isImplicit) {
6566
+
6567
+ var startOffset = byteOffset;
6528
6568
 
6529
- if (vr === 'NONE') {
6569
+ if (element.vr === 'NONE') {
6530
6570
  // nothing to do!
6531
6571
  } else if (value instanceof Uint8Array) {
6532
6572
  // binary data has been expanded 8 times at read
6533
- if (value.length === 8 * vl) {
6573
+ if (value.length === 8 * element.vl) {
6534
6574
  byteOffset = writer.writeBinaryArray(byteOffset, value);
6535
6575
  } else {
6536
6576
  byteOffset = writer.writeUint8Array(byteOffset, value);
@@ -6551,7 +6591,7 @@ dwv.dicom.DicomWriter.prototype.writeDataElementValue = function (
6551
6591
  byteOffset = writer.writeInt64Array(byteOffset, value);
6552
6592
  } else {
6553
6593
  // switch according to VR if input type is undefined
6554
- var vrType = dwv.dicom.vrTypes[vr];
6594
+ var vrType = dwv.dicom.vrTypes[element.vr];
6555
6595
  if (typeof vrType !== 'undefined') {
6556
6596
  if (vrType === 'Uint8') {
6557
6597
  byteOffset = writer.writeUint8Array(byteOffset, value);
@@ -6572,23 +6612,14 @@ dwv.dicom.DicomWriter.prototype.writeDataElementValue = function (
6572
6612
  } else if (vrType === 'Float64') {
6573
6613
  byteOffset = writer.writeFloat64Array(byteOffset, value);
6574
6614
  } else if (vrType === 'string') {
6575
- // join if array
6576
- if (Array.isArray(value)) {
6577
- value = value.join('\\');
6578
- }
6579
- // write
6580
- if (dwv.dicom.charSetString.includes(vr)) {
6581
- byteOffset = writer.writeSpecialString(byteOffset, value);
6582
- } else {
6583
- byteOffset = writer.writeString(byteOffset, value);
6584
- }
6615
+ byteOffset = writer.writeUint8Array(byteOffset, value);
6585
6616
  } else {
6586
6617
  throw Error('Unknown VR type: ' + vrType);
6587
6618
  }
6588
- } else if (vr === 'SQ') {
6619
+ } else if (element.vr === 'SQ') {
6589
6620
  byteOffset = this.writeDataElementItems(
6590
6621
  writer, byteOffset, value, isImplicit);
6591
- } else if (vr === 'AT') {
6622
+ } else if (element.vr === 'AT') {
6592
6623
  for (var i = 0; i < value.length; ++i) {
6593
6624
  var hexString = value[i] + '';
6594
6625
  var hexString1 = hexString.substring(1, 5);
@@ -6599,9 +6630,18 @@ dwv.dicom.DicomWriter.prototype.writeDataElementValue = function (
6599
6630
  byteOffset = writer.writeUint16Array(byteOffset, atValue);
6600
6631
  }
6601
6632
  } else {
6602
- dwv.logger.warn('Unknown VR: ' + vr);
6633
+ dwv.logger.warn('Unknown VR: ' + element.vr);
6603
6634
  }
6604
6635
  }
6636
+
6637
+ if (element.vr !== 'SQ' && element.vr !== 'NONE') {
6638
+ var diff = byteOffset - startOffset;
6639
+ if (diff !== element.vl) {
6640
+ dwv.logger.warn('Offset difference and VL are not equal: ' +
6641
+ diff + ' != ' + element.vl + ', vr:' + element.vr);
6642
+ }
6643
+ }
6644
+
6605
6645
  // return new offset
6606
6646
  return byteOffset;
6607
6647
  };
@@ -6610,17 +6650,21 @@ dwv.dicom.DicomWriter.prototype.writeDataElementValue = function (
6610
6650
  * Write a pixel data element.
6611
6651
  *
6612
6652
  * @param {dwv.dicom.DataWriter} writer The raw data writer.
6613
- * @param {string} vr The data Value Representation (VR).
6614
- * @param {string} vl The data Value Length (VL).
6653
+ * @param {object} element The element to write.
6615
6654
  * @param {number} byteOffset The offset to start writing from.
6616
6655
  * @param {Array} value The array to write.
6617
6656
  * @param {boolean} isImplicit Is the DICOM VR implicit?
6618
6657
  * @returns {number} The new offset position.
6619
6658
  */
6620
6659
  dwv.dicom.DicomWriter.prototype.writePixelDataElementValue = function (
6621
- writer, vr, vl, byteOffset, value, isImplicit) {
6660
+ writer, element, byteOffset, value, isImplicit) {
6661
+ // undefined length flag
6662
+ var undefinedLength = false;
6663
+ if (typeof element.undefinedLength !== 'undefined') {
6664
+ undefinedLength = element.undefinedLength;
6665
+ }
6622
6666
  // explicit length
6623
- if (vl !== 'u/l') {
6667
+ if (!undefinedLength) {
6624
6668
  var finalValue = value[0];
6625
6669
  // flatten multi frame
6626
6670
  if (value.length > 1) {
@@ -6628,30 +6672,22 @@ dwv.dicom.DicomWriter.prototype.writePixelDataElementValue = function (
6628
6672
  }
6629
6673
  // write
6630
6674
  byteOffset = this.writeDataElementValue(
6631
- writer, vr, vl, byteOffset, finalValue, isImplicit);
6675
+ writer, element, byteOffset, finalValue, isImplicit);
6632
6676
  } else {
6633
6677
  // pixel data as sequence
6634
6678
  var item = {};
6635
6679
  // first item: basic offset table
6636
6680
  item.xFFFEE000 = {
6637
- tag: {
6638
- group: '0xFFFE',
6639
- element: '0xE000',
6640
- name: 'xFFFEE000'
6641
- },
6642
- vr: 'UN',
6681
+ tag: dwv.dicom.getItemTag(),
6682
+ vr: 'NONE',
6643
6683
  vl: 0,
6644
6684
  value: []
6645
6685
  };
6646
6686
  // data
6647
6687
  for (var i = 0; i < value.length; ++i) {
6648
6688
  item[i] = {
6649
- tag: {
6650
- group: '0xFFFE',
6651
- element: '0xE000',
6652
- name: 'xFFFEE000'
6653
- },
6654
- vr: vr,
6689
+ tag: dwv.dicom.getItemTag(),
6690
+ vr: element.vr,
6655
6691
  vl: value[i].length,
6656
6692
  value: value[i]
6657
6693
  };
@@ -6676,36 +6712,47 @@ dwv.dicom.DicomWriter.prototype.writePixelDataElementValue = function (
6676
6712
  */
6677
6713
  dwv.dicom.DicomWriter.prototype.writeDataElement = function (
6678
6714
  writer, element, byteOffset, isImplicit) {
6679
- var isTagWithVR = new dwv.dicom.Tag(
6680
- element.tag.group, element.tag.element).isWithVR();
6715
+ var isTagWithVR = element.tag.isWithVR();
6681
6716
  var is32bitVLVR = (isImplicit || !isTagWithVR)
6682
6717
  ? true : dwv.dicom.is32bitVLVR(element.vr);
6683
6718
  // group
6684
- byteOffset = writer.writeHex(byteOffset, element.tag.group);
6719
+ byteOffset = writer.writeHex(byteOffset, element.tag.getGroup());
6685
6720
  // element
6686
- byteOffset = writer.writeHex(byteOffset, element.tag.element);
6721
+ byteOffset = writer.writeHex(byteOffset, element.tag.getElement());
6687
6722
  // VR
6688
6723
  var vr = element.vr;
6689
6724
  // use VR=UN for private sequence
6690
6725
  if (this.useUnVrForPrivateSq &&
6691
- new dwv.dicom.Tag(element.tag.group, element.tag.element).isPrivate() &&
6726
+ element.tag.isPrivate() &&
6692
6727
  vr === 'SQ') {
6693
6728
  dwv.logger.warn('Write element using VR=UN for private sequence.');
6694
6729
  vr = 'UN';
6695
6730
  }
6696
6731
  if (isTagWithVR && !isImplicit) {
6697
- byteOffset = writer.writeString(byteOffset, vr);
6732
+ byteOffset = writer.writeUint8Array(byteOffset, this.encodeString(vr));
6698
6733
  // reserved 2 bytes for 32bit VL
6699
6734
  if (is32bitVLVR) {
6700
6735
  byteOffset += 2;
6701
6736
  }
6702
6737
  }
6703
6738
 
6704
- // update vl for sequence or item with implicit length
6739
+ var undefinedLengthSequence = false;
6740
+ if (element.vr === 'SQ' ||
6741
+ dwv.dicom.isPixelDataTag(element.tag)) {
6742
+ if (typeof element.undefinedLength !== 'undefined') {
6743
+ undefinedLengthSequence = element.undefinedLength;
6744
+ }
6745
+ }
6746
+ var undefinedLengthItem = false;
6747
+ if (dwv.dicom.isItemTag(element.tag)) {
6748
+ if (typeof element.undefinedLength !== 'undefined') {
6749
+ undefinedLengthItem = element.undefinedLength;
6750
+ }
6751
+ }
6752
+
6753
+ // update vl for sequence or item with undefined length
6705
6754
  var vl = element.vl;
6706
- if (dwv.dicom.isImplicitLengthSequence(element) ||
6707
- dwv.dicom.isImplicitLengthItem(element) ||
6708
- dwv.dicom.isImplicitLengthPixels(element)) {
6755
+ if (undefinedLengthSequence || undefinedLengthItem) {
6709
6756
  vl = 0xffffffff;
6710
6757
  }
6711
6758
  // VL
@@ -6722,23 +6769,18 @@ dwv.dicom.DicomWriter.prototype.writeDataElement = function (
6722
6769
  value = [];
6723
6770
  }
6724
6771
  // write
6725
- if (element.tag.name === 'x7FE00010') {
6772
+ if (dwv.dicom.isPixelDataTag(element.tag)) {
6726
6773
  byteOffset = this.writePixelDataElementValue(
6727
- writer, element.vr, element.vl, byteOffset, value, isImplicit);
6774
+ writer, element, byteOffset, value, isImplicit);
6728
6775
  } else {
6729
6776
  byteOffset = this.writeDataElementValue(
6730
- writer, element.vr, element.vl, byteOffset, value, isImplicit);
6777
+ writer, element, byteOffset, value, isImplicit);
6731
6778
  }
6732
6779
 
6733
- // sequence delimitation item for sequence with implicit length
6734
- if (dwv.dicom.isImplicitLengthSequence(element) ||
6735
- dwv.dicom.isImplicitLengthPixels(element)) {
6780
+ // sequence delimitation item for sequence with undefined length
6781
+ if (undefinedLengthSequence) {
6736
6782
  var seqDelimElement = {
6737
- tag: {
6738
- group: '0xFFFE',
6739
- element: '0xE0DD',
6740
- name: 'SequenceDelimitationItem'
6741
- },
6783
+ tag: dwv.dicom.getSequenceDelimitationItemTag(),
6742
6784
  vr: 'NONE',
6743
6785
  vl: 0,
6744
6786
  value: []
@@ -6758,13 +6800,22 @@ dwv.dicom.DicomWriter.prototype.writeDataElement = function (
6758
6800
  * @returns {ArrayBuffer} The elements as a buffer.
6759
6801
  */
6760
6802
  dwv.dicom.DicomWriter.prototype.getBuffer = function (dicomElements) {
6761
- // array keys
6762
- var keys = Object.keys(dicomElements);
6763
-
6764
- // transfer syntax
6803
+ // Transfer Syntax
6765
6804
  var syntax = dwv.dicom.cleanString(dicomElements.x00020010.value[0]);
6766
6805
  var isImplicit = dwv.dicom.isImplicitTransferSyntax(syntax);
6767
6806
  var isBigEndian = dwv.dicom.isBigEndianTransferSyntax(syntax);
6807
+ // Bits Allocated
6808
+ var bitsAllocated = dicomElements.x00280100.value[0];
6809
+ // Specific CharacterSet
6810
+ if (typeof dicomElements.x00080005 !== 'undefined') {
6811
+ var oldscs = dwv.dicom.cleanString(dicomElements.x00080005.value[0]);
6812
+ // force UTF-8 if not default character set
6813
+ if (typeof oldscs !== 'undefined' && oldscs !== 'ISO-IR 6') {
6814
+ dwv.logger.debug('Change charset to UTF, was: ' + oldscs);
6815
+ this.useSpecialTextEncoder();
6816
+ dicomElements.x00080005.value = ['ISO_IR 192'];
6817
+ }
6818
+ }
6768
6819
 
6769
6820
  // calculate buffer size and split elements (meta and non meta)
6770
6821
  var totalSize = 128 + 4; // DICM
@@ -6774,17 +6825,24 @@ dwv.dicom.DicomWriter.prototype.getBuffer = function (dicomElements) {
6774
6825
  var element;
6775
6826
  var groupName;
6776
6827
  var metaLength = 0;
6828
+ // FileMetaInformationGroupLength
6777
6829
  var fmiglTag = dwv.dicom.getFileMetaInformationGroupLengthTag();
6830
+ // FileMetaInformationVersion
6831
+ var fmivTag = new dwv.dicom.Tag('0x0002', '0x0001');
6778
6832
  // ImplementationClassUID
6779
6833
  var icUIDTag = new dwv.dicom.Tag('0x0002', '0x0012');
6780
6834
  // ImplementationVersionName
6781
6835
  var ivnTag = new dwv.dicom.Tag('0x0002', '0x0013');
6836
+
6837
+ // loop through elements to get the buffer size
6838
+ var keys = Object.keys(dicomElements);
6782
6839
  for (var i = 0, leni = keys.length; i < leni; ++i) {
6783
6840
  element = this.getElementToWrite(dicomElements[keys[i]]);
6784
6841
  if (element !== null &&
6785
- !fmiglTag.equals2(element.tag) &&
6786
- !icUIDTag.equals2(element.tag) &&
6787
- !ivnTag.equals2(element.tag)) {
6842
+ !fmiglTag.equals(element.tag) &&
6843
+ !fmivTag.equals(element.tag) &&
6844
+ !icUIDTag.equals(element.tag) &&
6845
+ !ivnTag.equals(element.tag)) {
6788
6846
  localSize = 0;
6789
6847
 
6790
6848
  // XB7 2020-04-17
@@ -6794,8 +6852,12 @@ dwv.dicom.DicomWriter.prototype.getBuffer = function (dicomElements) {
6794
6852
  // (dcmdump may crash because of these bytes)
6795
6853
  dwv.dicom.checkUnknownVR(element);
6796
6854
 
6797
- // tag group name (remove first 0)
6798
- groupName = dwv.dicom.TagGroups[element.tag.group.substring(1)];
6855
+ // update value and vl
6856
+ this.setElementValue(
6857
+ element, element.value, isImplicit, bitsAllocated);
6858
+
6859
+ // tag group name
6860
+ groupName = element.tag.getGroupName();
6799
6861
 
6800
6862
  // prefix
6801
6863
  if (groupName === 'Meta Element') {
@@ -6806,8 +6868,7 @@ dwv.dicom.DicomWriter.prototype.getBuffer = function (dicomElements) {
6806
6868
  }
6807
6869
 
6808
6870
  // value
6809
- var realVl = element.endOffset - element.startOffset;
6810
- localSize += parseInt(realVl, 10);
6871
+ localSize += element.vl;
6811
6872
 
6812
6873
  // sort elements
6813
6874
  if (groupName === 'Meta Element') {
@@ -6822,44 +6883,52 @@ dwv.dicom.DicomWriter.prototype.getBuffer = function (dicomElements) {
6822
6883
  }
6823
6884
  }
6824
6885
 
6886
+ // FileMetaInformationVersion
6887
+ var fmiv = dwv.dicom.getDicomElement('FileMetaInformationVersion');
6888
+ var fmivSize = dwv.dicom.getDataElementPrefixByteSize(fmiv.vr, false);
6889
+ fmivSize += this.setElementValue(fmiv, [0, 1], false);
6890
+ metaElements.push(fmiv);
6891
+ metaLength += fmivSize;
6892
+ totalSize += fmivSize;
6825
6893
  // ImplementationClassUID
6826
6894
  var icUID = dwv.dicom.getDicomElement('ImplementationClassUID');
6827
- var icUIDSize = dwv.dicom.getDataElementPrefixByteSize(icUID.vr, isImplicit);
6828
- icUIDSize += dwv.dicom.setElementValue(
6829
- icUID, dwv.dicom.getUID('ImplementationClassUID'), false);
6895
+ var icUIDSize = dwv.dicom.getDataElementPrefixByteSize(icUID.vr, false);
6896
+ icUIDSize += this.setElementValue(
6897
+ icUID, [dwv.dicom.getUID('ImplementationClassUID')], false);
6830
6898
  metaElements.push(icUID);
6831
6899
  metaLength += icUIDSize;
6832
6900
  totalSize += icUIDSize;
6833
6901
  // ImplementationVersionName
6834
6902
  var ivn = dwv.dicom.getDicomElement('ImplementationVersionName');
6835
- var ivnSize = dwv.dicom.getDataElementPrefixByteSize(ivn.vr, isImplicit);
6903
+ var ivnSize = dwv.dicom.getDataElementPrefixByteSize(ivn.vr, false);
6836
6904
  var ivnValue = 'DWV_' + dwv.getVersion();
6837
- ivnSize += dwv.dicom.setElementValue(ivn, ivnValue, false);
6905
+ ivnSize += this.setElementValue(ivn, [ivnValue], false);
6838
6906
  metaElements.push(ivn);
6839
6907
  metaLength += ivnSize;
6840
6908
  totalSize += ivnSize;
6841
6909
 
6910
+ // sort elements
6911
+ var elemSortFunc = function (a, b) {
6912
+ return dwv.dicom.tagCompareFunction(a.tag, b.tag);
6913
+ };
6914
+ metaElements.sort(elemSortFunc);
6915
+ rawElements.sort(elemSortFunc);
6916
+
6842
6917
  // create the FileMetaInformationGroupLength element
6843
6918
  var fmigl = dwv.dicom.getDicomElement('FileMetaInformationGroupLength');
6844
- var fmiglSize = dwv.dicom.getDataElementPrefixByteSize(fmigl.vr, isImplicit);
6845
- fmiglSize += dwv.dicom.setElementValue(fmigl, metaLength, false);
6846
-
6847
- // add its size to the total one
6919
+ var fmiglSize = dwv.dicom.getDataElementPrefixByteSize(fmigl.vr, false);
6920
+ fmiglSize += this.setElementValue(
6921
+ fmigl, new Uint32Array([metaLength]), false);
6848
6922
  totalSize += fmiglSize;
6849
6923
 
6850
6924
  // create buffer
6851
6925
  var buffer = new ArrayBuffer(totalSize);
6852
6926
  var metaWriter = new dwv.dicom.DataWriter(buffer);
6853
6927
  var dataWriter = new dwv.dicom.DataWriter(buffer, !isBigEndian);
6854
- // special character set
6855
- if (typeof dicomElements.x00080005 !== 'undefined') {
6856
- var scs = dwv.dicom.cleanString(dicomElements.x00080005.value[0]);
6857
- dataWriter.setUtfLabel(dwv.dicom.getUtfLabel(scs));
6858
- }
6859
6928
 
6860
6929
  var offset = 128;
6861
6930
  // DICM
6862
- offset = metaWriter.writeString(offset, 'DICM');
6931
+ offset = metaWriter.writeUint8Array(offset, this.encodeString('DICM'));
6863
6932
  // FileMetaInformationGroupLength
6864
6933
  offset = this.writeDataElement(metaWriter, fmigl, offset, false);
6865
6934
  // write meta
@@ -6902,12 +6971,11 @@ dwv.dicom.DicomWriter.prototype.getBuffer = function (dicomElements) {
6902
6971
  */
6903
6972
  dwv.dicom.checkUnknownVR = function (element) {
6904
6973
  if (element.vr === 'UN') {
6905
- var tag = new dwv.dicom.Tag(element.tag.group, element.tag.element);
6906
- var dictVr = tag.getVrFromDictionary();
6974
+ var dictVr = element.tag.getVrFromDictionary();
6907
6975
  if (dictVr !== null && element.vr !== dictVr) {
6908
6976
  element.vr = dictVr;
6909
- dwv.logger.info('Element ' + element.tag.group +
6910
- ' ' + element.tag.element +
6977
+ dwv.logger.info('Element ' + element.tag.getGroup() +
6978
+ ' ' + element.tag.getElement() +
6911
6979
  ' VR changed from UN to ' + element.vr);
6912
6980
  }
6913
6981
  }
@@ -6921,176 +6989,212 @@ dwv.dicom.checkUnknownVR = function (element) {
6921
6989
  */
6922
6990
  dwv.dicom.getDicomElement = function (tagName) {
6923
6991
  var tag = dwv.dicom.getTagFromDictionary(tagName);
6924
- // return element definition
6925
6992
  return {
6926
- tag: {group: tag.getGroup(), element: tag.getElement()},
6993
+ tag: tag,
6927
6994
  vr: tag.getVrFromDictionary()
6928
6995
  };
6929
6996
  };
6930
6997
 
6998
+ /**
6999
+ * Get the number of bytes per element for a given VR type.
7000
+ *
7001
+ * @param {string} vrType The VR type as defined in the dictionary.
7002
+ * @returns {number} The bytes per element.
7003
+ */
7004
+ dwv.dicom.getBpeForVrType = function (vrType) {
7005
+ var bpe;
7006
+ if (vrType === 'Uint8') {
7007
+ bpe = Uint8Array.BYTES_PER_ELEMENT;
7008
+ } else if (vrType === 'Uint16') {
7009
+ bpe = Uint16Array.BYTES_PER_ELEMENT;
7010
+ } else if (vrType === 'Int16') {
7011
+ bpe = Int16Array.BYTES_PER_ELEMENT;
7012
+ } else if (vrType === 'Uint32') {
7013
+ bpe = Uint32Array.BYTES_PER_ELEMENT;
7014
+ } else if (vrType === 'Int32') {
7015
+ bpe = Int32Array.BYTES_PER_ELEMENT;
7016
+ } else if (vrType === 'Float32') {
7017
+ bpe = Float32Array.BYTES_PER_ELEMENT;
7018
+ } else if (vrType === 'Float64') {
7019
+ bpe = Float64Array.BYTES_PER_ELEMENT;
7020
+ } else if (vrType === 'Uint64') {
7021
+ bpe = BigUint64Array.BYTES_PER_ELEMENT;
7022
+ } else if (vrType === 'Int64') {
7023
+ bpe = BigInt64Array.BYTES_PER_ELEMENT;
7024
+ }
7025
+ return bpe;
7026
+ };
7027
+
6931
7028
  /**
6932
7029
  * Set a DICOM element value according to its VR (Value Representation).
6933
7030
  *
6934
7031
  * @param {object} element The DICOM element to set the value.
6935
7032
  * @param {object} value The value to set.
6936
7033
  * @param {boolean} isImplicit Does the data use implicit VR?
7034
+ * @param {number} bitsAllocated Bits allocated used for pixel data.
6937
7035
  * @returns {number} The total element size.
6938
7036
  */
6939
- dwv.dicom.setElementValue = function (element, value, isImplicit) {
7037
+ dwv.dicom.DicomWriter.prototype.setElementValue = function (
7038
+ element, value, isImplicit, bitsAllocated) {
6940
7039
  // byte size of the element
6941
7040
  var size = 0;
6942
7041
  // special sequence case
6943
7042
  if (element.vr === 'SQ') {
6944
7043
 
6945
- // set the value
6946
- element.value = value;
6947
- element.vl = 0;
6948
-
6949
7044
  if (value !== null && value !== 0) {
6950
- var sqItems = [];
7045
+ var newItems = [];
6951
7046
  var name;
6952
7047
 
6953
- // explicit or implicit length
6954
- var explicitLength = true;
6955
- if (typeof value.explicitLength !== 'undefined') {
6956
- explicitLength = value.explicitLength;
6957
- delete value.explicitLength;
7048
+ // explicit or undefined length sequence
7049
+ var undefinedLength = false;
7050
+ if (typeof element.undefinedLength !== 'undefined') {
7051
+ undefinedLength = element.undefinedLength;
7052
+ delete element.undefinedLength;
6958
7053
  }
6959
7054
 
6960
7055
  // items
6961
- var itemData;
6962
- var itemKeys = Object.keys(value);
6963
- for (var i = 0, leni = itemKeys.length; i < leni; ++i) {
6964
- var itemElements = {};
7056
+ for (var i = 0; i < value.length; ++i) {
7057
+ var oldItemElements = value[i];
7058
+ var newItemElements = {};
6965
7059
  var subSize = 0;
6966
- itemData = value[itemKeys[i]];
6967
7060
 
6968
7061
  // check data
6969
- if (itemData === null || itemData === 0) {
7062
+ if (oldItemElements === null || oldItemElements === 0) {
6970
7063
  continue;
6971
7064
  }
6972
7065
 
6973
7066
  // elements
6974
- var subElement;
6975
- var elemKeys = Object.keys(itemData);
6976
- for (var j = 0, lenj = elemKeys.length; j < lenj; ++j) {
6977
- subElement = dwv.dicom.getDicomElement(elemKeys[j]);
6978
- subSize += dwv.dicom.setElementValue(
6979
- subElement, itemData[elemKeys[j]]);
6980
-
6981
- name = new dwv.dicom.Tag(
6982
- subElement.tag.group, subElement.tag.element).getKey();
6983
- itemElements[name] = subElement;
7067
+ var itemKeys = Object.keys(oldItemElements);
7068
+ for (var j = 0, lenj = itemKeys.length; j < lenj; ++j) {
7069
+ var itemKey = itemKeys[j];
7070
+ var subElement = oldItemElements[itemKey];
7071
+ if (dwv.dicom.isItemTag(subElement.tag)) {
7072
+ continue;
7073
+ }
7074
+ // set item value
7075
+ subSize += this.setElementValue(
7076
+ subElement, subElement.value, isImplicit, bitsAllocated);
7077
+ newItemElements[itemKey] = subElement;
7078
+ // add prefix size
6984
7079
  subSize += dwv.dicom.getDataElementPrefixByteSize(
6985
7080
  subElement.vr, isImplicit);
6986
7081
  }
6987
7082
 
6988
- // item (after elements to get the size)
7083
+ // add item element (used to store its size)
6989
7084
  var itemElement = {
6990
- tag: {group: '0xFFFE', element: '0xE000'},
7085
+ tag: dwv.dicom.getItemTag(),
6991
7086
  vr: 'NONE',
6992
- vl: (explicitLength ? subSize : 'u/l'),
7087
+ vl: subSize,
6993
7088
  value: []
6994
7089
  };
6995
- name = new dwv.dicom.Tag(
6996
- itemElement.tag.group, itemElement.tag.element).getKey();
6997
- itemElements[name] = itemElement;
6998
- subSize += dwv.dicom.getDataElementPrefixByteSize('NONE', isImplicit);
6999
-
7000
- // item delimitation
7001
- if (!explicitLength) {
7002
- var itemDelimElement = {
7003
- tag: {group: '0xFFFE', element: '0xE00D'},
7004
- vr: 'NONE',
7005
- vl: 0,
7006
- value: []
7007
- };
7008
- name = new dwv.dicom.Tag(
7009
- itemDelimElement.tag.group, itemDelimElement.tag.element).getKey();
7010
- itemElements[name] = itemDelimElement;
7011
- subSize += dwv.dicom.getDataElementPrefixByteSize('NONE', isImplicit);
7090
+ if (undefinedLength) {
7091
+ itemElement.undefinedLength = undefinedLength;
7092
+ }
7093
+ name = itemElement.tag.getKey();
7094
+ newItemElements[name] = itemElement;
7095
+ subSize += dwv.dicom.getDataElementPrefixByteSize(
7096
+ itemElement.vr, isImplicit);
7097
+
7098
+ // add item delimitation size
7099
+ if (undefinedLength) {
7100
+ subSize += dwv.dicom.getDataElementPrefixByteSize(
7101
+ 'NONE', isImplicit);
7012
7102
  }
7013
7103
 
7014
7104
  size += subSize;
7015
- sqItems.push(itemElements);
7105
+ newItems.push(newItemElements);
7016
7106
  }
7017
7107
 
7018
7108
  // add sequence delimitation size
7019
- if (!explicitLength) {
7109
+ if (undefinedLength) {
7020
7110
  size += dwv.dicom.getDataElementPrefixByteSize('NONE', isImplicit);
7021
7111
  }
7022
7112
 
7023
- element.value = sqItems;
7024
- if (explicitLength) {
7025
- element.vl = size;
7026
- } else {
7027
- element.vl = 'u/l';
7113
+ // update sequence element
7114
+ element.value = newItems;
7115
+ element.vl = size;
7116
+ if (undefinedLength) {
7117
+ element.undefinedLength = undefinedLength;
7028
7118
  }
7029
7119
  }
7030
7120
  } else {
7031
- // set the value and calculate size
7121
+ // pad if necessary
7122
+ if (dwv.dicom.isVrToPad(element.vr)) {
7123
+ var pad = dwv.dicom.getVrPad(element.vr);
7124
+ // encode string
7125
+ // TODO: not sure for UN...
7126
+ if (dwv.dicom.isStringVr(element.vr)) {
7127
+ if (dwv.dicom.charSetString.includes(element.vr)) {
7128
+ value = this.encodeSpecialString(value.join('\\'));
7129
+ pad = this.encodeSpecialString(pad);
7130
+ } else {
7131
+ value = this.encodeString(value.join('\\'));
7132
+ pad = this.encodeString(pad);
7133
+ }
7134
+ if (!dwv.dicom.isEven(value.length)) {
7135
+ value = dwv.dicom.uint8ArrayPush(value, pad);
7136
+ }
7137
+ } else if (element.vr === 'OB') {
7138
+ value = dwv.dicom.padOBValue(value);
7139
+ }
7140
+ }
7141
+
7142
+ // calculate byte size
7032
7143
  size = 0;
7033
- var paddedValue = dwv.dicom.padElementValue(element, value);
7034
7144
  if (element.vr === 'AT') {
7035
- element.value = paddedValue;
7036
- size = 4;
7037
- } else if (dwv.dicom.isTypedArrayVr(element.vr)) {
7038
- // convert non array (number) value to array
7039
- if (typeof value.length === 'undefined') {
7040
- element.value = [paddedValue];
7145
+ size = 4 * value.length;
7146
+ } else if (element.vr === 'xs') {
7147
+ size = value.length * Uint16Array.BYTES_PER_ELEMENT;
7148
+ } else if (dwv.dicom.isTypedArrayVr(element.vr) || element.vr === 'ox') {
7149
+ if (dwv.dicom.isPixelDataTag(element.tag) &&
7150
+ Array.isArray(value)) {
7151
+ size = 0;
7152
+ for (var b = 0; b < value.length; ++b) {
7153
+ size += value[b].length;
7154
+ }
7041
7155
  } else {
7042
- element.value = paddedValue;
7156
+ size = value.length;
7043
7157
  }
7044
- size = element.value.length;
7045
7158
 
7046
7159
  // convert size to bytes
7047
7160
  var vrType = dwv.dicom.vrTypes[element.vr];
7048
- if (typeof vrType !== 'undefined') {
7049
- if (vrType === 'Uint8') {
7050
- size *= Uint8Array.BYTES_PER_ELEMENT;
7051
- } else if (vrType === 'Uint16') {
7052
- size *= Uint16Array.BYTES_PER_ELEMENT;
7053
- } else if (vrType === 'Int16') {
7054
- size *= Int16Array.BYTES_PER_ELEMENT;
7055
- } else if (vrType === 'Uint32') {
7056
- size *= Uint32Array.BYTES_PER_ELEMENT;
7057
- } else if (vrType === 'Int32') {
7058
- size *= Int32Array.BYTES_PER_ELEMENT;
7059
- } else if (vrType === 'Float32') {
7060
- size *= Float32Array.BYTES_PER_ELEMENT;
7061
- } else if (vrType === 'Float64') {
7062
- size *= Float64Array.BYTES_PER_ELEMENT;
7063
- } else if (vrType === 'Uint64') {
7064
- size *= BigUint64Array.BYTES_PER_ELEMENT;
7065
- } else if (vrType === 'Int64') {
7066
- size *= BigInt64Array.BYTES_PER_ELEMENT;
7161
+ if (dwv.dicom.isPixelDataTag(element.tag) || element.vr === 'ox') {
7162
+ if (element.undefinedLength) {
7163
+ var itemPrefixSize =
7164
+ dwv.dicom.getDataElementPrefixByteSize('NONE', isImplicit);
7165
+ // offset table
7166
+ size += itemPrefixSize;
7167
+ // pixel items
7168
+ size += itemPrefixSize * value.length;
7169
+ // add sequence delimitation size
7170
+ size += itemPrefixSize;
7067
7171
  } else {
7068
- throw Error('Unknown VR type: ' + vrType);
7069
- }
7070
- }
7071
- } else {
7072
- if (value instanceof Array) {
7073
- element.value = paddedValue;
7074
- for (var k = 0; k < paddedValue.length; ++k) {
7075
- // separator
7076
- if (k !== 0) {
7077
- size += 1;
7172
+ // use bitsAllocated for pixel data
7173
+ // no need to multiply for 8 bits
7174
+ if (typeof bitsAllocated !== 'undefined') {
7175
+ if (bitsAllocated === 1) {
7176
+ // binary data
7177
+ size /= 8;
7178
+ } else if (bitsAllocated === 16) {
7179
+ size *= Uint16Array.BYTES_PER_ELEMENT;
7180
+ }
7078
7181
  }
7079
- // value
7080
- size += paddedValue[k].toString().length;
7081
7182
  }
7082
- } else {
7083
- element.value = [paddedValue];
7084
- if (typeof paddedValue !== 'undefined' &&
7085
- typeof paddedValue.length !== 'undefined') {
7086
- size = paddedValue.length;
7183
+ } else if (typeof vrType !== 'undefined') {
7184
+ var bpe = dwv.dicom.getBpeForVrType(vrType);
7185
+ if (typeof bpe !== 'undefined') {
7186
+ size *= bpe;
7087
7187
  } else {
7088
- // numbers
7089
- size = 1;
7188
+ throw Error('Unknown bytes per element for VR type: ' + vrType);
7090
7189
  }
7190
+ } else {
7191
+ throw Error('Unsupported element: ' + element.vr);
7091
7192
  }
7193
+ } else {
7194
+ size = value.length;
7092
7195
  }
7093
7196
 
7197
+ element.value = value;
7094
7198
  element.vl = size;
7095
7199
  }
7096
7200
 
@@ -7099,40 +7203,63 @@ dwv.dicom.setElementValue = function (element, value, isImplicit) {
7099
7203
  };
7100
7204
 
7101
7205
  /**
7102
- * Get the DICOM element from a DICOM tags object.
7206
+ * Get the DICOM elements from a DICOM json tags object.
7207
+ * The json is a simplified version of the oficial DICOM json with
7208
+ * tag names instead of keys and direct values (no value property) for
7209
+ * simple tags.
7103
7210
  *
7104
- * @param {object} tags The DICOM tags object.
7105
- * @returns {object} The DICOM elements and the end offset.
7211
+ * @param {object} jsonTags The DICOM json tags object.
7212
+ * @returns {object} The DICOM elements.
7106
7213
  */
7107
- dwv.dicom.getElementsFromJSONTags = function (tags) {
7108
- // transfer syntax
7109
- var isImplicit = dwv.dicom.isImplicitTransferSyntax(tags.TransferSyntaxUID);
7110
- // convert JSON to DICOM element object
7111
- var keys = Object.keys(tags);
7214
+ dwv.dicom.getElementsFromJSONTags = function (jsonTags) {
7215
+ var keys = Object.keys(jsonTags);
7112
7216
  var dicomElements = {};
7113
- var dicomElement;
7114
- var name;
7115
- var offset = 128 + 4; // preamble
7116
- var size;
7117
7217
  for (var k = 0, len = keys.length; k < len; ++k) {
7118
7218
  // get the DICOM element definition from its name
7119
- dicomElement = dwv.dicom.getDicomElement(keys[k]);
7120
- // set its value
7121
- size = dwv.dicom.setElementValue(dicomElement, tags[keys[k]], isImplicit);
7122
- // set offsets
7123
- offset += dwv.dicom.getDataElementPrefixByteSize(
7124
- dicomElement.vr, isImplicit);
7125
- dicomElement.startOffset = offset;
7126
- offset += size;
7127
- dicomElement.endOffset = offset;
7128
- // create the tag group/element key
7129
- name = new dwv.dicom.Tag(
7130
- dicomElement.tag.group, dicomElement.tag.element).getKey();
7219
+ var tag = dwv.dicom.getTagFromDictionary(keys[k]);
7220
+ if (!tag) {
7221
+ continue;
7222
+ }
7223
+ var vr = tag.getVrFromDictionary();
7224
+ // tag value
7225
+ var value;
7226
+ var undefinedLength = false;
7227
+ var jsonTag = jsonTags[keys[k]];
7228
+ if (vr === 'SQ') {
7229
+ var items = [];
7230
+ if (typeof jsonTag.undefinedLength !== 'undefined') {
7231
+ undefinedLength = jsonTag.undefinedLength;
7232
+ }
7233
+ if (typeof jsonTag.value !== 'undefined' &&
7234
+ jsonTag.value !== null) {
7235
+ for (var i = 0; i < jsonTag.value.length; ++i) {
7236
+ items.push(dwv.dicom.getElementsFromJSONTags(jsonTag.value[i]));
7237
+ }
7238
+ } else {
7239
+ dwv.logger.trace('Undefined or null jsonTag SQ value.');
7240
+ }
7241
+ value = items;
7242
+ } else {
7243
+ if (Array.isArray(jsonTag)) {
7244
+ value = jsonTag;
7245
+ } else {
7246
+ value = [jsonTag];
7247
+ }
7248
+ }
7249
+ // create element
7250
+ var dicomElement = {
7251
+ tag: tag,
7252
+ vr: vr,
7253
+ value: value
7254
+ };
7255
+ if (undefinedLength) {
7256
+ dicomElement.undefinedLength = undefinedLength;
7257
+ }
7131
7258
  // store
7132
- dicomElements[name] = dicomElement;
7259
+ dicomElements[tag.getKey()] = dicomElement;
7133
7260
  }
7134
7261
  // return
7135
- return {elements: dicomElements, offset: offset};
7262
+ return dicomElements;
7136
7263
  };
7137
7264
 
7138
7265
  /*eslint max-len:0*/
@@ -13399,7 +13526,7 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
13399
13526
  *
13400
13527
  * @returns {string} The id of the div.
13401
13528
  */
13402
- this.getElementId = function () {
13529
+ this.getDivId = function () {
13403
13530
  return containerDiv.id;
13404
13531
  };
13405
13532
 
@@ -14191,9 +14318,9 @@ dwv.gui.Stage = function () {
14191
14318
  * @param {string} id The element id to find.
14192
14319
  * @returns {dwv.gui.LayerGroup} The layer group.
14193
14320
  */
14194
- this.getLayerGroupWithElementId = function (id) {
14321
+ this.getLayerGroupByDivId = function (id) {
14195
14322
  return layerGroups.find(function (item) {
14196
- return item.getElementId() === id;
14323
+ return item.getDivId() === id;
14197
14324
  });
14198
14325
  };
14199
14326
 
@@ -14820,6 +14947,24 @@ dwv.gui.ViewLayer = function (containerDiv) {
14820
14947
  */
14821
14948
  var listenerHandler = new dwv.utils.ListenerHandler();
14822
14949
 
14950
+ /**
14951
+ * Image smoothing flag.
14952
+ * see: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/imageSmoothingEnabled
14953
+ *
14954
+ * @private
14955
+ * @type {boolean}
14956
+ */
14957
+ var imageSmoothingEnabled = false;
14958
+
14959
+ /**
14960
+ * Set the imageSmoothingEnabled flag value.
14961
+ *
14962
+ * @param {boolean} flag True to enable smoothing.
14963
+ */
14964
+ this.enableImageSmoothing = function (flag) {
14965
+ imageSmoothingEnabled = flag;
14966
+ };
14967
+
14823
14968
  /**
14824
14969
  * Set the associated view.
14825
14970
  *
@@ -15165,7 +15310,7 @@ dwv.gui.ViewLayer = function (containerDiv) {
15165
15310
  );
15166
15311
 
15167
15312
  // disable smoothing (set just before draw, could be reset by resize)
15168
- context.imageSmoothingEnabled = false;
15313
+ context.imageSmoothingEnabled = imageSmoothingEnabled;
15169
15314
  // draw image
15170
15315
  context.drawImage(offscreenCanvas, 0, 0);
15171
15316
 
@@ -15429,7 +15574,10 @@ dwv.gui.ViewLayer = function (containerDiv) {
15429
15574
  var skip = typeof event.skipGenerate !== 'undefined' &&
15430
15575
  event.skipGenerate === true;
15431
15576
  if (!skip) {
15432
- var valid = typeof event.valid === 'undefined' || event.valid;
15577
+ var valid = true;
15578
+ if (typeof event.valid !== 'undefined') {
15579
+ valid = event.valid;
15580
+ }
15433
15581
  // clear for non valid events
15434
15582
  if (!valid) {
15435
15583
  // clear only once
@@ -16796,12 +16944,12 @@ dwv.image.Geometry = function (origin, size, spacing, orientation, time) {
16796
16944
  * the input one.
16797
16945
  *
16798
16946
  * @param {number} time The time point to check.
16799
- * @returns {number} The count.
16947
+ * @returns {number|undefined} The count.
16800
16948
  */
16801
16949
  this.getCurrentNumberOfSlicesBeforeTime = function (time) {
16802
16950
  var keys = Object.keys(timeOrigins);
16803
16951
  if (keys.length === 0) {
16804
- return;
16952
+ return undefined;
16805
16953
  }
16806
16954
  var count = 0;
16807
16955
  for (var i = 0; i < keys.length; ++i) {
@@ -19165,7 +19313,7 @@ dwv.image.getRegionSliceIterator = function (
19165
19313
  * @param {dwv.math.Point} position The current position.
19166
19314
  * @param {boolean} isRescaled Flag for rescaled values (default false).
19167
19315
  * @param {Array} regions An array of regions.
19168
- * @returns {object} The slice iterator.
19316
+ * @returns {object|undefined} The slice iterator.
19169
19317
  */
19170
19318
  dwv.image.getVariableRegionSliceIterator = function (
19171
19319
  image, position, isRescaled, regions) {
@@ -19217,7 +19365,7 @@ dwv.image.getVariableRegionSliceIterator = function (
19217
19365
 
19218
19366
  // exit if no offsets
19219
19367
  if (offsetRegions.length === 0) {
19220
- return;
19368
+ return undefined;
19221
19369
  }
19222
19370
 
19223
19371
  var startOffset = size.indexToOffset(position.getWithNew2D(
@@ -19558,21 +19706,21 @@ dwv.dicom.checkTag = function (rootElement, tagDefinition) {
19558
19706
  /**
19559
19707
  * List of DICOM Seg required tags.
19560
19708
  */
19561
- dwv.dicom.requiredSegDicomTags = [
19709
+ dwv.dicom.requiredDicomSegTags = [
19562
19710
  {
19563
- name: 'Transfer Syntax UID',
19711
+ name: 'TransferSyntaxUID',
19564
19712
  tag: 'x00020010',
19565
19713
  type: '1',
19566
19714
  enum: ['1.2.840.10008.1.2.1']
19567
19715
  },
19568
19716
  {
19569
- name: 'Media Storage SOP Class UID',
19717
+ name: 'MediaStorageSOPClassUID',
19570
19718
  tag: 'x00020002',
19571
19719
  type: '1',
19572
19720
  enum: ['1.2.840.10008.5.1.4.1.1.66.4']
19573
19721
  },
19574
19722
  {
19575
- name: 'SOP Class UID',
19723
+ name: 'SOPClassUID',
19576
19724
  tag: 'x00020002',
19577
19725
  type: '1',
19578
19726
  enum: ['1.2.840.10008.5.1.4.1.1.66.4']
@@ -19584,61 +19732,75 @@ dwv.dicom.requiredSegDicomTags = [
19584
19732
  enum: ['SEG']
19585
19733
  },
19586
19734
  {
19587
- name: 'Segmentation Type',
19735
+ name: 'SegmentationType',
19588
19736
  tag: 'x00620001',
19589
19737
  type: '1',
19590
19738
  enum: ['BINARY']
19591
19739
  },
19592
19740
  {
19593
- name: 'Dimension Organization Type',
19741
+ name: 'DimensionOrganizationType',
19594
19742
  tag: 'x00209311',
19595
19743
  type: '3',
19596
19744
  enum: ['3D']
19597
19745
  },
19598
19746
  {
19599
- name: 'Image Type',
19747
+ name: 'ImageType',
19600
19748
  tag: 'x00080008',
19601
19749
  type: '1',
19602
19750
  enum: [['DERIVED', 'PRIMARY']]
19603
19751
  },
19604
19752
  {
19605
- name: 'Samples Per Pixel',
19753
+ name: 'SamplesPerPixel',
19606
19754
  tag: 'x00280002',
19607
19755
  type: '1',
19608
19756
  enum: [1]
19609
19757
  },
19610
19758
  {
19611
- name: 'Photometric Interpretation',
19759
+ name: 'PhotometricInterpretation',
19612
19760
  tag: 'x00280004',
19613
19761
  type: '1',
19614
19762
  enum: ['MONOCHROME2']
19615
19763
  },
19616
19764
  {
19617
- name: 'Pixel Representation',
19765
+ name: 'PixelRepresentation',
19618
19766
  tag: 'x00280103',
19619
19767
  type: '1',
19620
19768
  enum: [0]
19621
19769
  },
19622
19770
  {
19623
- name: 'Bits Allocated',
19771
+ name: 'BitsAllocated',
19624
19772
  tag: 'x00280100',
19625
19773
  type: '1',
19626
19774
  enum: [1]
19627
19775
  },
19628
19776
  {
19629
- name: 'Bits Stored',
19777
+ name: 'BitsStored',
19630
19778
  tag: 'x00280101',
19631
19779
  type: '1',
19632
19780
  enum: [1]
19633
19781
  },
19634
19782
  {
19635
- name: 'High Bit',
19783
+ name: 'HighBit',
19636
19784
  tag: 'x00280102',
19637
19785
  type: '1',
19638
19786
  enum: [0]
19639
19787
  },
19640
19788
  ];
19641
19789
 
19790
+ /**
19791
+ * Get the default DICOM seg tags as an object.
19792
+ *
19793
+ * @returns {object} The default tags.
19794
+ */
19795
+ dwv.dicom.getDefaultDicomSegJson = function () {
19796
+ var tags = {};
19797
+ for (var i = 0; i < dwv.dicom.requiredDicomSegTags.length; ++i) {
19798
+ var reqTag = dwv.dicom.requiredDicomSegTags[i];
19799
+ tags[reqTag.name] = reqTag.enum[0];
19800
+ }
19801
+ return tags;
19802
+ };
19803
+
19642
19804
  /**
19643
19805
  * Check the dimension organization from a dicom element.
19644
19806
  *
@@ -19652,7 +19814,7 @@ dwv.dicom.getDimensionOrganization = function (rootElement) {
19652
19814
  throw new Error('Unsupported dimension organization sequence length');
19653
19815
  }
19654
19816
  // Dimension Organization UID
19655
- var orgs = [dwv.dicom.cleanString(orgSq[0].x00209164.value[0])];
19817
+ var orgUID = dwv.dicom.cleanString(orgSq[0].x00209164.value[0]);
19656
19818
 
19657
19819
  // Dimension Index Sequence (conditionally required)
19658
19820
  var indices = [];
@@ -19662,38 +19824,46 @@ dwv.dicom.getDimensionOrganization = function (rootElement) {
19662
19824
  if (indexSq.length !== 2) {
19663
19825
  throw new Error('Unsupported dimension index sequence length');
19664
19826
  }
19827
+ var indexPointer;
19665
19828
  for (var i = 0; i < indexSq.length; ++i) {
19666
19829
  // Dimension Organization UID (required)
19667
19830
  var indexOrg = dwv.dicom.cleanString(indexSq[i].x00209164.value[0]);
19668
- if (indexOrg !== orgs[0]) {
19831
+ if (indexOrg !== orgUID) {
19669
19832
  throw new Error(
19670
19833
  'Dimension Index Sequence contains a unknown Dimension Organization');
19671
19834
  }
19672
19835
  // Dimension Index Pointer (required)
19673
- var indexPointer =
19674
- dwv.dicom.cleanString(indexSq[i].x00209165.value[0]);
19836
+ indexPointer = dwv.dicom.cleanString(indexSq[i].x00209165.value[0]);
19675
19837
 
19676
19838
  var index = {
19677
- organization: indexOrg,
19678
- pointer: indexPointer
19839
+ DimensionOrganizationUID: indexOrg,
19840
+ DimensionIndexPointer: indexPointer
19679
19841
  };
19680
19842
  // Dimension Description Label (optional)
19681
19843
  if (typeof indexSq[i].x00209421 !== 'undefined') {
19682
- index.label =
19844
+ index.DimensionDescriptionLabel =
19683
19845
  dwv.dicom.cleanString(indexSq[i].x00209421.value[0]);
19684
19846
  }
19685
19847
  // store
19686
19848
  indices.push(index);
19687
19849
  }
19688
19850
  // expecting Image Position at last position
19689
- if (indices[1].pointer !== '(0020,0032)') {
19851
+ if (indexPointer !== '(0020,0032)') {
19690
19852
  throw new Error('Unsupported non image position as last index');
19691
19853
  }
19692
19854
  }
19693
19855
 
19694
19856
  return {
19695
- organizations: orgs,
19696
- indices: indices
19857
+ organizations: {
19858
+ value: [
19859
+ {
19860
+ DimensionOrganizationUID: orgUID
19861
+ }
19862
+ ]
19863
+ },
19864
+ indices: {
19865
+ value: indices
19866
+ }
19697
19867
  };
19698
19868
  };
19699
19869
 
@@ -19999,8 +20169,8 @@ dwv.image.MaskFactory = function () {};
19999
20169
  dwv.image.MaskFactory.prototype.create = function (
20000
20170
  dicomElements, pixelBuffer) {
20001
20171
  // check required and supported tags
20002
- for (var d = 0; d < dwv.dicom.requiredSegDicomTags.length; ++d) {
20003
- dwv.dicom.checkTag(dicomElements, dwv.dicom.requiredSegDicomTags[d]);
20172
+ for (var d = 0; d < dwv.dicom.requiredDicomSegTags.length; ++d) {
20173
+ dwv.dicom.checkTag(dicomElements, dwv.dicom.requiredDicomSegTags[d]);
20004
20174
  }
20005
20175
 
20006
20176
  // columns
@@ -20066,12 +20236,7 @@ dwv.image.MaskFactory.prototype.create = function (
20066
20236
  var planeOrientationSeq = funcGroup0.x00209116;
20067
20237
  if (planeOrientationSeq.value.length !== 0) {
20068
20238
  // should be only one
20069
- var orientArray = planeOrientationSeq.value[0].x00200037.value;
20070
- imageOrientationPatient = orientArray.map(
20071
- function (x) {
20072
- return parseFloat(x);
20073
- }
20074
- );
20239
+ imageOrientationPatient = planeOrientationSeq.value[0].x00200037.value;
20075
20240
  } else {
20076
20241
  dwv.logger.warn(
20077
20242
  'No shared functional group plane orientation sequence items.');
@@ -20158,20 +20323,53 @@ dwv.image.MaskFactory.prototype.create = function (
20158
20323
  throw new Error('No imageOrientationPatient found for DICOM SEG');
20159
20324
  }
20160
20325
 
20161
- // add missing posPats
20326
+ var point3DFromArray = function (arr) {
20327
+ return new dwv.math.Point3D(arr[0], arr[1], arr[2]);
20328
+ };
20329
+
20330
+ // create geometry
20331
+ var origin = point3DFromArray(framePosPats[0]);
20332
+ var rowCosines = new dwv.math.Vector3D(
20333
+ parseFloat(imageOrientationPatient[0]),
20334
+ parseFloat(imageOrientationPatient[1]),
20335
+ parseFloat(imageOrientationPatient[2]));
20336
+ var colCosines = new dwv.math.Vector3D(
20337
+ parseFloat(imageOrientationPatient[3]),
20338
+ parseFloat(imageOrientationPatient[4]),
20339
+ parseFloat(imageOrientationPatient[5]));
20340
+ var normal = rowCosines.crossProduct(colCosines);
20341
+ /* eslint-disable array-element-newline */
20342
+ var orientationMatrix = new dwv.math.Matrix33([
20343
+ rowCosines.getX(), colCosines.getX(), normal.getX(),
20344
+ rowCosines.getY(), colCosines.getY(), normal.getY(),
20345
+ rowCosines.getZ(), colCosines.getZ(), normal.getZ()
20346
+ ]);
20347
+ var geometry = new dwv.image.Geometry(
20348
+ origin, size, spacing, orientationMatrix);
20349
+
20350
+ // add possibly missing posPats
20162
20351
  var posPats = [];
20163
- var sliceSpacing = spacing.get(2);
20164
- for (var g = 0; g < framePosPats.length - 1; ++g) {
20352
+ posPats.push(framePosPats[0]);
20353
+ var sliceIndex = 0;
20354
+ for (var g = 1; g < framePosPats.length; ++g) {
20355
+ ++sliceIndex;
20356
+ var index = new dwv.math.Index([0, 0, sliceIndex]);
20357
+ var point = geometry.indexToWorld(index).get3D();
20358
+ var framePosPat = point3DFromArray(framePosPats[g]);
20359
+ // check if more pos pats are needed
20360
+ var dist = framePosPat.getDistance(point);
20361
+ // TODO: good threshold?
20362
+ while (dist > 1e-4) {
20363
+ dwv.logger.debug('Adding intermediate pos pats for DICOM seg');
20364
+ posPats.push([point.getX(), point.getY(), point.getZ()]);
20365
+ ++sliceIndex;
20366
+ index = new dwv.math.Index([0, 0, sliceIndex]);
20367
+ point = geometry.indexToWorld(index).get3D();
20368
+ dist = framePosPat.getDistance(point);
20369
+ }
20370
+ // add frame pos pat
20165
20371
  posPats.push(framePosPats[g]);
20166
- var nextZ = framePosPats[g][2] - sliceSpacing;
20167
- var diff = Math.abs(nextZ - framePosPats[g + 1][2]);
20168
- while (diff >= sliceSpacing) {
20169
- posPats.push([framePosPats[g][0], framePosPats[g][1], nextZ]);
20170
- nextZ -= sliceSpacing;
20171
- diff = Math.abs(nextZ - framePosPats[g + 1][2]);
20172
- }
20173
20372
  }
20174
- posPats.push(framePosPats[framePosPats.length - 1]);
20175
20373
 
20176
20374
  var getFindSegmentFunc = function (number) {
20177
20375
  return function (item) {
@@ -20187,7 +20385,6 @@ dwv.image.MaskFactory.prototype.create = function (
20187
20385
  buffer.fill(0);
20188
20386
  // merge frame buffers
20189
20387
  var sliceOffset = null;
20190
- var sliceIndex = null;
20191
20388
  var frameOffset = null;
20192
20389
  for (var f = 0; f < frameInfos.length; ++f) {
20193
20390
  // get the slice index from the position in the posPat array
@@ -20213,16 +20410,6 @@ dwv.image.MaskFactory.prototype.create = function (
20213
20410
  }
20214
20411
  }
20215
20412
 
20216
- if (typeof spacing === 'undefined') {
20217
- throw Error('No spacing found in DICOM seg file.');
20218
- }
20219
-
20220
- // geometry
20221
- var point3DFromArray = function (arr) {
20222
- return new dwv.math.Point3D(arr[0], arr[1], arr[2]);
20223
- };
20224
- var origin = point3DFromArray(posPats[0]);
20225
- var geometry = new dwv.image.Geometry(origin, size, spacing);
20226
20413
  var uids = [0];
20227
20414
  for (var m = 1; m < numberOfSlices; ++m) {
20228
20415
  // args: origin, volumeNumber, uid, index, increment
@@ -20236,32 +20423,52 @@ dwv.image.MaskFactory.prototype.create = function (
20236
20423
  image.setPhotometricInterpretation('RGB');
20237
20424
  }
20238
20425
  // meta information
20239
- var meta = {
20240
- Modality: 'SEG',
20241
- SegmentationType: 'BINARY',
20242
- DimensionOrganizationType: '3D',
20243
- DimensionOrganizations: dimension.organizations,
20244
- DimensionIndices: dimension.indices,
20245
- BitsStored: 1,
20246
- PatientName: dwv.dicom.cleanString(dicomElements.getFromKey('x00100010')),
20247
- PatientID: dwv.dicom.cleanString(dicomElements.getFromKey('x00100020')),
20248
- PatientBirthDate: dicomElements.getFromKey('x00100030'),
20249
- PatientSex: dwv.dicom.cleanString(dicomElements.getFromKey('x00100040')),
20250
- StudyInstanceUID: dicomElements.getFromKey('x0020000D'),
20251
- SeriesInstanceUID: dicomElements.getFromKey('x0020000E'),
20252
- SeriesNumber: dicomElements.getFromKey('x00200011'),
20253
- ImageOrientationPatient: imageOrientationPatient,
20254
- custom: {
20255
- segments: segments,
20256
- frameInfos: frameInfos,
20257
- SOPInstanceUID: dicomElements.getFromKey('x00080018')
20258
- }
20259
- };
20426
+ var meta = dwv.dicom.getDefaultDicomSegJson();
20427
+ // Study
20428
+ meta.StudyDate = dicomElements.getFromKey('x00080020');
20429
+ meta.StudyTime = dicomElements.getFromKey('x00080030');
20430
+ meta.StudyInstanceUID = dicomElements.getFromKey('x0020000D');
20431
+ meta.StudyID = dicomElements.getFromKey('x00200010');
20432
+ // Series
20433
+ meta.SeriesInstanceUID = dicomElements.getFromKey('x0020000E');
20434
+ meta.SeriesNumber = dicomElements.getFromKey('x00200011');
20435
+ // ReferringPhysicianName
20436
+ meta.ReferringPhysicianName = dicomElements.getFromKey('x00080090');
20437
+ // patient info
20438
+ meta.PatientName =
20439
+ dwv.dicom.cleanString(dicomElements.getFromKey('x00100010'));
20440
+ meta.PatientID = dwv.dicom.cleanString(dicomElements.getFromKey('x00100020'));
20441
+ meta.PatientBirthDate = dicomElements.getFromKey('x00100030');
20442
+ meta.PatientSex =
20443
+ dwv.dicom.cleanString(dicomElements.getFromKey('x00100040'));
20444
+ // Enhanced General Equipment Module
20445
+ meta.Manufacturer = dicomElements.getFromKey('x00080070');
20446
+ meta.ManufacturerModelName = dicomElements.getFromKey('x00081090');
20447
+ meta.DeviceSerialNumber = dicomElements.getFromKey('x00181000');
20448
+ meta.SoftwareVersions = dicomElements.getFromKey('x00181020');
20449
+ // dicom seg dimension
20450
+ meta.DimensionOrganizationSequence = dimension.organizations;
20451
+ meta.DimensionIndexSequence = dimension.indices;
20452
+ // custom
20453
+ meta.custom = {
20454
+ segments: segments,
20455
+ frameInfos: frameInfos,
20456
+ SOPInstanceUID: dicomElements.getFromKey('x00080018')
20457
+ };
20458
+
20459
+ // number of files: in this case equal to number slices,
20460
+ // used to calculate buffer size
20461
+ meta.numberOfFiles = numberOfSlices;
20260
20462
  // FrameOfReferenceUID (optional)
20261
20463
  var frameOfReferenceUID = dicomElements.getFromKey('x00200052');
20262
20464
  if (frameOfReferenceUID) {
20263
20465
  meta.FrameOfReferenceUID = frameOfReferenceUID;
20264
20466
  }
20467
+ // LossyImageCompression (optional)
20468
+ var lossyImageCompression = dicomElements.getFromKey('x00282110');
20469
+ if (lossyImageCompression) {
20470
+ meta.LossyImageCompression = lossyImageCompression;
20471
+ }
20265
20472
 
20266
20473
  image.setMeta(meta);
20267
20474
 
@@ -26815,7 +27022,7 @@ dwv.math.getMatrixInverse = function (m) {
26815
27022
  var det = m00 * a1212 + m01 * a2012 + m02 * a0112;
26816
27023
  if (det === 0) {
26817
27024
  dwv.logger.warn('Cannot invert 3*3 matrix with zero determinant.');
26818
- return;
27025
+ return undefined;
26819
27026
  }
26820
27027
  det = 1 / det;
26821
27028
 
@@ -30244,12 +30451,12 @@ dwv.tool.Draw.prototype.hasShape = function (name) {
30244
30451
  * Get the minimum position in a groups' anchors.
30245
30452
  *
30246
30453
  * @param {object} group The group that contains anchors.
30247
- * @returns {object} The minimum position as {x,y}.
30454
+ * @returns {object|undefined} The minimum position as {x,y}.
30248
30455
  */
30249
30456
  dwv.tool.getAnchorMin = function (group) {
30250
30457
  var anchors = group.find('.anchor');
30251
30458
  if (anchors.length === 0) {
30252
- return;
30459
+ return undefined;
30253
30460
  }
30254
30461
  var minX = anchors[0].x();
30255
30462
  var minY = anchors[0].y();
@@ -35462,7 +35669,7 @@ dwv.utils.uint8ArrayToString = function (arr) {
35462
35669
  * @param {Function} callbackFn The find function.
35463
35670
  * @param {number} start The array start index.
35464
35671
  * @param {number} end The array end index.
35465
- * @returns {number} The index where the element was found.
35672
+ * @returns {number|undefined} The index where the element was found.
35466
35673
  */
35467
35674
  dwv.utils.findInArraySubset = function (arr, callbackFn, start, end) {
35468
35675
  // check inputs
@@ -35483,7 +35690,7 @@ dwv.utils.findInArraySubset = function (arr, callbackFn, start, end) {
35483
35690
  return i;
35484
35691
  }
35485
35692
  }
35486
- return;
35693
+ return undefined;
35487
35694
  };
35488
35695
 
35489
35696
  /**
@@ -36012,9 +36219,9 @@ dwv.env.askModernizr = function (property) {
36012
36219
  dwv.env.hasFileApi = function () {
36013
36220
  // regular test does not work on Safari 5
36014
36221
  var isSafari5 = (navigator.appVersion.indexOf('Safari') !== -1) &&
36015
- (navigator.appVersion.indexOf('Chrome') === -1) &&
36016
- ((navigator.appVersion.indexOf('5.0.') !== -1) ||
36017
- (navigator.appVersion.indexOf('5.1.') !== -1));
36222
+ (navigator.appVersion.indexOf('Chrome') === -1) &&
36223
+ ((navigator.appVersion.indexOf('5.0.') !== -1) ||
36224
+ (navigator.appVersion.indexOf('5.1.') !== -1));
36018
36225
  if (isSafari5) {
36019
36226
  dwv.logger.warn('Assuming FileAPI support for Safari5...');
36020
36227
  return true;
@@ -36030,9 +36237,9 @@ dwv.env.hasFileApi = function () {
36030
36237
  */
36031
36238
  dwv.env.hasXmlHttpRequest = function () {
36032
36239
  return dwv.env.askModernizr('xhrresponsetype') &&
36033
- dwv.env.askModernizr('xhrresponsetypearraybuffer') &&
36034
- dwv.env.askModernizr('xhrresponsetypetext') &&
36035
- 'XMLHttpRequest' in window && 'withCredentials' in new XMLHttpRequest();
36240
+ dwv.env.askModernizr('xhrresponsetypearraybuffer') &&
36241
+ dwv.env.askModernizr('xhrresponsetypetext') &&
36242
+ 'XMLHttpRequest' in window && 'withCredentials' in new XMLHttpRequest();
36036
36243
  };
36037
36244
 
36038
36245
  /**
@@ -36065,8 +36272,7 @@ dwv.env.hasInputDirectory = function () {
36065
36272
  return dwv.env.askModernizr('fileinputdirectory');
36066
36273
  };
36067
36274
 
36068
-
36069
- //only check at startup (since we propose a replacement)
36275
+ // only check at startup (since we propose a replacement)
36070
36276
  dwv.env._hasTypedArraySlice =
36071
36277
  (typeof Uint8Array.prototype.slice !== 'undefined');
36072
36278
 
@@ -36093,7 +36299,7 @@ dwv.env.hasFloat64Array = function () {
36093
36299
  return dwv.env._hasFloat64Array;
36094
36300
  };
36095
36301
 
36096
- //only check at startup (since we propose a replacement)
36302
+ // only check at startup (since we propose a replacement)
36097
36303
  dwv.env._hasClampedArray = ('Uint8ClampedArray' in window);
36098
36304
 
36099
36305
  /**
@@ -36120,6 +36326,30 @@ dwv.env.hasBigInt = function () {
36120
36326
  return dwv.env._hasBigint;
36121
36327
  };
36122
36328
 
36329
+ // Check if the TextDecoder is defined
36330
+ dwv.env._hasTextDecoder = (typeof TextDecoder !== 'undefined');
36331
+
36332
+ /**
36333
+ * Does the environement provide TextDecoder.
36334
+ *
36335
+ * @returns {boolean} True if the env supports the feature.
36336
+ */
36337
+ dwv.env.hasTextDecoder = function () {
36338
+ return dwv.env._hasTextDecoder;
36339
+ };
36340
+
36341
+ // Check if the TextEncoder is defined
36342
+ dwv.env._hasTextEncoder = (typeof TextEncoder !== 'undefined');
36343
+
36344
+ /**
36345
+ * Does the environement provide TextEncoder.
36346
+ *
36347
+ * @returns {boolean} True if the env supports the feature.
36348
+ */
36349
+ dwv.env.hasTextEncoder = function () {
36350
+ return dwv.env._hasTextEncoder;
36351
+ };
36352
+
36123
36353
  /**
36124
36354
  * Browser checks to see if it can run dwv. Throws an error if not.
36125
36355
  * Silently replaces basic functions.
@@ -36144,6 +36374,12 @@ dwv.env.check = function () {
36144
36374
  message = 'The Typed arrays are not supported in this browser. ';
36145
36375
  throw new Error(message);
36146
36376
  }
36377
+ // Check text encoding/decoding
36378
+ if (!dwv.env.hasTextDecoder() || !dwv.env.hasTextEncoder()) {
36379
+ message = 'Text decoding/encoding is not supported in this browser.';
36380
+ throw new Error(message);
36381
+ }
36382
+
36147
36383
  // Check Float64
36148
36384
  if (!dwv.env.hasFloat64Array()) {
36149
36385
  dwv.logger.warn('Float64Array is not supported in this browser. ' +