Struct webp_animation::Encoder

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

An encoder for creating webp animation

Will take n frames as an input. WebP binary data is output at the end (wrapped into WebPData which acts as a &[u8])

Example without special configuration

use webp_animation::prelude::*;

// setup
let dimensions = (64, 32);
let bright_frame = [255, 255, 255, 255].repeat(64 * 32);
let dark_frame = [0, 0, 0, 255].repeat(64 * 32);

// init encoder
let mut encoder = Encoder::new(dimensions).unwrap();

// insert frames to specific (increasing) timestamps
for i in 0..5 {
  let rgba_data = if i % 2 == 0 { &bright_frame } else { &dark_frame };
  let frame_timestamp_ms = i * 170;

  encoder.add_frame(rgba_data, frame_timestamp_ms).unwrap();
}

// get encoded webp data
let final_timestamp_ms = 1_000;
let webp_data = encoder.finalize(final_timestamp_ms).unwrap();
// std::fs::write("my_animation.webp", webp_data);

Example with configuration

See EncodingConfig and LossyEncodingConfig for per-field explanations.

use webp_animation::prelude::*;

let mut encoder = Encoder::new_with_options((640, 480), EncoderOptions {
    kmin: 3,
    kmax: 5,
    encoding_config: Some(EncodingConfig {
        quality: 75.,
        encoding_type: EncodingType::Lossy(LossyEncodingConfig {
            segments: 2,
            alpha_compression: true,
            ..Default::default()
        }),
        ..Default::default()
    }),
    ..Default::default()
}).unwrap();

Example with per-frame configuration

use webp_animation::prelude::*;

let mut encoder = Encoder::new_with_options((640, 480), EncoderOptions {
    kmin: 3,
    kmax: 5,
    ..Default::default()
}).unwrap();

encoder.add_frame_with_config(&[0u8; 640 * 480 * 4], 0, &EncodingConfig {
    quality: 75.,
    encoding_type: EncodingType::Lossy(LossyEncodingConfig {
        segments: 2,
        alpha_compression: true,
        ..Default::default()
    }),
    ..Default::default()
}).unwrap();

Implementations§

source§

impl Encoder

source

pub fn new(dimensions: (u32, u32)) -> Result<Self, Error>

Construct a new encoder with default options for dimensions (width, height)

source

pub fn new_with_options( dimensions: (u32, u32), options: EncoderOptions ) -> Result<Self, Error>

Construct a new encoder with custom options for dimensions (width, height)

source

pub fn add_frame(&mut self, data: &[u8], timestamp_ms: i32) -> Result<(), Error>

Add a new frame to be encoded

Inputs

  • data is an array of pixels in ColorMode format set by EncoderOptions (ColorMode::Rgba by default)
  • timestamp_ms of this frame in milliseconds. Duration of a frame would be calculated as “timestamp of next frame - timestamp of this frame”. Hence, timestamps should be in non-decreasing order.
source

pub fn add_frame_with_config( &mut self, data: &[u8], timestamp_ms: i32, config: &EncodingConfig ) -> Result<(), Error>

Add a new frame to be encoded with special per-frame configuration (EncodingConfig)

See Encoder::add_frame for data and timestamp explanations

source

pub fn set_default_encoding_config( &mut self, config: EncodingConfig ) -> Result<(), Error>

Sets the default encoding config

Usually set in [EncderOptions] at constructor (Encoder::new_with_options)

source

pub fn finalize(self, timestamp_ms: i32) -> Result<WebPData, Error>

Will encode the stream and return encoded bytes in a WebPData upon success

timestamp_ms behaves as in Encoder::add_frame, and determines the duration of the last frame

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.