@dcmjs-org/dcmjs 1.0.0-beta.20 → 1.0.0-beta.22

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.
@@ -1610,7 +1610,7 @@ var AsyncDicomReader = /*#__PURE__*/function () {
1610
1610
  this.isLittleEndian = options === null || options === void 0 ? void 0 : options.isLittleEndian;
1611
1611
  // Number of sequence items being read, 0 at the top level
1612
1612
  this.sequenceDepth = 0;
1613
- // Default maxFragmentSize is 128 MB (128 * 1024 * 1024 bytes)
1613
+ // Default maxFragmentSize is 128 MB; a Big Endian fragment is at least one word, so it can be larger
1614
1614
  this.maxFragmentSize = (_options$maxFragmentS = options === null || options === void 0 ? void 0 : options.maxFragmentSize) !== null && _options$maxFragmentS !== void 0 ? _options$maxFragmentS : 128 * 1024 * 1024;
1615
1615
  this.stream = new core.ReadBufferStream(null, _objectSpread2(_objectSpread2({
1616
1616
  clearBuffers: true
@@ -2189,16 +2189,28 @@ var AsyncDicomReader = /*#__PURE__*/function () {
2189
2189
  /**
2190
2190
  * Emits one or more listener.value() calls for a fragment, splitting if needed.
2191
2191
  * This does NOT start/pop any array contexts; callers control the surrounding structure.
2192
+ * In Explicit VR Big Endian, each fragment holds whole words of wordWidth
2193
+ * bytes, swapped to little endian.
2192
2194
  */
2193
2195
  }, {
2194
2196
  key: "_emitSplitValues",
2195
2197
  value: (function () {
2196
2198
  var _emitSplitValues2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(length) {
2197
2199
  var _listener$awaitDrain;
2198
- var stream, listener, maxFragmentSize, offset, chunkSize, buffer;
2200
+ var wordWidth,
2201
+ stream,
2202
+ listener,
2203
+ maxFragmentSize,
2204
+ swap,
2205
+ fragmentSize,
2206
+ offset,
2207
+ chunkSize,
2208
+ buffer,
2209
+ _args7 = arguments;
2199
2210
  return _regeneratorRuntime().wrap(function (_context7) {
2200
2211
  while (1) switch (_context7.prev = _context7.next) {
2201
2212
  case 0:
2213
+ wordWidth = _args7.length > 1 && _args7[1] !== undefined ? _args7[1] : 1;
2202
2214
  stream = this.stream, listener = this.listener;
2203
2215
  _context7.next = 1;
2204
2216
  return (_listener$awaitDrain = listener.awaitDrain) === null || _listener$awaitDrain === void 0 ? void 0 : _listener$awaitDrain.call(listener);
@@ -2210,6 +2222,8 @@ var AsyncDicomReader = /*#__PURE__*/function () {
2210
2222
  }
2211
2223
  throw new Error("maxFragmentSize must be a positive number, got ".concat(maxFragmentSize));
2212
2224
  case 2:
2225
+ swap = this.syntax === core.EXPLICIT_BIG_ENDIAN && wordWidth > 1;
2226
+ fragmentSize = swap ? Math.max(wordWidth, maxFragmentSize - maxFragmentSize % wordWidth) : maxFragmentSize;
2213
2227
  offset = 0;
2214
2228
  case 3:
2215
2229
  if (!(offset < length)) {
@@ -2217,11 +2231,14 @@ var AsyncDicomReader = /*#__PURE__*/function () {
2217
2231
  break;
2218
2232
  }
2219
2233
  readLog.trace("_emitSplitValues loop", offset, length);
2220
- chunkSize = Math.min(maxFragmentSize, length - offset);
2234
+ chunkSize = Math.min(fragmentSize, length - offset);
2221
2235
  _context7.next = 4;
2222
2236
  return stream.ensureAvailable(chunkSize);
2223
2237
  case 4:
2224
2238
  buffer = stream.readArrayBuffer(chunkSize);
2239
+ if (swap) {
2240
+ core.swapBytes(buffer, wordWidth);
2241
+ }
2225
2242
  listener.value(buffer);
2226
2243
  offset += chunkSize;
2227
2244
  stream.consume();
@@ -2371,12 +2388,13 @@ var AsyncDicomReader = /*#__PURE__*/function () {
2371
2388
  key: "readUncompressed",
2372
2389
  value: (function () {
2373
2390
  var _readUncompressed = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee0(tagInfo) {
2374
- var length, listener, numberOfFrames, frameLength, _listener$information, _listener$information2, _listener$information3, _listener$information4, rows, cols, samplesPerPixel, bitsAllocated, bitsPerFrame, frameNumber;
2391
+ var length, listener, wordWidth, numberOfFrames, frameLength, _listener$information, _listener$information2, _listener$information3, _listener$information4, rows, cols, samplesPerPixel, bitsAllocated, bitsPerFrame, allPixelData, offset, frameNumber;
2375
2392
  return _regeneratorRuntime().wrap(function (_context0) {
2376
2393
  while (1) switch (_context0.prev = _context0.next) {
2377
2394
  case 0:
2378
2395
  length = tagInfo.length;
2379
2396
  listener = this.listener;
2397
+ wordWidth = this.wordWidth(tagInfo);
2380
2398
  numberOfFrames = this.getNumberOfFrames();
2381
2399
  frameLength = Math.floor(length / numberOfFrames);
2382
2400
  if (!(numberOfFrames > 1)) {
@@ -2409,24 +2427,41 @@ var AsyncDicomReader = /*#__PURE__*/function () {
2409
2427
  }
2410
2428
  throw new Error("Invalid frame length: ".concat(length, " for ").concat(numberOfFrames, " frames"));
2411
2429
  case 4:
2412
- frameNumber = 0;
2430
+ if (!(this.syntax === core.EXPLICIT_BIG_ENDIAN && frameLength % wordWidth !== 0)) {
2431
+ _context0.next = 6;
2432
+ break;
2433
+ }
2434
+ _context0.next = 5;
2435
+ return this.stream.ensureAvailable(length);
2413
2436
  case 5:
2437
+ allPixelData = this.stream.readArrayBuffer(length);
2438
+ this.stream.consume();
2439
+ core.swapBytes(allPixelData, wordWidth);
2440
+ for (offset = 0; offset < length; offset += frameLength) {
2441
+ listener.startObject([]);
2442
+ listener.value(allPixelData.slice(offset, offset + frameLength));
2443
+ listener.pop();
2444
+ }
2445
+ return _context0.abrupt("return");
2446
+ case 6:
2447
+ frameNumber = 0;
2448
+ case 7:
2414
2449
  if (!(frameNumber < numberOfFrames)) {
2415
- _context0.next = 8;
2450
+ _context0.next = 10;
2416
2451
  break;
2417
2452
  }
2418
2453
  readLog.trace("readUncompressed loop", frameNumber, numberOfFrames);
2419
2454
  listener.startObject([]);
2420
- _context0.next = 6;
2421
- return this._emitSplitValues(frameLength);
2422
- case 6:
2455
+ _context0.next = 8;
2456
+ return this._emitSplitValues(frameLength, wordWidth);
2457
+ case 8:
2423
2458
  listener.pop();
2424
2459
  // console.log(stream.getBufferMemoryInfo());
2425
- case 7:
2460
+ case 9:
2426
2461
  frameNumber++;
2427
- _context0.next = 5;
2462
+ _context0.next = 7;
2428
2463
  break;
2429
- case 8:
2464
+ case 10:
2430
2465
  case "end":
2431
2466
  return _context0.stop();
2432
2467
  }
@@ -2494,6 +2529,10 @@ var AsyncDicomReader = /*#__PURE__*/function () {
2494
2529
  case 4:
2495
2530
  allPixelData = stream.readArrayBuffer(totalBytes);
2496
2531
  stream.consume();
2532
+ if (this.syntax === core.EXPLICIT_BIG_ENDIAN) {
2533
+ // the bits of OW data are packed in big endian words
2534
+ core.swapBytes(allPixelData, this.wordWidth(tagInfo));
2535
+ }
2497
2536
 
2498
2537
  // Extract each frame, unpacking bits so each frame starts at byte 0
2499
2538
  for (frameNumber = 0; frameNumber < numberOfFrames; frameNumber++) {
@@ -2535,7 +2574,8 @@ var AsyncDicomReader = /*#__PURE__*/function () {
2535
2574
  }()
2536
2575
  /**
2537
2576
  * Reads raw binary data in chunks and delivers it to the listener.
2538
- * This method reads data in 64KB chunks to manage memory efficiently.
2577
+ * Reads the value in fragments of `maxFragmentSize` bytes.
2578
+ * OW/OF/OL/OD/OV words come as little endian, as on the other read paths.
2539
2579
  */
2540
2580
  )
2541
2581
  }, {
@@ -2548,7 +2588,7 @@ var AsyncDicomReader = /*#__PURE__*/function () {
2548
2588
  case 0:
2549
2589
  length = tagInfo.length;
2550
2590
  _context10.next = 1;
2551
- return this._emitSplitValues(length);
2591
+ return this._emitSplitValues(length, this.wordWidth(tagInfo));
2552
2592
  case 1:
2553
2593
  case "end":
2554
2594
  return _context10.stop();
@@ -2559,7 +2599,13 @@ var AsyncDicomReader = /*#__PURE__*/function () {
2559
2599
  return _readRawBinary.apply(this, arguments);
2560
2600
  }
2561
2601
  return readRawBinary;
2562
- }())
2602
+ }() /** Bytes per word of the value field; "ox" (a UN pixel data) is OW. */)
2603
+ }, {
2604
+ key: "wordWidth",
2605
+ value: function wordWidth(tagInfo) {
2606
+ var _ValueRepresentation$;
2607
+ return (_ValueRepresentation$ = core.ValueRepresentation.createByTypeString(tagInfo.vr).wordWidth) !== null && _ValueRepresentation$ !== void 0 ? _ValueRepresentation$ : 1;
2608
+ }
2563
2609
  }, {
2564
2610
  key: "isSequence",
2565
2611
  value: function isSequence(tagInfo) {
@@ -3524,6 +3570,22 @@ var ImageNormalizer = /*#__PURE__*/function (_Normalizer) {
3524
3570
  DimensionDescriptionLabel: "ImagePositionPatient"
3525
3571
  }];
3526
3572
  }
3573
+
3574
+ /**
3575
+ * Fills in the functional groups of a multiframe dataset.
3576
+ *
3577
+ * A missing or empty SharedFunctionalGroupsSequence is built from the
3578
+ * top-level image plane attributes; a one-item array is used as its item.
3579
+ * The shared item gets the list accessors (addAccessors), so both
3580
+ * `SharedFunctionalGroupsSequence.X` and `SharedFunctionalGroupsSequence[0].X`
3581
+ * work.
3582
+ * The PerFrameFunctionalGroupsSequence is filled up to the declared
3583
+ * NumberOfFrames with items at the top-level position, existing items keep
3584
+ * their content, and each item without a FrameContentSequence gets one.
3585
+ * Added items share the position but get increasing InStackPositionNumber.
3586
+ * Items beyond NumberOfFrames are kept unchanged; a null item there is
3587
+ * skipped and does not throw.
3588
+ */
3527
3589
  }, {
3528
3590
  key: "normalizeMultiframe",
3529
3591
  value: function normalizeMultiframe() {
@@ -3547,6 +3609,10 @@ var ImageNormalizer = /*#__PURE__*/function (_Normalizer) {
3547
3609
  if (!ds.PresentationLUTShape) {
3548
3610
  ds.PresentationLUTShape = "IDENTITY";
3549
3611
  }
3612
+ if (Array.isArray(ds.SharedFunctionalGroupsSequence)) {
3613
+ // The shared group has one item; an empty array counts as missing
3614
+ ds.SharedFunctionalGroupsSequence = ds.SharedFunctionalGroupsSequence[0];
3615
+ }
3550
3616
  if (!ds.SharedFunctionalGroupsSequence) {
3551
3617
  core.log.warn("Missing SharedFunctionalGroupsSequence; using the top-level image plane attributes");
3552
3618
  ds.SharedFunctionalGroupsSequence = ImageNormalizer.sharedImagePlaneGroups(ds, {
@@ -3554,6 +3620,7 @@ var ImageNormalizer = /*#__PURE__*/function (_Normalizer) {
3554
3620
  SpacingBetweenSlices: ds.SpacingBetweenSlices
3555
3621
  });
3556
3622
  }
3623
+ ds.SharedFunctionalGroupsSequence = core.addAccessors(ds.SharedFunctionalGroupsSequence);
3557
3624
 
3558
3625
  // TODO: special case!
3559
3626
  if (ds.BodyPartExamined === "PROSTATE") {
@@ -3573,23 +3640,20 @@ var ImageNormalizer = /*#__PURE__*/function (_Normalizer) {
3573
3640
  RescaleSlope: rescaleSlope,
3574
3641
  RescaleType: "US"
3575
3642
  };
3576
- var frameCount = this.datasets.length;
3577
- if (!ds.PerFrameFunctionalGroupsSequence) {
3578
- // One item per declared frame, all at the top-level position
3579
- frameCount = Math.max(frameCount, Number(ds.NumberOfFrames) || 0);
3580
- ds.PerFrameFunctionalGroupsSequence = Array.from({
3581
- length: frameCount
3582
- }, function () {
3583
- return ImageNormalizer.planePositionGroup(ds);
3584
- });
3585
- } else if (!Array.isArray(ds.PerFrameFunctionalGroupsSequence)) {
3586
- ds.PerFrameFunctionalGroupsSequence = [ds.PerFrameFunctionalGroupsSequence];
3643
+ var frameCount = Math.max(this.datasets.length, Number(ds.NumberOfFrames) || 0);
3644
+ var perFrame = ds.PerFrameFunctionalGroupsSequence || [];
3645
+ ds.PerFrameFunctionalGroupsSequence = Array.isArray(perFrame) ? perFrame : [perFrame];
3646
+ while (ds.PerFrameFunctionalGroupsSequence.length < frameCount) {
3647
+ ds.PerFrameFunctionalGroupsSequence.push(ImageNormalizer.planePositionGroup(ds));
3587
3648
  }
3588
3649
  for (var frameNumber = 1; frameNumber <= frameCount; frameNumber++) {
3589
3650
  var _ds$PerFrameFunctiona, _ref;
3651
+ var item = (_ds$PerFrameFunctiona = ds.PerFrameFunctionalGroupsSequence)[_ref = frameNumber - 1] || (_ds$PerFrameFunctiona[_ref] = {});
3652
+ if (item.FrameContentSequence) {
3653
+ continue;
3654
+ }
3590
3655
  var dataset = this.datasets[frameNumber - 1] || {};
3591
- (_ds$PerFrameFunctiona = ds.PerFrameFunctionalGroupsSequence)[_ref = frameNumber - 1] || (_ds$PerFrameFunctiona[_ref] = {});
3592
- ds.PerFrameFunctionalGroupsSequence[frameNumber - 1].FrameContentSequence = {
3656
+ item.FrameContentSequence = {
3593
3657
  FrameAcquisitionDuration: 0,
3594
3658
  StackID: 1,
3595
3659
  InStackPositionNumber: frameNumber,
@@ -3597,9 +3661,8 @@ var ImageNormalizer = /*#__PURE__*/function (_Normalizer) {
3597
3661
  };
3598
3662
  var frameTime = dataset.AcquisitionDate + dataset.AcquisitionTime;
3599
3663
  if (!isNaN(frameTime)) {
3600
- var frameContentSequence = ds.PerFrameFunctionalGroupsSequence[frameNumber - 1].FrameContentSequence;
3601
- frameContentSequence.FrameAcquisitionDateTime = frameTime;
3602
- frameContentSequence.FrameReferenceDateTime = frameTime;
3664
+ item.FrameContentSequence.FrameAcquisitionDateTime = frameTime;
3665
+ item.FrameContentSequence.FrameReferenceDateTime = frameTime;
3603
3666
  }
3604
3667
  }
3605
3668
 
@@ -3627,7 +3690,7 @@ var ImageNormalizer = /*#__PURE__*/function (_Normalizer) {
3627
3690
  count: 0
3628
3691
  };
3629
3692
  ds.PerFrameFunctionalGroupsSequence.forEach(function (functionalGroup) {
3630
- if (functionalGroup.FrameVOILUT) {
3693
+ if (functionalGroup !== null && functionalGroup !== void 0 && functionalGroup.FrameVOILUT) {
3631
3694
  var wc = functionalGroup.FrameVOILUTSequence.WindowCenter;
3632
3695
  var ww = functionalGroup.FrameVOILUTSequence.WindowWidth;
3633
3696
  if (functionalGroup.FrameVOILUTSequence && wc && ww) {
@@ -3701,9 +3764,11 @@ var ImageNormalizer = /*#__PURE__*/function (_Normalizer) {
3701
3764
  }, {
3702
3765
  key: "planePositionGroup",
3703
3766
  value: function planePositionGroup(dataset) {
3767
+ var position = dataset.ImagePositionPatient;
3704
3768
  return {
3705
3769
  PlanePositionSequence: {
3706
- ImagePositionPatient: dataset.ImagePositionPatient
3770
+ // A copy, so that no two frames share one array
3771
+ ImagePositionPatient: Array.isArray(position) ? position.slice() : position
3707
3772
  }
3708
3773
  };
3709
3774
  }
@@ -16021,4 +16086,4 @@ exports.derivations = derivations;
16021
16086
  exports.normalizers = normalizers;
16022
16087
  exports.sr = sr;
16023
16088
  exports.utilities = utilities;
16024
- //# sourceMappingURL=dcmjs.shared-Cmkm67T1.cjs.map
16089
+ //# sourceMappingURL=dcmjs.shared-Dkebpbbj.cjs.map