Skip to content

avif

stable

Image format detection and raw RGBA pixel buffer manipulation for PNG, JPEG, GIF, BMP, and AVIF files.

use plugin avif::{is_avif, detect_format, image_dimensions, …}
13 functions Image
/ filter jk navigate Esc clear
Functions (13)
  1. is_avif Check if bytes are an AVIF image
  2. detect_format Detect image format from magic bytes
  3. image_dimensions Parse width and height from image bytes
  4. file_format_info Get MIME type and properties for a format
  5. create_image Create a solid-color RGBA pixel buffer
  6. get_pixel Read a pixel's RGBA values
  7. set_pixel Write a pixel's RGBA values
  8. resize_nearest Resize RGBA buffer with nearest-neighbor
  9. crop Crop a rectangular region from a buffer
  10. flip_horizontal Mirror buffer left to right
  11. flip_vertical Flip buffer upside down
  12. pixel_count Compute total pixel count for dimensions
  13. byte_length Compute expected RGBA buffer byte size

Overview

The avif plugin is a small, dependency-free toolkit for two jobs: identifying image files from their bytes, and editing raw RGBA pixel buffers. Detection functions (is_avif, detect_format, image_dimensions, file_format_info) read magic bytes and container headers for PNG, JPEG, GIF, BMP, WebP, and AVIF without decoding the whole image. The pixel functions operate on a flat width * height * 4 byte buffer where every pixel is four consecutive bytes (red, green, blue, alpha, each 0–255).

There are no handles or open resources — every function is a pure transform that takes bytes in and returns either a value or a brand-new buffer, leaving the input untouched. Reach for this plugin when you need lightweight format probing, or to build, crop, resize, and flip uncompressed RGBA images entirely in Zolo.

Common patterns

Probe an unknown file before doing anything with it:

use plugin avif::{detect_format, image_dimensions, file_format_info}

let data = fs.read_bytes("upload.bin")
let fmt = detect_format(data)
if fmt == "unknown" {
  print("not a recognized image")
} else {
  let dims = image_dimensions(data)
  let info = file_format_info(fmt)
  print("{fmt} {dims["width"]}x{dims["height"]} ({info["mime_type"]})")
}

Build a solid image, edit a pixel, then transform it:

use plugin avif::{create_image, set_pixel, flip_horizontal, get_pixel}

let img = create_image(8, 8, 0, 0, 0, 255)
let img = set_pixel(img, 8, 0, 0, 255, 255, 255, 255)
let mirrored = flip_horizontal(img, 8, 8)

let corner = get_pixel(mirrored, 8, 7, 0)
print("mirrored white corner r={corner["r"]}")

Make a centered thumbnail by cropping then downscaling:

use plugin avif::{create_image, crop, resize_nearest, byte_length}

let img = create_image(200, 200, 100, 150, 200, 255)
let square = crop(img, 200, 50, 50, 100, 100)
let thumb = resize_nearest(square, 100, 100, 32, 32)
print("thumb bytes: {byte_length(32, 32)}")

Check if bytes are an AVIF image

Checks whether bytes contain an AVIF image by inspecting the ISO BMFF ftyp box for AVIF-compatible brands (avif, avis).

use plugin avif::{is_avif, detect_format}

let data = fs.read_bytes("image.avif")
print(is_avif(data))
print(detect_format(data))

Detect image format from magic bytes

Identifies the image format from the file's magic bytes. Returns one of "png", "jpeg", "gif", "webp", "bmp", "avif", or "unknown".

use plugin avif::{detect_format}

let data = fs.read_bytes("photo.jpg")
let fmt = detect_format(data)
print("format: {fmt}")

Use the result to branch on whether the file is even an image:

use plugin avif::{detect_format}

let data = fs.read_bytes("download.dat")
if detect_format(data) == "unknown" {
  print("rejecting: not an image")
}

Parse width and height from image bytes

Parses the image header and returns a table with width, height (integers), and format (string). Supports PNG, JPEG, GIF, and BMP. Errors on unsupported or truncated files.

use plugin avif::{image_dimensions}

let data = fs.read_bytes("banner.png")
let dims = image_dimensions(data)
print("{dims["width"]}x{dims["height"]} ({dims["format"]})")

Compute the aspect ratio from the parsed dimensions:

use plugin avif::{image_dimensions}

let dims = image_dimensions(fs.read_bytes("hero.jpg"))
let ratio = dims["width"] / dims["height"]
print("aspect: {ratio}")

Get MIME type and properties for a format

Returns static metadata for the given format name. The result table has mime_type, extension, lossy (bool), and supports_alpha (bool).

use plugin avif::{file_format_info}

let info = file_format_info("avif")
print(info["mime_type"])
print(info["lossy"])
print(info["supports_alpha"])

Pick the right file extension when saving by format name:

use plugin avif::{file_format_info}

let info = file_format_info("jpeg")
print("save as .{info["extension"]}")

Create a solid-color RGBA pixel buffer

Creates a raw RGBA pixel buffer of size width * height * 4 bytes, filled with the given color. Color components default to 0 for RGB and 255 for alpha.

use plugin avif::{create_image, byte_length}

let buf = create_image(100, 100, 255, 0, 0, 255)
print("buffer size: {byte_length(100, 100)}")

let transparent = create_image(64, 64, 0, 0, 0, 0)

Omit the color arguments to get an opaque black canvas (RGB default to 0, alpha defaults to 255):

use plugin avif::{create_image, get_pixel}

let canvas = create_image(16, 16)
let px = get_pixel(canvas, 16, 0, 0)
print("default alpha: {px["a"]}")

Read a pixel's RGBA values

Reads the RGBA values of the pixel at column x, row y from a raw RGBA buffer with the given width. Returns a table with r, g, b, a integer fields (0–255).

use plugin avif::{create_image, get_pixel}

let buf = create_image(10, 10, 128, 64, 32, 255)
let px = get_pixel(buf, 10, 5, 5)
print("r={px["r"]} g={px["g"]} b={px["b"]} a={px["a"]}")

Write a pixel's RGBA values

Returns a new RGBA buffer with the pixel at (x, y) replaced by the given color. Does not modify the original buffer.

use plugin avif::{create_image, set_pixel, get_pixel}

let buf = create_image(10, 10, 0, 0, 0, 255)
let buf = set_pixel(buf, 10, 2, 3, 255, 255, 0, 255)
let px = get_pixel(buf, 10, 2, 3)
print("r={px["r"]}")

Resize RGBA buffer with nearest-neighbor

Resizes a raw RGBA buffer from src_width x src_height to dst_width x dst_height using nearest-neighbor interpolation. Fast but may look pixelated. Maximum output size is 16384 x 16384.

use plugin avif::{create_image, resize_nearest}

let src = create_image(64, 64, 200, 100, 50, 255)
let scaled = resize_nearest(src, 64, 64, 128, 128)

Downscale to a small preview, then confirm the new buffer size:

use plugin avif::{create_image, resize_nearest, byte_length}

let src = create_image(256, 256, 30, 60, 90, 255)
let preview = resize_nearest(src, 256, 256, 48, 48)
print("preview bytes: {byte_length(48, 48)}")

Crop a rectangular region from a buffer

Extracts a rectangular sub-region from a raw RGBA buffer. The region starts at (x, y) with size crop_w x crop_h.

use plugin avif::{create_image, crop}

let img = create_image(200, 200, 100, 150, 200, 255)
let thumbnail = crop(img, 200, 50, 50, 100, 100)

Mirror buffer left to right

Returns a new RGBA buffer with each row mirrored left-to-right.

use plugin avif::{create_image, flip_horizontal}

let img = create_image(64, 64, 255, 0, 0, 255)
let flipped = flip_horizontal(img, 64, 64)

Flip buffer upside down

Returns a new RGBA buffer with rows reversed (top becomes bottom).

use plugin avif::{create_image, flip_vertical}

let img = create_image(64, 64, 0, 255, 0, 255)
let flipped = flip_vertical(img, 64, 64)

Compute total pixel count for dimensions

Returns width * height — the total number of pixels for the given dimensions.

use plugin avif::{pixel_count}

print(pixel_count(1920, 1080))

Compute expected RGBA buffer byte size

Returns width * height * 4 — the expected size of a raw RGBA buffer for the given dimensions.

use plugin avif::{byte_length, create_image}

let expected = byte_length(256, 256)
let buf = create_image(256, 256, 0, 0, 0, 255)
print(expected)
enespt-br