MoonBit image library: pure MoonBit backend for native/wasm-gc/js/wasm multi-target support.
详细说明(版本映射、格式检测、多目标、核心约束)见 docs/notes.md。
| 特性 | 说明 | |
|---|---|---|
| 🟢 | 零 C 依赖 | 全部纯 MoonBit 实现,无需 C 编译器,部署极简 |
| 🟢 | 四目标支持 | native / wasm-gc / js / wasm 共用同一代码库,无条件编译 |
| 🟢 | 格式覆盖广 | PNG / JPEG / BMP / GIF / QOI / TGA / PSD / HDR / PNM / TIFF / ICO / CUR / ICNS / APNG / WebP — 含独家 PSD、HDR |
| 🟢 | 像素深度全 | 8 位 Image、16 位 Image16、HDR 浮点 ImageF |
| 🟢 | 283 个 API | 从基础 I/O 到 FFT、Canny、分水岭、SLIC、ORB、SIFT、SIFT 匹配、RANSAC 单应性、grabCut、流式解码、光流、模板匹配、WebP lossy 编码等高级算法 |
| 🟢 | 流式解码 | 逐行 / 分块 / 指定通道回调,大图处理零内存峰值 |
| 🟢 | 多子包架构 | 8 个子包职责清晰,编译并行化,可独立测试 |
| 格式 | 解码 | 编码 | 备注 |
|---|---|---|---|
| PNG | ✅ | ✅ | 8/16-bit,Adam7 隔行扫描 |
| JPEG | ✅ | ✅ | baseline,可调质量 |
| BMP | ✅ | ✅ | 1/4/8/16/24/32-bit |
| GIF | ✅ | ✅ | 动画 GIF 解码/编码 |
| QOI | ✅ | ✅ | Quite OK Image |
| TGA | ✅ | ✅ | 含 RLE |
| PSD | ✅ | — | Photoshop 文档(独家) |
| HDR | ✅ | ✅ | IEEE 754 浮点(独家) |
| PNM | ✅ | ✅ | PPM / PGM |
| TIFF | ✅ | ✅ | 无压缩/LZW/PackBits |
| ICO | ✅ | ✅ | 单尺寸/多尺寸 |
| CUR | ✅ | ✅ | Windows 光标 |
| ICNS | ✅ | ✅ | macOS 图标 |
| APNG | ✅ | ✅ | 动画 PNG |
| WebP | ✅ | ✅ | lossless (VP8L) 解码 + lossy (VP8) 编码 |
moon add walkzzz/image// 从字节解码
let img : Image = load_from_bytes(png_bytes)
println("width=\{img.width}, height=\{img.height}, channels=\{img.channels}")
// 编码为 PNG 字节
let out : Bytes = write_png_to_bytes(img)
// 缩放(7 种滤波器 × 4 种边缘模式)
let resized : Image = resize(img, 128, 128)
// 自动检测格式并解码
let any : Image = decode_any(data, req_channels=Some(3))
// 加载动画 GIF
let anim : GifAnimation = load_gif_from_bytes(gif_bytes)
println("frames=\{anim.frames.length()}, delays=\{anim.delays}")
// 查询图像信息(不解码像素)
let info : ImageInfo? = info_from_bytes(data)
// 流式解码:逐行回调,大图零内存峰值
decode_stream(data, fn(row, y) {
// 处理第 y 行像素 row : Array[Array[Int]]
})try {
let img = load_from_bytes(data)
} catch {
LoadError::FileIO(msg) => println("文件IO错误: \{msg}")
LoadError::DecodeFailed(msg) => println("解码失败: \{msg}")
LoadError::UnsupportedFormat(msg) => println("不支持的格式: \{msg}")
}// 检测 SIFT 特征
let kp1 = sift_detect(img1)
let kp2 = sift_detect(img2)
// L2 距离 + Lowe 比率测试匹配
let matches = sift_match(kp1, kp2, ratio_threshold=0.75)
// RANSAC 鲁棒单应性估计
let homography = ransac_homography(matches, threshold=5.0, iterations=1000)| 目标 | 后端 | 测试 | 状态 |
|---|---|---|---|
| native | 纯 MoonBit | 1177 | ✅ |
| wasm-gc | 纯 MoonBit | 1177 | ✅ |
| js | 纯 MoonBit | 1177 | ✅ |
| wasm | 纯 MoonBit | 1177 | ✅ |
src/
├── types/ # 全目标类型 (Image, Image16, ImageF, LoadError 等)
├── pure/ # 纯 MoonBit 后端 (无 C FFI)
│ ├── codec/ # 格式编解码 (15 种格式)
│ ├── color/ # 颜色操作
│ └── util/ # 工具
├── lib/ # 高层封装 (自动格式分派)
├── meta/ # 元数据 (EXIF, PNG meta)
├── process/ # 高级图像处理算法 (7 子包)
│ ├── color/ # 色彩转换/调整/CLAHE/自适应阈值
│ ├── edge/ # 边缘检测/Canny/霍夫/轮廓
│ ├── feature/ # 特征检测: Harris/ORB/SIFT/模板匹配/光流/GLCM/LBP
│ ├── filter/ # 滤波/去噪/图像修复
│ ├── frequency/ # FFT/DCT/Haar 小波/频率滤波
│ ├── segment/ # 分水岭/SLIC/grabCut/形态学/连通域
│ └── transform/ # 几何变换/透视/Seam Carving/金字塔
├── examples/ # 示例代码 (32 个示例,覆盖全部 API)
├── util/ # 工具函数 (基于 pure 的上层封装)
├── bench.mbt # 性能基准测试
└── reexport.mbt # 顶层 API re-export (283 pub fn + 47 pub type)| 文档 | 说明 |
|---|---|
| docs/architecture.md | 架构图、包依赖关系、设计决策 |
| docs/api_reference.md | 完整 API 参考(283 函数 + 47 类型) |
| docs/roadmap.md | 迭代路线图 |
| docs/comparison.md | mooncakes.io 图像库对比 |
| docs/performance_report.md | 性能基准报告(31 项基准) |
| docs/notes.md | 使用说明与核心约束 |
| docs/examples.md | 完整示例集(32 个示例) |
| docs/features.md | 功能一览(283 API 分类概览) |
| docs/contributing.md | 贡献指南(开发环境/流程/规范) |
| docs/skill.md | AI 辅助开发技能描述 |
| docs/changelog.md | 版本变更历史 |
# 编译检查(四目标)
moon check
moon check --target wasm-gc
moon check --target js
moon check --target wasm
# 运行测试(四目标各 1177)
moon test --target native
moon test --target wasm-gc
moon test --target js
moon test --target wasm
# 运行性能基准测试
moon run --target native
# 重新生成 API 接口
moon info| 属性 | 值 |
|---|---|
| 原项目 | stb — stb_image.h |
| 原作者 | Sean Barrett (@nothings) |
| 原许可证 | MIT / Public Domain |
| 原语言 | C (单头文件库) |
| 移植目标 | MoonBit (纯语言,零 C FFI) |
| 方面 | stb_image.h | 本项目 |
|---|---|---|
| 语言 | C | MoonBit |
| 依赖 | C 编译器 | 零 C 依赖 |
| 目标 | native | native / wasm-gc / js / wasm |
| 格式数 | 7 | 15 |
| API 数 | ~30 | 283 |
| 高级算法 | 无 | 50+ |
| 内存安全 | 手动 | GC 管理 |
MoonBit image library: pure MoonBit backend for native/wasm-gc/js/wasm multi-target support.