Struct libavif::Encoder

source ·
pub struct Encoder { /* private fields */ }
Expand description

AVIF image encoder

§Encoding a single image

let luma_data = fs::read("luma.raw")?;
let image = AvifImage::from_luma8(128, 128, &luma_data)?;
let encoder = Encoder::new();
let data = encoder.encode(&image)?;
fs::write("luma.avif", &*data)?;

§Encoding an image sequence

let mut encoder = Encoder::new();
// Set timescale to 60Hz...
encoder.set_timescale(60);
for i in 0..60 {
    let luma_data = fs::read(&format!("luma{}.raw", i))?;
    let image = AvifImage::from_luma8(128, 128, &luma_data)?;
    // ...so we can use 1 as the duration.
    // The duration of a single frame is now 1/60 s.
    // So the framerate is 60fps
    encoder.add_image(&image, 1, Default::default())?;
}
let data = encoder.finish()?;
fs::write("luma_animation.avif", &*data)?;

Implementations§

source§

impl Encoder

source

pub fn new() -> Self

Create a new encoder with default settings

§Defaults
  • max_threads -> 1
  • quantizer -> 0
  • quantizer_alpha -> 0
  • speed -> 10
source

pub fn max_threads(&self) -> usize

Get the maximum allowed number of threads this Encoder can use

source

pub fn set_max_threads(&mut self, max_threads: usize) -> &mut Self

Set the maximum allowed number of threads this Encoder can use

source

pub fn quality(&self) -> u8

Get quality of the YUV channels

source

pub fn set_quality(&mut self, quality: u8) -> &mut Self

Set the quality for the YUV channels

Must be between 0 and 100.

  • 100 - lossless quality
  • 0 - lowest quality
source

pub fn alpha_quality(&self) -> u8

Get quality of the alpha channel

source

pub fn set_alpha_quality(&mut self, alpha_quality: u8) -> &mut Self

Set the quality for the alpha channel

Must be between 0 and 100.

  • 100 - lossless quality
  • 0 - lowest quality
source

pub fn speed(&self) -> u8

Get the speed of this Encoder

source

pub fn set_speed(&mut self, speed: u8) -> &mut Self

Set the speed of this Encoder

Must be between 0 and 10.

  • 10 - fastest
  • 0 - slowest
source

pub fn timescale(&self) -> u64

Get the timescale of this Encoder in Hz (1/s)

source

pub fn set_timescale(&mut self, timescale: u64) -> &mut Self

Set the timescale of this Encoder in Hz (1/s)

The duration of an image in seconds is duration_in_timescales / timescale [s].

For example, an image with a duration of 1 added to an Encoder with a timescale of 60 Hz would mean the image has a duration of 1/60 s.

source

pub fn encode(&self, image: &AvifImage) -> Result<AvifData<'static>, Error>

Encode an AvifImage using the settings from this Encoder

Calling this function is the same as calling add_image with AddImageFlags::SINGLE and calling finish.

source

pub fn add_image( &self, image: &AvifImage, duration_in_timescales: u64, flags: AddImageFlags ) -> Result<(), Error>

Add an AvifImage to this Encoder.

source

pub fn finish(&self) -> Result<AvifData<'static>, Error>

Finish encoding an image or an animation.

You only need to call this function if you used add_image.

Trait Implementations§

source§

impl Default for Encoder

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Drop for Encoder

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.