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
impl Encoder
sourcepub fn new() -> Self
pub fn new() -> Self
Create a new encoder with default settings
§Defaults
max_threads
->1
quantizer
->0
quantizer_alpha
->0
speed
->10
sourcepub fn max_threads(&self) -> usize
pub fn max_threads(&self) -> usize
Get the maximum allowed number of threads this Encoder
can use
sourcepub fn set_max_threads(&mut self, max_threads: usize) -> &mut Self
pub fn set_max_threads(&mut self, max_threads: usize) -> &mut Self
Set the maximum allowed number of threads this Encoder
can use
sourcepub fn set_quality(&mut self, quality: u8) -> &mut Self
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 quality0
- lowest quality
sourcepub fn alpha_quality(&self) -> u8
pub fn alpha_quality(&self) -> u8
Get quality of the alpha channel
sourcepub fn set_alpha_quality(&mut self, alpha_quality: u8) -> &mut Self
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 quality0
- lowest quality
sourcepub fn set_speed(&mut self, speed: u8) -> &mut Self
pub fn set_speed(&mut self, speed: u8) -> &mut Self
Set the speed of this Encoder
Must be between 0 and 10.
10
- fastest0
- slowest
sourcepub fn set_timescale(&mut self, timescale: u64) -> &mut Self
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
.
sourcepub fn encode(&self, image: &AvifImage) -> Result<AvifData<'static>, Error>
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
.