dcmjs 0.52.0 → 0.52.2

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.js CHANGED
@@ -8146,7 +8146,14 @@
8146
8146
  value: function slice() {
8147
8147
  var start = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
8148
8148
  var end = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.size;
8149
- if (start === end) {
8149
+ // Clamp to the data actually held, the way ArrayBuffer.prototype.slice
8150
+ // does. Otherwise a value length declared by a malformed file
8151
+ // allocates the declared size however few bytes are really present.
8152
+ // https://github.com/dcmjs-org/dcmjs/security/advisories/GHSA-xwvm-687g-rm36
8153
+ if (end > this.byteLength) {
8154
+ end = this.byteLength;
8155
+ }
8156
+ if (end <= start) {
8150
8157
  return new Uint8Array(0).buffer;
8151
8158
  }
8152
8159
  var index = this.findStart(start);
@@ -10230,6 +10237,15 @@
10230
10237
  } else if (this.type == 'OB') {
10231
10238
  bytes = stream.readUint8Array(length);
10232
10239
  }*/
10240
+ // A declared value length longer than the data present means a
10241
+ // truncated or malformed file, so read what is there rather than
10242
+ // allocating the declared length, which the file controls.
10243
+ // https://github.com/dcmjs-org/dcmjs/security/advisories/GHSA-xwvm-687g-rm36
10244
+ var available = stream.endOffset - stream.offset;
10245
+ if (length > available) {
10246
+ validationLog.warn("".concat(this.type, " value declares ").concat(length, " bytes but only ").concat(available, " are available - reading the available bytes"));
10247
+ length = available;
10248
+ }
10233
10249
  bytes = stream.getBuffer(stream.offset, stream.offset + length);
10234
10250
  stream.increment(length);
10235
10251
  return [bytes];
@@ -13344,25 +13360,65 @@
13344
13360
  //
13345
13361
  // Handle DICOM and CIELAB colors
13346
13362
  // based on:
13347
- // https://github.com/michaelonken/dcmtk/blob/3c68f0e882e22e6d9e2a42f836332c0ca21b3e7f/dcmiod/libsrc/cielabutil.cc
13363
+ // https://github.com/DCMTK/dcmtk/blob/88105877c3b610e23fc9422ba6a84af85552f5dd/dcmiod/libsrc/cielabutil.cc
13348
13364
  //
13349
13365
  // RGB here refers to sRGB 0-1 per component.
13350
13366
  // dicomlab is CIELAB values as defined in the dicom standard
13351
- // XYZ is CIEXYZ convention
13352
- //
13353
- // TODO: needs a test suite
13354
- // TODO: only dicomlab2RGB tested on real data
13367
+ // XYZ is CIEXYZ convention, relative to the D50 white point (ICC PCS)
13355
13368
  //
13369
+ // DICOM encodes CIELab values in the same way as the ICC Profile Connection
13370
+ // Space (PCS, see PS3.3 C.10.7.1.1), i.e. relative to the D50 illuminant,
13371
+ // whereas the native white point of sRGB is D65. The conversions below
13372
+ // therefore chromatically adapt between the two using the linear Bradford
13373
+ // transform. Matrices and constants are those of the CSS Color Module Level 4
13374
+ // sample code (https://www.w3.org/TR/css-color-4/#color-conversion-code).
13356
13375
  //
13376
+
13377
+ // White points, defined by their 4-figure CIE x,y chromaticities.
13378
+ // D50 is intentionally [0.96430, 1, 0.82510] and not the ICC.1 PCS illuminant
13379
+ // [0.9642, 1, 0.8249]: this matches DCMTK, highdicom and CSS Color 4, and
13380
+ // encodes sRGB white as exactly 0x8080 in a* and b*.
13381
+ var D50_WHITEPOINT_XYZ = [0.3457 / 0.3585, 1.0, (1.0 - 0.3457 - 0.3585) / 0.3585];
13382
+ var D65_WHITEPOINT_XYZ = [0.3127 / 0.329, 1.0, (1.0 - 0.3127 - 0.329) / 0.329];
13383
+ function clamp01(n) {
13384
+ return Math.min(1.0, Math.max(0.0, n));
13385
+ }
13386
+
13387
+ // Rounds to the nearest integer in [0, max]; NaN (invalid input) gives 0
13388
+ function roundClamp(n, max) {
13389
+ if (!(n >= 0.0)) {
13390
+ return 0;
13391
+ }
13392
+ if (n > max) {
13393
+ return max;
13394
+ }
13395
+ return Math.floor(n + 0.5);
13396
+ }
13357
13397
  var Colors = /*#__PURE__*/function () {
13358
13398
  function Colors() {
13359
13399
  _classCallCheck(this, Colors);
13360
13400
  }
13361
13401
  return _createClass(Colors, null, [{
13402
+ key: "d50WhitePointXYZ",
13403
+ value:
13404
+ /**
13405
+ * White point of the D50 illuminant (ICC PCS), the reference white of
13406
+ * DICOM CIELab. Use this white with the XYZ values of rgb2XYZ, xyz2RGB,
13407
+ * lab2XYZ and xyz2LAB.
13408
+ */
13409
+ function d50WhitePointXYZ() {
13410
+ return [].concat(D50_WHITEPOINT_XYZ);
13411
+ }
13412
+
13413
+ /**
13414
+ * White point of the D65 illuminant, the native white of sRGB. No
13415
+ * conversion in this class uses it. Do not combine it with the XYZ
13416
+ * values of this class, which are relative to D50.
13417
+ */
13418
+ }, {
13362
13419
  key: "d65WhitePointXYZ",
13363
13420
  value: function d65WhitePointXYZ() {
13364
- // white points of D65 light point (CIELAB standard white point)
13365
- return [0.950456, 1.0, 1.088754];
13421
+ return [].concat(D65_WHITEPOINT_XYZ);
13366
13422
  }
13367
13423
  }, {
13368
13424
  key: "dicomlab2RGB",
@@ -13374,6 +13430,26 @@
13374
13430
  value: function rgb2DICOMLAB(rgb) {
13375
13431
  return Colors.lab2DICOMLAB(Colors.rgb2LAB(rgb));
13376
13432
  }
13433
+
13434
+ // 16 bit DICOM CIELab -> 8 bit sRGB, rounded and clamped
13435
+ }, {
13436
+ key: "dicomlab2RGB8",
13437
+ value: function dicomlab2RGB8(dicomlab) {
13438
+ return Colors.dicomlab2RGB(dicomlab).map(function (n) {
13439
+ return roundClamp(n * 255.0, 255);
13440
+ });
13441
+ }
13442
+
13443
+ // 8 bit sRGB (array or typed array) -> 16 bit DICOM CIELab, rounded and clamped
13444
+ }, {
13445
+ key: "rgb82DICOMLAB",
13446
+ value: function rgb82DICOMLAB(rgb) {
13447
+ return Colors.rgb2DICOMLAB(Array.from(rgb, function (n) {
13448
+ return n / 255.0;
13449
+ })).map(function (n) {
13450
+ return roundClamp(n, 65535);
13451
+ });
13452
+ }
13377
13453
  }, {
13378
13454
  key: "dicomlab2LAB",
13379
13455
  value: function dicomlab2LAB(dicomlab) {
@@ -13402,7 +13478,8 @@
13402
13478
  }, {
13403
13479
  key: "gammaCorrection",
13404
13480
  value: function gammaCorrection(n) {
13405
- if (n <= 0.0031306684425005883) {
13481
+ // sRGB transfer function (IEC 61966-2-1), linear to gamma-encoded
13482
+ if (n <= 0.0031308) {
13406
13483
  return 12.92 * n;
13407
13484
  } else {
13408
13485
  return 1.055 * Math.pow(n, 1 / 2.4) - 0.055;
@@ -13411,27 +13488,39 @@
13411
13488
  }, {
13412
13489
  key: "invGammaCorrection",
13413
13490
  value: function invGammaCorrection(n) {
13414
- if (n <= 0.0404482362771076) {
13491
+ // sRGB transfer function (IEC 61966-2-1), gamma-encoded to linear
13492
+ if (n <= 0.04045) {
13415
13493
  return n / 12.92;
13416
13494
  } else {
13417
13495
  return Math.pow((n + 0.055) / 1.055, 2.4);
13418
13496
  }
13419
13497
  }
13498
+
13499
+ // sRGB -> CIE XYZ relative to D50, as DICOM CIELab requires (not D65)
13420
13500
  }, {
13421
13501
  key: "rgb2XYZ",
13422
13502
  value: function rgb2XYZ(rgb) {
13503
+ // sRGB to linear RGB
13423
13504
  var R = Colors.invGammaCorrection(rgb[0]);
13424
13505
  var G = Colors.invGammaCorrection(rgb[1]);
13425
13506
  var B = Colors.invGammaCorrection(rgb[2]);
13426
- 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];
13507
+
13508
+ // linear RGB to CIE XYZ (D65)
13509
+ var X65 = 506752.0 / 1228815.0 * R + 87881.0 / 245763.0 * G + 12673.0 / 70218.0 * B;
13510
+ var Y65 = 87098.0 / 409605.0 * R + 175762.0 / 245763.0 * G + 12673.0 / 175545.0 * B;
13511
+ var Z65 = 7918.0 / 409605.0 * R + 87881.0 / 737289.0 * G + 1001167.0 / 1053270.0 * B;
13512
+
13513
+ // chromatic adaptation D65 to D50 (linear Bradford)
13514
+ 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];
13427
13515
  }
13516
+
13517
+ // CIE XYZ relative to D50 -> CIELab
13428
13518
  }, {
13429
13519
  key: "xyz2LAB",
13430
13520
  value: function xyz2LAB(xyz) {
13431
- var whitePoint = Colors.d65WhitePointXYZ();
13432
- var X = xyz[0] / whitePoint[0];
13433
- var Y = xyz[1] / whitePoint[1];
13434
- var Z = xyz[2] / whitePoint[2];
13521
+ var X = xyz[0] / D50_WHITEPOINT_XYZ[0];
13522
+ var Y = xyz[1] / D50_WHITEPOINT_XYZ[1];
13523
+ var Z = xyz[2] / D50_WHITEPOINT_XYZ[2];
13435
13524
  X = Colors.labf(X);
13436
13525
  Y = Colors.labf(Y);
13437
13526
  Z = Colors.labf(Z);
@@ -13442,50 +13531,58 @@
13442
13531
  value: function lab2RGB(lab) {
13443
13532
  return Colors.xyz2RGB(Colors.lab2XYZ(lab));
13444
13533
  }
13534
+
13535
+ // CIELab -> CIE XYZ relative to D50
13445
13536
  }, {
13446
13537
  key: "lab2XYZ",
13447
13538
  value: function lab2XYZ(lab) {
13448
13539
  var L = (lab[0] + 16) / 116;
13449
13540
  var a = L + lab[1] / 500;
13450
13541
  var b = L - lab[2] / 200;
13451
- var whitePoint = Colors.d65WhitePointXYZ();
13452
- return [whitePoint[0] * Colors.labfInv(a), whitePoint[1] * Colors.labfInv(L), whitePoint[2] * Colors.labfInv(b)];
13542
+ return [D50_WHITEPOINT_XYZ[0] * Colors.labfInv(a), D50_WHITEPOINT_XYZ[1] * Colors.labfInv(L), D50_WHITEPOINT_XYZ[2] * Colors.labfInv(b)];
13453
13543
  }
13544
+
13545
+ // CIE XYZ relative to D50 -> sRGB, clipped per channel to [0, 1]
13454
13546
  }, {
13455
13547
  key: "xyz2RGB",
13456
13548
  value: function xyz2RGB(xyz) {
13457
- var R1 = 3.2406 * xyz[0] - 1.5372 * xyz[1] - 0.4986 * xyz[2];
13458
- var G1 = -0.9689 * xyz[0] + 1.8758 * xyz[1] + 0.0415 * xyz[2];
13459
- var B1 = 0.0557 * xyz[0] - 0.204 * xyz[1] + 1.057 * xyz[2];
13549
+ var _xyz = _slicedToArray(xyz, 3),
13550
+ X = _xyz[0],
13551
+ Y = _xyz[1],
13552
+ Z = _xyz[2];
13460
13553
 
13461
- /* Force nonnegative values so that gamma correction is well-defined. */
13462
- var minimumComponent = Math.min(R1, G1);
13463
- minimumComponent = Math.min(minimumComponent, B1);
13464
- if (minimumComponent < 0) {
13465
- R1 -= minimumComponent;
13466
- G1 -= minimumComponent;
13467
- B1 -= minimumComponent;
13468
- }
13554
+ // chromatic adaptation D50 to D65 (linear Bradford)
13555
+ var X65 = 0.955473421488075 * X - 0.02309845494876471 * Y + 0.06325924320057072 * Z;
13556
+ var Y65 = -0.0283697093338637 * X + 1.0099953980813041 * Y + 0.021041441191917323 * Z;
13557
+ var Z65 = 0.012314014864481998 * X - 0.020507649298898964 * Y + 1.330365926242124 * Z;
13469
13558
 
13470
- /* Transform from RGB to R'G'B' */
13471
- return [Colors.gammaCorrection(R1), Colors.gammaCorrection(G1), Colors.gammaCorrection(B1)];
13559
+ // CIE XYZ (D65) to linear RGB
13560
+ var R1 = 12831.0 / 3959.0 * X65 - 329.0 / 214.0 * Y65 - 1974.0 / 3959.0 * Z65;
13561
+ var G1 = -851781 / 878810.0 * X65 + 1648619.0 / 878810.0 * Y65 + 36519.0 / 878810.0 * Z65;
13562
+ var B1 = 705.0 / 12673.0 * X65 - 2585.0 / 12673.0 * Y65 + 705.0 / 667.0 * Z65;
13563
+
13564
+ // Clip to the sRGB gamut (also makes sure that gamma encoding is
13565
+ // well-defined), then transform from linear RGB to sRGB
13566
+ return [Colors.gammaCorrection(clamp01(R1)), Colors.gammaCorrection(clamp01(G1)), Colors.gammaCorrection(clamp01(B1))];
13472
13567
  }
13473
13568
  }, {
13474
13569
  key: "labf",
13475
13570
  value: function labf(n) {
13476
- if (n >= 8.85645167903563082e-3) {
13477
- return Math.pow(n, 0.333333333333333);
13571
+ // epsilon = (6/29)^3 = 216/24389, kappa = (29/3)^3 = 24389/27
13572
+ if (n > 216.0 / 24389.0) {
13573
+ return Math.cbrt(n);
13478
13574
  } else {
13479
- return 841.0 / 108.0 * n + 4.0 / 29.0;
13575
+ return (24389.0 / 27.0 * n + 16.0) / 116.0;
13480
13576
  }
13481
13577
  }
13482
13578
  }, {
13483
13579
  key: "labfInv",
13484
13580
  value: function labfInv(n) {
13485
- if (n >= 0.206896551724137931) {
13581
+ // n > 6/29 <=> n^3 > epsilon
13582
+ if (n > 6.0 / 29.0) {
13486
13583
  return n * n * n;
13487
13584
  } else {
13488
- return 108.0 / 841.0 * (n - 4.0 / 29.0);
13585
+ return (116.0 * n - 16.0) / (24389.0 / 27.0);
13489
13586
  }
13490
13587
  }
13491
13588
  }]);
@@ -24346,12 +24443,8 @@
24346
24443
  CORNERSTONE_3D_TAG: CORNERSTONE_3D_TAG
24347
24444
  };
24348
24445
 
24349
- // TODO: Is there a better name for this? RGBAInt?
24350
- // Should we move it to Colors.js
24351
24446
  function dicomlab2RGBA(cielab) {
24352
- var rgba = Colors.dicomlab2RGB(cielab).map(function (x) {
24353
- return Math.round(x * 255);
24354
- });
24447
+ var rgba = Colors.dicomlab2RGB8(cielab);
24355
24448
  rgba.push(255);
24356
24449
  return rgba;
24357
24450
  }