#walkzzz/image.mbt/lib

    高层封装(自动格式分派、流式解码、模糊测试)

    #概览

    项目数量
    公开函数16
    公开类型1
    常量0
    错误类型0

    #类型

    pub(all) struct StreamInfo {

    #API

    pub fn decode_stream(Bytes, (Int, Array[Byte]) -> Unit) -> StreamInfo raise @types.LoadError
    pub fn decode_stream_channels(Bytes, Int, (Int, Array[Byte]) -> Unit) -> StreamInfo raise @types.LoadError
    pub fn decode_stream_chunked(Bytes, Int, (Int, @types.Image) -> Unit) -> StreamInfo raise @types.LoadError
    pub fn detect_format(Bytes) -> @types.ImageFormat
    pub fn encode_bmp(@types.Image) -> Bytes
    pub fn encode_gif(@types.Image) -> Bytes raise @types.LoadError
    pub fn encode_hdr(@types.ImageF) -> Bytes raise @types.LoadError
    pub fn encode_jpeg(@types.Image) -> Bytes raise @types.LoadError
    pub fn encode_pgm_auto(@types.Image) -> Bytes
    pub fn encode_png(@types.Image) -> Bytes
    pub fn encode_pnm_auto(@types.Image) -> Bytes
    pub fn encode_ppm_auto(@types.Image) -> Bytes
    pub fn encode_qoi(@types.Image) -> Bytes raise @types.LoadError
    pub fn encode_tga(@types.Image) -> Bytes
    pub fn load_16_from_bytes_auto(Bytes, req_channels? : Int?) -> @types.Image16 raise @types.LoadError
    pub fn load_from_bytes_auto(Bytes, req_channels? : Int?) -> @types.Image raise @types.LoadError

    #纯 MoonBit 实现

    本包不依赖任何 C FFI,四目标(native/wasm-gc/js/wasm)均可编译运行。

    ImageFormat

    图像格式枚举 Image format enum

    标识支持的图像编解码格式。#non_exhaustive 表示未来可能添加新格式。 Identifies supported image codec formats. #non_exhaustive means new formats may be added in the future.

    EncodedImage

    pub(all) struct EncodedImage {
    format :
    core
    /types.ImageFormat
    bytes : Bytes
    } derive(
    Debug
    )

    一次候选编码的结果。

    StreamInfo

    pub(all) struct StreamInfo {
    width : Int
    height : Int
    channels : Int
    } derive(Default, Eq,
    Debug
    )

    流式解码信息

    StreamInfo::default

    fn StreamInfo::default() -> StreamInfo
    automatically derived

    StreamInfo::equal

    fn StreamInfo::equal(StreamInfo, StreamInfo) -> Bool
    automatically derived

    StreamInfo::not_equal

    fn StreamInfo::not_equal(x : StreamInfo, y : StreamInfo) -> Bool
    automatically derived

    StreamInfo::to_repr

    automatically derived

    UnifiedImageInfo

    pub(all) struct UnifiedImageInfo {
    format :
    core
    /types.ImageFormat
    format_name : String
    width : Int
    height : Int
    channels : Int
    bit_depth : Int
    has_alpha : Bool
    } derive(Eq,
    Debug
    )

    统一图像信息(跨格式通用字段)

    UnifiedImageInfo::equal

    automatically derived

    UnifiedImageInfo::not_equal

    fn UnifiedImageInfo::not_equal(x : UnifiedImageInfo, y : UnifiedImageInfo) -> Bool
    automatically derived

    UnifiedImageInfo::to_repr

    automatically derived

    best_format

    fn best_format(img :
    core
    /types.Image) ->
    core
    /types.ImageFormat

    廉价启发式:不编码,只按"有无透明"给一个偏好格式。 不透明图默认给 Jpeg(照片类通常最省);带透明给 Webp(无损且有 alpha)。 想要真实最小体积请用 encode_best。

    check_decode_budget

    fn check_decode_budget(data : Bytes, limits :
    core
    /types.DecodeLimits) -> (Int64, Int64) raise
    core
    /types.ImageError

    只做准入检查(不真正解码),供"先验后解"的调用方使用。 Admission check only (no decoding); for callers that validate before decoding.

    decode_animation

    fn decode_animation(data : Bytes) -> (Array[
    core
    /types.Image], Array[Int]) raise
    core
    /types.LoadError

    解码动画(GIF / APNG),返回帧数组与每帧延迟(毫秒)。 非动画格式报 DecodeFailed。

    decode_region

    fn decode_region(data : Bytes, x : Int, y : Int, w : Int, h : Int) ->
    core
    /types.Image raise
    core
    /types.LoadError

    区域解码入口 Region decode entry

    按魔数自动识别 PNG / BMP / QOI,只解码请求矩形。

    decode_stream

    fn decode_stream(data : Bytes, on_row : (Int, Array[Byte]) -> Unit) -> StreamInfo raise
    core
    /types.LoadError

    流式解码:逐行回调处理。 on_row 回调接收 (行号, 行数据) 参数。 行数据为 w*channels 个字节,按 RGB/RGBA 顺序排列。

    decode_stream_channels

    fn decode_stream_channels(data : Bytes, req_channels : Int, on_row : (Int, Array[Byte]) -> Unit) -> StreamInfo raise
    core
    /types.LoadError

    流式解码(带 req_channels):指定输出通道数。

    decode_stream_chunked

    fn decode_stream_chunked(data : Bytes, chunk_height : Int, on_chunk : (Int,
    core
    /types.Image) -> Unit) -> StreamInfo raise
    core
    /types.LoadError

    分块流式解码:每 chunk_height 行回调一次。 on_chunk 回调接收 (起始行号, 块图像) 参数。 适用于分块处理大图,减少回调开销。

    decode_webp

    fn decode_webp(data : Bytes) ->
    core
    /types.Image raise
    core
    /types.LoadError

    WebP 解码委托(统一走 RFC 9649 合规的 VP8L 路径,palette / predictor / cross-color 全支持; 有损 VP8 产物仍会报 unsupported,那是另一条待办)

    decode_with_limits

    fn decode_with_limits(data : Bytes, limits :
    core
    /types.DecodeLimits) ->
    core
    /types.Image raise
    core
    /types.ImageError

    带预算解码:字节上限 → 头部像素上限 → 解码,错误统一投影成 ImageError。

    参数 Parameters:
    • data - 输入字节 Input bytes
    • limits - 预算 Budget

    错误 Errors:
    • Budget(LimitBytes|LimitPixels, _) - 超出预算 Over budget
    • Decode(_, _) - 解码失败 Decode failure(code 稳定)

    detect_format

    fn detect_format(data : Bytes) ->
    core
    /types.ImageFormat

    • ICNS: 前4字节 "icns"
    • TIFF: 字节序标记 + magic 0x002A
    • ICO/CUR: 前4字节 00 00 01 00
    • TGA 无固定 magic bytes,按头部字段(color map / image type / bpp / 宽高)保守判定

    encode_apng

    fn encode_apng(frames : Array[
    core
    /types.Image], delays : Array[Int]) -> Bytes raise
    core
    /types.LoadError

    将帧序列 + 延迟(毫秒)编码为 APNG。 帧数必须 ≥1,delays 长度必须与 frames 一致,所有帧尺寸必须相同。

    encode_best

    fn encode_best(img :
    core
    /types.Image, quality : Int) -> EncodedImage raise
    core
    /types.ImageError

    把候选格式各编一遍,返回体积最小的那个(同体积时按候选顺序取前者)。 单个候选编码失败会被跳过;全部失败才报 ProcessFailed。 quality 传给 save_image_with_quality(当前仅 JPEG 生效,无损格式忽略)。

    encode_best_limited

    fn encode_best_limited(img :
    core
    /types.Image, quality : Int, max_bytes : Int64) -> EncodedImage raise
    core
    /types.ImageError

    带体积上限的 encode_best:最小结果仍超过 max_bytes 就报 Budget(LimitBytes)(不返回超限数据,避免调用方写出超限文件)。

    encode_bmp

    fn encode_bmp(img :
    core
    /types.Image) -> Bytes

    BMP 编码委托

    encode_gif

    fn encode_gif(img :
    core
    /types.Image) -> Bytes raise
    core
    /types.LoadError

    GIF 编码委托 GIF 编码委托 GIF encoding delegation

    错误 Errors:
    • LoadError::EncodeFailed - channels 不为 3 或 4 Channels not 3 or 4

    encode_hdr

    fn encode_hdr(img :
    core
    /types.ImageF) -> Bytes raise
    core
    /types.LoadError

    HDR 编码委托 HDR 编码委托 HDR encoding delegation

    错误 Errors:
    • LoadError::EncodeFailed - channels 不为 3 Channels not 3

    encode_jpeg

    fn encode_jpeg(img :
    core
    /types.Image) -> Bytes raise
    core
    /types.LoadError

    JPEG 编码委托 JPEG 编码委托 JPEG encoding delegation

    错误 Errors:
    • LoadError::EncodeFailed - 尺寸无效、channels 不支持 Invalid dimensions, unsupported channels

    encode_jpeg_progressive

    fn encode_jpeg_progressive(img :
    core
    /types.Image, quality : Int) -> Bytes raise
    core
    /types.LoadError

    渐进式 JPEG 编码委托(SOF2)。 Progressive JPEG encoding delegation (SOF2).

    参数 Parameters:
    • img - 输入图像;1 通道按灰度复制到三通道
    • quality - 1–100,50 等价标准量化表

    错误 Errors:
    • LoadError::EncodeFailed - 尺寸无效、或像素数超过 2,000,000(系数缓存上限)

    encode_jpeg_progressive_420

    fn encode_jpeg_progressive_420(img :
    core
    /types.Image, quality : Int) -> Bytes raise
    core
    /types.LoadError

    渐进式 JPEG 编码委托(SOF2,4:2:0 色度下采样)。 Progressive JPEG encoding with 4:2:0 chroma subsampling.

    参数 Parameters:
    • img - 输入图像;1 通道按灰度处理
    • quality - 1–100,50 等价标准量化表

    错误 Errors:
    • LoadError::EncodeFailed - 尺寸无效、或像素数超过 2,000,000(系数缓存上限)

    encode_jpeg_progressive_420_scripted

    fn encode_jpeg_progressive_420_scripted(img :
    core
    /types.Image, quality : Int, script : Array[
    codec
    /codec.JpegProgScan]) -> Bytes raise
    core
    /types.LoadError

    渐进式 JPEG 编码委托(SOF2,4:2:0,扫描脚本可配置)。

    script 给出每条扫描的分量集与 Ss/Se/Ah/Al;@codec.jpeg_progressive_script_libjpeg() 返回 libjpeg jpeg_simple_progression() 的 10 段默认脚本, @codec.jpeg_progressive_script_eight() 为 0.1.30 固定 8 段等价脚本。

    错误 Errors:
    • LoadError::EncodeFailed - 尺寸无效、像素数超上限、脚本非法

    encode_pgm_auto

    fn encode_pgm_auto(img :
    core
    /types.Image) -> Bytes

    PGM 编码委托

    encode_png

    fn encode_png(img :
    core
    /types.Image) -> Bytes

    PNG 编码委托

    encode_pnm_auto

    fn encode_pnm_auto(img :
    core
    /types.Image) -> Bytes

    PNM 编码委托(channels<=1 → PGM else → PPM)

    encode_ppm_auto

    fn encode_ppm_auto(img :
    core
    /types.Image) -> Bytes

    PPM 编码委托

    encode_qoi

    fn encode_qoi(img :
    core
    /types.Image) -> Bytes raise
    core
    /types.LoadError

    QOI 编码委托 QOI 编码委托 QOI encoding delegation

    错误 Errors:
    • LoadError::EncodeFailed - channels 不为 3 或 4 Channels not 3 or 4

    encode_tga

    fn encode_tga(img :
    core
    /types.Image) -> Bytes

    TGA 编码委托

    encode_webp_lossless

    fn encode_webp_lossless(img :
    core
    /types.Image) -> Bytes raise
    core
    /types.LoadError

    WebP 无损编码委托(VP8L)

    下游之所以需要这个别名:save_image_auto(img, ImageFormat::Webp) 能用,但很多人会先 import walkzzz/image.codec/lib 写"只用 lib"的代码,再去文档里找 webp 编码函数—— 而真正的实现在 image.codec/codec。这里给一条 @lib 就能拿到的入口,省一次 import。

    format_supports_16bit

    fn format_supports_16bit(format :
    core
    /types.ImageFormat) -> Bool

    查询格式是否支持 16-bit 加载,PNG/PNM → true,其他 → false

    format_supports_float

    fn format_supports_float(format :
    core
    /types.ImageFormat) -> Bool

    查询格式是否支持 Float 加载/保存,HDR → true,其他 → false

    get_image_info

    fn get_image_info(data : Bytes) -> UnifiedImageInfo raise
    core
    /types.LoadError

    统一图像信息探测 自动检测格式并返回跨格式通用信息(尺寸/通道/位深/alpha) 未知格式 raise LoadError::UnsupportedFormat

    get_image_summary

    fn get_image_summary(data : Bytes) -> String raise
    core
    /types.LoadError

    人类可读的图像信息摘要 返回格式如 "PNG 4x4 3ch 8bit"

    has_alpha

    fn has_alpha(img :
    core
    /types.Image) -> Bool

    图像是否带"真的透明"(4 通道且存在 alpha < 255 的像素)。 3 通道一律视为不透明;1 通道视为不透明。

    info_dims

    fn info_dims(info :
    codec
    /codec.ImageInfoExtended) -> (Int64, Int64)

    从 ImageInfoExtended 里取出宽高(未知/不支持返回 None)。 Extract width/height from extended info (None when unknown).

    jpeg_progressive_script_eight

    fn jpeg_progressive_script_eight() -> Array[
    codec
    /codec.JpegProgScan]

    0.1.30 固定 8 段扫描脚本(DC 首扫 Al=1 → 3×AC 首扫 → DC 细化 → 3×AC 细化)。

    jpeg_progressive_script_libjpeg

    fn jpeg_progressive_script_libjpeg() -> Array[
    codec
    /codec.JpegProgScan]

    libjpeg 默认(10 段)progressive 扫描脚本。

    load_16_from_bytes_auto

    fn load_16_from_bytes_auto(data : Bytes, req_channels? : Int?) ->
    core
    /types.Image16 raise
    core
    /types.LoadError

    16-bit 自动格式分派解码(纯 MoonBit,全目标可用) 根据格式委托对应 16-bit 解码器,输出 Image16(little-endian uint16)
    • PNG: 支持 8-bit 上采样和原生 16-bit
    • PNM: 支持 8-bit 上采样和原生 16-bit
    • 其他格式不支持 16-bit 注意:req_channels 仅对 PNG 生效,PNM 分支不传递 req_channels

    load_any_f

    fn load_any_f(data : Bytes) ->
    core
    /types.ImageF raise
    core
    /types.LoadError

    参数 Parameters:
    • data - 字节流 Byte stream

    返回 Return:
    • ImageF(HDR 解码结果)ImageF (HDR decode result)

    错误 Errors:
    • LoadError::DecodeFailed - 非 HDR 格式、HDR 头无效 Non-HDR format, invalid HDR header @see load_any 基础版本,本函数为 ImageF 变体

    load_from_bytes_auto

    fn load_from_bytes_auto(data : Bytes, req_channels? : Int?) ->
    core
    /types.Image raise
    core
    /types.LoadError

    test "doc_load_from_bytes_auto_bmp" {
    try {
    let img = @lib.load_from_bytes_auto(@types.DOC_TEST_BMP_1X1)
    assert_eq(img.width, 1)
    assert_eq(img.channels, 3)
    } catch {
    _ => assert_true(false)
    }
    }

    save_any_f

    fn save_any_f(img :
    core
    /types.ImageF, format :
    core
    /types.ImageFormat) -> Bytes raise
    core
    /types.LoadError

    Float 自动格式分派保存,HDR → encode_hdr_pure,其他格式不支持 Float 保存 raise @see save_any 基础版本,本函数为 ImageF 变体

    save_image_auto

    fn save_image_auto(img :
    core
    /types.Image, format :
    core
    /types.ImageFormat) -> Bytes raise
    core
    /types.LoadError

    统一保存 API:按指定格式自动分派编码
    • 支持:PNG/JPEG/BMP/QOI/PNM/GIF/TGA/TIFF/ICO/CUR/ICNS/WebP
    • 不支持:HDR(需 ImageF,用 encode_hdr)、APNG(需 PngAnimation)、AVIF/HEIF/JPEG-XL/PSD(无编码器)

    save_image_with_quality

    fn save_image_with_quality(img :
    core
    /types.Image, format :
    core
    /types.ImageFormat, quality : Int) -> Bytes raise
    core
    /types.LoadError

    带质量参数的统一保存 API
    • WebP: 走 VP8L 无损编码,quality 参数被忽略(无损)
    • JPEG: quality 1–100 生效(50 = 标准量化表)
    • 其他格式: quality 参数被忽略,使用默认质量
    • 不支持的格式 raise