forked from cornerstonejs/cornerstone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfalseColorMapping.js
More file actions
157 lines (129 loc) · 5.18 KB
/
Copy pathfalseColorMapping.js
File metadata and controls
157 lines (129 loc) · 5.18 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
(function(cornerstone) {
"use strict";
function getPixelValues(pixelData) {
var minPixelValue = Number.MAX_VALUE;
var maxPixelValue = Number.MIN_VALUE;
var len = pixelData.length;
var pixel;
for(var i = 0; i < len; i++) {
pixel = pixelData[i];
minPixelValue = minPixelValue < pixel ? minPixelValue : pixel;
maxPixelValue = maxPixelValue > pixel ? maxPixelValue : pixel;
}
return {
minPixelValue: minPixelValue,
maxPixelValue: maxPixelValue
};
}
function getRestoreImageMethod(image) {
if(image.restore) {
return image.restore;
}
var color = image.color;
var rgba = image.rgba;
var lut = image.lut;
var slope = image.slope;
var windowWidth = image.windowWidth;
var windowCenter = image.windowCenter;
var minPixelValue = image.minPixelValue;
var maxPixelValue = image.maxPixelValue;
return function() {
image.color = color;
image.rgba = rgba;
image.lut = lut;
image.slope = slope;
image.windowWidth = windowWidth;
image.windowCenter = windowCenter;
image.minPixelValue = minPixelValue;
image.maxPixelValue = maxPixelValue;
if(image.origPixelData) {
var pixelData = image.origPixelData;
image.getPixelData = function() {
return pixelData;
};
}
// Remove some attributes added by false color mapping
delete image.origPixelData;
delete image.colormapId;
delete image.falseColor;
};
}
// 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 as string.
function ensuresColormap(colormap) {
if(colormap && (typeof colormap === 'string')) {
colormap = cornerstone.colors.getColormap(colormap);
}
return colormap;
}
/**
* Restores a false color image to its original version
* @param image
*/
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
* @param colormap - it can be a colormap object or a colormap id (string)
*/
function convertImageToFalseColorImage(image, colormap) {
if (image.color && !image.falseColor) {
throw "Color transforms are not implemented yet";
}
// User can pass a colormap id or a colormap object
colormap = ensuresColormap(colormap);
var 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) {
var minPixelValue = image.minPixelValue || 0;
var maxPixelValue = image.maxPixelValue || 255;
var lookupTable;
image.restore = getRestoreImageMethod(image);
// cacheOriginalImageAttrs(image);
// var colormap = cornerstone.colors.getColormap(colormapId);
lookupTable = colormap.createLookupTable();
lookupTable.setTableRange(minPixelValue, maxPixelValue);
// Update the pixel data and render the new image
cornerstone.pixelDataToFalseColorData(image, lookupTable);
// Update min and max pixel values
var pixelValues = getPixelValues(image.getPixelData());
image.minPixelValue = pixelValues.minPixelValue;
image.maxPixelValue = pixelValues.maxPixelValue;
// 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 element
* @param colormap - it can be a colormap object or a colormap id (string)
*/
function convertToFalseColorImage(element, colormap) {
var enabledElement = cornerstone.getEnabledElement(element);
var falseColorImageUpdated = convertImageToFalseColorImage(enabledElement.image, colormap);
if(falseColorImageUpdated) {
cornerstone.updateImage(element, true);
}
}
cornerstone.convertImageToFalseColorImage = convertImageToFalseColorImage;
cornerstone.convertToFalseColorImage = convertToFalseColorImage;
cornerstone.restoreImage = restoreImage;
}(cornerstone));