image.media

    MoonBit media scene entry: codec, metadata, transform, encryption, pipeline.

    Download zip
    Author
    Version
    0.1.31
    License
    MIT
    Last updated
    1 hour ago
    Downloads
    16

    #walkzzz/image.media

    媒体场景入口(S16):编解码、元数据、几何/色彩变换、加密与流水线,日常"读进来—改一改—写出去"所需的能力一次 import。

    moon add walkzzz/image.media

    import "walkzzz/image.media"
    // 之后直接写 @media.<符号>;符号清单见本包 pkg.generated.mbti

    #再导出了什么

    原子包提供
    walkzzz/image.codec/codec18 种格式的纯编解码实现
    walkzzz/image.codec/lib统一加载/保存入口、格式探测、内存预算
    walkzzz/image.codec/util字节/压缩等编解码公共工具
    walkzzz/image.codec/color色彩空间转换与深度转换
    walkzzz/image.process/transform缩放、裁切、旋转、翻转、拼接
    walkzzz/image.domain/encryption图像加密与解密(含隐写/分块方案)
    walkzzz/image.domain/pipeline处理流水线与批处理编排

    #约定

    • 本包只有 pub using 再导出,没有实现;moon.pkg 与 media_scene.mbt 由 scripts/gen_scenes.mjs 生成,请勿手改。
    • 归属真值在仓库的 docs/scenes.json。
    • #deprecated 符号不导出;WebP lossy 的两个旧入口因此不在入口面上,请用 encode_webp_lossless。

    #文档

    • 场景总览:https://github.com/walkzzz/image.mbt/blob/main/docs/scenes.md
    • 能力矩阵:https://github.com/walkzzz/image.mbt/blob/main/docs/capabilities.md
    • 完整签名以本包 pkg.generated.mbti 为准。

    AffineMatrix

    2×3 仿射变换矩阵 (a, b, c, d, e, f) 正向仿射变换 x' = ax + by + c 正向仿射变换 y' = dx + ey + f

    ApngInfoExtended

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

    ArnoldParams

    Arnold 置乱参数

    AvifInfo

    AVIF 图像信息

    AvifInfoExtended

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

    BatchConfig

    批处理配置

    BatchResult

    批处理结果

    BatchTask

    批处理任务

    BmpInfoExtended

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

    ChaoticParams

    混沌系统参数

    CliCommand

    CLI 命令 #non_exhaustive 表示未来可能添加新构造器

    CurInfoExtended

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

    EncodedImage

    一次候选编码的结果。

    EncryptionAlgorithm

    加密算法类型 #non_exhaustive 表示未来可能添加新构造器

    EncryptionConfig

    加密配置

    EncryptionQuality

    加密质量评估

    EncryptionResult

    加密结果

    GifInfoExtended

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

    HdrInfoExtended

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

    HeifInfo

    HEIF 图像信息

    HeifInfoExtended

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

    IcnsInfoExtended

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

    IcoInfoExtended

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

    ImageEncryptionQuality

    加密质量评估

    ImageInfoExtended

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

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

    JpegInfoExtended

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

    JpegProgScan

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

    JpegXlInfo

    JPEG-XL 图像信息

    JpegXlInfoExtended

    JPEG-XL 扩展信息

    Operation

    处理操作类型 #non_exhaustive 表示未来可能添加新构造器

    PerspectiveMatrix

    3×3 透视变换矩阵 (m00, m01, m02, m10, m11, m12, m20, m21, m22)

    Pipeline

    流水线(链式操作序列)

    PngInfoExtended

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

    PnmInfoExtended

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

    ProcessResult

    处理结果

    PsdInfoExtended

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

    QoiInfoExtended

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

    StreamInfo

    流式解码信息

    TgaInfoExtended

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

    TiffInfoExtended

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

    UnifiedImageInfo

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

    Vp8ReferenceFrames

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

    WatermarkExtractionResult

    水印提取结果

    WatermarkInfo

    水印信息

    WatermarkType

    水印类型 #non_exhaustive 表示未来可能添加新构造器

    WebpInfoExtended

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

    adjacent_correlation

    fn adjacent_correlation(img :
    core
    /types.Image, direction? : String) -> Double raise
    core
    /types.LoadError

    计算相邻像素相关性(加密后应接近 0)

    adler32

    fn adler32(data : Bytes) -> Int

    计算 Adler-32 校验和

    apng_info_extended

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

    解析 APNG 扩展头部信息

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

    arnold_scramble

    fn arnold_scramble(img :
    core
    /types.Image, params :
    domain
    /encryption.ArnoldParams) ->
    domain
    /encryption.EncryptionResult raise
    core
    /types.LoadError

    Arnold 置乱加密

    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) ->
    codec
    /codec.AvifInfo raise
    core
    /types.LoadError

    解析 AVIF 头部信息

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

    avif_info_extended

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

    解析 AVIF 扩展头部信息

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

    batch_encrypt

    fn batch_encrypt(images : Array[
    core
    /types.Image], key : Double) -> Array[
    core
    /types.Image] raise
    core
    /types.LoadError

    批量加密

    batch_execute

    fn batch_execute(tasks : Array[
    domain
    /pipeline.BatchTask]) ->
    domain
    /pipeline.BatchResult

    执行批处理(模拟)

    注意:实际文件 I/O 需要运行时支持,此处提供框架和结果统计。

    batch_summary

    fn batch_summary(result :
    domain
    /pipeline.BatchResult) -> String

    批处理结果摘要

    batch_task

    fn batch_task(input_path : String, output_path : String, pipeline :
    domain
    /pipeline.Pipeline) ->
    domain
    /pipeline.BatchTask

    创建批处理任务

    best_format

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

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

    bmp_info_extended

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

    解析 BMP 扩展头部信息

    build_gaussian_pyramid

    fn build_gaussian_pyramid(img :
    core
    /types.Image, levels : Int) -> Array[
    core
    /types.Image] raise
    core
    /types.LoadError

    构建高斯金字塔。返回各级图像(从原图到最小级),levels 为最大层数(0=自动到 1x1)。

    build_gaussian_pyramid_16

    fn build_gaussian_pyramid_16(img :
    core
    /types.Image16, levels : Int) -> Array[
    core
    /types.Image16] raise
    core
    /types.LoadError

    构建高斯金字塔 16-bit 版本。 @see build_gaussian_pyramid 8-bit 版本,本函数为 Image16 变体

    build_gaussian_pyramid_f

    fn build_gaussian_pyramid_f(img :
    core
    /types.ImageF, levels : Int) -> Array[
    core
    /types.ImageF] raise
    core
    /types.LoadError

    构建高斯金字塔 float 版本。 @see build_gaussian_pyramid 8-bit 版本,本函数为 ImageF 变体

    build_laplacian_pyramid

    fn build_laplacian_pyramid(img :
    core
    /types.Image, levels : Int) -> Array[
    core
    /types.Image] raise
    core
    /types.LoadError

    构建拉普拉斯金字塔。返回各级拉普拉斯图像(高斯金字塔相邻层之差)。 最后一层为高斯金字塔的最小级(不加差)。

    build_laplacian_pyramid_16

    fn build_laplacian_pyramid_16(img :
    core
    /types.Image16, levels : Int) -> Array[
    core
    /types.Image16] raise
    core
    /types.LoadError

    构建拉普拉斯金字塔 16-bit 版本。 @see build_laplacian_pyramid 8-bit 版本,本函数为 Image16 变体

    build_laplacian_pyramid_float

    fn build_laplacian_pyramid_float(img :
    core
    /types.ImageF, levels : Int) -> Array[
    core
    /types.ImageF] raise
    core
    /types.LoadError

    构建拉普拉斯金字塔 float 版本。

    calculate_correlation

    fn calculate_correlation(img1 :
    core
    /types.Image, img2 :
    core
    /types.Image) -> Double raise
    core
    /types.LoadError

    计算相邻像素相关性

    calculate_image_entropy

    fn calculate_image_entropy(img :
    core
    /types.Image) -> Double raise
    core
    /types.LoadError

    计算图像熵

    calculate_psnr

    fn calculate_psnr(original :
    core
    /types.Image, encrypted :
    core
    /types.Image) -> Double raise
    core
    /types.LoadError

    计算 PSNR(峰值信噪比)

    chaotic_encrypt

    fn chaotic_encrypt(img :
    core
    /types.Image, params :
    domain
    /encryption.ChaoticParams) ->
    domain
    /encryption.EncryptionResult raise
    core
    /types.LoadError

    混沌加密

    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.

    cli_help

    fn cli_help() -> String

    CLI 帮助文本

    cli_parse

    fn cli_parse(args : Array[String]) ->
    domain
    /pipeline.CliCommand

    CLI 参数解析

    解析命令行参数,返回对应的命令。 支持的命令格式: CLI 命令:convert / resize / rotate / batch / info / help
    • convert 转换格式
    • resize 缩放
    • rotate 旋转
    • batch <input_dir> <output_dir> <pipeline_config> 批处理
    • info 信息
    • help 帮助 解析 Parse command-line arguments, return corresponding command. 描述 Supported command formats: 支持命令:convert / resize / rotate / batch / info / help

    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)

    compute_energy

    fn compute_energy(img :
    core
    /types.Image) -> Array[Double]

    计算图像能量图(Sobel 梯度幅值 |Gx| + |Gy|)

    compute_energy_16

    fn compute_energy_16(img :
    core
    /types.Image16) -> Array[Double]

    计算图像能量图 16-bit 版本 @see compute_energy 8-bit 版本,本函数为 Image16 变体

    compute_energy_f

    fn compute_energy_f(img :
    core
    /types.ImageF) -> Array[Double]

    计算图像能量图 float 版本 @see compute_energy 8-bit 版本,本函数为 ImageF 变体

    convert_channels_pure

    fn convert_channels_pure(img :
    core
    /types.Image, req_channels : Int) ->
    core
    /types.Image raise
    core
    /types.LoadError

    按 stb_image 语义转换通道数(纯 MoonBit,全目标可用) Convert channels following stb_image semantics (pure MoonBit, all targets)
    • 1: 灰度(BT.601 亮度公式) Grayscale (BT.601 luma formula)
    • 2: 灰度 + alpha(alpha=255 若源无 alpha) Grayscale + alpha (alpha=255 if source has no alpha)
    • 3: RGB(丢弃 alpha,灰度展开为 R=G=B) RGB (discard alpha, grayscale expanded to R=G=B)
    • 4: RGBA(alpha=255 若源无 alpha,灰度展开为 R=G=B) RGBA (alpha=255 if source has no alpha, grayscale expanded to R=G=B)

    test "doc_convert_channels_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 rgba = @color.convert_channels_pure(img, 4)
    assert_eq(rgba.channels, 4)
    } catch {
    _ => assert_true(false)
    }
    }

    convert_iphone_png_to_rgb_pure

    fn convert_iphone_png_to_rgb_pure(flag : Bool) -> Unit

    设置是否将 iPhone PNG 转换为 RGB(纯 MoonBit,全目标可用)

    crc32

    fn crc32(data : Bytes) -> Int

    计算 CRC-32 校验和

    create_default_arnold_params

    fn create_default_arnold_params() ->
    domain
    /encryption.ArnoldParams

    创建默认 Arnold 参数

    create_default_chaotic_params

    fn create_default_chaotic_params() ->
    domain
    /encryption.ChaoticParams

    创建默认混沌参数

    crop

    fn crop(img :
    core
    /types.Image, x : Int, y : Int, w : Int, h : Int) ->
    core
    /types.Image raise
    core
    /types.LoadError

    裁剪 8-bit Image 的指定区域

    crop_16

    fn crop_16(img :
    core
    /types.Image16, x : Int, y : Int, w : Int, h : Int) ->
    core
    /types.Image16 raise
    core
    /types.LoadError

    裁剪 16-bit Image16 @see crop 8-bit 版本,本函数为 Image16 变体

    crop_f

    fn crop_f(img :
    core
    /types.ImageF, x : Int, y : Int, w : Int, h : Int) ->
    core
    /types.ImageF raise
    core
    /types.LoadError

    裁剪 float ImageF @see crop 8-bit 版本,本函数为 ImageF 变体

    crop_region

    fn crop_region(img :
    core
    /types.Image, region :
    core
    /types.Region) ->
    core
    /types.Image

    按 Region 裁剪,越界部分自动裁掉(不报错)。空区域返回 0×0 图。 内部走零复制的 ImageRegion 视图,只为目标像素分配一次内存。

    cur_info_extended

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

    解析 CUR 扩展头部信息

    decode_animation

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

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

    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_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) ->
    codec
    /lib.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) ->
    codec
    /lib.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) ->
    codec
    /lib.StreamInfo raise
    core
    /types.LoadError

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

    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 :
    codec
    /codec.Vp8ReferenceFrames) -> (
    core
    /types.Image,
    codec
    /codec.Vp8ReferenceFrames) raise
    core
    /types.LoadError

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

    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_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)。

    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 稳定)

    decrypt_image_logistic

    fn decrypt_image_logistic(img :
    core
    /types.Image, key? : Double, mu? : Double) ->
    core
    /types.Image raise
    core
    /types.LoadError

    基于 Logistic 混沌映射的图像解密(与加密相同,因为 XOR 是对称的)

    decrypt_image_xor

    fn decrypt_image_xor(img :
    core
    /types.Image, key : String) ->
    core
    /types.Image raise
    core
    /types.LoadError

    简单的 XOR 解密

    default_batch_config

    fn default_batch_config() ->
    domain
    /pipeline.BatchConfig

    默认批处理配置

    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 / 宽高)保守判定

    detect_image_format

    fn detect_image_format(data : Bytes) -> String

    检测图像格式名称

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

    draw_circle

    fn draw_circle(img :
    core
    /types.Image, cx : Int, cy : Int, r : Int, color : Array[Byte], thickness? : Int, fill? : Bool) ->
    core
    /types.Image

    画圆(中点画圆算法)。color 为颜色数组。thickness 为线宽(默认 1)。fill 为是否填充(默认 false)。

    draw_circle_16

    fn draw_circle_16(img :
    core
    /types.Image16, cx : Int, cy : Int, r : Int, color : Array[Int], thickness? : Int, fill? : Bool) ->
    core
    /types.Image16

    16-bit 绘制圆形。 @see draw_circle 8-bit 版本,本函数为 Image16 变体

    draw_circle_f

    fn draw_circle_f(img :
    core
    /types.ImageF, cx : Int, cy : Int, r : Int, color : Array[Float], thickness? : Int, fill? : Bool) ->
    core
    /types.ImageF

    float 绘制圆形。 @see draw_circle 8-bit 版本,本函数为 ImageF 变体

    draw_copy

    fn draw_copy(dst :
    core
    /types.Image, src :
    core
    /types.Image, x : Int, y : Int) ->
    core
    /types.Image raise
    core
    /types.LoadError

    将 src 图像直接拷贝到 dst 的 (x, y) 位置 返回新的 Image,不修改原图。要求 dst 和 src channels 相同。

    draw_line

    fn draw_line(img :
    core
    /types.Image, x1 : Int, y1 : Int, x2 : Int, y2 : Int, color : Array[Byte], thickness? : Int) ->
    core
    /types.Image

    画线段(Bresenham 算法)。color 为颜色数组(如 [R,G,B])。thickness 为线宽(默认 1)。

    draw_line_16

    fn draw_line_16(img :
    core
    /types.Image16, x1 : Int, y1 : Int, x2 : Int, y2 : Int, color : Array[Int], thickness? : Int) ->
    core
    /types.Image16

    16-bit 绘制直线。 @see draw_line 8-bit 版本,本函数为 Image16 变体

    draw_line_f

    fn draw_line_f(img :
    core
    /types.ImageF, x1 : Int, y1 : Int, x2 : Int, y2 : Int, color : Array[Float], thickness? : Int) ->
    core
    /types.ImageF

    float 绘制直线。 @see draw_line 8-bit 版本,本函数为 ImageF 变体

    draw_over

    fn draw_over(dst :
    core
    /types.Image, src :
    core
    /types.Image, x : Int, y : Int) ->
    core
    /types.Image raise
    core
    /types.LoadError

    Alpha 混合合成(source over destination) 将 src 图像合成到 dst 的 (x, y) 位置 要求 dst 和 src 均为 RGBA (channels=4) 公式: result = src * src_a + dst * (1 - src_a)

    draw_polygon

    fn draw_polygon(img :
    core
    /types.Image, points : Array[(Int, Int)], color : Array[Byte], thickness? : Int, fill? : Bool) ->
    core
    /types.Image

    画多边形。points 为顶点数组 [(x1,y1), (x2,y2), ...]。color 为颜色数组。thickness 为线宽(默认 1)。fill 为是否填充(默认 false)。

    draw_polygon_16

    fn draw_polygon_16(img :
    core
    /types.Image16, points : Array[(Int, Int)], color : Array[Int], thickness? : Int) ->
    core
    /types.Image16

    16-bit 绘制多边形。 @see draw_polygon 8-bit 版本,本函数为 Image16 变体

    draw_polygon_f

    fn draw_polygon_f(img :
    core
    /types.ImageF, points : Array[(Int, Int)], color : Array[Float], thickness? : Int) ->
    core
    /types.ImageF

    float 绘制多边形。 @see draw_polygon 8-bit 版本,本函数为 ImageF 变体

    draw_rectangle

    fn draw_rectangle(img :
    core
    /types.Image, x : Int, y : Int, rw : Int, rh : Int, color : Array[Byte], thickness? : Int, fill? : Bool) ->
    core
    /types.Image

    画矩形。color 为颜色数组。thickness 为线宽(默认 1)。fill 为是否填充(默认 false)。

    draw_rectangle_16

    fn draw_rectangle_16(img :
    core
    /types.Image16, x : Int, y : Int, rw : Int, rh : Int, color : Array[Int], thickness? : Int, fill? : Bool) ->
    core
    /types.Image16

    16-bit 绘制矩形。 @see draw_rectangle 8-bit 版本,本函数为 Image16 变体

    draw_rectangle_f

    fn draw_rectangle_f(img :
    core
    /types.ImageF, x : Int, y : Int, rw : Int, rh : Int, color : Array[Float], thickness? : Int, fill? : Bool) ->
    core
    /types.ImageF

    float 绘制矩形。 @see draw_rectangle 8-bit 版本,本函数为 ImageF 变体

    embed_visible_watermark

    fn embed_visible_watermark(img :
    core
    /types.Image, watermark_text : String, x : Int, y : Int, opacity : Double) ->
    core
    /types.Image raise
    core
    /types.LoadError

    可见水印嵌入

    embed_watermark_lsb

    fn embed_watermark_lsb(img :
    core
    /types.Image, watermark : Array[Byte], strength : Int) -> (
    core
    /types.Image,
    domain
    /encryption.WatermarkInfo) raise
    core
    /types.LoadError

    LSB 水印嵌入

    encode_apng

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

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

    encode_apng_pure

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

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

    encode_best

    fn encode_best(img :
    core
    /types.Image, quality : Int) ->
    codec
    /lib.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) ->
    codec
    /lib.EncodedImage raise
    core
    /types.ImageError

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

    encode_bmp

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

    BMP 编码委托

    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

    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_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

    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_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

    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 渐进式编码(纯 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[
    codec
    /codec.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_auto

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

    PGM 编码委托

    encode_pgm_pure

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

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

    encode_png

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

    PNG 编码委托

    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_auto

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

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

    encode_pnm_pure

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

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

    encode_ppm_auto

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

    PPM 编码委托

    encode_ppm_pure

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

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

    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_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

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

    TGA 编码委托

    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_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。

    encrypt_image_logistic

    fn encrypt_image_logistic(img :
    core
    /types.Image, key? : Double, mu? : Double) ->
    core
    /types.Image raise
    core
    /types.LoadError

    基于 Logistic 混沌映射的图像加密

    encrypt_image_xor

    fn encrypt_image_xor(img :
    core
    /types.Image, key : String) ->
    core
    /types.Image raise
    core
    /types.LoadError

    简单的 XOR 加密(使用密钥字节序列)

    encryption_default_config

    fn encryption_default_config() ->
    domain
    /encryption.EncryptionConfig

    创建默认加密配置

    encryption_report_to_string

    fn encryption_report_to_string(encryption :
    domain
    /encryption.EncryptionResult, watermark :
    domain
    /encryption.WatermarkInfo, quality :
    domain
    /encryption.ImageEncryptionQuality) -> String

    生成加密与水印报告

    evaluate_encryption_quality

    fn evaluate_encryption_quality(original :
    core
    /types.Image, encrypted :
    core
    /types.Image) ->
    domain
    /encryption.EncryptionQuality raise
    core
    /types.LoadError

    评估加密质量

    evaluate_image_encryption_quality

    fn evaluate_image_encryption_quality(original :
    core
    /types.Image, encrypted :
    core
    /types.Image) ->
    domain
    /encryption.ImageEncryptionQuality raise
    core
    /types.LoadError

    评估加密质量

    extract_watermark_lsb

    fn extract_watermark_lsb(img :
    core
    /types.Image, watermark_length : Int, strength : Int) ->
    domain
    /encryption.WatermarkExtractionResult raise
    core
    /types.LoadError

    LSB 水印提取

    failure_reason_pure

    fn failure_reason_pure() -> String

    纯 MoonBit 错误原因字符串(全目标可用)。 返回上次解码失败的错误描述。

    find_horizontal_seam

    fn find_horizontal_seam(energy : Array[Double], w : Int, h : Int) -> Array[Int]

    找水平最小能量 seam(返回每列的行索引,长度为 w)

    find_vertical_seam

    fn find_vertical_seam(energy : Array[Double], w : Int, h : Int) -> Array[Int]

    找垂直最小能量 seam(返回每行的列索引,长度为 h)

    flip_horizontal

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

    水平翻转(左右镜像)

    flip_horizontal_16

    fn flip_horizontal_16(img :
    core
    /types.Image16) ->
    core
    /types.Image16

    @see flip_horizontal 8-bit 版本,本函数为 Image16 变体

    flip_horizontal_f

    fn flip_horizontal_f(img :
    core
    /types.ImageF) ->
    core
    /types.ImageF

    @see flip_horizontal 8-bit 版本,本函数为 ImageF 变体

    flip_vertical

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

    垂直翻转(上下镜像)

    flip_vertical_16

    fn flip_vertical_16(img :
    core
    /types.Image16) ->
    core
    /types.Image16

    16-bit 垂直翻转。上下镜像。 @see flip_vertical 8-bit 版本,本函数为 Image16 变体

    flip_vertical_f

    fn flip_vertical_f(img :
    core
    /types.ImageF) ->
    core
    /types.ImageF

    float 垂直翻转。上下镜像。 @see flip_vertical 8-bit 版本,本函数为 ImageF 变体

    flip_vertically_on_write_pure

    fn flip_vertically_on_write_pure(flag : Bool) -> Unit

    设置写入时是否垂直翻转(纯 MoonBit,全目标可用)

    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

    generate_chaotic_sequence_enc

    fn generate_chaotic_sequence_enc(params :
    domain
    /encryption.ChaoticParams, length : Int) -> Array[Double]

    生成混沌序列(Lorenz 系统简化版)

    get_affine_transform

    fn get_affine_transform(src : Array[(Float, Float)], dst : Array[(Float, Float)]) ->
    process
    /transform.AffineMatrix?

    从 3 对源点→目标点计算 2×3 仿射变换矩阵。

    get_convert_iphone_png_to_rgb_pure

    fn get_convert_iphone_png_to_rgb_pure() -> Bool

    获取是否将 iPhone PNG 转换为 RGB

    get_flip_vertically_on_load_pure

    fn get_flip_vertically_on_load_pure() -> Bool

    获取加载时是否垂直翻转

    get_flip_vertically_on_write_pure

    fn get_flip_vertically_on_write_pure() -> Bool

    获取写入时是否垂直翻转

    get_hdr_to_ldr_gamma_pure

    fn get_hdr_to_ldr_gamma_pure() -> Float

    获取 HDR→LDR gamma

    get_hdr_to_ldr_scale_pure

    fn get_hdr_to_ldr_scale_pure() -> Float

    获取 HDR→LDR scale

    get_image_info

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

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

    get_image_info_extended

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

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

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

    get_image_summary

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

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

    get_ldr_to_hdr_gamma_pure

    fn get_ldr_to_hdr_gamma_pure() -> Float

    获取 LDR→HDR gamma

    get_ldr_to_hdr_scale_pure

    fn get_ldr_to_hdr_scale_pure() -> Float

    获取 LDR→HDR scale

    get_perspective_transform

    fn get_perspective_transform(src : Array[(Float, Float)], dst : Array[(Float, Float)]) ->
    process
    /transform.PerspectiveMatrix?

    从 4 对源点→目标点计算 3×3 透视变换矩阵。 src 和 dst 均为 4 个 (Float, Float) 点。

    get_rotation_matrix_2d

    fn get_rotation_matrix_2d(center : (Float, Float), angle : Float, scale : Float) ->
    process
    /transform.AffineMatrix

    从中心点、角度和缩放计算 2×3 旋转矩阵。 angle 为弧度,scale 为缩放因子。

    get_unpremultiply_on_load_pure

    fn get_unpremultiply_on_load_pure() -> Bool

    获取加载时是否取消预乘 alpha

    gif_info_extended

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

    解析 GIF 扩展头部信息

    has_alpha

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

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

    hdr_info_extended

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

    解析 HDR 扩展头部信息

    hdr_to_ldr_gamma_pure

    fn hdr_to_ldr_gamma_pure(gamma : Float) -> Unit

    设置 HDR→LDR 转换的 gamma 值(纯 MoonBit,全目标可用)

    hdr_to_ldr_scale_pure

    fn hdr_to_ldr_scale_pure(scale : Float) -> Unit

    设置 HDR→LDR 转换的 scale 值(纯 MoonBit,全目标可用)

    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) ->
    codec
    /codec.HeifInfo raise
    core
    /types.LoadError

    解析 HEIF 头部信息

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

    heif_info_extended

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

    解析 HEIF 扩展头部信息

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

    histogram_entropy

    fn histogram_entropy(img :
    core
    /types.Image) -> Double raise
    core
    /types.LoadError

    计算直方图熵(衡量加密质量,熵越高越好)

    icns_info_extended

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

    解析 ICNS 扩展头部信息

    ico_info_extended

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

    解析 ICO 扩展头部信息

    info_dims

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

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

    info_from_bytes_pure

    fn info_from_bytes_pure(data : Bytes) ->
    core
    /types.ImageInfo? raise
    core
    /types.LoadError

    从字节流读取图像信息(纯 MoonBit,全目标可用)。 支持 BMP/QOI/PSD/GIF/PNG/JPEG/PNM(HDR 检测但不解析尺寸)。 返回 None 如果格式不支持或数据无效。

    is_16_bit_from_bytes_pure

    fn is_16_bit_from_bytes_pure(data : Bytes) -> Bool

    检测是否为 16-bit 图像(仅 PNG 支持 16-bit)

    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_hdr_from_bytes_pure

    fn is_hdr_from_bytes_pure(data : Bytes) -> Bool

    检测是否为 HDR 格式(magic: "#?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) ->
    codec
    /codec.JpegInfoExtended raise
    core
    /types.LoadError

    解析 JPEG 扩展头部信息

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

    jpeg_progressive_script_eight

    fn jpeg_progressive_script_eight() -> Array[
    codec
    /codec.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[
    codec
    /codec.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) ->
    codec
    /codec.JpegXlInfo raise
    core
    /types.LoadError

    解析 JPEG-XL 头部信息

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

    jpegxl_info_extended

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

    解析 JPEG-XL 扩展头部信息

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

    key_sensitivity_test

    fn key_sensitivity_test(img :
    core
    /types.Image, key : Double, delta? : Double) -> Double raise
    core
    /types.LoadError

    密钥敏感性测试(微小密钥变化导致完全不同的密文)

    ldr_to_hdr_gamma_pure

    fn ldr_to_hdr_gamma_pure(gamma : Float) -> Unit

    设置 LDR→HDR 转换的 gamma 值(纯 MoonBit,全目标可用)

    ldr_to_hdr_scale_pure

    fn ldr_to_hdr_scale_pure(scale : Float) -> Unit

    设置 LDR→HDR 转换的 scale 值(纯 MoonBit,全目标可用)

    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)
    }
    }

    multi_band_blend

    fn multi_band_blend(img_a :
    core
    /types.Image, img_b :
    core
    /types.Image, mask :
    core
    /types.Image, num_bands? : Int) ->
    core
    /types.Image raise
    core
    /types.LoadError

    多频带融合(Laplacian pyramid blend)。 img_a 和 img_b 尺寸需相同,mask 为灰度图(非零区域取 img_a,零区域取 img_b)。 num_bands 为金字塔层数(默认 5)。

    multi_band_blend_16

    fn multi_band_blend_16(img_a :
    core
    /types.Image16, img_b :
    core
    /types.Image16, mask :
    core
    /types.Image16, num_bands? : Int) ->
    core
    /types.Image16 raise
    core
    /types.LoadError

    多频带融合 16-bit 版本。 @see multi_band_blend 8-bit 版本,本函数为 Image16 变体

    multi_band_blend_f

    fn multi_band_blend_f(img_a :
    core
    /types.ImageF, img_b :
    core
    /types.ImageF, mask :
    core
    /types.ImageF, num_bands? : Int) ->
    core
    /types.ImageF raise
    core
    /types.LoadError

    多频带融合 float 版本。 @see multi_band_blend 8-bit 版本,本函数为 ImageF 变体

    pipeline_add

    fn pipeline_add(p :
    domain
    /pipeline.Pipeline, op :
    domain
    /pipeline.Operation) ->
    domain
    /pipeline.Pipeline

    添加操作到流水线

    pipeline_add_all

    fn pipeline_add_all(p :
    domain
    /pipeline.Pipeline, ops : Array[
    domain
    /pipeline.Operation]) ->
    domain
    /pipeline.Pipeline

    批量添加操作

    pipeline_describe

    fn pipeline_describe(p :
    domain
    /pipeline.Pipeline) -> String

    流水线转字符串描述

    pipeline_len

    fn pipeline_len(p :
    domain
    /pipeline.Pipeline) -> Int

    获取流水线中的操作数量

    pipeline_new

    fn pipeline_new() ->
    domain
    /pipeline.Pipeline

    创建空流水线

    png_info_extended

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

    解析 PNG 扩展头部信息

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

    pnm_info_extended

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

    解析 PNM 扩展头部信息

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

    premultiply_alpha_pure

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

    预乘 alpha(RGBA → RGBA premultiplied,纯 MoonBit,全目标可用) 要求 channels=4

    psd_info_extended

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

    解析 PSD 扩展头部信息

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

    pyr_down

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

    高斯金字塔下采样。将图像尺寸减半(宽高各 /2),使用 2x2 均值滤波。

    pyr_down_16

    fn pyr_down_16(img :
    core
    /types.Image16) ->
    core
    /types.Image16 raise
    core
    /types.LoadError

    高斯金字塔下采样 16-bit 版本。 @see pyr_down 8-bit 版本,本函数为 Image16 变体

    pyr_down_f

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

    高斯金字塔下采样 float 版本。 @see pyr_down 8-bit 版本,本函数为 ImageF 变体

    pyr_up

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

    拉普拉斯金字塔上采样。将图像尺寸加倍(宽高各 *2),使用双线性插值。

    pyr_up_16

    fn pyr_up_16(img :
    core
    /types.Image16) ->
    core
    /types.Image16 raise
    core
    /types.LoadError

    拉普拉斯金字塔上采样 16-bit 版本。 @see pyr_up 8-bit 版本,本函数为 Image16 变体

    pyr_up_f

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

    拉普拉斯金字塔上采样 float 版本。 @see pyr_up 8-bit 版本,本函数为 ImageF 变体

    qoi_info_extended

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

    解析 QOI 扩展头部信息

    remove_horizontal_seam

    fn remove_horizontal_seam(img :
    core
    /types.Image, seam : Array[Int]) ->
    core
    /types.Image

    移除水平 seam,高度减 1

    remove_horizontal_seam_16

    fn remove_horizontal_seam_16(img :
    core
    /types.Image16, seam : Array[Int]) ->
    core
    /types.Image16

    移除水平 seam 16-bit 版本 @see remove_horizontal_seam 8-bit 版本,本函数为 Image16 变体

    remove_horizontal_seam_f

    fn remove_horizontal_seam_f(img :
    core
    /types.ImageF, seam : Array[Int]) ->
    core
    /types.ImageF

    移除水平 seam float 版本 @see remove_horizontal_seam 8-bit 版本,本函数为 ImageF 变体

    remove_vertical_seam

    fn remove_vertical_seam(img :
    core
    /types.Image, seam : Array[Int]) ->
    core
    /types.Image

    移除垂直 seam,宽度减 1

    remove_vertical_seam_16

    fn remove_vertical_seam_16(img :
    core
    /types.Image16, seam : Array[Int]) ->
    core
    /types.Image16

    移除垂直 seam 16-bit 版本 @see remove_vertical_seam 8-bit 版本,本函数为 Image16 变体

    remove_vertical_seam_f

    fn remove_vertical_seam_f(img :
    core
    /types.ImageF, seam : Array[Int]) ->
    core
    /types.ImageF

    移除垂直 seam float 版本 @see remove_vertical_seam 8-bit 版本,本函数为 ImageF 变体

    resize_16

    fn resize_16(img :
    core
    /types.Image16, new_w : Int, new_h : Int) ->
    core
    /types.Image16

    16-bit 图像缩放。 @see resize 8-bit 版本,本函数为 Image16 变体

    resize_f

    fn resize_f(img :
    core
    /types.ImageF, new_w : Int, new_h : Int) ->
    core
    /types.ImageF

    float 图像缩放。 @see resize 8-bit 版本,本函数为 ImageF 变体

    resize_pure

    fn resize_pure(img :
    core
    /types.Image, out_w : Int, out_h : Int, filter? :
    core
    /types.ResizeFilter, edge? :
    core
    /types.ResizeEdge) ->
    core
    /types.Image raise
    core
    /types.LoadError

    resize 8-bit 图像(纯 MoonBit,全目标可用)。 使用双线性插值(filter = PointSample 时使用最近邻)。 edge 参数目前统一按 Clamp 处理(边缘像素重复)。 v190.0.0 优化:预计算坐标权重数组,消除 data_int 转换, 直接在 Bytes 上操作,减少 O(n) 内存拷贝和重复浮点运算。

    resize_subsample

    fn resize_subsample(img :
    core
    /types.Image, out_w : Int, out_h : Int) ->
    core
    /types.Image raise
    core
    /types.LoadError

    面积平均重采样(下采样首选,上采样退化为最近邻)。 每格把源图对应矩形内的像素求平均后写一个输出像素——不建中间缓冲。 失败条件:输入/输出尺寸非正、数据长度与 width*height*channels 不符。

    resize_to_contain

    fn resize_to_contain(img :
    core
    /types.Image, target_w : Int, target_h : Int, fill_color : Array[Byte]) ->
    core
    /types.Image raise
    core
    /types.LoadError

    Resize 到目标尺寸内(contain 模式),保持宽高比,不足部分用 fill_color 填充。

    resize_to_cover

    fn resize_to_cover(img :
    core
    /types.Image, target_w : Int, target_h : Int) ->
    core
    /types.Image raise
    core
    /types.LoadError

    Resize 并裁剪到精确尺寸(cover 模式)。 先按宽高比缩放使图像"覆盖"目标尺寸,然后居中裁剪多余部分。

    rotate

    fn rotate(img :
    core
    /types.Image, angle_degrees : Float) ->
    core
    /types.Image

    任意角度旋转(绕中心,保留原尺寸,角落填充黑色)。 angle 单位为度,正值逆时针旋转。使用双线性插值。

    rotate_180

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

    旋转 180 度

    rotate_180_16

    fn rotate_180_16(img :
    core
    /types.Image16) ->
    core
    /types.Image16

    @see rotate_180 8-bit 版本,本函数为 Image16 变体

    rotate_180_f

    fn rotate_180_f(img :
    core
    /types.ImageF) ->
    core
    /types.ImageF

    @see rotate_180 8-bit 版本,本函数为 ImageF 变体

    rotate_270

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

    顺时针旋转 270 度(逆时针 90 度)

    rotate_270_16

    fn rotate_270_16(img :
    core
    /types.Image16) ->
    core
    /types.Image16

    @see rotate_270 8-bit 版本,本函数为 Image16 变体

    rotate_270_f

    fn rotate_270_f(img :
    core
    /types.ImageF) ->
    core
    /types.ImageF

    @see rotate_270 8-bit 版本,本函数为 ImageF 变体

    rotate_90

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

    顺时针旋转 90 度

    rotate_90_16

    fn rotate_90_16(img :
    core
    /types.Image16) ->
    core
    /types.Image16

    @see rotate_90 8-bit 版本,本函数为 Image16 变体

    rotate_90_f

    fn rotate_90_f(img :
    core
    /types.ImageF) ->
    core
    /types.ImageF

    @see rotate_90 8-bit 版本,本函数为 ImageF 变体

    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

    seam_carve_resize

    fn seam_carve_resize(img :
    core
    /types.Image, new_w : Int, new_h : Int) ->
    core
    /types.Image raise
    core
    /types.LoadError

    内容感知缩放(Seam Carving) 支持缩小和放大,放大上限为原尺寸 2 倍

    seam_carve_resize_16

    fn seam_carve_resize_16(img :
    core
    /types.Image16, new_w : Int, new_h : Int) ->
    core
    /types.Image16 raise
    core
    /types.LoadError

    内容感知缩放 16-bit 版本 @see seam_carve_resize 8-bit 版本,本函数为 Image16 变体

    seam_carve_resize_f

    fn seam_carve_resize_f(img :
    core
    /types.ImageF, new_w : Int, new_h : Int) ->
    core
    /types.ImageF raise
    core
    /types.LoadError

    内容感知缩放 float 版本 @see seam_carve_resize 8-bit 版本,本函数为 ImageF 变体

    set_flip_vertically_on_load_pure

    fn set_flip_vertically_on_load_pure(flag : Bool) -> Unit

    设置加载时是否垂直翻转(纯 MoonBit,全目标可用)

    set_unpremultiply_on_load_pure

    fn set_unpremultiply_on_load_pure(flag : Bool) -> Unit

    设置加载时是否取消预乘 alpha(纯 MoonBit,全目标可用)

    tga_info_extended

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

    解析 TGA 扩展头部信息

    thumbnail

    fn thumbnail(img :
    core
    /types.Image, max_side : Int) ->
    core
    /types.Image raise
    core
    /types.LoadError

    等比缩略图:长边压到 max_side,短边按比例(四舍五入,至少 1 像素)。 原图两边都不超过 max_side 时原样返回(零分配)。

    thumbnail_budgeted

    fn thumbnail_budgeted(img :
    core
    /types.Image, max_side : Int, budget :
    core
    /types.AllocBudget) ->
    core
    /types.Image raise
    core
    /types.ImageError

    带分配预算的缩略图:先按尺寸算输出字节,超出 budget 直接拒(不分配)。 这是"大图出小图"的推荐入口:预算按输出计,底图内存由解码期 DecodeLimits 把关。

    thumbnail_dims

    fn thumbnail_dims(width : Int, height : Int, max_side : Int) -> (Int, Int)

    等比缩略图的输出尺寸(不分配内存,供预算检查先算)。

    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) ->
    codec
    /codec.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 压缩的字节流

    to_grayscale_pure

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

    转为灰度图(channels=1,纯 MoonBit,全目标可用) Convert to grayscale (channels=1, pure MoonBit, all targets) 使用 ITU-R BT.601 亮度公式: Y = 0.299R + 0.587G + 0.114B Uses ITU-R BT.601 luma formula: Y = 0.299R + 0.587G + 0.114B

    test "doc_to_grayscale_pure" {
    let data = Bytes::from_array([b'\xff', b'\x00', b'\x00'])
    let img = @types.Image::{ width: 1, height: 1, channels: 3, data, }
    let gray = @color.to_grayscale_pure(img)
    assert_eq(gray.channels, 1)
    assert_eq(gray.width, 1)
    }

    to_rgb_pure

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

    去除 alpha 通道(RGBA → RGB,channels=3,纯 MoonBit,全目标可用)

    to_rgba_pure

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

    添加 alpha 通道(RGB → RGBA,channels=4,alpha=255,纯 MoonBit,全目标可用)

    transpose_16

    fn transpose_16(img :
    core
    /types.Image16) ->
    core
    /types.Image16

    16-bit 转置。交换宽高,(x,y) -> (y,x)。 @see transpose 8-bit 版本,本函数为 Image16 变体

    transpose_f

    fn transpose_f(img :
    core
    /types.ImageF) ->
    core
    /types.ImageF

    float 转置。交换宽高,(x,y) -> (y,x)。 @see transpose 8-bit 版本,本函数为 ImageF 变体

    unpremultiply_alpha_pure

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

    反预乘 alpha(RGBA premA → RGBA unpremultiplied,纯 MoonBit,全目标可用) 要求 channels=4

    warp_affine

    fn warp_affine(img :
    core
    /types.Image, matrix : (Float, Float, Float, Float, Float, Float), new_w : Int, new_h : Int) ->
    core
    /types.Image

    仿射变换。matrix = (a, b, c, d, e, f) 映射 dst→src: 仿射逆映射 src_x = a * dst_x + b * dst_y + c 仿射逆映射 src_y = d * dst_x + e * dst_y + f 使用双线性插值,超出边界的像素 clamp 到边缘。 输出尺寸为 new_w × new_h,通道数与输入相同。

    warp_affine_16

    fn warp_affine_16(img :
    core
    /types.Image16, matrix : (Float, Float, Float, Float, Float, Float), new_w : Int, new_h : Int) ->
    core
    /types.Image16

    16-bit 仿射变换。 @see warp_affine 8-bit 版本,本函数为 Image16 变体

    warp_affine_f

    fn warp_affine_f(img :
    core
    /types.ImageF, matrix : (Float, Float, Float, Float, Float, Float), new_w : Int, new_h : Int) ->
    core
    /types.ImageF

    float 仿射变换。 @see warp_affine 8-bit 版本,本函数为 ImageF 变体

    warp_perspective

    fn warp_perspective(img :
    core
    /types.Image, matrix :
    process
    /transform.PerspectiveMatrix, new_w : Int, new_h : Int) ->
    core
    /types.Image

    透视变换。使用 3×3 矩阵将图像变换到 new_w × new_h。 采用 dst→src 映射 + 双线性插值。

    warp_perspective_16

    fn warp_perspective_16(img :
    core
    /types.Image16, matrix :
    process
    /transform.PerspectiveMatrix, new_w : Int, new_h : Int) ->
    core
    /types.Image16

    16-bit 透视变换。 @see warp_perspective 8-bit 版本,本函数为 Image16 变体

    warp_perspective_f

    fn warp_perspective_f(img :
    core
    /types.ImageF, matrix :
    process
    /transform.PerspectiveMatrix, new_w : Int, new_h : Int) ->
    core
    /types.ImageF

    float 透视变换。 @see warp_perspective 8-bit 版本,本函数为 ImageF 变体

    webp_info_extended

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

    解析 WebP 扩展头部信息

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

    xor_decrypt

    fn xor_decrypt(encrypted :
    domain
    /encryption.EncryptionResult, key : Array[Byte]) ->
    core
    /types.Image

    XOR 解密

    xor_encrypt

    fn xor_encrypt(img :
    core
    /types.Image, key : Array[Byte]) ->
    domain
    /encryption.EncryptionResult raise
    core
    /types.LoadError

    XOR 加密

    zlib_compress

    fn zlib_compress(data : Bytes) -> Bytes

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

    zlib_inflate

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

    DEFLATE inflate 解压缩(纯 MoonBit,全目标可用)

    Source Files