-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathrawFunctions.js
337 lines (278 loc) · 7.41 KB
/
rawFunctions.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
// Functions in this file are taken from https://www.npmjs.com/package/gbp-decode
// For simplicity they are mirrored in this file
const COMMAND_INIT = 0x1;
const COMMAND_PRINT = 0x2;
const COMMAND_DATA = 0x4;
const COMMAND_STATUS = 0xf;
const STATE_AWAIT_MAGIC_BYTES = 0;
const STATE_AWAIT_COMMAND = 1;
const STATE_AWAIT_COMPRESSION_INFO = 2;
const STATE_AWAIT_PACKET_DATA_LENGTH = 3;
const STATE_AWAIT_DATA = 4;
const STATE_AWAIT_CHECKSUM = 5;
const STATE_AWAIT_KEEPALIVE = 6;
const STATE_AWAIT_STATUS_QUERY = 7;
const MODE_DETECT_LENGTH = 0;
const MODE_COMPRESSED = 1;
const MODE_UNCOMPRESSED = 2;
const EMPTY_PACKET = {
command: null,
buffer: [],
data: [],
hasCompression: null,
dataLength: 0,
checksum: 0,
};
const toByteArray = (fileContents) => {
return fileContents
.split('\n')
.filter((line) => !line.startsWith('//'))
.map(line => line.trim())
.map(line => line
.split(' ')
.filter(Boolean)
.map((cc) => parseInt(cc, 16))
)
.flat();
};
const parsePackets = (bytes) => {
let state = STATE_AWAIT_MAGIC_BYTES;
let packet = {...EMPTY_PACKET};
const packets = [];
bytes.forEach((byte) => {
switch (state) {
case STATE_AWAIT_MAGIC_BYTES:
if (packet.buffer.length === 0 && byte === 0x88) {
packet.buffer.push(byte);
return;
} else if (packet.buffer.length === 1 && byte === 0x33) {
packet.buffer = [];
state = STATE_AWAIT_COMMAND;
return;
} else {
packet = {...EMPTY_PACKET};
return;
}
case STATE_AWAIT_COMMAND:
packet.command = byte;
state = STATE_AWAIT_COMPRESSION_INFO;
return;
case STATE_AWAIT_COMPRESSION_INFO:
packet.hasCompression = byte;
state = STATE_AWAIT_PACKET_DATA_LENGTH;
return;
case STATE_AWAIT_PACKET_DATA_LENGTH:
if (packet.buffer.length === 0) {
packet.buffer.push(byte);
return;
}
packet.dataLength = packet.buffer[0] + (byte << 8);
packet.buffer = [];
if (packet.dataLength === 0) {
state = STATE_AWAIT_CHECKSUM;
} else {
state = STATE_AWAIT_DATA;
}
return;
case STATE_AWAIT_DATA:
if (packet.buffer.length < packet.dataLength) {
packet.buffer.push(byte);
return;
}
packet.data = packet.buffer;
packet.buffer = [];
state = STATE_AWAIT_CHECKSUM;
return;
case STATE_AWAIT_CHECKSUM:
if (packet.buffer.length === 0) {
packet.buffer.push(byte);
return;
}
packet.checksum = packet.buffer[0] + (byte << 8);
packet.buffer = [];
state = STATE_AWAIT_KEEPALIVE;
return;
case STATE_AWAIT_KEEPALIVE:
state = STATE_AWAIT_STATUS_QUERY;
return;
case STATE_AWAIT_STATUS_QUERY:
state = STATE_AWAIT_MAGIC_BYTES;
delete packet.buffer;
packets.push(packet);
packet = {...EMPTY_PACKET};
return;
}
});
return packets;
};
const getImageDataStream = (packets) => {
return packets.filter(({ command }) => (
command === COMMAND_DATA ||
command === COMMAND_PRINT
));
};
const unpack = (data) => {
const dataOut = [];
let mode = MODE_DETECT_LENGTH;
let length = 0;
data.forEach((byte) => {
switch (mode) {
case MODE_DETECT_LENGTH:
// noinspection JSBitwiseOperatorUsage
if (byte & 0x80) {
mode = MODE_COMPRESSED;
length = (byte & 0x7f) + 2;
} else {
mode = MODE_UNCOMPRESSED;
length = byte + 1;
}
return;
case MODE_UNCOMPRESSED:
length -= 1;
if (length === 0) {
mode = MODE_DETECT_LENGTH;
}
dataOut.push(byte);
return;
case MODE_COMPRESSED:
dataOut.push(...[...Array(length)].map(() => byte))
mode = MODE_DETECT_LENGTH;
length = 0;
return;
}
});
return dataOut;
};
const decompressDataStream = (packets) => {
return packets
.map((packet) => {
if (packet.command === COMMAND_DATA) {
return {
... packet,
hasCompression: 0,
data: packet.hasCompression ? unpack(packet.data) : packet.data
};
}
return packet;
});
};
const parsePaletteByte = (paletteRaw) => {
return [
(paletteRaw >> 6) & 0x3,
(paletteRaw >> 4) & 0x3,
(paletteRaw >> 2) & 0x3,
(paletteRaw >> 0) & 0x3,
];
};
const decodePrintCommands = (packets) => {
return packets
.map((packet) => {
if (packet.command === COMMAND_PRINT) {
return {
...packet,
data: {
margins: packet.data[1],
marginUpper: packet.data[1] >> 4,
marginLower: packet.data[1] & 0xf,
palette: packet.data[2],
paletteData: parsePaletteByte(packet.data[2]),
}
}
}
return packet;
})
};
const harmonizePalette = (charA, charB, paletteDefinition = [3, 2, 1, 0]) => {
const bits = [...Array(8)].map((_, index) => ({
a: (charB >> 7 - index) % 2,
b: (charA >> 7 - index) % 2,
}))
const res = bits
.map(({ a, b, }) => ((a << 1) + b))
// .map(plg)
.map((val) => (paletteDefinition[3 - val]))
// .map(plg)
.map((mapped) => ({
a: (mapped >> 1) % 2,
b: mapped % 2,
}))
.reduce((acc, current, index) => (
{
a: acc.a + (current.a << 7-index),
b: acc.b + (current.b << 7-index),
}
), {
a: 0,
b: 0,
})
return [
res.b & 0xff,
res.a & 0xff,
];
};
const harmonizePalettes = (packets) => {
const unharmonizedPackets = [];
return packets
.map((packet) => {
switch (packet.command) {
case COMMAND_DATA:
unharmonizedPackets.push(packet);
break;
case COMMAND_PRINT:
while (unharmonizedPackets.length) {
let unharmonizedPacket = unharmonizedPackets.shift();
const data = [];
for (let i = 0; i < unharmonizedPacket.data.length; i += 2) {
data.push(
...harmonizePalette(
unharmonizedPacket.data[i],
unharmonizedPacket.data[i + 1],
packet.data.paletteData,
),
);
}
Object.assign(unharmonizedPacket, { data });
}
break;
}
return packet;
})
};
const transformToClassic = (packets) => {
let image = {
transformed: [],
palette: null,
};
let currentLine = [];
const images = [];
packets.forEach((packet) => {
switch (packet.command) {
case COMMAND_DATA:
for (let i = 0; i < packet.data.length; i += 1) {
currentLine.push(packet.data[i].toString(16).padStart(2, '0'));
if (i % 16 === 15) {
image.transformed.push(currentLine.join(' '));
currentLine = [];
}
}
break;
case COMMAND_PRINT:
image.palette = packet.data.paletteData || image.palette;
if (packet.data.marginLower !== 0) {
console.log(packet.data.marginLower);
images.push(image.transformed);
image = {
transformed: [],
};
currentLine = [];
}
break;
default:
break;
}
});
if (image.transformed.length) {
images.push(image.transformed);
}
return images;
};