Skip to main content

Picture

Struct Picture 

Source
pub struct Picture<'a> { /* private fields */ }
Expand description

A picture to encode: its dimensions and its samples, either planes it allocated (Picture::import) or the caller’s planes, borrowed exclusively for 'a (Picture::from_argb, Picture::from_yuva).

A picture holds no callbacks, so it is Send and Sync; the writer, the progress hook and the statistics belong to one Encoder call.

§Examples

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

// Caller-owned ARGB pixels (0xAARRGGBB), 2 by 2 with one pixel of padding.
let mut argb = vec![0xff20_4060_u32; 2 * 3];
let mut picture = Picture::from_argb(&mut argb, 2, 2, 3)?;
assert_eq!((picture.width(), picture.height()), (2, 2));
let config = Config::new().lossless(true);
let webp = Encoder::new(&config).encode(&mut picture)?;
assert_eq!(&webp[12..16], b"VP8L");

Implementations§

Source§

impl Picture<'static>

Source

pub fn import( rows: PixelRows<'_>, form: SampleForm, ) -> Result<Self, EncodeError>

Copies rows into planes that the picture allocates, in the form that form selects (C: WebPPictureImportRGB and its siblings, with use_argb). SampleForm::Yuva converts to YUV at once, and adds an alpha plane only when some pixel is not opaque.

§Errors

EncodeError::InvalidDimensions, EncodeError::InvalidStride or EncodeError::BufferTooShort for rows outside the rules of PixelRows; EncodeError::OutOfMemory when the planes cannot be allocated.

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

let rgba = [1_u8, 2, 3, 128].repeat(6);
let rows = PixelRows { layout: PixelLayout::Rgba, data: &rgba, width: 3, height: 2, stride: 12 };
let picture = Picture::import(rows, SampleForm::Yuva)?;
// Y 3 by 2, U and V 2 by 1, and alpha, since the pixels are translucent.
assert_eq!(picture.owned_sample_bytes(), 6 + 2 + 2 + 6);
Source§

impl<'a> Picture<'a>

Source

pub fn from_argb( argb: &'a mut [u32], width: u32, height: u32, stride: usize, ) -> Result<Self, EncodeError>

Borrows the caller’s ARGB pixels, 0xAARRGGBB in each u32 with straight alpha, as the picture’s samples (C: use_argb with the caller’s argb plane).

stride counts elements of the slice’s type, which for &mut [u32] are pixels, not bytes: at least width, at most i32::MAX. argb holds at least (height - 1) * stride + width pixels.

§Errors

EncodeError::InvalidDimensions, EncodeError::InvalidStride or EncodeError::BufferTooShort (with Plane::Argb).

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

let mut argb = vec![0x80ff_0000_u32; 4 * 4];
let picture = Picture::from_argb(&mut argb, 4, 4, 4)?;
assert_eq!(picture.owned_sample_bytes(), 0);
Source

pub fn from_yuva( width: u32, height: u32, planes: YuvaPlanes<'a>, ) -> Result<Self, EncodeError>

Borrows the caller’s Y, U, V and optional alpha planes as the picture’s samples, taken as they are (C: use_argb 0 with the caller’s planes, colorspace WEBP_YUV420 or, with alpha, WEBP_YUV420A). See YuvaPlanes for the stride and length rules.

§Errors

EncodeError::InvalidDimensions, EncodeError::InvalidStride or EncodeError::BufferTooShort, naming the plane.

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

let (mut y, mut u, mut v, mut a) = (vec![100_u8; 16], vec![90_u8; 4], vec![160_u8; 4], vec![255_u8; 16]);
let planes = YuvaPlanes {
    y: &mut y, y_stride: 4,
    u: &mut u, v: &mut v, uv_stride: 2,
    a: Some(&mut a), a_stride: 4,
};
let mut picture = Picture::from_yuva(4, 4, planes)?;
let webp = Encoder::new(&Config::new()).encode(&mut picture)?;
assert_eq!(&webp[12..16], b"VP8 ");
Source

pub const fn width(&self) -> u32

The width in pixels.

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

let mut argb = vec![0_u32; 6];
assert_eq!(Picture::from_argb(&mut argb, 3, 2, 3)?.width(), 3);
Source

pub const fn height(&self) -> u32

The height in pixels.

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

let mut argb = vec![0_u32; 6];
assert_eq!(Picture::from_argb(&mut argb, 3, 2, 3)?.height(), 2);
Source

pub fn owned_sample_bytes(&self) -> usize

The bytes of the samples that the picture allocated itself: the imported planes, and the planes that an encoding’s conversion added. The caller’s planes are not counted, nor is the encoder’s internal state.

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

let mut argb = vec![0xff00_ff00_u32; 4 * 4];
let mut picture = Picture::from_argb(&mut argb, 4, 4, 4)?;
assert_eq!(picture.owned_sample_bytes(), 0);
// A lossy encoding converts the ARGB pixels into YUV planes it keeps.
Encoder::new(&Config::new()).encode(&mut picture)?;
assert_eq!(picture.owned_sample_bytes(), 16 + 4 + 4);

Trait Implementations§

Source§

impl Debug for Picture<'_>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !UnwindSafe for Picture<'a>

§

impl<'a> Forget for Picture<'a>

§

impl<'a> Freeze for Picture<'a>

§

impl<'a> Move for Picture<'a>

§

impl<'a> RefUnwindSafe for Picture<'a>

§

impl<'a> Send for Picture<'a>

§

impl<'a> Sync for Picture<'a>

§

impl<'a> Unpin for Picture<'a>

§

impl<'a> UnsafeUnpin for Picture<'a>

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.