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