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
moon add mizchi/image@0.4.3
Download zip
Author
Version
0.4.3
License
Apache-2.0
Last updated
27 days ago
Downloads
15K

Dependencies

README

#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

#
EncodeError

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

impl Show for EncodeError

#
ColorType

pub enum ColorType {
Grayscale
Rgb
Indexed
GrayscaleAlpha
Rgba
}

impl Eq for ColorType
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
}

impl Eq for IhdrData
impl Show for IhdrData

#
ImageData

pub(all) struct ImageData {
width : Int
height : Int
data : Bytes
}

impl Eq for ImageData
impl Show for ImageData

#
ImageFormat

pub(all) enum ImageFormat {
Png
Bmp
}

impl Eq for ImageFormat
impl Show for ImageFormat

#
ResizeMethod

pub(all) enum ResizeMethod {
Nearest
Bilinear
Bicubic
}

impl Eq for ResizeMethod

#
StreamImageInfo

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

#
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