#walkzzz/image.mbt/pure/codec

    纯 MoonBit 图像编解码(18 种格式:PNG/JPEG/BMP/GIF/QOI/TGA/PSD/HDR/PNM/TIFF/ICO/CUR/ICNS/APNG/WebP/AVIF/HEIF/JPEG-XL)

    #概览

    项目数量
    公开函数54
    公开类型4
    常量0
    错误类型0

    #类型

    pub struct AvifInfo {
    pub struct HeifInfo {
    pub struct JpegXlInfo {
    pub struct Vp8ReferenceFrames {

    #API

    pub fn avif_decode(Bytes) -> @types.Image raise @types.LoadError
    pub fn avif_info(Bytes) -> AvifInfo raise @types.LoadError
    pub fn decode_apng_pure(Bytes) -> @types.PngAnimation raise @types.LoadError
    pub fn decode_bmp_pure(Bytes) -> @types.Image raise @types.LoadError
    pub fn decode_cur_pure(Bytes) -> @types.Image raise @types.LoadError
    pub fn decode_gif_animation_pure(Bytes) -> @types.GifAnimation raise @types.LoadError
    pub fn decode_gif_pure(Bytes) -> @types.Image raise @types.LoadError
    pub fn decode_hdr_pure(Bytes) -> @types.ImageF raise @types.LoadError
    pub fn decode_icns_pure(Bytes) -> @types.Image raise @types.LoadError
    pub fn decode_ico_pure(Bytes) -> @types.Image raise @types.LoadError
    pub fn decode_jpeg_pure(Bytes) -> @types.Image raise @types.LoadError
    pub fn decode_png_16_pure(Bytes, req_channels? : Int?) -> @types.Image16 raise @types.LoadError
    pub fn decode_png_pure(Bytes, req_channels? : Int?) -> @types.Image raise @types.LoadError
    pub fn decode_pnm_16_pure(Bytes) -> @types.Image16 raise @types.LoadError
    pub fn decode_pnm_pure(Bytes) -> @types.Image raise @types.LoadError
    pub fn decode_psd_pure(Bytes) -> @types.Image raise @types.LoadError
    pub fn decode_qoi_pure(Bytes) -> @types.Image raise @types.LoadError
    pub fn decode_tga_pure(Bytes) -> @types.Image raise @types.LoadError
    pub fn decode_tiff_pure(Bytes) -> @types.Image raise @types.LoadError
    pub fn decode_vp8_frame(Bytes, Int) -> @types.Image raise @types.LoadError
    pub fn decode_vp8_inter_frame(Bytes, Int, Vp8ReferenceFrames) -> (@types.Image, Vp8ReferenceFrames) raise @types.LoadError
    pub fn decode_webp_pure(Bytes) -> @types.Image raise @types.LoadError
    pub fn encode_apng_pure(@types.PngAnimation) -> Bytes raise @types.LoadError
    pub fn encode_bmp_pure(@types.Image) -> Bytes
    pub fn encode_cur_pure(@types.Image) -> Bytes
    pub fn encode_cur_with_hotspot_pure(@types.Image, Int, Int) -> Bytes
    pub fn encode_gif_animation_pure(@types.GifAnimation) -> Bytes raise @types.LoadError
    pub fn encode_gif_pure(@types.Image) -> Bytes raise @types.LoadError
    pub fn encode_hdr_pure(@types.ImageF) -> Bytes raise @types.LoadError
    pub fn encode_icns_pure(@types.Image) -> Bytes
    pub fn encode_ico_pure(@types.Image) -> Bytes
    pub fn encode_ico_with_sizes_pure(Array[@types.Image]) -> Bytes
    pub fn encode_jpeg_pure(@types.Image) -> Bytes raise @types.LoadError
    pub fn encode_pgm_pure(@types.Image) -> Bytes
    pub fn encode_png_pure(@types.Image) -> Bytes
    pub fn encode_pnm_pure(@types.Image) -> Bytes
    pub fn encode_ppm_pure(@types.Image) -> Bytes
    pub fn encode_qoi_pure(@types.Image) -> Bytes raise @types.LoadError
    pub fn encode_tga_pure(@types.Image) -> Bytes
    pub fn encode_tiff_pure(@types.Image) -> Bytes raise @types.LoadError
    pub fn encode_webp_lossy_full(@types.Image, quality? : Int) -> Bytes raise @types.LoadError
    pub fn encode_webp_lossy_pure(@types.Image, quality? : Int) -> Bytes raise @types.LoadError
    pub fn heif_decode(Bytes) -> @types.Image raise @types.LoadError
    pub fn heif_info(Bytes) -> HeifInfo raise @types.LoadError
    pub fn is_avif_format(Bytes) -> Bool
    pub fn is_heif_format(Bytes) -> Bool
    pub fn is_jpegxl_codestream(Bytes) -> Bool
    pub fn is_jpegxl_container(Bytes) -> Bool
    pub fn is_jpegxl_format(Bytes) -> Bool
    pub fn jpegxl_decode(Bytes) -> @types.Image raise @types.LoadError
    pub fn jpegxl_info(Bytes) -> JpegXlInfo raise @types.LoadError
    pub fn tiff_deflate_decode(Bytes) -> Array[Byte] raise @types.LoadError
    pub fn tiff_lzw_decode(Bytes) -> Array[Byte] raise @types.LoadError
    pub fn tiff_packbits_decode(Bytes) -> Array[Byte] raise @types.LoadError

    #纯 MoonBit 实现

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

    Codec

    pub trait Codec {
    fn format_name(Self) -> String
    fn can_encode(Self) -> Bool
    fn can_decode(Self) -> Bool
    fn is_lossless(Self) -> Bool
    fn encode(Self,
    core
    /types.AnyImage) -> Bytes raise
    core
    /types.LoadError
    fn decode(Self, Bytes) ->
    core
    /types.AnyImage raise
    core
    /types.LoadError
    }

    编解码器统一接口 trait

    为 ImageFormat 枚举实现,提供统一的 encode / decode 方法

    ApngInfoExtended

    pub struct ApngInfoExtended {
    width : Int
    height : Int
    bit_depth : Int
    color_type : Int
    color_type_name : String
    num_channels : Int
    has_alpha : Bool
    num_frames : Int
    num_plays : Int
    is_animated : Bool
    }

    APNG 扩展信息(包含帧数、播放次数、动画标志等)

    AvifInfo

    pub struct AvifInfo {
    width : Int
    height : Int
    bit_depth : Int
    num_channels : Int
    has_alpha : Bool
    }

    AVIF 图像信息

    AvifInfoExtended

    pub struct AvifInfoExtended {
    width : Int
    height : Int
    bit_depth : Int
    num_channels : Int
    has_alpha : Bool
    rotation : Int
    mirror : Int
    color_type : String
    color_primaries : Int
    transfer_characteristics : Int
    matrix_coefficients : Int
    full_range : Bool
    av1_profile : Int
    av1_level : Int
    av1_tier : Int
    av1_monochrome : Bool
    }

    AVIF 扩展信息(包含色彩空间、旋转、编码配置等)

    BmpInfoExtended

    pub struct BmpInfoExtended {
    width : Int
    height : Int
    bit_depth : Int
    num_channels : Int
    has_alpha : Bool
    compression : Int
    compression_name : String
    is_bottom_up : Bool
    dib_size : Int
    num_colors : Int
    data_offset : Int
    }

    BMP 扩展信息(包含位深、压缩方式、行序、调色板等)

    CurInfoExtended

    pub struct CurInfoExtended {
    icon_type : Int
    num_images : Int
    widths : Array[Int]
    heights : Array[Int]
    hotspot_xs : Array[Int]
    hotspot_ys : Array[Int]
    width : Int
    height : Int
    num_channels : Int
    has_alpha : Bool
    }

    CUR 扩展信息(包含图像数量、热点坐标等)

    GifInfoExtended

    pub struct GifInfoExtended {
    width : Int
    height : Int
    version : String
    has_global_color_table : Bool
    global_color_table_size : Int
    color_resolution : Int
    is_sorted : Bool
    background_color_index : Int
    pixel_aspect_ratio : Int
    num_channels : Int
    has_alpha : Bool
    }

    GIF 扩展信息(包含版本、全局颜色表、动画标志等)

    HdrInfoExtended

    pub struct HdrInfoExtended {
    width : Int
    height : Int
    num_channels : Int
    has_alpha : Bool
    format : String
    }

    HDR 扩展信息(包含尺寸、通道数等)

    HeifInfo

    pub struct HeifInfo {
    width : Int
    height : Int
    bit_depth : Int
    num_channels : Int
    has_alpha : Bool
    }

    HEIF 图像信息

    HeifInfoExtended

    pub struct HeifInfoExtended {
    width : Int
    height : Int
    bit_depth : Int
    num_channels : Int
    has_alpha : Bool
    rotation : Int
    mirror : Int
    color_type : String
    color_primaries : Int
    transfer_characteristics : Int
    matrix_coefficients : Int
    full_range : Bool
    hevc_profile : Int
    hevc_level : Int
    hevc_tier : Int
    hevc_chroma_format : Int
    hevc_bit_depth_luma : Int
    hevc_bit_depth_chroma : Int
    }

    HEIF 扩展信息(包含色彩空间、旋转、HEVC 编码配置等)

    IcnsInfoExtended

    pub struct IcnsInfoExtended {
    file_size : Int
    num_icons : Int
    icon_types : Array[String]
    icon_sizes : Array[Int]
    is_png_list : Array[Bool]
    width : Int
    height : Int
    num_channels : Int
    has_alpha : Bool
    }

    ICNS 扩展信息(包含图标数量、各图标类型/尺寸等)

    IcoInfoExtended

    pub struct IcoInfoExtended {
    icon_type : Int
    icon_type_name : String
    num_images : Int
    widths : Array[Int]
    heights : Array[Int]
    bit_depths : Array[Int]
    width : Int
    height : Int
    num_channels : Int
    has_alpha : Bool
    }

    ICO 扩展信息(包含图像数量、各子图像尺寸/位深等)

    ImageInfoExtended

    统一图像扩展信息(自动格式检测后的联合类型)

    通过 get_image_info_extended 自动检测格式并返回对应类型的扩展信息 #non_exhaustive 表示未来可能添加新构造器

    JpegInfoExtended

    pub struct JpegInfoExtended {
    width : Int
    height : Int
    precision : Int
    num_components : Int
    is_grayscale : Bool
    is_progressive : Bool
    sof_marker : Int
    sof_name : String
    num_channels : Int
    has_alpha : Bool
    }

    JPEG 扩展信息(包含精度、分量数、渐进标志等)

    JpegProgScan

    pub(all) struct JpegProgScan {
    comps : Array[Int]
    ss : Int
    se : Int
    ah : Int
    al : Int
    }

    一条 progressive 扫描(字段与 libjpeg jpeg_scan_info 对齐)。 comps 用本库的分量序号:0 = Y、1 = Cb、2 = Cr。

    JpegXlInfo

    pub struct JpegXlInfo {
    width : Int
    height : Int
    bit_depth : Int
    num_channels : Int
    has_alpha : Bool
    is_lossless : Bool
    }

    JPEG-XL 图像信息

    JpegXlInfoExtended

    pub struct JpegXlInfoExtended {
    width : Int
    height : Int
    bit_depth : Int
    num_channels : Int
    has_alpha : Bool
    is_lossless : Bool
    is_codestream : Bool
    orientation : Int
    }

    JPEG-XL 扩展信息

    PngInfoExtended

    pub struct PngInfoExtended {
    width : Int
    height : Int
    bit_depth : Int
    color_type : Int
    color_type_name : String
    num_channels : Int
    has_alpha : Bool
    is_palette : Bool
    compression_method : Int
    filter_method : Int
    interlace_method : Int
    }

    PNG 扩展信息(包含位深、颜色类型、隔行扫描等)

    PnmInfoExtended

    pub struct PnmInfoExtended {
    width : Int
    height : Int
    format_type : String
    maxval : Int
    bit_depth : Int
    num_channels : Int
    has_alpha : Bool
    is_binary : Bool
    }

    PNM 扩展信息(包含格式类型、maxval、位深等)

    PsdInfoExtended

    pub struct PsdInfoExtended {
    width : Int
    height : Int
    bit_depth : Int
    channel_count : Int
    num_channels : Int
    has_alpha : Bool
    color_mode : Int
    color_mode_name : String
    compression : Int
    compression_name : String
    version : Int
    }

    PSD 扩展信息(包含通道数、位深、颜色模式、压缩方式等)

    QoiInfoExtended

    pub struct QoiInfoExtended {
    width : Int
    height : Int
    channels : Int
    colorspace : Int
    colorspace_name : String
    num_channels : Int
    has_alpha : Bool
    }

    QOI 扩展信息(包含通道数、色彩空间等)

    TgaInfoExtended

    pub struct TgaInfoExtended {
    width : Int
    height : Int
    image_type : Int
    image_type_name : String
    bit_depth : Int
    num_channels : Int
    has_alpha : Bool
    is_bottom_up : Bool
    id_length : Int
    color_map_type : Int
    x_origin : Int
    y_origin : Int
    }

    TGA 扩展信息(包含图像类型、位深、行序等)

    TiffInfoExtended

    pub struct TiffInfoExtended {
    width : Int
    height : Int
    bit_depth : Int
    samples_per_pixel : Int
    num_channels : Int
    has_alpha : Bool
    compression : Int
    compression_name : String
    photometric : Int
    photometric_name : String
    orientation : Int
    resolution_unit : Int
    is_little_endian : Bool
    }

    TIFF 扩展信息(包含压缩方式、光度解释、方向、分辨率等)

    Vp8ReferenceFrames

    pub struct Vp8ReferenceFrames {
    last_y : Array[Int]
    last_u : Array[Int]
    last_v : Array[Int]
    golden_y : Array[Int]
    golden_u : Array[Int]
    golden_v : Array[Int]
    altref_y : Array[Int]
    altref_u : Array[Int]
    altref_v : Array[Int]
    width : Int
    height : Int
    }

    参考帧管理 VP8 使用 3 个参考帧:last(前向)、golden(黄金帧)、altref(备用参考帧)

    WebpInfoExtended

    pub struct WebpInfoExtended {
    width : Int
    height : Int
    format : String
    has_alpha : Bool
    has_animation : Bool
    has_icc_profile : Bool
    has_exif : Bool
    has_xmp : Bool
    num_channels : Int
    }

    WebP 扩展信息(包含格式类型、alpha、动画、元数据标志等)

    apng_info_extended

    fn apng_info_extended(data : Bytes) -> ApngInfoExtended raise
    core
    /types.LoadError

    解析 APNG 扩展头部信息

    遍历 PNG chunks,提取 IHDR(尺寸/位深/颜色类型) + acTL(帧数/播放次数)

    avif_decode

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

    解码 AVIF 图像

    注意:完整 AVIF 解码需要 AV1 解码器,当前返回 UnsupportedFormat。 使用 avif_info 获取元数据。

    avif_info

    fn avif_info(data : Bytes) -> AvifInfo raise
    core
    /types.LoadError

    解析 AVIF 头部信息

    解析 ISOBMFF box 结构,提取图像尺寸和位深信息。 注意:完整解析需要遍历所有 box,此处仅解析基本元数据。

    avif_info_extended

    fn avif_info_extended(data : Bytes) -> AvifInfoExtended raise
    core
    /types.LoadError

    解析 AVIF 扩展头部信息

    递归遍历 ISOBMFF box 结构,提取色彩空间、旋转、镜像、AV1 编码配置等

    bmp_info_extended

    fn bmp_info_extended(data : Bytes) -> BmpInfoExtended raise
    core
    /types.LoadError

    解析 BMP 扩展头部信息

    codec_decode_auto

    fn codec_decode_auto(data : Bytes) ->
    core
    /types.AnyImage raise
    core
    /types.LoadError

    通过 Codec trait 统一解码:自动检测格式并解码

    codec_detect_format

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

    检测图像格式(基于 magic bytes)

    cur_info_extended

    fn cur_info_extended(data : Bytes) -> CurInfoExtended raise
    core
    /types.LoadError

    解析 CUR 扩展头部信息

    decode_apng_pure

    fn decode_apng_pure(data : Bytes) ->
    core
    /types.PngAnimation raise
    core
    /types.LoadError

    解码 APNG 字节流为 PngAnimation 如果不是 APNG(无 acTL chunk),则当作单帧 PNG 返回

    decode_bmp_pure

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

    参数 Parameters:
    • data - BMP 字节流 BMP byte stream

    返回 Return:
    • 解码后的 8-bit Image Decoded 8-bit Image

    错误 Errors:
    • LoadError::DecodeFailed - 数据过短、magic 不匹配、不支持的位深/压缩/尺寸、像素越界 Data too short, magic mismatch, unsupported bit depth/compression/dimensions, pixel out of bounds

    test "doc_decode_bmp_pure" {
    try {
    let img = @codec.decode_bmp_pure(@types.DOC_TEST_BMP_1X1)
    assert_eq(img.width, 1)
    assert_eq(img.height, 1)
    assert_eq(img.channels, 3)
    } catch {
    _ => assert_true(false)
    }
    }

    decode_bmp_region

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

    BMP 区域解码。仅支持 BITMAPINFOHEADER + BI_RGB 的 24/32-bit(与 decode_bmp_pure 相同)。 无压缩格式可按行直接寻址,不分配整图缓冲。

    decode_cur_pure

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

    解码 CUR 字节流为 Image(选择面积最大的子图像) Decode CUR byte stream to Image (selects largest sub-image by area)

    参数 Parameters:
    • data - CUR 字节流 CUR byte stream

    返回 Return:
    • 解码后的 8-bit Image Decoded 8-bit Image

    错误 Errors:
    • LoadError::DecodeFailed - 数据过短、CUR 签名无效、无可用子图像 Data too short, invalid CUR signature, no usable sub-image

    decode_gif_animation_pure

    fn decode_gif_animation_pure(data : Bytes) ->
    core
    /types.GifAnimation raise
    core
    /types.LoadError

    解码 GIF 动画为 GifAnimation(纯 MoonBit,全目标可用)。 支持多帧动画、延迟时间、基本 disposal 方法。 不支持 interlace。

    decode_gif_pure

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

    参数 Parameters:
    • data - GIF 字节流 GIF byte stream

    返回 Return:
    • 解码后的 8-bit Image Decoded 8-bit Image

    错误 Errors:
    • LoadError::DecodeFailed - 数据过短、GIF 签名无效、interlace 不支持、LZW 解码失败 Data too short, invalid GIF signature, interlace not supported, LZW decode failure

    decode_hdr_pure

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

    参数 Parameters:
    • data - HDR 字节流 HDR byte stream

    返回 Return:
    • 解码后的 ImageF Decoded ImageF

    错误 Errors:
    • LoadError::DecodeFailed - RADIANCE 头无效、尺寸无效、数据过短 Invalid RADIANCE header, invalid dimensions, data too short

    decode_icns_pure

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

    参数 Parameters:
    • data - ICNS 字节流 ICNS byte stream

    返回 Return:
    • 解码后的 8-bit Image Decoded 8-bit Image

    错误 Errors:
    • LoadError::DecodeFailed - 数据过短、icns 签名无效、无可用图标 Data too short, invalid icns signature, no usable icon

    decode_ico_pure

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

    参数 Parameters:
    • data - ICO 字节流 ICO byte stream

    返回 Return:
    • 解码后的 8-bit Image Decoded 8-bit Image

    错误 Errors:
    • LoadError::DecodeFailed - 数据过短、ICO 签名无效、无可用子图像 Data too short, invalid ICO signature, no usable sub-image

    decode_jpeg_pure

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

    参数 Parameters:
    • data - JPEG 字节流 JPEG byte stream

    返回 Return:
    • 解码后的 8-bit Image Decoded 8-bit Image

    错误 Errors:
    • LoadError::DecodeFailed - 数据过短、SOI 标记缺失、不支持的颜色空间、Huffman 解码失败 Data too short, missing SOI marker, unsupported color space, Huffman decode failure

    decode_png_16_pure

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

    PNG 16-bit 解码(纯 MoonBit,全目标可用)。 支持 bit depth 8(上采样为 16-bit)和 16(原生)。 color type 0/2/3/4/6,不支持 interlace。 req_channels: 0/None=原色, 1=灰度, 2=灰度+alpha, 3=RGB, 4=RGBA

    decode_png_pure

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

    test "doc_decode_png_pure" {
    try {
    let img = @codec.decode_png_pure(@types.DOC_TEST_PNG_1X1_RGBA)
    assert_eq(img.width, 1)
    assert_eq(img.height, 1)
    assert_eq(img.channels, 4)
    } catch {
    _ => assert_true(false)
    }
    }

    decode_png_region

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

    PNG 区域解码。输出通道数与 decode_png_pure 默认一致(gray=1、RGB=3、GA=2、RGBA=4、palette=1)。 仅支持 8-bit、非 interlace(与 decode_png_pure 相同)。

    decode_pnm_16_pure

    fn decode_pnm_16_pure(data : Bytes) ->
    core
    /types.Image16 raise
    core
    /types.LoadError

    解码 PNM 字节流为 Image16 支持 P5(PGM 16-bit 灰度,channels=1)和 P6(PPM 16-bit RGB,channels=3) maxval ≥ 256 时为 16-bit(big-endian),maxval < 256 时上采样为 16-bit

    decode_pnm_pure

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

    解码 PNM 字节流为 Image 支持 P5(PGM 二进制灰度,channels=1)和 P6(PPM 二进制 RGB,channels=3) 仅支持 8-bit(maxval < 256),不支持 16-bit(maxval ≥ 256) Header: magic(2 字节) + whitespace + width(ASCII) + whitespace + height(ASCII) + whitespace + maxval(ASCII) + 单 whitespace + 像素 注释行(# 开头至行尾)可出现在 magic 后 header 任意位置

    参数 Parameters:
    • data - PNM 字节流 PNM byte stream

    返回 Return:
    • 解码后的 8-bit Image Decoded 8-bit Image

    错误 Errors:
    • LoadError::DecodeFailed - 数据过短、magic 不匹配、尺寸无效、maxval 不支持 Data too short, magic mismatch, invalid dimensions, unsupported maxval

    test "doc_decode_pnm_pure" {
    try {
    let img = @codec.decode_pnm_pure(@types.DOC_TEST_PNM_1X1)
    assert_eq(img.width, 1)
    assert_eq(img.height, 1)
    assert_eq(img.channels, 3)
    } catch {
    _ => assert_true(false)
    }
    }

    decode_psd_pure

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

    参数 Parameters:
    • data - PSD 字节流 PSD byte stream

    返回 Return:
    • 解码后的 8-bit Image Decoded 8-bit Image

    错误 Errors:
    • LoadError::DecodeFailed - 数据过短、8BPS 签名无效、不支持的颜色模式/压缩 Data too short, invalid 8BPS signature, unsupported color mode/compression

    decode_qoi_pure

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

    参数 Parameters:
    • data - QOI 字节流 QOI byte stream

    返回 Return:
    • 解码后的 8-bit Image Decoded 8-bit Image

    错误 Errors:
    • LoadError::DecodeFailed - 数据过短、magic 不匹配、尺寸无效 Data too short, magic mismatch, invalid dimensions

    test "doc_decode_qoi_pure" {
    try {
    let img = @codec.decode_qoi_pure(@types.DOC_TEST_QOI_1X1)
    assert_eq(img.width, 1)
    assert_eq(img.height, 1)
    assert_eq(img.channels, 4)
    } catch {
    _ => assert_true(false)
    }
    }

    decode_qoi_region

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

    QOI 区域解码。QOI 为顺序码流,仍需从头解码;但输出缓冲只分配 tile, 区域外像素只推进状态、不落内存。

    decode_tga_pure

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

    参数 Parameters:
    • data - TGA 字节流 TGA byte stream

    返回 Return:
    • 解码后的 8-bit Image Decoded 8-bit Image

    错误 Errors:
    • LoadError::DecodeFailed - 数据过短、不支持的类型/位深、尺寸无效、像素越界 Data too short, unsupported type/bpp, invalid dimensions, pixel out of bounds

    test "doc_decode_tga_pure" {
    try {
    let data = Bytes::from_array([b'\xff', b'\x00', b'\x00'])
    let img = @types.Image::{ width: 1, height: 1, channels: 3, data, }
    let tga_bytes = @codec.encode_tga_pure(img)
    let decoded = @codec.decode_tga_pure(tga_bytes)
    assert_eq(decoded.width, 1)
    assert_eq(decoded.height, 1)
    assert_eq(decoded.channels, 3)
    } catch {
    _ => assert_true(false)
    }
    }

    decode_tiff_pure

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

    参数 Parameters:
    • data - TIFF 字节流 TIFF byte stream

    返回 Return:
    • 解码后的 8-bit Image Decoded 8-bit Image

    错误 Errors:
    • LoadError::DecodeFailed - 数据过短、TIFF 签名无效、不支持的压缩/格式 Data too short, invalid TIFF signature, unsupported compression/format

    decode_vp8_frame

    fn decode_vp8_frame(data : Bytes, offset : Int) ->
    core
    /types.Image raise
    core
    /types.LoadError

    解码 VP8 lossy 帧

    decode_vp8_inter_frame

    fn decode_vp8_inter_frame(data : Bytes, offset : Int, refs : Vp8ReferenceFrames) -> (
    core
    /types.Image, Vp8ReferenceFrames) raise
    core
    /types.LoadError

    解码 VP8 非关键帧(支持帧间预测) refs:参考帧管理(包含 last/golden/altref) 返回解码后的图像和更新后的参考帧

    decode_webp_pure

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

    WebP 解码入口:统一走 RFC 合规的 VP8L 路径(palette / predictor / cross-color / subtract-green 全部支持)。

    decode_webp_vp8l_rfc

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

    RFC 合规的 VP8L 解码:图像流(literal / LZ77 / 色彩缓存 / meta 多组)
    • 四类逆变换(predictor / cross-color / subtract-green / color indexing)。

    返回 channels = 4(本实现始终输出 RGBA)。

    detect_image_format

    fn detect_image_format(data : Bytes) -> String

    检测图像格式名称

    检测 18 种格式,返回格式名称字符串

    encode_apng_pure

    fn encode_apng_pure(anim :
    core
    /types.PngAnimation) -> Bytes raise
    core
    /types.LoadError

    将 PngAnimation 编码为 APNG 字节流 所有帧必须尺寸相同

    encode_bmp_pure

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

    BMP 编码(纯 MoonBit,全目标可用)。

    encode_cur_pure

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

    将单个 Image 编码为 CUR 字节流(内嵌 32-bit BMP 子图像,热点默认为中心)

    encode_cur_with_hotspot_pure

    fn encode_cur_with_hotspot_pure(img :
    core
    /types.Image, hotspot_x : Int, hotspot_y : Int) -> Bytes

    将单个 Image 编码为 CUR 字节流,指定热点坐标

    encode_gif_animation_pure

    fn encode_gif_animation_pure(anim :
    core
    /types.GifAnimation) -> Bytes raise
    core
    /types.LoadError

    将多帧 Image 编码为动画 GIF89a 字节流。 frames 为帧数组,delays 为每帧延迟(毫秒,将转换为 1/100 秒单位)。 所有帧应具有相同尺寸和 channels(3 或 4)。 使用 3-3-2 均匀量化 + LZW 压缩,无限循环。

    encode_gif_pure

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

    参数 Parameters:
    • img - 输入图像 Input image

    返回 Return:
    • GIF89a 字节流 GIF89a byte stream

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

    test "doc_encode_gif_pure" {
    try {
    let data = Bytes::from_array([b'\xff', b'\x00', b'\x00'])
    let img = @types.Image::{ width: 1, height: 1, channels: 3, data, }
    let encoded = @codec.encode_gif_pure(img)
    assert_true(encoded.length() > 0)
    } catch {
    _ => assert_true(false)
    }
    }

    encode_hdr_pure

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

    参数 Parameters:
    • img - 输入 ImageF Input ImageF

    返回 Return:
    • HDR 字节流 HDR byte stream

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

    encode_icns_pure

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

    将单个 Image 编码为 ICNS 字节流(内嵌 PNG)

    encode_ico_pure

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

    将单个 Image 编码为 ICO 字节流(内嵌 32-bit BMP 子图像)

    encode_ico_with_sizes_pure

    fn encode_ico_with_sizes_pure(images : Array[
    core
    /types.Image]) -> Bytes

    将多个尺寸的 Image 编码为多分辨率 ICO 字节流

    encode_jpeg_progressive

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

    JPEG 渐进式编码(纯 MoonBit,全目标可用)。4:4:4 YCbCr + 标准量化表;

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

    返回 Return:
    • progressive JPEG(SOF2)字节流

    错误 Errors:
    • LoadError::EncodeFailed - 尺寸无效、像素数超上限(PROGRESSIVE_MAX_PIXELS)

    encode_jpeg_progressive_420

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

    JPEG 渐进式编码 · 4:2:0(亮度全分辨、色度 2×2 盒式平均),8 条扫描。

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

    返回 Return:
    • progressive JPEG(SOF2,Y 2×2、Cb/Cr 1×1)字节流

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

    encode_jpeg_progressive_420_scripted

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

    4:2:0 渐进式编码 · 扫描脚本可配置。

    参数 Parameters:
    • img - 输入图像(1 或 3 通道)
    • quality - 质量 1–100
    • script - 扫描脚本(jpeg_progressive_script_libjpeg() 为 libjpeg 默认 10 段)

    返回 Return:
    • progressive JPEG(SOF2,Y 2×2、Cb/Cr 1×1)字节流

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

    encode_jpeg_pure

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

    参数 Parameters:
    • img - 输入图像 Input image

    返回 Return:
    • JPEG 字节流 JPEG byte stream

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

    encode_jpeg_with_quality

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

    JPEG 编码(可指定质量)。quality 1–100,50 等价标准量化表;4:4:4 YCbCr + 标准霍夫曼表。

    encode_pgm_pure

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

    编码为 PGM (P5 binary grayscale) 格式。 RGB 输入自动转为灰度(BT.601),RGBA 丢弃 alpha。

    encode_png_pure

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

    PNG 编码(纯 MoonBit,全目标可用)。 输入 Image(RGB),返回 PNG 格式字节流。 使用 filter type 0 (None) + zlib stored blocks(无压缩)。

    encode_pnm_pure

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

    自动选择 PNM 格式编码。 channels <= 1 → PGM (P5),否则 → PPM (P6)。

    encode_ppm_pure

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

    编码为 PPM (P6 binary RGB) 格式。 灰度输入自动转为 RGB,RGBA 输入丢弃 alpha。

    encode_qoi_pure

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

    参数 Parameters:
    • img - 输入图像 Input image

    返回 Return:
    • QOI 字节流 QOI byte stream

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

    test "doc_encode_qoi_pure" {
    try {
    let data = Bytes::from_array([b'\xff', b'\x00', b'\x00'])
    let img = @types.Image::{ width: 1, height: 1, channels: 3, data, }
    let encoded = @codec.encode_qoi_pure(img)
    assert_true(encoded.length() > 0)
    let decoded = @codec.decode_qoi_pure(encoded)
    assert_eq(decoded.width, 1)
    } catch {
    _ => assert_true(false)
    }
    }

    encode_tga_pure

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

    TGA 编码(纯 MoonBit,全目标可用)。

    位深由通道数决定:1/2 通道 → 24-bit(灰度复制到 RGB),3 通道 → 24-bit,4 通道 → 32-bit BGRA。 图像描述符按 TGA 规范写:bit5 = top-down origin;bit4(right-to-left)不置位; 低 4 位 = alpha 位深(32-bit 时 8,24-bit 时 0)。

    encode_tiff_pure

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

    参数 Parameters:
    • img - 输入图像 Input image

    返回 Return:
    • TIFF 字节流 TIFF byte stream

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

    encode_webp_lossless

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

    无损 WebP 编码单入口:自动在 palette / subtract-green / predictor 之间选体积最小者。

    体积选择靠"实际编码一遍比长度",不做估计 —— 编码成本换确定性。

    encode_webp_lossy_full

    #deprecated("产物不符合 VP8 规范,请改用 encode_webp_lossless")
    fn encode_webp_lossy_full(img :
    core
    /types.Image, quality? : Int) -> Bytes raise
    core
    /types.LoadError

    WebP lossy 编码(完整实现) 实现: VP8 key frame, I16x16 DC 预测, 4x4 DCT, 量化, 布尔算术编码 quality: 1-100 (越高越好质量,文件越大) ⚠️ 实测(2026-11-25):产物同样不符合 VP8 规范(ffmpeg 报 Invalid partitions)。 合法 WebP 编码请用 [encode_webp_lossless]。

    encode_webp_lossy_pure

    #deprecated("产物不符合 VP8 规范,请改用 encode_webp_lossless")
    fn encode_webp_lossy_pure(img :
    core
    /types.Image, quality? : Int) -> Bytes raise
    core
    /types.LoadError

    WebP lossy 编码(纯 MoonBit,全目标可用) 实现: VP8 key frame, DC-only 预测, 固定量化 quality: 1-100 (越高越好质量,文件越大) ⚠️ 实测(2026-11-25):产物不符合 VP8 规范。RIFF 容器正确,但缺少 first partition 中的分段/滤波/量化/概率更新与 token partition 尺寸表,first_part_size 被写成整个 token 流长度。ffmpeg 报 Invalid partitions,浏览器同样无法解码。 本函数仅为占位实现,不得在任何能力宣称中使用;合法 VP8/VP8L 编码器列为后续工作。 本库解码器仅支持 lossless (VP8L),也无法解此函数的输出。 自 S02 M3 起改用 [encode_webp_lossless](VP8L,产物经 ffmpeg / libwebp 逐字节校验)。

    encode_webp_vp8l_literal

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

    M1 编码器:literal-only 的合法 VP8L 产物。

    参数:img 支持 channels = 3(RGB)或 4(RGBA);尺寸上限 16384。 返回:完整的 RIFF/WEBP 字节流(单 VP8L chunk)。

    注意:本实现体积约 3 字节/像素(劣于 PNG),LZ77 与变换完成后才会优于 PNG。 当前仅供内部验证与 M2/M3 基线,不作为对外 API 承诺。

    let bytes = @codec.encode_webp_vp8l_literal(img)

    encode_webp_vp8l_lz77

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

    M2 编码器:贪心 LZ77 + 频次驱动 Huffman(无变换)。

    变换版本见 encode_webp_lossless。

    get_image_info_extended

    fn get_image_info_extended(data : Bytes) -> ImageInfoExtended raise
    core
    /types.LoadError

    检测图像格式并返回对应的扩展信息

    按 magic number 顺序检测 18 种格式,返回对应类型的 ImageInfoExtended TGA 无明确 magic,放在最后通过头部字段有效性判断

    gif_info_extended

    fn gif_info_extended(data : Bytes) -> GifInfoExtended raise
    core
    /types.LoadError

    解析 GIF 扩展头部信息

    hdr_info_extended

    fn hdr_info_extended(data : Bytes) -> HdrInfoExtended raise
    core
    /types.LoadError

    解析 HDR 扩展头部信息

    heif_decode

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

    解码 HEIF 图像

    注意:完整 HEIF 解码需要 HEVC 解码器,当前返回 UnsupportedFormat。 使用 heif_info 获取元数据。

    heif_info

    fn heif_info(data : Bytes) -> HeifInfo raise
    core
    /types.LoadError

    解析 HEIF 头部信息

    解析 ISOBMFF box 结构,提取图像尺寸和位深信息。

    heif_info_extended

    fn heif_info_extended(data : Bytes) -> HeifInfoExtended raise
    core
    /types.LoadError

    解析 HEIF 扩展头部信息

    递归遍历 ISOBMFF box 结构,提取色彩空间、旋转、镜像、HEVC 编码配置等

    icns_info_extended

    fn icns_info_extended(data : Bytes) -> IcnsInfoExtended raise
    core
    /types.LoadError

    解析 ICNS 扩展头部信息

    ico_info_extended

    fn ico_info_extended(data : Bytes) -> IcoInfoExtended raise
    core
    /types.LoadError

    解析 ICO 扩展头部信息

    is_apng_format

    fn is_apng_format(data : Bytes) -> Bool

    检测数据是否为 APNG 格式(PNG signature + 含 acTL chunk)

    is_avif_format

    fn is_avif_format(data : Bytes) -> Bool

    检测数据是否为 AVIF 格式

    AVIF 文件以 ftyp box 开头,brand 为 "avif" 或 "avis"

    is_bmp_format

    fn is_bmp_format(data : Bytes) -> Bool

    检测数据是否为 BMP 格式

    is_cur_format

    fn is_cur_format(data : Bytes) -> Bool

    检测数据是否为 CUR 格式

    仅看前 4 字节会把 24/32-bit TGA 误判为 CUR(TGA 头前 4 字节恰好是 00 00 02 00)。这里同时校验图像条目数 ≥ 1 且目录表装得下。

    is_gif_format

    fn is_gif_format(data : Bytes) -> Bool

    检测数据是否为 GIF 格式

    is_hdr_format

    fn is_hdr_format(data : Bytes) -> Bool

    检测数据是否为 HDR (Radiance RGBE) 格式

    is_heif_format

    fn is_heif_format(data : Bytes) -> Bool

    检测数据是否为 HEIF 格式

    HEIF 文件以 ftyp box 开头,brand 为 "heic", "heix", "hevc", "hevm", "heim", "heis", "mif1" 等

    is_icns_format

    fn is_icns_format(data : Bytes) -> Bool

    检测数据是否为 ICNS 格式

    is_ico_format

    fn is_ico_format(data : Bytes) -> Bool

    检测数据是否为 ICO 格式

    与 CUR 同口径:除 reserved/type 外还校验条目数 ≥ 1 且目录表装得下, 避免其他格式(如 TGA 头)撞上前 4 字节。

    is_jpeg_format

    fn is_jpeg_format(data : Bytes) -> Bool

    检测数据是否为 JPEG 格式

    is_jpegxl_codestream

    fn is_jpegxl_codestream(data : Bytes) -> Bool

    检测 JPEG-XL 是否为裸码流格式

    is_jpegxl_container

    fn is_jpegxl_container(data : Bytes) -> Bool

    检测 JPEG-XL 是否为 ISOBMFF 容器格式

    is_jpegxl_format

    fn is_jpegxl_format(data : Bytes) -> Bool

    检测数据是否为 JPEG-XL 格式

    支持两种格式:裸码流 (0xFF 0x0A) 和 ISOBMFF 容器

    is_png_format

    fn is_png_format(data : Bytes) -> Bool

    检测数据是否为 PNG 格式

    is_pnm_format

    fn is_pnm_format(data : Bytes) -> Bool

    检测数据是否为 PNM 格式(P1-P6)

    is_psd_format

    fn is_psd_format(data : Bytes) -> Bool

    检测数据是否为 PSD 格式

    is_qoi_format

    fn is_qoi_format(data : Bytes) -> Bool

    检测数据是否为 QOI 格式

    is_tga_format

    fn is_tga_format(data : Bytes) -> Bool

    检测数据是否为 TGA 格式

    TGA 无明确 magic,通过头部字段有效性判断

    is_tiff_format

    fn is_tiff_format(data : Bytes) -> Bool

    检测数据是否为 TIFF 格式

    is_webp_format

    fn is_webp_format(data : Bytes) -> Bool

    检测数据是否为 WebP 格式

    jpeg_info_extended

    fn jpeg_info_extended(data : Bytes) -> JpegInfoExtended raise
    core
    /types.LoadError

    解析 JPEG 扩展头部信息

    遍历 JPEG markers,查找 SOF marker 提取尺寸、精度、分量数、渐进标志

    jpeg_progressive_script_eight

    fn jpeg_progressive_script_eight() -> Array[JpegProgScan]

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

    jpeg_progressive_script_libjpeg

    fn jpeg_progressive_script_libjpeg() -> Array[JpegProgScan]

    libjpeg jpeg_simple_progression() 的 YCbCr 默认 10 条扫描。

    jpegxl_decode

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

    解码 JPEG-XL 图像

    注意:完整 JPEG-XL 解码需要专用解码器,当前返回 UnsupportedFormat。 使用 jpegxl_info 获取元数据。

    jpegxl_info

    fn jpegxl_info(data : Bytes) -> JpegXlInfo raise
    core
    /types.LoadError

    解析 JPEG-XL 头部信息

    解析 JPEG-XL 码流头部,提取图像尺寸和位深信息。 注意:完整解析需要实现 JPEG-XL 熵解码和变换解析,此处仅提取基本元数据。

    jpegxl_info_extended

    fn jpegxl_info_extended(data : Bytes) -> JpegXlInfoExtended raise
    core
    /types.LoadError

    解析 JPEG-XL 扩展头部信息

    解析 SizeHeader 提取图像尺寸,支持裸码流和 ISOBMFF 容器两种格式

    png_info_extended

    fn png_info_extended(data : Bytes) -> PngInfoExtended raise
    core
    /types.LoadError

    解析 PNG 扩展头部信息

    解析 IHDR chunk,提取尺寸、位深、颜色类型、压缩/滤波/隔行方式

    pnm_info_extended

    fn pnm_info_extended(data : Bytes) -> PnmInfoExtended raise
    core
    /types.LoadError

    解析 PNM 扩展头部信息

    支持 P1(PBM)/P2(PGM ASCII)/P3(PPM ASCII)/P4(PBM binary)/P5(PGM binary)/P6(PPM binary)

    psd_info_extended

    fn psd_info_extended(data : Bytes) -> PsdInfoExtended raise
    core
    /types.LoadError

    解析 PSD 扩展头部信息

    解析 PSD 文件头,提取尺寸、通道数、位深、颜色模式、压缩方式等

    qoi_info_extended

    fn qoi_info_extended(data : Bytes) -> QoiInfoExtended raise
    core
    /types.LoadError

    解析 QOI 扩展头部信息

    tga_info_extended

    fn tga_info_extended(data : Bytes) -> TgaInfoExtended raise
    core
    /types.LoadError

    解析 TGA 扩展头部信息

    tiff_deflate_decode

    fn tiff_deflate_decode(data : Bytes) -> Array[Byte] raise
    core
    /types.LoadError

    解码 TIFF Deflate (ZIP) 压缩的字节流 支持标准 Huffman 和动态 Huffman 表

    tiff_info_extended

    fn tiff_info_extended(data : Bytes) -> TiffInfoExtended raise
    core
    /types.LoadError

    解析 TIFF 扩展头部信息

    遍历 IFD 标签,提取尺寸、位深、压缩方式、光度解释、方向、分辨率单位等

    tiff_lzw_decode

    fn tiff_lzw_decode(data : Bytes) -> Array[Byte] raise
    core
    /types.LoadError

    解码 TIFF LZW 压缩的字节流

    tiff_packbits_decode

    fn tiff_packbits_decode(data : Bytes) -> Array[Byte] raise
    core
    /types.LoadError

    解码 TIFF PackBits 压缩的字节流

    webp_info_extended

    fn webp_info_extended(data : Bytes) -> WebpInfoExtended raise
    core
    /types.LoadError

    解析 WebP 扩展头部信息

    遍历 RIFF chunk 结构,提取格式类型、尺寸、alpha、动画、ICC/EXIF/XMP 标志

    zlib_compress

    fn zlib_compress(data : Bytes) -> Bytes

    zlib 包装(RFC 1950):CMF/FLG + DEFLATE + Adler-32。