@dcmjs-org/dcmjs 1.0.0-beta.12 → 1.0.0-beta.14

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-CWXtbj14.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-BxKHutFS.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
@@ -11027,7 +11027,8 @@
11027
11027
  // Registered by core/writeCore.js at load; the eager engine's
11028
11028
  // DicomMessage.write is a delegate to the same function, so
11029
11029
  // this is the one item-serialization path either way.
11030
- written += writeDataSet$1(item, stream, syntax, writeOptions);
11030
+ // A null item (NAT-U-RT-7) is an empty item
11031
+ written += writeDataSet$1(item || {}, stream, syntax, writeOptions);
11031
11032
  _get(_getPrototypeOf(SequenceOfItems.prototype), "write", this).call(this, stream, "Uint16", 0xfffe);
11032
11033
  _get(_getPrototypeOf(SequenceOfItems.prototype), "write", this).call(this, stream, "Uint16", 0xe00d);
11033
11034
  _get(_getPrototypeOf(SequenceOfItems.prototype), "write", this).call(this, stream, "Uint32", 0x00000000);
@@ -11556,6 +11557,17 @@
11556
11557
  value: function write(stream, vrType, values, syntax, writeOptions) {
11557
11558
  var vr = ValueRepresentation$1.createByTypeString(vrType);
11558
11559
  var useSyntax = normalizeSyntax(syntax);
11560
+ // No value (NAT-U-RT-7): absent, null, "" or only undefined entries is
11561
+ // an empty list; an undefined entry beside a value is a null entry
11562
+ if (values === undefined || values === null || values === "") {
11563
+ values = [];
11564
+ } else if (Array.isArray(values) && values.includes(undefined)) {
11565
+ values = values.every(function (v) {
11566
+ return v === undefined;
11567
+ }) ? [] : values.map(function (v) {
11568
+ return v === undefined ? null : v;
11569
+ });
11570
+ }
11559
11571
  var implicit = useSyntax === IMPLICIT_LITTLE_ENDIAN;
11560
11572
  var isLittleEndian = useSyntax === IMPLICIT_LITTLE_ENDIAN || useSyntax === EXPLICIT_LITTLE_ENDIAN;
11561
11573
  var isEncapsulated = this.isPixelDataTag() && isEncapsulatedSyntax(syntax);
@@ -12880,10 +12892,10 @@
12880
12892
  * the specification.
12881
12893
  */
12882
12894
 
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})$/;
12895
+ // `s`: a key that a user writes may hold a line break; the producers write
12896
+ // it as the text `\n` (privateBlockKey).
12897
+ var BLOCK_KEY$1 = /^([0-9A-Fa-f]{4}):([^]+)$/;
12898
+ var ELEMENT_KEY$2 = /^x([0-9A-Fa-f]{2})$/;
12887
12899
  var HEX_TAG = /^[0-9A-Fa-f]{8}$/;
12888
12900
 
12889
12901
  /** True for an 8-digit hex tag key, for example "00291001". */
@@ -12896,7 +12908,7 @@
12896
12908
  // LO holds at most 64 characters.
12897
12909
  var CREATOR_MAX_BYTES = 64;
12898
12910
  var latin1 = createLatin1Decoder();
12899
- function hex2(n) {
12911
+ function hex2$1(n) {
12900
12912
  return n.toString(16).toUpperCase().padStart(2, "0");
12901
12913
  }
12902
12914
  function tagOf(hexTag) {
@@ -12912,9 +12924,26 @@
12912
12924
  return !!tag && (tag.isPrivateCreator() || tag.isPrivateValue());
12913
12925
  }
12914
12926
 
12927
+ /**
12928
+ * The block key `gggg:creator` of a group (a number, or 4 hex digits) and a
12929
+ * creator. A line break in the creator is written as the text `\n` (and
12930
+ * `\r`); LO holds no backslash, so the key reads back to the same creator
12931
+ * (NAT-U-PRIV-1).
12932
+ */
12933
+ function privateBlockKey(group, creator) {
12934
+ var gggg = typeof group === "number" ? group.toString(16).toUpperCase().padStart(4, "0") : group.toUpperCase();
12935
+ var text = creator.replace(/\r/g, "\\r").replace(/\n/g, "\\n");
12936
+ return "".concat(gggg, ":").concat(text);
12937
+ }
12938
+
12939
+ /** The creator of the creator part of a block key: `\n` and `\r` unescaped. */
12940
+ function keyCreator(text) {
12941
+ return text.replace(/\\r/g, "\r").replace(/\\n/g, "\n");
12942
+ }
12943
+
12915
12944
  /** The group and creator of a reserved block key, or undefined. */
12916
12945
  function parsePrivateBlockKey(key, value) {
12917
- var match = BLOCK_KEY.exec(key);
12946
+ var match = BLOCK_KEY$1.exec(key);
12918
12947
  if (!match || !value || _typeof(value) !== "object" || Array.isArray(value)) {
12919
12948
  return undefined;
12920
12949
  }
@@ -12922,7 +12951,7 @@
12922
12951
  if ((parseInt(group, 16) & 1) !== 1) return undefined;
12923
12952
  return {
12924
12953
  group: group,
12925
- creator: match[2]
12954
+ creator: keyCreator(match[2])
12926
12955
  };
12927
12956
  }
12928
12957
  function privateDictionaryEntry(group, creator, ee) {
@@ -13035,11 +13064,11 @@
13035
13064
  if (!parsed || typeof block.reservedBlock !== "number") return 1; // continue
13036
13065
  var group = parsed.group,
13037
13066
  creator = parsed.creator;
13038
- var xx = hex2(block.reservedBlock);
13067
+ var xx = hex2$1(block.reservedBlock);
13039
13068
  onDataset("".concat(group, "00").concat(xx), key, null);
13040
13069
  var _loop2 = function _loop2() {
13041
13070
  var elementKey = _Object$keys3[_i3];
13042
- var match = ELEMENT_KEY$1.exec(elementKey);
13071
+ var match = ELEMENT_KEY$2.exec(elementKey);
13043
13072
  if (!match) return 1; // continue
13044
13073
  var ee = match[1].toUpperCase();
13045
13074
  var tag = "".concat(group).concat(xx).concat(ee);
@@ -13096,7 +13125,7 @@
13096
13125
  var creator = creatorText(value);
13097
13126
  if (creator === "") continue;
13098
13127
  var hexTag = tag.toUpperCase();
13099
- var key = "".concat(hexTag.slice(0, 4), ":").concat(creator);
13128
+ var key = privateBlockKey(hexTag.slice(0, 4), creator);
13100
13129
  if (blockOfCreator.has(key)) {
13101
13130
  // PS3.5 7.8.1 permits one block per creator in a group
13102
13131
  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 +13167,7 @@
13138
13167
  recordVr(vrMap, name, entry, vr);
13139
13168
  continue;
13140
13169
  }
13141
- var _key = "".concat(group, ":").concat(_creator);
13170
+ var _key = privateBlockKey(group, _creator);
13142
13171
  var block = blockFor(target, group, _creator, _tag.slice(4, 6));
13143
13172
  var elementKey = "x".concat(_tag.slice(6, 8));
13144
13173
  block[elementKey] = _value;
@@ -13178,7 +13207,7 @@
13178
13207
  return text.replace(/^ +|[\0 ]+$/g, "");
13179
13208
  }
13180
13209
  function blockFor(target, group, creator, xx) {
13181
- var key = "".concat(group, ":").concat(creator);
13210
+ var key = privateBlockKey(group, creator);
13182
13211
  if (!target[key]) {
13183
13212
  target[key] = {
13184
13213
  creator: creator,
@@ -13394,7 +13423,7 @@
13394
13423
  if (b.block.creator !== undefined && b.block.creator !== b.creator) {
13395
13424
  log.warn("Private block ".concat(key, " has creator \"").concat(b.block.creator, "\"; the key creator \"").concat(b.creator, "\" is used"));
13396
13425
  }
13397
- var creatorTag = "".concat(b.group, "00").concat(hex2(xx));
13426
+ var creatorTag = "".concat(b.group, "00").concat(hex2$1(xx));
13398
13427
  view[creatorTag] = b.creator;
13399
13428
  vrMap[creatorTag] = "LO";
13400
13429
  var blockVrs = sourceVrMap[key] || {};
@@ -13405,7 +13434,7 @@
13405
13434
  log.warn("No VR for ".concat(name, " in private block"), key);
13406
13435
  return;
13407
13436
  }
13408
- var tag = "".concat(b.group).concat(hex2(xx)).concat(ee);
13437
+ var tag = "".concat(b.group).concat(hex2$1(xx)).concat(ee);
13409
13438
  view[tag] = value;
13410
13439
  vrMap[tag] = vr;
13411
13440
  };
@@ -13414,7 +13443,7 @@
13414
13443
  if (elementKey === "creator" || elementKey === "reservedBlock") {
13415
13444
  continue;
13416
13445
  }
13417
- var match = ELEMENT_KEY$1.exec(elementKey);
13446
+ var match = ELEMENT_KEY$2.exec(elementKey);
13418
13447
  if (!match) {
13419
13448
  log.warn("Unknown name in private block ".concat(key, ":"), elementKey);
13420
13449
  continue;
@@ -14705,7 +14734,7 @@
14705
14734
  fileStream.writeRawBytes(pako.deflateRaw(new Uint8Array(bodyStream.getBuffer())));
14706
14735
  }
14707
14736
 
14708
- var ELEMENT_KEY = /^x[0-9A-Fa-f]{2}$/;
14737
+ var ELEMENT_KEY$1 = /^x[0-9A-Fa-f]{2}$/;
14709
14738
 
14710
14739
  /**
14711
14740
  * The view of one frame of a multi-frame naturalized dataset (PS3.3
@@ -14833,7 +14862,7 @@
14833
14862
  for (var _i3 = 0, _Object$keys3 = Object.keys(value); _i3 < _Object$keys3.length; _i3++) {
14834
14863
  var _vrMap$key;
14835
14864
  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") {
14865
+ if (ELEMENT_KEY$1.test(elementKey) && ((_vrMap$key = vrMap[key]) === null || _vrMap$key === void 0 ? void 0 : _vrMap$key[elementKey]) === "SQ") {
14837
14866
  sequences.push(value[elementKey]);
14838
14867
  }
14839
14868
  }
@@ -14867,6 +14896,167 @@
14867
14896
  }
14868
14897
  }
14869
14898
 
14899
+ /**
14900
+ * Private blocks of a DICOM JSON dict (keys 'GGGGEEEE'), per PS3.5 7.8.1.
14901
+ *
14902
+ * The naturalized form groups a block under its block key (privateBlocks.js);
14903
+ * these functions give the same names at the dict level, for a caller that
14904
+ * builds or reads a dict directly (DicomDict, DicomMessage.readFile):
14905
+ *
14906
+ * dicomDict.privateBlock("4D43:ACME 1.0").set("x01", "CS", ["HYBRID"]);
14907
+ * findPrivateBlock(dict, "4D43:ACME 1.0")?.get("x01").Value;
14908
+ *
14909
+ * A block is named by its block key or by (group, creator); an element by
14910
+ * its number 0x00-0xFF or its element key "xEE". The creator is the
14911
+ * identity: the block number (`reservedBlock`) is looked up, or reserved at
14912
+ * the first free xx, never assumed. docs/specs/naturalized-dataset.md
14913
+ * NAT-U-PRIV-11 is the specification.
14914
+ */
14915
+
14916
+ var CREATOR_MAX_LENGTH = 64; // LO
14917
+ var BLOCK_KEY = /^([0-9A-Fa-f]{4}):([^]+)$/;
14918
+ var ELEMENT_KEY = /^x([0-9A-Fa-f]{2})$/;
14919
+ var hex2 = function hex2(n) {
14920
+ return n.toString(16).toUpperCase().padStart(2, "0");
14921
+ };
14922
+ var hex4 = function hex4(n) {
14923
+ return n.toString(16).toUpperCase().padStart(4, "0");
14924
+ };
14925
+
14926
+ /** [group, creator] from (groupOrKey, creator), validated. */
14927
+ function blockArgs(groupOrKey, creator) {
14928
+ var group = groupOrKey;
14929
+ if (typeof groupOrKey === "string") {
14930
+ var match = BLOCK_KEY.exec(groupOrKey);
14931
+ if (!match) {
14932
+ throw new Error("Private block key must be \"gggg:creator\", got ".concat(groupOrKey));
14933
+ }
14934
+ group = parseInt(match[1], 16);
14935
+ creator = match[2].replace(/\\r/g, "\r").replace(/\\n/g, "\n");
14936
+ }
14937
+ if (!Number.isInteger(group) || group < 0 || group > 0xffff) {
14938
+ throw new Error("Private group must be a 16 bit number, got ".concat(group));
14939
+ }
14940
+ if (group % 2 === 0) {
14941
+ throw new Error("Private group must be odd, got ".concat(hex4(group), " - an even group is standard, not private"));
14942
+ }
14943
+ if (typeof creator !== "string" || creatorText(creator).length === 0) {
14944
+ throw new Error("Private creator must be a non-empty string");
14945
+ }
14946
+ creator = creatorText(creator);
14947
+ if (creator.length > CREATOR_MAX_LENGTH) {
14948
+ throw new Error("Private creator must be at most ".concat(CREATOR_MAX_LENGTH, " characters (LO), got ").concat(creator.length));
14949
+ }
14950
+ return [group, creator];
14951
+ }
14952
+
14953
+ /** The element number of 0x00-0xFF or "xEE". */
14954
+ function elementNumber(element) {
14955
+ var match = typeof element === "string" && ELEMENT_KEY.exec(element);
14956
+ var n = match ? parseInt(match[1], 16) : element;
14957
+ if (!Number.isInteger(n) || n < 0x00 || n > 0xff) {
14958
+ throw new Error("Private element must be 0x00 to 0xFF or \"xEE\", got ".concat(element));
14959
+ }
14960
+ return n;
14961
+ }
14962
+ function blockAccessor(dict, group, creator, reservedBlock) {
14963
+ var tag = function tag(element) {
14964
+ return hex4(group) + hex2(reservedBlock) + hex2(elementNumber(element));
14965
+ };
14966
+ return {
14967
+ key: privateBlockKey(group, creator),
14968
+ group: group,
14969
+ creator: creator,
14970
+ reservedBlock: reservedBlock,
14971
+ /** The 'GGGGEEEE' key of an element of this block. */
14972
+ tag: tag,
14973
+ /** Whether this block holds that element. */
14974
+ has: function has(element) {
14975
+ return Object.prototype.hasOwnProperty.call(dict, tag(element));
14976
+ },
14977
+ /** The element object, or undefined. */
14978
+ get: function get(element) {
14979
+ return dict[tag(element)];
14980
+ },
14981
+ /**
14982
+ * Writes an element of this block, as DicomDict#upsertTag does.
14983
+ *
14984
+ * @param {number|string} element 0x00 to 0xFF, or "xEE"
14985
+ * @param {string} vr value representation
14986
+ * @param {*} values value, or array of values
14987
+ * @returns {string} the tag that was written
14988
+ */
14989
+ set: function set(element, vr, values) {
14990
+ var key = tag(element);
14991
+ dict[key] = {
14992
+ vr: vr,
14993
+ Value: ValueRepresentation$1.addValueAccessorsFor(vr, Array.isArray(values) ? values : [values])
14994
+ };
14995
+ return key;
14996
+ }
14997
+ };
14998
+ }
14999
+
15000
+ /**
15001
+ * Finds the block a creator has reserved in a dict.
15002
+ *
15003
+ * @param {Object} dict DICOM JSON dict, keyed by 'GGGGEEEE'
15004
+ * @param {number|string} groupOrKey odd group number, or block key "gggg:creator"
15005
+ * @param {string} [creator] creator string, with a group number
15006
+ * @returns {Object|null} a block accessor, or null when the creator is absent
15007
+ */
15008
+ function findPrivateBlock(dict, groupOrKey, creator) {
15009
+ var _blockArgs = blockArgs(groupOrKey, creator),
15010
+ _blockArgs2 = _slicedToArray(_blockArgs, 2),
15011
+ group = _blockArgs2[0],
15012
+ wanted = _blockArgs2[1];
15013
+ for (var xx = 0x10; xx <= 0xff; xx++) {
15014
+ var element = dict[hex4(group) + hex4(xx)];
15015
+ if (element && creatorText(element.Value) === wanted) {
15016
+ return blockAccessor(dict, group, wanted, xx);
15017
+ }
15018
+ }
15019
+ return null;
15020
+ }
15021
+
15022
+ /**
15023
+ * Finds the block a creator has reserved, or reserves the first free block
15024
+ * and writes the creator element. A block is free when the dict holds no
15025
+ * creator element for it and no element in its range.
15026
+ *
15027
+ * @param {Object} dict DICOM JSON dict, keyed by 'GGGGEEEE'
15028
+ * @param {number|string} groupOrKey odd group number, or block key "gggg:creator"
15029
+ * @param {string} [creator] creator string, with a group number
15030
+ * @returns {Object} a block accessor
15031
+ */
15032
+ function reservePrivateBlock(dict, groupOrKey, creator) {
15033
+ var existing = findPrivateBlock(dict, groupOrKey, creator);
15034
+ if (existing) return existing;
15035
+ var _blockArgs3 = blockArgs(groupOrKey, creator),
15036
+ _blockArgs4 = _slicedToArray(_blockArgs3, 2),
15037
+ group = _blockArgs4[0],
15038
+ wanted = _blockArgs4[1];
15039
+ var prefix = hex4(group);
15040
+ var used = new Set();
15041
+ for (var _i = 0, _Object$keys = Object.keys(dict); _i < _Object$keys.length; _i++) {
15042
+ var key = _Object$keys[_i];
15043
+ if (key.length !== 8 || key.slice(0, 4).toUpperCase() !== prefix) {
15044
+ continue;
15045
+ }
15046
+ var element = parseInt(key.slice(4), 16);
15047
+ used.add(element <= 0xff ? element : element >> 8);
15048
+ }
15049
+ var xx = firstFreeBlock(used);
15050
+ if (xx === undefined) {
15051
+ throw new Error("No free private block in group ".concat(prefix, " - all of 0x10 to 0xFF are reserved"));
15052
+ }
15053
+ dict[prefix + hex4(xx)] = {
15054
+ vr: "LO",
15055
+ Value: [wanted]
15056
+ };
15057
+ return blockAccessor(dict, group, wanted, xx);
15058
+ }
15059
+
14870
15060
  /**
14871
15061
  * Creates an information filter that tracks top-level DICOM attributes.
14872
15062
  *
@@ -15288,6 +15478,34 @@
15288
15478
  this.dict[tag].Value = ValueRepresentation$1.addValueAccessorsFor(this.dict[tag].vr, values);
15289
15479
  }
15290
15480
 
15481
+ /**
15482
+ * Reserves a private block for a creator, and writes its creator element
15483
+ * when the creator has no block yet; pass `{ create: false }` to only look
15484
+ * it up. The block is found by creator, never assumed (NAT-U-PRIV-11).
15485
+ *
15486
+ * dicomDict.privateBlock("4D43:ACME 1.0").set("x01", "CS", ["HYBRID"]);
15487
+ *
15488
+ * @param {number|string} groupOrKey odd group number, or block key "gggg:creator"
15489
+ * @param {string} [creator] creator string, with a group number
15490
+ * @param {{create?: boolean}} [options] create defaults to true; false
15491
+ * returns null when the creator is absent
15492
+ * @returns {Object|null} accessor with key, group, creator, reservedBlock,
15493
+ * tag, has, get and set
15494
+ */
15495
+ }, {
15496
+ key: "privateBlock",
15497
+ value: function privateBlock(groupOrKey, creator) {
15498
+ var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
15499
+ if (typeof groupOrKey === "string" && _typeof(creator) === "object") {
15500
+ options = creator;
15501
+ creator = undefined;
15502
+ }
15503
+ var _ref = options || {},
15504
+ _ref$create = _ref.create,
15505
+ create = _ref$create === void 0 ? true : _ref$create;
15506
+ return create ? reservePrivateBlock(this.dict, groupOrKey, creator) : findPrivateBlock(this.dict, groupOrKey, creator);
15507
+ }
15508
+
15291
15509
  // The Part 10 envelope (preamble/DICM, computed meta group length,
15292
15510
  // deflate branch, default transfer syntax) moved verbatim to
15293
15511
  // @dcmjs-org/core as writePart10 (core/writeCore.js); this delegation
@@ -30314,7 +30532,11 @@
30314
30532
  setAttribute: setAttribute,
30315
30533
  setPersonName: setPersonName,
30316
30534
  definePrivateAliases: definePrivateAliases,
30317
- frameDataset: frameDataset
30535
+ frameDataset: frameDataset,
30536
+ // private blocks of a DICOM JSON dict (NAT-U-PRIV-11)
30537
+ findPrivateBlock: findPrivateBlock,
30538
+ reservePrivateBlock: reservePrivateBlock,
30539
+ privateBlockKey: privateBlockKey
30318
30540
  };
30319
30541
  var async = {
30320
30542
  AsyncDicomReader: AsyncDicomReader