-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataToImage.js
372 lines (347 loc) · 11.7 KB
/
dataToImage.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
"use strict";
const fs = require("fs");
const path = require("path");
const fsExtra = require("fs-extra");
const prompt = require("prompt-sync")();
const { Worker } = require("worker_threads");
const ProgressManager = require("./progressManager");
// Globální proměnné
let RGBDataArray = [];
let imageCounter = 0;
let imageCreationPromises = [];
let MAX_WORKERS = 6;
// Persistentní pool pro data processing
class DataWorkerPool {
constructor(workerPath, size, progressManager) {
this.workerPath = workerPath;
this.size = size;
this.progressManager = progressManager;
this.pool = [];
this.idle = [];
this.taskQueue = [];
for (let i = 0; i < size; i++) {
const worker = new Worker(workerPath);
worker.currentTask = null;
worker.on("message", (msg) => {
if (msg.progressDelta !== undefined) {
this.progressManager.update(msg.progressDelta);
}
if (worker.currentTask) {
if (msg.error) {
worker.currentTask.reject(new Error(msg.error));
} else {
// Vracíme výsledek spolu s původním indexem
worker.currentTask.resolve({ result: msg.result, index: worker.currentTask.data.index });
}
worker.currentTask = null;
}
if (this.taskQueue.length > 0) {
const next = this.taskQueue.shift();
worker.currentTask = next;
if (next.transferList) {
worker.postMessage(next.data, next.transferList);
} else {
worker.postMessage(next.data);
}
} else {
this.idle.push(worker);
}
});
worker.on("error", (err) => {
if (worker.currentTask) {
worker.currentTask.reject(err);
worker.currentTask = null;
}
});
this.pool.push(worker);
this.idle.push(worker);
}
}
runTask(data, transferList = null) {
return new Promise((resolve, reject) => {
const task = { data, resolve, reject, transferList };
if (this.idle.length > 0) {
const worker = this.idle.shift();
worker.currentTask = task;
if (transferList) {
worker.postMessage(data, transferList);
} else {
worker.postMessage(data);
}
} else {
this.taskQueue.push(task);
}
});
}
async shutdown() {
await Promise.all(this.pool.map(worker => worker.terminate()));
}
}
// Pomocné funkce pro převod názvu na nibble řetězce a získání barev
function StringToNibbles(str) {
return str.split("").flatMap((char) => {
const binaryValue = char.charCodeAt(0).toString(2).padStart(8, "0");
return [binaryValue.substring(0, 4), binaryValue.substring(4, 8)];
});
}
const colorMap = {
"0000": { r: 255, g: 255, b: 255 },
"0001": { r: 0, g: 0, b: 0 },
"0010": { r: 255, g: 0, b: 0 },
"0011": { r: 0, g: 255, b: 0 },
"0100": { r: 0, g: 0, b: 255 },
"0101": { r: 255, g: 255, b: 0 },
"0110": { r: 0, g: 255, b: 255 },
"0111": { r: 255, g: 0, b: 255 },
"1000": { r: 128, g: 0, b: 0 },
"1001": { r: 0, g: 128, b: 0 },
"1010": { r: 0, g: 0, b: 128 },
"1011": { r: 255, g: 165, b: 0 },
"1100": { r: 75, g: 0, b: 130 },
"1101": { r: 173, g: 255, b: 47 },
"1110": { r: 255, g: 20, b: 147 },
"1111": { r: 192, g: 192, b: 192 }
};
function GetColorFromNibble(nibble) {
if (!colorMap[nibble]) {
console.warn(`Invalid nibble: ${nibble}`);
return { r: 0, g: 0, b: 0 };
}
return colorMap[nibble];
}
// Vložíme do dat informaci o názvu / cestě souboru
function PushFilename(name) {
RGBDataArray.push({ r: 128, g: 128, b: 128 });
const nibbleArray = StringToNibbles(name);
nibbleArray.forEach((nib) => {
RGBDataArray.push(GetColorFromNibble(nib));
});
RGBDataArray.push({ r: 128, g: 128, b: 128 });
}
// Worker pool pro tvorbu obrázků (max. MAX_WORKERS současně)
const imageWorkerPool = {
limit: MAX_WORKERS,
running: 0,
queue: [],
schedule(task) {
return new Promise((resolve, reject) => {
const executeTask = () => {
this.running++;
task()
.then(resolve)
.catch(reject)
.finally(() => {
this.running--;
if (this.queue.length > 0) {
const next = this.queue.shift();
next();
}
});
};
if (this.running < this.limit) {
executeTask();
} else {
this.queue.push(executeTask);
}
});
}
};
function createImageWithWorker(rgbArray, width, height, outputFilePath) {
return new Promise((resolve, reject) => {
const worker = new Worker(path.join(__dirname, "../workers/createImageWorker.js"), {
workerData: { rgbArray, width, height, outputFilePath }
});
worker.on("message", (msg) => {
if (msg === "done") {
resolve();
}
});
worker.on("error", reject);
worker.on("exit", (code) => {
if (code !== 0)
reject(new Error(`Create image worker exited with code ${code}`));
});
});
}
function GenerateImagesFromRGBData(width, height, outputDir) {
const expectedSize = width * height;
let imageArray;
if (RGBDataArray.length >= expectedSize) {
imageArray = RGBDataArray.slice(0, expectedSize);
RGBDataArray = RGBDataArray.slice(expectedSize);
} else {
imageArray = RGBDataArray.concat(
Array(expectedSize - RGBDataArray.length).fill({ r: 0, g: 0, b: 0 })
);
RGBDataArray = [];
}
const outputFilePath = path.join(outputDir, `${++imageCounter}.png`);
const promise = imageWorkerPool.schedule(() =>
createImageWithWorker(imageArray, width, height, outputFilePath)
);
imageCreationPromises.push(promise);
}
function FlushCompleteImages(width, height, outputDir) {
const expectedSize = width * height;
while (RGBDataArray.length >= expectedSize) {
const imageArray = RGBDataArray.slice(0, expectedSize);
RGBDataArray = RGBDataArray.slice(expectedSize);
const outputFilePath = path.join(outputDir, `${++imageCounter}.png`);
const promise = imageWorkerPool.schedule(() =>
createImageWithWorker(imageArray, width, height, outputFilePath)
);
imageCreationPromises.push(promise);
}
}
function FlushFinalImage(width, height, outputDir) {
const expectedSize = width * height;
if (RGBDataArray.length > 0) {
// Přesvědčíme se, že poslední pixel je marker D – pokud ne, vložíme ho.
if (
RGBDataArray.length === 0 ||
RGBDataArray[RGBDataArray.length - 1].r !== 128 ||
RGBDataArray[RGBDataArray.length - 1].g !== 0 ||
RGBDataArray[RGBDataArray.length - 1].b !== 128
) {
RGBDataArray.push({ r: 128, g: 0, b: 128 });
}
// Doplň chybějící pixely černou barvou
const imageArray = RGBDataArray.concat(
Array(expectedSize - RGBDataArray.length).fill({ r: 0, g: 0, b: 0 })
);
RGBDataArray = [];
const outputFilePath = path.join(outputDir, `${++imageCounter}.png`);
const promise = imageWorkerPool.schedule(() =>
createImageWithWorker(imageArray, width, height, outputFilePath)
);
imageCreationPromises.push(promise);
}
}
// Přechod na streamové zpracování – místo načítání celého souboru do paměti
function ConvertFileToRGB(filePath, imageWidth, imageHeight, output) {
return new Promise((resolve, reject) => {
if (!fs.existsSync(filePath)) {
reject(new Error("File does not exist."));
return;
}
const totalBytes = fs.statSync(filePath).size;
// --- Úprava progress baru: předáváme aktuální název souboru do ProgressManageru.
const progressManager = new ProgressManager(totalBytes, path.basename(filePath));
const pool = new DataWorkerPool(path.join(__dirname, "../workers/dataToImageWorker.js"), MAX_WORKERS, progressManager);
const CHUNK_SIZE = 1024 * 1024; // 1 MB
let resultsMap = [];
let nextProcessedIndex = 0;
let chunkIndex = 0;
const chunkPromises = [];
const stream = fs.createReadStream(filePath, { highWaterMark: CHUNK_SIZE });
stream.on("data", (chunk) => {
const p = pool
.runTask({ chunk, index: chunkIndex }, [chunk.buffer])
.then((content) => {
resultsMap[content.index] = content.result;
while (resultsMap[nextProcessedIndex] !== undefined) {
const dataToAdd = resultsMap[nextProcessedIndex];
for (let i = 0; i < dataToAdd.length; i++) {
RGBDataArray.push(dataToAdd[i]);
}
delete resultsMap[nextProcessedIndex];
nextProcessedIndex++;
}
function processImages() {
if (RGBDataArray.length >= imageWidth * imageHeight) {
GenerateImagesFromRGBData(imageWidth, imageHeight, output);
setImmediate(processImages);
}
}
processImages();
})
.catch((err) => {
console.error("Chyba workeru:", err);
});
chunkPromises.push(p);
chunkIndex++;
});
stream.on("end", () => {
Promise.all(chunkPromises)
.then(() => {
FlushCompleteImages(imageWidth, imageHeight, output);
// Ujistíme se, že progress bar dosáhne 100%
const remaining = totalBytes - progressManager.completedWorkUnits;
if (remaining > 0) {
progressManager.update(remaining);
}
pool.shutdown().then(resolve).catch(reject);
})
.catch(reject);
});
stream.on("error", (err) => reject(err));
});
}
function getAllFiles(dirPath, filesArray = []) {
const files = fs.readdirSync(dirPath);
files.forEach((file) => {
const fullPath = path.join(dirPath, file);
if (fs.statSync(fullPath).isDirectory()) {
getAllFiles(fullPath, filesArray);
} else {
filesArray.push(fullPath);
}
});
return filesArray;
}
function playSound(times, delay = 500) {
const { exec } = require("child_process");
let count = 0;
const interval = setInterval(() => {
exec('powershell -c [System.Media.SystemSounds]::Beep.Play()', (err) => {
if (err) console.error("Error playing sound:", err);
});
count++;
if (count >= times) clearInterval(interval);
}, delay);
}
const imageWidth = 1920;
const imageHeight = 1080;
const input = process.argv[2] || prompt("Define input file/directory: ");
const output = path.join(__dirname, "./../images");
fsExtra.emptyDirSync(output);
console.time("Processing Time");
async function processFiles(input) {
if (fs.statSync(input).isDirectory()) {
const allFiles = getAllFiles(input);
for (let i = 0; i < allFiles.length; i++) {
const file = allFiles[i];
try {
// Zobrazíme název souboru, který se zpracovává (PushFilename vloží informativní pixely)
PushFilename(file);
await ConvertFileToRGB(file, imageWidth, imageHeight, output);
// Vložíme terminátor D vždy po zpracování souboru, včetně posledního
RGBDataArray.push({ r: 128, g: 0, b: 128 });
FlushCompleteImages(imageWidth, imageHeight, output);
} catch (error) {
console.error("Error processing file:", error.message);
}
}
} else {
try {
PushFilename(input);
await ConvertFileToRGB(input, imageWidth, imageHeight, output);
// Vložíme terminátor D i u jediného souboru
RGBDataArray.push({ r: 128, g: 0, b: 128 });
} catch (error) {
console.error("Error processing file:", error.message);
}
}
// Po zpracování všech souborů flushneme poslední (neúplný blok je doleplněn černými pixely)
FlushFinalImage(imageWidth, imageHeight, output);
await Promise.all(imageCreationPromises);
}
processFiles(input)
.then(() => {
playSound(1);
console.log("\nAll files processed.");
console.timeEnd("Processing Time");
})
.catch((err) => {
console.error("Error during processing:", err);
});