Skip to main content

Config

Struct Config 

Source
pub struct Config { /* private fields */ }
Expand description

Compression parameters: a WebPConfig with typed setters.

The setters store their value in the matching C field and never fail; Config::validate, which every encoding calls first, reports a value that is out of range. Config::raw_mut reaches the fields without a setter (segments, filters, passes, partitions and the like).

§Examples

use webp::api::encode::{AlphaFilter, Config, ImageHint, Preset};

let lossy = Config::preset(Preset::Picture, 82.0)?
    .method(6)
    .alpha_quality(90)
    .alpha_filter(AlphaFilter::Best)
    .sharp_yuv(true);
lossy.validate()?;

let lossless = Config::new().lossless_level(9)?.exact(true).image_hint(ImageHint::Photo);
assert_eq!(lossless.as_raw().lossless, 1);

Implementations§

Source§

impl Config

Source

pub fn new() -> Self

The default configuration: the default preset at quality 75, lossy (C: WebPConfigInit).

§Examples
use webp::api::encode::Config;

let config = Config::new();
assert_eq!((config.as_raw().quality, config.as_raw().method), (75.0, 4));
Source

pub fn preset(preset: Preset, quality: f32) -> Result<Self, EncodeError>

The configuration of preset at quality (C: WebPConfigPreset).

§Errors

EncodeError::InvalidParameter when quality is not finite or outside 0 to 100.

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

let config = Config::preset(Preset::Text, 90.0)?;
assert_eq!(config.as_raw().quality, 90.0);
assert_eq!(
    Config::preset(Preset::Text, 100.5),
    Err(EncodeError::InvalidParameter { name: "quality" })
);
Source

pub fn lossless_level(self, level: u8) -> Result<Self, EncodeError>

Switches to lossless encoding with the method and quality of level, from 0 (fastest) to 9 (smallest), as cwebp -z does (C: WebPConfigLosslessPreset).

§Errors

EncodeError::InvalidParameter when level is above 9.

§Examples
use webp::api::encode::Config;

let config = Config::new().lossless_level(6)?;
assert_eq!((config.as_raw().lossless, config.as_raw().method), (1, 4));
assert!(Config::new().lossless_level(10).is_err());
Source

pub fn lossless(self, on: bool) -> Self

Lossless (true) or lossy (false, the default) encoding (C: lossless).

§Examples
use webp::api::encode::Config;

assert_eq!(Config::new().lossless(true).as_raw().lossless, 1);
Source

pub fn quality(self, quality: f32) -> Self

The quality, 0 to 100 (C: quality). Lossy: from small and poor to large and good. Lossless: from fast to slow and small.

§Examples
use webp::api::encode::Config;

assert_eq!(Config::new().quality(30.0).as_raw().quality, 30.0);
Source

pub fn method(self, method: u8) -> Self

The trade-off between speed and size, 0 (fast) to 6 (slow, smaller) (C: method).

§Examples
use webp::api::encode::Config;

assert_eq!(Config::new().method(6).as_raw().method, 6);
Source

pub fn image_hint(self, hint: ImageHint) -> Self

A hint about the content for the lossless encoder (C: image_hint).

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

assert_eq!(Config::new().image_hint(ImageHint::Photo).as_raw().image_hint.0, 2);
Source

pub fn target_size(self, bytes: u32) -> Self

A target size in bytes for a lossy encoding, 0 for none (C: target_size). The C field is an int, so the value saturates at i32::MAX, which no picture within the dimension limit reaches.

§Examples
use webp::api::encode::Config;

let config = Config::new().target_size(20_000);
assert_eq!(config.as_raw().target_size, 20_000);
assert_eq!(Config::new().target_size(u32::MAX).as_raw().target_size, i32::MAX);
Source

pub fn target_psnr(self, db: f32) -> Self

A target PSNR in dB for a lossy encoding, 0 for none (C: target_PSNR); it takes precedence over Config::target_size.

§Examples
use webp::api::encode::Config;

assert_eq!(Config::new().target_psnr(42.0).as_raw().target_psnr, 42.0);
Source

pub fn alpha_compression(self, on: bool) -> Self

Lossless compression of the alpha plane in a lossy encoding (true, the default) or no compression (C: alpha_compression).

§Examples
use webp::api::encode::Config;

assert_eq!(Config::new().alpha_compression(false).as_raw().alpha_compression, 0);
Source

pub fn alpha_filter(self, filter: AlphaFilter) -> Self

The predictive filter of the alpha plane (C: alpha_filtering).

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

assert_eq!(Config::new().alpha_filter(AlphaFilter::None).as_raw().alpha_filtering, 0);
Source

pub fn alpha_quality(self, quality: u8) -> Self

The quality of the alpha plane, 0 (smallest) to 100 (lossless, the default) (C: alpha_quality).

§Examples
use webp::api::encode::Config;

assert_eq!(Config::new().alpha_quality(50).as_raw().alpha_quality, 50);
Source

pub fn exact(self, on: bool) -> Self

Keeps the samples under fully transparent areas (C: exact); see the module documentation for what the encoder writes otherwise.

§Examples
use webp::api::encode::Config;

assert_eq!(Config::new().exact(true).as_raw().exact, 1);
Source

pub fn near_lossless(self, level: u8) -> Self

Near-lossless preprocessing of a lossless encoding, 0 (most loss) to 100 (off, the default) (C: near_lossless).

§Examples
use webp::api::encode::Config;

assert_eq!(Config::new().lossless(true).near_lossless(60).as_raw().near_lossless, 60);
Source

pub fn sharp_yuv(self, on: bool) -> Self

The sharp (iterative, slower) RGB-to-YUV conversion of a lossy encoding of ARGB samples (C: use_sharp_yuv).

§Examples
use webp::api::encode::Config;

assert_eq!(Config::new().sharp_yuv(true).as_raw().use_sharp_yuv, 1);
Source

pub fn multithreaded(self, on: bool) -> Self

Lets the encoder use one more thread inside the call (C: thread_level). The output depends on this setting, not on the machine; the writer and the progress hook stay on the calling thread.

§Examples
use webp::api::encode::Config;

assert_eq!(Config::new().multithreaded(true).as_raw().thread_level, 1);
Source

pub fn validate(&self) -> Result<(), EncodeError>

Checks the configuration as every encoding does before it starts: first the API’s own rules (quality finite and in 0 to 100, target_psnr finite, which C does not check), then libwebp’s validation (C: WebPValidateConfig).

§Errors

EncodeError::InvalidParameter for the API’s rules, EncodeError::InvalidConfiguration for libwebp’s.

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

assert_eq!(Config::new().validate(), Ok(()));
assert_eq!(Config::new().method(7).validate(), Err(EncodeError::InvalidConfiguration));
Source

pub const fn as_raw(&self) -> &WebPConfig

The configuration of the translation.

§Examples
use webp::api::encode::Config;

assert_eq!(Config::new().as_raw().segments, 4);
Source

pub const fn raw_mut(&mut self) -> &mut WebPConfig

The configuration of the translation, mutably, for the fields without a setter; Config::validate checks what is written.

§Examples
use webp::api::encode::Config;

let mut config = Config::new();
config.raw_mut().segments = 1;
config.raw_mut().pass = 6;
config.validate()?;

Trait Implementations§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Config

Source§

impl Debug for Config

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Config

Source§

fn default() -> Self

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

impl From<WebPConfig> for Config

Source§

fn from(raw: WebPConfig) -> Self

Wraps a configuration of the translation as it is; Config::validate checks it.

Source§

impl PartialEq for Config

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Config

Auto Trait Implementations§

§

impl Forget for Config

§

impl Freeze for Config

§

impl Move for Config

§

impl RefUnwindSafe for Config

§

impl Send for Config

§

impl Sync for Config

§

impl Unpin for Config

§

impl UnsafeUnpin for Config

§

impl UnwindSafe for Config

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.