-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
307 lines (257 loc) · 11.5 KB
/
index.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
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
<!--
Copyright (c) 2015, Jean-Francois Pambrun
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Jean-Francois Pambrun nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL JEAN FRANCOIS PAMBRUN BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="js/dicomParser.js"></script>
<script>
"use strict";
// =====
// initWebGL, initShaders, getShader adapted from
// from https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Tutorial/Getting_started_with_WebGL
// ====
function initWebGL(canvas) {
// TODO for cornerstone : use failIfMajorPerformanceCaveat to determine if fallback is required.
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.2.1
var gl = null;
try {
// Try to grab the standard context. If it fails, fallback to experimental.
gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
}
catch(e) {}
// If we don't have a GL context, give up now
if (!gl) {
alert("Unable to initialize WebGL. Your browser may not support it.");
gl = null;
}
return gl;
}
function initShaders(gl) {
var shaderProgram = null;
var fragmentShader = getShader(gl, "shader-fs");
var vertexShader = getShader(gl, "shader-vs");
// Create the shader program
shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
// If creating the shader program failed, alert
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Unable to initialize the shader program.");
}
gl.useProgram(shaderProgram);
return shaderProgram;
}
function getShader(gl, id) {
var shaderScript, theSource, currentChild, shader;
shaderScript = document.getElementById(id);
if (!shaderScript) {
return null;
}
theSource = "";
currentChild = shaderScript.firstChild;
while(currentChild) {
if (currentChild.nodeType == currentChild.TEXT_NODE) {
theSource += currentChild.textContent;
}
currentChild = currentChild.nextSibling;
}
if (shaderScript.type == "x-shader/x-fragment") {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (shaderScript.type == "x-shader/x-vertex") {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
// Unknown shader type
return null;
}
gl.shaderSource(shader, theSource);
// Compile the shader program
gl.compileShader(shader);
// See if it compiled successfully
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert("An error occurred compiling the shaders: " + gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
window.onload = main;
function main() {
var texCoordBuffer;
var posbuffer;
//fetch .dcm file
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://jpx.jfpb.net/data/raw.dcm", true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
if (!(xhr.status === 200 || xhr.status === 206)) {
throw Error(xhr.status + " " + xhr.statusText + ": " + parsedId.url);
}
//Obtain pixel data
var dcmData = new Uint8Array(xhr.response);
var dataSet = dicomParser.parseDicom(dcmData);
var pixelDataElement = dataSet.elements.x7fe00010;
var pixelData = new Uint16Array(dataSet.byteArray.buffer, pixelDataElement.dataOffset, pixelDataElement.length/2);
// Get A WebGL context
var canvas = document.getElementById("canvas");
var gl = initWebGL(canvas);
var program = initShaders(gl);
gl.clearColor(0.5, 0.0, 0.0, 1.0);
// Transfer image data.
// Some WebGL implementation supports floats components that could be used with medical images.
// However, it rely a optional extension that is not supported by all hardware.
// Furthermore, float textures have 4 channels (usually for RGBA) which means that each pixel requires
// 16 bytes (4 floats32) of memory. To mitigate both issues, I have decided pack 16 bit in the 2 first
// uint8 components (r and g). b and a are still available for other purposes.
// In the demo, I just concatenate a 512x512 image 4 times in each direction to create a 2048x2048 image.
var width = 2048;
var height = 2048;
var numberOfChannels = 4;
var format = gl.RGBA;
// GL texture configuration
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
// Create texture, pack uint16 into two uint8 (r and g) and concatenate.
var data = new Uint8Array(width * height * numberOfChannels);
// ii+=4 iterates over each pixels, not components.
for (var ii = 0; ii < data.length; ii+=4) {
// ugly modulo magic to translate ii to image coordinate and concatenate.
var x = Math.floor(ii/4)%width;
var y = Math.floor(Math.floor(ii/4)/height);
var val = pixelData[(y%512)*512+(x%512)];
// uint16 -> [uint8, uint8, ~, ~]
// Only unsigned is implemented. Shader will also need to support other formats.
data[ii+0] = (val & 0x0000FF00) >> 8;
data[ii+1] = (val & 0x000000FF);
data[ii+2] = 0;
data[ii+4] = 0;
}
gl.texImage2D(gl.TEXTURE_2D, 0, format, width, height, 0, format, gl.UNSIGNED_BYTE, data);
// look up where the vertex data needs to go.
var positionLocation = gl.getAttribLocation(program, "a_position");
var texCoordLocation = gl.getAttribLocation(program, "a_texCoord");
// provide texture coordinates for the rectangle.
texCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
0.0, 0.0,
1.0, 0.0,
0.0, 1.0,
0.0, 1.0,
1.0, 0.0,
1.0, 1.0]), gl.STATIC_DRAW);
gl.enableVertexAttribArray(texCoordLocation);
gl.vertexAttribPointer(texCoordLocation, 2, gl.FLOAT, false, 0, 0);
// set the resolution
var resolutionLocation = gl.getUniformLocation(program, "u_resolution");
gl.uniform2f(resolutionLocation, canvas.width, canvas.height);
// set initial window/level (vec2)
var wlLocation = gl.getUniformLocation(program, "u_wl");
gl.uniform2f(wlLocation, -160, 240 );
// set Slope Intercept (vec2)
var siLocation = gl.getUniformLocation(program, "u_slopeIntercept");
gl.uniform2f(siLocation, 1, -1000);
// Create a buffer for the position of the rectangle corners.
var posbuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, posbuffer);
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
setRectangle(gl, 0, 0, width, height);
var ii=0;
// redraw with changing W/L
// docs says requestAnimationFrame is better..
// setInterval(function(){
function step(timestamp) {
ii += 10;
var t0 = performance.now();
gl.uniform2f(wlLocation, -ii%1000-50, ii%1000+50 );
render(gl,texture, program);
var t1 = performance.now();
// console.log("Call render " + (t1 - t0) + " milliseconds.")
window.requestAnimationFrame(step);
}
window.requestAnimationFrame(step)
// }, 1);
}
xhr.send();
}
function render(gl) {
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES, 0, 6);
}
function setRectangle(gl, x, y, width, height) {
var x1 = x;
var x2 = x + width;
var y1 = y;
var y2 = y + height;
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
x1, y1,
x2, y1,
x1, y2,
x1, y2,
x2, y1,
x2, y2]), gl.STATIC_DRAW);
}
</script>
<!-- vertex shader -->
<script id="shader-vs" type="x-shader/x-vertex">
//copied from some tutorial, can't remember.
attribute vec2 a_position;
attribute vec2 a_texCoord;
uniform vec2 u_resolution;
varying vec2 v_texCoord;
void main() {
vec2 zeroToOne = a_position / u_resolution;
vec2 zeroToTwo = zeroToOne * 2.0;
vec2 clipSpace = zeroToTwo - 1.0;
gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1);
v_texCoord = a_texCoord;
}
</script>
<!-- fragment shader -->
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D u_image;
uniform vec2 u_wl;
uniform vec2 u_slopeIntercept;
varying vec2 v_texCoord;
void main() {
vec4 packedTextureElement = texture2D(u_image, v_texCoord);
// unpacking [ [uint8, uint8, ~, ~]] -> float and compute slope intercept
float grayLevel = (packedTextureElement[0]*255.0*256.0 + packedTextureElement[1]*255.0) * u_slopeIntercept[0] + u_slopeIntercept[1];
// W/L transformation.
float grayLevel_wl = clamp( ( grayLevel - u_wl[0] ) / (u_wl[1] - u_wl[0]), 0.0, 1.0);
// RGBA output
gl_FragColor = vec4(grayLevel_wl, grayLevel_wl, grayLevel_wl, 1);
}
</script>
</head>
<body>
<canvas id="canvas" width="2048" height="2048"></canvas>
</body>
</html>