Skip to main content

Container

Struct Container 

Source
pub struct Container<'d> { /* private fields */ }
Expand description

The container of a WebP file: its metadata chunks and the canvas and animation parameters, through libwebp’s demuxer (C: WebPDemuxer).

It borrows the file’s bytes; the chunk payloads borrow the container.

§Examples

use webp::api::decode::Container;

// `file` is an extended 2 by 2 WebP file with an ICC profile and EXIF data.
let container = Container::new(&file)?;
assert_eq!((container.canvas_width(), container.canvas_height()), (2, 2));
assert_eq!(container.icc_profile(), Some(&b"profile"[..]));
assert_eq!(container.exif(), Some(&b"Exif\0\0MM"[..]));
assert_eq!(container.xmp(), None);

Implementations§

Source§

impl<'d> Container<'d>

Source

pub fn new(data: &'d [u8]) -> Result<Self, DecodeError>

Parses a complete WebP file (C: WebPDemux).

§Errors

The error of Features::parse for data that is not a WebP file or ends inside its header; then DecodeError::NotEnoughData for a file that ends early and DecodeError::BitstreamError for one that does not parse.

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

assert!(Container::new(&file)?.is_complete());
assert_eq!(Container::new(&file[..file.len() - 2]).unwrap_err(), DecodeError::NotEnoughData);
assert_eq!(Container::new(b"not a WebP file").unwrap_err(), DecodeError::BitstreamError);
Source

pub fn partial(data: &'d [u8]) -> Result<Self, DecodeError>

Parses the start of a WebP file (C: WebPDemuxPartial); the chunks and frames that the data reaches are available, which is enough for an ICCP chunk, which precedes the image data.

§Errors

DecodeError::NotEnoughData when the data ends inside the file header, DecodeError::BitstreamError when it does not parse; as for Container::new, the error of Features::parse comes first.

§Examples
use webp::api::decode::Container;

let container = Container::partial(&file[..file.len() - 2])?;
assert!(!container.is_complete());
Source

pub fn is_complete(&self) -> bool

Whether the whole file was parsed (C: WEBP_DEMUX_DONE).

§Examples
use webp::api::decode::Container;

assert!(Container::partial(&file)?.is_complete());
Source

pub fn canvas_width(&self) -> u32

The canvas width in pixels: the image’s for a simple file.

§Examples
use webp::api::decode::Container;

assert_eq!(Container::new(&file)?.canvas_width(), 3);
Source

pub fn canvas_height(&self) -> u32

The canvas height in pixels: the image’s for a simple file.

§Examples
use webp::api::decode::Container;

assert_eq!(Container::new(&file)?.canvas_height(), 2);
Source

pub fn has_alpha(&self) -> bool

Whether the file declares alpha (C: ALPHA_FLAG of WEBP_FF_FORMAT_FLAGS, which the demuxer also sets for a simple file whose image has alpha).

§Examples
use webp::api::decode::Container;

assert!(Container::new(&file)?.has_alpha());
Source

pub fn has_animation(&self) -> bool

Whether the file is an animation (C: ANIMATION_FLAG of WEBP_FF_FORMAT_FLAGS).

§Examples
use webp::api::decode::Container;

assert!(!Container::new(&file)?.has_animation());
Source

pub fn loop_count(&self) -> u32

The number of times an animation loops, 0 for forever (C: WEBP_FF_LOOP_COUNT); a still image reports 1, the demuxer’s default.

§Examples
use webp::api::decode::Container;

assert_eq!(Container::new(&file)?.loop_count(), 1);
Source

pub fn background_color(&self) -> u32

The background color of an animation as 0xAARRGGBB: the ANIM chunk’s bytes (blue, green, red, alpha) read as a little-endian u32 (C: WEBP_FF_BACKGROUND_COLOR). It is reported, not applied: the canvas of AnimationDecoder starts transparent black. A still image reports 0xFFFFFFFF.

§Examples
use webp::api::decode::Container;

assert_eq!(Container::new(&file)?.background_color(), 0xFFFF_FFFF);
Source

pub fn frame_count(&self) -> u32

The number of frames: 1 for a still image (C: WEBP_FF_FRAME_COUNT); the frames parsed so far for a partial container.

§Examples
use webp::api::decode::Container;

assert_eq!(Container::new(&file)?.frame_count(), 1);
Source

pub fn icc_profile(&self) -> Option<&[u8]>

The raw ICC profile: the payload of the first ICCP chunk, which the demuxer keeps when the VP8X flags announce it.

§Examples
use webp::api::decode::Container;

// A simple file has no metadata; readers then assume sRGB.
assert_eq!(Container::new(&file)?.icc_profile(), None);
Source

pub fn exif(&self) -> Option<&[u8]>

The raw EXIF data: the payload of the first EXIF chunk, unparsed (the orientation, if any, is in it).

§Examples
use webp::api::decode::Container;

assert_eq!(Container::new(&file)?.exif(), None);
Source

pub fn xmp(&self) -> Option<&[u8]>

The raw XMP packet: the payload of the first XMP chunk.

§Examples
use webp::api::decode::Container;

assert_eq!(Container::new(&file)?.xmp(), None);
Source

pub fn chunks(&self, fourcc: [u8; 4]) -> Chunks<'_> ⓘ

The payloads of every chunk with fourcc that the demuxer keeps (ICCP, EXIF, XMP and unknown chunks), in file order (C: WebPDemuxGetChunk and WebPDemuxNextChunk).

§Examples
use webp::api::decode::Container;

assert_eq!(Container::new(&file)?.chunks(*b"EXIF").count(), 0);

Trait Implementations§

Source§

impl Debug for Container<'_>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'d> Forget for Container<'d>

§

impl<'d> Freeze for Container<'d>

§

impl<'d> Move for Container<'d>

§

impl<'d> RefUnwindSafe for Container<'d>

§

impl<'d> Send for Container<'d>

§

impl<'d> Sync for Container<'d>

§

impl<'d> Unpin for Container<'d>

§

impl<'d> UnsafeUnpin for Container<'d>

§

impl<'d> UnwindSafe for Container<'d>

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.