dcmjs 0.53.0 → 0.53.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/dcmjs.js CHANGED
@@ -9013,6 +9013,12 @@
9013
9013
  var end = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : start + 1;
9014
9014
  return this.view.hasData(start, end);
9015
9015
  }
9016
+
9017
+ /**
9018
+ * Returns a read stream over the next `length` bytes and moves past them.
9019
+ * The new stream decodes text with this stream's decoder, so sequence items
9020
+ * use the dataset's SpecificCharacterSet.
9021
+ */
9016
9022
  }, {
9017
9023
  key: "more",
9018
9024
  value: function more(length) {
@@ -9026,7 +9032,9 @@
9026
9032
  // start: this.offset,
9027
9033
  // stop: this.offset + length
9028
9034
  // });
9029
- var newBuf = new ReadBufferStream(this.slice(this.offset, this.offset + length));
9035
+ var newBuf = new ReadBufferStream(this.slice(this.offset, this.offset + length), null, {
9036
+ decoder: this.decoder
9037
+ });
9030
9038
  this.increment(length);
9031
9039
  newBuf.setComplete();
9032
9040
  return newBuf;
@@ -9077,7 +9085,8 @@
9077
9085
  littleEndian: littleEndian
9078
9086
  })]);
9079
9087
  _this2.noCopy = options.noCopy;
9080
- _this2.decoder = new TextDecoder("latin1");
9088
+ // A stream over part of a dataset gets the dataset's decoder in options
9089
+ _this2.decoder = options.decoder || new TextDecoder("latin1");
9081
9090
  if (buffer instanceof BufferStream) {
9082
9091
  _this2.view.from(buffer.view, options);
9083
9092
  _this2.isComplete = true;
@@ -10881,6 +10890,8 @@
10881
10890
  if (undef) stream.increment(8);
10882
10891
  var items = DicomMessage$3._read(itemStream, syntax);
10883
10892
  elements.push(items);
10893
+ } else {
10894
+ elements.push({});
10884
10895
  }
10885
10896
  if (!undefLength && read == sqlength) {
10886
10897
  break;
@@ -11203,7 +11214,9 @@
11203
11214
  key: "read",
11204
11215
  value: function read(stream, length, syntax, readOptions) {
11205
11216
  var arrayBuffer = this.readBytes(stream, length, syntax)[0];
11206
- var streamFromBuffer = new ReadBufferStream(arrayBuffer, true);
11217
+ var streamFromBuffer = new ReadBufferStream(arrayBuffer, true, {
11218
+ decoder: stream.decoder
11219
+ });
11207
11220
  var vr = ValueRepresentation.createByTypeString(this.type);
11208
11221
  if (vr.isBinary() && length > vr.maxLength && !vr.noMultiple) {
11209
11222
  var values = [];
@@ -12852,6 +12865,13 @@
12852
12865
  return String(paddingValue + string).slice(-paddingValue.length);
12853
12866
  }
12854
12867
  var DicomMessage$1;
12868
+
12869
+ /** True when every value is an item dataset object, not binary data. */
12870
+ function isItemList(values) {
12871
+ return Array.isArray(values) && values.length > 0 && values.every(function (v) {
12872
+ return v !== null && _typeof(v) === "object" && !(v instanceof ArrayBuffer) && !ArrayBuffer.isView(v);
12873
+ });
12874
+ }
12855
12875
  var Tag = /*#__PURE__*/function () {
12856
12876
  function Tag(value) {
12857
12877
  _classCallCheck(this, Tag);
@@ -12926,6 +12946,10 @@
12926
12946
  }, {
12927
12947
  key: "write",
12928
12948
  value: function write(stream, vrType, values, syntax, writeOptions) {
12949
+ // A UN whose values are item datasets is a sequence: write it as SQ (PS3.5 §6.2.2).
12950
+ if (vrType === "UN" && isItemList(values)) {
12951
+ vrType = "SQ";
12952
+ }
12929
12953
  var vr = ValueRepresentation.createByTypeString(vrType);
12930
12954
  var useSyntax = DicomMessage$1._normalizeSyntax(syntax);
12931
12955
  var implicit = useSyntax === IMPLICIT_LITTLE_ENDIAN;
@@ -13066,6 +13090,15 @@
13066
13090
  }
13067
13091
 
13068
13092
  var singleVRs = ["SQ", "OF", "OW", "OB", "UN", "LT"];
13093
+
13094
+ /**
13095
+ * PS3.5 §6.2.2: a UN value that holds a sequence is Implicit VR LE.
13096
+ * dcmjs finds that case by the item tag (FFFE,E000), FE FF 00 E0, at the
13097
+ * start of a defined-length value.
13098
+ */
13099
+ function isUnSequenceValue(stream, length) {
13100
+ return length >= 8 && length !== UNDEFINED_LENGTH && stream.peekUint8(0) === 0xfe && stream.peekUint8(1) === 0xff && stream.peekUint8(2) === 0x00 && stream.peekUint8(3) === 0xe0;
13101
+ }
13069
13102
  var DicomMessage = /*#__PURE__*/function () {
13070
13103
  function DicomMessage() {
13071
13104
  _classCallCheck(this, DicomMessage);
@@ -13305,7 +13338,9 @@
13305
13338
  }
13306
13339
  var length = null,
13307
13340
  vr = null,
13308
- vrType;
13341
+ vrType,
13342
+ wireVrType = null; // VR as literally encoded in the stream (explicit mode)
13343
+
13309
13344
  if (implicit) {
13310
13345
  length = stream.readUint32();
13311
13346
  var elementData = DicomMessage.lookupTag(tag);
@@ -13328,6 +13363,8 @@
13328
13363
  vr = ValueRepresentation.createByTypeString(vrType);
13329
13364
  } else {
13330
13365
  vrType = stream.readVR();
13366
+ wireVrType = vrType; // remember literal stream VR before any override
13367
+
13331
13368
  if (vrType === "UN" && DicomMessage.lookupTag(tag) && DicomMessage.lookupTag(tag).vr) {
13332
13369
  vrType = DicomMessage.lookupTag(tag).vr;
13333
13370
  vr = ValueRepresentation.parseUnknownVr(vrType);
@@ -13341,20 +13378,26 @@
13341
13378
  length = stream.readUint16();
13342
13379
  }
13343
13380
  }
13381
+ var readSyntax = syntax;
13382
+ if (!implicit && wireVrType === "UN" && isUnSequenceValue(stream, length)) {
13383
+ vrType = "SQ";
13384
+ vr = ValueRepresentation.createByTypeString("SQ");
13385
+ readSyntax = IMPLICIT_LITTLE_ENDIAN;
13386
+ }
13344
13387
  var values = [];
13345
13388
  var rawValues = [];
13346
13389
  if (vr.isBinary() && length > vr.maxLength && !vr.noMultiple) {
13347
13390
  var times = length / vr.maxLength,
13348
13391
  i = 0;
13349
13392
  while (i++ < times) {
13350
- var _vr$read = vr.read(stream, vr.maxLength, syntax, options),
13393
+ var _vr$read = vr.read(stream, vr.maxLength, readSyntax, options),
13351
13394
  rawValue = _vr$read.rawValue,
13352
13395
  value = _vr$read.value;
13353
13396
  rawValues.push(rawValue);
13354
13397
  values.push(value);
13355
13398
  }
13356
13399
  } else {
13357
- var _ref = vr.read(stream, length, syntax, options) || {},
13400
+ var _ref = vr.read(stream, length, readSyntax, options) || {},
13358
13401
  _rawValue = _ref.rawValue,
13359
13402
  _value = _ref.value;
13360
13403
  if (!vr.isBinary() && singleVRs.indexOf(vr.type) == -1) {
@@ -14662,12 +14705,14 @@
14662
14705
  key: "readSequence",
14663
14706
  value: function () {
14664
14707
  var _readSequence = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(listener, sqTagInfo, options) {
14665
- var length, stream, syntax, endOffset, tagInfo, tag, itemLength, itemUntilOffset, _t;
14708
+ var _ref, _sqTagInfo$sequenceRe;
14709
+ var length, stream, sequenceReadSyntax, endOffset, tagInfo, tag, itemLength, itemUntilOffset, decoder, _t;
14666
14710
  return _regeneratorRuntime().wrap(function (_context6) {
14667
14711
  while (1) switch (_context6.prev = _context6.next) {
14668
14712
  case 0:
14669
14713
  length = sqTagInfo.length;
14670
- stream = this.stream, syntax = this.syntax;
14714
+ stream = this.stream;
14715
+ sequenceReadSyntax = (_ref = (_sqTagInfo$sequenceRe = sqTagInfo.sequenceReadSyntax) !== null && _sqTagInfo$sequenceRe !== void 0 ? _sqTagInfo$sequenceRe : options === null || options === void 0 ? void 0 : options.readSyntax) !== null && _ref !== void 0 ? _ref : this.syntax;
14671
14716
  endOffset = length === UNDEFINED_LENGTH_FIX ? Number.MAX_SAFE_INTEGER : stream.offset + length;
14672
14717
  case 1:
14673
14718
  _t = stream.offset < endOffset;
@@ -14685,7 +14730,9 @@
14685
14730
  break;
14686
14731
  }
14687
14732
  readLog.debug("readSequence loop", stream.offset, endOffset);
14688
- tagInfo = this.readTagHeader(syntax, options);
14733
+ tagInfo = this.readTagHeader(_objectSpread2(_objectSpread2({}, options), {}, {
14734
+ readSyntax: sequenceReadSyntax
14735
+ }));
14689
14736
  tag = tagInfo.tag;
14690
14737
  if (!(tag === TagHex.Item)) {
14691
14738
  _context6.next = 5;
@@ -14699,12 +14746,17 @@
14699
14746
  // first item because read() treats subsequent Item tags
14700
14747
  // as instructions and skips them.
14701
14748
  itemLength = tagInfo.length;
14702
- itemUntilOffset = itemLength === UNDEFINED_LENGTH_FIX ? endOffset : Math.min(stream.offset + itemLength, endOffset);
14749
+ itemUntilOffset = itemLength === UNDEFINED_LENGTH_FIX ? endOffset : Math.min(stream.offset + itemLength, endOffset); // A SpecificCharacterSet in the item applies only to that
14750
+ // item (PS3.5 7.5.3), but the item is read on this stream:
14751
+ // put back the decoder the item started with.
14752
+ decoder = stream.decoder;
14703
14753
  _context6.next = 4;
14704
14754
  return this.read(listener, _objectSpread2(_objectSpread2({}, options), {}, {
14705
- untilOffset: itemUntilOffset
14755
+ untilOffset: itemUntilOffset,
14756
+ readSyntax: sequenceReadSyntax
14706
14757
  }));
14707
14758
  case 4:
14759
+ stream.setDecoder(decoder);
14708
14760
  _context6.next = 7;
14709
14761
  break;
14710
14762
  case 5:
@@ -15130,16 +15182,17 @@
15130
15182
  }, {
15131
15183
  key: "readTagHeader",
15132
15184
  value: function readTagHeader() {
15185
+ var _options$readSyntax;
15133
15186
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
15134
15187
  untilTag: null,
15135
15188
  includeUntilTagValue: false
15136
15189
  };
15137
- var stream = this.stream,
15138
- syntax = this.syntax;
15190
+ var stream = this.stream;
15139
15191
  var untilTag = options.untilTag,
15140
15192
  includeUntilTagValue = options.includeUntilTagValue;
15141
- var implicit = syntax == IMPLICIT_LITTLE_ENDIAN;
15142
- var isLittleEndian = syntax !== EXPLICIT_BIG_ENDIAN;
15193
+ var effectiveSyntax = (_options$readSyntax = options === null || options === void 0 ? void 0 : options.readSyntax) !== null && _options$readSyntax !== void 0 ? _options$readSyntax : this.syntax;
15194
+ var implicit = effectiveSyntax == IMPLICIT_LITTLE_ENDIAN;
15195
+ var isLittleEndian = effectiveSyntax !== EXPLICIT_BIG_ENDIAN;
15143
15196
  stream.setEndian(isLittleEndian);
15144
15197
  var tagObj = Tag.readTag(stream);
15145
15198
  var tag = tagObj.cleanString;
@@ -15157,6 +15210,7 @@
15157
15210
  var length = null;
15158
15211
  var vr = null;
15159
15212
  var vrType;
15213
+ var wireVrType = null;
15160
15214
  var isCommand = tagObj.group() === 0;
15161
15215
  if (tagObj.isInstruction()) {
15162
15216
  length = stream.readUint32();
@@ -15188,6 +15242,7 @@
15188
15242
  } else {
15189
15243
  var _DicomMessage$lookupT;
15190
15244
  vrType = stream.readVR();
15245
+ wireVrType = vrType;
15191
15246
  if (vrType === "UN" && (_DicomMessage$lookupT = DicomMessage.lookupTag(tagObj)) !== null && _DicomMessage$lookupT !== void 0 && _DicomMessage$lookupT.vr) {
15192
15247
  vrType = DicomMessage.lookupTag(tagObj).vr;
15193
15248
  vr = ValueRepresentation.parseUnknownVr(vrType);
@@ -15201,11 +15256,16 @@
15201
15256
  length = stream.readUint16();
15202
15257
  }
15203
15258
  }
15259
+ var sequenceReadSyntax;
15260
+ if (!implicit && wireVrType === "UN" && isUnSequenceValue(stream, length)) {
15261
+ vr = ValueRepresentation.createByTypeString("SQ");
15262
+ sequenceReadSyntax = IMPLICIT_LITTLE_ENDIAN;
15263
+ }
15204
15264
  var punctuatedTag = DicomMetaDictionary.punctuateTag(tag);
15205
15265
  var entry = DicomMetaDictionary.dictionary[punctuatedTag];
15206
15266
 
15207
15267
  // Include both the string and object values for vr and tag
15208
- var header = {
15268
+ var header = _objectSpread2({
15209
15269
  vrObj: vr,
15210
15270
  vr: vr.type,
15211
15271
  tag: tag,
@@ -15213,7 +15273,9 @@
15213
15273
  vm: entry === null || entry === void 0 ? void 0 : entry.vm,
15214
15274
  name: entry === null || entry === void 0 ? void 0 : entry.name,
15215
15275
  length: length === UNDEFINED_LENGTH ? -1 : length
15216
- };
15276
+ }, sequenceReadSyntax !== undefined && {
15277
+ sequenceReadSyntax: sequenceReadSyntax
15278
+ });
15217
15279
  return header;
15218
15280
  }
15219
15281
 
@@ -15227,12 +15289,14 @@
15227
15289
  key: "readSingle",
15228
15290
  value: (function () {
15229
15291
  var _readSingle = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee11(tagInfo, listener, options) {
15230
- var length, stream, syntax, vr, values, times, i, _vr$read, value, _vr$read2, _value, delimiterChar, _values, _values2, coding, valuesForListener;
15292
+ var _options$readSyntax2;
15293
+ var length, stream, readSyntax, vr, values, times, i, _vr$read, value, _vr$read2, _value, delimiterChar, _values, _values2, coding, valuesForListener;
15231
15294
  return _regeneratorRuntime().wrap(function (_context11) {
15232
15295
  while (1) switch (_context11.prev = _context11.next) {
15233
15296
  case 0:
15234
15297
  length = tagInfo.length;
15235
- stream = this.stream, syntax = this.syntax;
15298
+ stream = this.stream;
15299
+ readSyntax = (_options$readSyntax2 = options === null || options === void 0 ? void 0 : options.readSyntax) !== null && _options$readSyntax2 !== void 0 ? _options$readSyntax2 : this.syntax;
15236
15300
  _context11.next = 1;
15237
15301
  return this.stream.ensureAvailable(length);
15238
15302
  case 1:
@@ -15243,11 +15307,11 @@
15243
15307
  i = 0;
15244
15308
  while (i++ < times) {
15245
15309
  readLog.trace("readSingle multi-value loop", i, times);
15246
- _vr$read = vr.read(stream, vr.maxLength, syntax), value = _vr$read.value;
15310
+ _vr$read = vr.read(stream, vr.maxLength, readSyntax), value = _vr$read.value;
15247
15311
  values.push(value);
15248
15312
  }
15249
15313
  } else {
15250
- _value = (_vr$read2 = vr.read(stream, length, syntax)) === null || _vr$read2 === void 0 ? void 0 : _vr$read2.value;
15314
+ _value = (_vr$read2 = vr.read(stream, length, readSyntax)) === null || _vr$read2 === void 0 ? void 0 : _vr$read2.value;
15251
15315
  if (!vr.isBinary() && singleVRs.indexOf(vr.type) == -1) {
15252
15316
  values = _value;
15253
15317
  if (typeof _value === "string") {
@@ -19022,7 +19086,7 @@
19022
19086
  var _matrix$shape = _slicedToArray(matrix.shape, 2),
19023
19087
  rows = _matrix$shape[0],
19024
19088
  cols = _matrix$shape[1];
19025
- var result = ndarray(new Uint8Array(rows * cols), [rows, cols]);
19089
+ var result = ndarray(new matrix.data.constructor(rows * cols), [rows, cols]);
19026
19090
  for (var i = 0; i < rows; i++) {
19027
19091
  for (var j = 0; j < cols; j++) {
19028
19092
  result.set(i, j, matrix.get(i, cols - 1 - j));
@@ -19041,7 +19105,7 @@
19041
19105
  var _matrix$shape2 = _slicedToArray(matrix.shape, 2),
19042
19106
  rows = _matrix$shape2[0],
19043
19107
  cols = _matrix$shape2[1];
19044
- var result = ndarray(new Uint8Array(rows * cols), [rows, cols]);
19108
+ var result = ndarray(new matrix.data.constructor(rows * cols), [rows, cols]);
19045
19109
  for (var j = 0; j < cols; j++) {
19046
19110
  for (var i = 0; i < rows; i++) {
19047
19111
  result.set(i, j, matrix.get(rows - 1 - i, j));
@@ -19060,7 +19124,7 @@
19060
19124
  var _matrix$shape = _slicedToArray(matrix.shape, 2),
19061
19125
  rows = _matrix$shape[0],
19062
19126
  cols = _matrix$shape[1];
19063
- var result = ndarray(new Uint8Array(rows * cols), [cols, rows]);
19127
+ var result = ndarray(new matrix.data.constructor(rows * cols), [cols, rows]);
19064
19128
  var resultColsMinus1 = result.shape[1] - 1;
19065
19129
  for (var i = 0; i < rows; i++) {
19066
19130
  for (var j = 0; j < cols; j++) {