UNPKG

blitflash

Version:

A JavaScript implementation of the 32blit flash tools

96 lines (95 loc) 3.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BlitMetaStandalone = exports.BlitImage = void 0; const RandomAccessReader_1 = require("./RandomAccessReader"); class BlitImage { constructor(type, dataLength, width, height, format, pallete, pixels) { this.type = type; this.dataLength = dataLength; this.width = width; this.height = height; this.format = format; this.pallete = pallete; this.pixels = pixels; } // bitLength(): number { // if (this.type === 'RW') { // return 8; // } else { // //?Math.max(1, this.pallete) // } // } numPixels() { return this.width * this.height; } static parse(reader) { const header = reader.readString(6); if (header !== 'SPRITE') { throw new Error(`Invalid header for BlitImage: ${header}`); } const type = reader.readString(2); const dataLength = reader.readUint32(true); const width = reader.readUint16(true); const height = reader.readUint16(true); const format = reader.readUint8(); let palleteLength = reader.readUint8(); // See https://github.com/32blit/32blit-tools/blob/a520a742450c8da97f88f6c0ce74ac0038093e02/src/ttblit/core/struct.py#L24-L33 if (palleteLength === 0) { palleteLength = 256; } const pallete = reader.read(palleteLength * 4); const pixels = reader.read(dataLength - 18 - palleteLength * 4); return new BlitImage(type, dataLength, width, height, format, pallete, pixels); } } exports.BlitImage = BlitImage; /** * As defined at * https://github.com/32blit/32blit-tools/blob/a520a742450c8da97f88f6c0ce74ac0038093e02/src/ttblit/core/struct.py#L79 */ class BlitMetaStandalone { constructor(checksum, date, title, description, version, author, blittype, category, url, filetypes, icon, splash) { this.checksum = checksum; this.date = date; this.title = title; this.description = description; this.version = version; this.author = author; this.blittype = blittype; this.category = category; this.url = url; this.filetypes = filetypes; this.icon = icon; this.splash = splash; } static parse(buffer) { const reader = new RandomAccessReader_1.RandomAccessReader(buffer); const checksum = reader.readUint32(true); const date = reader.readString(16); const title = reader.readString(25); const description = reader.readString(129); const version = reader.readString(17); const author = reader.readString(17); let category; let url; let filetypes; const pos = reader.getPos(); const blitType = reader.readString(8); if (blitType === 'BLITTYPE') { category = reader.readString(17); url = reader.readString(129); const filetypesLength = reader.readUint8(); filetypes = []; for (let i = 0; i < filetypesLength; i++) { filetypes.push(reader.readString(5)); } } else { reader.setPos(pos); } const icon = BlitImage.parse(reader); const splash = BlitImage.parse(reader); return new BlitMetaStandalone(checksum, date, title, description, version, author, blitType, category, url, filetypes, icon, splash); } } exports.BlitMetaStandalone = BlitMetaStandalone;