@dcmjs-org/dcmjs 1.0.0-beta.11 → 1.0.0-beta.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/dcmjs.cjs CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var dcmjs = require('./dcmjs.shared-CvR7StGL.cjs');
5
+ var dcmjs = require('./dcmjs.shared-DI53lI_H.cjs');
6
6
  var core = require('@dcmjs-org/core');
7
7
 
8
8
 
package/build/dcmjs.es.js CHANGED
@@ -1,3 +1,3 @@
1
- export { D as DICOMWEB, j as adapters, k as anonymizer, l as async, m as constants, n as data, o as default, p as derivations, q as normalizers, s as sr, u as utilities } from './dcmjs.shared-DzVDSFp8.js';
1
+ export { D as DICOMWEB, j as adapters, k as anonymizer, l as async, m as constants, n as data, o as default, p as derivations, q as normalizers, s as sr, u as utilities } from './dcmjs.shared-CP8p6SfT.js';
2
2
  export { log } from '@dcmjs-org/core';
3
3
  //# sourceMappingURL=dcmjs.es.js.map
package/build/dcmjs.js CHANGED
@@ -12880,10 +12880,10 @@
12880
12880
  * the specification.
12881
12881
  */
12882
12882
 
12883
- // `s`: the creator may hold a line break; PS3.5 6.2 forbids one in LO, but a
12884
- // reader of real data can get one, and the block must still be written.
12885
- var BLOCK_KEY = /^([0-9A-Fa-f]{4}):([^]+)$/;
12886
- var ELEMENT_KEY$1 = /^x([0-9A-Fa-f]{2})$/;
12883
+ // `s`: a key that a user writes may hold a line break; the producers write
12884
+ // it as the text `\n` (privateBlockKey).
12885
+ var BLOCK_KEY$1 = /^([0-9A-Fa-f]{4}):([^]+)$/;
12886
+ var ELEMENT_KEY$2 = /^x([0-9A-Fa-f]{2})$/;
12887
12887
  var HEX_TAG = /^[0-9A-Fa-f]{8}$/;
12888
12888
 
12889
12889
  /** True for an 8-digit hex tag key, for example "00291001". */
@@ -12896,7 +12896,7 @@
12896
12896
  // LO holds at most 64 characters.
12897
12897
  var CREATOR_MAX_BYTES = 64;
12898
12898
  var latin1 = createLatin1Decoder();
12899
- function hex2(n) {
12899
+ function hex2$1(n) {
12900
12900
  return n.toString(16).toUpperCase().padStart(2, "0");
12901
12901
  }
12902
12902
  function tagOf(hexTag) {
@@ -12912,9 +12912,26 @@
12912
12912
  return !!tag && (tag.isPrivateCreator() || tag.isPrivateValue());
12913
12913
  }
12914
12914
 
12915
+ /**
12916
+ * The block key `gggg:creator` of a group (a number, or 4 hex digits) and a
12917
+ * creator. A line break in the creator is written as the text `\n` (and
12918
+ * `\r`); LO holds no backslash, so the key reads back to the same creator
12919
+ * (NAT-U-PRIV-1).
12920
+ */
12921
+ function privateBlockKey(group, creator) {
12922
+ var gggg = typeof group === "number" ? group.toString(16).toUpperCase().padStart(4, "0") : group.toUpperCase();
12923
+ var text = creator.replace(/\r/g, "\\r").replace(/\n/g, "\\n");
12924
+ return "".concat(gggg, ":").concat(text);
12925
+ }
12926
+
12927
+ /** The creator of the creator part of a block key: `\n` and `\r` unescaped. */
12928
+ function keyCreator(text) {
12929
+ return text.replace(/\\r/g, "\r").replace(/\\n/g, "\n");
12930
+ }
12931
+
12915
12932
  /** The group and creator of a reserved block key, or undefined. */
12916
12933
  function parsePrivateBlockKey(key, value) {
12917
- var match = BLOCK_KEY.exec(key);
12934
+ var match = BLOCK_KEY$1.exec(key);
12918
12935
  if (!match || !value || _typeof(value) !== "object" || Array.isArray(value)) {
12919
12936
  return undefined;
12920
12937
  }
@@ -12922,7 +12939,7 @@
12922
12939
  if ((parseInt(group, 16) & 1) !== 1) return undefined;
12923
12940
  return {
12924
12941
  group: group,
12925
- creator: match[2]
12942
+ creator: keyCreator(match[2])
12926
12943
  };
12927
12944
  }
12928
12945
  function privateDictionaryEntry(group, creator, ee) {
@@ -13035,11 +13052,11 @@
13035
13052
  if (!parsed || typeof block.reservedBlock !== "number") return 1; // continue
13036
13053
  var group = parsed.group,
13037
13054
  creator = parsed.creator;
13038
- var xx = hex2(block.reservedBlock);
13055
+ var xx = hex2$1(block.reservedBlock);
13039
13056
  onDataset("".concat(group, "00").concat(xx), key, null);
13040
13057
  var _loop2 = function _loop2() {
13041
13058
  var elementKey = _Object$keys3[_i3];
13042
- var match = ELEMENT_KEY$1.exec(elementKey);
13059
+ var match = ELEMENT_KEY$2.exec(elementKey);
13043
13060
  if (!match) return 1; // continue
13044
13061
  var ee = match[1].toUpperCase();
13045
13062
  var tag = "".concat(group).concat(xx).concat(ee);
@@ -13096,7 +13113,7 @@
13096
13113
  var creator = creatorText(value);
13097
13114
  if (creator === "") continue;
13098
13115
  var hexTag = tag.toUpperCase();
13099
- var key = "".concat(hexTag.slice(0, 4), ":").concat(creator);
13116
+ var key = privateBlockKey(hexTag.slice(0, 4), creator);
13100
13117
  if (blockOfCreator.has(key)) {
13101
13118
  // PS3.5 7.8.1 permits one block per creator in a group
13102
13119
  log.warn("Private creator \"".concat(creator, "\" reserves two blocks in group ").concat(hexTag.slice(0, 4), " (").concat(blockOfCreator.get(key), " and ").concat(hexTag.slice(6, 8), "); their elements merge into one block, and an element in both keeps the last value"));
@@ -13138,7 +13155,7 @@
13138
13155
  recordVr(vrMap, name, entry, vr);
13139
13156
  continue;
13140
13157
  }
13141
- var _key = "".concat(group, ":").concat(_creator);
13158
+ var _key = privateBlockKey(group, _creator);
13142
13159
  var block = blockFor(target, group, _creator, _tag.slice(4, 6));
13143
13160
  var elementKey = "x".concat(_tag.slice(6, 8));
13144
13161
  block[elementKey] = _value;
@@ -13178,7 +13195,7 @@
13178
13195
  return text.replace(/^ +|[\0 ]+$/g, "");
13179
13196
  }
13180
13197
  function blockFor(target, group, creator, xx) {
13181
- var key = "".concat(group, ":").concat(creator);
13198
+ var key = privateBlockKey(group, creator);
13182
13199
  if (!target[key]) {
13183
13200
  target[key] = {
13184
13201
  creator: creator,
@@ -13394,7 +13411,7 @@
13394
13411
  if (b.block.creator !== undefined && b.block.creator !== b.creator) {
13395
13412
  log.warn("Private block ".concat(key, " has creator \"").concat(b.block.creator, "\"; the key creator \"").concat(b.creator, "\" is used"));
13396
13413
  }
13397
- var creatorTag = "".concat(b.group, "00").concat(hex2(xx));
13414
+ var creatorTag = "".concat(b.group, "00").concat(hex2$1(xx));
13398
13415
  view[creatorTag] = b.creator;
13399
13416
  vrMap[creatorTag] = "LO";
13400
13417
  var blockVrs = sourceVrMap[key] || {};
@@ -13405,7 +13422,7 @@
13405
13422
  log.warn("No VR for ".concat(name, " in private block"), key);
13406
13423
  return;
13407
13424
  }
13408
- var tag = "".concat(b.group).concat(hex2(xx)).concat(ee);
13425
+ var tag = "".concat(b.group).concat(hex2$1(xx)).concat(ee);
13409
13426
  view[tag] = value;
13410
13427
  vrMap[tag] = vr;
13411
13428
  };
@@ -13414,7 +13431,7 @@
13414
13431
  if (elementKey === "creator" || elementKey === "reservedBlock") {
13415
13432
  continue;
13416
13433
  }
13417
- var match = ELEMENT_KEY$1.exec(elementKey);
13434
+ var match = ELEMENT_KEY$2.exec(elementKey);
13418
13435
  if (!match) {
13419
13436
  log.warn("Unknown name in private block ".concat(key, ":"), elementKey);
13420
13437
  continue;
@@ -14705,7 +14722,7 @@
14705
14722
  fileStream.writeRawBytes(pako.deflateRaw(new Uint8Array(bodyStream.getBuffer())));
14706
14723
  }
14707
14724
 
14708
- var ELEMENT_KEY = /^x[0-9A-Fa-f]{2}$/;
14725
+ var ELEMENT_KEY$1 = /^x[0-9A-Fa-f]{2}$/;
14709
14726
 
14710
14727
  /**
14711
14728
  * The view of one frame of a multi-frame naturalized dataset (PS3.3
@@ -14833,7 +14850,7 @@
14833
14850
  for (var _i3 = 0, _Object$keys3 = Object.keys(value); _i3 < _Object$keys3.length; _i3++) {
14834
14851
  var _vrMap$key;
14835
14852
  var elementKey = _Object$keys3[_i3];
14836
- if (ELEMENT_KEY.test(elementKey) && ((_vrMap$key = vrMap[key]) === null || _vrMap$key === void 0 ? void 0 : _vrMap$key[elementKey]) === "SQ") {
14853
+ if (ELEMENT_KEY$1.test(elementKey) && ((_vrMap$key = vrMap[key]) === null || _vrMap$key === void 0 ? void 0 : _vrMap$key[elementKey]) === "SQ") {
14837
14854
  sequences.push(value[elementKey]);
14838
14855
  }
14839
14856
  }
@@ -14867,6 +14884,167 @@
14867
14884
  }
14868
14885
  }
14869
14886
 
14887
+ /**
14888
+ * Private blocks of a DICOM JSON dict (keys 'GGGGEEEE'), per PS3.5 7.8.1.
14889
+ *
14890
+ * The naturalized form groups a block under its block key (privateBlocks.js);
14891
+ * these functions give the same names at the dict level, for a caller that
14892
+ * builds or reads a dict directly (DicomDict, DicomMessage.readFile):
14893
+ *
14894
+ * dicomDict.privateBlock("4D43:ACME 1.0").set("x01", "CS", ["HYBRID"]);
14895
+ * findPrivateBlock(dict, "4D43:ACME 1.0")?.get("x01").Value;
14896
+ *
14897
+ * A block is named by its block key or by (group, creator); an element by
14898
+ * its number 0x00-0xFF or its element key "xEE". The creator is the
14899
+ * identity: the block number (`reservedBlock`) is looked up, or reserved at
14900
+ * the first free xx, never assumed. docs/specs/naturalized-dataset.md
14901
+ * NAT-U-PRIV-11 is the specification.
14902
+ */
14903
+
14904
+ var CREATOR_MAX_LENGTH = 64; // LO
14905
+ var BLOCK_KEY = /^([0-9A-Fa-f]{4}):([^]+)$/;
14906
+ var ELEMENT_KEY = /^x([0-9A-Fa-f]{2})$/;
14907
+ var hex2 = function hex2(n) {
14908
+ return n.toString(16).toUpperCase().padStart(2, "0");
14909
+ };
14910
+ var hex4 = function hex4(n) {
14911
+ return n.toString(16).toUpperCase().padStart(4, "0");
14912
+ };
14913
+
14914
+ /** [group, creator] from (groupOrKey, creator), validated. */
14915
+ function blockArgs(groupOrKey, creator) {
14916
+ var group = groupOrKey;
14917
+ if (typeof groupOrKey === "string") {
14918
+ var match = BLOCK_KEY.exec(groupOrKey);
14919
+ if (!match) {
14920
+ throw new Error("Private block key must be \"gggg:creator\", got ".concat(groupOrKey));
14921
+ }
14922
+ group = parseInt(match[1], 16);
14923
+ creator = match[2].replace(/\\r/g, "\r").replace(/\\n/g, "\n");
14924
+ }
14925
+ if (!Number.isInteger(group) || group < 0 || group > 0xffff) {
14926
+ throw new Error("Private group must be a 16 bit number, got ".concat(group));
14927
+ }
14928
+ if (group % 2 === 0) {
14929
+ throw new Error("Private group must be odd, got ".concat(hex4(group), " - an even group is standard, not private"));
14930
+ }
14931
+ if (typeof creator !== "string" || creatorText(creator).length === 0) {
14932
+ throw new Error("Private creator must be a non-empty string");
14933
+ }
14934
+ creator = creatorText(creator);
14935
+ if (creator.length > CREATOR_MAX_LENGTH) {
14936
+ throw new Error("Private creator must be at most ".concat(CREATOR_MAX_LENGTH, " characters (LO), got ").concat(creator.length));
14937
+ }
14938
+ return [group, creator];
14939
+ }
14940
+
14941
+ /** The element number of 0x00-0xFF or "xEE". */
14942
+ function elementNumber(element) {
14943
+ var match = typeof element === "string" && ELEMENT_KEY.exec(element);
14944
+ var n = match ? parseInt(match[1], 16) : element;
14945
+ if (!Number.isInteger(n) || n < 0x00 || n > 0xff) {
14946
+ throw new Error("Private element must be 0x00 to 0xFF or \"xEE\", got ".concat(element));
14947
+ }
14948
+ return n;
14949
+ }
14950
+ function blockAccessor(dict, group, creator, reservedBlock) {
14951
+ var tag = function tag(element) {
14952
+ return hex4(group) + hex2(reservedBlock) + hex2(elementNumber(element));
14953
+ };
14954
+ return {
14955
+ key: privateBlockKey(group, creator),
14956
+ group: group,
14957
+ creator: creator,
14958
+ reservedBlock: reservedBlock,
14959
+ /** The 'GGGGEEEE' key of an element of this block. */
14960
+ tag: tag,
14961
+ /** Whether this block holds that element. */
14962
+ has: function has(element) {
14963
+ return Object.prototype.hasOwnProperty.call(dict, tag(element));
14964
+ },
14965
+ /** The element object, or undefined. */
14966
+ get: function get(element) {
14967
+ return dict[tag(element)];
14968
+ },
14969
+ /**
14970
+ * Writes an element of this block, as DicomDict#upsertTag does.
14971
+ *
14972
+ * @param {number|string} element 0x00 to 0xFF, or "xEE"
14973
+ * @param {string} vr value representation
14974
+ * @param {*} values value, or array of values
14975
+ * @returns {string} the tag that was written
14976
+ */
14977
+ set: function set(element, vr, values) {
14978
+ var key = tag(element);
14979
+ dict[key] = {
14980
+ vr: vr,
14981
+ Value: ValueRepresentation$1.addValueAccessorsFor(vr, Array.isArray(values) ? values : [values])
14982
+ };
14983
+ return key;
14984
+ }
14985
+ };
14986
+ }
14987
+
14988
+ /**
14989
+ * Finds the block a creator has reserved in a dict.
14990
+ *
14991
+ * @param {Object} dict DICOM JSON dict, keyed by 'GGGGEEEE'
14992
+ * @param {number|string} groupOrKey odd group number, or block key "gggg:creator"
14993
+ * @param {string} [creator] creator string, with a group number
14994
+ * @returns {Object|null} a block accessor, or null when the creator is absent
14995
+ */
14996
+ function findPrivateBlock(dict, groupOrKey, creator) {
14997
+ var _blockArgs = blockArgs(groupOrKey, creator),
14998
+ _blockArgs2 = _slicedToArray(_blockArgs, 2),
14999
+ group = _blockArgs2[0],
15000
+ wanted = _blockArgs2[1];
15001
+ for (var xx = 0x10; xx <= 0xff; xx++) {
15002
+ var element = dict[hex4(group) + hex4(xx)];
15003
+ if (element && creatorText(element.Value) === wanted) {
15004
+ return blockAccessor(dict, group, wanted, xx);
15005
+ }
15006
+ }
15007
+ return null;
15008
+ }
15009
+
15010
+ /**
15011
+ * Finds the block a creator has reserved, or reserves the first free block
15012
+ * and writes the creator element. A block is free when the dict holds no
15013
+ * creator element for it and no element in its range.
15014
+ *
15015
+ * @param {Object} dict DICOM JSON dict, keyed by 'GGGGEEEE'
15016
+ * @param {number|string} groupOrKey odd group number, or block key "gggg:creator"
15017
+ * @param {string} [creator] creator string, with a group number
15018
+ * @returns {Object} a block accessor
15019
+ */
15020
+ function reservePrivateBlock(dict, groupOrKey, creator) {
15021
+ var existing = findPrivateBlock(dict, groupOrKey, creator);
15022
+ if (existing) return existing;
15023
+ var _blockArgs3 = blockArgs(groupOrKey, creator),
15024
+ _blockArgs4 = _slicedToArray(_blockArgs3, 2),
15025
+ group = _blockArgs4[0],
15026
+ wanted = _blockArgs4[1];
15027
+ var prefix = hex4(group);
15028
+ var used = new Set();
15029
+ for (var _i = 0, _Object$keys = Object.keys(dict); _i < _Object$keys.length; _i++) {
15030
+ var key = _Object$keys[_i];
15031
+ if (key.length !== 8 || key.slice(0, 4).toUpperCase() !== prefix) {
15032
+ continue;
15033
+ }
15034
+ var element = parseInt(key.slice(4), 16);
15035
+ used.add(element <= 0xff ? element : element >> 8);
15036
+ }
15037
+ var xx = firstFreeBlock(used);
15038
+ if (xx === undefined) {
15039
+ throw new Error("No free private block in group ".concat(prefix, " - all of 0x10 to 0xFF are reserved"));
15040
+ }
15041
+ dict[prefix + hex4(xx)] = {
15042
+ vr: "LO",
15043
+ Value: [wanted]
15044
+ };
15045
+ return blockAccessor(dict, group, wanted, xx);
15046
+ }
15047
+
14870
15048
  /**
14871
15049
  * Creates an information filter that tracks top-level DICOM attributes.
14872
15050
  *
@@ -15288,6 +15466,34 @@
15288
15466
  this.dict[tag].Value = ValueRepresentation$1.addValueAccessorsFor(this.dict[tag].vr, values);
15289
15467
  }
15290
15468
 
15469
+ /**
15470
+ * Reserves a private block for a creator, and writes its creator element
15471
+ * when the creator has no block yet; pass `{ create: false }` to only look
15472
+ * it up. The block is found by creator, never assumed (NAT-U-PRIV-11).
15473
+ *
15474
+ * dicomDict.privateBlock("4D43:ACME 1.0").set("x01", "CS", ["HYBRID"]);
15475
+ *
15476
+ * @param {number|string} groupOrKey odd group number, or block key "gggg:creator"
15477
+ * @param {string} [creator] creator string, with a group number
15478
+ * @param {{create?: boolean}} [options] create defaults to true; false
15479
+ * returns null when the creator is absent
15480
+ * @returns {Object|null} accessor with key, group, creator, reservedBlock,
15481
+ * tag, has, get and set
15482
+ */
15483
+ }, {
15484
+ key: "privateBlock",
15485
+ value: function privateBlock(groupOrKey, creator) {
15486
+ var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
15487
+ if (typeof groupOrKey === "string" && _typeof(creator) === "object") {
15488
+ options = creator;
15489
+ creator = undefined;
15490
+ }
15491
+ var _ref = options || {},
15492
+ _ref$create = _ref.create,
15493
+ create = _ref$create === void 0 ? true : _ref$create;
15494
+ return create ? reservePrivateBlock(this.dict, groupOrKey, creator) : findPrivateBlock(this.dict, groupOrKey, creator);
15495
+ }
15496
+
15291
15497
  // The Part 10 envelope (preamble/DICM, computed meta group length,
15292
15498
  // deflate branch, default transfer syntax) moved verbatim to
15293
15499
  // @dcmjs-org/core as writePart10 (core/writeCore.js); this delegation
@@ -16449,7 +16655,7 @@
16449
16655
  break;
16450
16656
  }
16451
16657
  // Group length
16452
- stream.increment(tagObj.length);
16658
+ stream.increment(length);
16453
16659
  return _context5.abrupt("continue", 1);
16454
16660
  case 5:
16455
16661
  addTagResult = listener.addTag(tag, tagInfo);
@@ -30314,7 +30520,11 @@
30314
30520
  setAttribute: setAttribute,
30315
30521
  setPersonName: setPersonName,
30316
30522
  definePrivateAliases: definePrivateAliases,
30317
- frameDataset: frameDataset
30523
+ frameDataset: frameDataset,
30524
+ // private blocks of a DICOM JSON dict (NAT-U-PRIV-11)
30525
+ findPrivateBlock: findPrivateBlock,
30526
+ reservePrivateBlock: reservePrivateBlock,
30527
+ privateBlockKey: privateBlockKey
30318
30528
  };
30319
30529
  var async = {
30320
30530
  AsyncDicomReader: AsyncDicomReader