Sign in

    svgdiff-image-compat

    WIP image codec primitives for MoonBit (PNG-first).

    moonbit
    image
    png
    encoder
    decoder
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    9 days ago
    Downloads
    14

    Dependencies

    #mizchi/image

    Image codec primitives for MoonBit (no external dependencies except mizchi/zlib).

    #Features

    • PNG decode / encode (RGBA8, adaptive row filtering)
    • BMP decode / encode (24-bit uncompressed)
    • JPEG baseline decode / encode
    • 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)

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

    #Targets

    Works on js, native, and wasm-gc backends.

    #License

    Apache-2.0

    DecodeError

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

    impl Show for DecodeError

    DecodeError::output

    fn DecodeError::output(self : DecodeError, logger : &Logger) -> Unit

    DecodeError::to_string

    fn DecodeError::to_string(self : DecodeError) -> String

    EncodeError

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

    impl Show for EncodeError

    EncodeError::output

    fn EncodeError::output(self : EncodeError, logger : &Logger) -> Unit

    EncodeError::to_string

    fn EncodeError::to_string(self : EncodeError) -> String

    ColorType

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

    ColorType::equal

    fn ColorType::equal(ColorType, ColorType) -> Bool

    ColorType::not_equal

    fn ColorType::not_equal(x : ColorType, y : ColorType) -> Bool

    IhdrData

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

    IhdrData::equal

    fn IhdrData::equal(IhdrData, IhdrData) -> Bool

    IhdrData::not_equal

    fn IhdrData::not_equal(x : IhdrData, y : IhdrData) -> Bool

    IhdrData::to_repr

    ImageData

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

    ImageData::equal

    fn ImageData::equal(ImageData, ImageData) -> Bool

    ImageData::not_equal

    fn ImageData::not_equal(x : ImageData, y : ImageData) -> Bool

    ImageFormat

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

    ImageFormat::equal

    fn ImageFormat::equal(ImageFormat, ImageFormat) -> Bool

    ImageFormat::not_equal

    fn ImageFormat::not_equal(x : ImageFormat, y : ImageFormat) -> Bool

    ResizeMethod

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

    ResizeMethod::equal

    ResizeMethod::not_equal

    fn ResizeMethod::not_equal(x : ResizeMethod, y : ResizeMethod) -> Bool

    StreamImageInfo

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

    StreamImageInfo::equal

    StreamImageInfo::not_equal

    fn StreamImageInfo::not_equal(x : StreamImageInfo, y : StreamImageInfo) -> Bool

    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_bmp

    fn encode_bmp(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

    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