一个纯 MoonBit 实现的图像解码库,支持 BMP/QOI/TGA/PNG/GIF/JPEG 六种格式,内置完整 DEFLATE 解压器和 LZW/RLE 压缩支持,一行代码即可完成图像解码。
| Format | Status | Description |
|---|---|---|
| BMP | ✅ Complete | 1/4/8/24/32-bit, top-down & bottom-up |
| QOI | ✅ Complete | RGB & RGBA, full spec compliance |
| TGA | ✅ Complete | Uncompressed & RLE, 8/16/24/32-bit |
| PNG | ✅ Complete | 8-bit grayscale/RGB/RGBA, indexed, Adam7, full DEFLATE |
| GIF | ✅ Complete | GIF87a/89a, LZW decompression, interlace, transparency |
| JPEG | ✅ Baseline | Grayscale + YCbCr color, DCT/IDCT, Huffman |
moon add shunge/imagefn main {
// Auto-detect format and decode
let img = @image.decode(data)
println("Image: \{img.width} x \{img.height}")
println("Format: \{pixel_format_name(img.format)}")
// Access individual pixels
let pixel = img.get_pixel(10, 20)
println("Pixel: R=\{pixel.r} G=\{pixel.g} B=\{pixel.b} A=\{pixel.a}")
}// Auto-detect format and decode
pub fn decode(data : Bytes) -> Image raise Failure
// Decode with known format
pub fn decode_by_format(data : Bytes, format : ImageFormat) -> Image raise Failure
// Detect format from magic bytes
pub fn detect_format(data : Bytes) -> Option[ImageFormat]
// Check if data matches any supported format
pub fn is_supported_format(data : Bytes) -> Bool
// Get human-readable pixel format name
pub fn pixel_format_name(format : PixelFormat) -> Stringpub fn decode_bmp(data : Bytes) -> Image raise Failure
pub fn decode_qoi(data : Bytes) -> Image raise Failure
pub fn decode_tga(data : Bytes) -> Image raise Failure
pub fn decode_png(data : Bytes) -> Image raise Failure
pub fn decode_gif(data : Bytes) -> Image raise Failure
pub fn decode_jpeg(data : Bytes) -> Image raise Failure
pub fn decode_gif_all(data : Bytes) -> AnimatedImage raise Failure // animated GIFpub struct Image {
width : Int
height : Int
format : PixelFormat
data : Bytes // Raw pixel data, row-major
}
// Constructors & methods
pub fn Image::new(width : Int, height : Int, format : PixelFormat, data : Bytes) -> Image
pub fn Image::get_pixel(self : Image, x : Int, y : Int) -> Color
pub fn Image::to_rgba8(self : Image) -> Image
pub fn Image::bytes_per_pixel(self : Image) -> Intpub enum PixelFormat {
Gray8 // 8-bit grayscale
GrayA8 // 16-bit grayscale + alpha
RGB8 // 24-bit RGB
RGBA8 // 32-bit RGBA
}shunge/image
├── lib.mbt # Main entry: format detection + dispatch
├── types.mbt # Core types: Image, Color, PixelFormat, BitReader
├── utils.mbt # Byte reading utilities, CRC32, Adler32
├── color.mbt # IDCT, YCbCr→RGB color conversion
├── transform.mbt # Image transform utilities (crop/flip/resize/rotate)
│
├── bmp.mbt # BMP decoder
├── qoi.mbt # QOI decoder
├── tga.mbt # TGA decoder
├── png.mbt # PNG decoder + DEFLATE decompressor
├── gif.mbt # GIF decoder + LZW decompressor
├── jpeg.mbt # JPEG baseline decoder (DCT/IDCT/Huffman)
│
├── bmp_writer.mbt # BMP encoder
├── qoi_writer.mbt # QOI encoder
│
├── image_test.mbt # Core tests: format-specific decoders + error paths
├── medium_image_test.mbt # Medium-size tests: 64×64 images
├── complex_image_test.mbt # Complex pattern tests: 128×128 checkerboard, noise, Mandelbrot
├── comprehensive_test.mbt # Comprehensive tests: GIF/JPEG features, animated GIF, errors
├── fuzz_test.mbt # Fuzz testing (random bytes → decoders, must not crash)
├── roundtrip_test.mbt # Round-trip encode/decode tests
├── jpeg_real_test.mbt # Real JPEG photo tests (11 photos, decode_jpeg verification)
│
├── example/ # CLI example: image_info
├── tools/ # Test image generation scripts
├── test_images/ # Test images (up to 2048×2048 + real photos)
├── ARTICLE.md # Article: hand-writing DEFLATE in MoonBit
└── LICENSE # MITmoon test # Run all 108 testsfn AnimatedImage::new(frames : Array[Image], delays : Array[Int], width : Int, height : Int, loop_count : Int) -> AnimatedImagepub struct Color {
r : Int
g : Int
b : Int
a : Int
}pub enum ImageFormat {
BMP
QOI
TGA
PNG
GIF
JPEG
}pub enum PixelFormat {
Gray8
GrayA8
RGB8
RGBA8
}fn is_supported_format(data : Bytes) -> BoolInstall
Download zip一个纯 MoonBit 实现的图像解码库,支持 BMP/QOI/TGA/PNG/GIF/JPEG 六种格式,内置完整 DEFLATE 解压器和 LZW/RLE 压缩支持,一行代码即可完成图像解码。