@fdm-monster/server
Version:
FDM Monster is a bulk OctoPrint, Klipper, PrusaLink and BambuLab manager to set up, configure and monitor 3D printers. Our aim is to provide neat overview over your farm.
63 lines (62 loc) • 1.6 kB
JavaScript
import { deflateSync } from "node:zlib";
//#region src/utils/bgcode/png-encoder.ts
const PNG_SIGNATURE = Buffer.from([
137,
80,
78,
71,
13,
10,
26,
10
]);
function crc32(buffer) {
let crc = 4294967295;
for (const element of buffer) {
crc ^= element;
for (let j = 0; j < 8; j++) crc = crc >>> 1 ^ 3988292384 & -(crc & 1);
}
return (crc ^ 4294967295) >>> 0;
}
function writeChunk(type, data) {
const typeBuffer = Buffer.from(type, "ascii");
const length = Buffer.alloc(4);
length.writeUInt32BE(data.length, 0);
const crcBuffer = Buffer.concat([typeBuffer, data]);
const crc = Buffer.alloc(4);
crc.writeUInt32BE(crc32(crcBuffer), 0);
return Buffer.concat([
length,
typeBuffer,
data,
crc
]);
}
function encodePNG(width, height, rgba) {
const ihdr = Buffer.alloc(13);
ihdr.writeUInt32BE(width, 0);
ihdr.writeUInt32BE(height, 4);
ihdr.writeUInt8(8, 8);
ihdr.writeUInt8(6, 9);
ihdr.writeUInt8(0, 10);
ihdr.writeUInt8(0, 11);
ihdr.writeUInt8(0, 12);
const scanlineLength = width * 4;
const filteredData = Buffer.alloc(height * (scanlineLength + 1));
for (let y = 0; y < height; y++) {
const scanlineOffset = y * (scanlineLength + 1);
filteredData[scanlineOffset] = 0;
rgba.copy(filteredData, scanlineOffset + 1, y * scanlineLength, (y + 1) * scanlineLength);
}
const compressed = deflateSync(filteredData);
const chunks = [
PNG_SIGNATURE,
writeChunk("IHDR", ihdr),
writeChunk("IDAT", compressed),
writeChunk("IEND", Buffer.alloc(0))
];
return Buffer.concat(chunks);
}
//#endregion
export { encodePNG };
//# sourceMappingURL=png-encoder.js.map