This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.js
43 lines (36 loc) · 1.5 KB
/
main2.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
const unzipper = require('unzipper');
const fs = require('fs');
const PNG = require('pngjs').PNG;
const path = require('path');
// This implementation is trying to reduce IO ops by skipping the step of writing the unzipped files to disk.
// Instead, I pipe the unzipped files directly to the grayscale function.
const zipFilePath = path.join(__dirname, "myfile.zip");
const pathProcessed = path.join(__dirname, "grayscaled");
const grayScale = (fileStream, pathOut) => {
return new Promise((resolve, reject) => {
fileStream
.pipe(new PNG({ filterType: 4 }))
.on("parsed", function () {
for (let i = 0; i < this.data.length; i += 4) {
const avg = (this.data[i] + this.data[i + 1] + this.data[i + 2]) / 3;
this.data[i] = this.data[i + 1] = this.data[i + 2] = avg;
}
this.pack().pipe(fs.createWriteStream(pathOut)).on('finish', () => {
resolve();
console.log('Processed:', pathOut);
});
})
.on("error", (err) => reject(err));
});
};
const timeStart = Date.now();
const filterFn = (file) => path.dirname(file.path) === '.' && path.extname(file.path) === '.png';
const mapFn = (file) => grayScale(file.stream(), path.join(pathProcessed, file.path));
unzipper.Open.file(zipFilePath)
.then((d) => Promise.all(d.files.filter(filterFn).map(mapFn)))
.then(() => {
console.log('All done!');
const timeEnd = Date.now();
console.log(`Time elapsed: ${timeEnd - timeStart} ms`)
})
.catch((err) => console.log(err));