-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcanvas.html
181 lines (144 loc) · 6.57 KB
/
canvas.html
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
<!DOCTYPE html>
<!-- Test: Typical fullscreen usage; autoload an image and overlay. -->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<!-- <script type="text/javascript" src="../release/current/nifti-reader.js"></script> -->
<script type="module">
import * as nifti from '../release/current/nifti-reader.js';
window.nifti = nifti.default;
console.log(nifti);
</script>
<script type="text/javascript">
function readNIFTI(name, data) {
var canvas = document.getElementById('myCanvas');
var slider = document.getElementById('myRange');
var niftiHeader, niftiImage;
// 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
var slices = niftiHeader.dims[3];
slider.max = slices - 1;
slider.value = Math.round(slices / 2);
slider.oninput = function() {
drawCanvas(canvas, slider.value, niftiHeader, niftiImage);
};
// draw slice
drawCanvas(canvas, slider.value, niftiHeader, niftiImage);
}
function drawCanvas(canvas, slice, niftiHeader, niftiImage) {
// get nifti dimensions
var cols = niftiHeader.dims[1];
var rows = niftiHeader.dims[2];
// set canvas dimensions to nifti slice dimensions
canvas.width = cols;
canvas.height = rows;
// make canvas image data
var ctx = canvas.getContext("2d");
var canvasImageData = ctx.createImageData(canvas.width, canvas.height);
// convert raw data to typed array based on nifti datatype
var 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
var sliceSize = cols * rows;
var sliceOffset = sliceSize * slice;
// draw pixels
for (var row = 0; row < rows; row++) {
var rowOffset = row * cols;
for (var col = 0; col < cols; col++) {
var offset = sliceOffset + rowOffset + col;
var 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 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;
}
function readFile(file) {
var blob = makeSlice(file, 0, file.size);
var reader = new FileReader();
reader.onloadend = function (evt) {
if (evt.target.readyState === FileReader.DONE) {
readNIFTI(file.name, evt.target.result);
}
};
reader.readAsArrayBuffer(blob);
}
function handleFileSelect(evt) {
var files = evt.target.files;
readFile(files[0]);
}
</script>
<title>NIFTI-Reader-JS Test</title>
</head>
<body>
<div id="select" style="font-family:sans-serif">
<h3>NIFTI-Reader-JS — JavaScript NIFTI Reader</h3>
<h4><a href="https://github.com/rii-mango/NIFTI-Reader-JS">https://github.com/rii-mango/NIFTI-Reader-JS</a></h4>
<p>Select a file: <input type="file" id="file" name="files" /></p>
<hr />
</div>
<div id="results">
<canvas id="myCanvas" width="100" height="100"></canvas><br />
<input type="range" min="1" max="100" value="50" class="slider" id="myRange">
</div>
<script type="text/javascript">
document.getElementById('file').addEventListener('change', handleFileSelect, false);
</script>
</body>
</html>