-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscanSeq.js
More file actions
124 lines (109 loc) · 3.59 KB
/
scanSeq.js
File metadata and controls
124 lines (109 loc) · 3.59 KB
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
const fs = require('fs');
const config = require('./config.json');
const Jimp = require('jimp');
const idList = [];
const filePrefix = config.scan.prefix;
const fileSuffix = `${config.scan.suffix}.${config.scan.filetype}`;
async function resizeFrame(id) {
await Jimp.read(`./in/${filePrefix}${id}${fileSuffix}`)
.then(frame => {
return frame.resize(config.general.width, config.general.height)
.greyscale()
.write(`./temp/frames/${filePrefix}${id}${fileSuffix}`)
})
.catch(err => {
console.error(err);
});
}
const seqBuffer = [];
for (let i = 0; i < config.general.height; i++) {
seqBuffer.push([]);
}
async function scanRow(rowID, frameID, frameIndex, frame) {
let rowBuffer = seqBuffer[rowID];
if (frameIndex === 0) {
for (let i = 0; i < config.general.width; i++) {
rowBuffer.push({
last: -1
})
}
}
frame.scan(0, rowID, frame.bitmap.width, 1, function(x, y, idx) {
let value = this.bitmap.data[idx]; // Red
value += this.bitmap.data[idx + 1]; // Green
value += this.bitmap.data[idx + 2]; // Blue
value /= 3;
let pixelArr;
if (frameIndex === 0) {
rowBuffer[x] = {
0: value,
last: 0
};
}
else if (frameIndex === idList.length - 1) {
pixelArr = rowBuffer[x];
if (pixelArr[pixelArr.last] !== value && pixelArr.last !== frameID - 1) {
pixelArr[frameID - 1] = pixelArr[pixelArr.last];
}
pixelArr[frameID] = value;
pixelArr.last = frameID;
}
else {
pixelArr = rowBuffer[x];
if (pixelArr[pixelArr.last] !== value) {
if (pixelArr.last !== frameID - 1) {
pixelArr[frameID - 1] = pixelArr[pixelArr.last];
}
pixelArr[frameID] = value;
pixelArr.last = frameID;
}
}
});
}
async function scanFrame(frameIndex) {
const frameID = idList[frameIndex];
const frameNum = Number(frameID);
console.log('Processing Image #' + frameID);
await Jimp.read(`./temp/frames/${filePrefix}${frameID}${fileSuffix}`)
.then(frame => {
const rowScans = [];
for (let i = 0; i < config.general.height; i++) {
rowScans.push(scanRow(i, frameNum, frameIndex, frame));
}
return Promise.all(rowScans);
})
.catch(err => {
console.error(err);
});
if (frameIndex + 1 < idList.length) {
await scanFrame(frameIndex + 1);
}
}
console.log('Preparing Environment');
if (fs.existsSync('./temp')) {
fs.rmdirSync('./temp', { recursive: true });
}
fs.mkdirSync('./temp');
fs.mkdirSync('./temp/frames');
console.log('Scanning Input Directory');
const inDir = fs.readdirSync('./in');
for (const fileName of inDir) {
if (fileName.startsWith(filePrefix) && fileName.endsWith(fileSuffix)) {
const id = fileName.substring(config.scan.prefix.length, fileName.length - fileSuffix.length);
if (!isNaN(id)) {
idList.push(id);
}
}
}
if (idList.length > 1) {
console.log('Resizing all Frames');
const framePromise = [];
for (const id of idList) {
framePromise.push(resizeFrame(id));
}
Promise.all(framePromise).then(async () => {
await scanFrame(0);
fs.rmdirSync('./temp/frames', {recursive: true});
fs.writeFileSync(`./temp/sequence.json`, JSON.stringify(seqBuffer));
});
}