-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_jpeg.js
59 lines (52 loc) · 1.23 KB
/
parse_jpeg.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
const inkjet = require("inkjet");
const fs = require("fs");
const filepath = "jun.jpg";
const buf = fs.readFileSync(filepath);
const convToBlackWhite = (arr) => {
const res = [];
let i = 0;
for (let i = 0; i < arr.length; i += 4) {
const cur =
(arr[i] * 0.2126 + arr[i + 1] * 0.7152 + arr[i + 2] * 0.0722) / 255;
if (cur >= 0.5) {
res.push(0);
} else {
res.push(1);
}
}
return res;
};
const saveToImage = (filename, arr, width, height) => {
const frameData = Buffer.alloc(width * height * 4);
let i = 0;
let k = 0;
while (i < frameData.length) {
if (arr[k] === 1) {
frameData[i++] = 0;
frameData[i++] = 0;
frameData[i++] = 0;
frameData[i++] = 0xff;
} else {
frameData[i++] = 255;
frameData[i++] = 255;
frameData[i++] = 255;
frameData[i++] = 0xff;
}
k++;
}
const buf = frameData;
const options = {
width: width,
height: height,
quality: 80,
};
inkjet.encode(buf, options, (err, encoded) => {
fs.writeFileSync(filename, encoded.data);
});
};
inkjet.decode(buf, (err, decoded) => {
console.log(decoded);
const h = convToBlackWhite(decoded.data);
console.log(h);
saveToImage("test.jpg", h, 30, 20);
});