[b86468]: / v3 / experimental_mode / CanvasVersion / js / mainNiftiReadingFunctions.js

Download this file

313 lines (242 with data), 11.3 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
// Mohamed Masoud 2021 (Sergey Plis Lab)
// For [1, 38, 38, 38, 1] input shape, MeshNet model
// Main parameters:
niftiHeader = [];
niftiImage = [];
labelNiftiHeader = [];
labelNiftiImage = [];
gtLabelLoaded = false;
// This part inspired from https://github.com/rii-mango/NIFTI-Reader-JS
readNIFTI = (data) => {
let inputCanvas = document.getElementById('inputCanvas');
let gtCanvas = document.getElementById('gtCanvas');
let outCanvas = document.getElementById('outputCanvas');
let outCC2D = document.getElementById('out2dCC');
let outCC3D = document.getElementById('out3dCC');
let slider = document.getElementById('sliceNav');
// parse nifti
if (nifti.isCompressed(data)) {
data = nifti.decompress(data);
}
if (nifti.isNIFTI(data)) {
niftiHeader = nifti.readHeader(data);
niftiImage = nifti.readImage(niftiHeader, data);
}
// set up slider
let slices = niftiHeader.dims[3];
slider.max = slices - 1;
// slider.min = 0;
slider.value = Math.round(slices / 2);
slider.oninput = function() {
document.getElementById('sliceNumId').innerHTML = slider.value;
drawInputCanvas(inputCanvas, slider.value, niftiHeader, niftiImage);
if(gtLabelLoaded) {
drawGtCanvas(gtCanvas, slider.value, labelNiftiHeader, labelNiftiImage);
}
if(allOutputSlices.length) {
drawOutputCanvas(outCanvas, slider.value, niftiHeader, niftiImage, allOutputSlices);
drawOutputCanvas(outCC2D, slider.value, niftiHeader, niftiImage, allOutputSlices2DCC);
drawOutputCanvas(outCC3D, slider.value, niftiHeader, niftiImage, allOutputSlices3DCC);
}
};
// draw slice
drawInputCanvas(inputCanvas, slider.value, niftiHeader, niftiImage);
}
readNIFTILabels = (data) => {
let gtCanvas = document.getElementById('gtCanvas');
let slider = document.getElementById('sliceNav');
// parse nifti
if (nifti.isCompressed(data)) {
data = nifti.decompress(data);
}
gtLabelLoaded = true;
if (nifti.isNIFTI(data)) {
labelNiftiHeader = nifti.readHeader(data);
labelNiftiImage = nifti.readImage(labelNiftiHeader, data);
}
// draw slice
drawGtCanvas(gtCanvas, slider.value, labelNiftiHeader, labelNiftiImage);
}
drawInputCanvas = (canvas, sliceIdx, niftiHeader, niftiImage) => {
// get nifti dimensions
let cols = niftiHeader.dims[1];
let rows = niftiHeader.dims[2];
// set canvas dimensions to nifti slice dimensions
canvas.width = cols;
canvas.height = rows;
// make canvas image data
let ctx = canvas.getContext("2d");
let canvasImageData = ctx.createImageData(canvas.width, canvas.height);
// convert raw data to typed array based on nifti datatype
let typedData;
if (niftiHeader.datatypeCode === nifti.NIFTI1.TYPE_UINT8) {
typedData = new Uint8Array(niftiImage);
} else if (niftiHeader.datatypeCode === nifti.NIFTI1.TYPE_INT16) {
typedData = new Int16Array(niftiImage);
} else if (niftiHeader.datatypeCode === nifti.NIFTI1.TYPE_INT32) {
typedData = new Int32Array(niftiImage);
} else if (niftiHeader.datatypeCode === nifti.NIFTI1.TYPE_FLOAT32) {
typedData = new Float32Array(niftiImage);
} else if (niftiHeader.datatypeCode === nifti.NIFTI1.TYPE_FLOAT64) {
typedData = new Float64Array(niftiImage);
} else if (niftiHeader.datatypeCode === nifti.NIFTI1.TYPE_INT8) {
typedData = new Int8Array(niftiImage);
} else if (niftiHeader.datatypeCode === nifti.NIFTI1.TYPE_UINT16) {
typedData = new Uint16Array(niftiImage);
} else if (niftiHeader.datatypeCode === nifti.NIFTI1.TYPE_UINT32) {
typedData = new Uint32Array(niftiImage);
} else {
return;
}
// offset to specified slice
let sliceSize = cols * rows;
let sliceOffset = sliceSize * sliceIdx;
// draw pixels
for (let row = 0; row < rows; row++) {
let rowOffset = row * cols;
for (let col = 0; col < cols; col++) {
let offset = sliceOffset + rowOffset + col;
let value = typedData[offset];
/*
Assumes data is 8-bit, otherwise you would need to first convert
to 0-255 range based on datatype range, data range (iterate through
data to find), or display range (cal_min/max).
Other things to take into consideration:
- data scale: scl_slope and scl_inter, apply to raw value before
applying display range
- orientation: displays in raw orientation, see nifti orientation
info for how to orient data
- assumes voxel shape (pixDims) is isometric, if not, you'll need
to apply transform to the canvas
- byte order: see littleEndian flag
*/
canvasImageData.data[(rowOffset + col) * 4] = value & 0xFF;
canvasImageData.data[(rowOffset + col) * 4 + 1] = value & 0xFF;
canvasImageData.data[(rowOffset + col) * 4 + 2] = value & 0xFF;
canvasImageData.data[(rowOffset + col) * 4 + 3] = 0xFF;
}
}
ctx.putImageData(canvasImageData, 0, 0);
}
function labelMax(arr){
let max = arr[0];
for(let i=1; i<arr.length; i++){
if(arr[i] > max){
max = arr[i];
}
}
return max;
}
drawGtCanvas = (canvas, sliceIdx, labelNiftiHeader, labelNiftiImage) => {
// get nifti dimensions
let cols = labelNiftiHeader.dims[1];
let rows = labelNiftiHeader.dims[2];
// set canvas dimensions to nifti slice dimensions
canvas.width = cols;
canvas.height = rows;
// make canvas image data
let ctx = canvas.getContext("2d");
let canvasImageData = ctx.createImageData(canvas.width, canvas.height);
// convert raw data to typed array based on nifti datatype
let typedData;
if (labelNiftiHeader.datatypeCode === nifti.NIFTI1.TYPE_UINT8) {
typedData = new Uint8Array(labelNiftiImage);
} else if (labelNiftiHeader.datatypeCode === nifti.NIFTI1.TYPE_INT16) {
typedData = new Int16Array(labelNiftiImage);
} else if (labelNiftiHeader.datatypeCode === nifti.NIFTI1.TYPE_INT32) {
typedData = new Int32Array(labelNiftiImage);
} else if (labelNiftiHeader.datatypeCode === nifti.NIFTI1.TYPE_FLOAT32) {
typedData = new Float32Array(labelNiftiImage);
} else if (labelNiftiHeader.datatypeCode === nifti.NIFTI1.TYPE_FLOAT64) {
typedData = new Float64Array(labelNiftiImage);
} else if (labelNiftiHeader.datatypeCode === nifti.NIFTI1.TYPE_INT8) {
typedData = new Int8Array(labelNiftiImage);
} else if (labelNiftiHeader.datatypeCode === nifti.NIFTI1.TYPE_UINT16) {
typedData = new Uint16Array(labelNiftiImage);
} else if (labelNiftiHeader.datatypeCode === nifti.NIFTI1.TYPE_UINT32) {
typedData = new Uint32Array(labelNiftiImage);
} else {
return;
}
document.getElementById("numOfClassesId").value = labelMax(typedData) + 1;
let n_classes = labelMax(typedData) + 1;
document.getElementById("results").innerHTML = "Found " + n_classes.toString().fontcolor("green").bold() + " classes in the ground truth";
// offset to specified slice
let sliceSize = cols * rows;
let sliceOffset = sliceSize * sliceIdx;
// draw pixels
for (let row = 0; row < rows; row++) {
let rowOffset = row * cols;
for (let col = 0; col < cols; col++) {
let offset = sliceOffset + rowOffset + col;
let value = typedData[offset];
/*
Assumes data is 8-bit, otherwise you would need to first convert
to 0-255 range based on datatype range, data range (iterate through
data to find), or display range (cal_min/max).
Other things to take into consideration:
- data scale: scl_slope and scl_inter, apply to raw value before
applying display range
- orientation: displays in raw orientation, see nifti orientation
info for how to orient data
- assumes voxel shape (pixDims) is isometric, if not, you'll need
to apply transform to the canvas
- byte order: see littleEndian flag
*/
value = Math.ceil(value*255/(n_classes - 1))
canvasImageData.data[(rowOffset + col) * 4] = value & 0xFF;
canvasImageData.data[(rowOffset + col) * 4 + 1] = value & 0xFF;
canvasImageData.data[(rowOffset + col) * 4 + 2] = value & 0xFF;
canvasImageData.data[(rowOffset + col) * 4 + 3] = 0xFF;
}
}
ctx.putImageData(canvasImageData, 0, 0);
}
makeSlice = (file, start, length) => {
var fileType = (typeof File);
if (fileType === 'undefined') {
return function () {};
}
if (File.prototype.slice) {
return file.slice(start, start + length);
}
if (File.prototype.mozSlice) {
return file.mozSlice(start, length);
}
if (File.prototype.webkitSlice) {
return file.webkitSlice(start, length);
}
return null;
}
readFile = (file, sourceId) => {
console.log("file is :", file)
var blob = makeSlice(file, 0, file.size);
var reader = new FileReader();
reader.onloadend = function (evt) {
if (evt.target.readyState === FileReader.DONE) {
document.getElementById("results").innerHTML = "";
//evt.target.result is : ArrayBuffer { byteLength: 763810 }
if(sourceId == "file") {
readNIFTI(evt.target.result);
document.getElementById("mriTitle").innerHTML = "MRI";
document.getElementById("groundTruthFile").disabled = false;
document.getElementById("file").disabled = true;
document.getElementById("runInferenceId").disabled = false;
}
if(sourceId == "groundTruthFile") {
readNIFTILabels(evt.target.result);
document.getElementById("gtTitle").innerHTML = "Ground Truth";
document.getElementById("groundTruthFile").disabled = true;
}
allOutputSlices = [];
allOutputSlices2DCC = [];
allOutputSlices3DCC = [];
}
};
reader.readAsArrayBuffer(blob);
}
handleFileSelect = (evt) => {
let files = evt.target.files;
let source = evt.target || evt.srcElement;
readFile(files[0],source.id);
}