Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | 1x 117x 117x 117x 117x 1x 1x 116x 116x 116x 116x 120x 120x 3x 117x 117x 117x 117x 117x 117x 26x 26x 26x 94x 94x 94x 94x 91x 94x 1x 1x 1x 5x 1x 95x 3x 3x 3x 132x 94x 132x | import cache from '../cache/cache'; import Events from '../enums/Events'; import eventTarget from '../eventTarget'; import { triggerEvent } from '../utilities'; import { IImage, ImageLoaderFn, IImageLoadObject, EventTypes } from '../types'; import imageLoadPoolManager from '../requestPool/imageLoadPoolManager'; export interface ImageLoaderOptions { priority: number; requestType: string; additionalDetails?: Record<string, unknown>; } /** * This module deals with ImageLoaders, loading images and caching images */ const imageLoaders = {}; let unknownImageLoader; /** * Loads 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 imageId - A Cornerstone Image Object's imageId * @param Options - to be passed to the Image Loader * * @returns - An Object which can be used to act after an image is loaded or loading fails */ function loadImageFromImageLoader( imageId: string, options: ImageLoaderOptions ): IImageLoadObject { // Extract the image loader scheme: wadors:https://image1 => wadors const colonIndex = imageId.indexOf(':'); const scheme = imageId.substring(0, colonIndex); const loader = imageLoaders[scheme]; if (loader === undefined || loader === null) { Eif (unknownImageLoader !== undefined) { return unknownImageLoader(imageId); } throw new Error('loadImageFromImageLoader: no image loader for imageId'); } // Load using the registered loader const imageLoadObject = loader(imageId, options); // Broadcast an image loaded event once the image is loaded imageLoadObject.promise.then( function (image) { triggerEvent(eventTarget, Events.IMAGE_LOADED, { image }); }, function (error) { const errorObject: EventTypes.ImageLoadedFailedEventDetail = { imageId, error, }; triggerEvent(eventTarget, Events.IMAGE_LOAD_FAILED, errorObject); } ); return imageLoadObject; } /** * Gets the imageLoadObject by 1) Looking in to the cache to see if the * imageLoadObject has already been cached, 2) Checks inside the volume cache * to see if there is a volume that contains the same imageURI for the requested * imageID 3) Checks inside the imageCache for similar imageURI that might have * been stored as a result of decaching a volume 4) Finally if none were found * it request it from the registered imageLoaders. * * @param imageId - A Cornerstone Image Object's imageId * @param options - Options to be passed to the Image Loader * * @returns An Object which can be used to act after an image is loaded or loading fails */ function loadImageFromCacheOrVolume( imageId: string, options: ImageLoaderOptions ): IImageLoadObject { // 1. Check inside the image cache for imageId let imageLoadObject = cache.getImageLoadObject(imageId); if (imageLoadObject !== undefined) { return imageLoadObject; } // 2. Check if there exists a volume in the cache containing the imageId, // we copy the pixelData over. const cachedVolumeInfo = cache.getVolumeContainingImageId(imageId); Iif (cachedVolumeInfo && cachedVolumeInfo.volume.loadStatus.loaded) { // 2.1 Convert the volume at the specific slice to a cornerstoneImage object. // this will copy the pixel data over. const { volume, imageIdIndex } = cachedVolumeInfo; imageLoadObject = volume.convertToCornerstoneImage(imageId, imageIdIndex); return imageLoadObject; } // 3. If no volume found, we search inside the imageCache for the imageId // that has the same URI which had been cached if the volume was converted // to an image const cachedImage = cache.getCachedImageBasedOnImageURI(imageId); Iif (cachedImage) { imageLoadObject = cachedImage.imageLoadObject; return imageLoadObject; } // 4. if not in image cache nor inside the volume cache, we request the // image loaders to load it imageLoadObject = loadImageFromImageLoader(imageId, options); 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 imageId - A Cornerstone Image Object's imageId * @param options - Options to be passed to the Image Loader * * @returns An Object which can be used to act after an image is loaded or loading fails */ export function loadImage( imageId: string, options: ImageLoaderOptions = { priority: 0, requestType: 'prefetch' } ): Promise<IImage> { Iif (imageId === undefined) { throw new Error('loadImage: parameter imageId must not be undefined'); } return loadImageFromCacheOrVolume(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 imageId - A Cornerstone Image Object's imageId * @param options - Options to be passed to the Image Loader * * @returns Image Loader Object */ export function loadAndCacheImage( imageId: string, options: ImageLoaderOptions = { priority: 0, requestType: 'prefetch' } ): Promise<IImage> { Iif (imageId === undefined) { throw new Error( 'loadAndCacheImage: parameter imageId must not be undefined' ); } const imageLoadObject = loadImageFromCacheOrVolume(imageId, options); // if not inside cache, store it if (!cache.getImageLoadObject(imageId)) { cache.putImageLoadObject(imageId, imageLoadObject).catch((err) => { console.warn(err); }); } return imageLoadObject.promise; } /** * Load and cache a list of imageIds * * @param imageIds - list of imageIds * @param options - options for loader * */ export function loadAndCacheImages( imageIds: Array<string>, options: ImageLoaderOptions = { priority: 0, requestType: 'prefetch' } ): Promise<IImage>[] { Iif (!imageIds || imageIds.length === 0) { throw new Error( 'loadAndCacheImages: parameter imageIds must be list of image Ids' ); } const allPromises = imageIds.map((imageId) => { return loadAndCacheImage(imageId, options); }); return allPromises; } /** * Removes the imageId from the request pool manager and executes the `cancel` * function if it exists. * * @param imageId - A Cornerstone Image Object's imageId * */ export function cancelLoadImage(imageId: string): void { const filterFunction = ({ additionalDetails }) => { if (additionalDetails.imageId) { return additionalDetails.imageId !== imageId; } // for volumes return true; }; // Instruct the request pool manager to filter queued // requests to ensure requests we no longer need are // no longer sent. imageLoadPoolManager.filterRequests(filterFunction); // TODO: Cancel decoding and retrieval as well (somehow?) // cancel image loading if in progress const imageLoadObject = cache.getImageLoadObject(imageId); if (imageLoadObject) { imageLoadObject.cancelFn(); } } /** * Removes the imageIds from the request pool manager and calls the `cancel` * function if it exists. * * @param imageIds - Array of Cornerstone Image Object's imageIds * */ export function cancelLoadImages(imageIds: Array<string>): void { imageIds.forEach((imageId) => cancelLoadImage(imageId)); } /** * Removes all the ongoing image loads by calling the `cancel` method on each * imageLoadObject. If no `cancel` method is available, it will be ignored. * */ export function cancelLoadAll(): void { const requestPool = imageLoadPoolManager.getRequestPool(); Object.keys(requestPool).forEach((type: string) => { const requests = requestPool[type]; Object.keys(requests).forEach((priority) => { const requestDetails = requests[priority].pop(); const { imageId, volumeId } = requestDetails.additionalDetails; let loadObject; if (imageId) { loadObject = cache.getImageLoadObject(imageId); } else if (volumeId) { loadObject = cache.getVolumeLoadObject(volumeId); } if (loadObject) { loadObject.cancel(); } }); // resetting the pool types to be empty imageLoadPoolManager.clearRequestStack(type); // TODO: Clear retrieval and decoding queues as well }); } /** * Registers an imageLoader plugin with cornerstone for the specified scheme * * @param scheme - The scheme to use for this image loader (e.g. 'dicomweb', 'wadouri', 'http') * @param imageLoader - A Cornerstone Image Loader function */ export function registerImageLoader( scheme: string, imageLoader: ImageLoaderFn ): void { imageLoaders[scheme] = imageLoader; } /** * Registers a new unknownImageLoader and returns the previous one * * @param imageLoader - A Cornerstone Image Loader * * @returns The previous Unknown Image Loader */ export function registerUnknownImageLoader( imageLoader: ImageLoaderFn ): ImageLoaderFn { const oldImageLoader = unknownImageLoader; unknownImageLoader = imageLoader; return oldImageLoader; } /** * Removes all registered and unknown image loaders. This should be called * when the application is unmounted to prevent memory leaks. * */ export function unregisterAllImageLoaders(): void { Object.keys(imageLoaders).forEach( (imageLoader) => delete imageLoaders[imageLoader] ); unknownImageLoader = undefined; } |