image

    MoonBit image: simple image representation.

    image
    color
    moonbit
    Download zip
    Author
    Version
    0.17.14
    License
    Apache-2.0
    Last updated
    17 days ago
    Downloads
    8K

    Dependencies

    #gmlewis/image

    check

    This is a simple image representation based on Go's implementation: https://cs.opensource.google/go/go/+/refs/tags/go1.23.3:src/image/image.go which has the copyright notice:

    // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file.

    #Status

    The code has been updated to support compiler:

    $ moon version --all moon 0.1.20260827 (d0aaa07 2026-08-27) ~/.moon/bin/moon moonc v0.10.11+6ff76a5f9 (2026-08-28) ~/.moon/bin/moonc moonrun 0.1.20260827 (d0aaa07 2026-08-27) ~/.moon/bin/moonrun

    Image

    pub(open) trait Image {
    fn color_model(Self) -> &
    Model

    fn bounds(Self) -> Rectangle
    fn at(Self, Int, Int) -> &
    Color

    fn opaque_(Self) -> Bool
    fn set(Self, Int, Int, &
    Color
    ) -> Unit
    fn sub_image(Self, Rectangle) -> &Image
    fn as_ycbcr(Self) -> YCbCr?
    fn raw_data(Self) ->
    Slice
    [Byte]
    fn get_bytes_per_pixel(Self) -> Int
    fn get_stride(Self) -> Int
    fn pix_offset(Self, Int, Int) -> Int
    fn color_index_at(Self, Int, Int) -> Byte
    }

    Image is a finite rectangular grid of [color.Color] values taken from a color model.

    RGBA64Image

    pub(open) trait RGBA64Image {
    fn rgba64_at(Self, Int, Int) ->
    RGBA64

    fn color_model(Self) -> &
    Model

    fn bounds(Self) -> Rectangle
    fn at(Self, Int, Int) -> &
    Color

    }

    RGBA64Image is an [Image] whose pixels can be converted directly to a color.RGBA64.

    SizeError

    type SizeError derive(Eq)

    impl Show for SizeError

    Alpha

    pub(all) struct Alpha {
    pix :
    Slice
    [Byte]
    stride : Int
    rect : Rectangle
    }

    Alpha is an in-memory image whose At method returns [color.Alpha] values.
    impl Image for Alpha

    Alpha::alpha_at

    fn Alpha::alpha_at(self : Alpha, x : Int, y : Int) ->
    Alpha

    Alpha::at

    fn Alpha::at(self : Alpha, x : Int, y : Int) -> &
    Color

    Alpha::get

    #alias("_[_]")
    fn Alpha::get(self : Alpha, p : Point) -> &
    Color

    Alpha::new

    fn Alpha::new(r : Rectangle) -> Alpha raise SizeError

    Alpha::new returns a new [Alpha] image with the given bounds.

    Alpha::new_empty

    fn Alpha::new_empty() -> Alpha

    Alpha::pix_offset

    fn Alpha::pix_offset(self : Alpha, x : Int, y : Int) -> Int

    Alpha::rgba64_at

    fn Alpha::rgba64_at(self : Alpha, x : Int, y : Int) ->
    RGBA64

    Alpha::set

    fn Alpha::set(self : Alpha, x : Int, y : Int, c : &
    Color
    ) -> Unit

    Alpha::set_alpha

    fn Alpha::set_alpha(self : Alpha, x : Int, y : Int, c :
    Alpha
    ) -> Unit

    Alpha::set_point

    #alias("_[_]=_")
    fn Alpha::set_point(self : Alpha, p : Point, c : &
    Color
    ) -> Unit

    Alpha::set_rgba64

    fn Alpha::set_rgba64(self : Alpha, x : Int, y : Int, c :
    RGBA64
    ) -> Unit

    Alpha16

    pub(all) struct Alpha16 {
    pix :
    Slice
    [Byte]
    stride : Int
    rect : Rectangle
    }

    Alpha16 is an in-memory image whose At method returns [color.Alpha16] values.
    impl Image for Alpha16

    Alpha16::alpha16_at

    fn Alpha16::alpha16_at(self : Alpha16, x : Int, y : Int) ->
    Alpha16

    Alpha16::at

    fn Alpha16::at(self : Alpha16, x : Int, y : Int) -> &
    Color

    Alpha16::get

    #alias("_[_]")
    fn Alpha16::get(self : Alpha16, p : Point) -> &
    Color

    Alpha16::new

    fn Alpha16::new(r : Rectangle) -> Alpha16 raise SizeError

    Alpha16::new returns a new [Alpha16] image with the given bounds.

    Alpha16::new_empty

    fn Alpha16::new_empty() -> Alpha16

    Alpha16::pix_offset

    fn Alpha16::pix_offset(self : Alpha16, x : Int, y : Int) -> Int

    Alpha16::rgba64_at

    fn Alpha16::rgba64_at(self : Alpha16, x : Int, y : Int) ->
    RGBA64

    Alpha16::set

    fn Alpha16::set(self : Alpha16, x : Int, y : Int, c : &
    Color
    ) -> Unit

    Alpha16::set_alpha16

    fn Alpha16::set_alpha16(self : Alpha16, x : Int, y : Int, c :
    Alpha16
    ) -> Unit

    Alpha16::set_point

    #alias("_[_]=_")
    fn Alpha16::set_point(self : Alpha16, p : Point, c : &
    Color
    ) -> Unit

    Alpha16::set_rgba64

    fn Alpha16::set_rgba64(self : Alpha16, x : Int, y : Int, c :
    RGBA64
    ) -> Unit

    CMYK

    pub(all) struct CMYK {
    pix :
    Slice
    [Byte]
    stride : Int
    rect : Rectangle
    }

    CMYK is an in-memory image whose At method returns [color.CMYK] values.
    impl Image for CMYK

    CMYK::at

    fn CMYK::at(self : CMYK, x : Int, y : Int) -> &
    Color

    CMYK::cmyk_at

    fn CMYK::cmyk_at(self : CMYK, x : Int, y : Int) ->
    CMYK

    CMYK::get

    #alias("_[_]")
    fn CMYK::get(self : CMYK, p : Point) -> &
    Color

    CMYK::new

    fn CMYK::new(r : Rectangle) -> CMYK raise SizeError

    CMYK::new returns a new CMYK image with the given bounds.

    CMYK::new_empty

    fn CMYK::new_empty() -> CMYK

    CMYK::pix_offset

    fn CMYK::pix_offset(self : CMYK, x : Int, y : Int) -> Int

    CMYK::rgba64_at

    fn CMYK::rgba64_at(self : CMYK, x : Int, y : Int) ->
    RGBA64

    CMYK::set_cmyk

    fn CMYK::set_cmyk(self : CMYK, x : Int, y : Int, c :
    CMYK
    ) -> Unit

    CMYK::set_rgba64

    fn CMYK::set_rgba64(self : CMYK, x : Int, y : Int, c :
    RGBA64
    ) -> Unit

    Config

    pub(all) struct Config {
    color_model : &
    Model

    width : Int
    height : Int
    }

    Config holds an image's color model and dimensions.

    Config::new_empty

    fn Config::new_empty() -> Config

    Gray

    pub(all) struct Gray {
    pix :
    Slice
    [Byte]
    stride : Int
    rect : Rectangle
    }

    Gray is an in-memory image whose At method returns [color.Gray] values.
    impl Image for Gray

    Gray::at

    fn Gray::at(self : Gray, x : Int, y : Int) -> &
    Color

    Gray::get

    #alias("_[_]")
    fn Gray::get(self : Gray, p : Point) -> &
    Color

    Gray::gray_at

    fn Gray::gray_at(self : Gray, x : Int, y : Int) ->
    Gray

    Gray::new

    fn Gray::new(r : Rectangle) -> Gray raise SizeError

    Gray::new returns a new [Gray] image with the given bounds.

    Gray::new_empty

    fn Gray::new_empty() -> Gray

    Gray::pix_offset

    fn Gray::pix_offset(self : Gray, x : Int, y : Int) -> Int

    Gray::rgba64_at

    fn Gray::rgba64_at(self : Gray, x : Int, y : Int) ->
    RGBA64

    Gray::set

    fn Gray::set(self : Gray, x : Int, y : Int, c : &
    Color
    ) -> Unit

    Gray::set_gray

    fn Gray::set_gray(self : Gray, x : Int, y : Int, c :
    Gray
    ) -> Unit

    Gray::set_point

    #alias("_[_]=_")
    fn Gray::set_point(self : Gray, p : Point, c : &
    Color
    ) -> Unit

    Gray::set_rgba64

    fn Gray::set_rgba64(self : Gray, x : Int, y : Int, c :
    RGBA64
    ) -> Unit

    Gray16

    pub(all) struct Gray16 {
    pix :
    Slice
    [Byte]
    stride : Int
    rect : Rectangle
    }

    Gray16 is an in-memory image whose At method returns [color.Gray16] values.
    impl Image for Gray16

    Gray16::at

    fn Gray16::at(self : Gray16, x : Int, y : Int) -> &
    Color

    Gray16::get

    #alias("_[_]")
    fn Gray16::get(self : Gray16, p : Point) -> &
    Color

    Gray16::gray16_at

    fn Gray16::gray16_at(self : Gray16, x : Int, y : Int) ->
    Gray16

    Gray16::new

    fn Gray16::new(r : Rectangle) -> Gray16 raise SizeError

    Gray16::new returns a new [Gray16] image with the given bounds.

    Gray16::new_empty

    fn Gray16::new_empty() -> Gray16

    Gray16::pix_offset

    fn Gray16::pix_offset(self : Gray16, x : Int, y : Int) -> Int

    Gray16::rgba64_at

    fn Gray16::rgba64_at(self : Gray16, x : Int, y : Int) ->
    RGBA64

    Gray16::set

    fn Gray16::set(self : Gray16, x : Int, y : Int, c : &
    Color
    ) -> Unit

    Gray16::set_gray16

    fn Gray16::set_gray16(self : Gray16, x : Int, y : Int, c :
    Gray16
    ) -> Unit

    Gray16::set_point

    #alias("_[_]=_")
    fn Gray16::set_point(self : Gray16, p : Point, c : &
    Color
    ) -> Unit

    Gray16::set_rgba64

    fn Gray16::set_rgba64(self : Gray16, x : Int, y : Int, c :
    RGBA64
    ) -> Unit

    NRGBA

    pub(all) struct NRGBA {
    pix :
    Slice
    [Byte]
    stride : Int
    rect : Rectangle
    }

    NRGBA is an in-memory image whose At method returns [color.NRGBA] values.
    impl Image for NRGBA

    NRGBA::at

    fn NRGBA::at(self : NRGBA, x : Int, y : Int) -> &
    Color

    NRGBA::get

    #alias("_[_]")
    fn NRGBA::get(self : NRGBA, p : Point) -> &
    Color

    NRGBA::new

    fn NRGBA::new(r : Rectangle) -> NRGBA raise SizeError

    NRGBA::new returns a new [NRGBA] image with the given bounds.

    NRGBA::new_empty

    fn NRGBA::new_empty() -> NRGBA

    NRGBA::nrgba_at

    fn NRGBA::nrgba_at(self : NRGBA, x : Int, y : Int) ->
    NRGBA

    NRGBA::pix_offset

    fn NRGBA::pix_offset(self : NRGBA, x : Int, y : Int) -> Int

    NRGBA::rgba64_at

    fn NRGBA::rgba64_at(self : NRGBA, x : Int, y : Int) ->
    RGBA64

    NRGBA::set

    fn NRGBA::set(self : NRGBA, x : Int, y : Int, c : &
    Color
    ) -> Unit

    NRGBA::set_nrgba

    fn NRGBA::set_nrgba(self : NRGBA, x : Int, y : Int, c :
    NRGBA
    ) -> Unit

    NRGBA::set_point

    #alias("_[_]=_")
    fn NRGBA::set_point(self : NRGBA, p : Point, c : &
    Color
    ) -> Unit

    NRGBA::set_rgba64

    fn NRGBA::set_rgba64(self : NRGBA, x : Int, y : Int, c :
    RGBA64
    ) -> Unit

    NRGBA64

    pub(all) struct NRGBA64 {
    pix :
    Slice
    [Byte]
    stride : Int
    rect : Rectangle
    }

    NRGBA64 is an in-memory image whose At method returns [color.NRGBA64] values.
    impl Image for NRGBA64

    NRGBA64::at

    fn NRGBA64::at(self : NRGBA64, x : Int, y : Int) -> &
    Color

    NRGBA64::get

    #alias("_[_]")
    fn NRGBA64::get(self : NRGBA64, p : Point) -> &
    Color

    NRGBA64::new

    fn NRGBA64::new(r : Rectangle) -> NRGBA64 raise SizeError

    NRGBA64::new returns a new [NRGBA64] image with the given bounds.

    NRGBA64::new_empty

    fn NRGBA64::new_empty() -> NRGBA64

    NRGBA64::nrgba64_at

    fn NRGBA64::nrgba64_at(self : NRGBA64, x : Int, y : Int) ->
    NRGBA64

    NRGBA64::pix_offset

    fn NRGBA64::pix_offset(self : NRGBA64, x : Int, y : Int) -> Int

    NRGBA64::rgba64_at

    fn NRGBA64::rgba64_at(self : NRGBA64, x : Int, y : Int) ->
    RGBA64

    NRGBA64::set

    fn NRGBA64::set(self : NRGBA64, x : Int, y : Int, c : &
    Color
    ) -> Unit

    NRGBA64::set_nrgba64

    fn NRGBA64::set_nrgba64(self : NRGBA64, x : Int, y : Int, c :
    NRGBA64
    ) -> Unit

    NRGBA64::set_point

    #alias("_[_]=_")
    fn NRGBA64::set_point(self : NRGBA64, p : Point, c : &
    Color
    ) -> Unit

    NRGBA64::set_rgba64

    fn NRGBA64::set_rgba64(self : NRGBA64, x : Int, y : Int, c :
    RGBA64
    ) -> Unit

    Paletted

    pub(all) struct Paletted {
    pix :
    Slice
    [Byte]
    stride : Int
    rect : Rectangle
    palette :
    Palette

    }

    Paletted is an in-memory image of Byte indices into a given palette.
    impl Image for Paletted

    Paletted::at

    fn Paletted::at(self : Paletted, x : Int, y : Int) -> &
    Color

    Paletted::get

    #alias("_[_]")
    fn Paletted::get(self : Paletted, p : Point) -> &
    Color

    Paletted::new

    Paletted::new returns a new [Paletted] image with the given width, height and palette.

    Paletted::new_empty

    fn Paletted::new_empty() -> Paletted

    Paletted::pix_offset

    fn Paletted::pix_offset(self : Paletted, x : Int, y : Int) -> Int

    Paletted::rgba64_at

    fn Paletted::rgba64_at(self : Paletted, x : Int, y : Int) ->
    RGBA64

    Paletted::set

    fn Paletted::set(self : Paletted, x : Int, y : Int, c : &
    Color
    ) -> Unit

    Paletted::set_color_index

    fn Paletted::set_color_index(self : Paletted, x : Int, y : Int, index : Byte) -> Unit

    Paletted::set_point

    #alias("_[_]=_")
    fn Paletted::set_point(self : Paletted, p : Point, c : &
    Color
    ) -> Unit

    Paletted::set_rgba64

    fn Paletted::set_rgba64(self : Paletted, x : Int, y : Int, c :
    RGBA64
    ) -> Unit

    Point

    pub(all) struct Point {
    x : Int
    y : Int
    } derive(Eq)

    A Point is an X, Y coordinate pair. The axes increase right and down.
    impl Add for Point
    impl Show for Point
    impl Sub for Point

    Point::div

    fn Point::div(self : Point, k : Int) -> Point

    div returns the vector p/k.

    Point::is_in

    fn Point::is_in(self : Point, r : Rectangle) -> Bool

    is_in reports whether p is in r.

    Point::mod

    fn Point::mod(self : Point, r : Rectangle) -> Point

    mod returns the point q in r such that p.X-q.X is a multiple of r's width and p.Y-q.Y is a multiple of r's height.

    Point::mul

    fn Point::mul(self : Point, k : Int) -> Point

    mul returns the vector p*k.

    Point::to_string

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

    to_string returns a string representation of p like "(3,4)".

    RGBA

    pub(all) struct RGBA {
    pix :
    Slice
    [Byte]
    stride : Int
    rect : Rectangle
    }

    RGBA is an in-memory image whose At method returns [color.RGBA] values.
    impl Image for RGBA

    RGBA::at

    fn RGBA::at(self : RGBA, x : Int, y : Int) -> &
    Color

    RGBA::get

    #alias("_[_]")
    fn RGBA::get(self : RGBA, p : Point) -> &
    Color

    RGBA::new

    fn RGBA::new(r : Rectangle) -> RGBA raise SizeError

    RGBA::new returns a new [RGBA] image with the given bounds.

    RGBA::new_empty

    fn RGBA::new_empty() -> RGBA

    RGBA::pix_offset

    fn RGBA::pix_offset(self : RGBA, x : Int, y : Int) -> Int

    RGBA::rgba64_at

    fn RGBA::rgba64_at(self : RGBA, x : Int, y : Int) ->
    RGBA64

    RGBA::rgba_at

    fn RGBA::rgba_at(self : RGBA, x : Int, y : Int) ->
    RGBA

    RGBA::set

    fn RGBA::set(self : RGBA, x : Int, y : Int, c : &
    Color
    ) -> Unit

    RGBA::set_point

    #alias("_[_]=_")
    fn RGBA::set_point(self : RGBA, p : Point, c : &
    Color
    ) -> Unit

    RGBA::set_rgba

    fn RGBA::set_rgba(self : RGBA, x : Int, y : Int, c :
    RGBA
    ) -> Unit

    RGBA::set_rgba64

    fn RGBA::set_rgba64(self : RGBA, x : Int, y : Int, c :
    RGBA64
    ) -> Unit

    RGBA64

    pub(all) struct RGBA64 {
    pix :
    Slice
    [Byte]
    stride : Int
    rect : Rectangle
    }

    RGBA64 is an in-memory image whose At method returns [color.RGBA64] values.
    impl Image for RGBA64

    RGBA64::at

    fn RGBA64::at(self : RGBA64, x : Int, y : Int) -> &
    Color

    RGBA64::get

    #alias("_[_]")
    fn RGBA64::get(self : RGBA64, p : Point) -> &
    Color

    RGBA64::new

    fn RGBA64::new(r : Rectangle) -> RGBA64 raise SizeError

    RGBA64::new returns a new [RGBA64] image with the given bounds.

    RGBA64::new_empty

    fn RGBA64::new_empty() -> RGBA64

    RGBA64::pix_offset

    fn RGBA64::pix_offset(self : RGBA64, x : Int, y : Int) -> Int

    RGBA64::rgba64_at

    fn RGBA64::rgba64_at(self : RGBA64, x : Int, y : Int) ->
    RGBA64

    RGBA64::set

    fn RGBA64::set(self : RGBA64, x : Int, y : Int, c : &
    Color
    ) -> Unit

    RGBA64::set_point

    #alias("_[_]=_")
    fn RGBA64::set_point(self : RGBA64, p : Point, c : &
    Color
    ) -> Unit

    RGBA64::set_rgba64

    fn RGBA64::set_rgba64(self : RGBA64, x : Int, y : Int, c :
    RGBA64
    ) -> Unit

    Rectangle

    pub(all) struct Rectangle {
    min : Point
    max : Point
    }

    A Rectangle contains the points with Min.X <= X < Max.X, Min.Y <= Y < Max.Y. It is well-formed if Min.X <= Max.X and likewise for Y. Points are always

    A Rectangle is also an [Image] whose bounds are the rectangle itself. At returns color.Opaque for points in the rectangle and color.Transparent otherwise.
    impl Image for Rectangle
    impl Eq for Rectangle
    impl Show for Rectangle

    Rectangle::add

    fn Rectangle::add(self : Rectangle, p : Point) -> Rectangle

    add returns the rectangle r translated by p.

    Rectangle::canon

    fn Rectangle::canon(self : Rectangle) -> Rectangle

    canon returns the canonical version of r. The returned rectangle has minimum and maximum coordinates swapped if necessary so that it is well-formed.

    Rectangle::copy

    fn Rectangle::copy(self : Rectangle) -> Rectangle

    copy makes a copy of the provided rectangle without changing it.

    Rectangle::dx

    fn Rectangle::dx(self : Rectangle) -> Int

    dx returns r's width.

    Rectangle::dy

    fn Rectangle::dy(self : Rectangle) -> Int

    dy returns r's height.

    Rectangle::empty

    fn Rectangle::empty(self : Rectangle) -> Bool

    empty reports whether the rectangle contains no points.

    Rectangle::inset

    fn Rectangle::inset(self : Rectangle, n : Int) -> Rectangle

    inset returns the rectangle r inset by n, which may be negative. If either of r's dimensions is less than 2*n then an empty rectangle near the center of r will be returned.

    Rectangle::intersect

    fn Rectangle::intersect(self : Rectangle, s : Rectangle) -> Rectangle

    intersect returns the largest rectangle contained by both r and s. If the two rectangles do not overlap then the zero rectangle will be returned.

    Rectangle::is_in

    fn Rectangle::is_in(self : Rectangle, s : Rectangle) -> Bool

    is_in reports whether every point in r is in s.

    Rectangle::new

    fn Rectangle::new() -> Rectangle

    Rectangle::new returns an empty (all zeros) rectangle.

    Rectangle::overlaps

    fn Rectangle::overlaps(self : Rectangle, s : Rectangle) -> Bool

    overlaps reports whether r and s have a non-empty intersection.

    Rectangle::rgba64_at

    fn Rectangle::rgba64_at(self : Rectangle, x : Int, y : Int) ->
    RGBA64

    rgba64_at implements the [RGBA64Image] trait.

    Rectangle::size

    fn Rectangle::size(self : Rectangle) -> Point

    size returns r's width and height.

    Rectangle::sub

    fn Rectangle::sub(self : Rectangle, p : Point) -> Rectangle

    sub returns the rectangle r translated by -p.

    Rectangle::to_string

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

    to_string returns a string representation of r like "(3,4)-(6,5)".

    Rectangle::union

    fn Rectangle::union(self : Rectangle, s : Rectangle) -> Rectangle

    union returns the smallest rectangle that contains both r and s.

    YCbCr

    pub(all) struct YCbCr {
    y :
    Slice
    [Byte]
    cb :
    Slice
    [Byte]
    cr :
    Slice
    [Byte]
    y_stride : Int
    c_stride : Int
    subsampling : YCbCrSubsampling
    rect : Rectangle
    }

    YCbCr is an in-memory image of Y'CbCr colors.
    impl Image for YCbCr

    YCbCr::new

    fn YCbCr::new(r : Rectangle, subsampling : YCbCrSubsampling) -> YCbCr raise SizeError

    YCbCr::pix_offset

    fn YCbCr::pix_offset(self : YCbCr, x : Int, y : Int) -> Int

    YCbCrSubsampling

    pub(all) enum YCbCrSubsampling {
    YCbCrSubsampling444
    YCbCrSubsampling422
    YCbCrSubsampling420
    YCbCrSubsampling440
    YCbCrSubsampling411
    YCbCrSubsampling410
    } derive(Eq)

    YCbCrSubsampling represents the chroma subsampling ratio.

    add2_non_neg

    fn add2_non_neg(x : Int, y : Int) -> Int

    add2_non_neg returns (x + y), unless at least one argument is negative or if the computation overflows the int type, in which case it returns -1.

    mul3_non_neg

    fn mul3_non_neg(x : Int, y : Int, z : Int) -> Int

    mul3_non_neg returns (x * y * z), unless at least one argument is negative or if the computation overflows the int type, in which case it returns -1.
    fn pt(x : Int, y : Int) -> Point

    pt is shorthand for [Point]{X, Y}.

    rect

    fn rect(x0 : Int, y0 : Int, x1 : Int, y1 : Int) -> Rectangle

    rect is shorthand for [Rectangle]{Pt(x0, y0), [Pt](x1, y1)}. The returned rectangle has minimum and maximum coordinates swapped if necessary so that it is well-formed.