-
Notifications
You must be signed in to change notification settings - Fork 10
/
DrawManager.js
170 lines (136 loc) · 4.88 KB
/
DrawManager.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
const Jimp = require('jimp')
const fs = require('fs')
const { ColorPalette, FloydSteinbergDither } = require('multidither')
const Config = require('./config/Config')
const Positions = require('./config/Positions')
const Setting = require('./config/Setting')
const InstructionWriter = require('./instructions/InstructionWriter')
const Resizer = require('./Resizer')
const ProgressBar = require('progress')
module.exports = class DrawManager {
/**
* @param {Config} config
*/
constructor(config) {
this.config = config.data
this.resizer = new Resizer(config)
this.instructionWriter = new InstructionWriter(config)
this.state = "idle"
}
/**
* @param {Setting} settings
* @param {Positions} positions
*/
async startDraw(settings, positions) {
if (this.state != "idle") {
console.error("Already drawing")
return
}
this.state = "initializing"
this.settings = settings.data
/**
* @type {Jimp}
*/
// @ts-ignore
let img = this.settings.img
console.time("total")
// if base64 encode the image
this.state = "decoding"
if (this.settings.img.startsWith("data:image/")) {
let decode = Buffer.from(this.settings.img.split(',')[1], 'base64')
fs.writeFileSync(this.config.temp + 'decoded.png', decode)
// @ts-ignore
img = this.config.temp + 'decoded.png'
}
// @ts-ignore
if (typeof img == "string" && !img.startsWith("http")) {
if (!fs.existsSync(img)) {
console.error("Image not found: " + img)
this.state = "idle"
console.timeEnd("total")
return
}
}
this.state = "reading image"
if (typeof img == 'string') {
try {
img = await Jimp.read(img)
}
catch (e) {
console.error(e)
console.timeEnd("total")
this.state = "idle"
return
}
}
this.state = "resizing"
let position = positions.getPlatform(this.settings.name)
if (settings.data.positionOverride.enabled) {
position.topleft = {
x: settings.data.positionOverride.x1,
y: settings.data.positionOverride.y1
}
position.bottomright = {
x: settings.data.positionOverride.x2,
y: settings.data.positionOverride.y2
}
}
let size = {
w: Math.round((position.bottomright.x - position.topleft.x) / this.settings.distancing),
h: Math.round((position.bottomright.y - position.topleft.y) / this.settings.distancing)
}
console.log(`resizing to ${size.w}x${size.h}`)
// @ts-ignore
let resized = await this.resizer.resize(img, size, settings.data.resizeImgAlg)
await resized.writeAsync(this.config.temp + 'resized.png')
// let resized = img // to test stuff
// resized = await resized.rotate(42)
if (settings.data.dither) {
this.state = "dithering"
let colors = Object.keys(position.colors)
let dither = new FloydSteinbergDither(resized, new ColorPalette(colors))
img = dither.dither(this.config.temp + 'dithered.png')
}
this.state = "writing instructions"
let instructions = await this.instructionWriter.write(img, positions, settings)
this.state = "drawing"
// console.log(instructions)
console.time("draw")
/**@type {ProgressBar} */
let bar
if (this.config.progressBar.enabled) {
/** @type {number | undefined}*/
let width
if (this.config.progressBar.width === null) {
width = undefined
}
else {
width = this.config.progressBar.width
}
bar = new ProgressBar('drawing [:bar] :current/:total :percent :etas', {
total: instructions.length,
width: width,
head: this.config.progressBar.head,
incomplete: this.config.progressBar.incomplete,
renderThrottle: this.config.progressBar.renderThrottle
})
}
let pos = 0
for (let instruction of instructions) {
// console.log(instruction)
if (pos % 10 === 0) {
if (fs.existsSync(this.config.temp + this.config.abortingFile)) {
break
}
pos = 0
}
await instruction.execute()
if (this.config.progressBar.enabled && bar) {
bar.tick()
}
pos++
}
console.timeEnd("draw")
console.timeEnd("total")
}
}