dwv 0.30.8 → 0.31.0-beta.0

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/dist/dwv.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! dwv 0.30.8 2022-01-04 12:43:27 */
1
+ /*! dwv 0.31.0-beta.0 2022-03-11 16:27:12 */
2
2
  // Inspired from umdjs
3
3
  // See https://github.com/umdjs/umd/blob/master/templates/returnExports.js
4
4
  (function (root, factory) {
@@ -295,7 +295,7 @@ dwv.App = function () {
295
295
  * - `dataViewConfigs`: data indexed object containing the data view
296
296
  * configurations in the form of a list of objects containing:
297
297
  * - divId: the HTML div id
298
- * - orientation: optional 'axial', 'coronal' or 'sagittal' otientation
298
+ * - orientation: optional 'axial', 'coronal' or 'sagittal' orientation
299
299
  * string (default undefined keeps the original slice order)
300
300
  * - `binders`: array of layerGroup binders
301
301
  * - `tools`: tool name indexed object containing individual tool
@@ -526,20 +526,15 @@ dwv.App = function () {
526
526
  // load API [end] ---------------------------------------------------------
527
527
 
528
528
  /**
529
- * Fit the display to the given size. To be called once the image is loaded.
529
+ * Fit the display to the data of each layer group.
530
+ * To be called once the image is loaded.
530
531
  */
531
532
  this.fitToContainer = function () {
532
- var layerGroup = stage.getActiveLayerGroup();
533
- if (layerGroup) {
534
- var geometry = self.getLastImage().getGeometry();
535
- var size = geometry.getSize().get2D();
536
- var spacing = geometry.getSpacing().get2D();
537
- var width = size.x * spacing.x;
538
- var height = size.y * spacing.y;
539
- layerGroup.fitToContainer({x: width, y: height});
533
+ for (var i = 0; i < stage.getNumberOfLayerGroups(); ++i) {
534
+ var layerGroup = stage.getLayerGroup(i);
535
+ var mazSize = getLayerGroupMaxSize(layerGroup);
536
+ layerGroup.fitToContainer(mazSize);
540
537
  layerGroup.draw();
541
- // update style
542
- //style.setBaseScale(layerGroup.getBaseScale());
543
538
  }
544
539
  };
545
540
 
@@ -563,7 +558,7 @@ dwv.App = function () {
563
558
  // check options
564
559
  if (options.dataViewConfigs === null ||
565
560
  typeof options.dataViewConfigs === 'undefined') {
566
- throw new Error('No available data iew configuration');
561
+ throw new Error('No available data view configuration');
567
562
  }
568
563
  var configs = null;
569
564
  if (typeof options.dataViewConfigs['*'] !== 'undefined') {
@@ -1067,7 +1062,8 @@ dwv.App = function () {
1067
1062
  var eventMetaData = null;
1068
1063
  if (event.loadtype === 'image') {
1069
1064
  if (isFirstLoadItem && timeId === 0) {
1070
- dataController.addNew(event.data.image, event.data.info);
1065
+ dataController.addNew(
1066
+ event.loadid, event.data.image, event.data.info);
1071
1067
  } else {
1072
1068
  dataController.update(
1073
1069
  event.loadid, event.data.image, event.data.info,
@@ -1240,6 +1236,36 @@ dwv.App = function () {
1240
1236
  }
1241
1237
  }
1242
1238
 
1239
+ /**
1240
+ * Get the data max size for a layer group.
1241
+ *
1242
+ * @todo Filter for data of the layer group.
1243
+ * @param {object} lg The layer group.
1244
+ * @returns {object} The max size as {x,y}.
1245
+ */
1246
+ function getLayerGroupMaxSize(lg) {
1247
+ var maxSize = {x: 0, y: 0};
1248
+ for (var i = 0; i < dataController.length(); ++i) {
1249
+ var dc = dataController.get(i);
1250
+ var geometry = dc.image.getGeometry();
1251
+ var viewOrient = dwv.gui.getViewOrientation(
1252
+ geometry,
1253
+ lg.getTargetOrientation()
1254
+ );
1255
+ var size = geometry.getSize(viewOrient).get2D();
1256
+ var spacing = geometry.getSpacing(viewOrient).get2D();
1257
+ var width = size.x * spacing.x;
1258
+ if (width > maxSize.x) {
1259
+ maxSize.x = width;
1260
+ }
1261
+ var height = size.y * spacing.y;
1262
+ if (height > maxSize.y) {
1263
+ maxSize.y = height;
1264
+ }
1265
+ }
1266
+ return maxSize;
1267
+ }
1268
+
1243
1269
  /**
1244
1270
  * Add a view layer.
1245
1271
  *
@@ -1324,25 +1350,7 @@ dwv.App = function () {
1324
1350
  }
1325
1351
 
1326
1352
  // fit to the maximum size
1327
- var maxSize = {x: 0, y: 0};
1328
- for (var i = 0; i < dataController.length(); ++i) {
1329
- var dc = dataController.get(i);
1330
- var geometry = dc.image.getGeometry();
1331
- var viewOrient = dwv.gui.getViewOrientation(
1332
- geometry,
1333
- layerGroup.getTargetOrientation()
1334
- );
1335
- var size = geometry.getSize(viewOrient).get2D();
1336
- var spacing = geometry.getSpacing(viewOrient).get2D();
1337
- var width = size.x * spacing.x;
1338
- if (width > maxSize.x) {
1339
- maxSize.x = width;
1340
- }
1341
- var height = size.y * spacing.y;
1342
- if (height > maxSize.y) {
1343
- maxSize.y = height;
1344
- }
1345
- }
1353
+ var maxSize = getLayerGroupMaxSize(layerGroup);
1346
1354
  layerGroup.fitToContainer(maxSize);
1347
1355
  }
1348
1356
 
@@ -1364,9 +1372,9 @@ dwv.ctrl.DataController = function () {
1364
1372
  * List of {image, meta}.
1365
1373
  *
1366
1374
  * @private
1367
- * @type {Array}
1375
+ * @type {object}
1368
1376
  */
1369
- var data = [];
1377
+ var data = {};
1370
1378
 
1371
1379
  /**
1372
1380
  * Listener handler.
@@ -1382,7 +1390,7 @@ dwv.ctrl.DataController = function () {
1382
1390
  * @returns {number} The length.
1383
1391
  */
1384
1392
  this.length = function () {
1385
- return data.length;
1393
+ return Object.keys(data).length;
1386
1394
  };
1387
1395
 
1388
1396
  /**
@@ -1419,15 +1427,19 @@ dwv.ctrl.DataController = function () {
1419
1427
  /**
1420
1428
  * Add a new data.
1421
1429
  *
1430
+ * @param {number} index The index of the data.
1422
1431
  * @param {dwv.image.Image} image The image.
1423
1432
  * @param {object} meta The image meta.
1424
1433
  */
1425
- this.addNew = function (image, meta) {
1434
+ this.addNew = function (index, image, meta) {
1435
+ if (typeof data[index] !== 'undefined') {
1436
+ throw new Error('Index already used in storage: ' + index);
1437
+ }
1426
1438
  // store the new image
1427
- data.push({
1439
+ data[index] = {
1428
1440
  image: image,
1429
1441
  meta: getMetaObject(meta)
1430
- });
1442
+ };
1431
1443
  };
1432
1444
 
1433
1445
  /**
@@ -2225,17 +2237,21 @@ dwv.ctrl.LoadController = function (defaultCharacterSet) {
2225
2237
  };
2226
2238
  loader.onprogress = augmentCallbackEvent(self.onprogress, eventInfo);
2227
2239
  loader.onloaditem = function (event) {
2228
- var isFirstItem = currentLoaders[loadId].isFirstItem;
2229
2240
  var eventInfoItem = {
2230
2241
  loadtype: loadType,
2231
- loadid: loadId,
2232
- isfirstitem: isFirstItem
2242
+ loadid: loadId
2233
2243
  };
2244
+ if (typeof currentLoaders[loadId] !== 'undefined') {
2245
+ eventInfoItem.isfirstitem = currentLoaders[loadId].isFirstItem;
2246
+ }
2234
2247
  if (hasTimepoint) {
2235
2248
  eventInfoItem.timepoint = options.timepoint;
2236
2249
  }
2250
+ // callback
2237
2251
  augmentCallbackEvent(self.onloaditem, eventInfoItem)(event);
2238
- if (isFirstItem) {
2252
+ // update loader
2253
+ if (typeof currentLoaders[loadId] !== 'undefined' &&
2254
+ currentLoaders[loadId].isFirstItem) {
2239
2255
  currentLoaders[loadId].isFirstItem = false;
2240
2256
  }
2241
2257
  };
@@ -3493,7 +3509,7 @@ dwv.dicom.DataReader.prototype.readHex = function (byteOffset) {
3493
3509
  // read and convert to hex string
3494
3510
  var str = this.readUint16(byteOffset).toString(16);
3495
3511
  // return padded
3496
- return '0x0000'.substr(0, 6 - str.length) + str.toUpperCase();
3512
+ return '0x0000'.substring(0, 6 - str.length) + str.toUpperCase();
3497
3513
  };
3498
3514
 
3499
3515
  // namespaces
@@ -3653,7 +3669,7 @@ dwv.dicom.DataWriter = function (buffer, isLittleEndian) {
3653
3669
  */
3654
3670
  this.writeHex = function (byteOffset, str) {
3655
3671
  // remove first two chars and parse
3656
- var value = parseInt(str.substr(2), 16);
3672
+ var value = parseInt(str.substring(2), 16);
3657
3673
  view.setUint16(byteOffset, value, isLittleEndian);
3658
3674
  return byteOffset + Uint16Array.BYTES_PER_ELEMENT;
3659
3675
  };
@@ -4035,15 +4051,17 @@ dwv.dicom.DicomElementsWrapper.prototype.getElementValueAsString = function (
4035
4051
  dayBeginIndex = 8;
4036
4052
  }
4037
4053
  var da = new Date(
4038
- parseInt(daValue.substr(0, 4), 10),
4039
- parseInt(daValue.substr(monthBeginIndex, 2), 10) - 1, // 0-11 range
4040
- parseInt(daValue.substr(dayBeginIndex, 2), 10));
4054
+ parseInt(daValue.substring(0, 4), 10),
4055
+ parseInt(daValue.substring(
4056
+ monthBeginIndex, monthBeginIndex + 2), 10) - 1, // 0-11 range
4057
+ parseInt(daValue.substring(
4058
+ dayBeginIndex, dayBeginIndex + 2), 10));
4041
4059
  str = da.toLocaleDateString();
4042
4060
  } else if (dicomElement.vr === 'TM' && pretty) {
4043
4061
  var tmValue = dicomElement.value[0];
4044
- var tmHour = tmValue.substr(0, 2);
4045
- var tmMinute = tmValue.length >= 4 ? tmValue.substr(2, 2) : '00';
4046
- var tmSeconds = tmValue.length >= 6 ? tmValue.substr(4, 2) : '00';
4062
+ var tmHour = tmValue.substring(0, 2);
4063
+ var tmMinute = tmValue.length >= 4 ? tmValue.substring(2, 4) : '00';
4064
+ var tmSeconds = tmValue.length >= 6 ? tmValue.substring(4, 6) : '00';
4047
4065
  str = tmHour + ':' + tmMinute + ':' + tmSeconds;
4048
4066
  } else {
4049
4067
  var isOtherVR = false;
@@ -4073,9 +4091,9 @@ dwv.dicom.DicomElementsWrapper.prototype.getElementValueAsString = function (
4073
4091
  } else if (isOtherVR) {
4074
4092
  var tmp = dicomElement.value[k].toString(16);
4075
4093
  if (dicomElement.vr === 'OB') {
4076
- tmp = '00'.substr(0, 2 - tmp.length) + tmp;
4094
+ tmp = '00'.substring(0, 2 - tmp.length) + tmp;
4077
4095
  } else {
4078
- tmp = '0000'.substr(0, 4 - tmp.length) + tmp;
4096
+ tmp = '0000'.substring(0, 4 - tmp.length) + tmp;
4079
4097
  }
4080
4098
  valueStr += tmp;
4081
4099
  } else if (typeof dicomElement.value[k] === 'string') {
@@ -4146,9 +4164,9 @@ dwv.dicom.DicomElementsWrapper.prototype.getElementAsString = function (
4146
4164
 
4147
4165
  // (group,element)
4148
4166
  line = '(';
4149
- line += dicomElement.tag.group.substr(2, 5).toLowerCase();
4167
+ line += dicomElement.tag.group.substring(2).toLowerCase();
4150
4168
  line += ',';
4151
- line += dicomElement.tag.element.substr(2, 5).toLowerCase();
4169
+ line += dicomElement.tag.element.substring(2).toLowerCase();
4152
4170
  line += ') ';
4153
4171
  // value representation
4154
4172
  line += dicomElement.vr;
@@ -4450,7 +4468,7 @@ dwv.dicom = dwv.dicom || {};
4450
4468
  * @returns {string} The version of the library.
4451
4469
  */
4452
4470
  dwv.getVersion = function () {
4453
- return '0.30.8';
4471
+ return '0.31.0-beta.0';
4454
4472
  };
4455
4473
 
4456
4474
  /**
@@ -4564,7 +4582,7 @@ dwv.dicom.getReverseOrientation = function (ori) {
4564
4582
 
4565
4583
  var rori = '';
4566
4584
  for (var n = 0; n < ori.length; n++) {
4567
- var o = ori.substr(n, 1);
4585
+ var o = ori.substring(n, n + 1);
4568
4586
  var r = rlabels[o];
4569
4587
  if (r) {
4570
4588
  rori += r;
@@ -4778,7 +4796,7 @@ dwv.dicom.guessTransferSyntax = function (firstDataElement) {
4778
4796
  group !== oEightGroupLittleEndian) {
4779
4797
  throw new Error(
4780
4798
  'Not a valid DICOM file (no magic DICM word found' +
4781
- 'and first element not in 0x0008 group)'
4799
+ ' and first element not in 0x0008 group)'
4782
4800
  );
4783
4801
  }
4784
4802
  // reasonable assumption: 2 uppercase characters => explicit vr
@@ -5346,9 +5364,9 @@ dwv.dicom.DicomParser.prototype.interpretElement = function (
5346
5364
  var stri = raw[i].toString(16);
5347
5365
  var stri1 = raw[i + 1].toString(16);
5348
5366
  var str = '(';
5349
- str += '0000'.substr(0, 4 - stri.length) + stri.toUpperCase();
5367
+ str += '0000'.substring(0, 4 - stri.length) + stri.toUpperCase();
5350
5368
  str += ',';
5351
- str += '0000'.substr(0, 4 - stri1.length) + stri1.toUpperCase();
5369
+ str += '0000'.substring(0, 4 - stri1.length) + stri1.toUpperCase();
5352
5370
  str += ')';
5353
5371
  data.push(str);
5354
5372
  }
@@ -5661,19 +5679,21 @@ dwv.dicom.Tag.prototype.equals2 = function (rhs) {
5661
5679
  /**
5662
5680
  * Get the group-element key used to store DICOM elements.
5663
5681
  *
5664
- * @returns {string} The key.
5682
+ * @returns {string} The key as 'x########'.
5665
5683
  */
5666
5684
  dwv.dicom.Tag.prototype.getKey = function () {
5667
- return 'x' + this.getGroup().substr(2, 6) + this.getElement().substr(2, 6);
5685
+ // group and element are in the '0x####' form
5686
+ return 'x' + this.getGroup().substring(2) + this.getElement().substring(2);
5668
5687
  };
5669
5688
 
5670
5689
  /**
5671
5690
  * Get a simplified group-element key.
5672
5691
  *
5673
- * @returns {string} The key.
5692
+ * @returns {string} The key as '########'.
5674
5693
  */
5675
5694
  dwv.dicom.Tag.prototype.getKey2 = function () {
5676
- return this.getGroup().substr(2, 6) + this.getElement().substr(2, 6);
5695
+ // group and element are in the '0x####' form
5696
+ return this.getGroup().substring(2) + this.getElement().substring(2);
5677
5697
  };
5678
5698
 
5679
5699
  /**
@@ -5682,7 +5702,9 @@ dwv.dicom.Tag.prototype.getKey2 = function () {
5682
5702
  * @returns {string} The name.
5683
5703
  */
5684
5704
  dwv.dicom.Tag.prototype.getGroupName = function () {
5685
- return dwv.dicom.TagGroups[this.getGroup().substr(1)];
5705
+ // group is in the '0x####' form
5706
+ // TagGroups include the x
5707
+ return dwv.dicom.TagGroups[this.getGroup().substring(1)];
5686
5708
  };
5687
5709
 
5688
5710
 
@@ -5693,7 +5715,7 @@ dwv.dicom.Tag.prototype.getGroupName = function () {
5693
5715
  * @returns {object} The DICOM tag.
5694
5716
  */
5695
5717
  dwv.dicom.getTagFromKey = function (key) {
5696
- return new dwv.dicom.Tag(key.substr(1, 4), key.substr(5, 8));
5718
+ return new dwv.dicom.Tag(key.substring(1, 5), key.substring(5, 9));
5697
5719
  };
5698
5720
 
5699
5721
  /**
@@ -5717,7 +5739,8 @@ dwv.dicom.Tag.prototype.isWithVR = function () {
5717
5739
  * ie if its group is an odd number.
5718
5740
  */
5719
5741
  dwv.dicom.Tag.prototype.isPrivate = function () {
5720
- var groupNumber = parseInt(this.getGroup().substr(2, 6), 10);
5742
+ // group is in the '0x####' form
5743
+ var groupNumber = parseInt(this.getGroup().substring(2), 10);
5721
5744
  return groupNumber % 2 === 1;
5722
5745
  };
5723
5746
 
@@ -6521,7 +6544,7 @@ dwv.dicom.DicomWriter.prototype.getBuffer = function (dicomElements) {
6521
6544
  dwv.dicom.checkUnknownVR(element);
6522
6545
 
6523
6546
  // tag group name (remove first 0)
6524
- groupName = dwv.dicom.TagGroups[element.tag.group.substr(1)];
6547
+ groupName = dwv.dicom.TagGroups[element.tag.group.substring(1)];
6525
6548
 
6526
6549
  // prefix
6527
6550
  if (groupName === 'Meta Element') {
@@ -6801,6 +6824,43 @@ dwv.dicom.setElementValue = function (element, value, isImplicit) {
6801
6824
  return size;
6802
6825
  };
6803
6826
 
6827
+ /**
6828
+ * Get the DICOM element from a DICOM tags object.
6829
+ *
6830
+ * @param {object} tags The DICOM tags object.
6831
+ * @returns {object} The DICOM elements and the end offset.
6832
+ */
6833
+ dwv.dicom.getElementsFromJSONTags = function (tags) {
6834
+ // transfer syntax
6835
+ var isImplicit = dwv.dicom.isImplicitTransferSyntax(tags.TransferSyntaxUID);
6836
+ // convert JSON to DICOM element object
6837
+ var keys = Object.keys(tags);
6838
+ var dicomElements = {};
6839
+ var dicomElement;
6840
+ var name;
6841
+ var offset = 128 + 4; // preamble
6842
+ var size;
6843
+ for (var k = 0, len = keys.length; k < len; ++k) {
6844
+ // get the DICOM element definition from its name
6845
+ dicomElement = dwv.dicom.getDicomElement(keys[k]);
6846
+ // set its value
6847
+ size = dwv.dicom.setElementValue(dicomElement, tags[keys[k]], isImplicit);
6848
+ // set offsets
6849
+ offset += dwv.dicom.getDataElementPrefixByteSize(
6850
+ dicomElement.vr, isImplicit);
6851
+ dicomElement.startOffset = offset;
6852
+ offset += size;
6853
+ dicomElement.endOffset = offset;
6854
+ // create the tag group/element key
6855
+ name = new dwv.dicom.Tag(
6856
+ dicomElement.tag.group, dicomElement.tag.element).getKey();
6857
+ // store
6858
+ dicomElements[name] = dicomElement;
6859
+ }
6860
+ // return
6861
+ return {elements: dicomElements, offset: offset};
6862
+ };
6863
+
6804
6864
  /*eslint max-len:0*/
6805
6865
  // namespaces
6806
6866
  var dwv = dwv || {};
@@ -19597,9 +19657,22 @@ dwv.io.DicomDataLoader.prototype.canLoadFile = function (file) {
19597
19657
  * - the url has no 'contentType' and no extension or the extension is 'dcm'
19598
19658
  *
19599
19659
  * @param {string} url The url to check.
19660
+ * @param {object} options Optional url request options.
19600
19661
  * @returns {boolean} True if the url can be loaded.
19601
19662
  */
19602
- dwv.io.DicomDataLoader.prototype.canLoadUrl = function (url) {
19663
+ dwv.io.DicomDataLoader.prototype.canLoadUrl = function (url, options) {
19664
+ // if there are options.requestHeaders, just base check on them
19665
+ if (typeof options !== 'undefined' &&
19666
+ typeof options.requestHeaders !== 'undefined') {
19667
+ // starts with 'application/dicom'
19668
+ var isDicom = function (element) {
19669
+ return element.name === 'Accept' &&
19670
+ dwv.utils.startsWith(element.value, 'application/dicom') &&
19671
+ element.value[18] !== '+';
19672
+ };
19673
+ return typeof options.requestHeaders.find(isDicom) !== 'undefined';
19674
+ }
19675
+
19603
19676
  var urlObjext = dwv.utils.getUrlFromUri(url);
19604
19677
  // extension
19605
19678
  var ext = dwv.utils.getFileExtension(urlObjext.pathname);
@@ -19608,12 +19681,29 @@ dwv.io.DicomDataLoader.prototype.canLoadUrl = function (url) {
19608
19681
  // content type (for wado url)
19609
19682
  var contentType = urlObjext.searchParams.get('contentType');
19610
19683
  var hasContentType = contentType !== null &&
19611
- typeof contentType !== 'undefined';
19684
+ typeof contentType !== 'undefined';
19612
19685
  var hasDicomContentType = (contentType === 'application/dicom');
19613
19686
 
19614
19687
  return hasContentType ? hasDicomContentType : (hasNoExt || hasDcmExt);
19615
19688
  };
19616
19689
 
19690
+ /**
19691
+ * Check if the loader can load the provided memory object.
19692
+ *
19693
+ * @param {object} mem The memory object.
19694
+ * @returns {boolean} True if the object can be loaded.
19695
+ */
19696
+ dwv.io.DicomDataLoader.prototype.canLoadMemory = function (mem) {
19697
+ if (typeof mem['Content-Type'] !== 'undefined' &&
19698
+ mem['Content-Type'] === 'application/dicom') {
19699
+ return true;
19700
+ }
19701
+ if (typeof mem.filename !== 'undefined') {
19702
+ return this.canLoadFile(mem.filename);
19703
+ }
19704
+ return false;
19705
+ };
19706
+
19617
19707
  /**
19618
19708
  * Get the file content type needed by the loader.
19619
19709
  *
@@ -20189,14 +20279,45 @@ dwv.io.JSONTextLoader.prototype.canLoadFile = function (file) {
20189
20279
  * Check if the loader can load the provided url.
20190
20280
  *
20191
20281
  * @param {string} url The url to check.
20282
+ * @param {object} options Optional url request options.
20192
20283
  * @returns {boolean} True if the url can be loaded.
20193
20284
  */
20194
- dwv.io.JSONTextLoader.prototype.canLoadUrl = function (url) {
20285
+ dwv.io.JSONTextLoader.prototype.canLoadUrl = function (url, options) {
20286
+ // if there are options.requestHeader, just base check on them
20287
+ if (typeof options !== 'undefined' &&
20288
+ typeof options.requestHeaders !== 'undefined') {
20289
+ // starts with 'application/json' or 'application/dicom+json
20290
+ var isJson = function (element) {
20291
+ return element.name === 'Accept' &&
20292
+ dwv.utils.startsWith(element.value, 'application/json') &&
20293
+ dwv.utils.startsWith(element.value, 'application/dicom+json');
20294
+ };
20295
+ return typeof options.requestHeaders.find(isJson) !== 'undefined';
20296
+ }
20297
+
20195
20298
  var urlObjext = dwv.utils.getUrlFromUri(url);
20196
20299
  var ext = dwv.utils.getFileExtension(urlObjext.pathname);
20197
20300
  return (ext === 'json');
20198
20301
  };
20199
20302
 
20303
+ /**
20304
+ * Check if the loader can load the provided memory object.
20305
+ *
20306
+ * @param {object} mem The memory object.
20307
+ * @returns {boolean} True if the object can be loaded.
20308
+ */
20309
+ dwv.io.JSONTextLoader.prototype.canLoadMemory = function (mem) {
20310
+ if (typeof mem['Content-Type'] !== 'undefined') {
20311
+ if (mem['Content-Type'].includes('json')) {
20312
+ return true;
20313
+ }
20314
+ }
20315
+ if (typeof mem.filename !== 'undefined') {
20316
+ return this.canLoadFile(mem.filename);
20317
+ }
20318
+ return false;
20319
+ };
20320
+
20200
20321
  /**
20201
20322
  * Get the file content type needed by the loader.
20202
20323
  *
@@ -20459,7 +20580,7 @@ dwv.io.MemoryLoader = function () {
20459
20580
  var foundLoader = false;
20460
20581
  for (var l = 0; l < loaders.length; ++l) {
20461
20582
  loader = loaders[l];
20462
- if (loader.canLoadUrl(dataElement.filename)) {
20583
+ if (loader.canLoadMemory(dataElement)) {
20463
20584
  foundLoader = true;
20464
20585
  // load options
20465
20586
  loader.setOptions({
@@ -20494,7 +20615,7 @@ dwv.io.MemoryLoader = function () {
20494
20615
  for (var i = 0; i < data.length; ++i) {
20495
20616
  dataElement = data[i];
20496
20617
  // check loader
20497
- if (!loader.canLoadFile({name: dataElement.filename})) {
20618
+ if (!loader.canLoadMemory(dataElement)) {
20498
20619
  throw new Error('Input data of different type: ' +
20499
20620
  dataElement.filename);
20500
20621
  }
@@ -20572,6 +20693,214 @@ dwv.io.MemoryLoader.prototype.onabort = function (_event) {};
20572
20693
  var dwv = dwv || {};
20573
20694
  dwv.io = dwv.io || {};
20574
20695
 
20696
+ /**
20697
+ * Multipart data loader.
20698
+ *
20699
+ * @class
20700
+ */
20701
+ dwv.io.MultipartLoader = function () {
20702
+ // closure to self
20703
+ var self = this;
20704
+
20705
+ /**
20706
+ * Loading flag.
20707
+ *
20708
+ * @private
20709
+ * @type {boolean}
20710
+ */
20711
+ var isLoading = false;
20712
+
20713
+ /**
20714
+ * Set the loader options.
20715
+ *
20716
+ * @param {object} _opt The input options.
20717
+ */
20718
+ this.setOptions = function (_opt) {
20719
+ // does nothing
20720
+ };
20721
+
20722
+ /**
20723
+ * Is the load ongoing?
20724
+ *
20725
+ * @returns {boolean} True if loading.
20726
+ */
20727
+ this.isLoading = function () {
20728
+ return isLoading;
20729
+ };
20730
+
20731
+ /**
20732
+ * Load data.
20733
+ *
20734
+ * @param {object} buffer The DICOM buffer.
20735
+ * @param {string} origin The data origin.
20736
+ * @param {number} index The data index.
20737
+ */
20738
+ this.load = function (buffer, origin, index) {
20739
+ // send start event
20740
+ this.onloadstart({
20741
+ source: origin
20742
+ });
20743
+ // set loading flag
20744
+ isLoading = true;
20745
+
20746
+ var memoryIO = new dwv.io.MemoryLoader();
20747
+ // memoryIO.onloadstart: nothing to do
20748
+ memoryIO.onprogress = function (progress) {
20749
+ // add 50% to take into account the un-Multipartping
20750
+ progress.loaded = 50 + progress.loaded / 2;
20751
+ // set data index
20752
+ progress.index = index;
20753
+ self.onprogress(progress);
20754
+ };
20755
+ memoryIO.onloaditem = self.onloaditem;
20756
+ memoryIO.onload = self.onload;
20757
+ memoryIO.onloadend = function (event) {
20758
+ // reset loading flag
20759
+ isLoading = false;
20760
+ // call listeners
20761
+ self.onloadend(event);
20762
+ };
20763
+ memoryIO.onerror = self.onerror;
20764
+ memoryIO.onabort = self.onabort;
20765
+ // launch
20766
+ memoryIO.load(dwv.utils.parseMultipart(buffer));
20767
+ };
20768
+
20769
+ /**
20770
+ * Abort load: pass to listeners.
20771
+ */
20772
+ this.abort = function () {
20773
+ // reset loading flag
20774
+ isLoading = false;
20775
+ // call listeners
20776
+ self.onabort({});
20777
+ self.onloadend({});
20778
+ };
20779
+
20780
+ }; // class MultipartLoader
20781
+
20782
+ /**
20783
+ * Check if the loader can load the provided file.
20784
+ *
20785
+ * @param {object} _file The file to check.
20786
+ * @returns {boolean} True if the file can be loaded.
20787
+ */
20788
+ dwv.io.MultipartLoader.prototype.canLoadFile = function (_file) {
20789
+ return false;
20790
+ };
20791
+
20792
+ /**
20793
+ * Check if the loader can load the provided url.
20794
+ *
20795
+ * @param {string} url The url to check.
20796
+ * @param {object} options The url request options.
20797
+ * @returns {boolean} True if the url can be loaded.
20798
+ */
20799
+ dwv.io.MultipartLoader.prototype.canLoadUrl = function (url, options) {
20800
+ // if there are options.requestHeaders, just base check on them
20801
+ if (typeof options !== 'undefined' &&
20802
+ typeof options.requestHeaders !== 'undefined') {
20803
+ var isMultipart = function (element) {
20804
+ return element.name === 'Accept' &&
20805
+ dwv.utils.startsWith(element.value, 'multipart/related');
20806
+ };
20807
+ return typeof options.requestHeaders.find(isMultipart) !== 'undefined';
20808
+ }
20809
+
20810
+ return false;
20811
+ };
20812
+
20813
+ /**
20814
+ * Check if the loader can load the provided memory object.
20815
+ *
20816
+ * @param {object} _mem The memory object.
20817
+ * @returns {boolean} True if the url can be loaded.
20818
+ */
20819
+ dwv.io.MultipartLoader.prototype.canLoadMemory = function (_mem) {
20820
+ return false;
20821
+ };
20822
+
20823
+ /**
20824
+ * Get the file content type needed by the loader.
20825
+ *
20826
+ * @returns {number} One of the 'dwv.io.fileContentTypes'.
20827
+ */
20828
+ dwv.io.MultipartLoader.prototype.loadFileAs = function () {
20829
+ return dwv.io.fileContentTypes.ArrayBuffer;
20830
+ };
20831
+
20832
+ /**
20833
+ * Get the url content type needed by the loader.
20834
+ *
20835
+ * @returns {number} One of the 'dwv.io.urlContentTypes'.
20836
+ */
20837
+ dwv.io.MultipartLoader.prototype.loadUrlAs = function () {
20838
+ return dwv.io.urlContentTypes.ArrayBuffer;
20839
+ };
20840
+
20841
+ /**
20842
+ * Handle a load start event.
20843
+ * Default does nothing.
20844
+ *
20845
+ * @param {object} _event The load start event.
20846
+ */
20847
+ dwv.io.MultipartLoader.prototype.onloadstart = function (_event) {};
20848
+ /**
20849
+ * Handle a load progress event.
20850
+ * Default does nothing.
20851
+ *
20852
+ * @param {object} _event The progress event.
20853
+ */
20854
+ dwv.io.MultipartLoader.prototype.onprogress = function (_event) {};
20855
+ /**
20856
+ * Handle a load item event.
20857
+ * Default does nothing.
20858
+ *
20859
+ * @param {object} _event The load item event fired
20860
+ * when a file item has been loaded successfully.
20861
+ */
20862
+ dwv.io.MultipartLoader.prototype.onloaditem = function (_event) {};
20863
+ /**
20864
+ * Handle a load event.
20865
+ * Default does nothing.
20866
+ *
20867
+ * @param {object} _event The load event fired
20868
+ * when a file has been loaded successfully.
20869
+ */
20870
+ dwv.io.MultipartLoader.prototype.onload = function (_event) {};
20871
+ /**
20872
+ * Handle an load end event.
20873
+ * Default does nothing.
20874
+ *
20875
+ * @param {object} _event The load end event fired
20876
+ * when a file load has completed, successfully or not.
20877
+ */
20878
+ dwv.io.MultipartLoader.prototype.onloadend = function (_event) {};
20879
+ /**
20880
+ * Handle an error event.
20881
+ * Default does nothing.
20882
+ *
20883
+ * @param {object} _event The error event.
20884
+ */
20885
+ dwv.io.MultipartLoader.prototype.onerror = function (_event) {};
20886
+ /**
20887
+ * Handle an abort event.
20888
+ * Default does nothing.
20889
+ *
20890
+ * @param {object} _event The abort event.
20891
+ */
20892
+ dwv.io.MultipartLoader.prototype.onabort = function (_event) {};
20893
+
20894
+ /**
20895
+ * Add to Loader list.
20896
+ */
20897
+ dwv.io.loaderList = dwv.io.loaderList || [];
20898
+ dwv.io.loaderList.push('MultipartLoader');
20899
+
20900
+ // namespaces
20901
+ var dwv = dwv || {};
20902
+ dwv.io = dwv.io || {};
20903
+
20575
20904
  /**
20576
20905
  * Raw image loader.
20577
20906
  *
@@ -20698,9 +21027,21 @@ dwv.io.RawImageLoader.prototype.canLoadFile = function (file) {
20698
21027
  * Check if the loader can load the provided url.
20699
21028
  *
20700
21029
  * @param {string} url The url to check.
21030
+ * @param {object} options Optional url request options.
20701
21031
  * @returns {boolean} True if the url can be loaded.
20702
21032
  */
20703
- dwv.io.RawImageLoader.prototype.canLoadUrl = function (url) {
21033
+ dwv.io.RawImageLoader.prototype.canLoadUrl = function (url, options) {
21034
+ // if there are options.requestHeaders, just base check on them
21035
+ if (typeof options !== 'undefined' &&
21036
+ typeof options.requestHeaders !== 'undefined') {
21037
+ // starts with 'image/'
21038
+ var isImage = function (element) {
21039
+ return element.name === 'Accept' &&
21040
+ dwv.utils.startsWith(element.value, 'image/');
21041
+ };
21042
+ return typeof options.requestHeaders.find(isImage) !== 'undefined';
21043
+ }
21044
+
20704
21045
  var urlObjext = dwv.utils.getUrlFromUri(url);
20705
21046
  // extension
20706
21047
  var ext = dwv.utils.getFileExtension(urlObjext.pathname);
@@ -20717,6 +21058,19 @@ dwv.io.RawImageLoader.prototype.canLoadUrl = function (url) {
20717
21058
  return hasContentType ? hasImageContentType : hasImageExt;
20718
21059
  };
20719
21060
 
21061
+ /**
21062
+ * Check if the loader can load the provided memory object.
21063
+ *
21064
+ * @param {object} mem The memory object.
21065
+ * @returns {boolean} True if the object can be loaded.
21066
+ */
21067
+ dwv.io.RawImageLoader.prototype.canLoadMemory = function (mem) {
21068
+ if (typeof mem.filename !== 'undefined') {
21069
+ return this.canLoadFile(mem.filename);
21070
+ }
21071
+ return false;
21072
+ };
21073
+
20720
21074
  /**
20721
21075
  * Get the file content type needed by the loader.
20722
21076
  *
@@ -20902,15 +21256,40 @@ dwv.io.RawVideoLoader.prototype.canLoadFile = function (file) {
20902
21256
  * Check if the loader can load the provided url.
20903
21257
  *
20904
21258
  * @param {string} url The url to check.
21259
+ * @param {object} options Optional url request options.
20905
21260
  * @returns {boolean} True if the url can be loaded.
20906
21261
  */
20907
- dwv.io.RawVideoLoader.prototype.canLoadUrl = function (url) {
21262
+ dwv.io.RawVideoLoader.prototype.canLoadUrl = function (url, options) {
21263
+ // if there are options.requestHeaders, just base check on them
21264
+ if (typeof options !== 'undefined' &&
21265
+ typeof options.requestHeaders !== 'undefined') {
21266
+ // starts with 'video/'
21267
+ var isVideo = function (element) {
21268
+ return element.name === 'Accept' &&
21269
+ dwv.utils.startsWith(element.value, 'video/');
21270
+ };
21271
+ return typeof options.requestHeaders.find(isVideo) !== 'undefined';
21272
+ }
21273
+
20908
21274
  var urlObjext = dwv.utils.getUrlFromUri(url);
20909
21275
  var ext = dwv.utils.getFileExtension(urlObjext.pathname);
20910
21276
  return (ext === 'mp4') || (ext === 'ogg') ||
20911
21277
  (ext === 'webm');
20912
21278
  };
20913
21279
 
21280
+ /**
21281
+ * Check if the loader can load the provided memory object.
21282
+ *
21283
+ * @param {object} mem The memory object.
21284
+ * @returns {boolean} True if the object can be loaded.
21285
+ */
21286
+ dwv.io.RawVideoLoader.prototype.canLoadMemory = function (mem) {
21287
+ if (typeof mem.filename !== 'undefined') {
21288
+ return this.canLoadFile(mem.filename);
21289
+ }
21290
+ return false;
21291
+ };
21292
+
20914
21293
  /**
20915
21294
  * Get the file content type needed by the loader.
20916
21295
  *
@@ -21374,8 +21753,8 @@ dwv.io.v01Tov02DrawingsAndDetails = function (inputDrawings) {
21374
21753
  if (drawGroup.name() === 'ruler-group') {
21375
21754
  quant = {
21376
21755
  length: {
21377
- value: parseFloat(textExpr.substr(0, txtLen - 2)),
21378
- unit: textExpr.substr(-2, 2)
21756
+ value: parseFloat(textExpr.substring(0, txtLen - 2)),
21757
+ unit: textExpr.substring(-2)
21379
21758
  }
21380
21759
  };
21381
21760
  textExpr = '{length}';
@@ -21383,8 +21762,8 @@ dwv.io.v01Tov02DrawingsAndDetails = function (inputDrawings) {
21383
21762
  drawGroup.name() === 'rectangle-group') {
21384
21763
  quant = {
21385
21764
  surface: {
21386
- value: parseFloat(textExpr.substr(0, txtLen - 3)),
21387
- unit: textExpr.substr(-3, 3)
21765
+ value: parseFloat(textExpr.substring(0, txtLen - 3)),
21766
+ unit: textExpr.substring(-3)
21388
21767
  }
21389
21768
  };
21390
21769
  textExpr = '{surface}';
@@ -21392,8 +21771,8 @@ dwv.io.v01Tov02DrawingsAndDetails = function (inputDrawings) {
21392
21771
  drawGroup.name() === 'rectangle-group') {
21393
21772
  quant = {
21394
21773
  angle: {
21395
- value: parseFloat(textExpr.substr(0, txtLen - 1)),
21396
- unit: textExpr.substr(-1, 1)
21774
+ value: parseFloat(textExpr.substring(0, txtLen - 1)),
21775
+ unit: textExpr.substring(-1)
21397
21776
  }
21398
21777
  };
21399
21778
  textExpr = '{angle}';
@@ -21786,7 +22165,7 @@ dwv.io.UrlsLoader = function () {
21786
22165
  var foundLoader = false;
21787
22166
  for (var l = 0; l < loaders.length; ++l) {
21788
22167
  loader = loaders[l];
21789
- if (loader.canLoadUrl(dataElement)) {
22168
+ if (loader.canLoadUrl(dataElement, options)) {
21790
22169
  foundLoader = true;
21791
22170
  // load options
21792
22171
  loader.setOptions({
@@ -21854,7 +22233,7 @@ dwv.io.UrlsLoader = function () {
21854
22233
  dataElement = data[i];
21855
22234
 
21856
22235
  // check loader
21857
- if (!loader.canLoadUrl(dataElement)) {
22236
+ if (!loader.canLoadUrl(dataElement, options)) {
21858
22237
  throw new Error('Input url of different type: ' + dataElement);
21859
22238
  }
21860
22239
  /**
@@ -22208,14 +22587,36 @@ dwv.io.ZipLoader.prototype.canLoadFile = function (file) {
22208
22587
  * Check if the loader can load the provided url.
22209
22588
  *
22210
22589
  * @param {string} url The url to check.
22590
+ * @param {object} options Optional url request options.
22211
22591
  * @returns {boolean} True if the url can be loaded.
22212
22592
  */
22213
- dwv.io.ZipLoader.prototype.canLoadUrl = function (url) {
22593
+ dwv.io.ZipLoader.prototype.canLoadUrl = function (url, options) {
22594
+ // if there are options.requestHeaders, just base check on them
22595
+ if (typeof options !== 'undefined' &&
22596
+ typeof options.requestHeaders !== 'undefined') {
22597
+ // starts with 'application/zip'
22598
+ var isZip = function (element) {
22599
+ return element.name === 'Accept' &&
22600
+ dwv.utils.startsWith(element.value, 'application/zip');
22601
+ };
22602
+ return typeof options.requestHeaders.find(isZip) !== 'undefined';
22603
+ }
22604
+
22214
22605
  var urlObjext = dwv.utils.getUrlFromUri(url);
22215
22606
  var ext = dwv.utils.getFileExtension(urlObjext.pathname);
22216
22607
  return (ext === 'zip');
22217
22608
  };
22218
22609
 
22610
+ /**
22611
+ * Check if the loader can load the provided memory object.
22612
+ *
22613
+ * @param {object} _mem The memory object.
22614
+ * @returns {boolean} True if the object can be loaded.
22615
+ */
22616
+ dwv.io.ZipLoader.prototype.canLoadMemory = function (_mem) {
22617
+ return false;
22618
+ };
22619
+
22219
22620
  /**
22220
22621
  * Get the file content type needed by the loader.
22221
22622
  *
@@ -31848,6 +32249,223 @@ dwv.tool.ZoomAndPan.prototype.init = function () {
31848
32249
  var dwv = dwv || {};
31849
32250
  dwv.utils = dwv.utils || {};
31850
32251
 
32252
+ /**
32253
+ * Convert a Uint8Array to a string.
32254
+ *
32255
+ * @param {Uint8Array} arr The array to convert.
32256
+ * @returns {string} The array as string.
32257
+ */
32258
+ dwv.utils.uint8ArrayToString = function (arr) {
32259
+ return String.fromCharCode.apply(String, arr);
32260
+ };
32261
+
32262
+ /**
32263
+ * Array find in a subset of the input array.
32264
+ * Equivalent to: arr.slice(start, end).find(callbackFn)
32265
+ *
32266
+ * @param {Array} arr The input array to search.
32267
+ * @param {Function} callbackFn The find function.
32268
+ * @param {number} start The array start index.
32269
+ * @param {number} end The array end index.
32270
+ * @returns {number} The index where the element was found.
32271
+ */
32272
+ dwv.utils.findInArraySubset = function (arr, callbackFn, start, end) {
32273
+ // check inputs
32274
+ if (typeof start === 'undefined' ||
32275
+ start < 0 ||
32276
+ start >= arr.length
32277
+ ) {
32278
+ start = 0;
32279
+ }
32280
+ if (typeof end === 'undefined' ||
32281
+ end <= start ||
32282
+ end > arr.length) {
32283
+ end = arr.length;
32284
+ }
32285
+ // run
32286
+ for (var i = start; i < end; ++i) {
32287
+ if (callbackFn(arr[i], i, arr)) {
32288
+ return i;
32289
+ }
32290
+ }
32291
+ return;
32292
+ };
32293
+
32294
+ /**
32295
+ * Get a find in array callback.
32296
+ *
32297
+ * @param {Array} arr1 The array to find.
32298
+ * @returns {Function} The find callback function.
32299
+ */
32300
+ dwv.utils.getFindArrayInArrayCallback = function (arr1) {
32301
+ return function (element, index, arr0) {
32302
+ for (var i = 0; i < arr1.length; ++i) {
32303
+ if (arr0[index + i] !== arr1[i]) {
32304
+ return false;
32305
+ }
32306
+ }
32307
+ return true;
32308
+ };
32309
+ };
32310
+
32311
+ /**
32312
+ * Extract each element of a multipart ArrayBuffer.
32313
+ * https://en.wikipedia.org/wiki/MIME#Multipart_messages
32314
+ *
32315
+ * @param {ArrayBuffer} arr The multipart array.
32316
+ * @returns {Array} The multipart parts as an array of object as
32317
+ * {'Content-Type', ..., data} (depending on header tags)
32318
+ */
32319
+ dwv.utils.parseMultipart = function (arr) {
32320
+ var u8Array = new Uint8Array(arr);
32321
+
32322
+ var parts = [];
32323
+ // check input
32324
+ if (u8Array.length === 0) {
32325
+ return parts;
32326
+ }
32327
+
32328
+ // \r\n\r\n
32329
+ var doubleReturnNew = new Uint8Array([0x0d, 0x0a, 0x0d, 0x0a]);
32330
+ var partHeaderEndCb = dwv.utils.getFindArrayInArrayCallback(doubleReturnNew);
32331
+
32332
+ // look for boundary in first part header
32333
+ var partHeaderEndIndex = dwv.utils.findInArraySubset(
32334
+ u8Array, partHeaderEndCb, 0
32335
+ );
32336
+ if (typeof partHeaderEndIndex === 'undefined') {
32337
+ throw new Error('Can\'t find the end of the first multipart header');
32338
+ }
32339
+ var firstPartHeader = u8Array.slice(0, partHeaderEndIndex);
32340
+ // switch to string to use split
32341
+ var lines = dwv.utils.uint8ArrayToString(firstPartHeader).split('\r\n');
32342
+ // boundary should start with '--'
32343
+ var boundaryStr;
32344
+ for (var i = 0; i < lines.length; ++i) {
32345
+ if (lines[i][0] === '-' && lines[i][1] === '-') {
32346
+ boundaryStr = lines[i];
32347
+ break;
32348
+ }
32349
+ }
32350
+ if (typeof boundaryStr === 'undefined') {
32351
+ throw new Error('Can\'t find the boundary between multi-parts');
32352
+ }
32353
+ var boundary = dwv.utils.stringToUint8Array(boundaryStr);
32354
+ var boundaryCb = dwv.utils.getFindArrayInArrayCallback(boundary);
32355
+ var boundaryLen = boundaryStr.length;
32356
+
32357
+ // skip mime header
32358
+ var nextBoundaryIndex = dwv.utils.findInArraySubset(
32359
+ u8Array, boundaryCb, 0
32360
+ );
32361
+
32362
+ // loop through content
32363
+ while (typeof partHeaderEndIndex !== 'undefined') {
32364
+ var part = {};
32365
+
32366
+ // header
32367
+ var partHeader = u8Array.slice(
32368
+ nextBoundaryIndex + boundaryLen, partHeaderEndIndex);
32369
+ // split into object
32370
+ var partHeaderLines =
32371
+ dwv.utils.uint8ArrayToString(partHeader).split('\r\n');
32372
+ for (var l = 0; l < partHeaderLines.length; ++l) {
32373
+ var line = partHeaderLines[l];
32374
+ var semiColonIndex = line.indexOf(':');
32375
+ if (semiColonIndex !== -1) {
32376
+ var key = line.substring(0, semiColonIndex).trim();
32377
+ var val = line.substring(semiColonIndex + 1).trim();
32378
+ part[key] = val;
32379
+ }
32380
+ }
32381
+
32382
+ // find next boundary
32383
+ nextBoundaryIndex = dwv.utils.findInArraySubset(
32384
+ u8Array, boundaryCb, partHeaderEndIndex
32385
+ );
32386
+ // exit if none
32387
+ if (typeof nextBoundaryIndex === 'undefined') {
32388
+ break;
32389
+ }
32390
+
32391
+ // get part
32392
+ // partHeaderEndIndex plus the size of the '\r\n\r\n' separator
32393
+ var dataBeginIndex = partHeaderEndIndex + 4;
32394
+ // nextBoundaryIndex minus the previous '\r\n'
32395
+ var dataEndIndex = nextBoundaryIndex - 2;
32396
+ if (dataBeginIndex < dataEndIndex) {
32397
+ part.data = u8Array.slice(dataBeginIndex, dataEndIndex).buffer;
32398
+ } else {
32399
+ part.data = new Uint8Array();
32400
+ }
32401
+
32402
+ // store part
32403
+ parts.push(part);
32404
+
32405
+ // find next part header end
32406
+ partHeaderEndIndex = dwv.utils.findInArraySubset(
32407
+ u8Array, partHeaderEndCb,
32408
+ nextBoundaryIndex + boundaryLen
32409
+ );
32410
+ }
32411
+
32412
+ return parts;
32413
+ };
32414
+
32415
+ /**
32416
+ * Build a multipart message.
32417
+ * See: https://en.wikipedia.org/wiki/MIME#Multipart_messages
32418
+ * See: https://hg.orthanc-server.com/orthanc-dicomweb/file/tip/Resources/Samples/JavaScript/stow-rs.js
32419
+ *
32420
+ * @param {Array} parts The message parts as an array of object containing
32421
+ * content headers and messages as the data property (as returned by parse).
32422
+ * @param {string} boundary The message boundary.
32423
+ * @returns {Uint8Array} The full multipart message.
32424
+ */
32425
+ dwv.utils.buildMultipart = function (parts, boundary) {
32426
+ var lineBreak = '\r\n';
32427
+ // build headers and calculate size
32428
+ var partsSize = 0;
32429
+ var headers = [];
32430
+ for (var i = 0; i < parts.length; ++i) {
32431
+ var headerStr = '--' + boundary + lineBreak;
32432
+ var partKeys = Object.keys(parts[i]);
32433
+ for (var k = 0; k < partKeys.length; ++k) {
32434
+ var key = partKeys[k];
32435
+ if (key !== 'data') {
32436
+ headerStr += key + ': ' + parts[i][key] + lineBreak;
32437
+ }
32438
+ }
32439
+ headerStr += lineBreak;
32440
+ var header = dwv.utils.stringToUint8Array(headerStr);
32441
+ headers.push(header);
32442
+ partsSize += header.byteLength + parts[i].data.byteLength;
32443
+ }
32444
+ // build trailer
32445
+ var trailerStr = '--' + boundary + '--' + lineBreak;
32446
+ var trailer = dwv.utils.stringToUint8Array(trailerStr);
32447
+
32448
+ // final buffer
32449
+ var buffer = new Uint8Array(partsSize + trailer.byteLength);
32450
+ var offset = 0;
32451
+ // concatenate parts
32452
+ for (var j = 0; j < parts.length; ++j) {
32453
+ buffer.set(headers[j], offset);
32454
+ offset += headers[j].byteLength;
32455
+ buffer.set(new Uint8Array(parts[j].data), offset);
32456
+ offset += parts[j].data.byteLength;
32457
+ }
32458
+ // end buffer with trailer
32459
+ buffer.set(trailer, offset);
32460
+
32461
+ // return
32462
+ return buffer;
32463
+ };
32464
+
32465
+ // namespaces
32466
+ var dwv = dwv || {};
32467
+ dwv.utils = dwv.utils || {};
32468
+
31851
32469
  // example implementation: dcmtk/dcmiod/libsrc/cielabutil.cc
31852
32470
  // https://github.com/DCMTK/dcmtk/blob/DCMTK-3.6.6/dcmiod/libsrc/cielabutil.cc
31853
32471
 
@@ -31870,19 +32488,30 @@ dwv.utils.ybrToRgb = function (y, cb, cr) {
31870
32488
  };
31871
32489
 
31872
32490
  /**
31873
- * Convert a hexadecimal colour to RGB.
32491
+ * Convert a hex color into RGB.
31874
32492
  *
31875
- * @param {string} hexColour The hexadecimal color as '#ab01ef'.
31876
- * @returns {object} RGB equivalent as {r,g,b}.
32493
+ * @param {string} hexStr The hex color as '#ab01ef'.
32494
+ * @returns {object} The RGB values as {r,g,b}.
31877
32495
  */
31878
- dwv.utils.hexToRgb = function (hexColour) {
32496
+ dwv.utils.hexToRgb = function (hexStr) {
31879
32497
  return {
31880
- r: parseInt(hexColour.substr(1, 2), 16),
31881
- g: parseInt(hexColour.substr(3, 2), 16),
31882
- b: parseInt(hexColour.substr(5, 2), 16)
32498
+ r: parseInt(hexStr.substring(1, 3), 16),
32499
+ g: parseInt(hexStr.substring(3, 5), 16),
32500
+ b: parseInt(hexStr.substring(5, 7), 16)
31883
32501
  };
31884
32502
  };
31885
32503
 
32504
+ /**
32505
+ * Convert RGB to its hex equivalent.
32506
+ *
32507
+ * @param {object} rgb The RGB object as {r,g,b}.
32508
+ * @returns {string} A string representing the hex color as '#ab01ef'.
32509
+ */
32510
+ dwv.utils.rgbToHex = function (rgb) {
32511
+ return '#' +
32512
+ ((1 << 24) + (rgb.r << 16) + (rgb.g << 8) + rgb.b).toString(16).slice(1);
32513
+ };
32514
+
31886
32515
  /**
31887
32516
  * Get the brightness of a RGB colour: calculates
31888
32517
  * the luma (Y) of the YIQ colour space.
@@ -31957,7 +32586,7 @@ dwv.utils.labToUintLab = function (triplet) {
31957
32586
  };
31958
32587
 
31959
32588
  /**
31960
- * CIE Standard Illuminant D65
32589
+ * CIE Standard Illuminant D65, standard 2° observer.
31961
32590
  *
31962
32591
  * @see https://en.wikipedia.org/wiki/Illuminant_D65
31963
32592
  */
@@ -32033,7 +32662,7 @@ dwv.utils.ciexyzToCielab = function (triplet) {
32033
32662
  /**
32034
32663
  * Convert CIE XYZ to sRGB.
32035
32664
  *
32036
- * @see https://en.wikipedia.org/wiki/SRGB#The_forward_transformation_(CIE_XYZ_to_sRGB)
32665
+ * @see https://en.wikipedia.org/wiki/SRGB#From_CIE_XYZ_to_sRGB
32037
32666
  * @param {object} triplet CIE XYZ triplet as {x,y,z}.
32038
32667
  * @returns {object} sRGB triplet as {r,g,b}.
32039
32668
  */
@@ -32046,7 +32675,8 @@ dwv.utils.ciexyzToSrgb = function (triplet) {
32046
32675
  // 0.416666667 = 1 / 2.4
32047
32676
  res = 1.055 * Math.pow(x, 0.416666667) - 0.055;
32048
32677
  }
32049
- return res;
32678
+ // clip [0,1]
32679
+ return Math.min(1, Math.max(0, res));
32050
32680
  };
32051
32681
 
32052
32682
  var x = triplet.x / 100;
@@ -32063,7 +32693,7 @@ dwv.utils.ciexyzToSrgb = function (triplet) {
32063
32693
  /**
32064
32694
  * Convert sRGB to CIE XYZ.
32065
32695
  *
32066
- * @see https://en.wikipedia.org/wiki/SRGB#The_forward_transformation_(CIE_XYZ_to_sRGB)
32696
+ * @see https://en.wikipedia.org/wiki/SRGB#From_sRGB_to_CIE_XYZ
32067
32697
  * @param {object} triplet sRGB triplet as {r,g,b}.
32068
32698
  * @returns {object} CIE XYZ triplet as {x,y,z}.
32069
32699
  */
@@ -32405,9 +33035,11 @@ dwv.env.check = function () {
32405
33035
 
32406
33036
  // check string startsWith
32407
33037
  if (!String.prototype.startsWith) {
32408
- String.prototype.startsWith = function (search, pos) {
32409
- return this.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search;
32410
- };
33038
+ Object.defineProperty(String.prototype, 'startsWith', {
33039
+ value: function (search, rawPos) {
33040
+ return dwv.utils.startsWith(this, search, rawPos);
33041
+ }
33042
+ });
32411
33043
  }
32412
33044
  };
32413
33045
 
@@ -32580,7 +33212,7 @@ dwv.i18nPage = function () {
32580
33212
  * @returns {string} The path to the locale resource.
32581
33213
  */
32582
33214
  dwv.i18nGetLocalePath = function (filename) {
32583
- var lng = i18next.language.substr(0, 2);
33215
+ var lng = i18next.language.substring(0, 2);
32584
33216
  return dwv.i18nLocalesPath +
32585
33217
  '/locales/' + lng + '/' + filename;
32586
33218
  };
@@ -32593,7 +33225,7 @@ dwv.i18nGetLocalePath = function (filename) {
32593
33225
  * @returns {string} The path to the locale resource.
32594
33226
  */
32595
33227
  dwv.i18nGetFallbackLocalePath = function (filename) {
32596
- var lng = i18next.languages[i18next.languages.length - 1].substr(0, 2);
33228
+ var lng = i18next.languages[i18next.languages.length - 1].substring(0, 2);
32597
33229
  return dwv.i18nLocalesPath +
32598
33230
  '/locales/' + lng + '/' + filename;
32599
33231
  };
@@ -33835,19 +34467,37 @@ dwv.utils.capitaliseFirstLetter = function (string) {
33835
34467
  return res;
33836
34468
  };
33837
34469
 
34470
+ /**
34471
+ * Check if a string starts with the input element.
34472
+ *
34473
+ * @param {string} str The input string.
34474
+ * @param {string} search The searched start.
34475
+ * @param {number} rawPos The position in this string at which to begin
34476
+ * searching for searchString. Defaults to 0.
34477
+ * @returns {boolean} True if the input string starts with the searched string.
34478
+ */
34479
+ dwv.utils.startsWith = function (str, search, rawPos) {
34480
+ if (typeof str === 'undefined' || str === null ||
34481
+ typeof search === 'undefined' || search === null) {
34482
+ return false;
34483
+ }
34484
+ var pos = rawPos > 0 ? rawPos | 0 : 0;
34485
+ return str.substring(pos, pos + search.length) === search;
34486
+ };
34487
+
33838
34488
  /**
33839
34489
  * Check if a string ends with the input element.
33840
34490
  *
33841
34491
  * @param {string} str The input string.
33842
- * @param {string} end The searched ending.
33843
- * @returns {boolean} True if the input string ends with the seached ending.
34492
+ * @param {string} search The searched ending.
34493
+ * @returns {boolean} True if the input string ends with the searched string.
33844
34494
  */
33845
- dwv.utils.endsWith = function (str, end) {
34495
+ dwv.utils.endsWith = function (str, search) {
33846
34496
  if (typeof str === 'undefined' || str === null ||
33847
- typeof end === 'undefined' || end === null) {
34497
+ typeof search === 'undefined' || search === null) {
33848
34498
  return false;
33849
34499
  }
33850
- return str.substr(str.length - end.length) === end;
34500
+ return str.substring(str.length - search.length) === search;
33851
34501
  };
33852
34502
 
33853
34503
  /**
@@ -34029,6 +34679,20 @@ dwv.utils.getFileExtension = function (filePath) {
34029
34679
  return ext;
34030
34680
  };
34031
34681
 
34682
+ /**
34683
+ * Convert a string to a Uint8Array.
34684
+ *
34685
+ * @param {string} str The string to convert.
34686
+ * @returns {Uint8Array} The Uint8Array.
34687
+ */
34688
+ dwv.utils.stringToUint8Array = function (str) {
34689
+ var arr = new Uint8Array(str.length);
34690
+ for (var i = 0, leni = str.length; i < leni; i++) {
34691
+ arr[i] = str.charCodeAt(i);
34692
+ }
34693
+ return arr;
34694
+ };
34695
+
34032
34696
  // namespaces
34033
34697
  var dwv = dwv || {};
34034
34698
  dwv.utils = dwv.utils || {};
@@ -34404,13 +35068,13 @@ dwv.utils.splitUri = function (uri) {
34404
35068
  var sepIndex = null;
34405
35069
  if (uri && (sepIndex = uri.indexOf('?')) !== -1) {
34406
35070
  // base: before the '?'
34407
- result.base = uri.substr(0, sepIndex);
35071
+ result.base = uri.substring(0, sepIndex);
34408
35072
  // query : after the '?' and until possible '#'
34409
35073
  var hashIndex = uri.indexOf('#');
34410
35074
  if (hashIndex === -1) {
34411
35075
  hashIndex = uri.length;
34412
35076
  }
34413
- var query = uri.substr(sepIndex + 1, (hashIndex - 1 - sepIndex));
35077
+ var query = uri.substring(sepIndex + 1, hashIndex);
34414
35078
  // split key/value pairs of the query
34415
35079
  result.query = dwv.utils.splitKeyValueString(query);
34416
35080
  }
@@ -34443,16 +35107,19 @@ dwv.utils.getUriQuery = function (uri) {
34443
35107
  * or encoded URI with base and key value/pairs:
34444
35108
  * [dwv root]?input=encodeURIComponent([root]?key0=value0&key1=value1)
34445
35109
  *
34446
- * @param {string} query The query part to the input URI.
34447
- * @param {Function} callback The function to call with the decoded file urls.
35110
+ * @param {string} query The query part to the input URI.
35111
+ * @param {Function} callback The function to call with the decoded file urls.
35112
+ * @param {object} options Optional url request options.
34448
35113
  */
34449
- dwv.utils.decodeQuery = function (query, callback) {
35114
+ dwv.utils.decodeQuery = function (query, callback, options) {
34450
35115
  // manifest
34451
35116
  if (query.type && query.type === 'manifest') {
34452
35117
  dwv.utils.decodeManifestQuery(query, callback);
34453
35118
  } else {
34454
35119
  // default case: encoded URI with base and key/value pairs
34455
- callback(dwv.utils.decodeKeyValueUri(query.input, query.dwvReplaceMode));
35120
+ callback(
35121
+ dwv.utils.decodeKeyValueUri(query.input, query.dwvReplaceMode),
35122
+ options);
34456
35123
  }
34457
35124
  };
34458
35125
 
@@ -34625,12 +35292,13 @@ dwv.utils.decodeManifest = function (manifest, nslices) {
34625
35292
  *
34626
35293
  * @param {string} uri The input uri, for example: 'window.location.href'.
34627
35294
  * @param {dwv.App} app The associated app that handles the load.
35295
+ * @param {object} options Optional url request options.
34628
35296
  */
34629
- dwv.utils.loadFromUri = function (uri, app) {
35297
+ dwv.utils.loadFromUri = function (uri, app, options) {
34630
35298
  var query = dwv.utils.getUriQuery(uri);
34631
35299
  // check query
34632
35300
  if (query && typeof query.input !== 'undefined') {
34633
- dwv.utils.loadFromQuery(query, app);
35301
+ dwv.utils.loadFromQuery(query, app, options);
34634
35302
  }
34635
35303
  // no else to allow for empty uris
34636
35304
  };
@@ -34640,10 +35308,11 @@ dwv.utils.loadFromUri = function (uri, app) {
34640
35308
  *
34641
35309
  * @param {object} query A query derived from an uri.
34642
35310
  * @param {object} app The associated app that handles the load.
35311
+ * @param {object} options Optional url request options.
34643
35312
  */
34644
- dwv.utils.loadFromQuery = function (query, app) {
35313
+ dwv.utils.loadFromQuery = function (query, app, options) {
34645
35314
  // load base
34646
- dwv.utils.decodeQuery(query, app.loadURLs);
35315
+ dwv.utils.decodeQuery(query, app.loadURLs, options);
34647
35316
  // optional display state
34648
35317
  if (typeof query.state !== 'undefined') {
34649
35318
  var onLoadEnd = function (/*event*/) {