This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageloader.js
164 lines (124 loc) · 4.08 KB
/
imageloader.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
const fs = require("fs");
const Jimp = require("jimp");
const cliProgress = require('cli-progress');
class ImageLoader {
filename;
resolution;
sourceWidth = 0;
sourceHeight = 0;
imageObject = null;
targetWidth = 1;
targetHeight = 1;
sectionSize = 1;
resFactor = 1;
array = [];
output = 'output.png';
silent = false;
constructor(file, resolution, output) {
this.filename = file;
this.resolution = resolution;
this.output = output;
}
logMessage(...message) {
if (!this.silent) {
console.log(...message);
}
}
logError(...message) {
if (!this.silent) {
console.error(...message);
}
}
load() {
this.logMessage(`\n[Loading ${this.filename}]`);
Jimp.read(this.filename).then(image => {
this.imageObject = image;
this.sourceWidth = image.getWidth();
this.sourceHeight = image.getHeight();
this.resFactor = this.resolution / 100;
this.targetWidth = Math.floor(this.resFactor * this.sourceWidth);
this.targetHeight = Math.floor(this.resFactor * this.sourceHeight);
this.logMessage([
`Image loaded`,
`Resolution: ${this.resolution}`,
`Resolution factor: ${this.resFactor}`,
`Sections: ${this.targetWidth}x${this.targetHeight}`,
`Section size: ${this.sectionSize}`,
`Source size: ${this.sourceWidth}x${this.sourceHeight}`,
].join("\n"));
this.logMessage('Getting pixels');
this.getPixels(image);
this.logMessage('Generating output');
this.processArray();
}).catch(err => {
this.logMessage(err);
});
}
getPixels(image) {
if (this.resFactor < 1) {
image = image.resize(this.targetWidth, this.targetHeight);
}
let multibar = new cliProgress.MultiBar({
clearOnComplete: false,
hideCursor: true
}, cliProgress.Presets.legacy);
let yProgress = multibar.create(this.targetHeight, 0);
let xProgress = multibar.create(this.targetWidth, 0);
for (let y = 0; y < this.targetHeight; y++) {
let xAr = [];
yProgress.update(y);
for (let x = 0; x < this.targetWidth; x++) {
xProgress.update(x);
let color = 0;
color = image.getPixelColor(x * this.sectionSize, y * this.sectionSize);
xAr.push(color);
}
this.array.push(xAr);
}
multibar.stop();
}
processArray() {
const scaleFactor = 3;
let multibar = new cliProgress.MultiBar({
clearOnComplete: false,
hideCursor: true
}, cliProgress.Presets.legacy);
let yProgress = multibar.create(this.targetHeight, 0);
let xProgress = multibar.create(this.targetWidth, 0);
new Jimp(this.targetWidth * scaleFactor, this.targetHeight * scaleFactor, '#ffffff', (err, image) => {
for (let y = 0; y < this.array.length; y++) {
yProgress.update(y);
let xAr = this.array[y];
for (let x = 0; x < xAr.length; x++) {
xProgress.update(x);
let rgb = Jimp.intToRGBA(xAr[x]);
let r = rgb.r;
let g = rgb.g;
let b = rgb.b;
let bg = {
r: 0,
g: 0,
b: 0
};
// Set R color
image.setPixelColor(Jimp.rgbaToInt(r, bg.g, bg.b, 255), x * scaleFactor, y * scaleFactor);
image.setPixelColor(Jimp.rgbaToInt(r, bg.g, bg.b, 255), x * scaleFactor, (y * scaleFactor) + 1);
image.setPixelColor(Jimp.rgbaToInt(r, bg.g, bg.b, 255), x * scaleFactor, (y * scaleFactor) + 2);
// Set B color
image.setPixelColor(Jimp.rgbaToInt(bg.r, g, bg.b, 255), (x * scaleFactor) + 1, y * scaleFactor);
image.setPixelColor(Jimp.rgbaToInt(bg.r, g, bg.b, 255), (x * scaleFactor) + 1, (y * scaleFactor) + 1);
image.setPixelColor(Jimp.rgbaToInt(bg.r, g, bg.b, 255), (x * scaleFactor) + 1, (y * scaleFactor) + 2);
// Set G color
image.setPixelColor(Jimp.rgbaToInt(bg.r, bg.g, b, 255), (x * scaleFactor) + 2, y * scaleFactor);
image.setPixelColor(Jimp.rgbaToInt(bg.r, bg.g, b, 255), (x * scaleFactor) + 2, (y * scaleFactor) + 1);
image.setPixelColor(Jimp.rgbaToInt(bg.r, bg.g, b, 255), (x * scaleFactor) + 2, (y * scaleFactor) + 2);
}
}
multibar.stop();
this.logMessage('Saving image');
image.write(this.output);
this.logMessage('Image saved to ' + this.output);
});
}
}
module.exports = ImageLoader;