Skip to main content

39  img standard library

The img module provides a pure Edva lossless 2D Image buffer (contiguous RGBA8 pixels) along with encoders and decoders for the QOI (Quite OK Image) and PNG (RFC 2083) image formats. The PNG decoder supports all five RFC 2083 scanline filters (None, Sub, Up, Average, and Paeth) and 8-bit Grayscale (0), RGB (2), Grayscale+Alpha (4), and RGBA (6) color types.

#use img
#use io
#use str

im = img::new_rgba(8, 8, 255, 128, 0, 255)
png = img::encode_png(im)
qoi = img::encode_qoi(im)
io::out $ "PNG bytes: " + str::from_int(?png) + ", QOI bytes: " + str::from_int(?qoi)

39.1 Types & function catalog

Symbol / Function Signature Description
img::Image (width: Int, height: Int, pixels: String) 2D RGBA8 image buffer (width * height * 4 bytes)
img::new_rgba(w, h, r, g, b, a) Int, Int, Int, Int, Int, Int => Image Creates a solid-color w x h RGBA8 image with bytes in 0..255
img::from_fn(w, h, fn) Int, Int, (Int, Int => (Int, Int, Int, Int)) => Image Rasterizes a pixel coordinate function (x, y => (r, g, b, a))
img::from_sampler(w, h, sampler) Int, Int, (Float, Float => color::Color) => Image Rasterizes a normalized [0, 1]^2 UV color sampler into an Image
img::get_rgba(im, x, y) Image, Int, Int => (Int, Int, Int, Int) Reads pixel (x, y) as (r, g, b, a) in 0..255 (clamped)
img::get_color(im, x, y) Image, Int, Int => color::Color Reads pixel (x, y) as a normalized color::Color
img::encode_qoi(im) Image => String Encodes an Image into the lossless QOI binary format
img::decode_qoi(data) String => < Image | Error > Decodes a QOI byte string into an RGBA8 Image
img::encode_png(im) Image => String Encodes an Image into an RFC 2083 RGBA8 PNG byte string
img::decode_png(data) String => < Image | Error > Decodes an RFC 2083 PNG byte string into an RGBA8 Image
img::decode(data) String => < Image | Error > Auto-detects PNG (\x89PNG) or QOI (qoif) magic header and decodes
img::load(path) String => < Image | Error > Reads and decodes a PNG or QOI image file from disk
img::save(path, im) String, Image => <> Writes im to path (QOI if .qoi extension, otherwise PNG)