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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | 428x 9634x 9634x 9634x 9634x 9634x 9634x 9634x 9634x 9634x 9634x 9634x 9634x 9634x 9634x 183046x 9634x 9634x 9634x 9634x 9458x 9458x 7818x 7818x 7818x 7818x 7818x 7818x 7818x 7818x 7818x 7818x 7818x 9458x 1640x 9458x 9458x 9458x 9458x 9458x 9458x 9458x 60x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 52x 20x 20x 20x 60x 60x 60x 9458x 9458x 8x 9458x 9458x 782x 9458x 60x 9458x 9458x 9458x 60x 60x 9458x 380x 380x 380x 9458x | import type { ByteArray } from 'dicom-parser'; import getMinMax from '../shared/getMinMax'; import type { DICOMLoaderImageOptions, DICOMLoaderIImage } from '../types'; import type { Types } from '@cornerstonejs/core'; import { canRenderFloatTextures, Enums, metaData, utilities, } from '@cornerstonejs/core'; import convertColorSpace from './convertColorSpace'; import isColorConversionRequired from './isColorConversionRequired'; import decodeImageFrame from './decodeImageFrame'; import getImageFrame from './getImageFrame'; import getScalingParameters from './getScalingParameters'; import { getOptions } from './internal/options'; import isColorImageFn from '../shared/isColorImage'; import removeAFromRGBA from './removeAFromRGBA'; import isModalityLUTForDisplay from './isModalityLutForDisplay'; import setPixelDataType from './setPixelDataType'; let lastImageIdDrawn = ''; function createImage( imageId: string, pixelData: ByteArray, transferSyntax: string, options: DICOMLoaderImageOptions = {} ): Promise<DICOMLoaderIImage | Types.IImageFrame> { // whether to use RGBA for color images, default true as cs-legacy uses RGBA // but we don't need RGBA in cs3d, and it's faster, and memory-efficient // in cs3d const useRGBA = options.useRGBA; // always preScale the pixel array unless it is asked not to options.preScale = { enabled: options.preScale && options.preScale.enabled !== undefined ? options.preScale.enabled : true, }; Iif (!pixelData?.length) { return Promise.reject(new Error('The pixel data is missing')); } const { MetadataModules } = Enums; const canvas = document.createElement('canvas'); const imageFrame = getImageFrame(imageId); imageFrame.decodeLevel = options.decodeLevel; options.allowFloatRendering = canRenderFloatTextures(); // Get the scaling parameters from the metadata Eif (options.preScale.enabled) { const scalingParameters = getScalingParameters(metaData, imageId); Eif (scalingParameters) { options.preScale = { ...options.preScale, scalingParameters: scalingParameters as Types.ScalingParameters, }; } } const { decodeConfig } = getOptions(); // Remove any property of the `imageFrame` that cannot be transferred to the worker, // such as promises and functions. // This is necessary because the `imageFrame` object is passed to the worker. Object.keys(imageFrame).forEach((key) => { Iif ( typeof imageFrame[key] === 'function' || imageFrame[key] instanceof Promise ) { delete imageFrame[key]; } }); const decodePromise = decodeImageFrame( imageFrame, transferSyntax, pixelData, canvas, options, decodeConfig ); const isColorImage = isColorImageFn(imageFrame.photometricInterpretation); return new Promise<DICOMLoaderIImage | Types.IImageFrame>( (resolve, reject) => { // eslint-disable-next-line complexity decodePromise.then(function (imageFrame: Types.IImageFrame) { // If we have a target buffer that was written to in the // Decode task, point the image to it here. let alreadyTyped = false; // We can safely render color image in 8 bit, so no need to convert if ( options.targetBuffer && options.targetBuffer.type && !isColorImage ) { const { arrayBuffer, type, offset: rawOffset = 0, length: rawLength, } = options.targetBuffer; const imageFrameLength = imageFrame.pixelDataLength; const offset = rawOffset; const length = rawLength !== null && rawLength !== undefined ? rawLength : imageFrameLength - offset; const typedArrayConstructors = { Uint8Array, Uint16Array, Int16Array, Float32Array, Uint32Array, }; Iif (length !== imageFrame.pixelDataLength) { throw new Error( `target array for image does not have the same length (${length}) as the decoded image length (${imageFrame.pixelDataLength}).` ); } const TypedArrayConstructor = typedArrayConstructors[type]; // TypedArray.Set is api level and ~50x faster than copying elements even for // Arrays of different types, which aren't simply memcpy ops. const typedArray = arrayBuffer ? new TypedArrayConstructor(arrayBuffer, offset, length) : new TypedArrayConstructor(imageFrame.pixelData); Iif (length !== imageFrame.pixelDataLength) { throw new Error( 'target array for image does not have the same length as the decoded image length.' ); } imageFrame.pixelData = typedArray; alreadyTyped = true; } if (!alreadyTyped) { setPixelDataType(imageFrame); } const imagePlaneModule: Types.ImagePlaneModuleMetadata = metaData.get(MetadataModules.IMAGE_PLANE, imageId) || {}; const voiLutModule = metaData.get(MetadataModules.VOI_LUT, imageId) || {}; const modalityLutModule = metaData.get(MetadataModules.MODALITY_LUT, imageId) || {}; const sopCommonModule: Types.SopCommonModuleMetadata = metaData.get(MetadataModules.SOP_COMMON, imageId) || {}; const calibrationModule = metaData.get(MetadataModules.CALIBRATION, imageId) || {}; const { rows, columns } = imageFrame; if (isColorImage) { if (isColorConversionRequired(imageFrame)) { canvas.height = imageFrame.rows; canvas.width = imageFrame.columns; const context = canvas.getContext('2d'); let imageData = context.createImageData( imageFrame.columns, imageFrame.rows ); Eif (!useRGBA) { // Use a hard coded 3 samples per pixel for the destination, as the // original samples per pixel may not be 3 for palette color imageData = { ...imageData, data: new Uint8ClampedArray( 3 * imageFrame.columns * imageFrame.rows ), }; } convertColorSpace(imageFrame, imageData.data, useRGBA); imageFrame.imageData = imageData; imageFrame.pixelData = imageData.data; imageFrame.pixelDataLength = imageData.data.length; } else if ( !useRGBA && imageFrame.pixelDataLength === 4 * rows * columns ) { // This case is the case where we need RGB (that is !useRGBA), and // we have RGBA (that is 4 values per pixel, not 3). For this case, // remove the A value. // Note: rendering libraries like vtk expect Uint8Array for RGB images // otherwise they will convert them to Float32Array which might be slow const colorBuffer = new Uint8Array( (imageFrame.pixelData.length / 4) * 3 ); // remove the A from the RGBA of the imageFrame imageFrame.pixelData = removeAFromRGBA( imageFrame.pixelData, colorBuffer ); imageFrame.pixelDataLength = imageFrame.pixelData.length; } // else { // No need to do any conversion - already RGB // Consider RGB to RGBA conversion? /** @todo check as any */ // calculate smallest and largest PixelValue of the converted pixelData const minMax = getMinMax(imageFrame.pixelData); imageFrame.smallestPixelValue = minMax.min; imageFrame.largestPixelValue = minMax.max; } // Set numberOfComponents based on photometric interpretation let numberOfComponents = imageFrame.samplesPerPixel; if (imageFrame.photometricInterpretation === 'PALETTE COLOR') { numberOfComponents = useRGBA ? 4 : 3; } const voxelManager = utilities.VoxelManager.createImageVoxelManager({ scalarData: imageFrame.pixelData, width: imageFrame.columns, height: imageFrame.rows, numberOfComponents: numberOfComponents, }); const image: DICOMLoaderIImage = { imageId, dataType: imageFrame.pixelData.constructor .name as Types.PixelDataTypedArrayString, color: isColorImage, calibration: calibrationModule, columnPixelSpacing: imagePlaneModule.columnPixelSpacing, columns: imageFrame.columns, height: imageFrame.rows, preScale: imageFrame.preScale, intercept: modalityLutModule.rescaleIntercept ? modalityLutModule.rescaleIntercept : 0, slope: modalityLutModule.rescaleSlope ? modalityLutModule.rescaleSlope : 1, invert: imageFrame.photometricInterpretation === 'MONOCHROME1', minPixelValue: imageFrame.smallestPixelValue, maxPixelValue: imageFrame.largestPixelValue, rowPixelSpacing: imagePlaneModule.rowPixelSpacing, rows: imageFrame.rows, sizeInBytes: imageFrame.pixelData.byteLength, width: imageFrame.columns, // use the first value for rendering, if other values // are needed later, it can be grabbed again from the voiLUtModule windowCenter: voiLutModule.windowCenter ? voiLutModule.windowCenter[0] : undefined, windowWidth: voiLutModule.windowWidth ? voiLutModule.windowWidth[0] : undefined, voiLUTFunction: voiLutModule.voiLUTFunction ? voiLutModule.voiLUTFunction : undefined, decodeTimeInMS: imageFrame.decodeTimeInMS, floatPixelData: undefined, imageFrame, voxelManager, rgba: isColorImage && useRGBA, getPixelData: () => imageFrame.pixelData, getCanvas: undefined, numberOfComponents: numberOfComponents, }; if (image.color) { image.getCanvas = function () { // the getCanvas function is used in the CPU rendering path // and it is used to use the canvas api to draw the image // instead of looping through the pixel data and drawing each pixel // to use the canvas api, we need to convert the pixel data to a // Uint8ClampedArray (which is what the canvas api expects) // and then we can use the putImageData api to draw the image // However, if the image already was loaded without the alpha channel // we need to add the alpha channel back in if (lastImageIdDrawn === imageId) { return canvas; } const width = image.columns; const height = image.rows; canvas.height = height; canvas.width = width; const ctx = canvas.getContext('2d'); const imageData = ctx.createImageData(width, height); const arr = imageFrame.pixelData; if (arr.length === width * height * 4) { for (let i = 0; i < arr.length; i++) { imageData.data[i] = arr[i]; } } // Set pixel data for RGB array else if (arr.length === width * height * 3) { let j = 0; for (let i = 0; i < arr.length; i += 3) { imageData.data[j++] = arr[i]; imageData.data[j++] = arr[i + 1]; imageData.data[j++] = arr[i + 2]; imageData.data[j++] = 255; } } imageFrame.pixelData = imageData.data; imageFrame.pixelDataLength = imageData.data.length; imageFrame.imageData = imageData; ctx.putImageData(imageFrame.imageData, 0, 0); lastImageIdDrawn = imageId; return canvas; }; } // Modality LUT Iif ( modalityLutModule.modalityLUTSequence && modalityLutModule.modalityLUTSequence.length > 0 && isModalityLUTForDisplay(sopCommonModule.sopClassUID) ) { image.modalityLUT = modalityLutModule.modalityLUTSequence[0]; } // VOI LUT Iif ( voiLutModule.voiLUTSequence && voiLutModule.voiLUTSequence.length > 0 ) { image.voiLUT = voiLutModule.voiLUTSequence[0]; } if (image.color) { // Note that by the DICOM definition, the window width and center are // 256/128 for an identity transform. image.windowWidth = 256; image.windowCenter = 128; } // set the ww/wc to cover the dynamic range of the image if no values are supplied if ( image.windowCenter === undefined || image.windowWidth === undefined ) { const windowLevel = utilities.windowLevel.toWindowLevel( image.imageFrame.smallestPixelValue, image.imageFrame.largestPixelValue ); image.windowWidth = windowLevel.windowWidth; image.windowCenter = windowLevel.windowCenter; } resolve(image); }, reject); } ); } export default createImage; |