image

MoonBit image: simple image representation.

image
color
moonbit
moon add gmlewis/image@0.17.11
Download zip
Author
Version
0.17.11
License
Apache-2.0
Last updated
8 days ago
Downloads
8K

Dependencies

README

#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.20260309 (f21b520 2026-03-09) ~/.moon/bin/moon moonc v0.8.3+cd28f524e (2026-03-09) ~/.moon/bin/moonc moonrun 0.1.20260309 (f21b520 2026-03-09) ~/.moon/bin/moonrun moon-pilot 0.0.1-df92511 (2026-03-09) ~/.moon/bin/moon-pilot

#
Image

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

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

opaque_(Self) -> Bool
set(Self, Int, Int, &
Color
) -> Unit
sub_image(Self, Rectangle) -> &Image
as_ycbcr(Self) -> YCbCr?
raw_data(Self) ->
Slice
[Byte]
get_bytes_per_pixel(Self) -> Int
get_stride(Self) -> Int
pix_offset(Self, Int, Int) -> Int
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 {
rgba64_at(Self, Int, Int) ->
RGBA64

color_model(Self) -> &
Model

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

}

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

#
SizeError

type SizeError

impl Eq for SizeError
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::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::op_get

fn Alpha::op_get(self : Alpha, p : Point) -> &
Color

#
Alpha::op_set

fn Alpha::op_set(self : Alpha, p : Point, c : &
Color
) -> Unit

#
Alpha::rgba64_at

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

#
Alpha::set_alpha

fn Alpha::set_alpha(self : Alpha, x : Int, y : Int, c :
Alpha
) -> 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::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::op_get

#
Alpha16::op_set

fn Alpha16::op_set(self : Alpha16, p : Point, c : &
Color
) -> Unit

#
Alpha16::rgba64_at

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

#
Alpha16::set_alpha16

fn Alpha16::set_alpha16(self : Alpha16, x : Int, y : Int, c :
Alpha16
) -> 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::cmyk_at

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

#
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::op_get

fn CMYK::op_get(self : CMYK, p : Point) -> &
Color

#
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::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::op_get

fn Gray::op_get(self : Gray, p : Point) -> &
Color

#
Gray::op_set

fn Gray::op_set(self : Gray, p : Point, c : &
Color
) -> Unit

#
Gray::rgba64_at

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

#
Gray::set_gray

fn Gray::set_gray(self : Gray, x : Int, y : Int, c :
Gray
) -> 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::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::op_get

#
Gray16::op_set

fn Gray16::op_set(self : Gray16, p : Point, c : &
Color
) -> Unit

#
Gray16::rgba64_at

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

#
Gray16::set_gray16

fn Gray16::set_gray16(self : Gray16, x : Int, y : Int, c :
Gray16
) -> 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::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::op_get

fn NRGBA::op_get(self : NRGBA, p : Point) -> &
Color

#
NRGBA::op_set

fn NRGBA::op_set(self : NRGBA, p : Point, c : &
Color
) -> Unit

#
NRGBA::rgba64_at

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

#
NRGBA::set_nrgba

fn NRGBA::set_nrgba(self : NRGBA, x : Int, y : Int, c :
NRGBA
) -> 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::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::op_get

#
NRGBA64::op_set

fn NRGBA64::op_set(self : NRGBA64, p : Point, c : &
Color
) -> Unit

#
NRGBA64::rgba64_at

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

#
NRGBA64::set_nrgba64

fn NRGBA64::set_nrgba64(self : NRGBA64, x : Int, y : Int, c :
NRGBA64
) -> 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::new

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

#
Paletted::new_empty

fn Paletted::new_empty() -> Paletted

#
Paletted::op_get

#
Paletted::op_set

fn Paletted::op_set(self : Paletted, p : Point, c : &
Color
) -> Unit

#
Paletted::rgba64_at

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

#
Paletted::set_color_index

fn Paletted::set_color_index(self : Paletted, x : Int, y : Int, index : Byte) -> 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
}

A Point is an X, Y coordinate pair. The axes increase right and down.
impl Add for Point
impl Eq 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::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::op_get

fn RGBA::op_get(self : RGBA, p : Point) -> &
Color

#
RGBA::op_set

fn RGBA::op_set(self : RGBA, p : Point, c : &
Color
) -> Unit

#
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_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::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::op_get

#
RGBA64::op_set

fn RGBA64::op_set(self : RGBA64, p : Point, c : &
Color
) -> Unit

#
RGBA64::rgba64_at

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

#
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

#
YCbCrSubsampling

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

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.

Source Files