一个纯 MoonBit 实现的图像解码库,支持 BMP/QOI/TGA/PNG/GIF/JPEG 六种格式,内置完整 DEFLATE 解压器和 LZW/RLE 压缩支持,一行代码即可完成图像解码。
| Format | Supported input |
|---|---|
| BMP | 1/4/8-bit indexed, 24-bit BGR, 32-bit BGRA; RLE4/RLE8; selected 32-bit BITFIELDS layouts |
| QOI | RGB/RGBA headers, all six chunk types, RGBA8 output |
| TGA | Types 2/3/10/11, truecolor 16/24/32-bit and grayscale 8/16-bit, four image origins |
| PNG | Standard color/depth combinations, tRNS, Adam7, stored/fixed/dynamic DEFLATE |
| GIF | GIF87a/89a, LZW, interlace, transparency and composited animation frames |
| JPEG | 8-bit SOF0 baseline, grayscale and conventional YCbCr 4:4:4/4:2:2/4:2:0 |
moon add shunge/imagefn main {
// Auto-detect format and decode
let img = @image.decode(data)
println("Image: \{img.width} x \{img.height}")
println("Format: \{@image.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 the header resembles a supported format; this does not validate pixels
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 # Embedded JPEG photographs with Pillow reference pixels
├── real_photo_test.mbt # Embedded photographs in BMP/PNG/TGA/QOI/GIF
├── *_regression_test.mbt # Format-specific independent pixel and malformed-input cases
├── decoder_stability_test.mbt # Structured malformed-input stability checks
│
├── 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 check --deny-warn
moon fmt --check
moon info
git diff --exit-code -- pkg.generated.mbti
moon test --deny-warn --target wasm-gc
moon test --deny-warn --target js
moon test --deny-warn --target nativepython -m pip install Pillow==12.3.0
python -B -m unittest discover -s tools -p test_pillow_reference.py
python tools/verify_decoders.py
python tools/verify_decoders.py --corpus /path/to/photos --max-side 128python tools/generate_png_regression_fixtures.py
python tools/generate_png_deflate_fixtures.py
python tools/generate_bmp_regression_fixtures.py
python tools/generate_gif_regression_fixtures.py
python tools/generate_jpeg_regression_fixtures.py
python tools/generate_qoi_tga_fixtures.py
python tools/generate_photo_references.py
moon fmt
moon test --deny-warnfn 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 压缩支持,一行代码即可完成图像解码。