@dcmjs-org/dcmjs 1.0.0-beta.16 → 1.0.0-beta.18

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.
@@ -1355,25 +1355,65 @@ var ValueRepresentation = /*#__PURE__*/function (_CoreValueRepresentat) {
1355
1355
  //
1356
1356
  // Handle DICOM and CIELAB colors
1357
1357
  // based on:
1358
- // https://github.com/michaelonken/dcmtk/blob/3c68f0e882e22e6d9e2a42f836332c0ca21b3e7f/dcmiod/libsrc/cielabutil.cc
1358
+ // https://github.com/DCMTK/dcmtk/blob/88105877c3b610e23fc9422ba6a84af85552f5dd/dcmiod/libsrc/cielabutil.cc
1359
1359
  //
1360
1360
  // RGB here refers to sRGB 0-1 per component.
1361
1361
  // dicomlab is CIELAB values as defined in the dicom standard
1362
- // XYZ is CIEXYZ convention
1363
- //
1364
- // TODO: needs a test suite
1365
- // TODO: only dicomlab2RGB tested on real data
1362
+ // XYZ is CIEXYZ convention, relative to the D50 white point (ICC PCS)
1366
1363
  //
1364
+ // DICOM encodes CIELab values in the same way as the ICC Profile Connection
1365
+ // Space (PCS, see PS3.3 C.10.7.1.1), i.e. relative to the D50 illuminant,
1366
+ // whereas the native white point of sRGB is D65. The conversions below
1367
+ // therefore chromatically adapt between the two using the linear Bradford
1368
+ // transform. Matrices and constants are those of the CSS Color Module Level 4
1369
+ // sample code (https://www.w3.org/TR/css-color-4/#color-conversion-code).
1367
1370
  //
1371
+
1372
+ // White points, defined by their 4-figure CIE x,y chromaticities.
1373
+ // D50 is intentionally [0.96430, 1, 0.82510] and not the ICC.1 PCS illuminant
1374
+ // [0.9642, 1, 0.8249]: this matches DCMTK, highdicom and CSS Color 4, and
1375
+ // encodes sRGB white as exactly 0x8080 in a* and b*.
1376
+ var D50_WHITEPOINT_XYZ = [0.3457 / 0.3585, 1.0, (1.0 - 0.3457 - 0.3585) / 0.3585];
1377
+ var D65_WHITEPOINT_XYZ = [0.3127 / 0.329, 1.0, (1.0 - 0.3127 - 0.329) / 0.329];
1378
+ function clamp01(n) {
1379
+ return Math.min(1.0, Math.max(0.0, n));
1380
+ }
1381
+
1382
+ // Rounds to the nearest integer in [0, max]; NaN (invalid input) gives 0
1383
+ function roundClamp(n, max) {
1384
+ if (!(n >= 0.0)) {
1385
+ return 0;
1386
+ }
1387
+ if (n > max) {
1388
+ return max;
1389
+ }
1390
+ return Math.floor(n + 0.5);
1391
+ }
1368
1392
  var Colors = /*#__PURE__*/function () {
1369
1393
  function Colors() {
1370
1394
  _classCallCheck(this, Colors);
1371
1395
  }
1372
1396
  return _createClass(Colors, null, [{
1397
+ key: "d50WhitePointXYZ",
1398
+ value:
1399
+ /**
1400
+ * White point of the D50 illuminant (ICC PCS), the reference white of
1401
+ * DICOM CIELab. Use this white with the XYZ values of rgb2XYZ, xyz2RGB,
1402
+ * lab2XYZ and xyz2LAB.
1403
+ */
1404
+ function d50WhitePointXYZ() {
1405
+ return [].concat(D50_WHITEPOINT_XYZ);
1406
+ }
1407
+
1408
+ /**
1409
+ * White point of the D65 illuminant, the native white of sRGB. No
1410
+ * conversion in this class uses it. Do not combine it with the XYZ
1411
+ * values of this class, which are relative to D50.
1412
+ */
1413
+ }, {
1373
1414
  key: "d65WhitePointXYZ",
1374
1415
  value: function d65WhitePointXYZ() {
1375
- // white points of D65 light point (CIELAB standard white point)
1376
- return [0.950456, 1.0, 1.088754];
1416
+ return [].concat(D65_WHITEPOINT_XYZ);
1377
1417
  }
1378
1418
  }, {
1379
1419
  key: "dicomlab2RGB",
@@ -1385,6 +1425,26 @@ var Colors = /*#__PURE__*/function () {
1385
1425
  value: function rgb2DICOMLAB(rgb) {
1386
1426
  return Colors.lab2DICOMLAB(Colors.rgb2LAB(rgb));
1387
1427
  }
1428
+
1429
+ // 16 bit DICOM CIELab -> 8 bit sRGB, rounded and clamped
1430
+ }, {
1431
+ key: "dicomlab2RGB8",
1432
+ value: function dicomlab2RGB8(dicomlab) {
1433
+ return Colors.dicomlab2RGB(dicomlab).map(function (n) {
1434
+ return roundClamp(n * 255.0, 255);
1435
+ });
1436
+ }
1437
+
1438
+ // 8 bit sRGB (array or typed array) -> 16 bit DICOM CIELab, rounded and clamped
1439
+ }, {
1440
+ key: "rgb82DICOMLAB",
1441
+ value: function rgb82DICOMLAB(rgb) {
1442
+ return Colors.rgb2DICOMLAB(Array.from(rgb, function (n) {
1443
+ return n / 255.0;
1444
+ })).map(function (n) {
1445
+ return roundClamp(n, 65535);
1446
+ });
1447
+ }
1388
1448
  }, {
1389
1449
  key: "dicomlab2LAB",
1390
1450
  value: function dicomlab2LAB(dicomlab) {
@@ -1413,7 +1473,8 @@ var Colors = /*#__PURE__*/function () {
1413
1473
  }, {
1414
1474
  key: "gammaCorrection",
1415
1475
  value: function gammaCorrection(n) {
1416
- if (n <= 0.0031306684425005883) {
1476
+ // sRGB transfer function (IEC 61966-2-1), linear to gamma-encoded
1477
+ if (n <= 0.0031308) {
1417
1478
  return 12.92 * n;
1418
1479
  } else {
1419
1480
  return 1.055 * Math.pow(n, 1 / 2.4) - 0.055;
@@ -1422,27 +1483,39 @@ var Colors = /*#__PURE__*/function () {
1422
1483
  }, {
1423
1484
  key: "invGammaCorrection",
1424
1485
  value: function invGammaCorrection(n) {
1425
- if (n <= 0.0404482362771076) {
1486
+ // sRGB transfer function (IEC 61966-2-1), gamma-encoded to linear
1487
+ if (n <= 0.04045) {
1426
1488
  return n / 12.92;
1427
1489
  } else {
1428
1490
  return Math.pow((n + 0.055) / 1.055, 2.4);
1429
1491
  }
1430
1492
  }
1493
+
1494
+ // sRGB -> CIE XYZ relative to D50, as DICOM CIELab requires (not D65)
1431
1495
  }, {
1432
1496
  key: "rgb2XYZ",
1433
1497
  value: function rgb2XYZ(rgb) {
1498
+ // sRGB to linear RGB
1434
1499
  var R = Colors.invGammaCorrection(rgb[0]);
1435
1500
  var G = Colors.invGammaCorrection(rgb[1]);
1436
1501
  var B = Colors.invGammaCorrection(rgb[2]);
1437
- return [0.4123955889674142161 * R + 0.3575834307637148171 * G + 0.1804926473817015735 * B, 0.2125862307855955516 * R + 0.7151703037034108499 * G + 0.07220049864333622685 * B, 0.01929721549174694484 * R + 0.1191838645808485318 * G + 0.950497125131579766 * B];
1502
+
1503
+ // linear RGB to CIE XYZ (D65)
1504
+ var X65 = 506752.0 / 1228815.0 * R + 87881.0 / 245763.0 * G + 12673.0 / 70218.0 * B;
1505
+ var Y65 = 87098.0 / 409605.0 * R + 175762.0 / 245763.0 * G + 12673.0 / 175545.0 * B;
1506
+ var Z65 = 7918.0 / 409605.0 * R + 87881.0 / 737289.0 * G + 1001167.0 / 1053270.0 * B;
1507
+
1508
+ // chromatic adaptation D65 to D50 (linear Bradford)
1509
+ return [1.0479297925449969 * X65 + 0.022946870601609652 * Y65 - 0.05019226628920524 * Z65, 0.02962780877005599 * X65 + 0.9904344267538799 * Y65 - 0.017073799063418826 * Z65, -0.009243040646204504 * X65 + 0.015055191490298152 * Y65 + 0.7518742814281371 * Z65];
1438
1510
  }
1511
+
1512
+ // CIE XYZ relative to D50 -> CIELab
1439
1513
  }, {
1440
1514
  key: "xyz2LAB",
1441
1515
  value: function xyz2LAB(xyz) {
1442
- var whitePoint = Colors.d65WhitePointXYZ();
1443
- var X = xyz[0] / whitePoint[0];
1444
- var Y = xyz[1] / whitePoint[1];
1445
- var Z = xyz[2] / whitePoint[2];
1516
+ var X = xyz[0] / D50_WHITEPOINT_XYZ[0];
1517
+ var Y = xyz[1] / D50_WHITEPOINT_XYZ[1];
1518
+ var Z = xyz[2] / D50_WHITEPOINT_XYZ[2];
1446
1519
  X = Colors.labf(X);
1447
1520
  Y = Colors.labf(Y);
1448
1521
  Z = Colors.labf(Z);
@@ -1453,50 +1526,58 @@ var Colors = /*#__PURE__*/function () {
1453
1526
  value: function lab2RGB(lab) {
1454
1527
  return Colors.xyz2RGB(Colors.lab2XYZ(lab));
1455
1528
  }
1529
+
1530
+ // CIELab -> CIE XYZ relative to D50
1456
1531
  }, {
1457
1532
  key: "lab2XYZ",
1458
1533
  value: function lab2XYZ(lab) {
1459
1534
  var L = (lab[0] + 16) / 116;
1460
1535
  var a = L + lab[1] / 500;
1461
1536
  var b = L - lab[2] / 200;
1462
- var whitePoint = Colors.d65WhitePointXYZ();
1463
- return [whitePoint[0] * Colors.labfInv(a), whitePoint[1] * Colors.labfInv(L), whitePoint[2] * Colors.labfInv(b)];
1537
+ return [D50_WHITEPOINT_XYZ[0] * Colors.labfInv(a), D50_WHITEPOINT_XYZ[1] * Colors.labfInv(L), D50_WHITEPOINT_XYZ[2] * Colors.labfInv(b)];
1464
1538
  }
1539
+
1540
+ // CIE XYZ relative to D50 -> sRGB, clipped per channel to [0, 1]
1465
1541
  }, {
1466
1542
  key: "xyz2RGB",
1467
1543
  value: function xyz2RGB(xyz) {
1468
- var R1 = 3.2406 * xyz[0] - 1.5372 * xyz[1] - 0.4986 * xyz[2];
1469
- var G1 = -0.9689 * xyz[0] + 1.8758 * xyz[1] + 0.0415 * xyz[2];
1470
- var B1 = 0.0557 * xyz[0] - 0.204 * xyz[1] + 1.057 * xyz[2];
1544
+ var _xyz = _slicedToArray(xyz, 3),
1545
+ X = _xyz[0],
1546
+ Y = _xyz[1],
1547
+ Z = _xyz[2];
1471
1548
 
1472
- /* Force nonnegative values so that gamma correction is well-defined. */
1473
- var minimumComponent = Math.min(R1, G1);
1474
- minimumComponent = Math.min(minimumComponent, B1);
1475
- if (minimumComponent < 0) {
1476
- R1 -= minimumComponent;
1477
- G1 -= minimumComponent;
1478
- B1 -= minimumComponent;
1479
- }
1549
+ // chromatic adaptation D50 to D65 (linear Bradford)
1550
+ var X65 = 0.955473421488075 * X - 0.02309845494876471 * Y + 0.06325924320057072 * Z;
1551
+ var Y65 = -0.0283697093338637 * X + 1.0099953980813041 * Y + 0.021041441191917323 * Z;
1552
+ var Z65 = 0.012314014864481998 * X - 0.020507649298898964 * Y + 1.330365926242124 * Z;
1480
1553
 
1481
- /* Transform from RGB to R'G'B' */
1482
- return [Colors.gammaCorrection(R1), Colors.gammaCorrection(G1), Colors.gammaCorrection(B1)];
1554
+ // CIE XYZ (D65) to linear RGB
1555
+ var R1 = 12831.0 / 3959.0 * X65 - 329.0 / 214.0 * Y65 - 1974.0 / 3959.0 * Z65;
1556
+ var G1 = -851781 / 878810.0 * X65 + 1648619.0 / 878810.0 * Y65 + 36519.0 / 878810.0 * Z65;
1557
+ var B1 = 705.0 / 12673.0 * X65 - 2585.0 / 12673.0 * Y65 + 705.0 / 667.0 * Z65;
1558
+
1559
+ // Clip to the sRGB gamut (also makes sure that gamma encoding is
1560
+ // well-defined), then transform from linear RGB to sRGB
1561
+ return [Colors.gammaCorrection(clamp01(R1)), Colors.gammaCorrection(clamp01(G1)), Colors.gammaCorrection(clamp01(B1))];
1483
1562
  }
1484
1563
  }, {
1485
1564
  key: "labf",
1486
1565
  value: function labf(n) {
1487
- if (n >= 8.85645167903563082e-3) {
1488
- return Math.pow(n, 0.333333333333333);
1566
+ // epsilon = (6/29)^3 = 216/24389, kappa = (29/3)^3 = 24389/27
1567
+ if (n > 216.0 / 24389.0) {
1568
+ return Math.cbrt(n);
1489
1569
  } else {
1490
- return 841.0 / 108.0 * n + 4.0 / 29.0;
1570
+ return (24389.0 / 27.0 * n + 16.0) / 116.0;
1491
1571
  }
1492
1572
  }
1493
1573
  }, {
1494
1574
  key: "labfInv",
1495
1575
  value: function labfInv(n) {
1496
- if (n >= 0.206896551724137931) {
1576
+ // n > 6/29 <=> n^3 > epsilon
1577
+ if (n > 6.0 / 29.0) {
1497
1578
  return n * n * n;
1498
1579
  } else {
1499
- return 108.0 / 841.0 * (n - 4.0 / 29.0);
1580
+ return (116.0 * n - 16.0) / (24389.0 / 27.0);
1500
1581
  }
1501
1582
  }
1502
1583
  }]);
@@ -3347,16 +3428,10 @@ var ImageNormalizer = /*#__PURE__*/function (_Normalizer) {
3347
3428
  //
3348
3429
  // shared
3349
3430
  var SpacingBetweenSlices = Math.abs(distance1 - distance0);
3350
- ds.SharedFunctionalGroupsSequence = {
3351
- PlaneOrientationSequence: {
3352
- ImageOrientationPatient: dataset0.ImageOrientationPatient
3353
- },
3354
- PixelMeasuresSequence: {
3355
- PixelSpacing: dataset0.PixelSpacing,
3356
- SpacingBetweenSlices: SpacingBetweenSlices,
3357
- SliceThickness: SpacingBetweenSlices
3358
- }
3359
- };
3431
+ ds.SharedFunctionalGroupsSequence = ImageNormalizer.sharedImagePlaneGroups(dataset0, {
3432
+ SpacingBetweenSlices: SpacingBetweenSlices,
3433
+ SliceThickness: SpacingBetweenSlices
3434
+ });
3360
3435
  ds.ReferencedSeriesSequence = {
3361
3436
  SeriesInstanceUID: dataset0.SeriesInstanceUID,
3362
3437
  ReferencedInstanceSequence: []
@@ -3369,15 +3444,12 @@ var ImageNormalizer = /*#__PURE__*/function (_Normalizer) {
3369
3444
  // and set the referenced series uid
3370
3445
  distanceDatasetPairs.forEach(function (pair) {
3371
3446
  var dataset = pair[1];
3372
- ds.PerFrameFunctionalGroupsSequence.push({
3373
- PlanePositionSequence: {
3374
- ImagePositionPatient: dataset.ImagePositionPatient
3375
- },
3447
+ ds.PerFrameFunctionalGroupsSequence.push(_objectSpread2(_objectSpread2({}, ImageNormalizer.planePositionGroup(dataset)), {}, {
3376
3448
  FrameVOILUTSequence: {
3377
3449
  WindowCenter: dataset.WindowCenter,
3378
3450
  WindowWidth: dataset.WindowWidth
3379
3451
  }
3380
- });
3452
+ }));
3381
3453
  ds.ReferencedSeriesSequence.ReferencedInstanceSequence.push({
3382
3454
  ReferencedSOPClassUID: dataset.SOPClassUID,
3383
3455
  ReferencedSOPInstanceUID: dataset.SOPInstanceUID
@@ -3419,7 +3491,11 @@ var ImageNormalizer = /*#__PURE__*/function (_Normalizer) {
3419
3491
  ds.PresentationLUTShape = "IDENTITY";
3420
3492
  }
3421
3493
  if (!ds.SharedFunctionalGroupsSequence) {
3422
- core.log.error("Can only process multiframe data with SharedFunctionalGroupsSequence");
3494
+ core.log.warn("Missing SharedFunctionalGroupsSequence; using the top-level image plane attributes");
3495
+ ds.SharedFunctionalGroupsSequence = ImageNormalizer.sharedImagePlaneGroups(ds, {
3496
+ SliceThickness: ds.SliceThickness,
3497
+ SpacingBetweenSlices: ds.SpacingBetweenSlices
3498
+ });
3423
3499
  }
3424
3500
 
3425
3501
  // TODO: special case!
@@ -3440,9 +3516,22 @@ var ImageNormalizer = /*#__PURE__*/function (_Normalizer) {
3440
3516
  RescaleSlope: rescaleSlope,
3441
3517
  RescaleType: "US"
3442
3518
  };
3443
- var frameNumber = 1;
3444
- this.datasets.forEach(function (dataset) {
3445
- if (ds.NumberOfFrames === 1) ds.PerFrameFunctionalGroupsSequence = [ds.PerFrameFunctionalGroupsSequence];
3519
+ var frameCount = this.datasets.length;
3520
+ if (!ds.PerFrameFunctionalGroupsSequence) {
3521
+ // One item per declared frame, all at the top-level position
3522
+ frameCount = Math.max(frameCount, Number(ds.NumberOfFrames) || 0);
3523
+ ds.PerFrameFunctionalGroupsSequence = Array.from({
3524
+ length: frameCount
3525
+ }, function () {
3526
+ return ImageNormalizer.planePositionGroup(ds);
3527
+ });
3528
+ } else if (!Array.isArray(ds.PerFrameFunctionalGroupsSequence)) {
3529
+ ds.PerFrameFunctionalGroupsSequence = [ds.PerFrameFunctionalGroupsSequence];
3530
+ }
3531
+ for (var frameNumber = 1; frameNumber <= frameCount; frameNumber++) {
3532
+ var _ds$PerFrameFunctiona, _ref;
3533
+ var dataset = this.datasets[frameNumber - 1] || {};
3534
+ (_ds$PerFrameFunctiona = ds.PerFrameFunctionalGroupsSequence)[_ref = frameNumber - 1] || (_ds$PerFrameFunctiona[_ref] = {});
3446
3535
  ds.PerFrameFunctionalGroupsSequence[frameNumber - 1].FrameContentSequence = {
3447
3536
  FrameAcquisitionDuration: 0,
3448
3537
  StackID: 1,
@@ -3455,8 +3544,7 @@ var ImageNormalizer = /*#__PURE__*/function (_Normalizer) {
3455
3544
  frameContentSequence.FrameAcquisitionDateTime = frameTime;
3456
3545
  frameContentSequence.FrameReferenceDateTime = frameTime;
3457
3546
  }
3458
- frameNumber++;
3459
- });
3547
+ }
3460
3548
 
3461
3549
  //
3462
3550
  // TODO: convert this to shared functional group not top level element
@@ -3541,6 +3629,27 @@ var ImageNormalizer = /*#__PURE__*/function (_Normalizer) {
3541
3629
  value: function vec3Dot(a, b) {
3542
3630
  return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
3543
3631
  }
3632
+ }, {
3633
+ key: "sharedImagePlaneGroups",
3634
+ value: function sharedImagePlaneGroups(dataset, pixelMeasures) {
3635
+ return {
3636
+ PlaneOrientationSequence: {
3637
+ ImageOrientationPatient: dataset.ImageOrientationPatient
3638
+ },
3639
+ PixelMeasuresSequence: _objectSpread2({
3640
+ PixelSpacing: dataset.PixelSpacing
3641
+ }, pixelMeasures)
3642
+ };
3643
+ }
3644
+ }, {
3645
+ key: "planePositionGroup",
3646
+ value: function planePositionGroup(dataset) {
3647
+ return {
3648
+ PlanePositionSequence: {
3649
+ ImagePositionPatient: dataset.ImagePositionPatient
3650
+ }
3651
+ };
3652
+ }
3544
3653
  }]);
3545
3654
  }(Normalizer);
3546
3655
  var MRImageNormalizer = /*#__PURE__*/function (_ImageNormalizer) {
@@ -6329,7 +6438,7 @@ function h(matrix) {
6329
6438
  var _matrix$shape = _slicedToArray(matrix.shape, 2),
6330
6439
  rows = _matrix$shape[0],
6331
6440
  cols = _matrix$shape[1];
6332
- var result = ndarray(new Uint8Array(rows * cols), [rows, cols]);
6441
+ var result = ndarray(new matrix.data.constructor(rows * cols), [rows, cols]);
6333
6442
  for (var i = 0; i < rows; i++) {
6334
6443
  for (var j = 0; j < cols; j++) {
6335
6444
  result.set(i, j, matrix.get(i, cols - 1 - j));
@@ -6348,7 +6457,7 @@ function v(matrix) {
6348
6457
  var _matrix$shape2 = _slicedToArray(matrix.shape, 2),
6349
6458
  rows = _matrix$shape2[0],
6350
6459
  cols = _matrix$shape2[1];
6351
- var result = ndarray(new Uint8Array(rows * cols), [rows, cols]);
6460
+ var result = ndarray(new matrix.data.constructor(rows * cols), [rows, cols]);
6352
6461
  for (var j = 0; j < cols; j++) {
6353
6462
  for (var i = 0; i < rows; i++) {
6354
6463
  result.set(i, j, matrix.get(rows - 1 - i, j));
@@ -6367,7 +6476,7 @@ function rotateMatrix902D (matrix) {
6367
6476
  var _matrix$shape = _slicedToArray(matrix.shape, 2),
6368
6477
  rows = _matrix$shape[0],
6369
6478
  cols = _matrix$shape[1];
6370
- var result = ndarray(new Uint8Array(rows * cols), [cols, rows]);
6479
+ var result = ndarray(new matrix.data.constructor(rows * cols), [cols, rows]);
6371
6480
  var resultColsMinus1 = result.shape[1] - 1;
6372
6481
  for (var i = 0; i < rows; i++) {
6373
6482
  for (var j = 0; j < cols; j++) {
@@ -12004,12 +12113,8 @@ var Cornerstone3D = {
12004
12113
  CORNERSTONE_3D_TAG: CORNERSTONE_3D_TAG
12005
12114
  };
12006
12115
 
12007
- // TODO: Is there a better name for this? RGBAInt?
12008
- // Should we move it to Colors.js
12009
12116
  function dicomlab2RGBA(cielab) {
12010
- var rgba = Colors.dicomlab2RGB(cielab).map(function (x) {
12011
- return Math.round(x * 255);
12012
- });
12117
+ var rgba = Colors.dicomlab2RGB8(cielab);
12013
12118
  rgba.push(255);
12014
12119
  return rgba;
12015
12120
  }
@@ -15854,4 +15959,4 @@ exports.derivations = derivations;
15854
15959
  exports.normalizers = normalizers;
15855
15960
  exports.sr = sr;
15856
15961
  exports.utilities = utilities;
15857
- //# sourceMappingURL=dcmjs.shared-D1MD8zKQ.cjs.map
15962
+ //# sourceMappingURL=dcmjs.shared-CKG7Js9d.cjs.map