Expand description
§img4avif
Converts JPEG, PNG, WebP, and HEIC/HEIF images to AVIF using the
pure-Rust rav1e AV1 encoder. Designed for serverless workloads (AWS
Lambda x86_64 / aarch64) with built-in guards against memory
exhaustion and malformed input.
§Quick start
use img4avif::{Config, Converter};
let jpeg_bytes = std::fs::read("photo.jpg")?;
let config = Config::default()
.quality(9)
.speed(6)
.strip_exif(true); // default
let converter = Converter::new(config)?;
let avif_bytes = converter.convert(&jpeg_bytes)?;
std::fs::write("photo.avif", &avif_bytes)?;§Supported input formats
| Format | Extensions | Feature flag | AVIF output bit-depth |
|---|---|---|---|
| JPEG | .jpg, .jpeg | (always on) | 10-bit (ravif auto) |
| PNG (8-bit) | .png | (always on) | 10-bit (ravif auto) |
| PNG (16-bit / HDR10) | .png | (always on) | 10-bit (encode_raw_planes_10_bit) |
| WebP | .webp | (always on) | 10-bit (ravif auto) |
| HEIC / HEIF | .heic, .heif | heic-experimental | 10-bit (ravif auto) |
§HDR10
16-bit PNG files (a standard still-image HDR10 format) are decoded at
full precision and encoded as genuine 10-bit AVIF using ravif’s
encode_raw_planes_10_bit. Each 16-bit channel is scaled to 10 bits
(right-shift by 6) and then converted to YCbCr BT.601, preserving 1 024
distinct levels per channel instead of the 256 available in 8-bit output.
Note on CICP metadata: The AVIF colour primaries and transfer characteristics fields will reflect BT.601 / sRGB because ravif 0.13 hardcodes those values in the raw-planes encoder path. True HDR10 CICP metadata (BT.2020 primaries + PQ / HLG transfer) requires a future upgrade to a newer
rav1eversion.
HEIC files that carry HDR10 colour profiles are decoded by libheif when
the heic-experimental feature is enabled.
§Security model
- Input-size cap (
Config::max_input_bytes, default 100 MiB) — rejected before any bytes are decompressed. - Decompression-bomb protection (
Config::max_pixels) — the decoder allocation budget is derived frommax_pixels * 8 + 64 MiB; an image that claims huge dimensions is rejected before the pixel buffer lands in RAM. - RSS guard (
Config::memory_limit_bytes, default 512 MiB) — checked before and after decode; breaches returnError::MemoryExceeded. - No unsafe code — enforced by
#![forbid(unsafe_code)].
§Output resolution control
By default img4avif encodes images at their original resolution. Set
Config::output_resolutions to resize before encoding:
use img4avif::{Config, Converter, OutputResolution};
let src = std::fs::read("photo.jpg")?;
// Single output at 1080 px wide:
let config = Config::default()
.output_resolutions(vec![OutputResolution::Width1080]);
let avif_1080 = Converter::new(config)?.convert(&src)?;
// All three sizes in one decode pass:
let config_all = Config::default()
.output_resolutions(vec![
OutputResolution::Original,
OutputResolution::Width2560,
OutputResolution::Width1080,
]);
let outputs = Converter::new(config_all)?.convert_multi(&src)?;
for out in &outputs {
println!("{:?}: {} bytes", out.resolution, out.data.len());
}§Feature flags
| Flag | Default | Notes |
|---|---|---|
dev-logging | off | Structured pipeline logging via the log crate. Enable to get DEBUG/INFO/WARN/ERROR records from every pipeline stage. Zero cost when disabled. |
heic-experimental | off | HEIC/HEIF support via the libheif C library. Linking libheif makes the binary LGPL-encumbered. |
raw-experimental | off | Pure Rust RAW camera format support via rawloader. Unstable API. |
Re-exports§
pub use config::Config;pub use error::Error;pub use memory_guard::MemoryGuard;pub use resize::OutputResolution;
Modules§
- config
- Configuration — see
Config. - error
- Error types — see
Error. - memory_
guard - RSS memory guard — see
MemoryGuard. - metadata
- EXIF / metadata stripping utilities.
- resize
- Output resolution control and image resizing — see
OutputResolution. Output resolution control and image resizing.
Structs§
- Conversion
Output - A single AVIF output produced by
Converter::convert_multi. - Converter
- The main conversion entry-point.