-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstep1.html
344 lines (285 loc) · 13.2 KB
/
step1.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>STEP - WebGL2 </title>
<link rel="stylesheet" type="text/css" href="./css/jquery.dataTables.min.css" media="screen" />
<script src="./js/jquery-2.1.4.min.js"></script>
<script src="./js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="./js/gl-matrix-min.js"></script>
<script src="./js/pouchdb-5.1.0.js"></script>
<script src="./js/pouchdb.memory.js"></script>
<script type="text/javascript" src="../dcmio/DicomMetaDictionary.js"></script>
<script type="text/javascript" src="../dcmio/BufferStream.js"></script>
<script type="text/javascript" src="../dcmio/ValueRepresentation.js"></script>
<script type="text/javascript" src="../dcmio/Tag.js"></script>
<script type="text/javascript" src="../dcmio/DicomMessage.js"></script>
<script type="text/javascript" src="../dcmio/normalizers.js"></script>
<script type="text/javascript" src="./fields.js"></script>
<script type="text/javascript" src="./view.js"></script>
<script type="text/javascript" src="./space.js"></script>
<link rel="stylesheet" href="./css/step.css" />
<script type="text/javascript" src="./ui.js"></script>
<script type="text/javascript" src="./ui.step.js"></script>
</head>
<body>
<canvas id="renderCanvas"></canvas>
<script id="vertexShader" type="x-shader/x-vertex">#version 300 es
precision highp float;
in vec3 coordinate;
in vec2 textureCoordinate;
smooth out vec2 varyingTextureCoordinate;
void main(void) {
gl_Position = vec4(coordinate,1.);
varyingTextureCoordinate = textureCoordinate;
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">#version 300 es
precision highp float;
precision highp int;
precision highp isampler3D;
uniform highp sampler3D sourceTextureSampler;
uniform float sliceOffset;
uniform float windowCenter;
uniform float windowWidth;
in vec2 varyingTextureCoordinate;
out vec4 fragmentColor;
void main(void) {
vec3 point = vec3(varyingTextureCoordinate,sliceOffset);
vec4 outputColor = vec4(0., .4, .3, 1.);
// W/L per http://dicom.nema.org/MEDICAL/Dicom/2015c/output/chtml/part03/sect_C.11.2.html#sect_C.11.2.1.2
float pixelValue = 0.;
float steps = 50.;
float thickness = .25;
float increment = thickness / steps;
float start = thickness / -2.;
float end = start + thickness;
vec3 offset = vec3(0., 0., 0.);
for (float step = start; step < end; step+=increment) {
offset.z = step;
pixelValue += texture(sourceTextureSampler, point+offset).r;
}
pixelValue /= steps;
pixelValue = clamp( (pixelValue - (windowCenter-0.5)) / (windowWidth-1.) + .5, 0., 1. );
outputColor.rgb = vec3(pixelValue);
fragmentColor = outputColor;
}
</script>
<script>
'use strict'
// globals
let sliceOffset = 0.5;
let textureDimensions;
let renderImageCoordinatesBuffer;
let renderImageTexureCoordinatesBuffer;
let sourceTexture;
let renderCanvas = document.querySelector('#renderCanvas');
let sliceOffsetUI;
let gl = renderCanvas.getContext('webgl2');
gl.disable(gl.DEPTH_TEST); // no depth, we ray cast
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
// the program and shaders
let glProgram = gl.createProgram();
let vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, document.getElementById("vertexShader").innerHTML);
gl.compileShader(vertexShader);
if (!gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS)) {
alert('Could not compile vertexShader');
console.log(gl.getShaderInfoLog(vertexShader));
}
let fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragmentShader, document.getElementById("fragmentShader").innerHTML);
gl.compileShader(fragmentShader);
if (!gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS)) {
alert('Could not compile fragmentShader');
console.log(gl.getShaderInfoLog(fragmentShader));
}
gl.attachShader(glProgram, vertexShader);
gl.deleteShader(vertexShader);
gl.attachShader(glProgram, fragmentShader);
gl.deleteShader(fragmentShader);
gl.linkProgram(glProgram);
function setupDataset(dataset) {
// width by height by depth (x, y z)
textureDimensions = [dataset.Columns,dataset.Rows,dataset.NumberOfFrames];
textureDimensions = textureDimensions.map(Number);
if (dataset.BitsAllocated != 16) {
alert('Can only render 16 bit data');
return;
}
let imageArray;
if (dataset.PixelRepresentation == 1) {
imageArray = new Int16Array(dataset.PixelData);
} else {
imageArray = new Uint16Array(dataset.PixelData);
}
let imageFloat32Array = Float32Array.from(imageArray);
//
// set up webgl2
//
// buffers for the textured plane in normalized space
renderImageCoordinatesBuffer = gl.createBuffer();
renderImageTexureCoordinatesBuffer = gl.createBuffer();
let renderImageVertices = [ -1., -1., 0., 1., -1., 0., -1., 1., 0., 1., 1., 0., ];
gl.bindBuffer(gl.ARRAY_BUFFER, renderImageCoordinatesBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(renderImageVertices), gl.STATIC_DRAW);
let renderImageTextureCoordinates = [ 0, 1, 1, 1, 0, 0, 1, 0 ];
gl.bindBuffer(gl.ARRAY_BUFFER, renderImageTexureCoordinatesBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(renderImageTextureCoordinates), gl.STATIC_DRAW);
sourceTexture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_3D, sourceTexture);
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_BASE_LEVEL, 0);
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_MAX_LEVEL, 0);
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE);
gl.texStorage3D(gl.TEXTURE_3D, 1, gl.R32F,
textureDimensions[0], textureDimensions[1], textureDimensions[2]);
gl.texSubImage3D(gl.TEXTURE_3D,
0, 0, 0, 0, // level, offsets
textureDimensions[0], textureDimensions[1], textureDimensions[2],
gl.RED, gl.FLOAT, imageFloat32Array);
}
// render a frame
function render() {
gl.viewport(0, 0, renderCanvas.width, renderCanvas.height);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.useProgram(glProgram);
// set up the textureDimensions
gl.uniform3f(gl.getUniformLocation(glProgram, "textureDimensions"), textureDimensions[0], textureDimensions[1], textureDimensions[2]);
// the coordinate attribute
gl.bindBuffer(gl.ARRAY_BUFFER, renderImageCoordinatesBuffer);
let coordinateLocation = gl.getAttribLocation(glProgram, "coordinate");
gl.enableVertexAttribArray( coordinateLocation );
gl.vertexAttribPointer( coordinateLocation, 3, gl.FLOAT, false, 0, 0);
// the textureCoordinate attribute
gl.bindBuffer(gl.ARRAY_BUFFER, renderImageTexureCoordinatesBuffer);
let textureCoordinateLocation = gl.getAttribLocation(glProgram, "textureCoordinate");
gl.enableVertexAttribArray( textureCoordinateLocation );
gl.vertexAttribPointer( textureCoordinateLocation, 2, gl.FLOAT, false, 0, 0);
let sourceTextureSampler = gl.createSampler();
gl.bindSampler(0, sourceTextureSampler);
gl.uniform1i(gl.getUniformLocation(glProgram, "sourceTextureSampler"), sourceTextureSampler);
gl.uniform1f(gl.getUniformLocation(glProgram, "sliceOffset"), sliceOffset);
gl.uniform1f(gl.getUniformLocation(glProgram, "windowCenter"), Number(step.dataset.WindowCenter[0]));
gl.uniform1f(gl.getUniformLocation(glProgram, "windowWidth"), Number(step.dataset.WindowWidth[0]));
// final render of calculation to display
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_3D, sourceTexture);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
}
//
// user interface elements
//
let bottomBar;
function updateParameters() {
sliceOffset = bottomBar.sliceOffset;
render();
}
// once the data is downloaded
function readDICOM(arrayBuffer) {
let dicomData = DicomMessage.readFile(arrayBuffer);
let dataset = DicomMetaDictionary.naturalizeDataset(dicomData.dict);
return (dataset);
}
// example use:
// let url = 'http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.103997818648421156885408778508/object.dcm';
// requestInstance(url, function(ab) { let ds = DicomMessage.readFile(ab); console.log(DicomMetaDictionary.naturalizeDataset(ds.dict))})
function requestInstance(url, loaded) {
console.log('requesting', url);
let dataRequest = new XMLHttpRequest();
dataRequest.responseType = "arraybuffer";
dataRequest.onload = function (event) {
let arrayBuffer = dataRequest.response;
loaded(arrayBuffer);
};
dataRequest.onprogress = function () {
console.log('LOADING', dataRequest.status);
};
dataRequest.open("GET", url, true);
dataRequest.send(null);
}
var seriesDatasets = [];
function requestSeries(instanceURLs) {
instanceURLs.forEach(function(instanceURL) {
requestInstance(instanceURL, function(arrayBuffer) {
let dataset = readDICOM(arrayBuffer);
seriesDatasets.push(dataset);
if (seriesDatasets.length == instanceURLs.length) {
step.dataset = Normalizer.normalizeToDataset(seriesDatasets);
setupDataset(step.dataset);
render();
}
});
});
}
const dataURLs = [
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.103997818648421156885408778508/object.dcm",
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.132342573698930826478895707476/object.dcm",
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.132765832419882059380935428706/object.dcm",
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.133983309860870314301667037544/object.dcm",
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.183129620499202809352195734791/object.dcm",
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.185019404170243350547132089057/object.dcm",
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.190856693629143151966532006066/object.dcm",
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.213672649793036324016969672899/object.dcm",
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.220483229612542930590572673225/object.dcm",
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.223378409272621777170419332045/object.dcm",
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.228265524314313520313994911302/object.dcm",
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.237613705642967241727822941063/object.dcm",
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.276117299669179235189617200092/object.dcm",
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.291378801009832862134854162473/object.dcm",
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.291756004040853923387536996513/object.dcm",
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.318384541576876915009051710666/object.dcm",
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.324960553099781141817880090681/object.dcm",
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.328063687024673117172378941554/object.dcm",
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.566710636073578871359629982455/object.dcm",
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.613309369284887117391265294649/object.dcm",
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.676104320644886284956145587187/object.dcm",
"http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.897524458414944086987347883855/object.dcm",
];
// once document is loaded...
var step;
var multiframeMR;
$(function () {
step = new STEP();
let menubar = new stepMenubar(step, {
requestSeries : requestSeries
});
document.body.insertBefore(menubar.dom, document.body.firstChild);
bottomBar = new stepBottomBar(step, {
onSliceOffetChange : updateParameters
});
document.body.appendChild(bottomBar.dom);
if (true) {
let instanceURL;
// mr prostate
instanceURL = 'https://s3.amazonaws.com/IsomicsPublic/4-T2-multiframe-IPP_sorted_UID_fixed.dcm';
// mr prostate
instanceURL = 'http://quantome.org:5984/chronicle/1.3.6.1.4.1.14519.5.2.1.3671.7001.175674587621970014199598735325/object.dcm';
instanceURL = 'http://quantome.org:5984/chronicle/1.2.276.0.7230010.3.1.4.8323329.18234.1440001306.224431/object.dcm'
requestInstance(instanceURL, function(arrayBuffer) {
step.dataset = Normalizer.normalizeToDataset([readDICOM(arrayBuffer)]);
setupDataset(step.dataset);
render();
});
} else {
requestSeries(dataURLs);
}
});
let onResize = function() {
renderCanvas.width = window.innerWidth;
renderCanvas.height = window.innerHeight;
}
onResize();
window.onresize = onResize;
</script>
<p>
This demo uses <a href='https://www.khronos.org/registry/webgl/specs/latest/2.0/'>WebGL2</a>. Not all devices and browsers are supported. As of this writing only the latest Chrome Canary or Firefox Nightly will work. <a href='https://www.khronos.org/webgl/wiki/Getting_a_WebGL_Implementation'>See this page for details.</a>
</p>
</body>
</html>