-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdoFrame.js
63 lines (49 loc) · 1.76 KB
/
doFrame.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
const fs = require("fs");
const getPixels = require("get-pixels");
const { toFourDigits } = require("./utilities");
let id;
let startIndex;
let endIndex;
function doFrame(id, index = 1, end = NaN) {
let indexString = toFourDigits(index.toString());
let path = `frames/frame_${indexString}.png`;
if (!isNaN(end) && index === end)
return process.exit();
getPixels(path, (err, pixels) => {
if (err)
return process.exit();
let string = "";
const symbols = "⠀⠃⠇⠏⠟⠿";
let widthCounter = 0;
for (let i = 0; i < pixels.data.length; i += 4) {
let value = (pixels.data[i] + pixels.data[i + 1] + pixels.data[i + 2]) / 3;
value = Math.max(pixels.data[i], pixels.data[i + 1], pixels.data[i + 2]);
// string += getCharacterForGrayScale(value) + getCharacterForGrayScale(value);
const index = Math.floor(value / (256 / 6));
string += symbols[index].repeat(2);
widthCounter++;
if (widthCounter === 120) {
widthCounter = 0;
string += "\n";
}
}
string += "\n";
// compress
const regexes = [/(⠀+)/g, /(⠃+)/g, /(⠇+)/g, /(⠏+)/g, /(⠟+)/g, /(⠿+)/g];
for (let i = 0; i < regexes.length; i++) {
const matches = string.match(regexes[i]) || [];
for (let match of matches) {
string = string.replace(match, symbols[i] + toFourDigits(match.length.toString()));
}
}
fs.writeFileSync(`./data/data_${id}.txt`, string, { flag: "a" }, (err) => {});
process.send("plus");
doFrame(id, index + 1, end);
});
}
process.on('message', (msg) => {
id = msg.id;
startIndex = msg.index;
endIndex = msg.end;
doFrame(msg.id, msg.index, msg.end);
});