-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathcreateImage.js
More file actions
416 lines (352 loc) · 13.5 KB
/
createImage.js
File metadata and controls
416 lines (352 loc) · 13.5 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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import external from '../externalModules.js';
import getImageFrame from './getImageFrame.js';
import decodeImageFrame from './decodeImageFrame.js';
import isColorImageFn from './isColorImage.js';
import convertColorSpace from './convertColorSpace.js';
import getMinMax from '../shared/getMinMax.js';
import isJPEGBaseline8BitColor from './isJPEGBaseline8BitColor.js';
import { getOptions } from './internal/options.js';
import getScalingParameters from './getScalingParameters.js';
let lastImageIdDrawn = '';
function isModalityLUTForDisplay(sopClassUid) {
// special case for XA and XRF
// https://groups.google.com/forum/#!searchin/comp.protocols.dicom/Modality$20LUT$20XA/comp.protocols.dicom/UBxhOZ2anJ0/D0R_QP8V2wIJ
return (
sopClassUid !== '1.2.840.10008.5.1.4.1.1.12.1' && // XA
sopClassUid !== '1.2.840.10008.5.1.4.1.1.12.2.1'
); // XRF
}
function convertToIntPixelData(floatPixelData) {
const floatMinMax = getMinMax(floatPixelData);
const floatRange = Math.abs(floatMinMax.max - floatMinMax.min);
const intRange = 65535;
const slope = floatRange / intRange;
const intercept = floatMinMax.min;
const numPixels = floatPixelData.length;
const intPixelData = new Uint16Array(numPixels);
let min = 65535;
let max = 0;
for (let i = 0; i < numPixels; i++) {
const rescaledPixel = Math.floor((floatPixelData[i] - intercept) / slope);
intPixelData[i] = rescaledPixel;
min = Math.min(min, rescaledPixel);
max = Math.max(max, rescaledPixel);
}
return {
min,
max,
intPixelData,
slope,
intercept,
};
}
/**
* Helper function to set pixel data to the right typed array. This is needed because web workers
* can transfer array buffers but not typed arrays
* @param imageFrame
*/
function setPixelDataType(imageFrame, preScale) {
const isScaled = preScale?.scaled;
const scalingParmeters = preScale?.scalingParameters;
let isNegative = false;
if (isScaled && scalingParmeters) {
const rescaleSlope = scalingParmeters?.rescaleSlope || 1;
const rescaleIntercept = scalingParmeters?.rescaleIntercep || 0;
const suvbw = scalingParmeters?.suvbw || 1;
isNegative =
suvbw *
(imageFrame.smallestPixelValue * rescaleSlope + rescaleIntercept) <
0;
}
if (imageFrame.bitsAllocated === 32) {
imageFrame.pixelData = new Float32Array(imageFrame.pixelData);
} else if (imageFrame.bitsAllocated === 16) {
if (imageFrame.pixelRepresentation === 0 && !isNegative) {
imageFrame.pixelData = new Uint16Array(imageFrame.pixelData);
} else {
imageFrame.pixelData = new Int16Array(imageFrame.pixelData);
}
} else {
imageFrame.pixelData = new Uint8Array(imageFrame.pixelData);
}
}
/**
* Removes the A from RGBA to return RGB buffer, this is used when the
* decoding happens with browser API which results in RGBA, but if useRGBA flag
* is set to false, we want to return RGB
*
* @param imageFrame - decoded image in RGBA
* @param targetBuffer - target buffer to write to
*/
function removeAFromRGBA(imageFrame, targetBuffer) {
const numPixels = imageFrame.length / 4;
let rgbIndex = 0;
let bufferIndex = 0;
for (let i = 0; i < numPixels; i++) {
targetBuffer[bufferIndex++] = imageFrame[rgbIndex++]; // red
targetBuffer[bufferIndex++] = imageFrame[rgbIndex++]; // green
targetBuffer[bufferIndex++] = imageFrame[rgbIndex++]; // blue
rgbIndex++; // skip alpha
}
return targetBuffer;
}
function createImage(imageId, pixelData, transferSyntax, options = {}) {
// 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
let useRGBA = true;
if (options.useRGBA !== undefined) {
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
: false,
};
if (!pixelData || !pixelData.length) {
return Promise.reject(new Error('The file does not contain image data.'));
}
const { cornerstone } = external;
const canvas = document.createElement('canvas');
const imageFrame = getImageFrame(imageId);
// Get the scaling parameters from the metadata
if (options.preScale.enabled) {
const scalingParameters = getScalingParameters(
cornerstone.metaData,
imageId
);
if (scalingParameters) {
options.preScale = {
...options.preScale,
scalingParameters,
};
}
}
const { decodeConfig } = getOptions();
const decodePromise = decodeImageFrame(
imageFrame,
transferSyntax,
pixelData,
canvas,
options,
decodeConfig
);
const { convertFloatPixelDataToInt, use16BitDataType } = decodeConfig;
return new Promise((resolve, reject) => {
// eslint-disable-next-line complexity
decodePromise.then(function (imageFrame) {
// if it is desired to skip creating image, return the imageFrame
// after the decode. This might be useful for some applications
// that only need the decoded pixel data and not the image object
if (options.skipCreateImage) {
return resolve(imageFrame);
}
// If we have a target buffer that was written to in the
// Decode task, point the image to it here.
// We can't have done it within the thread incase it was a SharedArrayBuffer.
let alreadyTyped = false;
if (options.targetBuffer) {
let offset, length;
// If we have a target buffer, write to that instead. This helps reduce memory duplication.
({ offset, length } = options.targetBuffer);
const { arrayBuffer, type } = options.targetBuffer;
let TypedArrayConstructor;
if (length === null || length === undefined) {
length = imageFrame.pixelDataLength;
}
if (offset === null || offset === undefined) {
offset = 0;
}
switch (type) {
case 'Uint8Array':
TypedArrayConstructor = Uint8Array;
break;
case use16BitDataType && 'Uint16Array':
TypedArrayConstructor = Uint16Array;
break;
case use16BitDataType && 'Int16Array':
TypedArrayConstructor = Int16Array;
break;
case 'Float32Array':
TypedArrayConstructor = Float32Array;
break;
default:
throw new Error(
'target array for image does not have a valid type.'
);
}
if (length !== imageFrame.pixelDataLength) {
throw new Error(
'target array for image does not have the same length as the decoded image length.'
);
}
// TypedArray.Set is api level and ~50x faster than copying elements even for
// Arrays of different types, which aren't simply memcpy ops.
let typedArray;
if (arrayBuffer) {
typedArray = new TypedArrayConstructor(arrayBuffer, offset, length);
} else {
typedArray = new TypedArrayConstructor(imageFrame.pixelData);
}
// If need to scale, need to scale correct array.
imageFrame.pixelData = typedArray;
alreadyTyped = true;
}
if (!alreadyTyped) {
setPixelDataType(imageFrame, imageFrame.preScale);
}
const imagePlaneModule =
cornerstone.metaData.get('imagePlaneModule', imageId) || {};
const voiLutModule =
cornerstone.metaData.get('voiLutModule', imageId) || {};
const modalityLutModule =
cornerstone.metaData.get('modalityLutModule', imageId) || {};
const sopCommonModule =
cornerstone.metaData.get('sopCommonModule', imageId) || {};
const isColorImage = isColorImageFn(imageFrame.photometricInterpretation);
if (isColorImage) {
if (useRGBA) {
// JPEGBaseline (8 bits) is already returning the pixel data in the right format (rgba)
// because it's using a canvas to load and decode images.
if (!isJPEGBaseline8BitColor(imageFrame, transferSyntax)) {
canvas.height = imageFrame.rows;
canvas.width = imageFrame.columns;
const context = canvas.getContext('2d');
const imageData = context.createImageData(
imageFrame.columns,
imageFrame.rows
);
convertColorSpace(imageFrame, imageData.data, useRGBA);
imageFrame.imageData = imageData;
imageFrame.pixelData = imageData.data;
}
} else if (isJPEGBaseline8BitColor(imageFrame, transferSyntax)) {
// If we don't need the RGBA but the decoding is done with RGBA (the case
// for JPEG Baseline 8 bit color), AND the option specifies to use RGB (no RGBA)
// we need to remove the A channel from pixel data
const colorBuffer = new Uint8ClampedArray(
(imageFrame.pixelData.length / 4) * 3
);
// remove the A from the RGBA of the imageFrame
imageFrame.pixelData = removeAFromRGBA(
imageFrame.pixelData,
colorBuffer
);
} else if (imageFrame.photometricInterpretation === 'PALETTE COLOR') {
canvas.height = imageFrame.rows;
canvas.width = imageFrame.columns;
const context = canvas.getContext('2d');
const imageData = context.createImageData(
imageFrame.columns,
imageFrame.rows
);
convertColorSpace(imageFrame, imageData.data, true);
const colorBuffer = new imageData.data.constructor(
(imageData.data.length / 4) * 3
);
// remove the A from the RGBA of the imageFrame
imageFrame.pixelData = removeAFromRGBA(imageData.data, colorBuffer);
}
// calculate smallest and largest PixelValue of the converted pixelData
const minMax = getMinMax(imageFrame.pixelData);
imageFrame.smallestPixelValue = minMax.min;
imageFrame.largestPixelValue = minMax.max;
}
const image = {
imageId,
color: isColorImage,
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,
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,
rgba: isColorImage && useRGBA,
};
// If pixel data is intrinsically floating 32 array, we convert it to int for
// display in cornerstone. For other cases when pixel data is typed as
// Float32Array for scaling; this conversion is not needed.
if (
imageFrame.pixelData instanceof Float32Array &&
convertFloatPixelDataToInt
) {
const floatPixelData = imageFrame.pixelData;
const results = convertToIntPixelData(floatPixelData);
image.minPixelValue = results.min;
image.maxPixelValue = results.max;
image.slope = results.slope;
image.intercept = results.intercept;
image.floatPixelData = floatPixelData;
image.getPixelData = () => results.intPixelData;
} else {
image.getPixelData = () => imageFrame.pixelData;
}
if (image.color) {
image.getCanvas = function () {
if (lastImageIdDrawn === imageId) {
return canvas;
}
canvas.height = image.rows;
canvas.width = image.columns;
const context = canvas.getContext('2d');
context.putImageData(imageFrame.imageData, 0, 0);
lastImageIdDrawn = imageId;
return canvas;
};
}
// Modality LUT
if (
modalityLutModule.modalityLUTSequence &&
modalityLutModule.modalityLUTSequence.length > 0 &&
isModalityLUTForDisplay(sopCommonModule.sopClassUID)
) {
image.modalityLUT = modalityLutModule.modalityLUTSequence[0];
}
// VOI LUT
if (
voiLutModule.voiLUTSequence &&
voiLutModule.voiLUTSequence.length > 0
) {
image.voiLUT = voiLutModule.voiLUTSequence[0];
}
if (image.color) {
image.windowWidth = 255;
image.windowCenter = 127;
}
// 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 maxVoi = image.maxPixelValue * image.slope + image.intercept;
const minVoi = image.minPixelValue * image.slope + image.intercept;
image.windowWidth = maxVoi - minVoi;
image.windowCenter = (maxVoi + minVoi) / 2;
}
resolve(image);
}, reject);
});
}
export default createImage;