Skip to main content

encode_avif

Function encode_avif 

Source
pub fn encode_avif(img: &DynamicImage, quality: u8) -> Result<Vec<u8>, SsgError>
Expand description

Encode a DynamicImage as AVIF bytes using ravif.

The image is first converted to RGBA8 (ravif’s most general input format — it auto-detects fully-opaque images and drops the alpha plane internally, so this isn’t wasteful for JPEG sources). The returned Vec<u8> is a self-contained AVIF file ready to write to disk.

quality is clamped to 1..=100; the speed preset is fixed at AVIF_SPEED (4 — “balanced”, private constant). Alpha channels use the same quality as colour.

§Examples

use ssg::image_plugin::encode_avif;
use image::{DynamicImage, RgbaImage};

let img = DynamicImage::ImageRgba8(RgbaImage::new(8, 8));
let bytes = encode_avif(&img, 60).unwrap();
assert!(!bytes.is_empty());

§Errors

Returns SsgError::Io wrapping the underlying ravif::Error if rav1e fails to encode (effectively a bug in ravif/rav1e — the inputs we feed are always valid).