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>
impl Picture<'static>
Sourcepub fn import(
rows: PixelRows<'_>,
form: SampleForm,
) -> Result<Self, EncodeError>
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>
impl<'a> Picture<'a>
Sourcepub fn from_argb(
argb: &'a mut [u32],
width: u32,
height: u32,
stride: usize,
) -> Result<Self, EncodeError>
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);Sourcepub fn from_yuva(
width: u32,
height: u32,
planes: YuvaPlanes<'a>,
) -> Result<Self, EncodeError>
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 ");Sourcepub const fn width(&self) -> u32
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);Sourcepub const fn height(&self) -> u32
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);Sourcepub fn owned_sample_bytes(&self) -> usize
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);