@cornerstonejs/core 0.36.2 → 0.36.4

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.
Files changed (233) hide show
  1. package/dist/cjs/loaders/volumeLoader.d.ts +1 -0
  2. package/dist/cjs/loaders/volumeLoader.js +5 -1
  3. package/dist/cjs/loaders/volumeLoader.js.map +1 -1
  4. package/dist/cjs/utilities/getSliceRange.js +3 -1
  5. package/dist/cjs/utilities/getSliceRange.js.map +1 -1
  6. package/dist/cjs/utilities/getTargetVolumeAndSpacingInNormalDir.js +7 -0
  7. package/dist/cjs/utilities/getTargetVolumeAndSpacingInNormalDir.js.map +1 -1
  8. package/dist/esm/loaders/volumeLoader.d.ts +1 -0
  9. package/dist/esm/loaders/volumeLoader.js +3 -0
  10. package/dist/esm/loaders/volumeLoader.js.map +1 -1
  11. package/dist/esm/utilities/getSliceRange.js +3 -1
  12. package/dist/esm/utilities/getSliceRange.js.map +1 -1
  13. package/dist/esm/utilities/getTargetVolumeAndSpacingInNormalDir.js +7 -0
  14. package/dist/esm/utilities/getTargetVolumeAndSpacingInNormalDir.js.map +1 -1
  15. package/dist/umd/index.js +1 -1
  16. package/dist/umd/index.js.map +1 -1
  17. package/package.json +4 -3
  18. package/src/RenderingEngine/BaseVolumeViewport.ts +847 -0
  19. package/src/RenderingEngine/RenderingEngine.ts +1364 -0
  20. package/src/RenderingEngine/StackViewport.ts +2690 -0
  21. package/src/RenderingEngine/Viewport.ts +1244 -0
  22. package/src/RenderingEngine/VolumeViewport.ts +420 -0
  23. package/src/RenderingEngine/VolumeViewport3D.ts +42 -0
  24. package/src/RenderingEngine/getRenderingEngine.ts +34 -0
  25. package/src/RenderingEngine/helpers/addVolumesToViewports.ts +52 -0
  26. package/src/RenderingEngine/helpers/cpuFallback/colors/colormap.ts +343 -0
  27. package/src/RenderingEngine/helpers/cpuFallback/colors/index.ts +4 -0
  28. package/src/RenderingEngine/helpers/cpuFallback/colors/lookupTable.ts +469 -0
  29. package/src/RenderingEngine/helpers/cpuFallback/drawImageSync.ts +58 -0
  30. package/src/RenderingEngine/helpers/cpuFallback/rendering/calculateTransform.ts +136 -0
  31. package/src/RenderingEngine/helpers/cpuFallback/rendering/canvasToPixel.ts +25 -0
  32. package/src/RenderingEngine/helpers/cpuFallback/rendering/computeAutoVoi.ts +47 -0
  33. package/src/RenderingEngine/helpers/cpuFallback/rendering/correctShift.ts +38 -0
  34. package/src/RenderingEngine/helpers/cpuFallback/rendering/createViewport.ts +64 -0
  35. package/src/RenderingEngine/helpers/cpuFallback/rendering/doesImageNeedToBeRendered.ts +36 -0
  36. package/src/RenderingEngine/helpers/cpuFallback/rendering/fitToWindow.ts +22 -0
  37. package/src/RenderingEngine/helpers/cpuFallback/rendering/generateColorLUT.ts +60 -0
  38. package/src/RenderingEngine/helpers/cpuFallback/rendering/generateLut.ts +83 -0
  39. package/src/RenderingEngine/helpers/cpuFallback/rendering/getDefaultViewport.ts +88 -0
  40. package/src/RenderingEngine/helpers/cpuFallback/rendering/getImageFitScale.ts +52 -0
  41. package/src/RenderingEngine/helpers/cpuFallback/rendering/getImageSize.ts +55 -0
  42. package/src/RenderingEngine/helpers/cpuFallback/rendering/getLut.ts +53 -0
  43. package/src/RenderingEngine/helpers/cpuFallback/rendering/getModalityLut.ts +55 -0
  44. package/src/RenderingEngine/helpers/cpuFallback/rendering/getTransform.ts +17 -0
  45. package/src/RenderingEngine/helpers/cpuFallback/rendering/getVOILut.ts +74 -0
  46. package/src/RenderingEngine/helpers/cpuFallback/rendering/initializeRenderCanvas.ts +37 -0
  47. package/src/RenderingEngine/helpers/cpuFallback/rendering/lutMatches.ts +21 -0
  48. package/src/RenderingEngine/helpers/cpuFallback/rendering/now.ts +13 -0
  49. package/src/RenderingEngine/helpers/cpuFallback/rendering/pixelToCanvas.ts +22 -0
  50. package/src/RenderingEngine/helpers/cpuFallback/rendering/renderColorImage.ts +193 -0
  51. package/src/RenderingEngine/helpers/cpuFallback/rendering/renderGrayscaleImage.ts +166 -0
  52. package/src/RenderingEngine/helpers/cpuFallback/rendering/renderPseudoColorImage.ts +203 -0
  53. package/src/RenderingEngine/helpers/cpuFallback/rendering/resetCamera.ts +32 -0
  54. package/src/RenderingEngine/helpers/cpuFallback/rendering/resize.ts +109 -0
  55. package/src/RenderingEngine/helpers/cpuFallback/rendering/saveLastRendered.ts +36 -0
  56. package/src/RenderingEngine/helpers/cpuFallback/rendering/setDefaultViewport.ts +17 -0
  57. package/src/RenderingEngine/helpers/cpuFallback/rendering/setToPixelCoordinateSystem.ts +32 -0
  58. package/src/RenderingEngine/helpers/cpuFallback/rendering/storedColorPixelDataToCanvasImageData.ts +58 -0
  59. package/src/RenderingEngine/helpers/cpuFallback/rendering/storedPixelDataToCanvasImageData.ts +76 -0
  60. package/src/RenderingEngine/helpers/cpuFallback/rendering/storedPixelDataToCanvasImageDataColorLUT.ts +60 -0
  61. package/src/RenderingEngine/helpers/cpuFallback/rendering/storedPixelDataToCanvasImageDataPET.ts +50 -0
  62. package/src/RenderingEngine/helpers/cpuFallback/rendering/storedPixelDataToCanvasImageDataPseudocolorLUT.ts +66 -0
  63. package/src/RenderingEngine/helpers/cpuFallback/rendering/storedPixelDataToCanvasImageDataPseudocolorLUTPET.ts +68 -0
  64. package/src/RenderingEngine/helpers/cpuFallback/rendering/storedPixelDataToCanvasImageDataRGBA.ts +81 -0
  65. package/src/RenderingEngine/helpers/cpuFallback/rendering/storedRGBAPixelDataToCanvasImageData.ts +56 -0
  66. package/src/RenderingEngine/helpers/cpuFallback/rendering/transform.ts +126 -0
  67. package/src/RenderingEngine/helpers/cpuFallback/rendering/validator.ts +31 -0
  68. package/src/RenderingEngine/helpers/createVolumeActor.ts +103 -0
  69. package/src/RenderingEngine/helpers/createVolumeMapper.ts +37 -0
  70. package/src/RenderingEngine/helpers/getOrCreateCanvas.ts +58 -0
  71. package/src/RenderingEngine/helpers/index.ts +15 -0
  72. package/src/RenderingEngine/helpers/isRgbaSourceRgbDest.ts +1 -0
  73. package/src/RenderingEngine/helpers/setDefaultVolumeVOI.ts +227 -0
  74. package/src/RenderingEngine/helpers/setVolumesForViewports.ts +52 -0
  75. package/src/RenderingEngine/helpers/viewportTypeToViewportClass.ts +14 -0
  76. package/src/RenderingEngine/helpers/viewportTypeUsesCustomRenderingPipeline.ts +7 -0
  77. package/src/RenderingEngine/helpers/volumeNewImageEventDispatcher.ts +75 -0
  78. package/src/RenderingEngine/index.ts +23 -0
  79. package/src/RenderingEngine/renderingEngineCache.ts +43 -0
  80. package/src/RenderingEngine/vtkClasses/index.js +11 -0
  81. package/src/RenderingEngine/vtkClasses/vtkOffscreenMultiRenderWindow.js +149 -0
  82. package/src/RenderingEngine/vtkClasses/vtkSharedVolumeMapper.js +52 -0
  83. package/src/RenderingEngine/vtkClasses/vtkSlabCamera.d.ts +781 -0
  84. package/src/RenderingEngine/vtkClasses/vtkSlabCamera.js +155 -0
  85. package/src/RenderingEngine/vtkClasses/vtkStreamingOpenGLRenderWindow.js +47 -0
  86. package/src/RenderingEngine/vtkClasses/vtkStreamingOpenGLTexture.js +272 -0
  87. package/src/RenderingEngine/vtkClasses/vtkStreamingOpenGLViewNodeFactory.js +159 -0
  88. package/src/RenderingEngine/vtkClasses/vtkStreamingOpenGLVolumeMapper.js +319 -0
  89. package/src/Settings.ts +294 -0
  90. package/src/cache/cache.ts +854 -0
  91. package/src/cache/classes/Contour.ts +70 -0
  92. package/src/cache/classes/ContourSet.ts +151 -0
  93. package/src/cache/classes/ImageVolume.ts +155 -0
  94. package/src/cache/index.ts +5 -0
  95. package/src/constants/cpuColormaps.ts +1537 -0
  96. package/src/constants/epsilon.ts +3 -0
  97. package/src/constants/index.ts +13 -0
  98. package/src/constants/mprCameraValues.ts +20 -0
  99. package/src/constants/rendering.ts +8 -0
  100. package/src/constants/viewportPresets.ts +357 -0
  101. package/src/enums/BlendModes.ts +23 -0
  102. package/src/enums/ContourType.ts +6 -0
  103. package/src/enums/Events.ts +196 -0
  104. package/src/enums/GeometryType.ts +5 -0
  105. package/src/enums/InterpolationType.ts +13 -0
  106. package/src/enums/OrientationAxis.ts +8 -0
  107. package/src/enums/RequestType.ts +13 -0
  108. package/src/enums/SharedArrayBufferModes.ts +11 -0
  109. package/src/enums/VOILUTFunctionType.ts +10 -0
  110. package/src/enums/ViewportType.ts +21 -0
  111. package/src/enums/index.ts +23 -0
  112. package/src/eventTarget.ts +67 -0
  113. package/src/getEnabledElement.ts +105 -0
  114. package/src/global.ts +8 -0
  115. package/src/index.ts +123 -0
  116. package/src/init.ts +247 -0
  117. package/src/loaders/geometryLoader.ts +108 -0
  118. package/src/loaders/imageLoader.ts +298 -0
  119. package/src/loaders/volumeLoader.ts +477 -0
  120. package/src/metaData.ts +84 -0
  121. package/src/requestPool/imageLoadPoolManager.ts +43 -0
  122. package/src/requestPool/imageRetrievalPoolManager.ts +25 -0
  123. package/src/requestPool/requestPoolManager.ts +329 -0
  124. package/src/types/ActorSliceRange.ts +17 -0
  125. package/src/types/CPUFallbackColormap.ts +23 -0
  126. package/src/types/CPUFallbackColormapData.ts +12 -0
  127. package/src/types/CPUFallbackColormapsData.ts +7 -0
  128. package/src/types/CPUFallbackEnabledElement.ts +71 -0
  129. package/src/types/CPUFallbackLUT.ts +5 -0
  130. package/src/types/CPUFallbackLookupTable.ts +17 -0
  131. package/src/types/CPUFallbackRenderingTools.ts +25 -0
  132. package/src/types/CPUFallbackTransform.ts +16 -0
  133. package/src/types/CPUFallbackViewport.ts +29 -0
  134. package/src/types/CPUFallbackViewportDisplayedArea.ts +15 -0
  135. package/src/types/CPUIImageData.ts +47 -0
  136. package/src/types/ContourData.ts +19 -0
  137. package/src/types/Cornerstone3DConfig.ts +31 -0
  138. package/src/types/CustomEventType.ts +14 -0
  139. package/src/types/EventTypes.ts +403 -0
  140. package/src/types/FlipDirection.ts +9 -0
  141. package/src/types/IActor.ts +23 -0
  142. package/src/types/ICache.ts +28 -0
  143. package/src/types/ICachedGeometry.ts +13 -0
  144. package/src/types/ICachedImage.ts +13 -0
  145. package/src/types/ICachedVolume.ts +12 -0
  146. package/src/types/ICamera.ts +36 -0
  147. package/src/types/IContour.ts +18 -0
  148. package/src/types/IContourSet.ts +56 -0
  149. package/src/types/IDynamicImageVolume.ts +18 -0
  150. package/src/types/IEnabledElement.ts +21 -0
  151. package/src/types/IGeometry.ts +12 -0
  152. package/src/types/IImage.ts +113 -0
  153. package/src/types/IImageData.ts +45 -0
  154. package/src/types/IImageVolume.ts +78 -0
  155. package/src/types/ILoadObject.ts +36 -0
  156. package/src/types/IRegisterImageLoader.ts +10 -0
  157. package/src/types/IRenderingEngine.ts +28 -0
  158. package/src/types/IStackViewport.ts +138 -0
  159. package/src/types/IStreamingImageVolume.ts +13 -0
  160. package/src/types/IStreamingVolumeProperties.ts +14 -0
  161. package/src/types/IViewport.ts +149 -0
  162. package/src/types/IViewportId.ts +9 -0
  163. package/src/types/IVolume.ts +45 -0
  164. package/src/types/IVolumeInput.ts +36 -0
  165. package/src/types/IVolumeViewport.ts +141 -0
  166. package/src/types/ImageLoaderFn.ts +16 -0
  167. package/src/types/ImageSliceData.ts +6 -0
  168. package/src/types/Mat3.ts +16 -0
  169. package/src/types/Metadata.ts +39 -0
  170. package/src/types/OrientationVectors.ts +36 -0
  171. package/src/types/Plane.ts +6 -0
  172. package/src/types/Point2.ts +6 -0
  173. package/src/types/Point3.ts +6 -0
  174. package/src/types/Point4.ts +6 -0
  175. package/src/types/ScalingParameters.ts +27 -0
  176. package/src/types/StackViewportProperties.ts +25 -0
  177. package/src/types/TransformMatrix2D.ts +4 -0
  178. package/src/types/ViewportInputOptions.ts +21 -0
  179. package/src/types/ViewportPreset.ts +14 -0
  180. package/src/types/VolumeLoaderFn.ts +18 -0
  181. package/src/types/VolumeViewportProperties.ts +14 -0
  182. package/src/types/index.ts +157 -0
  183. package/src/types/voi.ts +15 -0
  184. package/src/utilities/actorCheck.ts +24 -0
  185. package/src/utilities/applyPreset.ts +132 -0
  186. package/src/utilities/calculateViewportsSpatialRegistration.ts +74 -0
  187. package/src/utilities/calibratedPixelSpacingMetadataProvider.ts +38 -0
  188. package/src/utilities/createFloat32SharedArray.ts +45 -0
  189. package/src/utilities/createInt16SharedArray.ts +43 -0
  190. package/src/utilities/createLinearRGBTransferFunction.ts +22 -0
  191. package/src/utilities/createSigmoidRGBTransferFunction.ts +63 -0
  192. package/src/utilities/createUInt16SharedArray.ts +43 -0
  193. package/src/utilities/createUint8SharedArray.ts +45 -0
  194. package/src/utilities/deepFreeze.ts +19 -0
  195. package/src/utilities/deepMerge.ts +81 -0
  196. package/src/utilities/getClosestImageId.ts +80 -0
  197. package/src/utilities/getClosestStackImageIndexForPoint.ts +116 -0
  198. package/src/utilities/getImageSliceDataForVolumeViewport.ts +61 -0
  199. package/src/utilities/getMinMax.ts +31 -0
  200. package/src/utilities/getRuntimeId.ts +54 -0
  201. package/src/utilities/getScalarDataType.ts +31 -0
  202. package/src/utilities/getScalingParameters.ts +35 -0
  203. package/src/utilities/getSliceRange.ts +86 -0
  204. package/src/utilities/getSpacingInNormalDirection.ts +44 -0
  205. package/src/utilities/getTargetVolumeAndSpacingInNormalDir.ts +126 -0
  206. package/src/utilities/getViewportImageCornersInWorld.ts +102 -0
  207. package/src/utilities/getViewportsWithImageURI.ts +46 -0
  208. package/src/utilities/getViewportsWithVolumeId.ts +38 -0
  209. package/src/utilities/getVoiFromSigmoidRGBTransferFunction.ts +23 -0
  210. package/src/utilities/getVolumeActorCorners.ts +24 -0
  211. package/src/utilities/getVolumeSliceRangeInfo.ts +52 -0
  212. package/src/utilities/getVolumeViewportScrollInfo.ts +32 -0
  213. package/src/utilities/getVolumeViewportsContainingSameVolumes.ts +58 -0
  214. package/src/utilities/hasNaNValues.ts +12 -0
  215. package/src/utilities/imageIdToURI.ts +10 -0
  216. package/src/utilities/imageToWorldCoords.ts +54 -0
  217. package/src/utilities/index.ts +100 -0
  218. package/src/utilities/indexWithinDimensions.ts +27 -0
  219. package/src/utilities/invertRgbTransferFunction.ts +36 -0
  220. package/src/utilities/isEqual.ts +27 -0
  221. package/src/utilities/isOpposite.ts +23 -0
  222. package/src/utilities/isTypedArray.ts +20 -0
  223. package/src/utilities/loadImageToCanvas.ts +80 -0
  224. package/src/utilities/planar.ts +91 -0
  225. package/src/utilities/renderToCanvas.ts +32 -0
  226. package/src/utilities/scaleRgbTransferFunction.ts +37 -0
  227. package/src/utilities/snapFocalPointToSlice.ts +78 -0
  228. package/src/utilities/spatialRegistrationMetadataProvider.ts +50 -0
  229. package/src/utilities/transformWorldToIndex.ts +16 -0
  230. package/src/utilities/triggerEvent.ts +38 -0
  231. package/src/utilities/uuidv4.ts +13 -0
  232. package/src/utilities/windowLevel.ts +39 -0
  233. package/src/utilities/worldToImageCoords.ts +64 -0
@@ -0,0 +1,108 @@
1
+ import '@kitware/vtk.js/Rendering/Profiles/Geometry';
2
+
3
+ import cache from '../cache';
4
+ import { ContourSet } from '../cache/classes/ContourSet';
5
+ import { GeometryType } from '../enums';
6
+ import { IGeometry, PublicContourSetData } from '../types';
7
+
8
+ type GeometryOptions = {
9
+ type: GeometryType;
10
+ geometryData: PublicContourSetData; // | PublicClosedSurfaceData, ...
11
+ };
12
+
13
+ /**
14
+ * Todo: currently we are not targeting loading geometry from a file.
15
+ * This is a placeholder for future work. For instance, separate loaders
16
+ * for .vti, .vtk, .obj, .dat etc. can be created and registered here.
17
+ */
18
+
19
+ /**
20
+ * It creates a geometry object and caches it
21
+ * @param geometryId - A unique identifier for the geometry.
22
+ * @param options - GeometryOptions
23
+ * @returns A promise that resolves to a geometry object.
24
+ */
25
+ async function createAndCacheGeometry(
26
+ geometryId: string,
27
+ options: GeometryOptions
28
+ ): Promise<IGeometry> {
29
+ let geometry = cache.getGeometry(geometryId);
30
+
31
+ if (geometry) {
32
+ return geometry;
33
+ }
34
+
35
+ if (options.type === GeometryType.CONTOUR) {
36
+ geometry = _createContourSet(geometryId, options.geometryData);
37
+ } else {
38
+ throw new Error('Unknown geometry type, Only CONTOUR is supported');
39
+ }
40
+
41
+ const geometryLoadObject = {
42
+ promise: Promise.resolve(geometry),
43
+ };
44
+
45
+ await cache.putGeometryLoadObject(geometryId, geometryLoadObject);
46
+
47
+ return geometry;
48
+ }
49
+
50
+ // Todo: this should be moved
51
+ function _createContourSet(
52
+ geometryId: string,
53
+ contourSetData: PublicContourSetData
54
+ ) {
55
+ // validate the data to make sure it is a valid contour set
56
+ if (!contourSetData || contourSetData.data.length === 0) {
57
+ throw new Error(
58
+ 'Invalid contour set data, see publicContourSetData type for more info'
59
+ );
60
+ }
61
+
62
+ // make sure it each has id, and each has data of type Point3[]
63
+ if (!contourSetData.id) {
64
+ throw new Error(
65
+ 'Invalid contour set data, each contour set must have an id'
66
+ );
67
+ }
68
+
69
+ if (!contourSetData.data || !Array.isArray(contourSetData.data)) {
70
+ throw new Error(
71
+ 'Invalid contour set data, each contour set must have an array of contours'
72
+ );
73
+ }
74
+
75
+ contourSetData.data.forEach((contourData) => {
76
+ if (!contourData.points || !Array.isArray(contourData.points)) {
77
+ throw new Error(
78
+ 'Invalid contour set data, each contour must have an array of points'
79
+ );
80
+ }
81
+
82
+ contourData.points.forEach((point) => {
83
+ if (!point || !Array.isArray(point) || point.length !== 3) {
84
+ throw new Error(
85
+ 'Invalid contour set data, each point must be an array of length 3'
86
+ );
87
+ }
88
+ });
89
+ });
90
+
91
+ const contourSet = new ContourSet({
92
+ id: contourSetData.id,
93
+ data: contourSetData.data,
94
+ color: contourSetData.color,
95
+ frameOfReferenceUID: contourSetData.frameOfReferenceUID,
96
+ });
97
+
98
+ const geometry: IGeometry = {
99
+ id: geometryId,
100
+ type: GeometryType.CONTOUR,
101
+ data: contourSet,
102
+ sizeInBytes: contourSet.getSizeInBytes(),
103
+ };
104
+
105
+ return geometry;
106
+ }
107
+
108
+ export { createAndCacheGeometry };
@@ -0,0 +1,298 @@
1
+ import cache from '../cache/cache';
2
+ import Events from '../enums/Events';
3
+ import eventTarget from '../eventTarget';
4
+ import { triggerEvent } from '../utilities';
5
+ import { IImage, ImageLoaderFn, IImageLoadObject, EventTypes } from '../types';
6
+ import imageLoadPoolManager from '../requestPool/imageLoadPoolManager';
7
+
8
+ export interface ImageLoaderOptions {
9
+ priority: number;
10
+ requestType: string;
11
+ additionalDetails?: Record<string, unknown>;
12
+ }
13
+ /**
14
+ * This module deals with ImageLoaders, loading images and caching images
15
+ */
16
+ const imageLoaders = {};
17
+ let unknownImageLoader;
18
+
19
+ /**
20
+ * Loads an image using a registered Cornerstone Image Loader.
21
+ *
22
+ * The image loader that is used will be
23
+ * determined by the image loader scheme matching against the imageId.
24
+ *
25
+ * @param imageId - A Cornerstone Image Object's imageId
26
+ * @param Options - to be passed to the Image Loader
27
+ *
28
+ * @returns - An Object which can be used to act after an image is loaded or loading fails
29
+ */
30
+ function loadImageFromImageLoader(
31
+ imageId: string,
32
+ options: ImageLoaderOptions
33
+ ): IImageLoadObject {
34
+ // Extract the image loader scheme: wadors:https://image1 => wadors
35
+ const colonIndex = imageId.indexOf(':');
36
+ const scheme = imageId.substring(0, colonIndex);
37
+ const loader = imageLoaders[scheme];
38
+ if (loader === undefined || loader === null) {
39
+ if (unknownImageLoader !== undefined) {
40
+ return unknownImageLoader(imageId);
41
+ }
42
+ throw new Error('loadImageFromImageLoader: no image loader for imageId');
43
+ }
44
+ // Load using the registered loader
45
+ const imageLoadObject = loader(imageId, options);
46
+ // Broadcast an image loaded event once the image is loaded
47
+ imageLoadObject.promise.then(
48
+ function (image) {
49
+ triggerEvent(eventTarget, Events.IMAGE_LOADED, { image });
50
+ },
51
+ function (error) {
52
+ const errorObject: EventTypes.ImageLoadedFailedEventDetail = {
53
+ imageId,
54
+ error,
55
+ };
56
+ triggerEvent(eventTarget, Events.IMAGE_LOAD_FAILED, errorObject);
57
+ }
58
+ );
59
+ return imageLoadObject;
60
+ }
61
+
62
+ /**
63
+ * Gets the imageLoadObject by 1) Looking in to the cache to see if the
64
+ * imageLoadObject has already been cached, 2) Checks inside the volume cache
65
+ * to see if there is a volume that contains the same imageURI for the requested
66
+ * imageID 3) Checks inside the imageCache for similar imageURI that might have
67
+ * been stored as a result of decaching a volume 4) Finally if none were found
68
+ * it request it from the registered imageLoaders.
69
+ *
70
+ * @param imageId - A Cornerstone Image Object's imageId
71
+ * @param options - Options to be passed to the Image Loader
72
+ *
73
+ * @returns An Object which can be used to act after an image is loaded or loading fails
74
+ */
75
+ function loadImageFromCacheOrVolume(
76
+ imageId: string,
77
+ options: ImageLoaderOptions
78
+ ): IImageLoadObject {
79
+ // 1. Check inside the image cache for imageId
80
+ let imageLoadObject = cache.getImageLoadObject(imageId);
81
+ if (imageLoadObject !== undefined) {
82
+ return imageLoadObject;
83
+ }
84
+ // 2. Check if there exists a volume in the cache containing the imageId,
85
+ // we copy the pixelData over.
86
+ const cachedVolumeInfo = cache.getVolumeContainingImageId(imageId);
87
+ if (cachedVolumeInfo && cachedVolumeInfo.volume.loadStatus.loaded) {
88
+ // 2.1 Convert the volume at the specific slice to a cornerstoneImage object.
89
+ // this will copy the pixel data over.
90
+ const { volume, imageIdIndex } = cachedVolumeInfo;
91
+ imageLoadObject = volume.convertToCornerstoneImage(imageId, imageIdIndex);
92
+ return imageLoadObject;
93
+ }
94
+ // 3. If no volume found, we search inside the imageCache for the imageId
95
+ // that has the same URI which had been cached if the volume was converted
96
+ // to an image
97
+ const cachedImage = cache.getCachedImageBasedOnImageURI(imageId);
98
+ if (cachedImage) {
99
+ imageLoadObject = cachedImage.imageLoadObject;
100
+ return imageLoadObject;
101
+ }
102
+ // 4. if not in image cache nor inside the volume cache, we request the
103
+ // image loaders to load it
104
+ imageLoadObject = loadImageFromImageLoader(imageId, options);
105
+
106
+ return imageLoadObject;
107
+ }
108
+
109
+ /**
110
+ * Loads an image given an imageId and optional priority and returns a promise
111
+ * which will resolve to the loaded image object or fail if an error occurred.
112
+ * The loaded image is not stored in the cache.
113
+ *
114
+ *
115
+ * @param imageId - A Cornerstone Image Object's imageId
116
+ * @param options - Options to be passed to the Image Loader
117
+ *
118
+ * @returns An Object which can be used to act after an image is loaded or loading fails
119
+ */
120
+ export function loadImage(
121
+ imageId: string,
122
+ options: ImageLoaderOptions = { priority: 0, requestType: 'prefetch' }
123
+ ): Promise<IImage> {
124
+ if (imageId === undefined) {
125
+ throw new Error('loadImage: parameter imageId must not be undefined');
126
+ }
127
+
128
+ return loadImageFromCacheOrVolume(imageId, options).promise;
129
+ }
130
+
131
+ /**
132
+ * Loads an image given an imageId and optional priority and returns a promise
133
+ * which will resolve to the loaded image object or fail if an error occurred.
134
+ * The image is stored in the cache.
135
+ *
136
+ * @param imageId - A Cornerstone Image Object's imageId
137
+ * @param options - Options to be passed to the Image Loader
138
+ *
139
+ * @returns Image Loader Object
140
+ */
141
+ export function loadAndCacheImage(
142
+ imageId: string,
143
+ options: ImageLoaderOptions = { priority: 0, requestType: 'prefetch' }
144
+ ): Promise<IImage> {
145
+ if (imageId === undefined) {
146
+ throw new Error(
147
+ 'loadAndCacheImage: parameter imageId must not be undefined'
148
+ );
149
+ }
150
+ const imageLoadObject = loadImageFromCacheOrVolume(imageId, options);
151
+
152
+ // if not inside cache, store it
153
+ if (!cache.getImageLoadObject(imageId)) {
154
+ cache.putImageLoadObject(imageId, imageLoadObject).catch((err) => {
155
+ console.warn(err);
156
+ });
157
+ }
158
+
159
+ return imageLoadObject.promise;
160
+ }
161
+
162
+ /**
163
+ * Load and cache a list of imageIds
164
+ *
165
+ * @param imageIds - list of imageIds
166
+ * @param options - options for loader
167
+ *
168
+ */
169
+ export function loadAndCacheImages(
170
+ imageIds: Array<string>,
171
+ options: ImageLoaderOptions = { priority: 0, requestType: 'prefetch' }
172
+ ): Promise<IImage>[] {
173
+ if (!imageIds || imageIds.length === 0) {
174
+ throw new Error(
175
+ 'loadAndCacheImages: parameter imageIds must be list of image Ids'
176
+ );
177
+ }
178
+
179
+ const allPromises = imageIds.map((imageId) => {
180
+ return loadAndCacheImage(imageId, options);
181
+ });
182
+
183
+ return allPromises;
184
+ }
185
+
186
+ /**
187
+ * Removes the imageId from the request pool manager and executes the `cancel`
188
+ * function if it exists.
189
+ *
190
+ * @param imageId - A Cornerstone Image Object's imageId
191
+ *
192
+ */
193
+ export function cancelLoadImage(imageId: string): void {
194
+ const filterFunction = ({ additionalDetails }) => {
195
+ if (additionalDetails.imageId) {
196
+ return additionalDetails.imageId !== imageId;
197
+ }
198
+
199
+ // for volumes
200
+ return true;
201
+ };
202
+
203
+ // Instruct the request pool manager to filter queued
204
+ // requests to ensure requests we no longer need are
205
+ // no longer sent.
206
+ imageLoadPoolManager.filterRequests(filterFunction);
207
+
208
+ // TODO: Cancel decoding and retrieval as well (somehow?)
209
+
210
+ // cancel image loading if in progress
211
+ const imageLoadObject = cache.getImageLoadObject(imageId);
212
+
213
+ if (imageLoadObject) {
214
+ imageLoadObject.cancelFn();
215
+ }
216
+ }
217
+
218
+ /**
219
+ * Removes the imageIds from the request pool manager and calls the `cancel`
220
+ * function if it exists.
221
+ *
222
+ * @param imageIds - Array of Cornerstone Image Object's imageIds
223
+ *
224
+ */
225
+ export function cancelLoadImages(imageIds: Array<string>): void {
226
+ imageIds.forEach((imageId) => cancelLoadImage(imageId));
227
+ }
228
+
229
+ /**
230
+ * Removes all the ongoing image loads by calling the `cancel` method on each
231
+ * imageLoadObject. If no `cancel` method is available, it will be ignored.
232
+ *
233
+ */
234
+ export function cancelLoadAll(): void {
235
+ const requestPool = imageLoadPoolManager.getRequestPool();
236
+
237
+ Object.keys(requestPool).forEach((type: string) => {
238
+ const requests = requestPool[type];
239
+
240
+ Object.keys(requests).forEach((priority) => {
241
+ const requestDetails = requests[priority].pop();
242
+ const { imageId, volumeId } = requestDetails.additionalDetails;
243
+
244
+ let loadObject;
245
+
246
+ if (imageId) {
247
+ loadObject = cache.getImageLoadObject(imageId);
248
+ } else if (volumeId) {
249
+ loadObject = cache.getVolumeLoadObject(volumeId);
250
+ }
251
+ if (loadObject) {
252
+ loadObject.cancel();
253
+ }
254
+ });
255
+ // resetting the pool types to be empty
256
+ imageLoadPoolManager.clearRequestStack(type);
257
+
258
+ // TODO: Clear retrieval and decoding queues as well
259
+ });
260
+ }
261
+
262
+ /**
263
+ * Registers an imageLoader plugin with cornerstone for the specified scheme
264
+ *
265
+ * @param scheme - The scheme to use for this image loader (e.g. 'dicomweb', 'wadouri', 'http')
266
+ * @param imageLoader - A Cornerstone Image Loader function
267
+ */
268
+ export function registerImageLoader(
269
+ scheme: string,
270
+ imageLoader: ImageLoaderFn
271
+ ): void {
272
+ imageLoaders[scheme] = imageLoader;
273
+ }
274
+ /**
275
+ * Registers a new unknownImageLoader and returns the previous one
276
+ *
277
+ * @param imageLoader - A Cornerstone Image Loader
278
+ *
279
+ * @returns The previous Unknown Image Loader
280
+ */
281
+ export function registerUnknownImageLoader(
282
+ imageLoader: ImageLoaderFn
283
+ ): ImageLoaderFn {
284
+ const oldImageLoader = unknownImageLoader;
285
+ unknownImageLoader = imageLoader;
286
+ return oldImageLoader;
287
+ }
288
+ /**
289
+ * Removes all registered and unknown image loaders. This should be called
290
+ * when the application is unmounted to prevent memory leaks.
291
+ *
292
+ */
293
+ export function unregisterAllImageLoaders(): void {
294
+ Object.keys(imageLoaders).forEach(
295
+ (imageLoader) => delete imageLoaders[imageLoader]
296
+ );
297
+ unknownImageLoader = undefined;
298
+ }