-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebview.html
493 lines (436 loc) · 17.3 KB
/
webview.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Monochrome Image Viewer with Line Profiles</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<div style="display: flex; align-items: center; height: 100%">
<div id="spinner"></div>
</div>
<div class="mainContainer" style="display: none">
<div id="imageGridContainer" class="grid-container">
<canvas id="loadedImage"></canvas>
<canvas id="yProfile" width="100"></canvas>
<canvas id="xProfile" height="100"></canvas>
<div class="info-corner">
<p id="headerTab" class="toggle-button">header ></p>
<p id="pixelValue"></p>
<p id="pixelPosition"></p>
</div>
</div>
<div
id="headerGridContainer"
class="grid-container"
style="display: none"
>
<div class="header-container">
<div class="search-controls">
<input
type="text"
id="searchInput"
placeholder="Search headers..."
/>
<div id="resetButton" title="reset search">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="m9.75 9.75 4.5 4.5m0-4.5-4.5 4.5M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"
/>
</svg>
</div>
</div>
<table id="headerTable"></table>
</div>
<div></div>
<div
style="
display: flex;
align-items: flex-end;
justify-content: center;
height: 100%;
"
>
<p style="font-size: 12px; opacity: 0.25">
Made with ❤️ by
<a
href="https://www.ppp.one/"
style="text-decoration-line: underline"
>Peter Pihlmann Pedersen</a
>
</p>
</div>
<div class="info-corner">
<p id="returnButton" class="toggle-button">< image</p>
</div>
</div>
</div>
<script>
const vscode = acquireVsCodeApi();
const spinner = document.getElementById("spinner");
const canvas = document.getElementById("loadedImage");
const ctx = canvas.getContext("2d");
const xProfileCanvas = document.getElementById("xProfile");
const xProfileCtx = xProfileCanvas.getContext("2d");
const yProfileCanvas = document.getElementById("yProfile");
const yProfileCtx = yProfileCanvas.getContext("2d");
let offscreenCanvas, offscreenCtx;
let imageWidth, imageHeight;
let currentTransform = d3.zoomIdentity;
let imageData = null;
let imageDataT = null;
let normalizedData = null;
let scaleFactor = 1;
let rect = null;
let scaleX = null;
let scaleY = null;
let headerData = {};
let profileColor = "ffffff";
let gridColor = "#e0e0e0";
const mainContainer = document.querySelector(".mainContainer");
const headerTab = document.getElementById("headerTab");
const headerGridContainer = document.getElementById(
"headerGridContainer"
);
const imageGridContainer = document.getElementById("imageGridContainer");
const returnButton = document.getElementById("returnButton");
const headerTable = document.getElementById("headerTable");
const searchInput = document.getElementById("searchInput");
const resetButton = document.getElementById("resetButton");
headerTab.addEventListener("click", () => {
imageGridContainer.style.display = "none";
headerGridContainer.style.display = "grid";
});
returnButton.addEventListener("click", () => {
headerGridContainer.style.display = "none";
imageGridContainer.style.display = "grid";
});
searchInput.addEventListener("input", () => {
const query = searchInput.value.toLowerCase();
const filteredData = Object.fromEntries(
Object.entries(headerData).filter(
([key, value]) =>
key.toLowerCase().includes(query) ||
value.toLowerCase().includes(query)
)
);
displayHeaderTable(filteredData);
});
resetButton.addEventListener("click", () => {
searchInput.value = "";
displayHeaderTable(headerData);
});
const mutationObserver = new MutationObserver((mutationsList, observer) => {
updateColor();
});
mutationObserver.observe(document.body, { childList: false, attributes: true })
function updateColor() {
const styles = getComputedStyle(document.querySelector('html'));
const editorCompositionBorder = styles.getPropertyValue('--vscode-editor-compositionBorder');
const editorForeGround = styles.getPropertyValue('--vscode-editor-foreground');
profileColor = editorCompositionBorder;
gridColor = editorForeGround;
}
function displayHeaderTable(data) {
headerTable.innerHTML = "";
// table header
const headerRow = document.createElement("tr");
const keyHeader = document.createElement("th");
const valueHeader = document.createElement("th");
const commentHeader = document.createElement("th");
keyHeader.textContent = "Key";
valueHeader.textContent = "Value";
commentHeader.textContent = "Comment";
headerRow.appendChild(keyHeader);
headerRow.appendChild(valueHeader);
headerRow.appendChild(commentHeader);
headerTable.appendChild(headerRow);
for (const [key, value] of Object.entries(data)) {
const row = document.createElement("tr");
const keyCell = document.createElement("td");
const valueCell = document.createElement("td");
const commentCell = document.createElement("td");
keyCell.textContent = key;
valueCell.textContent = value.split("/")[0];
commentCell.textContent = value.split("/")[1];
row.appendChild(keyCell);
row.appendChild(valueCell);
row.appendChild(commentCell);
headerTable.appendChild(row);
}
}
function drawLineProfile(profileCtx, profileData, isHorizontal, offset) {
// set canvas size
profileCtx.clearRect(
0,
0,
profileCtx.canvas.width,
profileCtx.canvas.height
);
// Find max value for scaling
const maxVal = Math.max(...profileData);
// Draw background grid
profileCtx.strokeStyle = gridColor;
profileCtx.beginPath();
if (isHorizontal) {
for (let i = 0; i <= 5; i++) {
const y = profileCtx.canvas.height * (1 - i / 5);
profileCtx.moveTo(0, y);
profileCtx.lineTo(profileCtx.canvas.width, y);
}
} else {
for (let i = 0; i <= 5; i++) {
const x = profileCtx.canvas.width * (1 - i / 5);
profileCtx.moveTo(x, 0);
profileCtx.lineTo(x, profileCtx.canvas.height);
}
}
profileCtx.stroke();
// Draw profile line
profileCtx.strokeStyle = profileColor;
profileCtx.beginPath();
if (isHorizontal) {
profileData.forEach((val, index) => {
const x =
((index - offset) / (imageWidth / currentTransform.k)) *
profileCtx.canvas.width;
const y = profileCtx.canvas.height * (1 - val / maxVal);
if (index === 0) {
profileCtx.moveTo(x, y);
} else {
const prevY =
profileCtx.canvas.height *
(1 - profileData[index - 1] / maxVal);
profileCtx.lineTo(x, prevY);
profileCtx.lineTo(x, y);
}
// Draw the last point off-screen
if (index == profileData.length - 1) {
const nextX =
((index + 1 - offset) / (imageWidth / currentTransform.k)) *
profileCtx.canvas.width;
profileCtx.lineTo(nextX, y);
}
});
} else {
profileData.forEach((val, index) => {
const x = profileCtx.canvas.width * (1 - val / maxVal);
const y =
((index - offset) / (imageHeight / currentTransform.k)) *
profileCtx.canvas.height;
if (index === 0) {
profileCtx.moveTo(x, y);
} else {
const prevX =
profileCtx.canvas.width * (1 - profileData[index - 1] / maxVal);
profileCtx.lineTo(prevX, y);
profileCtx.lineTo(x, y);
}
// Draw the last point off-screen
if (index == profileData.length - 1) {
const nextY =
((index + 1 - offset) / (imageHeight / currentTransform.k)) *
profileCtx.canvas.height;
profileCtx.lineTo(x, nextY);
}
});
}
profileCtx.stroke();
}
async function renderMonochromeImage(fileUri) {
// Step 1: Read the FITS file
console.time("renderMonochromeImage");
const response = await fetch(fileUri);
const arrayBuffer = await response.arrayBuffer();
console.timeLog("renderMonochromeImage", "FITS file loaded");
// Step 2: Create DataView for parsing
const dataView = new DataView(arrayBuffer);
console.timeLog("renderMonochromeImage", "DataView created");
// Step 3: Parse the FITS header and data
[headerData, normalizedData, imageWidth, imageHeight, imageData] =
parseFITSImage(arrayBuffer, dataView);
displayHeaderTable(headerData);
console.timeLog("renderMonochromeImage", "FITS header and data parsed");
// Step 4: Precompute the transposed data for vertical profiles
imageDataT = new Array(imageWidth);
for (let x = 0; x < imageWidth; x++) {
imageDataT[x] = new Array(imageHeight);
for (let y = 0; y < imageHeight; y++) {
imageDataT[x][y] = imageData[y * imageWidth + x];
}
}
console.timeLog("renderMonochromeImage", "Data transposed");
// Step 5: Compute the ImageData object for rendering
const canvasData = new ImageData(imageWidth, imageHeight);
const data = canvasData.data;
for (let i = 0; i < normalizedData.length; i++) {
const pixelValue = normalizedData[i];
const index = i * 4;
data[index] = data[index + 1] = data[index + 2] = pixelValue;
data[index + 3] = 255;
}
console.timeLog("renderMonochromeImage", "ImageData object created");
// Step 6: Render the image on the canvas
canvas.width = imageWidth;
canvas.height = imageHeight;
offscreenCanvas = document.createElement("canvas");
offscreenCanvas.width = imageWidth;
offscreenCanvas.height = imageHeight;
offscreenCtx = offscreenCanvas.getContext("2d");
offscreenCtx.putImageData(canvasData, 0, 0);
ctx.drawImage(offscreenCanvas, 0, 0);
ctx.webkitImageSmoothingEnabled = false;
ctx.mozImageSmoothingEnabled = false;
ctx.imageSmoothingEnabled = false;
console.timeLog("renderMonochromeImage", "Image rendered");
// rescale the canvas to fit the window
scaleFactor = Math.min(
window.innerWidth / imageWidth,
window.innerHeight / imageHeight
);
canvas.style.width = `${imageWidth * scaleFactor - 100}px`;
canvas.style.height = `${imageHeight * scaleFactor - 100}px`;
spinner.style.display = "none";
mainContainer.style.display = "grid";
rect = canvas.getBoundingClientRect();
scaleX = canvas.width / rect.width;
scaleY = canvas.height / rect.height;
// set width and height of line profile canvases
xProfileCanvas.width = rect.width;
yProfileCanvas.height = rect.height;
// set width of header-container to match the canvas width
document.querySelector(
".header-container"
).style.width = `${rect.width}px`;
// Add mousemove event listener to show pixel value and line profiles
canvas.addEventListener("mousemove", (event) => {
imageInteractionHandler(event, imageWidth, imageHeight);
});
console.timeEnd("renderMonochromeImage", "Image rendered finished");
}
function imageInteractionHandler(event, width, height) {
const x = Math.floor((event.clientX - rect.left) * scaleX);
const y = Math.floor((event.clientY - rect.top) * scaleY);
// Apply the current transform to get the actual pixel coordinates
const transformedX = Math.floor(
(x - currentTransform.x * scaleX) / currentTransform.k
);
const transformedY = Math.floor(
(y - currentTransform.y * scaleY) / currentTransform.k
);
// current x width and y height in terms of pixels in the image
const xWidth = Math.ceil(imageWidth / currentTransform.k);
const yHeight = Math.ceil(imageHeight / currentTransform.k);
// left and top of the image in terms of pixels in the image
const left = Math.floor(
(-currentTransform.x * scaleX) / currentTransform.k
);
const top = Math.floor(
(-currentTransform.y * scaleY) / currentTransform.k
);
if (
transformedX >= 0 &&
transformedX < width &&
transformedY >= 0 &&
transformedY < height
) {
// Extract X and Y line profiles of region shown in the canvas
const xProfile = imageData.slice(
transformedY * width + left,
transformedY * width + left + xWidth + 1
);
const yProfile = imageDataT[transformedX].slice(top, top + yHeight + 1);
// Draw line profiles
drawLineProfile(
xProfileCtx,
xProfile,
true,
(-currentTransform.x * scaleX) / currentTransform.k - left
);
drawLineProfile(
yProfileCtx,
yProfile,
false,
(-currentTransform.y * scaleY) / currentTransform.k - top
);
// Show pixel value
const pixelValue = formatNumber(
imageData[transformedY * width + transformedX],
2
);
document.getElementById("pixelValue").innerText = `${pixelValue}`;
document.getElementById(
"pixelPosition"
).innerText = `${transformedX}, ${transformedY}`;
}
}
function setupZoom() {
const zoom = d3
.zoom()
.scaleExtent([1, 100]) // Zoom range
.on("zoom", (event) => {
let transform = event.transform;
currentTransform = transform;
imageInteractionHandler(event.sourceEvent, imageWidth, imageHeight);
const right =
(transform.x - imageWidth / scaleX) / (transform.k / scaleX) +
imageWidth;
const left = transform.x / (transform.k / scaleX);
const top = transform.y / (transform.k / scaleY);
const bottom =
(transform.y - imageHeight / scaleY) / (transform.k / scaleY) +
imageHeight;
const right_limit = right < 0;
const bottom_limit = bottom < 0;
const top_limit = top > 0;
const left_limit = left > 0;
const right_scaled = (right * transform.k) / scaleX;
const left_scaled = (left * transform.k) / scaleX;
const top_scaled = (top * transform.k) / scaleY;
const bottom_scaled = (bottom * transform.k) / scaleY;
if (left_limit) {
transform.x = 0;
}
if (top_limit) {
transform.y = 0;
}
if (right_limit) {
transform.x = left_scaled - right_scaled;
}
if (bottom_limit) {
transform.y = top_scaled - bottom_scaled;
}
// Compute visible canvas size and resample from the original resolution
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.save();
ctx.translate(transform.x * scaleX, transform.y * scaleY);
ctx.scale(transform.k, transform.k);
// Redraw the original image with the appropriate transformation
ctx.drawImage(offscreenCanvas, 0, 0, imageWidth, imageHeight);
ctx.restore();
});
d3.select(canvas).call(zoom);
}
console.time("loadData");
window.addEventListener("message", (event) => {
const message = event.data;
if (message.command === "loadData") {
renderMonochromeImage(message.fileUri);
setupZoom(); // Initialize zoom after rendering
updateColor();
console.timeEnd("loadData");
}
});
vscode.postMessage({ command: "ready" });
</script>
</body>
</html>