@ankhzet/goo
Version:
Elegoo .goo file format reader/writer
208 lines (207 loc) • 12.6 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncDelegator = (this && this.__asyncDelegator) || function (o) {
var i, p;
return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: false } : f ? f(v) : v; } : f; }
};
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
import { formatDate, rgb565Buffer } from './utils.js';
import { rleEncode } from './rle/index.js';
import { GOO_DELIMITER, GOO_MAGIC, GOO_TAIL, GOO_VERSION } from './magics.js';
import { loadPreview, loadSlice } from './loaders.js';
import { CRC8 } from './CRC8.js';
const GENERATOR = {
description: 'AnkhZetGoo',
version: '0.0.2',
};
export class GooWriter {
constructor(writer) {
this.writer = writer;
}
write(goo, consumer) {
return __awaiter(this, void 0, void 0, function* () {
var _a, e_1, _b, _c;
let offset = 0;
const consume = (buffer) => __awaiter(this, void 0, void 0, function* () {
offset += buffer.length;
return consumer(buffer);
});
try {
for (var _d = true, _e = __asyncValues(this.writeData(goo)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
_c = _f.value;
_d = false;
const buffer = _c;
yield consume((buffer instanceof Buffer)
? buffer
: buffer(offset));
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
}
finally { if (e_1) throw e_1.error; }
}
return offset;
});
}
writeData(goo) {
return __asyncGenerator(this, arguments, function* writeData_1() {
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.string(GOO_VERSION, 4))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.binary(GOO_MAGIC))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writeHeader(goo.header))));
for (const layer of goo.layers) {
yield __await(yield* __asyncDelegator(__asyncValues(this.writeLayer(layer, goo.header.printer))));
}
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.u24(GOO_TAIL))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.binary(GOO_MAGIC))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.flush())));
});
}
writeHeader(header) {
return __asyncGenerator(this, arguments, function* writeHeader_1() {
const generator = header.generator || GENERATOR;
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.string(generator.description, 32))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.string(generator.version, 24))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.string(formatDate(header.date), 24))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.string(header.printer.name, 32))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.string(header.printer.type, 32))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.string(header.printer.resinProfile, 32))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.u16(header.printer.antialiasing))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.u16(header.printer.gray))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.u16(header.printer.blur))));
for (const preview of header.previews) {
yield __await(yield* __asyncDelegator(__asyncValues(this.writePreview(preview))));
}
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.flush())));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.u32(header.layers))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.u16(header.printer.resolution.x))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.u16(header.printer.resolution.y))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.bool(header.printer.mirror.x))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.bool(header.printer.mirror.y))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.f32(header.printer.platform.x))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.f32(header.printer.platform.y))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.f32(header.printer.platform.z))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.flush())));
yield __await(yield* __asyncDelegator(__asyncValues(this.writeLayerConfig(header.layerConfig))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.flush())));
yield __await(yield* __asyncDelegator(__asyncValues(this.writeSummary(header.summary))));
yield __await(yield* __asyncDelegator(__asyncValues(this.mark(7))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.bool(header.printer.grayscale))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.u16(header.layerConfig.transitionLayers))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.flush())));
});
}
*mark(offset) {
yield* this.writer.flush();
yield (pos) => Buffer.from(Uint32Array.of(pos + offset).buffer).reverse();
}
*writeLayerConfig(config) {
yield* this.writer.f32(config.thickness);
yield* this.writer.f32(config.commonExposure);
yield* this.writer.bool(config.exposureDelay);
yield* this.writer.f32(config.turnOffTime);
yield* this.writeGlobalConfig(config.timings, (i) => this.writeMotionTimes(i));
yield* this.writer.f32(config.bottomExposure);
yield* this.writer.u32(config.bottomLayers);
yield* this.writeMotions(config.motions, (motion) => (this.writeLiftRetract(motion, (lr) => (this.writeGlobalConfig(lr, (c) => this.writeMotionConfig(c))))));
yield* this.writer.u16(config.pwm.bottom);
yield* this.writer.u16(config.pwm.common);
yield* this.writer.bool(config.advance);
}
*writeSummary(summary) {
yield* this.writer.u32(summary.time);
yield* this.writer.f32(summary.volume);
yield* this.writer.f32(summary.weight);
yield* this.writer.f32(summary.price);
yield* this.writer.string(summary.currency, 8);
}
*writeGlobalConfig(config, map) {
yield* map(config.bottom);
yield* map(config.common);
}
*writeMotions(config, map) {
yield* map(config.first);
yield* map(config.second);
}
*writeLiftRetract(config, map) {
yield* map(config.lift);
yield* map(config.retract);
}
*writeMotionTimes(times) {
yield* this.writer.f32(times.before.lift);
yield* this.writer.f32(times.after.lift);
yield* this.writer.f32(times.after.retract);
}
*writeMotionConfig(config) {
yield* this.writer.f32(config.distance);
yield* this.writer.f32(config.speed);
}
writePreview(preview) {
return __asyncGenerator(this, arguments, function* writePreview_1() {
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.flush())));
const { buffer, channels } = (typeof preview.input === 'string'
? yield __await(loadPreview(preview.input, preview.dimensions))
: preview.input);
yield yield __await(rgb565Buffer(buffer, channels));
yield __await(yield* __asyncDelegator(__asyncValues(this.writeDelimiter())));
});
}
*writeDelimiter() {
yield* this.writer.u16(GOO_DELIMITER);
}
writeLayer(layer, printer) {
return __asyncGenerator(this, arguments, function* writeLayer_1() {
const slice = (typeof layer.slice === 'string'
? yield __await(loadSlice(layer.slice, layer.transform, printer))
: layer.slice);
const buffer = rleEncode(slice.buffer, slice.channels);
yield __await(yield* __asyncDelegator(__asyncValues(this.writeLayerDefinition(layer.definition))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.u32(buffer.length + 2))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.u8(0x55))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.flush())));
yield yield __await(buffer);
yield __await(yield* __asyncDelegator(__asyncValues(this.writer.u8((new CRC8()).checksum(buffer)))));
yield __await(yield* __asyncDelegator(__asyncValues(this.writeDelimiter())));
});
}
*writeLayerDefinition(definition) {
yield* this.writer.u16(definition.pause.mode);
yield* this.writer.f32(definition.pause.z);
yield* this.writer.f32(definition.z);
yield* this.writer.f32(definition.exposure);
yield* this.writer.f32(definition.offTime);
yield* this.writeMotionTimes(definition.times);
yield* this.writeLiftRetract(definition.motions, (m) => (this.writeMotions(m, (lr) => (this.writeMotionConfig(lr)))));
yield* this.writer.u16(definition.pwm);
yield* this.writeDelimiter();
}
}