forked from cornerstonejs/cornerstone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageLoader.js
More file actions
137 lines (111 loc) · 4.17 KB
/
Copy pathimageLoader.js
File metadata and controls
137 lines (111 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { getImageLoadObject, putImageLoadObject } from './imageCache.js';
import EVENTS, { events } from './events.js';
import triggerEvent from './triggerEvent.js';
/**
* This module deals with ImageLoaders, loading images and caching images
* @module ImageLoader
*/
const imageLoaders = {};
let unknownImageLoader;
/**
* Load an image using a registered Cornerstone Image Loader.
*
* The image loader that is used will be
* determined by the image loader scheme matching against the imageId.
*
* @param {String} imageId A Cornerstone Image Object's imageId
* @param {Object} [options] Options to be passed to the Image Loader
*
* @returns {ImageLoadObject} An Object which can be used to act after an image is loaded or loading fails
* @memberof ImageLoader
*/
function loadImageFromImageLoader (imageId, options) {
const colonIndex = imageId.indexOf(':');
const scheme = imageId.substring(0, colonIndex);
const loader = imageLoaders[scheme];
if (loader === undefined || loader === null) {
if (unknownImageLoader !== undefined) {
return unknownImageLoader(imageId);
}
throw new Error('loadImageFromImageLoader: no image loader for imageId');
}
const imageLoadObject = loader(imageId, options);
// Broadcast an image loaded event once the image is loaded
imageLoadObject.promise.then(function (image) {
triggerEvent(events, EVENTS.IMAGE_LOADED, { image });
}, function (error) {
const errorObject = {
imageId,
error
};
triggerEvent(events, EVENTS.IMAGE_LOAD_FAILED, errorObject);
});
return imageLoadObject;
}
/**
* Loads an image given an imageId and optional priority and returns a promise which will resolve to
* the loaded image object or fail if an error occurred. The loaded image is not stored in the cache.
*
* @param {String} imageId A Cornerstone Image Object's imageId
* @param {Object} [options] Options to be passed to the Image Loader
*
* @returns {ImageLoadObject} An Object which can be used to act after an image is loaded or loading fails
* @memberof ImageLoader
*/
export function loadImage (imageId, options) {
if (imageId === undefined) {
throw new Error('loadImage: parameter imageId must not be undefined');
}
const imageLoadObject = getImageLoadObject(imageId);
if (imageLoadObject !== undefined) {
return imageLoadObject.promise;
}
return loadImageFromImageLoader(imageId, options).promise;
}
//
/**
* Loads an image given an imageId and optional priority and returns a promise which will resolve to
* the loaded image object or fail if an error occurred. The image is stored in the cache.
*
* @param {String} imageId A Cornerstone Image Object's imageId
* @param {Object} [options] Options to be passed to the Image Loader
*
* @returns {ImageLoadObject} Image Loader Object
* @memberof ImageLoader
*/
export function loadAndCacheImage (imageId, options) {
if (imageId === undefined) {
throw new Error('loadAndCacheImage: parameter imageId must not be undefined');
}
let imageLoadObject = getImageLoadObject(imageId);
if (imageLoadObject !== undefined) {
return imageLoadObject.promise;
}
imageLoadObject = loadImageFromImageLoader(imageId, options);
putImageLoadObject(imageId, imageLoadObject);
return imageLoadObject.promise;
}
/**
* Registers an imageLoader plugin with cornerstone for the specified scheme
*
* @param {String} scheme The scheme to use for this image loader (e.g. 'dicomweb', 'wadouri', 'http')
* @param {Function} imageLoader A Cornerstone Image Loader function
* @returns {void}
* @memberof ImageLoader
*/
export function registerImageLoader (scheme, imageLoader) {
imageLoaders[scheme] = imageLoader;
}
/**
* Registers a new unknownImageLoader and returns the previous one
*
* @param {Function} imageLoader A Cornerstone Image Loader
*
* @returns {Function|Undefined} The previous Unknown Image Loader
* @memberof ImageLoader
*/
export function registerUnknownImageLoader (imageLoader) {
const oldImageLoader = unknownImageLoader;
unknownImageLoader = imageLoader;
return oldImageLoader;
}