dcmjs 0.52.3 → 0.53.1

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;
@@ -11203,7 +11212,9 @@
11203
11212
  key: "read",
11204
11213
  value: function read(stream, length, syntax, readOptions) {
11205
11214
  var arrayBuffer = this.readBytes(stream, length, syntax)[0];
11206
- var streamFromBuffer = new ReadBufferStream(arrayBuffer, true);
11215
+ var streamFromBuffer = new ReadBufferStream(arrayBuffer, true, {
11216
+ decoder: stream.decoder
11217
+ });
11207
11218
  var vr = ValueRepresentation.createByTypeString(this.type);
11208
11219
  if (vr.isBinary() && length > vr.maxLength && !vr.noMultiple) {
11209
11220
  var values = [];
@@ -11330,6 +11341,208 @@
11330
11341
  UV: new Unsigned64BitVeryLong()
11331
11342
  };
11332
11343
 
11344
+ /**
11345
+ * Private block reservation, per PS3.5 Section 7.8.1.
11346
+ *
11347
+ * A private group is odd. Within it, a Private Creator Data Element at
11348
+ * (gggg,00xx) - where xx is 0x10 through 0xFF - identifies an implementer and
11349
+ * reserves the block of Data Elements (gggg,xx00) through (gggg,xxFF) for them.
11350
+ *
11351
+ * Which block a creator gets is decided by whoever wrote the file, so the
11352
+ * creator string is the identity and the block has to be looked up. A block
11353
+ * is named as in the 1.0 naturalized form: the block key "gggg:creator"
11354
+ * (for example "4D43:ACME 1.0"), or the pair (group, creator); an element of
11355
+ * the block is its number 0x00-0xFF, or the element key "xEE" (for example
11356
+ * "x01"):
11357
+ *
11358
+ * // writing
11359
+ * const block = dicomDict.privateBlock("4D43:ACME 1.0");
11360
+ * block.set("x01", "CS", ["HYBRID"]); // -> (4D43,1001), or wherever it landed
11361
+ *
11362
+ * // reading, from any parsed dict
11363
+ * const found = findPrivateBlock(dict, "4D43:ACME 1.0");
11364
+ * found && found.get("x01").Value;
11365
+ */
11366
+
11367
+ var CREATOR_MIN = 0x10;
11368
+ var CREATOR_MAX = 0xff;
11369
+ var CREATOR_MAX_LENGTH = 64; // LO
11370
+ var BLOCK_KEY = /^([0-9A-Fa-f]{4}):([^]+)$/;
11371
+ var ELEMENT_KEY = /^x([0-9A-Fa-f]{2})$/;
11372
+ var hex2 = function hex2(n) {
11373
+ return n.toString(16).toUpperCase().padStart(2, "0");
11374
+ };
11375
+ var hex4 = function hex4(n) {
11376
+ return n.toString(16).toUpperCase().padStart(4, "0");
11377
+ };
11378
+
11379
+ /**
11380
+ * The block key "gggg:creator" of a group and creator. A line break in the
11381
+ * creator is written as the text `\n` (and `\r`); LO holds no backslash, so
11382
+ * the key reads back to the same creator.
11383
+ */
11384
+ function privateBlockKey(group, creator) {
11385
+ var text = creator.replace(/\r/g, "\\r").replace(/\n/g, "\\n");
11386
+ return "".concat(hex4(group), ":").concat(text);
11387
+ }
11388
+
11389
+ /** The creator of the creator part of a block key: `\n` and `\r` unescaped. */
11390
+ function keyCreator(text) {
11391
+ return text.replace(/\\r/g, "\r").replace(/\\n/g, "\n");
11392
+ }
11393
+
11394
+ /**
11395
+ * The creator text of a creator element value: no leading spaces, no
11396
+ * trailing spaces or NULs. A value in bytes (UN) is read as Latin-1.
11397
+ */
11398
+ function creatorText(value) {
11399
+ var v = Array.isArray(value) ? value[0] : value;
11400
+ if (typeof v === "string") return v.replace(/^ +|[\0 ]+$/g, "");
11401
+ if (v instanceof ArrayBuffer || ArrayBuffer.isView(v)) {
11402
+ var bytes = ArrayBuffer.isView(v) ? new Uint8Array(v.buffer, v.byteOffset, v.byteLength) : new Uint8Array(v);
11403
+ return creatorText(String.fromCharCode.apply(String, _toConsumableArray(bytes.subarray(0, 64))));
11404
+ }
11405
+ return "";
11406
+ }
11407
+
11408
+ /** [group, creator] from (groupOrKey, creator), validated. */
11409
+ function blockArgs(groupOrKey, creator) {
11410
+ var group = groupOrKey;
11411
+ if (typeof groupOrKey === "string") {
11412
+ var match = BLOCK_KEY.exec(groupOrKey);
11413
+ if (!match) {
11414
+ throw new Error("Private block key must be \"gggg:creator\", got ".concat(groupOrKey));
11415
+ }
11416
+ group = parseInt(match[1], 16);
11417
+ creator = keyCreator(match[2]);
11418
+ }
11419
+ if (!Number.isInteger(group) || group < 0 || group > 0xffff) {
11420
+ throw new Error("Private group must be a 16 bit number, got ".concat(group));
11421
+ }
11422
+ if (group % 2 === 0) {
11423
+ throw new Error("Private group must be odd, got ".concat(hex4(group), " - an even group is standard, not private"));
11424
+ }
11425
+ if (typeof creator !== "string" || creatorText(creator).length === 0) {
11426
+ throw new Error("Private creator must be a non-empty string");
11427
+ }
11428
+ creator = creatorText(creator);
11429
+ if (creator.length > CREATOR_MAX_LENGTH) {
11430
+ throw new Error("Private creator must be at most ".concat(CREATOR_MAX_LENGTH, " characters (LO), got ").concat(creator.length));
11431
+ }
11432
+ return [group, creator];
11433
+ }
11434
+
11435
+ /** The element number of 0x00-0xFF or "xEE". */
11436
+ function elementNumber(element) {
11437
+ var match = typeof element === "string" && ELEMENT_KEY.exec(element);
11438
+ var n = match ? parseInt(match[1], 16) : element;
11439
+ if (!Number.isInteger(n) || n < 0x00 || n > 0xff) {
11440
+ throw new Error("Private element must be 0x00 to 0xFF or \"xEE\", got ".concat(element));
11441
+ }
11442
+ return n;
11443
+ }
11444
+
11445
+ /**
11446
+ * An accessor for one reserved block of a dict, keyed by 'GGGGEEEE'.
11447
+ * `reservedBlock` is the xx of the block in this dict.
11448
+ */
11449
+ function blockAccessor(dict, group, creator, reservedBlock) {
11450
+ var tag = function tag(element) {
11451
+ return hex4(group) + hex2(reservedBlock) + hex2(elementNumber(element));
11452
+ };
11453
+ return {
11454
+ key: privateBlockKey(group, creator),
11455
+ group: group,
11456
+ creator: creator,
11457
+ reservedBlock: reservedBlock,
11458
+ /** The 'GGGGEEEE' key of an element of this block. */
11459
+ tag: tag,
11460
+ /** Whether this block holds that element. */
11461
+ has: function has(element) {
11462
+ return Object.prototype.hasOwnProperty.call(dict, tag(element));
11463
+ },
11464
+ /** The element object, or undefined. */
11465
+ get: function get(element) {
11466
+ return dict[tag(element)];
11467
+ },
11468
+ /**
11469
+ * Writes an element of this block, as DicomDict#upsertTag does.
11470
+ *
11471
+ * @param {number|string} element 0x00 to 0xFF, or "xEE"
11472
+ * @param {string} vr value representation
11473
+ * @param {*} values value, or array of values
11474
+ * @returns {string} the tag that was written
11475
+ */
11476
+ set: function set(element, vr, values) {
11477
+ var key = tag(element);
11478
+ dict[key] = ValueRepresentation.addTagAccessors({
11479
+ vr: vr
11480
+ });
11481
+ dict[key].Value = Array.isArray(values) ? values : [values];
11482
+ return key;
11483
+ }
11484
+ };
11485
+ }
11486
+
11487
+ /**
11488
+ * Finds the block a creator has reserved in a dict.
11489
+ *
11490
+ * @param {object} dict parsed dataset, keyed by 'GGGGEEEE'
11491
+ * @param {number|string} groupOrKey odd group number, or block key "gggg:creator"
11492
+ * @param {string} [creator] creator string, with a group number
11493
+ * @returns {object|null} a block accessor, or null when the creator is absent
11494
+ */
11495
+ function findPrivateBlock(dict, groupOrKey, creator) {
11496
+ var _blockArgs = blockArgs(groupOrKey, creator),
11497
+ _blockArgs2 = _slicedToArray(_blockArgs, 2),
11498
+ group = _blockArgs2[0],
11499
+ wanted = _blockArgs2[1];
11500
+ for (var xx = CREATOR_MIN; xx <= CREATOR_MAX; xx++) {
11501
+ var element = dict[hex4(group) + hex4(xx)];
11502
+ if (element && creatorText(element.Value) === wanted) {
11503
+ return blockAccessor(dict, group, wanted, xx);
11504
+ }
11505
+ }
11506
+ return null;
11507
+ }
11508
+
11509
+ /**
11510
+ * Finds the block a creator has reserved, or reserves the first free block
11511
+ * and writes the creator element. A block is free when the dict holds no
11512
+ * creator element for it and no element in its range.
11513
+ *
11514
+ * @param {object} dict parsed dataset, keyed by 'GGGGEEEE'
11515
+ * @param {number|string} groupOrKey odd group number, or block key "gggg:creator"
11516
+ * @param {string} [creator] creator string, with a group number
11517
+ * @returns {object} a block accessor
11518
+ */
11519
+ function reservePrivateBlock(dict, groupOrKey, creator) {
11520
+ var existing = findPrivateBlock(dict, groupOrKey, creator);
11521
+ if (existing) return existing;
11522
+ var _blockArgs3 = blockArgs(groupOrKey, creator),
11523
+ _blockArgs4 = _slicedToArray(_blockArgs3, 2),
11524
+ group = _blockArgs4[0],
11525
+ wanted = _blockArgs4[1];
11526
+ var prefix = hex4(group);
11527
+ var used = new Set();
11528
+ for (var _i = 0, _Object$keys = Object.keys(dict); _i < _Object$keys.length; _i++) {
11529
+ var key = _Object$keys[_i];
11530
+ if (!key.startsWith(prefix) || key.length !== 8) continue;
11531
+ var element = parseInt(key.slice(4), 16);
11532
+ used.add(element <= 0xff ? element : element >> 8);
11533
+ }
11534
+ for (var xx = CREATOR_MIN; xx <= CREATOR_MAX; xx++) {
11535
+ if (!used.has(xx)) {
11536
+ dict[prefix + hex4(xx)] = ValueRepresentation.addTagAccessors({
11537
+ vr: "LO"
11538
+ });
11539
+ dict[prefix + hex4(xx)].Value = [wanted];
11540
+ return blockAccessor(dict, group, wanted, xx);
11541
+ }
11542
+ }
11543
+ throw new Error("No free private block in group ".concat(prefix, " - all of 0x10 to 0xFF are reserved"));
11544
+ }
11545
+
11333
11546
  var EXPLICIT_LITTLE_ENDIAN = "1.2.840.10008.1.2.1";
11334
11547
  var DicomMessage$2;
11335
11548
  var DicomDict = /*#__PURE__*/function () {
@@ -11351,6 +11564,35 @@
11351
11564
  this.dict[tag].Value = values;
11352
11565
  }
11353
11566
  }
11567
+
11568
+ /**
11569
+ * Reserves a private block for a creator, and writes its creator element
11570
+ * when the creator has no block yet; pass `{ create: false }` to only look
11571
+ * it up. See src/privateBlock.js: the block is found by creator, never
11572
+ * assumed.
11573
+ *
11574
+ * dicomDict.privateBlock("4D43:ACME 1.0").set("x01", "CS", ["HYBRID"]);
11575
+ *
11576
+ * @param {number|string} groupOrKey odd group number, or block key "gggg:creator"
11577
+ * @param {string} [creator] creator string, with a group number
11578
+ * @param {{create?: boolean}} [options] create defaults to true; false
11579
+ * returns null when the creator is absent
11580
+ * @returns {object|null} accessor with key, group, creator, reservedBlock,
11581
+ * tag, has, get and set
11582
+ */
11583
+ }, {
11584
+ key: "privateBlock",
11585
+ value: function privateBlock(groupOrKey, creator) {
11586
+ var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
11587
+ if (typeof groupOrKey === "string" && _typeof(creator) === "object") {
11588
+ options = creator;
11589
+ creator = undefined;
11590
+ }
11591
+ var _ref = options || {},
11592
+ _ref$create = _ref.create,
11593
+ create = _ref$create === void 0 ? true : _ref$create;
11594
+ return create ? reservePrivateBlock(this.dict, groupOrKey, creator) : findPrivateBlock(this.dict, groupOrKey, creator);
11595
+ }
11354
11596
  }, {
11355
11597
  key: "write",
11356
11598
  value: function write() {
@@ -14365,7 +14607,7 @@
14365
14607
  break;
14366
14608
  }
14367
14609
  // Group length
14368
- stream.increment(tagObj.length);
14610
+ stream.increment(length);
14369
14611
  return _context5.abrupt("continue", 1);
14370
14612
  case 5:
14371
14613
  addTagResult = listener.addTag(tag, tagInfo);
@@ -14431,7 +14673,7 @@
14431
14673
  key: "readSequence",
14432
14674
  value: function () {
14433
14675
  var _readSequence = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(listener, sqTagInfo, options) {
14434
- var length, stream, syntax, endOffset, tagInfo, tag, itemLength, itemUntilOffset, _t;
14676
+ var length, stream, syntax, endOffset, tagInfo, tag, itemLength, itemUntilOffset, decoder, _t;
14435
14677
  return _regeneratorRuntime().wrap(function (_context6) {
14436
14678
  while (1) switch (_context6.prev = _context6.next) {
14437
14679
  case 0:
@@ -14468,12 +14710,16 @@
14468
14710
  // first item because read() treats subsequent Item tags
14469
14711
  // as instructions and skips them.
14470
14712
  itemLength = tagInfo.length;
14471
- itemUntilOffset = itemLength === UNDEFINED_LENGTH_FIX ? endOffset : Math.min(stream.offset + itemLength, endOffset);
14713
+ itemUntilOffset = itemLength === UNDEFINED_LENGTH_FIX ? endOffset : Math.min(stream.offset + itemLength, endOffset); // A SpecificCharacterSet in the item applies only to that
14714
+ // item (PS3.5 7.5.3), but the item is read on this stream:
14715
+ // put back the decoder the item started with.
14716
+ decoder = stream.decoder;
14472
14717
  _context6.next = 4;
14473
14718
  return this.read(listener, _objectSpread2(_objectSpread2({}, options), {}, {
14474
14719
  untilOffset: itemUntilOffset
14475
14720
  }));
14476
14721
  case 4:
14722
+ stream.setDecoder(decoder);
14477
14723
  _context6.next = 7;
14478
14724
  break;
14479
14725
  case 5:
@@ -28193,7 +28439,12 @@
28193
28439
  Colors: Colors,
28194
28440
  datasetToDict: datasetToDict,
28195
28441
  datasetToBuffer: datasetToBuffer,
28196
- datasetToBlob: datasetToBlob
28442
+ datasetToBlob: datasetToBlob,
28443
+ // Private block reservation, PS3.5 Section 7.8.1. findPrivateBlock() works
28444
+ // on any parsed dict; DicomDict#privateBlock() is the writing side.
28445
+ findPrivateBlock: findPrivateBlock,
28446
+ reservePrivateBlock: reservePrivateBlock,
28447
+ privateBlockKey: privateBlockKey
28197
28448
  };
28198
28449
  var async = {
28199
28450
  AsyncDicomReader: AsyncDicomReader