UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

74 lines (73 loc) 2.74 kB
import { IDecodedImage } from "../core/ImageCodec"; /** * Node.js-specific image codec implementation. * Uses native Node.js modules (pngjs, zlib) for optimal performance. */ export default class ImageCodecNode { private static _crc32Table; /** * Decode PNG image data to RGBA pixels using pngjs. * This is synchronous and only works in Node.js. * * @param data Raw PNG file bytes * @returns Decoded image, or undefined if decoding fails */ static decodePng(data: Uint8Array): IDecodedImage | undefined; /** * Decode PNG image data asynchronously. * Wraps the synchronous decoder in a Promise for API consistency. * * @param data Raw PNG file bytes * @returns Promise resolving to decoded image, or undefined if decoding fails */ static decodePngAsync(data: Uint8Array): Promise<IDecodedImage | undefined>; /** * Encode RGBA pixel data to PNG format using pngjs. * * @param pixels RGBA pixel data (4 bytes per pixel) * @param width Image width * @param height Image height * @returns PNG file bytes, or undefined if encoding fails */ static encodeToPng(pixels: Uint8Array, width: number, height: number): Uint8Array | undefined; /** * Encode RGBA pixel data to PNG format using manual chunk creation. * Used as fallback if pngjs encoding fails. * * @param pixels RGBA pixel data (4 bytes per pixel) * @param width Image width * @param height Image height * @returns PNG file bytes, or undefined if encoding fails */ static encodeToPngManual(pixels: Uint8Array, width: number, height: number): Uint8Array | undefined; /** * Encode RGBA pixel data to PNG format asynchronously. * Wraps the synchronous encoder in a Promise for API consistency. * * @param pixels RGBA pixel data (4 bytes per pixel) * @param width Image width * @param height Image height * @returns Promise resolving to PNG file bytes */ static encodeToPngAsync(pixels: Uint8Array, width: number, height: number): Promise<Uint8Array | undefined>; /** * Create a PNG chunk with type, data, and CRC. */ private static createPngChunk; /** * Calculate CRC32 checksum for PNG chunk validation. */ private static crc32; /** * Get the CRC32 lookup table (lazy initialized). */ private static getCrc32Table; /** * Convert raw image bytes to a data URL using Node.js Buffer. * * @param data Image file bytes (PNG, JPEG, etc.) * @param mimeType MIME type (e.g., "image/png") * @returns Data URL string */ static toDataUrl(data: Uint8Array, mimeType: string): string; }