-
Notifications
You must be signed in to change notification settings - Fork 256
/
registerWebImageLoader.ts
248 lines (210 loc) · 5.92 KB
/
registerWebImageLoader.ts
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
import * as cornerstone from '@cornerstonejs/core';
const canvas = document.createElement('canvas');
let lastImageIdDrawn;
// Todo: this loader should exist in a separate package in the same monorepo
/**
* creates a cornerstone Image object for the specified Image and imageId
*
* @param image - An Image
* @param imageId - the imageId for this image
* @returns Cornerstone Image Object
*/
function createImage(image, imageId) {
// extract the attributes we need
const rows = image.naturalHeight;
const columns = image.naturalWidth;
function getPixelData(targetBuffer) {
const imageData = getImageData();
let targetArray;
// Check if targetBuffer is provided for volume viewports
if (targetBuffer) {
targetArray = new Uint8Array(
targetBuffer.arrayBuffer,
targetBuffer.offset,
targetBuffer.length
);
} else {
targetArray = new Uint8Array(imageData.width * imageData.height * 3);
}
// modify original image data and remove alpha channel (RGBA to RGB)
convertImageDataToRGB(imageData, targetArray);
return targetArray;
}
function convertImageDataToRGB(imageData, targetArray) {
for (let i = 0, j = 0; i < imageData.data.length; i += 4, j += 3) {
targetArray[j] = imageData.data[i];
targetArray[j + 1] = imageData.data[i + 1];
targetArray[j + 2] = imageData.data[i + 2];
}
}
function getImageData() {
let context;
if (lastImageIdDrawn === imageId) {
context = canvas.getContext('2d');
} else {
canvas.height = image.naturalHeight;
canvas.width = image.naturalWidth;
context = canvas.getContext('2d');
context.drawImage(image, 0, 0);
lastImageIdDrawn = imageId;
}
return context.getImageData(0, 0, image.naturalWidth, image.naturalHeight);
}
function getCanvas() {
if (lastImageIdDrawn === imageId) {
return canvas;
}
canvas.height = image.naturalHeight;
canvas.width = image.naturalWidth;
const context = canvas.getContext('2d');
context.drawImage(image, 0, 0);
lastImageIdDrawn = imageId;
return canvas;
}
// Extract the various attributes we need
return {
imageId,
minPixelValue: 0,
maxPixelValue: 255,
slope: 1,
intercept: 0,
windowCenter: 128,
windowWidth: 255,
getPixelData,
getCanvas,
getImage: () => image,
rows,
columns,
height: rows,
width: columns,
color: true,
// we converted the canvas rgba already to rgb above
rgba: false,
columnPixelSpacing: 1, // for web it's always 1
rowPixelSpacing: 1, // for web it's always 1
invert: false,
sizeInBytes: rows * columns * 3,
};
}
function arrayBufferToImage(arrayBuffer) {
return new Promise((resolve, reject) => {
const image = new Image();
const arrayBufferView = new Uint8Array(arrayBuffer);
const blob = new Blob([arrayBufferView]);
const urlCreator = window.URL || window.webkitURL;
const imageUrl = urlCreator.createObjectURL(blob);
image.src = imageUrl;
image.onload = () => {
resolve(image);
urlCreator.revokeObjectURL(imageUrl);
};
image.onerror = (error) => {
urlCreator.revokeObjectURL(imageUrl);
reject(error);
};
});
}
//
// This is a cornerstone image loader for web images such as PNG and JPEG
//
const options = {
// callback allowing customization of the xhr (e.g. adding custom auth headers, cors, etc)
beforeSend: (xhr) => {
// xhr
},
};
// Loads an image given a url to an image
function loadImage(uri, imageId) {
const xhr = new XMLHttpRequest();
xhr.open('GET', uri, true);
xhr.responseType = 'arraybuffer';
options.beforeSend(xhr);
xhr.onprogress = function (oProgress) {
if (oProgress.lengthComputable) {
// evt.loaded the bytes browser receive
// evt.total the total bytes set by the header
const loaded = oProgress.loaded;
const total = oProgress.total;
const percentComplete = Math.round((loaded / total) * 100);
const eventDetail = {
imageId,
loaded,
total,
percentComplete,
};
cornerstone.triggerEvent(
cornerstone.eventTarget,
'cornerstoneimageloadprogress',
eventDetail
);
}
};
const promise = new Promise((resolve, reject) => {
xhr.onload = function () {
const imagePromise = arrayBufferToImage(this.response);
imagePromise
.then((image) => {
const imageObject = createImage(image, imageId);
resolve(imageObject);
}, reject)
.catch((error) => {
console.error(error);
});
};
xhr.onerror = function (error) {
reject(error);
};
xhr.send();
});
const cancelFn = () => {
xhr.abort();
};
return {
promise,
cancelFn,
};
}
function registerWebImageLoader(imageLoader): void {
imageLoader.registerImageLoader('web', _loadImageIntoBuffer);
}
/**
* Small stripped down loader from cornerstoneDICOMImageLoader
* Which doesn't create cornerstone images that we don't need
*/
function _loadImageIntoBuffer(
imageId: string,
options?: Record<string, any>
): { promise: Promise<Record<string, any>>; cancelFn: () => void } {
const uri = imageId.replace('web:', '');
const promise = new Promise((resolve, reject) => {
// get the pixel data from the server
loadImage(uri, imageId)
.promise.then(
(image) => {
if (
!options ||
!options.targetBuffer ||
!options.targetBuffer.length ||
!options.targetBuffer.offset
) {
resolve(image);
return;
}
// @ts-ignore
image.getPixelData(options.targetBuffer);
resolve(true);
},
(error) => {
reject(error);
}
)
.catch((error) => {
reject(error);
});
});
return {
promise,
cancelFn: undefined,
};
}
export default registerWebImageLoader;