forked from cornerstonejs/cornerstone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfalseColorMapping.js
187 lines (153 loc) · 5.32 KB
/
falseColorMapping.js
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
import { getEnabledElement } from './enabledElements.js';
import pixelDataToFalseColorData from './pixelDataToFalseColorData.js';
import { getColormap } from './colors/colormap.js';
/**
* Retrieves the minimum and maximum pixel values from an Array of pixel data
*
* @param {Array} pixelData The input pixel data array
*
* @returns {{minPixelValue: Number, maxPixelValue: Number}} The minimum and maximum pixel values in the input Array
*/
function getPixelValues (pixelData) {
let minPixelValue = Number.MAX_VALUE;
let maxPixelValue = Number.MIN_VALUE;
const len = pixelData.length;
let pixel;
for (let i = 0; i < len; i++) {
pixel = pixelData[i];
minPixelValue = minPixelValue < pixel ? minPixelValue : pixel;
maxPixelValue = maxPixelValue > pixel ? maxPixelValue : pixel;
}
return {
minPixelValue,
maxPixelValue
};
}
/**
* Retrieve a function that will allow an image object to be reset to its original form
* after a false color mapping transformation
*
* @param {Image} image A Cornerstone Image Object
*
* @return {Function} A function for resetting an Image Object to its original form
*/
function getRestoreImageMethod (image) {
if (image.restore) {
return image.restore;
}
const color = image.color;
const rgba = image.rgba;
const cachedLut = image.cachedLut;
const slope = image.slope;
const windowWidth = image.windowWidth;
const windowCenter = image.windowCenter;
const minPixelValue = image.minPixelValue;
const maxPixelValue = image.maxPixelValue;
return function () {
image.color = color;
image.rgba = rgba;
image.cachedLut = cachedLut;
image.slope = slope;
image.windowWidth = windowWidth;
image.windowCenter = windowCenter;
image.minPixelValue = minPixelValue;
image.maxPixelValue = maxPixelValue;
if (image.origPixelData) {
const pixelData = image.origPixelData;
image.getPixelData = () => pixelData;
}
// Remove some attributes added by false color mapping
image.origPixelData = undefined;
image.colormapId = undefined;
image.falseColor = undefined;
};
}
//
// Then we need to make sure it will be converted into a colormap object if it's as string.
/**
* User can pass a colormap or its id as string to some of these public functions.
* Then we need to make sure it will be converted into a colormap object if it's a string.
*
* @param {*} colormap A colormap ID or Object
* @return {*} The colormap
*/
function ensuresColormap (colormap) {
if (colormap && (typeof colormap === 'string')) {
colormap = getColormap(colormap);
}
return colormap;
}
/**
* Restores a false color image to its original version
*
* @param {Image} image A Cornerstone Image Object
* @returns {Boolean} True if the image object had a valid restore function, which was run. Otherwise, false.
*/
function restoreImage (image) {
if (image.restore && (typeof image.restore === 'function')) {
image.restore();
return true;
}
return false;
}
/**
* Convert an image to a false color image
*
* @param {Image} image A Cornerstone Image Object
* @param {String|Object} colormap - it can be a colormap object or a colormap id (string)
*
* @returns {Boolean} - Whether or not the image has been converted to a false color image
*/
function convertImageToFalseColorImage (image, colormap) {
if (image.color && !image.falseColor) {
throw new Error('Color transforms are not implemented yet');
}
// User can pass a colormap id or a colormap object
colormap = ensuresColormap(colormap);
const colormapId = colormap.getId();
// Doesn't do anything if colormapId hasn't changed
if (image.colormapId === colormapId) {
// It has already being converted into a false color image
// Using the colormapId passed as parameter
return false;
}
// Restore the image attributes updated when converting to a false color image
restoreImage(image);
// Convert the image to a false color image
if (colormapId) {
const minPixelValue = image.minPixelValue || 0;
const maxPixelValue = image.maxPixelValue || 255;
image.restore = getRestoreImageMethod(image);
const lookupTable = colormap.createLookupTable();
lookupTable.setTableRange(minPixelValue, maxPixelValue);
// Update the pixel data and render the new image
pixelDataToFalseColorData(image, lookupTable);
// Update min and max pixel values
const pixelValues = getPixelValues(image.getPixelData());
image.minPixelValue = pixelValues.minPixelValue;
image.maxPixelValue = pixelValues.maxPixelValue;
image.windowWidth = 255;
image.windowCenter = 128;
// Cache the last colormapId used for performance
// Then it doesn't need to be re-rendered on next
// Time if the user hasn't updated it
image.colormapId = colormapId;
}
// Return `true` to tell the caller that the image has got updated
return true;
}
/**
* Convert the image of a element to a false color image
*
* @param {HTMLElement} element The Cornerstone element
* @param {*} colormap - it can be a colormap object or a colormap id (string)
*
* @returns {void}
*/
function convertToFalseColorImage (element, colormap) {
const enabledElement = getEnabledElement(element);
return convertImageToFalseColorImage(enabledElement.image, colormap);
}
export { convertImageToFalseColorImage,
convertToFalseColorImage,
restoreImage };