Skip to content

image.decoders.webp #

============================================================================Container / chunk constants

fn decode #

fn decode(data []u8) !common.Image

decode decodes a WEBP file into a common.Image (RGBA).

fn decode_animation #

fn decode_animation(data []u8) !WebPAnimation

decode_animation decodes an animated WEBP into fully composited frames. The caller owns each frame's rgba buffer and the frames array and must free them; the decoder's arena is released before returning.

fn decode_with #

fn decode_with(data []u8, opts Options) !common.Image

decode_with decodes a WEBP file and applies the output geometry in opts (crop first, then scale) before producing the RGBA image.

fn new_webp_player #

fn new_webp_player(data []u8) !&WebpPlayer

new_webp_player decodes and composites every frame up front and rewinds the cursor before frame 0.

struct Options #

struct Options {
pub:
	scale_w int // 0 = preserve aspect ratio
	scale_h int // 0 = preserve aspect ratio
	crop    bool
	crop_x  int
	crop_y  int
	crop_w  int
	crop_h  int
}

Options controls the output geometry applied after decoding.

struct WebPAnimation #

struct WebPAnimation {
pub:
	frames     []WebPFrame
	loop_count int
	width      int
	height     int
	bg_r       int
	bg_g       int
	bg_b       int
	bg_a       int
}

WebPAnimation is a decoded, composited animation ready for a viewer to play: full-canvas frames plus loop, timing, and background helpers that mirror the GIF animation API.

fn (WebPAnimation) frame_count #

fn (a &WebPAnimation) frame_count() int

fn (WebPAnimation) total_duration_ms #

fn (a &WebPAnimation) total_duration_ms() int

fn (WebPAnimation) is_animated #

fn (a &WebPAnimation) is_animated() bool

fn (WebPAnimation) frame_index_at #

fn (a &WebPAnimation) frame_index_at(elapsed_ms int) int

frame_index_at maps an elapsed time to the frame to display. loop_count 0 wraps forever; a finite loop clamps to the last frame at the end.

struct WebPFrame #

struct WebPFrame {
pub:
	rgba         []u8
	width        int
	height       int
	duration_ms  int
	dispose_bg   bool
	blend        bool
	x            int
	y            int
	frame_width  int
	frame_height int
}

WebPFrame is one fully composited animation frame. The rgba buffer is the complete canvas after drawing this frame; the caller owns it and must free it (same rule as common.Image.rgba).

struct WebpPlayer #

struct WebpPlayer {
mut:
	anim  WebPAnimation
	index int
}

WebpPlayer steps through a fully composited animated WEBP one frame at a time, mirroring the viewer-facing API of gif.GifPlayer. Unlike the GIF player (which composites incrementally), WEBP frames are already composited by decode_animation, so the player only owns the frame buffers and a cursor.

fn (WebpPlayer) frame_count #

fn (p &WebpPlayer) frame_count() int

frame_count returns the number of frames in the animation.

fn (WebpPlayer) loop_count #

fn (p &WebpPlayer) loop_count() int

loop_count returns the ANIM loop count (0 = forever, >=1 finite).

fn (WebpPlayer) delay_ms #

fn (p &WebpPlayer) delay_ms(i int) int

delay_ms returns the display duration of frame i in milliseconds.

fn (WebpPlayer) canvas_width #

fn (p &WebpPlayer) canvas_width() int

canvas_width returns the animation canvas width.

fn (WebpPlayer) canvas_height #

fn (p &WebpPlayer) canvas_height() int

canvas_height returns the animation canvas height.

fn (WebpPlayer) reset #

fn (mut p WebpPlayer) reset()

reset rewinds the cursor so the next step() renders frame 0. The frames are pre-composited, so there is no canvas to clear.

fn (WebpPlayer) step #

fn (mut p WebpPlayer) step() ![]u8

step returns the fully composited RGBA of the next frame in sequence. The buffer is owned by the player and stays valid until free(); the caller must copy it (e.g. upload to a texture) if it needs to outlive the player.

fn (WebpPlayer) free #

fn (mut p WebpPlayer) free()

free releases every frame buffer and the frames array, mirroring gif.GifPlayer.free (the player struct itself is freed by the caller).