webp
stableWebP image encoding, decoding, and inspection utilities. Supports VP8, VP8L, and VP8X (extended/animated) WebP containers alongside PNG and JPEG dimension parsing.
use plugin webp::{is_webp, webp_dimensions, detect_image_format, …} Functions (10)
- is_webp Check if bytes are a valid WebP file
- webp_dimensions Extract width and height from a WebP file
- detect_image_format Detect image format from magic bytes
- png_dimensions Extract width and height from a PNG file
- jpeg_dimensions Extract width and height from a JPEG file
- encode_rgba Encode raw RGBA pixels into a WebP container
- decode_rgba Decode a VP8L WebP container back to RGBA pixels
- resize_nearest Resize an RGBA pixel buffer using nearest-neighbor
- is_animated Check if a WebP file contains animation
- metadata Extract format, alpha, animation, and dimensions metadata
Overview
webp is a dependency-free image toolkit focused on inspecting and round-tripping
raster images by reading their raw byte headers directly. Nothing here is a stateful
object: every function takes a byte buffer (the contents of an image file) and returns
either a primitive, a small table of dimensions/metadata, or a new byte buffer. That
makes it ideal for fast, allocation-light tasks like sniffing a file's true format,
extracting width/height without a full decode, or generating thumbnails.
The encode/decode pair works on a minimal lossless RIFF/WEBP/VP8L container produced by
this plugin itself, with pixels stored as raw RGBA (width * height * 4 bytes). Use
detect_image_format to route a file, the *_dimensions helpers to size PNG/JPEG/WebP
inputs cheaply, and the encode_rgba / decode_rgba / resize_nearest trio when you
need to manipulate pixel data.
Common patterns
Sniff an unknown file and size it without decoding the full image:
use plugin webp::{detect_image_format, png_dimensions, jpeg_dimensions, webp_dimensions}
let data = file.read_bytes("upload.bin")
let fmt = detect_image_format(data)
if fmt == "png" {
let d = png_dimensions(data)
print("png {d["w"]}x{d["h"]}")
} else if fmt == "jpeg" {
let d = jpeg_dimensions(data)
print("jpeg {d["w"]}x{d["h"]}")
} else if fmt == "webp" {
let d = webp_dimensions(data)
print("webp {d["width"]}x{d["height"]}")
}
Decode a WebP, shrink it to a thumbnail, and re-encode it:
use plugin webp::{decode_rgba, resize_nearest, encode_rgba}
let img = decode_rgba(file.read_bytes("photo.webp"))
let thumb = resize_nearest(img["pixels"], img["width"], img["height"], 128, 128)
let out = encode_rgba(thumb, 128, 128, 100)
file.write_bytes("thumb.webp", out)
Validate a WebP and report a one-line summary before processing it:
use plugin webp::{is_webp, metadata}
let data = file.read_bytes("sticker.webp")
if is_webp(data) {
let m = metadata(data)
print("{m["format"]} {m["width"]}x{m["height"]} anim={m["has_animation"]}")
}
Check if bytes are a valid WebP file
Returns true if the byte slice begins with the RIFF....WEBP magic header. Use this to quickly validate a file before passing it to other functions.
use plugin webp::{is_webp}
let data = file.read_bytes("image.webp")
if is_webp(data) {
print("valid WebP file")
}
Extract width and height from a WebP file
Parses the WebP bitstream header (VP8, VP8L, or VP8X chunk) to return canvas dimensions. Returns a table with width and height integer keys.
use plugin webp::{webp_dimensions}
let data = file.read_bytes("photo.webp")
let dims = webp_dimensions(data)
print("size: {dims["width"]}x{dims["height"]}")
Detect image format from magic bytes
Inspects the first few bytes of an image and returns "png", "jpeg", "gif", "webp", "bmp", or "unknown". Useful for routing files to the correct decoder without relying on file extensions.
use plugin webp::{detect_image_format}
let data = file.read_bytes("unknown.img")
let fmt = detect_image_format(data)
print("detected format: {fmt}")
Use it as a guard to reject unsupported uploads:
use plugin webp::{detect_image_format}
let fmt = detect_image_format(file.read_bytes("upload.bin"))
if fmt == "unknown" || fmt == "bmp" {
print("unsupported image: {fmt}")
}
Extract width and height from a PNG file
Reads the PNG IHDR chunk to extract width and height. Returns a table with w and h integer keys. Errors if the bytes do not begin with the PNG magic signature.
use plugin webp::{png_dimensions}
let data = file.read_bytes("icon.png")
let dims = png_dimensions(data)
print("{dims["w"]}x{dims["h"]}")
Extract width and height from a JPEG file
Scans JPEG markers to find SOF0, SOF1, or SOF2 and returns dimensions. Returns a table with w and h integer keys.
use plugin webp::{jpeg_dimensions}
let data = file.read_bytes("photo.jpg")
let dims = jpeg_dimensions(data)
print("{dims["w"]}x{dims["h"]}")
Encode raw RGBA pixels into a WebP container
Wraps raw RGBA pixel data into a minimal RIFF/WEBP/VP8L container. The pixel buffer must be exactly w * h * 4 bytes. The quality parameter is accepted but currently unused (lossless encoding only).
use plugin webp::{encode_rgba, decode_rgba}
let pixels = file.read_bytes("frame.raw")
let webp_bytes = encode_rgba(pixels, 64, 64, 100)
let result = decode_rgba(webp_bytes)
print("decoded {result["width"]}x{result["height"]}")
Generate a solid-color 2x2 swatch from scratch and write it out:
use plugin webp::{encode_rgba}
let red = [255, 0, 0, 255]
let pixels = []
for i in 0..4 {
pixels = pixels + red
}
let swatch = encode_rgba(pixels, 2, 2, 100)
file.write_bytes("red.webp", swatch)
Decode a VP8L WebP container back to RGBA pixels
Decodes a VP8L WebP container produced by encode_rgba and returns width, height, and the raw pixels bytes. Only VP8L (lossless) containers produced by this plugin are supported.
use plugin webp::{decode_rgba}
let data = file.read_bytes("frame.webp")
let img = decode_rgba(data)
print("pixels length: {#img["pixels"]}")
Resize an RGBA pixel buffer using nearest-neighbor
Performs nearest-neighbor resampling of a raw RGBA pixel buffer from src_w x src_h to dst_w x dst_h. Returns the resized pixel buffer as bytes.
use plugin webp::{decode_rgba, resize_nearest, encode_rgba}
let img = decode_rgba(file.read_bytes("photo.webp"))
let small = resize_nearest(img["pixels"], img["width"], img["height"], 128, 128)
let out = encode_rgba(small, 128, 128, 100)
file.write_bytes("thumb.webp", out)
Check if a WebP file contains animation
Returns true if the WebP file is an extended (VP8X) container with the animation flag set in the feature flags byte.
use plugin webp::{is_animated}
let data = file.read_bytes("sticker.webp")
if is_animated(data) {
print("animated WebP")
}
Extract format, alpha, animation, and dimensions metadata
Returns a comprehensive summary of a WebP file. format is one of "lossy", "lossless", or "extended". has_alpha and has_animation are booleans. width and height are integers (0 if dimensions could not be parsed).
use plugin webp::{metadata}
let data = file.read_bytes("image.webp")
let m = metadata(data)
print("format={m["format"]} alpha={m["has_alpha"]} {m["width"]}x{m["height"]}")
Branch on the parsed format to decide how to handle transparency:
use plugin webp::{metadata}
let m = metadata(file.read_bytes("logo.webp"))
if m["has_alpha"] {
print("{m["format"]} image has an alpha channel")
} else {
print("{m["format"]} image is fully opaque")
}