-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
418 lines (341 loc) · 11.2 KB
/
index.js
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
import { createEXR } from './third_party/exr.js';
const $ = (x) => document.querySelector(x);
const assert = (x, m = 'assert() failed') => { if (!x) { debugger; throw new Error(m); } };
const topCanvas = $('canvas');
const elStatus = $('#status');
// https://en.wikipedia.org/wiki/Y%E2%80%B2UV
const RGB_YUV = [1, 0, 1.13983, 1, -0.39465, -0.58060, 1, 2.03211, 0];
const YUV_RGB = [0.299, 0.587, 0.114, -0.14713, -0.28886, 0.436, 0.615, -0.51499, -0.10001];
const TXID_STEP = 100;
const MAX_CHANNELS = 6; // 5.1 ch maps to RGB (re,im) channels
const IMAGE_SIZE = [1024, 1024];
const AUDIO_SIZE = [1024, 2048];
const SAMPLE_RATE = 48000;
let bgThreads = []; // 3 total: one per channel
let base_txid = 0;
let textureHDR = []; // 1..3 Float32Arrays of (re,im) pairs.
let gamma = 2.0, brightness = 0.0; // HDR tone mapping: x -> A*pow(x,1.0/GAMMA)
window.onerror = (event, source, lineno, colno, error) => setStatus(error, 'error');
window.onunhandledrejection = (event) => setStatus(event.reason, 'error');
window.onload = () => init();
function init() {
$('#open_audio').onclick = () => openAudio();
$('#open_image').onclick = () => openImage();
$('#fft_rows').onclick = () => applyFFT();
$('#swap_reim').onclick = () => swapReIm();
$('#transpose').onclick = () => transposeImage();
$('#h_shift').onclick = () => shiftTexture();
$('#save_png').onclick = () => savePNG();
$('#save_exr').onclick = () => saveEXR();
$('#abs_square').onclick = () => squareAbs();
$('#gaussian').onclick = () => applyGaussianWindow();
$('#yuv_to_rgb').onclick = () => changeColorSpace(YUV_RGB);
$('#rgb_to_yuv').onclick = () => changeColorSpace(RGB_YUV);
$('#rect_to_disk').onclick = () => mapRectToDisk();
$('svg').onclick = (e) => setToneMapping(e);
initWorkerThreads();
updateSVG();
setStatus('Ready');
}
function setStatus(text, type = '') {
elStatus.textContent = text;
elStatus.className = type;
}
async function openAudio() {
let file = await openFile('audio/*');
if (!file) return;
let [w, h] = AUDIO_SIZE;
topCanvas.width = w;
topCanvas.height = h;
updateSVG();
setStatus('Decoding audio file...');
let channels = await decodeAudio(file);
console.log('Audio channels:', channels.length);
if (channels.length > MAX_CHANNELS)
console.warn('Audio contains more than 3 channels:', channels.length);
textureHDR.length = 0;
for (let ch = 0; ch < channels.length && ch < MAX_CHANNELS; ch++) {
let len = channels[ch].length;
let tex_ch = ch / 2 | 0;
let tex = textureHDR[tex_ch] || new Float32Array(h * w * 2);
let step = (len - w) / h | 0;
assert((h - 1) * step + w - 1 < len);
for (let y = 0; y < h; y++)
for (let x = 0; x < w; x++)
tex[(y * w + x) * 2 + (ch % 2)] = channels[ch][y * step + x];
textureHDR[tex_ch] = tex;
}
drawTextureHDR();
}
async function openImage() {
let file = await openFile('image/*');
if (!file) return;
let [w, h] = IMAGE_SIZE;
topCanvas.width = w;
topCanvas.height = h;
updateSVG();
setStatus('Decoding image file...');
let img = await drawImage(file, topCanvas);
textureHDR.length = 0;
for (let ch = 0; ch < 3; ch++) {
textureHDR[ch] = new Float32Array(h * w * 2);
for (let i = 0; i < h * w; i++)
textureHDR[ch][i * 2] = img.data[i * 4 + ch] / 256;
}
drawTextureHDR();
}
function openFile(mime_type) {
let input = document.createElement('input');
input.type = 'file';
input.accept = mime_type;
input.multiple = false;
input.click();
return new Promise(resolve =>
input.onchange = () =>
resolve(input.files[0]));
}
/// Image related utils.
function changeColorSpace(matrix_3x3) {
let [m11, m12, m13, m21, m22, m23, m31, m32, m33] = matrix_3x3;
let [h, w] = getTextureHW(), hw2 = h * w * 2;
if (textureHDR.length == 0)
return;
for (let i = 0; i < 3; i++)
textureHDR[i] = textureHDR[i] || new Float32Array(hw2);
let [rr, gg, bb] = textureHDR;
setStatus('Changing color space...');
for (let i = 0; i < hw2; i++) {
let r = rr[i], g = gg[i], b = bb[i];
rr[i] = m11 * r + m12 * g + m13 * b;
gg[i] = m21 * r + m22 * g + m23 * b;
bb[i] = m31 * r + m32 * g + m33 * b;
}
drawTextureHDR();
}
function getTextureHW() {
return [topCanvas.height, topCanvas.width];
}
function shiftTexture() {
let h = topCanvas.height;
let w = topCanvas.width;
let tmp = new Float32Array(w * 2);
setStatus('Shifting texture...');
for (let tex of textureHDR) {
for (let y = 0; y < h; y++) {
let scanline = tex.subarray(y * w * 2, (y + 1) * w * 2);
tmp.set(scanline);
scanline.set(tmp.subarray(w));
scanline.set(tmp.subarray(0, w), w);
}
}
drawTextureHDR();
}
async function mapRectToDisk() {
let txid = base_txid;
setStatus('Mapping texture to polar coordinates...');
let tasks = textureHDR.map(async (tex, ch) => {
let [h, w] = getTextureHW();
let args = [tex, [h, w]];
let res = await postThreadMessage(ch, txid + ch, mapRectToDisk.name, args);
tex.set(res);
});
await Promise.all(tasks);
drawTextureHDR();
}
// This can be done with WebGL.
async function mapTextureToRGBA(rgba, max = 1.0, scale = 1.0, contrast = 1.0) {
let txid = base_txid;
let tasks = textureHDR.map(async (tex, ch) => {
let res = rgba.slice(0, tex.length / 2);
let args = [res, tex, max, scale, contrast];
res = await postThreadMessage(ch, txid + ch, mapTextureToRGBA.name, args, [res.buffer]);
for (let i = 0; i < res.length; i++)
rgba[i * 4 + ch] = res[i];
});
await Promise.all(tasks);
}
function setToneMapping(e) {
let svg = $('svg');
let x = (e.clientX - svg.parentElement.offsetLeft) / svg.clientWidth;
let y = 1 - (e.clientY - svg.parentElement.offsetTop) / svg.clientHeight;
let [xmin, ymin, svgw, svgh] = svg.getAttribute('viewBox').split(' ').map(x => +x);
gamma = xmin + x * svgw;
brightness = ymin + y * svgh;
updateSVG();
drawTextureHDR();
}
function updateSVG() {
let dot = $('svg circle');
dot.setAttribute('cx', gamma);
dot.setAttribute('cy', brightness);
$('#alpha').textContent = brightness.toFixed(2);
$('#gamma').textContent = gamma.toFixed(2);
}
async function drawTextureHDR() {
setStatus('Drawing the texture...');
dropPendingTXIDs();
let ts = Date.now();
let ctx = topCanvas.getContext('2d');
let w = topCanvas.width, h = topCanvas.height;
let img = ctx.getImageData(0, 0, w, h);
new Int32Array(img.data.buffer).fill(0xFF000000); // R,G,B,A = 0,0,0,1
await mapTextureToRGBA(img.data, 0xFF, 10 ** brightness, 1.0 / gamma);
ctx.putImageData(img, 0, 0);
setStatus('drawTexture time: ' + (Date.now() - ts) + ' ms');
}
async function drawImage(blob, canvas) {
let img = new Image;
img.src = URL.createObjectURL(blob);
await new Promise((resolve, reject) => {
img.onload = resolve;
img.onerror = reject;
});
let ctx = canvas.getContext('2d');
let w = canvas.width, h = canvas.height;
ctx.drawImage(img, 0, 0, w, h);
return ctx.getImageData(0, 0, w, h);
}
function transposeImage() {
let h = topCanvas.height;
let w = topCanvas.width;
setStatus('Transposing the texture...');
for (let tex of textureHDR) {
let tmp = new Float32Array(h * w * 2);
for (let y = 0; y < h; y++)
for (let x = 0; x < w; x++)
for (let i = 0; i < 2; i++)
tmp[(x * h + y) * 2 + i] = tex[(y * w + x) * 2 + i];
tex.set(tmp);
}
topCanvas.width = h;
topCanvas.height = w;
drawTextureHDR();
}
function genImageName(ext) {
let t = new Date().toJSON().replace(/[-:T]|\.\d+Z$/g, '');
return 'image_' + t + '.' + ext;
}
function saveBlobAsFile(blob, name) {
let a = document.createElement('a');
a.download = name;
a.href = URL.createObjectURL(blob);
a.click();
}
async function savePNG() {
setStatus('Creating an RGB x int16 PNG image');
dropPendingTXIDs();
let w = topCanvas.width;
let h = topCanvas.height;
let u16 = new Uint16Array(4 * h * w);
// PNG alpha=1.0
for (let i = 0; i < h * w; i++)
u16[i * 4 + 3] = 0xFFFF;
await mapTextureToRGBA(u16, 0xFFFF, 10 ** brightness, 1.0 / gamma);
// big-endian for PNG
let bswap = (b) => (b >> 8) | ((b & 255) << 8);
for (let i = 0; i < u16.length; i++)
u16[i] = bswap(u16[i]);
let png = UPNG.encodeLL([u16.buffer], w, h, 3, 1, 16);
let blob = new Blob([png], { type: 'image/png' });
saveBlobAsFile(blob, genImageName('png'));
}
async function saveEXR() {
setStatus('Creating an RGB x float32 EXR image');
dropPendingTXIDs();
let w = topCanvas.width;
let h = topCanvas.height;
let rgba = new Float32Array(w * h * 4);
await mapTextureToRGBA(rgba, 1.0, 10 ** brightness, 1.0 / gamma);
let blob = createEXR(w, h, 3, rgba);
saveBlobAsFile(blob, genImageName('exr'));
}
/// Audio related utils.
async function decodeAudio(blob) {
let encoded_data = await blob.arrayBuffer();
let ctx = new AudioContext({ sampleRate: SAMPLE_RATE });
try {
let cloned_data = encoded_data.slice(0);
let audio_buffer = await ctx.decodeAudioData(cloned_data);
let channels = [];
for (let i = 0; i < audio_buffer.numberOfChannels; i++)
channels[i] = audio_buffer.getChannelData(i);
return channels;
} finally {
ctx.close();
}
}
/// FFT related utils
function swap(a, i, j) {
let x = a[i]; a[i] = a[j]; a[j] = x;
}
function swapReIm() {
for (let tex of textureHDR)
for (let i = 0; i < tex.length / 2; i++)
swap(tex, 2 * i, 2 * i + 1);
drawTextureHDR();
}
function squareAbs() {
for (let tex of textureHDR) {
for (let i = 0; i < tex.length / 2; i++) {
let re = tex[2 * i], im = tex[2 * i + 1];
tex[2 * i] = re * re + im * im;
tex[2 * i + 1] = 0;
}
}
drawTextureHDR();
}
function applyGaussianWindow() {
let [h, w] = getTextureHW();
let mask = new Float32Array(w);
for (let x = 0; x < w; x++) {
let dx = Math.abs(x + 0.5 - w / 2) / w * 2; // -1..1
mask[x] = Math.exp(-dx * dx);
}
for (let tex of textureHDR)
for (let y = 0; y < h; y++)
for (let i = 0; i < w * 2; i++)
tex[y * w * 2 + i] *= mask[i >> 1];
drawTextureHDR();
}
async function applyFFT() {
let txid = dropPendingTXIDs();
let [h, w] = getTextureHW();
let tasks = textureHDR.map(async (tex, ch) => {
let args = [tex, [h, w]];
let res = await postThreadMessage(ch, txid + ch, applyFFT.name, args);
tex.set(res);
});
await Promise.all(tasks);
drawTextureHDR();
}
/// Worker thread related utils
function dropPendingTXIDs() {
return base_txid += TXID_STEP;
}
function initWorkerThreads() {
for (let ch = 0; ch < MAX_CHANNELS; ch++) {
let w = new Worker('thread.js', { type: 'module' });
w.onmessage = processThreadMessage;
w.promises = {};
bgThreads[ch] = w;
}
}
function postThreadMessage(ch, txid, fn, args, transfer) {
return new Promise((resolve, reject) => {
let ts = Date.now();
bgThreads[ch].promises[txid] = { resolve, reject };
bgThreads[ch].postMessage({ txid, ts, ch, fn, args }, transfer);
});
}
function processThreadMessage(message) {
let { txid, ch, ts, fn, res, err } = message.data;
//console.debug('Message from bg thread', ch, fn, 'ts diff', Date.now() - ts, 'ms');
if (txid < base_txid) {
console.warn('Dropped outdated response: txid', txid, '<', base_txid);
return;
}
let promises = bgThreads[ch].promises;
let promise = promises[txid];
delete promises[txid];
promise.resolve(res);
}