Pure MoonBit image processing library with filters, morphology, bilateral and color-space operations, affine transforms, drawing primitives with a bitmap font, layer compositing with blend modes, PNG/GIF/QOI/BMP codecs, baseline TIFF decoding, and JPEG/WebP/AVIF codec adapters. Backend-agnostic (js, wasm-gc, native) with a browser Playground.
一个纯 MoonBit 实现的图像处理库,附带一个在浏览器里实时运行的 Playground。 后端无关的核心库可编译到 JavaScript / WebAssembly (wasm-gc & 线性内存 wasm) / native。稳定 API 里程碑已发布:GitHub 保留 v1.0.0 作为稳定版标记;当前 MoonBit 包版本为 0.14.0(mooncakes.io 要求主版本号为 0)。公开 API 在 0.x 系列中保持兼容,破坏性变更会记录在 CHANGELOG。
| 现有项目 | 定位 | 与 PixelForge 的差异 |
|---|---|---|
| megemini/millow | 更广的计算机视觉算法库(增强、轮廓/区域特征、HOG/LBP、SSIM 等) | 侧重 CV 算法;PixelForge 侧重零依赖、多后端编解码和浏览器交互 |
| PingGuoMiaoMiao/MoonVision | 轻量图像处理 + 基础 CV(灰度图为主、模板匹配) | 侧重灰度图像处理;不提供 RGBA 全彩编解码、绘图/文字、噪声与哈希 |
| shunge/image | 纯解码器(BMP/QOI/TGA/PNG/GIF/JPEG 六格式解码) | 仅解码;不提供滤镜、编码、绘图与分析能力 |
pixelforge/
├── image.mbt # Image 数据结构、像素读写、clamp_byte
├── filters_basic.mbt # map_rgb 引擎 + 灰度/反色/亮度/对比度
├── convolution.mbt # Kernel + convolve + 模糊/锐化/浮雕/边缘/Sobel/Scharr
├── filters_advanced.mbt # 棕褐色/二值化/像素化/中值/直方图均衡/色调分离
├── filters_effects.mbt # 伽马校正/暗角
├── colorspace.mbt # RGB↔HSV、RGB↔YCbCr、饱和度/色相旋转
├── morphology.mbt # 3×3 腐蚀/膨胀/开/闭运算
├── canny.mbt # Canny 边缘检测(NMS + 滞后阈值)
├── affine.mbt # 仿射变换(旋转/平移/缩放/错切,逆映射采样)
├── drawing.mbt # 绘图原语(Bresenham 直线/矩形/圆/填充)
├── gaussian.mbt # 可分离高斯模糊(任意半径,二项式权重)
├── unsharp.mbt # 锐化蒙版(复用高斯)
├── croppad.mbt # 裁剪 / 颜色边框填充
├── integral.mbt # 积分图(SAT)+ O(1) 盒式模糊
├── floodfill.mbt # 泛洪填充(四连通种子填充)
├── distance.mbt # chamfer (3,4) 距离变换
├── stats.mbt # 图像统计 / 自动对比度 / 色阶
├── bicubic.mbt # 双三次缩放(Catmull-Rom)
├── phash.mbt # 感知哈希(aHash/dHash + 汉明距离)
├── noise.mbt # 确定性噪声(高斯 / 椒盐,LCG)
├── hsl.mbt # HSL 色彩空间 + 亮度调整
├── histogram.mbt # 直方图 API(亮度 / RGB)
├── bilateral.mbt # 双边滤波(保边平滑)
├── otsu.mbt # Otsu 自动阈值(类间方差最大化)
├── dither.mbt # Floyd–Steinberg 误差扩散抖动
├── components.mbt # 四连通连通域标记/计数
├── blend.mbt # 图层合成(source-over + 8 种混合模式)
├── text.mbt # 5×7 位图字体 draw_char/draw_text
├── png.mbt # PNG 编解码(完整 DEFLATE inflate + 校验)
├── gif.mbt # GIF 解码(变长 LZW、交错、透明索引)
├── qoi.mbt # QOI 图像编解码(完整规范)
├── bmp.mbt # BMP 编解码(无压缩 24/32 位)
├── transform.mbt # 水平/垂直翻转、90° 旋转
├── resize.mbt # 最近邻/双线性缩放
├── dispatch.mbt # Image::apply_filter_id 统一派发(各绑定共用)
├── pipeline.mbt # 类型安全、可复用的 Filter / Pipeline 管线 API
├── metrics.mbt # MSE / PSNR / luma MSE / SSIM
├── adaptive_threshold.mbt # Sauvola / 局部均值阈值
├── regionprops.mbt # 连通域面积、边界框、质心
├── *_test.mbt # 确定性测试(黑盒 + 白盒)
├── cmd/main/ # 原生 CLI 示例(moon run cmd/main)
├── cmd/ppm/ # PPM 图像输出示例(moon run cmd/ppm > edges.ppm)
├── cmd/showcase/ # 综合展示(绘图+文字+滤镜+PNG 往返自检)
├── cmd/cli/ # native info/convert 编解码 CLI + 十六进制模式
├── web/ # 浏览器绑定 + Playground(HTML/CSS/JS)
│ ├── bindings.mbt # js 后端绑定 apply_filter(零拷贝)
│ ├── dist/web.js # 已构建的 MoonBit→JS 产物
│ ├── dist/wasmcore.wasm # 已构建的线性内存 wasm 产物
│ └── index.html / playground.js / style.css
├── wasmcore/ # 线性内存 wasm 绑定(alloc/process + 导出 memory)
├── scripts/build-web.mjs # 构建并校验 web/dist 产物
└── serve.mjs # 零依赖静态服务器moon test # 运行完整单元测试套件
moon run cmd/main # 运行原生示例(生成图像并跑滤镜,打印校验和)
moon run cmd/showcase > showcase.ppm # 综合展示:绘图+文字+滤镜+PNG 往返自检
moon run cmd/ppm > edges.ppm # 生成一张 Sobel 边缘检测的 PPM 图片
moon run --target native cmd/cli -- --help # 查看文件编解码 CLInode serve.mjs # 然后打开 http://localhost:8123node scripts/build-web.mjs # 构建并同步 web/dist/
node scripts/build-web.mjs --check # 仅检查提交的产物是否同步// 从 RGBA 字节缓冲区(w*h*4)构造图像
let img = @pixelforge.Image::from_bytes(width, height, rgba_bytes)
// 链式调用滤镜(每个滤镜返回一张新图,不修改原图)
let stylized = img.grayscale().sobel()
let soft = img.blur().brightness(20)
// 自定义卷积核
let kernel = @pixelforge.Kernel::new(3, [0.0, -1.0, 0.0, -1.0, 5.0, -1.0, 0.0, -1.0, 0.0], 1.0, 0.0)
let sharp = img.convolve(kernel)
// 按数字 id 派发(供各宿主绑定 / CLI 共用)
let out = img.apply_filter_id(8, 0.0) // 8 = Sobel
// 类型安全的可复用管线(按追加顺序执行)
let pipeline = @pixelforge.Pipeline::new()
.append(@pixelforge.Filter::Grayscale)
.append(@pixelforge.Filter::Contrast(1.25))
.append(@pixelforge.Filter::Sobel)
let out2 = img.apply_pipeline(pipeline)
// 质量指标:RGBA 误差与 Rec.601 亮度相似度
let error = img.mse(out2)
let score = img.psnr(out2)
let similarity = img.ssim(out2)
// 取回处理后的像素
let bytes = out.data // FixedArray[Byte],长度 = width*height*4| id | 滤镜 | 方法 | amount |
|---|---|---|---|
| 0 | 灰度 | grayscale() | — |
| 1 | 反色 | invert() | — |
| 2 | 亮度 | brightness(delta) | −255..255 |
| 3 | 对比度 | contrast(factor) | 0.0..3.0 |
| 4 | 高斯模糊 | blur() | — |
| 5 | 锐化 | sharpen() | — |
| 6 | 浮雕 | emboss() | — |
| 7 | 拉普拉斯边缘 | edges() | — |
| 8 | Sobel 边缘 | sobel() | — |
| 9 | 棕褐色 | sepia() | — |
| 10 | 二值化 | threshold(level) | 阈值(默认 128) |
| 11 | 像素化 | pixelate(block) | 块大小(默认 8) |
| 12 | 中值降噪 | median() | — |
| 13 | 直方图均衡 | histogram_equalize() | — |
| 14 | 水平翻转 | flip_horizontal() | — |
| 15 | 垂直翻转 | flip_vertical() | — |
| 16 | 色调分离 | posterize(levels) | 色阶数(默认 4) |
| 17 | 伽马校正 | gamma(value) | 伽马值(默认 2.2) |
| 18 | 暗角 | vignette(strength) | 强度 0..1(默认 0.5) |
| 19 | Scharr 边缘 | scharr() | — |
| 20 | Canny 边缘 | canny(low, high) | 高阈值(默认 100,低阈值取一半) |
| 21 | Otsu 自动阈值 | otsu() | — |
| 22 | Floyd–Steinberg 抖动(二值) | dither_mono() | — |
会改变尺寸的变换不走 id 派发,直接调用库 API:rotate90()、resize_nearest(w, h)、resize_bilinear(w, h)、resize_bicubic(w, h)。多参数 / 非图像→图像的 API 同理:average_hash()/difference_hash()/hamming_distance、add_gaussian_noise/add_salt_pepper、histogram_luma()/histogram_rgb()、rgb_to_hsl/hsl_to_rgb/adjust_lightness、box_blur(radius)、flood_fill(x, y, color, tol)、distance_transform(t)、gaussian(radius)、unsharp_mask(radius, amount)、crop(x, y, w, h)、pad(l, t, r, b, color)、bilateral(radius, σs, σr)、dither_grayscale(levels)/dither_mono()、otsu_threshold()、label_components(t)/count_components(t)、composite(top, mode)、draw_text(...)、rotate(deg)、translate(dx, dy)、affine(t)、draw_line/draw_rect/draw_circle 等绘图原语、saturate(factor)、hue_rotate(deg)、erode()/dilate()/morph_open()/morph_close()、png_encode/png_decode、gif_encode/gif_decode、qoi_encode/qoi_decode、bmp_encode/bmp_decode、tiff_decode、jpeg_decode/jpeg_encode、webp_encode、avif_encode、rgb_to_hsv 等色彩空间函数。
一个诚实的性能观察:在浏览器里对同一套滤镜做基准对比,MoonBit 的 js 后端反而比线性内存 wasm 快约 4–5×。原因是 V8 对 JS 后端产物做了深度 JIT 优化,而 wasm 路径还多了进/出线性内存的拷贝与运行时开销。这说明"WASM 一定更快"是一种误解——Playground 保留了引擎切换与对比按钮,你可以自己复现这个结论。
moon test # 默认后端(wasm-gc)
moon test --target js # js 后端模块名为 0717lee/pixelforge。其他 MoonBit 项目可通过 moon add 0717lee/pixelforge 添加依赖。
moon login # 登录 mooncakes.io
moon publish # 发布pub(all) struct Affine {
a : Double
b : Double
c : Double
d : Double
e : Double
f : Double
}pub(all) enum BlendMode {
Normal
Multiply
Screen
Overlay
Darken
Lighten
Difference
Add
}pub(all) struct BoundingBox {
min_x : Int
min_y : Int
max_x : Int
max_y : Int
}pub(all) struct ComponentStats {
label : Int
area : Int
bbox : BoundingBox
centroid : (Double, Double)
min_x : Int
min_y : Int
max_x : Int
max_y : Int
centroid_x : Double
centroid_y : Double
}pub(all) struct ContourPoint {
x : Int
y : Int
}pub(all) struct Corner {
x : Int
y : Int
response : Double
}pub(all) enum Filter {
Grayscale
Invert
Brightness(Int)
Contrast(Double)
Blur
Sharpen
Emboss
Edges
Sobel
Sepia
Threshold(Int)
Pixelate(Int)
Median
HistogramEqualize
FlipHorizontal
FlipVertical
Posterize(Int)
Gamma(Double)
Vignette(Double)
Scharr
Canny(Int)
Otsu
DitherMono
}pub(all) struct GifFrame {
image : Image
left : Int
top : Int
delay_cs : Int
disposal : Int
transparent_index : Int
}pub(all) struct Image {
width : Int
height : Int
data : FixedArray[Byte]
}| id | filter | amount |
|---|---|---|
| 0 | grayscale | — |
| 1 | invert | — |
| 2 | brightness | delta (−255..255) |
| 3 | contrast | factor (0.0..3.0) |
| 4 | gaussian blur | — |
| 5 | sharpen | — |
| 6 | emboss | — |
| 7 | laplacian edges | — |
| 8 | sobel edges | — |
| 9 | sepia | — |
| 10 | threshold | level (default 128) |
| 11 | pixelate | block size (default 8) |
| 12 | median denoise | — |
| 13 | histogram equalize | — |
| 14 | flip horizontal | — |
| 15 | flip vertical | — |
| 16 | posterize | levels (default 4) |
| 17 | gamma | gamma value (default 2.2) |
| 18 | vignette | strength 0..1 (default 0.5) |
| 19 | scharr edges | — |
| 20 | canny edges | high threshold (default 100, low = high/2) |
| 21 | otsu auto threshold | — |
| 22 | floyd–steinberg dither (mono) | — |
pub(all) enum ImageFormat {
Png
Gif
Qoi
Bmp
Jpeg
Webp
Avif
Tiff
}pub(all) struct ImageTile {
image : Image
origin_x : Int
origin_y : Int
tile_width : Int
tile_height : Int
}pub struct Stats {
min_r : Int
max_r : Int
mean_r : Int
min_g : Int
max_g : Int
mean_g : Int
min_b : Int
max_b : Int
mean_b : Int
luma_min : Int
luma_max : Int
luma_mean : Int
}fn clamp_byte(value : Int) -> Bytefn hamming_distance(a : UInt64, b : UInt64) -> Int= 20 means "clearly different".
fn hsl_to_rgb(h : Double, s : Double, l : Double) -> (Int, Int, Int)fn hsv_to_rgb(h : Double, s : Double, v : Double) -> (Int, Int, Int)fn rgb_to_hsl(r : Int, g : Int, b : Int) -> (Double, Double, Double)fn rgb_to_hsv(r : Int, g : Int, b : Int) -> (Double, Double, Double)fn rgb_to_ycbcr(r : Int, g : Int, b : Int) -> (Int, Int, Int)fn ycbcr_to_rgb(y : Int, cb : Int, cr : Int) -> (Int, Int, Int)Install
Download zipPure MoonBit image processing library with filters, morphology, bilateral and color-space operations, affine transforms, drawing primitives with a bitmap font, layer compositing with blend modes, PNG/GIF/QOI/BMP codecs, baseline TIFF decoding, and JPEG/WebP/AVIF codec adapters. Backend-agnostic (js, wasm-gc, native) with a browser Playground.