Skip to main content

Encoder

Struct Encoder 

Source
pub struct Encoder<'e> { /* private fields */ }
Expand description

One encoding: a configuration, and optionally a progress hook and statistics, applied to a Picture with Encoder::encode or Encoder::encode_to.

§Examples

use webp::api::encode::{Config, Encoder, Picture, WebPAuxStats};

let mut argb = vec![0xff40_8020_u32; 32 * 32];
let mut picture = Picture::from_argb(&mut argb, 32, 32, 32)?;
let config = Config::new().quality(60.0);
let mut stats = WebPAuxStats::default();
let mut reports = Vec::new();
let webp = Encoder::new(&config)
    .progress(|percent| {
        reports.push(percent);
        true
    })
    .stats(&mut stats)
    .encode(&mut picture)?;
assert_eq!(stats.coded_size as usize, webp.len());
assert!(!reports.is_empty());

Implementations§

Source§

impl<'e> Encoder<'e>

Source

pub const fn new(config: &'e Config) -> Self

An encoding with config, no progress hook and no statistics.

§Examples
use webp::api::encode::{Config, Encoder};

let config = Config::new();
let encoder = Encoder::new(&config);
Source

pub fn progress(self, hook: impl FnMut(i32) -> bool + 'e) -> Self

Reports progress to hook (C: WebPPicture::progress_hook). It receives libwebp’s percentages, 0 to 100, each time the value changes, on the calling thread. Returning false stops the encoding with EncodeError::UserAbort; the writer keeps what it received.

libwebp ignores the answer at two places, the end of a lossy analysis without segments (C: ResetAllMBInfo) and the statistics pass of lossy methods 0 to 2 (C: VP8EncLoop drops the result of StatLoop): the encoding then goes on and can succeed. A hook that cancels should therefore keep returning false.

§Examples
use webp::api::encode::{Config, EncodeError, Encoder, Picture};

let mut argb = vec![0xff00_0000_u32; 64 * 64];
let mut picture = Picture::from_argb(&mut argb, 64, 64, 64)?;
let config = Config::new();
let result = Encoder::new(&config).progress(|percent| percent < 50).encode(&mut picture);
assert_eq!(result, Err(EncodeError::UserAbort));
Source

pub fn stats(self, stats: &'e mut WebPAuxStats) -> Self

Fills stats with the encoding’s statistics (C: WebPPicture::stats). They are reset once the configuration and the picture pass libwebp’s validation and filled as libwebp fills them, on failure too.

§Examples
use webp::api::encode::{Config, Encoder, Picture, WebPAuxStats};

let mut argb = vec![0xff80_8080_u32; 16 * 16];
let mut picture = Picture::from_argb(&mut argb, 16, 16, 16)?;
let config = Config::new().quality(90.0);
let mut stats = WebPAuxStats::default();
let webp = Encoder::new(&config).stats(&mut stats).encode(&mut picture)?;
assert_eq!(stats.coded_size as usize, webp.len());
assert!(stats.psnr[3] > 40.0); // Y, U and V together, in dB
Source

pub fn encode(self, picture: &mut Picture<'_>) -> Result<Vec<u8>, EncodeError>

Encodes picture into memory, through libwebp’s memory writer (C: WebPEncode with WebPMemoryWrite), and returns the WebP file.

The encoder may write into the picture’s planes, as the module documentation describes, and leaves the planes of its conversions with the picture. If the progress hook panics, the panic propagates and the picture is left without planes: a later encoding of it returns EncodeError::NullParameter.

§Errors

The errors of Config::validate, and the code libwebp records: EncodeError::BadWrite when the output cannot grow, EncodeError::UserAbort from the progress hook, and the others of EncodeError.

§Examples
use webp::api::encode::{Config, Encoder, Picture, PixelLayout, PixelRows, SampleForm};

let rgb = [200_u8, 100, 50].repeat(16 * 16);
let rows = PixelRows { layout: PixelLayout::Rgb, data: &rgb, width: 16, height: 16, stride: 48 };
let mut picture = Picture::import(rows, SampleForm::Yuva)?;
let webp = Encoder::new(&Config::new()).encode(&mut picture)?;
assert_eq!(&webp[..4], b"RIFF");
Source

pub fn encode_to( self, picture: &mut Picture<'_>, writer: impl FnMut(&[u8]) -> bool, ) -> Result<(), EncodeError>

Encodes picture through writer (C: WebPEncode with WebPPicture::writer). The writer receives the WebP file in order, in chunks that may be empty, on the calling thread; returning false stops the encoding with EncodeError::BadWrite, and the bytes it accepted stay with it.

The encoder may write into the picture’s planes, as the module documentation describes, and leaves the planes of its conversions with the picture. If the writer or the progress hook panics, the panic propagates and the picture is left without planes: a later encoding of it returns EncodeError::NullParameter.

§Errors

The errors of Config::validate, and the code libwebp records: EncodeError::BadWrite from the writer, EncodeError::UserAbort from the progress hook, and the others of EncodeError.

§Examples
use webp::api::encode::{Config, EncodeError, Encoder, Picture};

let mut argb = vec![0xff12_3456_u32; 10 * 10];
let mut picture = Picture::from_argb(&mut argb, 10, 10, 10)?;
let config = Config::new();
let mut file = Vec::new();
Encoder::new(&config).encode_to(&mut picture, |chunk| {
    file.extend_from_slice(chunk);
    true
})?;
assert_eq!(&file[..4], b"RIFF");

let refused = Encoder::new(&config).encode_to(&mut picture, |_| false);
assert_eq!(refused, Err(EncodeError::BadWrite));

Trait Implementations§

Source§

impl Debug for Encoder<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'e> !RefUnwindSafe for Encoder<'e>

§

impl<'e> !Send for Encoder<'e>

§

impl<'e> !Sync for Encoder<'e>

§

impl<'e> !UnwindSafe for Encoder<'e>

§

impl<'e> Forget for Encoder<'e>

§

impl<'e> Freeze for Encoder<'e>

§

impl<'e> Move for Encoder<'e>

§

impl<'e> Unpin for Encoder<'e>

§

impl<'e> UnsafeUnpin for Encoder<'e>

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>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.