image

    Image codec primitives for MoonBit with PNG/BMP/JPEG decode+encode, GIF/WebP/ICO/AVIF encode, and resize.

    moonbit
    image
    bmp
    jpeg
    gif
    png
    webp
    ico
    avif
    encoder
    decoder
    Download zip
    Author
    Version
    0.4.3
    License
    Apache-2.0
    Last updated
    2 months ago
    Downloads
    21K

    Dependencies

    #mizchi/image

    Image codec primitives for MoonBit. Core codecs have no external dependencies except mizchi/zlib; the AVIF js-target MVP uses browser AVIF canvas support or local ffmpeg.

    #Features

    • PNG decode / encode (RGBA8, adaptive row filtering)
    • BMP decode / encode
    • JPEG baseline decode / encode
    • GIF encode (single-frame, indexed palette, binary transparency)
    • WebP lossless encode (still image)
    • ICO encode (single-image, PNG payload)
    • AVIF encode (js target MVP, opaque still image)
    • Resize with Nearest / Bilinear / Bicubic interpolation

    All decoded images are normalized to ImageData (RGBA8 buffer).

    #Install

    moon add mizchi/image

    #Usage

    // PNG decode → resize → encode
    let img = @image.decode_png(png_bytes)
    let resized = @image.resize(img, 128, 128, Bilinear)
    let out = @image.encode_png(resized)

    // JPEG encode (quality defaults to 85)
    let jpg = @image.encode_jpeg(resized)

    // GIF encode (<= 256 colors, alpha 0/255 only)
    let gif = @image.encode_gif(resized)

    // WebP lossless encode
    let webp = @image.encode_webp(resized)

    // ICO encode (single PNG-backed icon, <= 256x256)
    let ico = @image.encode_ico(resized)

    // AVIF encode (js target MVP, opaque images only)
    let avif = @image.encode_avif(resized)

    // Stream decode (PNG/BMP auto detect)
    let rows : Array[Bytes] = []
    let info = @image.decode_image_stream(
    image_bytes,
    on_row=fn(_y, row) { rows.push(row) },
    )
    inspect((info.format, info.width, info.height))

    #API

    pub fn decode_png(Bytes) -> ImageData raise DecodeError pub fn decode_png_stream(Bytes, on_row~ : (Int, Bytes) -> Unit) -> IhdrData raise DecodeError pub fn decode_image_stream(Bytes, on_row~ : (Int, Bytes) -> Unit) -> StreamImageInfo raise DecodeError pub fn encode_png(ImageData) -> Bytes raise EncodeError pub fn decode_bmp(Bytes) -> ImageData raise DecodeError pub fn decode_bmp_stream(Bytes, on_row~ : (Int, Bytes) -> Unit) -> (Int, Int) raise DecodeError pub fn encode_bmp(ImageData) -> Bytes raise EncodeError pub fn decode_jpeg(Bytes) -> ImageData raise DecodeError pub fn encode_jpeg(ImageData, quality? : Int = 85) -> Bytes raise EncodeError pub fn encode_gif(ImageData) -> Bytes raise EncodeError pub fn encode_webp(ImageData) -> Bytes raise EncodeError pub fn encode_ico(ImageData) -> Bytes raise EncodeError pub fn encode_avif(ImageData) -> Bytes raise EncodeError pub fn resize(ImageData, Int, Int, ResizeMethod) -> ImageData raise EncodeError

    #Types

    pub(all) struct ImageData { width : Int; height : Int; data : Bytes } pub(all) enum ResizeMethod { Nearest; Bilinear; Bicubic }

    #Support

    FeatureDecodeEncodeTargetsNotes
    PNGyesyesjs / native / wasm-gcRGBA8 normalize, stream decode
    BMPyesyesjs / native / wasm-gcdecode: 24/32-bit, encode: 32-bit BGRA
    JPEGyesyesjs / native / wasm-gcbaseline only
    GIFnoyesjs / native / wasm-gcsingle-frame, <=256 colors, alpha 0/255 only
    WebPnoyesjs / native / wasm-gclossless still-image encoder
    ICOnoyesjs / native / wasm-gcsingle PNG payload, <=256x256
    AVIFnoyesjsopaque images only, requires browser AVIF canvas support or local ffmpeg
    Resizen/an/ajs / native / wasm-gcNearest / Bilinear / Bicubic

    #License

    Apache-2.0

    DecodeError

    pub(all) suberror DecodeError {
    InvalidSignature(String)
    InvalidChunkCrc(String)
    MissingChunk(String)
    UnsupportedFeature(String)
    CorruptData(String)
    }

    impl Show for DecodeError

    EncodeError

    pub(all) suberror EncodeError {
    InvalidDimensions(String)
    InvalidData(String)
    }

    impl Show for EncodeError

    ColorType

    pub enum ColorType {
    Grayscale
    Rgb
    Indexed
    GrayscaleAlpha
    Rgba
    } derive(Eq,
    Debug
    )

    impl Show for ColorType

    IhdrData

    pub(all) struct IhdrData {
    width : Int
    height : Int
    bit_depth : Int
    color_type : ColorType
    compression : Int
    filter : Int
    interlace : Int
    } derive(Eq)

    impl Show for IhdrData

    ImageData

    pub(all) struct ImageData {
    width : Int
    height : Int
    data : Bytes
    } derive(Eq,
    Debug
    )

    impl Show for ImageData

    ImageFormat

    pub(all) enum ImageFormat {
    Png
    Bmp
    } derive(Eq,
    Debug
    )

    impl Show for ImageFormat

    ResizeMethod

    pub(all) enum ResizeMethod {
    Nearest
    Bilinear
    Bicubic
    } derive(Eq)

    StreamImageInfo

    pub(all) struct StreamImageInfo {
    format : ImageFormat
    width : Int
    height : Int
    } derive(Eq)

    decode_bmp

    fn decode_bmp(raw : Bytes) -> ImageData raise DecodeError

    decode_bmp_stream

    fn decode_bmp_stream(raw : Bytes, on_row~ : (Int, Bytes) -> Unit) -> (Int, Int) raise DecodeError

    decode_image_stream

    fn decode_image_stream(raw : Bytes, on_row~ : (Int, Bytes) -> Unit) -> StreamImageInfo raise DecodeError

    decode_jpeg

    fn decode_jpeg(data : Bytes) -> ImageData raise DecodeError

    decode_png

    fn decode_png(raw : Bytes) -> ImageData raise DecodeError

    decode_png_stream

    fn decode_png_stream(raw : Bytes, on_row~ : (Int, Bytes) -> Unit) -> IhdrData raise DecodeError

    encode_avif

    fn encode_avif(_img : ImageData) -> Bytes raise EncodeError

    encode_bmp

    fn encode_bmp(img : ImageData) -> Bytes raise EncodeError

    encode_gif

    fn encode_gif(img : ImageData) -> Bytes raise EncodeError

    encode_ico

    fn encode_ico(img : ImageData) -> Bytes raise EncodeError

    encode_jpeg

    fn encode_jpeg(img : ImageData, quality? : Int) -> Bytes raise EncodeError

    encode_png

    fn encode_png(img : ImageData) -> Bytes raise EncodeError

    encode_webp

    fn encode_webp(img : ImageData) -> Bytes raise EncodeError

    package_name

    fn package_name() -> String

    Project marker.

    resize

    fn resize(img : ImageData, new_width : Int, new_height : Int, resize_method : ResizeMethod) -> ImageData raise EncodeError