-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhelpers.ts
48 lines (42 loc) · 1.01 KB
/
helpers.ts
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
export class Helpers {
/**
* Returns the index of the first occurrence of the maximum value along an array.
* @param arr
*/
argmax(arr): number {
let index = 0;
let value = arr[0];
arr.forEach((item, key) => {
if (item > value) {
value = item;
index = key;
}
});
return index;
}
/**
* TODO: document
* @param buffer
*/
normalizeRGB(buffer: any): Float32Array {
const npixels = buffer.length / 4;
const out = new Float32Array(npixels * 3);
for (let i = 0; i < npixels; i++) {
out[3 * i] = buffer[4 * i] / 128 - 1;
out[3 * i + 1] = buffer[4 * i + 1] / 128 - 1;
out[3 * i + 2] = buffer[4 * i + 2] / 128 - 1;
}
return out;
}
/**
* TODO: document
* @param buffer
*/
bufferToFloat32Array(buffer: Buffer): Float32Array {
const outArray = new Float32Array(buffer.length / 4);
for (let i = 0; i < outArray.length; i++) {
outArray[i] = buffer.readFloatLE(4 * i);
}
return outArray;
}
}