Skip to main content

Config

Struct Config 

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

Decoding options and the color mode: a WebPDecoderOptions with typed setters, and the output colorspace.

The setters store their value and never fail; Config::validate, which every decoding calls first, reports a value that the API refuses, and Config::output_size checks the crop against an image.

§Examples

use webp::api::decode::{ColorMode, Config};

let config = Config::new(ColorMode::PremultipliedBgra)
    .crop(64, 32, 640, 480)
    .scale(320, 240)
    .multithreaded(true)
    .dithering(50);
config.validate()?;
assert_eq!(config.as_raw().use_cropping, 1);

Implementations§

Source§

impl Config

Source

pub fn new(mode: ColorMode) -> Self

A configuration for mode with libwebp’s default options: no crop, scale, flip or dithering, the in-loop filter and fancy upsampling on, one thread (C: WebPInitDecoderConfig).

§Examples
use webp::api::decode::{ColorMode, Config};

let config = Config::new(ColorMode::Yuva);
assert_eq!(config.mode(), ColorMode::Yuva);
assert_eq!(config.as_raw().no_fancy_upsampling, 0);
Source

pub fn crop(self, left: u32, top: u32, width: u32, height: u32) -> Self

Decodes only the rectangle of width by height pixels at left, top (C: use_cropping, crop_left, crop_top, crop_width, crop_height). left and top must be even: libwebp moves an odd lossy crop to the even position, so the API refuses odd offsets for every image. To read a rectangle at an odd position, crop one column or row earlier and skip it. The crop applies before the scale.

§Examples
use webp::api::decode::{ColorMode, Config, DecodeError};

assert!(Config::new(ColorMode::Rgb).crop(2, 4, 3, 5).validate().is_ok());
assert!(Config::new(ColorMode::Rgb).crop(3, 4, 3, 5).validate().is_err());
Source

pub fn scale(self, width: u32, height: u32) -> Self

Scales the (cropped) image to width by height pixels, each 1 to 16383 (C: use_scaling, scaled_width, scaled_height). libwebp’s rescaler disables fancy upsampling, and a scale below 3/4 in both directions skips the in-loop filter. libwebp derives a side given as 0; the API needs both, so that it can bound the output before decoding.

§Examples
use webp::api::decode::{ColorMode, Config, DecodeError};

assert!(Config::new(ColorMode::Rgb).scale(100, 50).validate().is_ok());
assert_eq!(
    Config::new(ColorMode::Rgb).scale(100, 0).validate(),
    Err(DecodeError::InvalidScale { width: 100, height: 0 })
);
Source

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

Flips the output vertically (C: flip).

§Examples
use webp::api::decode::{ColorMode, Config};

assert_eq!(Config::new(ColorMode::Rgb).flip(true).as_raw().flip, 1);
Source

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

Skips the in-loop filter of lossy images, which is faster and changes the samples (C: bypass_filtering).

§Examples
use webp::api::decode::{ColorMode, Config};

assert_eq!(Config::new(ColorMode::Rgb).bypass_filtering(true).as_raw().bypass_filtering, 1);
Source

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

Fancy upsampling of the chroma of lossy images in the RGB modes, on by default; off uses the faster point upsampler (C: the negation of no_fancy_upsampling).

§Examples
use webp::api::decode::{ColorMode, Config};

let config = Config::new(ColorMode::Rgb).fancy_upsampling(false);
assert_eq!(config.as_raw().no_fancy_upsampling, 1);
Source

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

Decodes lossy images with a second thread for filtering and output (C: use_threads); the samples do not change.

§Examples
use webp::api::decode::{ColorMode, Config};

assert_eq!(Config::new(ColorMode::Rgb).multithreaded(true).as_raw().use_threads, 1);
Source

pub fn dithering(self, strength: u8) -> Self

The strength of the dither that libwebp adds to lossy output, 0 (off, the default) to 100 (C: dithering_strength).

§Examples
use webp::api::decode::{ColorMode, Config, DecodeError};

assert!(Config::new(ColorMode::Rgb).dithering(100).validate().is_ok());
assert_eq!(
    Config::new(ColorMode::Rgb).dithering(101).validate(),
    Err(DecodeError::InvalidParameter { name: "dithering" })
);
Source

pub fn alpha_dithering(self, strength: u8) -> Self

The strength of the smoothing of quantized alpha, 0 (off, the default) to 100 (C: alpha_dithering_strength).

§Examples
use webp::api::decode::{ColorMode, Config};

let config = Config::new(ColorMode::Rgba).alpha_dithering(100);
assert_eq!(config.as_raw().alpha_dithering_strength, 100);
Source

pub const fn mode(&self) -> ColorMode

The color mode of the output.

§Examples
use webp::api::decode::{ColorMode, Config};

assert_eq!(Config::default().mode(), ColorMode::Rgb);
Source

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

Checks what does not depend on the image: a crop of at least 1 by 1 pixel at even left and top, every value at most i32::MAX; a scale of 1 to 16383 on each side; dithering strengths of at most 100; then libwebp’s WebPValidateDecoderConfig.

§Errors

DecodeError::InvalidCrop, DecodeError::InvalidScale or DecodeError::InvalidParameter for the API’s rules, and DecodeError::InvalidParam if libwebp refused the configuration after them.

§Examples
use webp::api::decode::{ColorMode, Config, DecodeError};

assert_eq!(
    Config::new(ColorMode::Rgb).crop(0, 0, 0, 8).validate(),
    Err(DecodeError::InvalidCrop { left: 0, top: 0, width: 0, height: 8 })
);
Source

pub fn output_size( &self, features: &Features, ) -> Result<(u32, u32), DecodeError>

The width and height of the output for an image with features: the crop’s size, then the scale’s, after Config::validate and a check that the crop lies inside the image (C: WebPCheckCropDimensions).

§Errors

The errors of Config::validate, and DecodeError::InvalidCrop for a crop that does not lie inside the image.

§Examples
use webp::api::decode::{ColorMode, Config, Features};

let features = Features { width: 640, height: 480, has_alpha: false, has_animation: false, format: None };
let config = Config::new(ColorMode::Rgb).crop(320, 0, 320, 480).scale(160, 240);
assert_eq!(config.output_size(&features)?, (160, 240));
assert!(Config::new(ColorMode::Rgb).crop(322, 0, 320, 480).output_size(&features).is_err());
Source

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

The options as libwebp reads them (C: WebPDecoderOptions).

§Examples
use webp::api::decode::{ColorMode, Config};

let config = Config::new(ColorMode::Rgb).crop(0, 2, 10, 20);
let raw = config.as_raw();
assert_eq!((raw.crop_top, raw.crop_width, raw.crop_height), (2, 10, 20));

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

ColorMode::Rgb without options, as WebPInitDecoderConfig leaves a configuration.

Source§

impl Eq for Config

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.