pub struct Features {
pub width: u32,
pub height: u32,
pub has_alpha: bool,
pub has_animation: bool,
pub format: Option<Format>,
}Expand description
What a WebP file says about itself before decoding (C:
WebPBitstreamFeatures).
§Examples
use webp::api::decode::{DecodeError, Features};
let features = Features::parse(&file)?;
assert_eq!((features.width, features.height), (5, 3));
assert!(features.has_alpha && !features.has_animation);
// A prefix that ends before the dimensions asks for more data.
assert_eq!(Features::parse(&file[..10]), Err(DecodeError::NotEnoughData));Fields§
§width: u32The width in pixels: the canvas of an extended (VP8X) file.
height: u32The height in pixels: the canvas of an extended (VP8X) file.
has_alpha: boolWhether the file declares alpha (the VP8X flag, an ALPH chunk or a
lossless bitstream’s alpha bit).
has_animation: boolWhether the file is an animation; decode it with AnimationDecoder.
format: Option<Format>The compression of the image, or None when the parse did not reach
a bitstream header (libwebp reports 0, “undefined”): for an animation,
whose frames each carry their own format, and for an extended still
image probed with a prefix that ends before its image chunk. Use
has_animation to tell the two apart.
Implementations§
Source§impl Features
impl Features
Sourcepub fn parse(data: &[u8]) -> Result<Self, DecodeError>
pub fn parse(data: &[u8]) -> Result<Self, DecodeError>
Reads the features from the start of a WebP file (C:
WebPGetFeatures); a prefix is enough once it reaches the bitstream’s
header, or the VP8X chunk of an extended file.
After the VP8X chunk of an extended still image the result can carry
format: None, with the canvas as the size, until the prefix reaches
the image chunk’s header; a caller who needs the format reads more
until it is Some.
§Errors
DecodeError::NotEnoughData when the prefix ends too early, and
DecodeError::BitstreamError for a file that is not a WebP file
libwebp decodes.
§Examples
use webp::api::decode::{DecodeError, Features};
assert_eq!(Features::parse(b"RIFF"), Err(DecodeError::NotEnoughData));
assert_eq!(Features::parse(b"GIF89a, not a WebP file"), Err(DecodeError::BitstreamError));A prefix of an extended still image that ends after the VP8X chunk
gives the canvas but no format yet:
use webp::api::decode::{Features, Format};
// `file` is an extended 2 by 2 still image: a 12-byte RIFF header, an
// 18-byte `VP8X` chunk, then `ICCP`, `VP8L` and `EXIF` chunks.
let prefix = Features::parse(&file[..30])?;
assert_eq!((prefix.width, prefix.height), (2, 2));
assert_eq!((prefix.format, prefix.has_animation), (None, false));
assert_eq!(Features::parse(&file)?.format, Some(Format::Lossless));