@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,477 @@
1
+ import '@kitware/vtk.js/Rendering/Profiles/Volume';
2
+
3
+ import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData';
4
+ import type { vtkImageData as vtkImageDataType } from '@kitware/vtk.js/Common/DataModel/ImageData';
5
+ import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray';
6
+ import cloneDeep from 'lodash.clonedeep';
7
+
8
+ import { ImageVolume } from '../cache/classes/ImageVolume';
9
+ import type * as Types from '../types';
10
+ import cache from '../cache/cache';
11
+ import Events from '../enums/Events';
12
+ import eventTarget from '../eventTarget';
13
+ import triggerEvent from '../utilities/triggerEvent';
14
+ import { uuidv4 } from '../utilities';
15
+ import { Point3, Metadata, EventTypes, Mat3 } from '../types';
16
+ import { getConfiguration } from '../init';
17
+
18
+ interface VolumeLoaderOptions {
19
+ imageIds: Array<string>;
20
+ }
21
+
22
+ interface DerivedVolumeOptions {
23
+ volumeId: string;
24
+ targetBuffer?: {
25
+ type: 'Float32Array' | 'Uint8Array' | 'Uint16Array' | 'Int16Array';
26
+ sharedArrayBuffer?: boolean;
27
+ };
28
+ }
29
+ interface LocalVolumeOptions {
30
+ scalarData: Float32Array | Uint8Array | Uint16Array | Int16Array;
31
+ metadata: Metadata;
32
+ dimensions: Point3;
33
+ spacing: Point3;
34
+ origin: Point3;
35
+ direction: Mat3;
36
+ }
37
+
38
+ /**
39
+ * Adds a single scalar data to a 3D volume
40
+ */
41
+ function addScalarDataToImageData(
42
+ imageData: vtkImageDataType,
43
+ scalarData: Types.VolumeScalarData,
44
+ dataArrayAttrs
45
+ ) {
46
+ const scalarArray = vtkDataArray.newInstance({
47
+ name: `Pixels`,
48
+ values: scalarData,
49
+ ...dataArrayAttrs,
50
+ });
51
+
52
+ imageData.getPointData().setScalars(scalarArray);
53
+ }
54
+
55
+ /**
56
+ * Adds multiple scalar data (time points) to a 4D volume
57
+ */
58
+ function addScalarDataArraysToImageData(
59
+ imageData: vtkImageDataType,
60
+ scalarDataArrays: Types.VolumeScalarData[],
61
+ dataArrayAttrs
62
+ ) {
63
+ scalarDataArrays.forEach((scalarData, i) => {
64
+ const vtkScalarArray = vtkDataArray.newInstance({
65
+ name: `timePoint-${i}`,
66
+ values: scalarData,
67
+ ...dataArrayAttrs,
68
+ });
69
+
70
+ imageData.getPointData().addArray(vtkScalarArray);
71
+ });
72
+
73
+ // Set the first as active otherwise nothing is displayed on the screen
74
+ imageData.getPointData().setActiveScalars('timePoint-0');
75
+ }
76
+
77
+ function createInternalVTKRepresentation(
78
+ volume: Types.IImageVolume
79
+ ): vtkImageDataType {
80
+ const { dimensions, metadata, spacing, direction, origin } = volume;
81
+ const { PhotometricInterpretation } = metadata;
82
+
83
+ let numComponents = 1;
84
+ if (PhotometricInterpretation === 'RGB') {
85
+ numComponents = 3;
86
+ }
87
+
88
+ const imageData = vtkImageData.newInstance();
89
+ const dataArrayAttrs = { numberOfComponents: numComponents };
90
+
91
+ imageData.setDimensions(dimensions);
92
+ imageData.setSpacing(spacing);
93
+ imageData.setDirection(direction);
94
+ imageData.setOrigin(origin);
95
+
96
+ // Add scalar datas to 3D or 4D volume
97
+ if (volume.isDynamicVolume()) {
98
+ const scalarDataArrays = (<Types.IDynamicImageVolume>(
99
+ volume
100
+ )).getScalarDataArrays();
101
+
102
+ addScalarDataArraysToImageData(imageData, scalarDataArrays, dataArrayAttrs);
103
+ } else {
104
+ const scalarData = volume.getScalarData();
105
+
106
+ addScalarDataToImageData(imageData, scalarData, dataArrayAttrs);
107
+ }
108
+
109
+ return imageData;
110
+ }
111
+
112
+ /**
113
+ * This module deals with VolumeLoaders and loading volumes
114
+ */
115
+
116
+ const volumeLoaders = {};
117
+
118
+ let unknownVolumeLoader;
119
+
120
+ /**
121
+ * Load a volume using a registered Cornerstone Volume Loader.
122
+ *
123
+ * The volume loader that is used will be
124
+ * determined by the volume loader scheme matching against the volumeId.
125
+ *
126
+ * @param volumeId - A Cornerstone Volume Object's volumeId
127
+ * @param options - Options to be passed to the Volume Loader. Options
128
+ * contain the ImageIds that is passed to the loader
129
+ *
130
+ * @returns An Object which can be used to act after a volume is loaded or loading fails
131
+ *
132
+ */
133
+ function loadVolumeFromVolumeLoader(
134
+ volumeId: string,
135
+ options: VolumeLoaderOptions
136
+ ): Types.IVolumeLoadObject {
137
+ const colonIndex = volumeId.indexOf(':');
138
+ const scheme = volumeId.substring(0, colonIndex);
139
+ const loader = volumeLoaders[scheme];
140
+
141
+ if (loader === undefined || loader === null) {
142
+ if (unknownVolumeLoader !== undefined) {
143
+ return unknownVolumeLoader(volumeId, options);
144
+ }
145
+
146
+ throw new Error(
147
+ 'loadVolumeFromVolumeLoader: no volume loader for volumeId'
148
+ );
149
+ }
150
+
151
+ const volumeLoadObject = loader(volumeId, options);
152
+
153
+ // Broadcast a volume loaded event once the image is loaded
154
+ volumeLoadObject.promise.then(
155
+ function (volume) {
156
+ triggerEvent(eventTarget, Events.VOLUME_LOADED, { volume });
157
+ },
158
+ function (error) {
159
+ const errorObject: EventTypes.VolumeLoadedFailedEventDetail = {
160
+ volumeId,
161
+ error,
162
+ };
163
+
164
+ triggerEvent(eventTarget, Events.VOLUME_LOADED_FAILED, errorObject);
165
+ }
166
+ );
167
+
168
+ return volumeLoadObject;
169
+ }
170
+
171
+ /**
172
+ * Loads a volume given a volumeId and optional priority and returns a promise which will resolve to
173
+ * the loaded image object or fail if an error occurred. The loaded image is not stored in the cache.
174
+ *
175
+ * @param volumeId - A Cornerstone Image Object's volumeId
176
+ * @param options - Options to be passed to the Volume Loader
177
+ *
178
+ * @returns An Object which can be used to act after an image is loaded or loading fails
179
+ */
180
+ export function loadVolume(
181
+ volumeId: string,
182
+ options: VolumeLoaderOptions = { imageIds: [] }
183
+ ): Promise<Types.IImageVolume> {
184
+ if (volumeId === undefined) {
185
+ throw new Error('loadVolume: parameter volumeId must not be undefined');
186
+ }
187
+
188
+ let volumeLoadObject = cache.getVolumeLoadObject(volumeId);
189
+
190
+ if (volumeLoadObject !== undefined) {
191
+ return volumeLoadObject.promise;
192
+ }
193
+
194
+ volumeLoadObject = loadVolumeFromVolumeLoader(volumeId, options);
195
+
196
+ return volumeLoadObject.promise.then((volume: Types.IImageVolume) => {
197
+ volume.imageData = createInternalVTKRepresentation(volume);
198
+ return volume;
199
+ });
200
+ }
201
+
202
+ /**
203
+ * Loads an image given an volumeId and optional priority and returns a promise which will resolve to
204
+ * the loaded image object or fail if an error occurred. The image is stored in the cache.
205
+ *
206
+ * @param volumeId - A Cornerstone Image Object's volumeId
207
+ * @param options - Options to be passed to the Volume Loader
208
+ *
209
+ * @returns Volume Loader Object
210
+ */
211
+ export async function createAndCacheVolume(
212
+ volumeId: string,
213
+ options: VolumeLoaderOptions
214
+ ): Promise<Record<string, any>> {
215
+ if (volumeId === undefined) {
216
+ throw new Error(
217
+ 'createAndCacheVolume: parameter volumeId must not be undefined'
218
+ );
219
+ }
220
+
221
+ let volumeLoadObject = cache.getVolumeLoadObject(volumeId);
222
+
223
+ if (volumeLoadObject !== undefined) {
224
+ return volumeLoadObject.promise;
225
+ }
226
+
227
+ volumeLoadObject = loadVolumeFromVolumeLoader(volumeId, options);
228
+
229
+ volumeLoadObject.promise.then((volume: Types.IImageVolume) => {
230
+ volume.imageData = createInternalVTKRepresentation(volume);
231
+ });
232
+
233
+ cache.putVolumeLoadObject(volumeId, volumeLoadObject).catch((err) => {
234
+ throw err;
235
+ });
236
+
237
+ return volumeLoadObject.promise;
238
+ }
239
+
240
+ /**
241
+ * Based on a referencedVolumeId, it will build and cache a new volume. If
242
+ * no scalarData is specified in the options, an empty derived volume will be
243
+ * created that matches the image metadata of the referenceVolume. If scalarData
244
+ * is given, it will be used to generate the intensity values for the derivedVolume.
245
+ * Finally, it will save the volume in the cache.
246
+ * @param referencedVolumeId - the volumeId from which the new volume will get its metadata
247
+ * @param options - DerivedVolumeOptions {uid: derivedVolumeUID, targetBuffer: { type: Float32Array | Uint8Array |
248
+ * Uint16Array | Uint32Array }, scalarData: if provided}
249
+ *
250
+ * @returns ImageVolume
251
+ */
252
+ export async function createAndCacheDerivedVolume(
253
+ referencedVolumeId: string,
254
+ options: DerivedVolumeOptions
255
+ ): Promise<ImageVolume> {
256
+ const referencedVolume = cache.getVolume(referencedVolumeId);
257
+
258
+ if (!referencedVolume) {
259
+ throw new Error(
260
+ `Cannot created derived volume: Referenced volume with id ${referencedVolumeId} does not exist.`
261
+ );
262
+ }
263
+
264
+ let { volumeId } = options;
265
+ const { targetBuffer } = options;
266
+
267
+ if (volumeId === undefined) {
268
+ volumeId = uuidv4();
269
+ }
270
+
271
+ const { metadata, dimensions, spacing, origin, direction } = referencedVolume;
272
+ const scalarData = referencedVolume.getScalarData();
273
+ const scalarLength = scalarData.length;
274
+
275
+ let numBytes, TypedArray;
276
+
277
+ const { useNorm16Texture } = getConfiguration().rendering;
278
+
279
+ // If target buffer is provided
280
+ if (targetBuffer) {
281
+ if (targetBuffer.type === 'Float32Array') {
282
+ numBytes = scalarLength * 4;
283
+ TypedArray = Float32Array;
284
+ } else if (targetBuffer.type === 'Uint8Array') {
285
+ numBytes = scalarLength;
286
+ TypedArray = Uint8Array;
287
+ } else if (useNorm16Texture && targetBuffer.type === 'Uint16Array') {
288
+ numBytes = scalarLength * 2;
289
+ TypedArray = Uint16Array;
290
+ } else if (useNorm16Texture && targetBuffer.type === 'Int16Array') {
291
+ numBytes = scalarLength * 2;
292
+ TypedArray = Uint16Array;
293
+ } else {
294
+ throw new Error('TargetBuffer should be Float32Array or Uint8Array');
295
+ }
296
+ } else {
297
+ // Use float32 if no targetBuffer is provided
298
+ numBytes = scalarLength * 4;
299
+ TypedArray = Float32Array;
300
+ }
301
+
302
+ // check if there is enough space in unallocated + image Cache
303
+ const isCacheable = cache.isCacheable(numBytes);
304
+ if (!isCacheable) {
305
+ throw new Error(Events.CACHE_SIZE_EXCEEDED);
306
+ }
307
+
308
+ let volumeScalarData;
309
+ if (targetBuffer?.sharedArrayBuffer) {
310
+ const buffer = new SharedArrayBuffer(numBytes);
311
+ volumeScalarData = new TypedArray(buffer);
312
+ } else {
313
+ volumeScalarData = new TypedArray(scalarLength);
314
+ }
315
+
316
+ // Todo: handle more than one component for segmentation (RGB)
317
+ const scalarArray = vtkDataArray.newInstance({
318
+ name: 'Pixels',
319
+ numberOfComponents: 1,
320
+ values: volumeScalarData,
321
+ });
322
+
323
+ const derivedImageData = vtkImageData.newInstance();
324
+
325
+ derivedImageData.setDimensions(dimensions);
326
+ derivedImageData.setSpacing(spacing);
327
+ derivedImageData.setDirection(direction);
328
+ derivedImageData.setOrigin(origin);
329
+ derivedImageData.getPointData().setScalars(scalarArray);
330
+
331
+ const derivedVolume = new ImageVolume({
332
+ volumeId,
333
+ metadata: cloneDeep(metadata),
334
+ dimensions: [dimensions[0], dimensions[1], dimensions[2]],
335
+ spacing,
336
+ origin,
337
+ direction,
338
+ imageData: derivedImageData,
339
+ scalarData: volumeScalarData,
340
+ sizeInBytes: numBytes,
341
+ referencedVolumeId,
342
+ });
343
+
344
+ const volumeLoadObject = {
345
+ promise: Promise.resolve(derivedVolume),
346
+ };
347
+
348
+ await cache.putVolumeLoadObject(volumeId, volumeLoadObject);
349
+
350
+ return derivedVolume;
351
+ }
352
+
353
+ /**
354
+ * Creates and cache a volume based on a set of provided properties including
355
+ * dimensions, spacing, origin, direction, metadata, scalarData. It should be noted that
356
+ * scalarData should be provided for this function to work. If a volume with the same
357
+ * Id exists in the cache it returns it immediately.
358
+ * @param options - { scalarData, metadata, dimensions, spacing, origin, direction }
359
+ * @param volumeId - Id of the generated volume
360
+ *
361
+ * @returns ImageVolume
362
+ */
363
+ export function createLocalVolume(
364
+ options: LocalVolumeOptions,
365
+ volumeId: string,
366
+ preventCache = false
367
+ ): ImageVolume {
368
+ const { scalarData, metadata, dimensions, spacing, origin, direction } =
369
+ options;
370
+
371
+ if (
372
+ !scalarData ||
373
+ !(
374
+ scalarData instanceof Uint8Array ||
375
+ scalarData instanceof Float32Array ||
376
+ scalarData instanceof Uint16Array ||
377
+ scalarData instanceof Int16Array
378
+ )
379
+ ) {
380
+ throw new Error(
381
+ 'To use createLocalVolume you should pass scalarData of type Uint8Array, Uint16Array, Int16Array or Float32Array'
382
+ );
383
+ }
384
+
385
+ // Todo: handle default values for spacing, origin, direction if not provided
386
+ if (volumeId === undefined) {
387
+ volumeId = uuidv4();
388
+ }
389
+
390
+ const cachedVolume = cache.getVolume(volumeId);
391
+
392
+ if (cachedVolume) {
393
+ return cachedVolume as ImageVolume;
394
+ }
395
+
396
+ const scalarLength = dimensions[0] * dimensions[1] * dimensions[2];
397
+
398
+ const numBytes = scalarData ? scalarData.buffer.byteLength : scalarLength * 4;
399
+
400
+ // check if there is enough space in unallocated + image Cache
401
+ const isCacheable = cache.isCacheable(numBytes);
402
+ if (!isCacheable) {
403
+ throw new Error(Events.CACHE_SIZE_EXCEEDED);
404
+ }
405
+
406
+ const scalarArray = vtkDataArray.newInstance({
407
+ name: 'Pixels',
408
+ numberOfComponents: 1,
409
+ values: scalarData,
410
+ });
411
+
412
+ const imageData = vtkImageData.newInstance();
413
+
414
+ imageData.setDimensions(dimensions);
415
+ imageData.setSpacing(spacing);
416
+ imageData.setDirection(direction);
417
+ imageData.setOrigin(origin);
418
+ imageData.getPointData().setScalars(scalarArray);
419
+
420
+ const derivedVolume = new ImageVolume({
421
+ volumeId,
422
+ metadata: cloneDeep(metadata),
423
+ dimensions: [dimensions[0], dimensions[1], dimensions[2]],
424
+ spacing,
425
+ origin,
426
+ direction,
427
+ imageData: imageData,
428
+ scalarData,
429
+ sizeInBytes: numBytes,
430
+ });
431
+
432
+ if (preventCache) {
433
+ return derivedVolume;
434
+ }
435
+
436
+ const volumeLoadObject = {
437
+ promise: Promise.resolve(derivedVolume),
438
+ };
439
+ cache.putVolumeLoadObject(volumeId, volumeLoadObject);
440
+
441
+ return derivedVolume;
442
+ }
443
+
444
+ /**
445
+ * Registers an volumeLoader plugin with cornerstone for the specified scheme
446
+ *
447
+ * @param scheme - The scheme to use for this volume loader (e.g. 'dicomweb', 'wadouri', 'http')
448
+ * @param volumeLoader - A Cornerstone Volume Loader function
449
+ */
450
+ export function registerVolumeLoader(
451
+ scheme: string,
452
+ volumeLoader: Types.VolumeLoaderFn
453
+ ): void {
454
+ volumeLoaders[scheme] = volumeLoader;
455
+ }
456
+
457
+ /** Gets the array of volume loader schemes */
458
+ export function getVolumeLoaderSchemes(): string[] {
459
+ return Object.keys(volumeLoaders);
460
+ }
461
+
462
+ /**
463
+ * Registers a new unknownVolumeLoader and returns the previous one
464
+ *
465
+ * @param volumeLoader - A Cornerstone Volume Loader
466
+ *
467
+ * @returns The previous Unknown Volume Loader
468
+ */
469
+ export function registerUnknownVolumeLoader(
470
+ volumeLoader: Types.VolumeLoaderFn
471
+ ): Types.VolumeLoaderFn | undefined {
472
+ const oldVolumeLoader = unknownVolumeLoader;
473
+
474
+ unknownVolumeLoader = volumeLoader;
475
+
476
+ return oldVolumeLoader;
477
+ }
@@ -0,0 +1,84 @@
1
+ // This module defines a way to access various metadata about an imageId. This layer of abstraction exists
2
+ // So metadata can be provided in different ways (e.g. by parsing DICOM P10 or by a WADO-RS document)
3
+
4
+ const providers = [];
5
+
6
+ /**
7
+ * Adds a metadata provider with the specified priority
8
+ * @param provider - Metadata provider function
9
+ * @param priority - 0 is default/normal, > 0 is high, < 0 is low
10
+ *
11
+ * @category MetaData
12
+ */
13
+ export function addProvider(
14
+ provider: (type: string, query: any) => any,
15
+ priority = 0
16
+ ): void {
17
+ let i;
18
+
19
+ // Find the right spot to insert this provider based on priority
20
+ for (i = 0; i < providers.length; i++) {
21
+ if (providers[i].priority <= priority) {
22
+ break;
23
+ }
24
+ }
25
+
26
+ // Insert the decode task at position i
27
+ providers.splice(i, 0, {
28
+ priority,
29
+ provider,
30
+ });
31
+ }
32
+
33
+ /**
34
+ * Removes the specified provider
35
+ *
36
+ * @param provider - Metadata provider function
37
+ *
38
+ * @category MetaData
39
+ */
40
+ export function removeProvider(
41
+ provider: (type: string, query: any) => { any }
42
+ ): void {
43
+ for (let i = 0; i < providers.length; i++) {
44
+ if (providers[i].provider === provider) {
45
+ providers.splice(i, 1);
46
+
47
+ break;
48
+ }
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Removes all providers
54
+ *
55
+ * @category MetaData
56
+ */
57
+ export function removeAllProviders(): void {
58
+ while (providers.length > 0) {
59
+ providers.pop();
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Gets metadata from the registered metadata providers. Will call each one from highest priority to lowest
65
+ * until one responds
66
+ *
67
+ * @param type - The type of metadata requested from the metadata store
68
+ * @param query - The query for the metadata store, often imageId
69
+ *
70
+ * @returns The metadata retrieved from the metadata store
71
+ * @category MetaData
72
+ */
73
+ function getMetaData(type: string, query: string): any {
74
+ // Invoke each provider in priority order until one returns something
75
+ for (let i = 0; i < providers.length; i++) {
76
+ const result = providers[i].provider(type, query);
77
+
78
+ if (result !== undefined) {
79
+ return result;
80
+ }
81
+ }
82
+ }
83
+
84
+ export { getMetaData as get };
@@ -0,0 +1,43 @@
1
+ import { RequestPoolManager } from './requestPoolManager';
2
+ import RequestType from '../enums/RequestType';
3
+
4
+ /**
5
+ * You can use the imageLoadPoolManager to load images, by providing a `requestFn`
6
+ * that returns a promise for the image. You can provide a `type` to specify the type of
7
+ * request (interaction, thumbnail, prefetch), and you can provide additional details
8
+ * that will be passed to the requestFn. Below is an example of a requestFn that loads
9
+ * an image from an imageId:
10
+ *
11
+ * ```javascript
12
+ *
13
+ * const priority = -5
14
+ * const requestType = RequestType.Interaction
15
+ * const additionalDetails = { imageId }
16
+ * const options = {
17
+ * targetBuffer: {
18
+ * type: 'Float32Array',
19
+ * offset: null,
20
+ * length: null,
21
+ * },
22
+ * preScale: {
23
+ * enabled: true,
24
+ * },
25
+ * }
26
+ *
27
+ * imageLoadPoolManager.addRequest(
28
+ * loadAndCacheImage(imageId, options).then(() => { // set on viewport}),
29
+ * requestType,
30
+ * additionalDetails,
31
+ * priority
32
+ * )
33
+ * ```
34
+ */
35
+ const imageLoadPoolManager = new RequestPoolManager('imageLoadPool');
36
+
37
+ imageLoadPoolManager.grabDelay = 0;
38
+
39
+ imageLoadPoolManager.setMaxSimultaneousRequests(RequestType.Interaction, 1000);
40
+ imageLoadPoolManager.setMaxSimultaneousRequests(RequestType.Thumbnail, 1000);
41
+ imageLoadPoolManager.setMaxSimultaneousRequests(RequestType.Prefetch, 1000);
42
+
43
+ export default imageLoadPoolManager;
@@ -0,0 +1,25 @@
1
+ import { RequestPoolManager } from './requestPoolManager';
2
+ import RequestType from '../enums/RequestType';
3
+
4
+ /**
5
+ * ImageRetrievalPoolManager
6
+ * You don't need to directly use the imageRetrievalPoolManager to load images
7
+ * since the imageLoadPoolManager will automatically use it for retrieval. However,
8
+ * maximum number of concurrent requests can be set by calling `setMaxConcurrentRequests`.
9
+ *
10
+ * Retrieval (usually) === XHR requests
11
+ */
12
+ const imageRetrievalPoolManager = new RequestPoolManager('imageRetrievalPool');
13
+
14
+ imageRetrievalPoolManager.setMaxSimultaneousRequests(
15
+ RequestType.Interaction,
16
+ 200
17
+ );
18
+ imageRetrievalPoolManager.setMaxSimultaneousRequests(
19
+ RequestType.Thumbnail,
20
+ 200
21
+ );
22
+ imageRetrievalPoolManager.setMaxSimultaneousRequests(RequestType.Prefetch, 200);
23
+ imageRetrievalPoolManager.grabDelay = 0;
24
+
25
+ export default imageRetrievalPoolManager;