@ankhzet/goo
Version:
Elegoo .goo file format reader/writer
84 lines (83 loc) • 3.12 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());
});
};
import sharp from 'sharp';
import { promisify, transformInCoordinateSystem } from './utils.js';
export const loadPreview = (pathname_1, _a) => __awaiter(void 0, [pathname_1, _a], void 0, function* (pathname, { x, y }) {
return (sharp(pathname, { pages: 1 })
.resize({
width: x,
height: y,
fit: 'contain',
withoutEnlargement: false,
withoutReduction: false,
kernel: 'lanczos3',
})
.raw({
depth: 'uchar',
})
.toBuffer({ resolveWithObject: true })
.then(({ data: buffer, info }) => ({
buffer,
channels: info.channels,
})));
});
const sliceCanvas = ({ resolution: { x, y } }) => ({
create: {
width: x,
height: y,
channels: 4,
background: '#000000',
},
});
export const loadSlice = (pathname, transform, printer) => __awaiter(void 0, void 0, void 0, function* () {
const [buffer, info] = yield promisify((cb) => __awaiter(void 0, void 0, void 0, function* () {
const slice = sharp(pathname);
const { x: left, y: top, width, height } = transformInCoordinateSystem(printer, transform, yield slice.metadata());
sharp(sliceCanvas(printer))
.composite([{
input: yield (slice
.resize({
width,
height,
fit: 'contain',
withoutEnlargement: false,
withoutReduction: false,
kernel: 'lanczos3',
background: (transform === null || transform === void 0 ? void 0 : transform.invert) ? '#ffffff' : '#000000',
})
.negate((transform === null || transform === void 0 ? void 0 : transform.invert) || false)
.toBuffer()),
left,
top,
}])
.grayscale()
.raw({
depth: 'uchar',
})
.toBuffer(cb);
}));
return {
buffer,
channels: info.channels,
};
});
export const saveImage = ({ dimensions, input, pathname }) => {
if (typeof input === 'string') {
return sharp(input).toFile(pathname);
}
return (sharp(input.buffer, {
raw: {
width: dimensions.x,
height: dimensions.y,
channels: input.channels,
},
})
.toFile(pathname));
};