dwv 0.31.0-beta.14 → 0.31.0-beta.15

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.15 2022-10-28 12:46:58 */
2
2
  // Inspired from umdjs
3
3
  // See https://github.com/umdjs/umd/blob/master/templates/returnExports.js
4
4
  (function (root, factory) {
@@ -617,11 +617,8 @@ dwv.App = function () {
617
617
  * To be called once the image is loaded.
618
618
  */
619
619
  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
- }
620
+ stage.syncLayerGroupScale();
621
+
625
622
  };
626
623
 
627
624
  /**
@@ -2031,6 +2028,8 @@ dwv.ctrl.DrawController = function (konvaLayer) {
2031
2028
 
2032
2029
  // udpate current layer
2033
2030
  konvaLayer.draw();
2031
+
2032
+ return true;
2034
2033
  };
2035
2034
 
2036
2035
  /**
@@ -3240,33 +3239,6 @@ dwv.dicom.DataReader = function (buffer, isLittleEndian) {
3240
3239
  isLittleEndian = true;
3241
3240
  }
3242
3241
 
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
3242
  /**
3271
3243
  * Is the Native endianness Little Endian.
3272
3244
  *
@@ -3301,6 +3273,16 @@ dwv.dicom.DataReader = function (buffer, isLittleEndian) {
3301
3273
  return view.getUint16(byteOffset, isLittleEndian);
3302
3274
  };
3303
3275
 
3276
+ /**
3277
+ * Read Int16 (2 bytes) data.
3278
+ *
3279
+ * @param {number} byteOffset The offset to start reading from.
3280
+ * @returns {number} The read data.
3281
+ */
3282
+ this.readInt16 = function (byteOffset) {
3283
+ return view.getInt16(byteOffset, isLittleEndian);
3284
+ };
3285
+
3304
3286
  /**
3305
3287
  * Read Uint32 (4 bytes) data.
3306
3288
  *
@@ -3614,32 +3596,6 @@ dwv.dicom.DataReader = function (buffer, isLittleEndian) {
3614
3596
  }
3615
3597
  return data;
3616
3598
  };
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
3599
  }; // class DataReader
3644
3600
 
3645
3601
  /**
@@ -3673,33 +3629,6 @@ dwv.dicom.DataWriter = function (buffer, isLittleEndian) {
3673
3629
  isLittleEndian = true;
3674
3630
  }
3675
3631
 
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
3632
  // private DataView
3704
3633
  var view = new DataView(buffer);
3705
3634
 
@@ -3841,31 +3770,6 @@ dwv.dicom.DataWriter = function (buffer, isLittleEndian) {
3841
3770
  return byteOffset + Uint16Array.BYTES_PER_ELEMENT;
3842
3771
  };
3843
3772
 
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
3773
  }; // class DataWriter
3870
3774
 
3871
3775
  /**
@@ -4105,10 +4009,9 @@ dwv.dicom.DicomElementsWrapper = function (dicomElements) {
4105
4009
  * @returns {string} The tag name.
4106
4010
  */
4107
4011
  this.getTagName = function (tag) {
4108
- var tagObj = new dwv.dicom.Tag(tag.group, tag.element);
4109
- var name = tagObj.getNameFromDictionary();
4012
+ var name = tag.getNameFromDictionary();
4110
4013
  if (name === null) {
4111
- name = tagObj.getKey2();
4014
+ name = tag.getKey2();
4112
4015
  }
4113
4016
  return name;
4114
4017
  };
@@ -4123,8 +4026,7 @@ dwv.dicom.DicomElementsWrapper = function (dicomElements) {
4123
4026
  // element value
4124
4027
  var value = null;
4125
4028
 
4126
- var isPixel = dicomElement.tag.group === '0x7FE0' &&
4127
- dicomElement.tag.element === '0x0010';
4029
+ var isPixel = dwv.dicom.isPixelDataTag(dicomElement.tag);
4128
4030
 
4129
4031
  var vr = dicomElement.vr;
4130
4032
  if (vr === 'SQ' &&
@@ -4153,8 +4055,8 @@ dwv.dicom.DicomElementsWrapper = function (dicomElements) {
4153
4055
  // return
4154
4056
  return {
4155
4057
  value: value,
4156
- group: dicomElement.tag.group,
4157
- element: dicomElement.tag.element,
4058
+ group: dicomElement.tag.getGroup(),
4059
+ element: dicomElement.tag.getElement(),
4158
4060
  vr: vr,
4159
4061
  vl: dicomElement.vl
4160
4062
  };
@@ -4181,7 +4083,7 @@ dwv.dicom.DicomElementsWrapper = function (dicomElements) {
4181
4083
  var checkHeader = true;
4182
4084
  for (var i = 0, leni = keys.length; i < leni; ++i) {
4183
4085
  dicomElement = dicomElements[keys[i]];
4184
- if (checkHeader && dicomElement.tag.group !== '0x0002') {
4086
+ if (checkHeader && dicomElement.tag.getGroup() !== '0x0002') {
4185
4087
  result += '\n';
4186
4088
  result += '# Dicom-Data-Set\n';
4187
4089
  result += '# Used TransferSyntax: ';
@@ -4230,9 +4132,8 @@ dwv.dicom.DicomElementsWrapper.prototype.getElementValueAsString = function (
4230
4132
  if (dicomElement.vr !== 'SQ' &&
4231
4133
  dicomElement.value.length === 1 && dicomElement.value[0] === '') {
4232
4134
  str += '(no value available)';
4233
- } else if (dicomElement.tag.group === '0x7FE0' &&
4234
- dicomElement.tag.element === '0x0010' &&
4235
- dicomElement.vl === 'u/l') {
4135
+ } else if (dwv.dicom.isPixelDataTag(dicomElement.tag) &&
4136
+ dicomElement.undefinedLength) {
4236
4137
  str = '(PixelSequence)';
4237
4138
  } else if (dicomElement.vr === 'DA' && pretty) {
4238
4139
  var daValue = dicomElement.value[0];
@@ -4332,8 +4233,7 @@ dwv.dicom.DicomElementsWrapper.prototype.getElementAsString = function (
4332
4233
  prefix = prefix || '';
4333
4234
 
4334
4235
  // get tag anme from dictionary
4335
- var tag = new dwv.dicom.Tag(
4336
- dicomElement.tag.group, dicomElement.tag.element);
4236
+ var tag = dicomElement.tag;
4337
4237
  var tagName = tag.getNameFromDictionary();
4338
4238
 
4339
4239
  var deSize = dicomElement.value.length;
@@ -4343,25 +4243,23 @@ dwv.dicom.DicomElementsWrapper.prototype.getElementAsString = function (
4343
4243
  }
4344
4244
 
4345
4245
  // no size for delimitations
4346
- if (dicomElement.tag.group === '0xFFFE' && (
4347
- dicomElement.tag.element === '0xE00D' ||
4348
- dicomElement.tag.element === '0xE0DD')) {
4246
+ if (dwv.dicom.isItemDelimitationItemTag(dicomElement.tag) ||
4247
+ dwv.dicom.isSequenceDelimitationItemTag(dicomElement.tag)) {
4349
4248
  deSize = 0;
4350
4249
  } else if (isOtherVR) {
4351
4250
  deSize = 1;
4352
4251
  }
4353
4252
 
4354
- var isPixSequence = (dicomElement.tag.group === '0x7FE0' &&
4355
- dicomElement.tag.element === '0x0010' &&
4356
- dicomElement.vl === 'u/l');
4253
+ var isPixSequence = (dwv.dicom.isPixelDataTag(dicomElement.tag) &&
4254
+ dicomElement.undefinedLength);
4357
4255
 
4358
4256
  var line = null;
4359
4257
 
4360
4258
  // (group,element)
4361
4259
  line = '(';
4362
- line += dicomElement.tag.group.substring(2).toLowerCase();
4260
+ line += dicomElement.tag.getGroup().substring(2).toLowerCase();
4363
4261
  line += ',';
4364
- line += dicomElement.tag.element.substring(2).toLowerCase();
4262
+ line += dicomElement.tag.getElement().substring(2).toLowerCase();
4365
4263
  line += ') ';
4366
4264
  // value representation
4367
4265
  line += dicomElement.vr;
@@ -4381,7 +4279,7 @@ dwv.dicom.DicomElementsWrapper.prototype.getElementAsString = function (
4381
4279
  line += ' (PixelSequence #=' + deSize + ')';
4382
4280
  } else if (dicomElement.vr === 'SQ') {
4383
4281
  line += ' (Sequence with';
4384
- if (dicomElement.vl === 'u/l') {
4282
+ if (dicomElement.undefinedLength) {
4385
4283
  line += ' undefined';
4386
4284
  } else {
4387
4285
  line += ' explicit';
@@ -4448,7 +4346,7 @@ dwv.dicom.DicomElementsWrapper.prototype.getElementAsString = function (
4448
4346
  // get the item element
4449
4347
  var itemElement = item.xFFFEE000;
4450
4348
  message = '(Item with';
4451
- if (itemElement.vl === 'u/l') {
4349
+ if (itemElement.undefinedLength) {
4452
4350
  message += ' undefined';
4453
4351
  } else {
4454
4352
  message += ' explicit';
@@ -4468,12 +4366,12 @@ dwv.dicom.DicomElementsWrapper.prototype.getElementAsString = function (
4468
4366
  }
4469
4367
 
4470
4368
  message = '(ItemDelimitationItem';
4471
- if (itemElement.vl !== 'u/l') {
4369
+ if (!itemElement.undefinedLength) {
4472
4370
  message += ' for re-encoding';
4473
4371
  }
4474
4372
  message += ')';
4475
4373
  var itemDelimElement = {
4476
- tag: {group: '0xFFFE', element: '0xE00D'},
4374
+ tag: dwv.dicom.getItemDelimitationItemTag(),
4477
4375
  vr: 'na',
4478
4376
  vl: '0',
4479
4377
  value: [message]
@@ -4484,12 +4382,12 @@ dwv.dicom.DicomElementsWrapper.prototype.getElementAsString = function (
4484
4382
  }
4485
4383
 
4486
4384
  message = '(SequenceDelimitationItem';
4487
- if (dicomElement.vl !== 'u/l') {
4385
+ if (!dicomElement.undefinedLength) {
4488
4386
  message += ' for re-encod.';
4489
4387
  }
4490
4388
  message += ')';
4491
4389
  var sqDelimElement = {
4492
- tag: {group: '0xFFFE', element: '0xE0DD'},
4390
+ tag: dwv.dicom.getSequenceDelimitationItemTag(),
4493
4391
  vr: 'na',
4494
4392
  vl: '0',
4495
4393
  value: [message]
@@ -4507,7 +4405,7 @@ dwv.dicom.DicomElementsWrapper.prototype.getElementAsString = function (
4507
4405
  }
4508
4406
 
4509
4407
  var pixDelimElement = {
4510
- tag: {group: '0xFFFE', element: '0xE0DD'},
4408
+ tag: dwv.dicom.getSequenceDelimitationItemTag(),
4511
4409
  vr: 'na',
4512
4410
  vl: '0',
4513
4411
  value: ['(SequenceDelimitationItem)']
@@ -4621,7 +4519,7 @@ dwv.dicom.DicomElementsWrapper.prototype.getTime = function () {
4621
4519
  * Get the file list from a DICOMDIR
4622
4520
  *
4623
4521
  * @param {object} data The buffer data of the DICOMDIR
4624
- * @returns {Array} The file list as an array ordered by
4522
+ * @returns {Array|undefined} The file list as an array ordered by
4625
4523
  * STUDY > SERIES > IMAGES.
4626
4524
  */
4627
4525
  dwv.dicom.getFileListFromDicomDir = function (data) {
@@ -4634,13 +4532,13 @@ dwv.dicom.getFileListFromDicomDir = function (data) {
4634
4532
  if (typeof elements.x00041220 === 'undefined' ||
4635
4533
  typeof elements.x00041220.value === 'undefined') {
4636
4534
  dwv.logger.warn('No Directory Record Sequence found in DICOMDIR.');
4637
- return;
4535
+ return undefined;
4638
4536
  }
4639
4537
  var dirSeq = elements.x00041220.value;
4640
4538
 
4641
4539
  if (dirSeq.length === 0) {
4642
4540
  dwv.logger.warn('The Directory Record Sequence of the DICOMDIR is empty.');
4643
- return;
4541
+ return undefined;
4644
4542
  }
4645
4543
 
4646
4544
  var records = [];
@@ -4693,7 +4591,7 @@ dwv.dicom = dwv.dicom || {};
4693
4591
  * @returns {string} The version of the library.
4694
4592
  */
4695
4593
  dwv.getVersion = function () {
4696
- return '0.31.0-beta.14';
4594
+ return '0.31.0-beta.15';
4697
4595
  };
4698
4596
 
4699
4597
  /**
@@ -4785,6 +4683,25 @@ dwv.dicom.getUtfLabel = function (charSetTerm) {
4785
4683
  return label;
4786
4684
  };
4787
4685
 
4686
+ /**
4687
+ * Default text decoder
4688
+ */
4689
+ dwv.dicom.DefaultTextDecoder = function () {
4690
+ /**
4691
+ * Decode an input string buffer.
4692
+ *
4693
+ * @param {Uint8Array} buffer The buffer to decode.
4694
+ * @returns {string} The decoded string.
4695
+ */
4696
+ this.decode = function (buffer) {
4697
+ var result = '';
4698
+ for (var i = 0, leni = buffer.length; i < leni; ++i) {
4699
+ result += String.fromCharCode(buffer[i]);
4700
+ }
4701
+ return result;
4702
+ };
4703
+ };
4704
+
4788
4705
  /**
4789
4706
  * Get patient orientation label in the reverse direction.
4790
4707
  *
@@ -5016,7 +4933,7 @@ dwv.dicom.guessTransferSyntax = function (firstDataElement) {
5016
4933
  var oEightGroupBigEndian = '0x0800';
5017
4934
  var oEightGroupLittleEndian = '0x0008';
5018
4935
  // check that group is 0x0008
5019
- var group = firstDataElement.tag.group;
4936
+ var group = firstDataElement.tag.getGroup();
5020
4937
  if (group !== oEightGroupBigEndian &&
5021
4938
  group !== oEightGroupLittleEndian) {
5022
4939
  throw new Error(
@@ -5054,12 +4971,7 @@ dwv.dicom.guessTransferSyntax = function (firstDataElement) {
5054
4971
  }
5055
4972
  // set transfer syntax data element
5056
4973
  var dataElement = {
5057
- tag: {
5058
- group: '0x0002',
5059
- element: '0x0010',
5060
- name: 'x00020010',
5061
- endOffset: 4
5062
- },
4974
+ tag: new dwv.dicom.Tag('0x0002', '0x0010'),
5063
4975
  vr: 'UI'
5064
4976
  };
5065
4977
  dataElement.value = [syntax + ' ']; // even length
@@ -5170,6 +5082,43 @@ dwv.dicom.DicomParser = function () {
5170
5082
  * @type {string}
5171
5083
  */
5172
5084
  var defaultCharacterSet;
5085
+
5086
+ /**
5087
+ * Default text decoder.
5088
+ *
5089
+ * @private
5090
+ * @type {dwv.dicom.DefaultTextDecoder}
5091
+ */
5092
+ var defaultTextDecoder = new dwv.dicom.DefaultTextDecoder();
5093
+
5094
+ /**
5095
+ * Special text decoder.
5096
+ *
5097
+ * @private
5098
+ * @type {dwv.dicom.DefaultTextDecoder|TextDecoder}
5099
+ */
5100
+ var textDecoder = defaultTextDecoder;
5101
+
5102
+ /**
5103
+ * Decode an input string buffer using the default text decoder.
5104
+ *
5105
+ * @param {Uint8Array} buffer The buffer to decode.
5106
+ * @returns {string} The decoded string.
5107
+ */
5108
+ this.decodeString = function (buffer) {
5109
+ return defaultTextDecoder.decode(buffer);
5110
+ };
5111
+
5112
+ /**
5113
+ * Decode an input string buffer using the 'special' text decoder.
5114
+ *
5115
+ * @param {Uint8Array} buffer The buffer to decode.
5116
+ * @returns {string} The decoded string.
5117
+ */
5118
+ this.decodeSpecialString = function (buffer) {
5119
+ return textDecoder.decode(buffer);
5120
+ };
5121
+
5173
5122
  /**
5174
5123
  * Get the default character set.
5175
5124
  *
@@ -5178,14 +5127,30 @@ dwv.dicom.DicomParser = function () {
5178
5127
  this.getDefaultCharacterSet = function () {
5179
5128
  return defaultCharacterSet;
5180
5129
  };
5130
+
5181
5131
  /**
5182
5132
  * Set the default character set.
5183
- * param {String} The character set.
5184
5133
  *
5185
5134
  * @param {string} characterSet The input character set.
5186
5135
  */
5187
5136
  this.setDefaultCharacterSet = function (characterSet) {
5188
5137
  defaultCharacterSet = characterSet;
5138
+ this.setCharacterSet(characterSet);
5139
+ };
5140
+
5141
+ /**
5142
+ * Set the text decoder character set.
5143
+ *
5144
+ * @param {string} characterSet The input character set.
5145
+ */
5146
+ this.setDecoderCharacterSet = function (characterSet) {
5147
+ /**
5148
+ * The text decoder.
5149
+ *
5150
+ * @external TextDecoder
5151
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
5152
+ */
5153
+ textDecoder = new TextDecoder(characterSet);
5189
5154
  };
5190
5155
  };
5191
5156
 
@@ -5212,8 +5177,7 @@ dwv.dicom.DicomParser.prototype.getDicomElements = function () {
5212
5177
  *
5213
5178
  * @param {dwv.dicom.DataReader} reader The raw data reader.
5214
5179
  * @param {number} offset The offset where to start to read.
5215
- * @returns {object} An object containing the tags 'group',
5216
- * 'element' and 'name'.
5180
+ * @returns {object} An object containing the tag and the end offset.
5217
5181
  */
5218
5182
  dwv.dicom.DicomParser.prototype.readTag = function (reader, offset) {
5219
5183
  // group
@@ -5222,59 +5186,22 @@ dwv.dicom.DicomParser.prototype.readTag = function (reader, offset) {
5222
5186
  // element
5223
5187
  var element = reader.readHex(offset);
5224
5188
  offset += Uint16Array.BYTES_PER_ELEMENT;
5225
- // name
5226
- var name = new dwv.dicom.Tag(group, element).getKey();
5227
5189
  // return
5228
5190
  return {
5229
- group: group,
5230
- element: element,
5231
- name: name,
5191
+ tag: new dwv.dicom.Tag(group, element),
5232
5192
  endOffset: offset
5233
5193
  };
5234
5194
  };
5235
5195
 
5236
5196
  /**
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.
5197
+ * Read an item data element.
5271
5198
  *
5272
5199
  * @param {dwv.dicom.DataReader} reader The raw data reader.
5273
5200
  * @param {number} offset The offset where to start to read.
5274
5201
  * @param {boolean} implicit Is the DICOM VR implicit?
5275
5202
  * @returns {object} The item data as a list of data elements.
5276
5203
  */
5277
- dwv.dicom.DicomParser.prototype.readImplicitItemDataElement = function (
5204
+ dwv.dicom.DicomParser.prototype.readItemDataElement = function (
5278
5205
  reader, offset, implicit) {
5279
5206
  var itemData = {};
5280
5207
 
@@ -5283,7 +5210,7 @@ dwv.dicom.DicomParser.prototype.readImplicitItemDataElement = function (
5283
5210
  offset = item.endOffset;
5284
5211
 
5285
5212
  // exit if it is a sequence delimitation item
5286
- if (item.tag.name === 'xFFFEE0DD') {
5213
+ if (dwv.dicom.isSequenceDelimitationItemTag(item.tag)) {
5287
5214
  return {
5288
5215
  data: itemData,
5289
5216
  endOffset: item.endOffset,
@@ -5291,17 +5218,33 @@ dwv.dicom.DicomParser.prototype.readImplicitItemDataElement = function (
5291
5218
  };
5292
5219
  }
5293
5220
 
5294
- // store item
5295
- itemData[item.tag.name] = item;
5221
+ // store item (mainly to keep vl)
5222
+ itemData[item.tag.getKey()] = {
5223
+ tag: item.tag,
5224
+ vr: 'NONE',
5225
+ vl: item.vl,
5226
+ undefinedLength: item.undefinedLength
5227
+ };
5296
5228
 
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;
5229
+ if (!item.undefinedLength) {
5230
+ // explicit VR item: read until the end offset
5231
+ var endOffset = offset;
5232
+ offset -= item.vl;
5233
+ while (offset < endOffset) {
5234
+ item = this.readDataElement(reader, offset, implicit);
5235
+ offset = item.endOffset;
5236
+ itemData[item.tag.getKey()] = item;
5237
+ }
5238
+ } else {
5239
+ // implicit VR item: read until the item delimitation item
5240
+ var isItemDelim = false;
5241
+ while (!isItemDelim) {
5242
+ item = this.readDataElement(reader, offset, implicit);
5243
+ offset = item.endOffset;
5244
+ isItemDelim = dwv.dicom.isItemDelimitationItemTag(item.tag);
5245
+ if (!isItemDelim) {
5246
+ itemData[item.tag.getKey()] = item;
5247
+ }
5305
5248
  }
5306
5249
  }
5307
5250
 
@@ -5335,8 +5278,10 @@ dwv.dicom.DicomParser.prototype.readPixelItemDataElement = function (
5335
5278
  while (!isSeqDelim) {
5336
5279
  item = this.readDataElement(reader, offset, implicit);
5337
5280
  offset = item.endOffset;
5338
- isSeqDelim = item.tag.name === 'xFFFEE0DD';
5281
+ isSeqDelim = dwv.dicom.isSequenceDelimitationItemTag(item.tag);
5339
5282
  if (!isSeqDelim) {
5283
+ // force pixel item vr to OB
5284
+ item.vr = 'OB';
5340
5285
  itemData.push(item);
5341
5286
  }
5342
5287
  }
@@ -5361,9 +5306,9 @@ dwv.dicom.DicomParser.prototype.readPixelItemDataElement = function (
5361
5306
  dwv.dicom.DicomParser.prototype.readDataElement = function (
5362
5307
  reader, offset, implicit) {
5363
5308
  // 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;
5309
+ var readTagRes = this.readTag(reader, offset);
5310
+ var tag = readTagRes.tag;
5311
+ offset = readTagRes.endOffset;
5367
5312
 
5368
5313
  // Value Representation (VR)
5369
5314
  var vr = null;
@@ -5377,7 +5322,7 @@ dwv.dicom.DicomParser.prototype.readDataElement = function (
5377
5322
  }
5378
5323
  is32bitVLVR = true;
5379
5324
  } else {
5380
- vr = reader.readString(offset, 2);
5325
+ vr = this.decodeString(reader.readUint8Array(offset, 2));
5381
5326
  offset += 2 * Uint8Array.BYTES_PER_ELEMENT;
5382
5327
  is32bitVLVR = dwv.dicom.is32bitVLVR(vr);
5383
5328
  // reserved 2 bytes
@@ -5386,7 +5331,7 @@ dwv.dicom.DicomParser.prototype.readDataElement = function (
5386
5331
  }
5387
5332
  }
5388
5333
  } else {
5389
- vr = 'UN';
5334
+ vr = 'NONE';
5390
5335
  is32bitVLVR = true;
5391
5336
  }
5392
5337
 
@@ -5401,14 +5346,13 @@ dwv.dicom.DicomParser.prototype.readDataElement = function (
5401
5346
  }
5402
5347
 
5403
5348
  // check the value of VL
5404
- var vlString = vl;
5349
+ var undefinedLength = false;
5405
5350
  if (vl === 0xffffffff) {
5406
- vlString = 'u/l';
5351
+ undefinedLength = true;
5407
5352
  vl = 0;
5408
5353
  }
5409
5354
 
5410
5355
  // 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
5356
  if (tag.isPrivate() && vr === 'UN' && vl === 0) {
5413
5357
  vr = 'SQ';
5414
5358
  }
@@ -5418,35 +5362,35 @@ dwv.dicom.DicomParser.prototype.readDataElement = function (
5418
5362
 
5419
5363
  // read sequence elements
5420
5364
  var data = null;
5421
- if (dwv.dicom.isPixelDataTag(tag) && vlString === 'u/l') {
5365
+ if (dwv.dicom.isPixelDataTag(tag) && undefinedLength) {
5422
5366
  // pixel data sequence (implicit)
5423
5367
  var pixItemData = this.readPixelItemDataElement(reader, offset, implicit);
5424
5368
  offset = pixItemData.endOffset;
5425
5369
  startOffset += pixItemData.offsetTableVl;
5426
5370
  data = pixItemData.data;
5427
5371
  endOffset = offset;
5372
+ vl = offset - startOffset;
5428
5373
  } else if (vr === 'SQ') {
5429
5374
  // sequence
5430
5375
  data = [];
5431
5376
  var itemData;
5432
- if (vlString !== 'u/l') {
5433
- // explicit VR sequence
5377
+ if (!undefinedLength) {
5434
5378
  if (vl !== 0) {
5435
- // read until the end offset
5379
+ // explicit VR sequence: read until the end offset
5436
5380
  var sqEndOffset = offset + vl;
5437
5381
  while (offset < sqEndOffset) {
5438
- itemData = this.readExplicitItemDataElement(reader, offset, implicit);
5382
+ itemData = this.readItemDataElement(reader, offset, implicit);
5439
5383
  data.push(itemData.data);
5440
5384
  offset = itemData.endOffset;
5441
5385
  }
5442
5386
  endOffset = offset;
5387
+ vl = offset - startOffset;
5443
5388
  }
5444
5389
  } else {
5445
- // implicit VR sequence
5446
- // read until the sequence delimitation item
5390
+ // implicit VR sequence: read until the sequence delimitation item
5447
5391
  var isSeqDelim = false;
5448
5392
  while (!isSeqDelim) {
5449
- itemData = this.readImplicitItemDataElement(reader, offset, implicit);
5393
+ itemData = this.readItemDataElement(reader, offset, implicit);
5450
5394
  isSeqDelim = itemData.isSeqDelim;
5451
5395
  offset = itemData.endOffset;
5452
5396
  // do not store the delimitation item
@@ -5455,19 +5399,24 @@ dwv.dicom.DicomParser.prototype.readDataElement = function (
5455
5399
  }
5456
5400
  }
5457
5401
  endOffset = offset;
5402
+ vl = offset - startOffset;
5458
5403
  }
5459
5404
  }
5460
5405
 
5461
5406
  // return
5462
5407
  var element = {
5463
- tag: tagData,
5408
+ tag: tag,
5464
5409
  vr: vr,
5465
- vl: vlString,
5410
+ vl: vl,
5466
5411
  startOffset: startOffset,
5467
5412
  endOffset: endOffset
5468
5413
  };
5414
+ // only set if true (only for sequences and items)
5415
+ if (undefinedLength) {
5416
+ element.undefinedLength = undefinedLength;
5417
+ }
5469
5418
  if (data) {
5470
- element.elements = data;
5419
+ element.items = data;
5471
5420
  }
5472
5421
  return element;
5473
5422
  };
@@ -5492,56 +5441,46 @@ dwv.dicom.DicomParser.prototype.interpretElement = function (
5492
5441
 
5493
5442
  // data
5494
5443
  var data = null;
5495
- var isPixelDataTag = dwv.dicom.isPixelDataTag(
5496
- new dwv.dicom.Tag(tag.group, tag.element));
5444
+ var isPixelDataTag = dwv.dicom.isPixelDataTag(tag);
5497
5445
  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));
5446
+ if (isPixelDataTag) {
5447
+ if (element.undefinedLength) {
5448
+ // implicit pixel data sequence
5449
+ data = [];
5450
+ for (var j = 0; j < element.items.length; ++j) {
5451
+ data.push(this.interpretElement(
5452
+ element.items[j], reader,
5453
+ pixelRepresentation, bitsAllocated));
5454
+ }
5455
+ // remove non parsed items
5456
+ delete element.items;
5457
+ } else {
5458
+ // check bits allocated and VR
5459
+ // https://dicom.nema.org/medical/dicom/2022a/output/chtml/part05/sect_A.2.html
5460
+ if (bitsAllocated > 8 && vr === 'OB') {
5461
+ dwv.logger.warn(
5462
+ 'Reading DICOM pixel data with bitsAllocated>8 and OB VR.'
5463
+ );
5536
5464
  }
5537
- } else if (bitsAllocated === 64) {
5538
- if (pixelRepresentation === 0) {
5539
- data.push(reader.readUint64Array(offset, vl));
5465
+ // read
5466
+ data = [];
5467
+ if (bitsAllocated === 1) {
5468
+ data.push(reader.readBinaryArray(offset, vl));
5469
+ } else if (bitsAllocated === 8) {
5470
+ if (pixelRepresentation === 0) {
5471
+ data.push(reader.readUint8Array(offset, vl));
5472
+ } else {
5473
+ data.push(reader.readInt8Array(offset, vl));
5474
+ }
5475
+ } else if (bitsAllocated === 16) {
5476
+ if (pixelRepresentation === 0) {
5477
+ data.push(reader.readUint16Array(offset, vl));
5478
+ } else {
5479
+ data.push(reader.readInt16Array(offset, vl));
5480
+ }
5540
5481
  } else {
5541
- data.push(reader.readInt64Array(offset, vl));
5482
+ throw new Error('Unsupported bits allocated: ' + bitsAllocated);
5542
5483
  }
5543
- } else {
5544
- throw new Error('Unsupported bits allocated: ' + bitsAllocated);
5545
5484
  }
5546
5485
  } else if (typeof vrType !== 'undefined') {
5547
5486
  if (vrType === 'Uint8') {
@@ -5563,10 +5502,11 @@ dwv.dicom.DicomParser.prototype.interpretElement = function (
5563
5502
  } else if (vrType === 'Float64') {
5564
5503
  data = reader.readFloat64Array(offset, vl);
5565
5504
  } else if (vrType === 'string') {
5505
+ var stream = reader.readUint8Array(offset, vl);
5566
5506
  if (dwv.dicom.charSetString.includes(vr)) {
5567
- data = reader.readSpecialString(offset, vl);
5507
+ data = this.decodeSpecialString(stream);
5568
5508
  } else {
5569
- data = reader.readString(offset, vl);
5509
+ data = this.decodeString(stream);
5570
5510
  }
5571
5511
  data = data.split('\\');
5572
5512
  } else {
@@ -5603,8 +5543,8 @@ dwv.dicom.DicomParser.prototype.interpretElement = function (
5603
5543
  } else if (vr === 'SQ') {
5604
5544
  // sequence
5605
5545
  data = [];
5606
- for (var k = 0; k < element.elements.length; ++k) {
5607
- var item = element.elements[k];
5546
+ for (var k = 0; k < element.items.length; ++k) {
5547
+ var item = element.items[k];
5608
5548
  var itemData = {};
5609
5549
  var keys = Object.keys(item);
5610
5550
  for (var l = 0; l < keys.length; ++l) {
@@ -5616,6 +5556,11 @@ dwv.dicom.DicomParser.prototype.interpretElement = function (
5616
5556
  }
5617
5557
  data.push(itemData);
5618
5558
  }
5559
+ // remove non parsed elements
5560
+ delete element.items;
5561
+ } else if (vr === 'NONE') {
5562
+ // no VR -> no data
5563
+ data = [];
5619
5564
  } else {
5620
5565
  dwv.logger.warn('Unknown VR: ' + vr);
5621
5566
  }
@@ -5643,6 +5588,9 @@ dwv.dicom.DicomParser.prototype.interpret = function (
5643
5588
  element.value = this.interpretElement(
5644
5589
  element, reader, pixelRepresentation, bitsAllocated);
5645
5590
  }
5591
+ // delete interpretation specific properties
5592
+ delete element.startOffset;
5593
+ delete element.endOffset;
5646
5594
  }
5647
5595
  };
5648
5596
 
@@ -5662,7 +5610,7 @@ dwv.dicom.DicomParser.prototype.parse = function (buffer) {
5662
5610
 
5663
5611
  // 128 -> 132: magic word
5664
5612
  offset = 128;
5665
- var magicword = metaReader.readString(offset, 4);
5613
+ var magicword = this.decodeString(metaReader.readUint8Array(offset, 4));
5666
5614
  offset += 4 * Uint8Array.BYTES_PER_ELEMENT;
5667
5615
  if (magicword === 'DICM') {
5668
5616
  // 0x0002, 0x0000: FileMetaInformationGroupLength
@@ -5671,7 +5619,7 @@ dwv.dicom.DicomParser.prototype.parse = function (buffer) {
5671
5619
  // increment offset
5672
5620
  offset = dataElement.endOffset;
5673
5621
  // store the data element
5674
- this.dicomElements[dataElement.tag.name] = dataElement;
5622
+ this.dicomElements[dataElement.tag.getKey()] = dataElement;
5675
5623
  // get meta length
5676
5624
  var metaLength = parseInt(dataElement.value[0], 10);
5677
5625
 
@@ -5682,7 +5630,7 @@ dwv.dicom.DicomParser.prototype.parse = function (buffer) {
5682
5630
  dataElement = this.readDataElement(metaReader, offset, false);
5683
5631
  offset = dataElement.endOffset;
5684
5632
  // store the data element
5685
- this.dicomElements[dataElement.tag.name] = dataElement;
5633
+ this.dicomElements[dataElement.tag.getKey()] = dataElement;
5686
5634
  }
5687
5635
 
5688
5636
  // check the TransferSyntaxUID (has to be there!)
@@ -5694,12 +5642,13 @@ dwv.dicom.DicomParser.prototype.parse = function (buffer) {
5694
5642
  syntax = dwv.dicom.cleanString(dataElement.value[0]);
5695
5643
 
5696
5644
  } else {
5645
+ dwv.logger.warn('No DICM prefix, trying to guess tansfer syntax.');
5697
5646
  // read first element
5698
5647
  dataElement = this.readDataElement(dataReader, 0, false);
5699
5648
  // guess transfer syntax
5700
5649
  var tsElement = dwv.dicom.guessTransferSyntax(dataElement);
5701
5650
  // store
5702
- this.dicomElements[tsElement.tag.name] = tsElement;
5651
+ this.dicomElements[tsElement.tag.getKey()] = tsElement;
5703
5652
  syntax = dwv.dicom.cleanString(tsElement.value[0]);
5704
5653
  // reset offset
5705
5654
  offset = 0;
@@ -5722,11 +5671,6 @@ dwv.dicom.DicomParser.prototype.parse = function (buffer) {
5722
5671
  dataReader = new dwv.dicom.DataReader(buffer, false);
5723
5672
  }
5724
5673
 
5725
- // default character set
5726
- if (typeof this.getDefaultCharacterSet() !== 'undefined') {
5727
- dataReader.setUtfLabel(this.getDefaultCharacterSet());
5728
- }
5729
-
5730
5674
  // DICOM data elements
5731
5675
  while (offset < buffer.byteLength) {
5732
5676
  // get the data element
@@ -5734,10 +5678,10 @@ dwv.dicom.DicomParser.prototype.parse = function (buffer) {
5734
5678
  // increment offset
5735
5679
  offset = dataElement.endOffset;
5736
5680
  // store the data element
5737
- if (typeof this.dicomElements[dataElement.tag.name] === 'undefined') {
5738
- this.dicomElements[dataElement.tag.name] = dataElement;
5681
+ if (typeof this.dicomElements[dataElement.tag.getKey()] === 'undefined') {
5682
+ this.dicomElements[dataElement.tag.getKey()] = dataElement;
5739
5683
  } else {
5740
- dwv.logger.warn('Not saving duplicate tag: ' + dataElement.tag.name);
5684
+ dwv.logger.warn('Not saving duplicate tag: ' + dataElement.tag.getKey());
5741
5685
  }
5742
5686
  }
5743
5687
 
@@ -5774,7 +5718,12 @@ dwv.dicom.DicomParser.prototype.parse = function (buffer) {
5774
5718
  dwv.logger.warn('Reading DICOM pixel data with default bitsAllocated.');
5775
5719
  }
5776
5720
 
5777
- // character set
5721
+ // default character set
5722
+ if (typeof this.getDefaultCharacterSet() !== 'undefined') {
5723
+ this.setDecoderCharacterSet(this.getDefaultCharacterSet());
5724
+ }
5725
+
5726
+ // SpecificCharacterSet
5778
5727
  dataElement = this.dicomElements.x00080005;
5779
5728
  if (typeof dataElement !== 'undefined') {
5780
5729
  dataElement.value = this.interpretElement(dataElement, dataReader);
@@ -5786,7 +5735,7 @@ dwv.dicom.DicomParser.prototype.parse = function (buffer) {
5786
5735
  dwv.logger.warn('Unsupported character set with code extensions: \'' +
5787
5736
  charSetTerm + '\'.');
5788
5737
  }
5789
- dataReader.setUtfLabel(dwv.dicom.getUtfLabel(charSetTerm));
5738
+ this.setDecoderCharacterSet(dwv.dicom.getUtfLabel(charSetTerm));
5790
5739
  }
5791
5740
 
5792
5741
  // interpret the dicom elements
@@ -5800,7 +5749,7 @@ dwv.dicom.DicomParser.prototype.parse = function (buffer) {
5800
5749
  // (third note, "Depending on the transfer syntax...")
5801
5750
  dataElement = this.dicomElements.x7FE00010;
5802
5751
  if (typeof dataElement !== 'undefined') {
5803
- if (dataElement.vl === 'u/l') {
5752
+ if (dataElement.undefinedLength) {
5804
5753
  var numberOfFrames = 1;
5805
5754
  if (typeof this.dicomElements.x00280008 !== 'undefined') {
5806
5755
  numberOfFrames = dwv.dicom.cleanString(
@@ -5851,6 +5800,18 @@ dwv.dicom = dwv.dicom || {};
5851
5800
  * @param {string} element The tag element as '0x####'.
5852
5801
  */
5853
5802
  dwv.dicom.Tag = function (group, element) {
5803
+ if (!group || typeof group === 'undefined') {
5804
+ throw new Error('Cannot create tag with no group.');
5805
+ }
5806
+ if (group.length !== 6 || !group.startsWith('0x')) {
5807
+ throw new Error('Cannot create tag with badly formed group.');
5808
+ }
5809
+ if (!element || typeof element === 'undefined') {
5810
+ throw new Error('Cannot create tag with no element.');
5811
+ }
5812
+ if (element.length !== 6 || !element.startsWith('0x')) {
5813
+ throw new Error('Cannot create tag with badly formed element.');
5814
+ }
5854
5815
  /**
5855
5816
  * Get the tag group.
5856
5817
  *
@@ -5877,23 +5838,28 @@ dwv.dicom.Tag = function (group, element) {
5877
5838
  */
5878
5839
  dwv.dicom.Tag.prototype.equals = function (rhs) {
5879
5840
  return rhs !== null &&
5841
+ typeof rhs !== 'undefined' &&
5880
5842
  this.getGroup() === rhs.getGroup() &&
5881
5843
  this.getElement() === rhs.getElement();
5882
5844
  };
5883
5845
 
5884
5846
  /**
5885
- * Check for Tag equality.
5847
+ * Tag compare function.
5886
5848
  *
5887
- * @param {object} rhs The other tag to compare to provided as a simple object.
5888
- * @returns {boolean} True if both tags are equal.
5849
+ * @param {dwv.dicom.Tag} a The first tag.
5850
+ * @param {dwv.dicom.Tag} b The second tag.
5851
+ * @returns {number} The result of the tag comparison,
5852
+ * positive for b before a, negative for a before b and
5853
+ * zero to keep same order.
5889
5854
  */
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;
5855
+ dwv.dicom.tagCompareFunction = function (a, b) {
5856
+ // first by group
5857
+ var res = parseInt(a.getGroup()) - parseInt(b.getGroup());
5858
+ if (res === 0) {
5859
+ // by element if same group
5860
+ res = parseInt(a.getElement()) - parseInt(b.getElement());
5895
5861
  }
5896
- return this.equals(new dwv.dicom.Tag(rhs.group, rhs.element));
5862
+ return res;
5897
5863
  };
5898
5864
 
5899
5865
  /**
@@ -5935,7 +5901,9 @@ dwv.dicom.Tag.prototype.getGroupName = function () {
5935
5901
  * @returns {object} The DICOM tag.
5936
5902
  */
5937
5903
  dwv.dicom.getTagFromKey = function (key) {
5938
- return new dwv.dicom.Tag(key.substring(1, 5), key.substring(5, 9));
5904
+ return new dwv.dicom.Tag(
5905
+ '0x' + key.substring(1, 5),
5906
+ '0x' + key.substring(5, 9));
5939
5907
  };
5940
5908
 
5941
5909
  /**
@@ -6241,6 +6209,7 @@ dwv.dicom.isStringVr = function (vr) {
6241
6209
 
6242
6210
  /**
6243
6211
  * Is the input VR a VR that could need padding.
6212
+ * see http://dicom.nema.org/dicom/2013/output/chtml/part05/sect_6.2.html
6244
6213
  *
6245
6214
  * @param {string} vr The element VR.
6246
6215
  * @returns {boolean} True if the VR needs padding.
@@ -6268,60 +6237,52 @@ dwv.dicom.getVrPad = function (vr) {
6268
6237
  };
6269
6238
 
6270
6239
  /**
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?
6240
+ * Push a value at the end of an input Uint8Array.
6293
6241
  *
6294
- * @param {object} element The element to check.
6295
- * @returns {boolean} True if it is.
6242
+ * @param {Uint8Array} arr The input array.
6243
+ * @param {number} value The value to push.
6244
+ * @returns {Uint8Array} The new array.
6296
6245
  */
6297
- dwv.dicom.isImplicitLengthSequence = function (element) {
6298
- // sequence with no length
6299
- return (element.vr === 'SQ') &&
6300
- (element.vl === 'u/l');
6246
+ dwv.dicom.uint8ArrayPush = function (arr, value) {
6247
+ var newArr = new Uint8Array(arr.length + 1);
6248
+ newArr.set(arr);
6249
+ newArr.set(value, arr.length);
6250
+ return newArr;
6301
6251
  };
6302
6252
 
6303
6253
  /**
6304
- * Is this element an implicit length item?
6254
+ * Pad an input OB value.
6305
6255
  *
6306
- * @param {object} element The element to check.
6307
- * @returns {boolean} True if it is.
6256
+ * @param {Array|Uint8Array} value The input value.
6257
+ * @returns {Array|Uint8Array} The padded input.
6308
6258
  */
6309
- dwv.dicom.isImplicitLengthItem = function (element) {
6310
- // item with no length
6311
- return (element.tag.name === 'xFFFEE000') &&
6312
- (element.vl === 'u/l');
6313
- };
6314
-
6315
- /**
6316
- * Is this element an implicit length pixel data?
6317
- *
6318
- * @param {object} element The element to check.
6319
- * @returns {boolean} True if it is.
6320
- */
6321
- dwv.dicom.isImplicitLengthPixels = function (element) {
6322
- // pixel data with no length
6323
- return (element.tag.name === 'x7FE00010') &&
6324
- (element.vl === 'u/l');
6259
+ dwv.dicom.padOBValue = function (value) {
6260
+ if (value !== null &&
6261
+ typeof value !== 'undefined' &&
6262
+ typeof value.length !== 'undefined') {
6263
+ // calculate size and pad if needed
6264
+ if (value.length !== 0 &&
6265
+ typeof value[0].length !== 'undefined') {
6266
+ // handle array of array
6267
+ var size = 0;
6268
+ for (var i = 0; i < value.length; ++i) {
6269
+ size += value[i].length;
6270
+ }
6271
+ if (!dwv.dicom.isEven(size)) {
6272
+ value[value.length - 1] = dwv.dicom.uint8ArrayPush(
6273
+ value[value.length - 1], 0);
6274
+ }
6275
+ } else {
6276
+ if (!dwv.dicom.isEven(value.length)) {
6277
+ value = dwv.dicom.uint8ArrayPush(value, 0);
6278
+ }
6279
+ }
6280
+ } else {
6281
+ throw new Error('Cannot pad undefined or null OB value.');
6282
+ }
6283
+ // uint8ArrayPush may create a new array so we
6284
+ // need to return it
6285
+ return value;
6325
6286
  };
6326
6287
 
6327
6288
  /**
@@ -6349,6 +6310,25 @@ dwv.dicom.flattenArrayOfTypedArrays = function (initialArray) {
6349
6310
  return flattenedArray;
6350
6311
  };
6351
6312
 
6313
+ /**
6314
+ * Default text encoder.
6315
+ */
6316
+ dwv.dicom.DefaultTextEncoder = function () {
6317
+ /**
6318
+ * Encode an input string.
6319
+ *
6320
+ * @param {string} str The string to encode.
6321
+ * @returns {Uint8Array} The encoded string.
6322
+ */
6323
+ this.encode = function (str) {
6324
+ var result = new Uint8Array(str.length);
6325
+ for (var i = 0, leni = str.length; i < leni; ++i) {
6326
+ result[i] = str.charCodeAt(i);
6327
+ }
6328
+ return result;
6329
+ };
6330
+ };
6331
+
6352
6332
  /**
6353
6333
  * DICOM writer.
6354
6334
  *
@@ -6380,16 +6360,11 @@ dwv.dicom.DicomWriter = function () {
6380
6360
  return null;
6381
6361
  },
6382
6362
  clear: function (item) {
6383
- item.value[0] = '';
6384
- item.vl = 0;
6385
- item.endOffset = item.startOffset;
6363
+ item.value = [];
6386
6364
  return item;
6387
6365
  },
6388
6366
  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;
6367
+ item.value = [value];
6393
6368
  return item;
6394
6369
  }
6395
6370
  };
@@ -6410,6 +6385,55 @@ dwv.dicom.DicomWriter = function () {
6410
6385
  */
6411
6386
  this.rules = defaultRules;
6412
6387
 
6388
+ /**
6389
+ * Default text encoder.
6390
+ *
6391
+ * @private
6392
+ * @type {dwv.dicom.DefaultTextEncoder}
6393
+ */
6394
+ var defaultTextEncoder = new dwv.dicom.DefaultTextEncoder();
6395
+
6396
+ /**
6397
+ * Special text encoder.
6398
+ *
6399
+ * @private
6400
+ * @type {dwv.dicom.DefaultTextEncoder|TextEncoder}
6401
+ */
6402
+ var textEncoder = defaultTextEncoder;
6403
+
6404
+ /**
6405
+ * Encode string data.
6406
+ *
6407
+ * @param {number} str The string to encode.
6408
+ * @returns {Uint8Array} The encoded string.
6409
+ */
6410
+ this.encodeString = function (str) {
6411
+ return defaultTextEncoder.encode(str);
6412
+ };
6413
+
6414
+ /**
6415
+ * Encode data as a UTF-8.
6416
+ *
6417
+ * @param {number} str The string to write.
6418
+ * @returns {Uint8Array} The encoded string.
6419
+ */
6420
+ this.encodeSpecialString = function (str) {
6421
+ return textEncoder.encode(str);
6422
+ };
6423
+
6424
+ /**
6425
+ * Use a TextEncoder instead of the default text decoder.
6426
+ */
6427
+ this.useSpecialTextEncoder = function () {
6428
+ /**
6429
+ * The text encoder.
6430
+ *
6431
+ * @external TextEncoder
6432
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder
6433
+ */
6434
+ textEncoder = new TextEncoder();
6435
+ };
6436
+
6413
6437
  /**
6414
6438
  * Example anonymisation rules.
6415
6439
  */
@@ -6432,15 +6456,14 @@ dwv.dicom.DicomWriter = function () {
6432
6456
  */
6433
6457
  this.getElementToWrite = function (element) {
6434
6458
  // 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();
6459
+ var groupName = element.tag.getGroupName();
6460
+ var tagName = element.tag.getNameFromDictionary();
6438
6461
 
6439
6462
  // apply rules:
6440
6463
  var rule;
6441
- if (typeof this.rules[element.tag.name] !== 'undefined') {
6464
+ if (typeof this.rules[element.tag.getKey()] !== 'undefined') {
6442
6465
  // 1. tag itself
6443
- rule = this.rules[element.tag.name];
6466
+ rule = this.rules[element.tag.getKey()];
6444
6467
  } else if (tagName !== null && typeof this.rules[tagName] !== 'undefined') {
6445
6468
  // 2. tag name
6446
6469
  rule = this.rules[tagName];
@@ -6475,11 +6498,14 @@ dwv.dicom.DicomWriter.prototype.writeDataElementItems = function (
6475
6498
  continue;
6476
6499
  }
6477
6500
  // item element (create new to not modify original)
6478
- var implicitLength = item.xFFFEE000.vl === 'u/l';
6501
+ var undefinedLength = false;
6502
+ if (typeof item.xFFFEE000.undefinedLength !== 'undefined') {
6503
+ undefinedLength = item.xFFFEE000.undefinedLength;
6504
+ }
6479
6505
  var itemElement = {
6480
- tag: item.xFFFEE000.tag,
6481
- vr: item.xFFFEE000.vr,
6482
- vl: implicitLength ? 0xffffffff : item.xFFFEE000.vl,
6506
+ tag: dwv.dicom.getItemTag(),
6507
+ vr: 'NONE',
6508
+ vl: undefinedLength ? 0xffffffff : item.xFFFEE000.vl,
6483
6509
  value: []
6484
6510
  };
6485
6511
  byteOffset = this.writeDataElement(
@@ -6492,13 +6518,9 @@ dwv.dicom.DicomWriter.prototype.writeDataElementItems = function (
6492
6518
  }
6493
6519
  }
6494
6520
  // item delimitation
6495
- if (implicitLength) {
6521
+ if (undefinedLength) {
6496
6522
  var itemDelimElement = {
6497
- tag: {
6498
- group: '0xFFFE',
6499
- element: '0xE00D',
6500
- name: 'ItemDelimitationItem'
6501
- },
6523
+ tag: dwv.dicom.getItemDelimitationItemTag(),
6502
6524
  vr: 'NONE',
6503
6525
  vl: 0,
6504
6526
  value: []
@@ -6516,21 +6538,22 @@ dwv.dicom.DicomWriter.prototype.writeDataElementItems = function (
6516
6538
  * Write data with a specific Value Representation (VR).
6517
6539
  *
6518
6540
  * @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).
6541
+ * @param {object} element The element to write.
6521
6542
  * @param {number} byteOffset The offset to start writing from.
6522
6543
  * @param {Array} value The array to write.
6523
6544
  * @param {boolean} isImplicit Is the DICOM VR implicit?
6524
6545
  * @returns {number} The new offset position.
6525
6546
  */
6526
6547
  dwv.dicom.DicomWriter.prototype.writeDataElementValue = function (
6527
- writer, vr, vl, byteOffset, value, isImplicit) {
6548
+ writer, element, byteOffset, value, isImplicit) {
6528
6549
 
6529
- if (vr === 'NONE') {
6550
+ var startOffset = byteOffset;
6551
+
6552
+ if (element.vr === 'NONE') {
6530
6553
  // nothing to do!
6531
6554
  } else if (value instanceof Uint8Array) {
6532
6555
  // binary data has been expanded 8 times at read
6533
- if (value.length === 8 * vl) {
6556
+ if (value.length === 8 * element.vl) {
6534
6557
  byteOffset = writer.writeBinaryArray(byteOffset, value);
6535
6558
  } else {
6536
6559
  byteOffset = writer.writeUint8Array(byteOffset, value);
@@ -6551,7 +6574,7 @@ dwv.dicom.DicomWriter.prototype.writeDataElementValue = function (
6551
6574
  byteOffset = writer.writeInt64Array(byteOffset, value);
6552
6575
  } else {
6553
6576
  // switch according to VR if input type is undefined
6554
- var vrType = dwv.dicom.vrTypes[vr];
6577
+ var vrType = dwv.dicom.vrTypes[element.vr];
6555
6578
  if (typeof vrType !== 'undefined') {
6556
6579
  if (vrType === 'Uint8') {
6557
6580
  byteOffset = writer.writeUint8Array(byteOffset, value);
@@ -6572,23 +6595,14 @@ dwv.dicom.DicomWriter.prototype.writeDataElementValue = function (
6572
6595
  } else if (vrType === 'Float64') {
6573
6596
  byteOffset = writer.writeFloat64Array(byteOffset, value);
6574
6597
  } 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
- }
6598
+ byteOffset = writer.writeUint8Array(byteOffset, value);
6585
6599
  } else {
6586
6600
  throw Error('Unknown VR type: ' + vrType);
6587
6601
  }
6588
- } else if (vr === 'SQ') {
6602
+ } else if (element.vr === 'SQ') {
6589
6603
  byteOffset = this.writeDataElementItems(
6590
6604
  writer, byteOffset, value, isImplicit);
6591
- } else if (vr === 'AT') {
6605
+ } else if (element.vr === 'AT') {
6592
6606
  for (var i = 0; i < value.length; ++i) {
6593
6607
  var hexString = value[i] + '';
6594
6608
  var hexString1 = hexString.substring(1, 5);
@@ -6599,9 +6613,18 @@ dwv.dicom.DicomWriter.prototype.writeDataElementValue = function (
6599
6613
  byteOffset = writer.writeUint16Array(byteOffset, atValue);
6600
6614
  }
6601
6615
  } else {
6602
- dwv.logger.warn('Unknown VR: ' + vr);
6616
+ dwv.logger.warn('Unknown VR: ' + element.vr);
6617
+ }
6618
+ }
6619
+
6620
+ if (element.vr !== 'SQ' && element.vr !== 'NONE') {
6621
+ var diff = byteOffset - startOffset;
6622
+ if (diff !== element.vl) {
6623
+ dwv.logger.warn('Offset difference and VL are not equal: ' +
6624
+ diff + ' != ' + element.vl + ', vr:' + element.vr);
6603
6625
  }
6604
6626
  }
6627
+
6605
6628
  // return new offset
6606
6629
  return byteOffset;
6607
6630
  };
@@ -6610,17 +6633,21 @@ dwv.dicom.DicomWriter.prototype.writeDataElementValue = function (
6610
6633
  * Write a pixel data element.
6611
6634
  *
6612
6635
  * @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).
6636
+ * @param {object} element The element to write.
6615
6637
  * @param {number} byteOffset The offset to start writing from.
6616
6638
  * @param {Array} value The array to write.
6617
6639
  * @param {boolean} isImplicit Is the DICOM VR implicit?
6618
6640
  * @returns {number} The new offset position.
6619
6641
  */
6620
6642
  dwv.dicom.DicomWriter.prototype.writePixelDataElementValue = function (
6621
- writer, vr, vl, byteOffset, value, isImplicit) {
6643
+ writer, element, byteOffset, value, isImplicit) {
6644
+ // undefined length flag
6645
+ var undefinedLength = false;
6646
+ if (typeof element.undefinedLength !== 'undefined') {
6647
+ undefinedLength = element.undefinedLength;
6648
+ }
6622
6649
  // explicit length
6623
- if (vl !== 'u/l') {
6650
+ if (!undefinedLength) {
6624
6651
  var finalValue = value[0];
6625
6652
  // flatten multi frame
6626
6653
  if (value.length > 1) {
@@ -6628,30 +6655,22 @@ dwv.dicom.DicomWriter.prototype.writePixelDataElementValue = function (
6628
6655
  }
6629
6656
  // write
6630
6657
  byteOffset = this.writeDataElementValue(
6631
- writer, vr, vl, byteOffset, finalValue, isImplicit);
6658
+ writer, element, byteOffset, finalValue, isImplicit);
6632
6659
  } else {
6633
6660
  // pixel data as sequence
6634
6661
  var item = {};
6635
6662
  // first item: basic offset table
6636
6663
  item.xFFFEE000 = {
6637
- tag: {
6638
- group: '0xFFFE',
6639
- element: '0xE000',
6640
- name: 'xFFFEE000'
6641
- },
6642
- vr: 'UN',
6664
+ tag: dwv.dicom.getItemTag(),
6665
+ vr: 'NONE',
6643
6666
  vl: 0,
6644
6667
  value: []
6645
6668
  };
6646
6669
  // data
6647
6670
  for (var i = 0; i < value.length; ++i) {
6648
6671
  item[i] = {
6649
- tag: {
6650
- group: '0xFFFE',
6651
- element: '0xE000',
6652
- name: 'xFFFEE000'
6653
- },
6654
- vr: vr,
6672
+ tag: dwv.dicom.getItemTag(),
6673
+ vr: element.vr,
6655
6674
  vl: value[i].length,
6656
6675
  value: value[i]
6657
6676
  };
@@ -6676,36 +6695,47 @@ dwv.dicom.DicomWriter.prototype.writePixelDataElementValue = function (
6676
6695
  */
6677
6696
  dwv.dicom.DicomWriter.prototype.writeDataElement = function (
6678
6697
  writer, element, byteOffset, isImplicit) {
6679
- var isTagWithVR = new dwv.dicom.Tag(
6680
- element.tag.group, element.tag.element).isWithVR();
6698
+ var isTagWithVR = element.tag.isWithVR();
6681
6699
  var is32bitVLVR = (isImplicit || !isTagWithVR)
6682
6700
  ? true : dwv.dicom.is32bitVLVR(element.vr);
6683
6701
  // group
6684
- byteOffset = writer.writeHex(byteOffset, element.tag.group);
6702
+ byteOffset = writer.writeHex(byteOffset, element.tag.getGroup());
6685
6703
  // element
6686
- byteOffset = writer.writeHex(byteOffset, element.tag.element);
6704
+ byteOffset = writer.writeHex(byteOffset, element.tag.getElement());
6687
6705
  // VR
6688
6706
  var vr = element.vr;
6689
6707
  // use VR=UN for private sequence
6690
6708
  if (this.useUnVrForPrivateSq &&
6691
- new dwv.dicom.Tag(element.tag.group, element.tag.element).isPrivate() &&
6709
+ element.tag.isPrivate() &&
6692
6710
  vr === 'SQ') {
6693
6711
  dwv.logger.warn('Write element using VR=UN for private sequence.');
6694
6712
  vr = 'UN';
6695
6713
  }
6696
6714
  if (isTagWithVR && !isImplicit) {
6697
- byteOffset = writer.writeString(byteOffset, vr);
6715
+ byteOffset = writer.writeUint8Array(byteOffset, this.encodeString(vr));
6698
6716
  // reserved 2 bytes for 32bit VL
6699
6717
  if (is32bitVLVR) {
6700
6718
  byteOffset += 2;
6701
6719
  }
6702
6720
  }
6703
6721
 
6704
- // update vl for sequence or item with implicit length
6722
+ var undefinedLengthSequence = false;
6723
+ if (element.vr === 'SQ' ||
6724
+ dwv.dicom.isPixelDataTag(element.tag)) {
6725
+ if (typeof element.undefinedLength !== 'undefined') {
6726
+ undefinedLengthSequence = element.undefinedLength;
6727
+ }
6728
+ }
6729
+ var undefinedLengthItem = false;
6730
+ if (dwv.dicom.isItemTag(element.tag)) {
6731
+ if (typeof element.undefinedLength !== 'undefined') {
6732
+ undefinedLengthItem = element.undefinedLength;
6733
+ }
6734
+ }
6735
+
6736
+ // update vl for sequence or item with undefined length
6705
6737
  var vl = element.vl;
6706
- if (dwv.dicom.isImplicitLengthSequence(element) ||
6707
- dwv.dicom.isImplicitLengthItem(element) ||
6708
- dwv.dicom.isImplicitLengthPixels(element)) {
6738
+ if (undefinedLengthSequence || undefinedLengthItem) {
6709
6739
  vl = 0xffffffff;
6710
6740
  }
6711
6741
  // VL
@@ -6722,23 +6752,18 @@ dwv.dicom.DicomWriter.prototype.writeDataElement = function (
6722
6752
  value = [];
6723
6753
  }
6724
6754
  // write
6725
- if (element.tag.name === 'x7FE00010') {
6755
+ if (dwv.dicom.isPixelDataTag(element.tag)) {
6726
6756
  byteOffset = this.writePixelDataElementValue(
6727
- writer, element.vr, element.vl, byteOffset, value, isImplicit);
6757
+ writer, element, byteOffset, value, isImplicit);
6728
6758
  } else {
6729
6759
  byteOffset = this.writeDataElementValue(
6730
- writer, element.vr, element.vl, byteOffset, value, isImplicit);
6760
+ writer, element, byteOffset, value, isImplicit);
6731
6761
  }
6732
6762
 
6733
- // sequence delimitation item for sequence with implicit length
6734
- if (dwv.dicom.isImplicitLengthSequence(element) ||
6735
- dwv.dicom.isImplicitLengthPixels(element)) {
6763
+ // sequence delimitation item for sequence with undefined length
6764
+ if (undefinedLengthSequence) {
6736
6765
  var seqDelimElement = {
6737
- tag: {
6738
- group: '0xFFFE',
6739
- element: '0xE0DD',
6740
- name: 'SequenceDelimitationItem'
6741
- },
6766
+ tag: dwv.dicom.getSequenceDelimitationItemTag(),
6742
6767
  vr: 'NONE',
6743
6768
  vl: 0,
6744
6769
  value: []
@@ -6758,13 +6783,22 @@ dwv.dicom.DicomWriter.prototype.writeDataElement = function (
6758
6783
  * @returns {ArrayBuffer} The elements as a buffer.
6759
6784
  */
6760
6785
  dwv.dicom.DicomWriter.prototype.getBuffer = function (dicomElements) {
6761
- // array keys
6762
- var keys = Object.keys(dicomElements);
6763
-
6764
- // transfer syntax
6786
+ // Transfer Syntax
6765
6787
  var syntax = dwv.dicom.cleanString(dicomElements.x00020010.value[0]);
6766
6788
  var isImplicit = dwv.dicom.isImplicitTransferSyntax(syntax);
6767
6789
  var isBigEndian = dwv.dicom.isBigEndianTransferSyntax(syntax);
6790
+ // Bits Allocated
6791
+ var bitsAllocated = dicomElements.x00280100.value[0];
6792
+ // Specific CharacterSet
6793
+ if (typeof dicomElements.x00080005 !== 'undefined') {
6794
+ var oldscs = dwv.dicom.cleanString(dicomElements.x00080005.value[0]);
6795
+ // force UTF-8 if not default character set
6796
+ if (typeof oldscs !== 'undefined' && oldscs !== 'ISO-IR 6') {
6797
+ dwv.logger.debug('Change charset to UTF, was: ' + oldscs);
6798
+ this.useSpecialTextEncoder();
6799
+ dicomElements.x00080005.value = ['ISO_IR 192'];
6800
+ }
6801
+ }
6768
6802
 
6769
6803
  // calculate buffer size and split elements (meta and non meta)
6770
6804
  var totalSize = 128 + 4; // DICM
@@ -6779,12 +6813,15 @@ dwv.dicom.DicomWriter.prototype.getBuffer = function (dicomElements) {
6779
6813
  var icUIDTag = new dwv.dicom.Tag('0x0002', '0x0012');
6780
6814
  // ImplementationVersionName
6781
6815
  var ivnTag = new dwv.dicom.Tag('0x0002', '0x0013');
6816
+
6817
+ // loop through elements to get the buffer size
6818
+ var keys = Object.keys(dicomElements);
6782
6819
  for (var i = 0, leni = keys.length; i < leni; ++i) {
6783
6820
  element = this.getElementToWrite(dicomElements[keys[i]]);
6784
6821
  if (element !== null &&
6785
- !fmiglTag.equals2(element.tag) &&
6786
- !icUIDTag.equals2(element.tag) &&
6787
- !ivnTag.equals2(element.tag)) {
6822
+ !fmiglTag.equals(element.tag) &&
6823
+ !icUIDTag.equals(element.tag) &&
6824
+ !ivnTag.equals(element.tag)) {
6788
6825
  localSize = 0;
6789
6826
 
6790
6827
  // XB7 2020-04-17
@@ -6794,8 +6831,12 @@ dwv.dicom.DicomWriter.prototype.getBuffer = function (dicomElements) {
6794
6831
  // (dcmdump may crash because of these bytes)
6795
6832
  dwv.dicom.checkUnknownVR(element);
6796
6833
 
6797
- // tag group name (remove first 0)
6798
- groupName = dwv.dicom.TagGroups[element.tag.group.substring(1)];
6834
+ // update value and vl
6835
+ this.setElementValue(
6836
+ element, element.value, isImplicit, bitsAllocated);
6837
+
6838
+ // tag group name
6839
+ groupName = element.tag.getGroupName();
6799
6840
 
6800
6841
  // prefix
6801
6842
  if (groupName === 'Meta Element') {
@@ -6806,8 +6847,7 @@ dwv.dicom.DicomWriter.prototype.getBuffer = function (dicomElements) {
6806
6847
  }
6807
6848
 
6808
6849
  // value
6809
- var realVl = element.endOffset - element.startOffset;
6810
- localSize += parseInt(realVl, 10);
6850
+ localSize += element.vl;
6811
6851
 
6812
6852
  // sort elements
6813
6853
  if (groupName === 'Meta Element') {
@@ -6825,8 +6865,8 @@ dwv.dicom.DicomWriter.prototype.getBuffer = function (dicomElements) {
6825
6865
  // ImplementationClassUID
6826
6866
  var icUID = dwv.dicom.getDicomElement('ImplementationClassUID');
6827
6867
  var icUIDSize = dwv.dicom.getDataElementPrefixByteSize(icUID.vr, isImplicit);
6828
- icUIDSize += dwv.dicom.setElementValue(
6829
- icUID, dwv.dicom.getUID('ImplementationClassUID'), false);
6868
+ icUIDSize += this.setElementValue(
6869
+ icUID, [dwv.dicom.getUID('ImplementationClassUID')], false);
6830
6870
  metaElements.push(icUID);
6831
6871
  metaLength += icUIDSize;
6832
6872
  totalSize += icUIDSize;
@@ -6834,32 +6874,33 @@ dwv.dicom.DicomWriter.prototype.getBuffer = function (dicomElements) {
6834
6874
  var ivn = dwv.dicom.getDicomElement('ImplementationVersionName');
6835
6875
  var ivnSize = dwv.dicom.getDataElementPrefixByteSize(ivn.vr, isImplicit);
6836
6876
  var ivnValue = 'DWV_' + dwv.getVersion();
6837
- ivnSize += dwv.dicom.setElementValue(ivn, ivnValue, false);
6877
+ ivnSize += this.setElementValue(ivn, [ivnValue], false);
6838
6878
  metaElements.push(ivn);
6839
6879
  metaLength += ivnSize;
6840
6880
  totalSize += ivnSize;
6841
6881
 
6882
+ // sort elements
6883
+ var elemSortFunc = function (a, b) {
6884
+ return dwv.dicom.tagCompareFunction(a.tag, b.tag);
6885
+ };
6886
+ metaElements.sort(elemSortFunc);
6887
+ rawElements.sort(elemSortFunc);
6888
+
6842
6889
  // create the FileMetaInformationGroupLength element
6843
6890
  var fmigl = dwv.dicom.getDicomElement('FileMetaInformationGroupLength');
6844
6891
  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
6892
+ fmiglSize += this.setElementValue(
6893
+ fmigl, new Uint32Array([metaLength]), false);
6848
6894
  totalSize += fmiglSize;
6849
6895
 
6850
6896
  // create buffer
6851
6897
  var buffer = new ArrayBuffer(totalSize);
6852
6898
  var metaWriter = new dwv.dicom.DataWriter(buffer);
6853
6899
  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
6900
 
6860
6901
  var offset = 128;
6861
6902
  // DICM
6862
- offset = metaWriter.writeString(offset, 'DICM');
6903
+ offset = metaWriter.writeUint8Array(offset, this.encodeString('DICM'));
6863
6904
  // FileMetaInformationGroupLength
6864
6905
  offset = this.writeDataElement(metaWriter, fmigl, offset, false);
6865
6906
  // write meta
@@ -6902,12 +6943,11 @@ dwv.dicom.DicomWriter.prototype.getBuffer = function (dicomElements) {
6902
6943
  */
6903
6944
  dwv.dicom.checkUnknownVR = function (element) {
6904
6945
  if (element.vr === 'UN') {
6905
- var tag = new dwv.dicom.Tag(element.tag.group, element.tag.element);
6906
- var dictVr = tag.getVrFromDictionary();
6946
+ var dictVr = element.tag.getVrFromDictionary();
6907
6947
  if (dictVr !== null && element.vr !== dictVr) {
6908
6948
  element.vr = dictVr;
6909
- dwv.logger.info('Element ' + element.tag.group +
6910
- ' ' + element.tag.element +
6949
+ dwv.logger.info('Element ' + element.tag.getGroup() +
6950
+ ' ' + element.tag.getElement() +
6911
6951
  ' VR changed from UN to ' + element.vr);
6912
6952
  }
6913
6953
  }
@@ -6921,176 +6961,212 @@ dwv.dicom.checkUnknownVR = function (element) {
6921
6961
  */
6922
6962
  dwv.dicom.getDicomElement = function (tagName) {
6923
6963
  var tag = dwv.dicom.getTagFromDictionary(tagName);
6924
- // return element definition
6925
6964
  return {
6926
- tag: {group: tag.getGroup(), element: tag.getElement()},
6965
+ tag: tag,
6927
6966
  vr: tag.getVrFromDictionary()
6928
6967
  };
6929
6968
  };
6930
6969
 
6970
+ /**
6971
+ * Get the number of bytes per element for a given VR type.
6972
+ *
6973
+ * @param {string} vrType The VR type as defined in the dictionary.
6974
+ * @returns {number} The bytes per element.
6975
+ */
6976
+ dwv.dicom.getBpeForVrType = function (vrType) {
6977
+ var bpe;
6978
+ if (vrType === 'Uint8') {
6979
+ bpe = Uint8Array.BYTES_PER_ELEMENT;
6980
+ } else if (vrType === 'Uint16') {
6981
+ bpe = Uint16Array.BYTES_PER_ELEMENT;
6982
+ } else if (vrType === 'Int16') {
6983
+ bpe = Int16Array.BYTES_PER_ELEMENT;
6984
+ } else if (vrType === 'Uint32') {
6985
+ bpe = Uint32Array.BYTES_PER_ELEMENT;
6986
+ } else if (vrType === 'Int32') {
6987
+ bpe = Int32Array.BYTES_PER_ELEMENT;
6988
+ } else if (vrType === 'Float32') {
6989
+ bpe = Float32Array.BYTES_PER_ELEMENT;
6990
+ } else if (vrType === 'Float64') {
6991
+ bpe = Float64Array.BYTES_PER_ELEMENT;
6992
+ } else if (vrType === 'Uint64') {
6993
+ bpe = BigUint64Array.BYTES_PER_ELEMENT;
6994
+ } else if (vrType === 'Int64') {
6995
+ bpe = BigInt64Array.BYTES_PER_ELEMENT;
6996
+ }
6997
+ return bpe;
6998
+ };
6999
+
6931
7000
  /**
6932
7001
  * Set a DICOM element value according to its VR (Value Representation).
6933
7002
  *
6934
7003
  * @param {object} element The DICOM element to set the value.
6935
7004
  * @param {object} value The value to set.
6936
7005
  * @param {boolean} isImplicit Does the data use implicit VR?
7006
+ * @param {number} bitsAllocated Bits allocated used for pixel data.
6937
7007
  * @returns {number} The total element size.
6938
7008
  */
6939
- dwv.dicom.setElementValue = function (element, value, isImplicit) {
7009
+ dwv.dicom.DicomWriter.prototype.setElementValue = function (
7010
+ element, value, isImplicit, bitsAllocated) {
6940
7011
  // byte size of the element
6941
7012
  var size = 0;
6942
7013
  // special sequence case
6943
7014
  if (element.vr === 'SQ') {
6944
7015
 
6945
- // set the value
6946
- element.value = value;
6947
- element.vl = 0;
6948
-
6949
7016
  if (value !== null && value !== 0) {
6950
- var sqItems = [];
7017
+ var newItems = [];
6951
7018
  var name;
6952
7019
 
6953
- // explicit or implicit length
6954
- var explicitLength = true;
6955
- if (typeof value.explicitLength !== 'undefined') {
6956
- explicitLength = value.explicitLength;
6957
- delete value.explicitLength;
7020
+ // explicit or undefined length sequence
7021
+ var undefinedLength = false;
7022
+ if (typeof element.undefinedLength !== 'undefined') {
7023
+ undefinedLength = element.undefinedLength;
7024
+ delete element.undefinedLength;
6958
7025
  }
6959
7026
 
6960
7027
  // items
6961
- var itemData;
6962
- var itemKeys = Object.keys(value);
6963
- for (var i = 0, leni = itemKeys.length; i < leni; ++i) {
6964
- var itemElements = {};
7028
+ for (var i = 0; i < value.length; ++i) {
7029
+ var oldItemElements = value[i];
7030
+ var newItemElements = {};
6965
7031
  var subSize = 0;
6966
- itemData = value[itemKeys[i]];
6967
7032
 
6968
7033
  // check data
6969
- if (itemData === null || itemData === 0) {
7034
+ if (oldItemElements === null || oldItemElements === 0) {
6970
7035
  continue;
6971
7036
  }
6972
7037
 
6973
7038
  // 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;
7039
+ var itemKeys = Object.keys(oldItemElements);
7040
+ for (var j = 0, lenj = itemKeys.length; j < lenj; ++j) {
7041
+ var itemKey = itemKeys[j];
7042
+ var subElement = oldItemElements[itemKey];
7043
+ if (dwv.dicom.isItemTag(subElement.tag)) {
7044
+ continue;
7045
+ }
7046
+ // set item value
7047
+ subSize += this.setElementValue(
7048
+ subElement, subElement.value, isImplicit, bitsAllocated);
7049
+ newItemElements[itemKey] = subElement;
7050
+ // add prefix size
6984
7051
  subSize += dwv.dicom.getDataElementPrefixByteSize(
6985
7052
  subElement.vr, isImplicit);
6986
7053
  }
6987
7054
 
6988
- // item (after elements to get the size)
7055
+ // add item element (used to store its size)
6989
7056
  var itemElement = {
6990
- tag: {group: '0xFFFE', element: '0xE000'},
7057
+ tag: dwv.dicom.getItemTag(),
6991
7058
  vr: 'NONE',
6992
- vl: (explicitLength ? subSize : 'u/l'),
7059
+ vl: subSize,
6993
7060
  value: []
6994
7061
  };
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);
7062
+ if (undefinedLength) {
7063
+ itemElement.undefinedLength = undefinedLength;
7064
+ }
7065
+ name = itemElement.tag.getKey();
7066
+ newItemElements[name] = itemElement;
7067
+ subSize += dwv.dicom.getDataElementPrefixByteSize(
7068
+ itemElement.vr, isImplicit);
7069
+
7070
+ // add item delimitation size
7071
+ if (undefinedLength) {
7072
+ subSize += dwv.dicom.getDataElementPrefixByteSize(
7073
+ 'NONE', isImplicit);
7012
7074
  }
7013
7075
 
7014
7076
  size += subSize;
7015
- sqItems.push(itemElements);
7077
+ newItems.push(newItemElements);
7016
7078
  }
7017
7079
 
7018
7080
  // add sequence delimitation size
7019
- if (!explicitLength) {
7081
+ if (undefinedLength) {
7020
7082
  size += dwv.dicom.getDataElementPrefixByteSize('NONE', isImplicit);
7021
7083
  }
7022
7084
 
7023
- element.value = sqItems;
7024
- if (explicitLength) {
7025
- element.vl = size;
7026
- } else {
7027
- element.vl = 'u/l';
7085
+ // update sequence element
7086
+ element.value = newItems;
7087
+ element.vl = size;
7088
+ if (undefinedLength) {
7089
+ element.undefinedLength = undefinedLength;
7028
7090
  }
7029
7091
  }
7030
7092
  } else {
7031
- // set the value and calculate size
7093
+ // pad if necessary
7094
+ if (dwv.dicom.isVrToPad(element.vr)) {
7095
+ var pad = dwv.dicom.getVrPad(element.vr);
7096
+ // encode string
7097
+ // TODO: not sure for UN...
7098
+ if (dwv.dicom.isStringVr(element.vr)) {
7099
+ if (dwv.dicom.charSetString.includes(element.vr)) {
7100
+ value = this.encodeSpecialString(value.join('\\'));
7101
+ pad = this.encodeSpecialString(pad);
7102
+ } else {
7103
+ value = this.encodeString(value.join('\\'));
7104
+ pad = this.encodeString(pad);
7105
+ }
7106
+ if (!dwv.dicom.isEven(value.length)) {
7107
+ value = dwv.dicom.uint8ArrayPush(value, pad);
7108
+ }
7109
+ } else if (element.vr === 'OB') {
7110
+ value = dwv.dicom.padOBValue(value);
7111
+ }
7112
+ }
7113
+
7114
+ // calculate byte size
7032
7115
  size = 0;
7033
- var paddedValue = dwv.dicom.padElementValue(element, value);
7034
7116
  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];
7117
+ size = 4 * value.length;
7118
+ } else if (element.vr === 'xs') {
7119
+ size = value.length * Uint16Array.BYTES_PER_ELEMENT;
7120
+ } else if (dwv.dicom.isTypedArrayVr(element.vr) || element.vr === 'ox') {
7121
+ if (dwv.dicom.isPixelDataTag(element.tag) &&
7122
+ Array.isArray(value)) {
7123
+ size = 0;
7124
+ for (var b = 0; b < value.length; ++b) {
7125
+ size += value[b].length;
7126
+ }
7041
7127
  } else {
7042
- element.value = paddedValue;
7128
+ size = value.length;
7043
7129
  }
7044
- size = element.value.length;
7045
7130
 
7046
7131
  // convert size to bytes
7047
7132
  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;
7133
+ if (dwv.dicom.isPixelDataTag(element.tag) || element.vr === 'ox') {
7134
+ if (element.undefinedLength) {
7135
+ var itemPrefixSize =
7136
+ dwv.dicom.getDataElementPrefixByteSize('NONE', isImplicit);
7137
+ // offset table
7138
+ size += itemPrefixSize;
7139
+ // pixel items
7140
+ size += itemPrefixSize * value.length;
7141
+ // add sequence delimitation size
7142
+ size += itemPrefixSize;
7067
7143
  } 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;
7144
+ // use bitsAllocated for pixel data
7145
+ // no need to multiply for 8 bits
7146
+ if (typeof bitsAllocated !== 'undefined') {
7147
+ if (bitsAllocated === 1) {
7148
+ // binary data
7149
+ size /= 8;
7150
+ } else if (bitsAllocated === 16) {
7151
+ size *= Uint16Array.BYTES_PER_ELEMENT;
7152
+ }
7078
7153
  }
7079
- // value
7080
- size += paddedValue[k].toString().length;
7081
7154
  }
7082
- } else {
7083
- element.value = [paddedValue];
7084
- if (typeof paddedValue !== 'undefined' &&
7085
- typeof paddedValue.length !== 'undefined') {
7086
- size = paddedValue.length;
7155
+ } else if (typeof vrType !== 'undefined') {
7156
+ var bpe = dwv.dicom.getBpeForVrType(vrType);
7157
+ if (typeof bpe !== 'undefined') {
7158
+ size *= bpe;
7087
7159
  } else {
7088
- // numbers
7089
- size = 1;
7160
+ throw Error('Unknown bytes per element for VR type: ' + vrType);
7090
7161
  }
7162
+ } else {
7163
+ throw Error('Unsupported element: ' + element.vr);
7091
7164
  }
7165
+ } else {
7166
+ size = value.length;
7092
7167
  }
7093
7168
 
7169
+ element.value = value;
7094
7170
  element.vl = size;
7095
7171
  }
7096
7172
 
@@ -7099,40 +7175,60 @@ dwv.dicom.setElementValue = function (element, value, isImplicit) {
7099
7175
  };
7100
7176
 
7101
7177
  /**
7102
- * Get the DICOM element from a DICOM tags object.
7178
+ * Get the DICOM elements from a DICOM json tags object.
7179
+ * The json is a simplified version of the oficial DICOM json with
7180
+ * tag names instead of keys and direct values (no value property) for
7181
+ * simple tags.
7103
7182
  *
7104
- * @param {object} tags The DICOM tags object.
7105
- * @returns {object} The DICOM elements and the end offset.
7183
+ * @param {object} jsonTags The DICOM json tags object.
7184
+ * @returns {object} The DICOM elements.
7106
7185
  */
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);
7186
+ dwv.dicom.getElementsFromJSONTags = function (jsonTags) {
7187
+ var keys = Object.keys(jsonTags);
7112
7188
  var dicomElements = {};
7113
- var dicomElement;
7114
- var name;
7115
- var offset = 128 + 4; // preamble
7116
- var size;
7117
7189
  for (var k = 0, len = keys.length; k < len; ++k) {
7118
7190
  // 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();
7191
+ var tag = dwv.dicom.getTagFromDictionary(keys[k]);
7192
+ var vr = tag.getVrFromDictionary();
7193
+ // tag value
7194
+ var value;
7195
+ var undefinedLength = false;
7196
+ var jsonTag = jsonTags[keys[k]];
7197
+ if (vr === 'SQ') {
7198
+ var items = [];
7199
+ if (typeof jsonTag.undefinedLength !== 'undefined') {
7200
+ undefinedLength = jsonTag.undefinedLength;
7201
+ }
7202
+ if (typeof jsonTag.value !== 'undefined' &&
7203
+ jsonTag.value !== null) {
7204
+ for (var i = 0; i < jsonTag.value.length; ++i) {
7205
+ items.push(dwv.dicom.getElementsFromJSONTags(jsonTag.value[i]));
7206
+ }
7207
+ } else {
7208
+ dwv.logger.trace('Undefined or null jsonTag SQ value.');
7209
+ }
7210
+ value = items;
7211
+ } else {
7212
+ if (Array.isArray(jsonTag)) {
7213
+ value = jsonTag;
7214
+ } else {
7215
+ value = [jsonTag];
7216
+ }
7217
+ }
7218
+ // create element
7219
+ var dicomElement = {
7220
+ tag: tag,
7221
+ vr: vr,
7222
+ value: value
7223
+ };
7224
+ if (undefinedLength) {
7225
+ dicomElement.undefinedLength = undefinedLength;
7226
+ }
7131
7227
  // store
7132
- dicomElements[name] = dicomElement;
7228
+ dicomElements[tag.getKey()] = dicomElement;
7133
7229
  }
7134
7230
  // return
7135
- return {elements: dicomElements, offset: offset};
7231
+ return dicomElements;
7136
7232
  };
7137
7233
 
7138
7234
  /*eslint max-len:0*/
@@ -14820,6 +14916,24 @@ dwv.gui.ViewLayer = function (containerDiv) {
14820
14916
  */
14821
14917
  var listenerHandler = new dwv.utils.ListenerHandler();
14822
14918
 
14919
+ /**
14920
+ * Image smoothing flag.
14921
+ * see: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/imageSmoothingEnabled
14922
+ *
14923
+ * @private
14924
+ * @type {boolean}
14925
+ */
14926
+ var imageSmoothingEnabled = false;
14927
+
14928
+ /**
14929
+ * Set the imageSmoothingEnabled flag value.
14930
+ *
14931
+ * @param {boolean} flag True to enable smoothing.
14932
+ */
14933
+ this.enableImageSmoothing = function (flag) {
14934
+ imageSmoothingEnabled = flag;
14935
+ };
14936
+
14823
14937
  /**
14824
14938
  * Set the associated view.
14825
14939
  *
@@ -15165,7 +15279,7 @@ dwv.gui.ViewLayer = function (containerDiv) {
15165
15279
  );
15166
15280
 
15167
15281
  // disable smoothing (set just before draw, could be reset by resize)
15168
- context.imageSmoothingEnabled = false;
15282
+ context.imageSmoothingEnabled = imageSmoothingEnabled;
15169
15283
  // draw image
15170
15284
  context.drawImage(offscreenCanvas, 0, 0);
15171
15285
 
@@ -15429,7 +15543,10 @@ dwv.gui.ViewLayer = function (containerDiv) {
15429
15543
  var skip = typeof event.skipGenerate !== 'undefined' &&
15430
15544
  event.skipGenerate === true;
15431
15545
  if (!skip) {
15432
- var valid = typeof event.valid === 'undefined' || event.valid;
15546
+ var valid = true;
15547
+ if (typeof event.valid !== 'undefined') {
15548
+ valid = event.valid;
15549
+ }
15433
15550
  // clear for non valid events
15434
15551
  if (!valid) {
15435
15552
  // clear only once
@@ -16796,12 +16913,12 @@ dwv.image.Geometry = function (origin, size, spacing, orientation, time) {
16796
16913
  * the input one.
16797
16914
  *
16798
16915
  * @param {number} time The time point to check.
16799
- * @returns {number} The count.
16916
+ * @returns {number|undefined} The count.
16800
16917
  */
16801
16918
  this.getCurrentNumberOfSlicesBeforeTime = function (time) {
16802
16919
  var keys = Object.keys(timeOrigins);
16803
16920
  if (keys.length === 0) {
16804
- return;
16921
+ return undefined;
16805
16922
  }
16806
16923
  var count = 0;
16807
16924
  for (var i = 0; i < keys.length; ++i) {
@@ -19165,7 +19282,7 @@ dwv.image.getRegionSliceIterator = function (
19165
19282
  * @param {dwv.math.Point} position The current position.
19166
19283
  * @param {boolean} isRescaled Flag for rescaled values (default false).
19167
19284
  * @param {Array} regions An array of regions.
19168
- * @returns {object} The slice iterator.
19285
+ * @returns {object|undefined} The slice iterator.
19169
19286
  */
19170
19287
  dwv.image.getVariableRegionSliceIterator = function (
19171
19288
  image, position, isRescaled, regions) {
@@ -19217,7 +19334,7 @@ dwv.image.getVariableRegionSliceIterator = function (
19217
19334
 
19218
19335
  // exit if no offsets
19219
19336
  if (offsetRegions.length === 0) {
19220
- return;
19337
+ return undefined;
19221
19338
  }
19222
19339
 
19223
19340
  var startOffset = size.indexToOffset(position.getWithNew2D(
@@ -20158,20 +20275,53 @@ dwv.image.MaskFactory.prototype.create = function (
20158
20275
  throw new Error('No imageOrientationPatient found for DICOM SEG');
20159
20276
  }
20160
20277
 
20161
- // add missing posPats
20278
+ var point3DFromArray = function (arr) {
20279
+ return new dwv.math.Point3D(arr[0], arr[1], arr[2]);
20280
+ };
20281
+
20282
+ // create geometry
20283
+ var origin = point3DFromArray(framePosPats[0]);
20284
+ var rowCosines = new dwv.math.Vector3D(
20285
+ parseFloat(imageOrientationPatient[0]),
20286
+ parseFloat(imageOrientationPatient[1]),
20287
+ parseFloat(imageOrientationPatient[2]));
20288
+ var colCosines = new dwv.math.Vector3D(
20289
+ parseFloat(imageOrientationPatient[3]),
20290
+ parseFloat(imageOrientationPatient[4]),
20291
+ parseFloat(imageOrientationPatient[5]));
20292
+ var normal = rowCosines.crossProduct(colCosines);
20293
+ /* eslint-disable array-element-newline */
20294
+ var orientationMatrix = new dwv.math.Matrix33([
20295
+ rowCosines.getX(), colCosines.getX(), normal.getX(),
20296
+ rowCosines.getY(), colCosines.getY(), normal.getY(),
20297
+ rowCosines.getZ(), colCosines.getZ(), normal.getZ()
20298
+ ]);
20299
+ var geometry = new dwv.image.Geometry(
20300
+ origin, size, spacing, orientationMatrix);
20301
+
20302
+ // add possibly missing posPats
20162
20303
  var posPats = [];
20163
- var sliceSpacing = spacing.get(2);
20164
- for (var g = 0; g < framePosPats.length - 1; ++g) {
20304
+ posPats.push(framePosPats[0]);
20305
+ var sliceIndex = 0;
20306
+ for (var g = 1; g < framePosPats.length; ++g) {
20307
+ ++sliceIndex;
20308
+ var index = new dwv.math.Index([0, 0, sliceIndex]);
20309
+ var point = geometry.indexToWorld(index).get3D();
20310
+ var framePosPat = point3DFromArray(framePosPats[g]);
20311
+ // check if more pos pats are needed
20312
+ var dist = framePosPat.getDistance(point);
20313
+ // TODO: good threshold?
20314
+ while (dist > 1e-4) {
20315
+ dwv.logger.debug('Adding intermediate pos pats for DICOM seg');
20316
+ posPats.push([point.getX(), point.getY(), point.getZ()]);
20317
+ ++sliceIndex;
20318
+ index = new dwv.math.Index([0, 0, sliceIndex]);
20319
+ point = geometry.indexToWorld(index).get3D();
20320
+ dist = framePosPat.getDistance(point);
20321
+ }
20322
+ // add frame pos pat
20165
20323
  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
20324
  }
20174
- posPats.push(framePosPats[framePosPats.length - 1]);
20175
20325
 
20176
20326
  var getFindSegmentFunc = function (number) {
20177
20327
  return function (item) {
@@ -20187,7 +20337,6 @@ dwv.image.MaskFactory.prototype.create = function (
20187
20337
  buffer.fill(0);
20188
20338
  // merge frame buffers
20189
20339
  var sliceOffset = null;
20190
- var sliceIndex = null;
20191
20340
  var frameOffset = null;
20192
20341
  for (var f = 0; f < frameInfos.length; ++f) {
20193
20342
  // get the slice index from the position in the posPat array
@@ -20213,16 +20362,6 @@ dwv.image.MaskFactory.prototype.create = function (
20213
20362
  }
20214
20363
  }
20215
20364
 
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
20365
  var uids = [0];
20227
20366
  for (var m = 1; m < numberOfSlices; ++m) {
20228
20367
  // args: origin, volumeNumber, uid, index, increment
@@ -26815,7 +26954,7 @@ dwv.math.getMatrixInverse = function (m) {
26815
26954
  var det = m00 * a1212 + m01 * a2012 + m02 * a0112;
26816
26955
  if (det === 0) {
26817
26956
  dwv.logger.warn('Cannot invert 3*3 matrix with zero determinant.');
26818
- return;
26957
+ return undefined;
26819
26958
  }
26820
26959
  det = 1 / det;
26821
26960
 
@@ -30244,12 +30383,12 @@ dwv.tool.Draw.prototype.hasShape = function (name) {
30244
30383
  * Get the minimum position in a groups' anchors.
30245
30384
  *
30246
30385
  * @param {object} group The group that contains anchors.
30247
- * @returns {object} The minimum position as {x,y}.
30386
+ * @returns {object|undefined} The minimum position as {x,y}.
30248
30387
  */
30249
30388
  dwv.tool.getAnchorMin = function (group) {
30250
30389
  var anchors = group.find('.anchor');
30251
30390
  if (anchors.length === 0) {
30252
- return;
30391
+ return undefined;
30253
30392
  }
30254
30393
  var minX = anchors[0].x();
30255
30394
  var minY = anchors[0].y();
@@ -35462,7 +35601,7 @@ dwv.utils.uint8ArrayToString = function (arr) {
35462
35601
  * @param {Function} callbackFn The find function.
35463
35602
  * @param {number} start The array start index.
35464
35603
  * @param {number} end The array end index.
35465
- * @returns {number} The index where the element was found.
35604
+ * @returns {number|undefined} The index where the element was found.
35466
35605
  */
35467
35606
  dwv.utils.findInArraySubset = function (arr, callbackFn, start, end) {
35468
35607
  // check inputs
@@ -35483,7 +35622,7 @@ dwv.utils.findInArraySubset = function (arr, callbackFn, start, end) {
35483
35622
  return i;
35484
35623
  }
35485
35624
  }
35486
- return;
35625
+ return undefined;
35487
35626
  };
35488
35627
 
35489
35628
  /**
@@ -36012,9 +36151,9 @@ dwv.env.askModernizr = function (property) {
36012
36151
  dwv.env.hasFileApi = function () {
36013
36152
  // regular test does not work on Safari 5
36014
36153
  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));
36154
+ (navigator.appVersion.indexOf('Chrome') === -1) &&
36155
+ ((navigator.appVersion.indexOf('5.0.') !== -1) ||
36156
+ (navigator.appVersion.indexOf('5.1.') !== -1));
36018
36157
  if (isSafari5) {
36019
36158
  dwv.logger.warn('Assuming FileAPI support for Safari5...');
36020
36159
  return true;
@@ -36030,9 +36169,9 @@ dwv.env.hasFileApi = function () {
36030
36169
  */
36031
36170
  dwv.env.hasXmlHttpRequest = function () {
36032
36171
  return dwv.env.askModernizr('xhrresponsetype') &&
36033
- dwv.env.askModernizr('xhrresponsetypearraybuffer') &&
36034
- dwv.env.askModernizr('xhrresponsetypetext') &&
36035
- 'XMLHttpRequest' in window && 'withCredentials' in new XMLHttpRequest();
36172
+ dwv.env.askModernizr('xhrresponsetypearraybuffer') &&
36173
+ dwv.env.askModernizr('xhrresponsetypetext') &&
36174
+ 'XMLHttpRequest' in window && 'withCredentials' in new XMLHttpRequest();
36036
36175
  };
36037
36176
 
36038
36177
  /**
@@ -36065,8 +36204,7 @@ dwv.env.hasInputDirectory = function () {
36065
36204
  return dwv.env.askModernizr('fileinputdirectory');
36066
36205
  };
36067
36206
 
36068
-
36069
- //only check at startup (since we propose a replacement)
36207
+ // only check at startup (since we propose a replacement)
36070
36208
  dwv.env._hasTypedArraySlice =
36071
36209
  (typeof Uint8Array.prototype.slice !== 'undefined');
36072
36210
 
@@ -36093,7 +36231,7 @@ dwv.env.hasFloat64Array = function () {
36093
36231
  return dwv.env._hasFloat64Array;
36094
36232
  };
36095
36233
 
36096
- //only check at startup (since we propose a replacement)
36234
+ // only check at startup (since we propose a replacement)
36097
36235
  dwv.env._hasClampedArray = ('Uint8ClampedArray' in window);
36098
36236
 
36099
36237
  /**
@@ -36120,6 +36258,30 @@ dwv.env.hasBigInt = function () {
36120
36258
  return dwv.env._hasBigint;
36121
36259
  };
36122
36260
 
36261
+ // Check if the TextDecoder is defined
36262
+ dwv.env._hasTextDecoder = (typeof TextDecoder !== 'undefined');
36263
+
36264
+ /**
36265
+ * Does the environement provide TextDecoder.
36266
+ *
36267
+ * @returns {boolean} True if the env supports the feature.
36268
+ */
36269
+ dwv.env.hasTextDecoder = function () {
36270
+ return dwv.env._hasTextDecoder;
36271
+ };
36272
+
36273
+ // Check if the TextEncoder is defined
36274
+ dwv.env._hasTextEncoder = (typeof TextEncoder !== 'undefined');
36275
+
36276
+ /**
36277
+ * Does the environement provide TextEncoder.
36278
+ *
36279
+ * @returns {boolean} True if the env supports the feature.
36280
+ */
36281
+ dwv.env.hasTextEncoder = function () {
36282
+ return dwv.env._hasTextEncoder;
36283
+ };
36284
+
36123
36285
  /**
36124
36286
  * Browser checks to see if it can run dwv. Throws an error if not.
36125
36287
  * Silently replaces basic functions.
@@ -36144,6 +36306,12 @@ dwv.env.check = function () {
36144
36306
  message = 'The Typed arrays are not supported in this browser. ';
36145
36307
  throw new Error(message);
36146
36308
  }
36309
+ // Check text encoding/decoding
36310
+ if (!dwv.env.hasTextDecoder() || !dwv.env.hasTextEncoder()) {
36311
+ message = 'Text decoding/encoding is not supported in this browser.';
36312
+ throw new Error(message);
36313
+ }
36314
+
36147
36315
  // Check Float64
36148
36316
  if (!dwv.env.hasFloat64Array()) {
36149
36317
  dwv.logger.warn('Float64Array is not supported in this browser. ' +