@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
195 lines (194 loc) • 7.32 kB
TypeScript
/**
* Decoded image pixel data.
* All decoders return this common format.
*/
export interface IDecodedImage {
/** Image width in pixels */
width: number;
/** Image height in pixels */
height: number;
/** RGBA pixel data (4 bytes per pixel: R, G, B, A) */
pixels: Uint8Array;
}
/**
* Supported image formats for decoding
*/
export type ImageFormat = "png" | "tga" | "jpg" | "jpeg";
/**
* Unified image encoding/decoding utilities.
*
* This is the main entry point for all image operations.
* It automatically selects the best implementation for the current environment.
*/
export default class ImageCodec {
private static _crc32Table;
/**
* Decode TGA image data to RGBA pixels.
* Works in all environments (Node.js, browser, web worker).
*
* Uses @lunapaint/tga-codec which handles:
* - Uncompressed true-color (type 2)
* - Uncompressed grayscale (type 3)
* - RLE compressed (types 9, 10, 11)
* - Various bit depths (8, 16, 24, 32)
*
* @param data Raw TGA file bytes
* @returns Decoded image with RGBA pixels, or undefined if decoding fails
*/
static decodeTga(data: Uint8Array): Promise<IDecodedImage | undefined>;
/**
* Decode PNG image data to RGBA pixels.
* Automatically uses the best decoder for the current environment.
*
* - Node.js: Uses pngjs (synchronous, fast)
* - Browser: Uses createImageBitmap + Canvas (async)
* - Web Worker: Uses createImageBitmap + OffscreenCanvas (async)
*
* @param data Raw PNG file bytes
* @returns Decoded image with RGBA pixels, or undefined if decoding fails
*/
static decodePng(data: Uint8Array): Promise<IDecodedImage | undefined>;
/**
* Decode PNG using browser APIs (createImageBitmap + Canvas).
* Works in browser main thread and web workers.
*
* @param data Raw PNG file bytes
* @returns Decoded image, or undefined if not in browser or decoding fails
*/
static decodePngBrowser(data: Uint8Array): Promise<IDecodedImage | undefined>;
/**
* Decode JPEG image data to RGBA pixels.
* Only works in browser environments (uses createImageBitmap).
*
* @param data Raw JPEG file bytes
* @returns Decoded image with RGBA pixels, or undefined if decoding fails
*/
static decodeJpeg(data: Uint8Array): Promise<IDecodedImage | undefined>;
/**
* Decode image data to RGBA pixels based on specified format.
*
* @param data Raw image file bytes
* @param format File format ("png", "tga", "jpg", "jpeg")
* @returns Decoded image, or undefined if decoding fails
*/
static decode(data: Uint8Array, format: ImageFormat | string): Promise<IDecodedImage | undefined>;
/**
* Decode image data, auto-detecting format from file header.
*
* @param data Raw image file bytes
* @returns Decoded image, or undefined if format unknown or decoding fails
*/
static decodeAuto(data: Uint8Array): Promise<IDecodedImage | undefined>;
/**
* Encode RGBA pixel data to PNG format.
* Automatically uses the best encoder for the current environment.
*
* - Node.js: Uses pngjs (synchronous, optimized)
* - Browser: Uses canvas.toBlob (async)
*
* @param pixels RGBA pixel data (4 bytes per pixel)
* @param width Image width in pixels
* @param height Image height in pixels
* @returns PNG file bytes, or undefined if encoding fails
*/
static encodeToPng(pixels: Uint8Array, width: number, height: number): Promise<Uint8Array | undefined>;
/**
* Synchronous PNG encoding (Node.js only).
* Use this when you need synchronous behavior and know you're in Node.js.
*
* @param pixels RGBA pixel data (4 bytes per pixel)
* @param width Image width in pixels
* @param height Image height in pixels
* @returns PNG file bytes, or undefined if not in Node.js or encoding fails
*/
static encodeToPngSync(pixels: Uint8Array, width: number, height: number): Uint8Array | undefined;
/**
* Encode RGBA pixels to PNG using browser Canvas API.
*
* @param pixels RGBA pixel data (4 bytes per pixel)
* @param width Image width in pixels
* @param height Image height in pixels
* @returns PNG file bytes, or undefined if not in browser or encoding fails
*/
static encodeToPngBrowser(pixels: Uint8Array, width: number, height: number): Promise<Uint8Array | undefined>;
/**
* Check if data is a PNG file (magic number: 0x89 0x50 0x4E 0x47).
*/
static isPngData(data: Uint8Array): boolean;
/**
* Check if data is a JPEG file (magic number: 0xFF 0xD8 0xFF).
*/
static isJpegData(data: Uint8Array): boolean;
/**
* Check if data is a TGA file.
*
* TGA has no magic number, so we check:
* 1. It's NOT PNG or JPEG (which have magic numbers)
* 2. Byte 2 (image type) is a valid TGA type
*
* Valid TGA types:
* - 1: Uncompressed color-mapped
* - 2: Uncompressed true-color
* - 3: Uncompressed grayscale
* - 9: RLE color-mapped
* - 10: RLE true-color
* - 11: RLE grayscale
*/
static isTgaData(data: Uint8Array): boolean;
/**
* Detect image format from file header bytes.
*
* @param data Raw file bytes
* @returns Detected format, or undefined if unknown
*/
static detectFormat(data: Uint8Array): ImageFormat | undefined;
/**
* Convert raw image bytes to a data URL.
*
* @param data Image file bytes (PNG, JPEG, etc.)
* @param mimeType MIME type (e.g., "image/png")
* @returns Data URL string (data:image/png;base64,...)
*/
static toDataUrl(data: Uint8Array, mimeType: string): string;
/**
* Convert decoded image to PNG data URL.
*
* @param image Decoded image with RGBA pixels
* @returns PNG data URL, or undefined if encoding fails
*/
static toPngDataUrl(image: IDecodedImage): Promise<string | undefined>;
/**
* Convert TGA data directly to PNG data.
*
* @param tgaData Raw TGA file bytes
* @returns PNG file bytes, or undefined if conversion fails
*/
static tgaToPng(tgaData: Uint8Array): Promise<Uint8Array | undefined>;
/**
* Convert TGA data to PNG data URL.
*
* @param tgaData Raw TGA file bytes
* @returns PNG data URL (data:image/png;base64,...), or undefined if fails
*/
static tgaToPngDataUrl(tgaData: Uint8Array): Promise<string | undefined>;
/**
* Convert BGRA pixel data to RGBA.
* TGA files often store pixels in BGRA format.
*
* @param pixels BGRA pixel data (modified in place)
* @returns The same array with R and B swapped
*/
static bgraToRgba(pixels: Uint8Array): Uint8Array;
/**
* Create a solid color image.
*
* @param width Image width
* @param height Image height
* @param r Red component (0-255)
* @param g Green component (0-255)
* @param b Blue component (0-255)
* @param a Alpha component (0-255, default 255)
* @returns Decoded image with solid color
*/
static createSolidColor(width: number, height: number, r: number, g: number, b: number, a?: number): IDecodedImage;
}