use std::fmt::{self, Display};
mod decoder;
mod encoder;
mod encoder_config;
mod frame;
mod webp_data;
pub use decoder::*;
pub use encoder::*;
pub use encoder_config::*;
pub use frame::*;
pub use webp_data::*;
pub mod prelude {
pub use crate::ColorMode;
pub use crate::{Decoder, DecoderOptions};
pub use crate::{Encoder, EncoderOptions, EncodingConfig, EncodingType, LossyEncodingConfig};
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum ColorMode {
Rgb,
Rgba,
Bgra,
Bgr,
}
impl ColorMode {
pub fn size(&self) -> usize {
match self {
Self::Rgb | Self::Bgr => 3,
Self::Rgba | Self::Bgra => 4,
}
}
}
#[derive(Debug, PartialEq)]
pub enum Error {
OptionsInitFailed,
DecodeFailed,
DecoderGetInfoFailed,
TooLargeCanvas(u32, u32, usize),
EncoderCreateFailed,
BufferSizeFailed(usize, usize),
PictureImportFailed,
EncoderAddFailed,
WrongColorMode(ColorMode, ColorMode),
TimestampMustBeHigherThanPrevious(i32, i32),
TimestampMustBeEqualOrHigherThanPrevious(i32, i32),
EncoderAssmebleFailed,
DimensionsMustbePositive,
NoFramesAdded,
ZeroSizeBuffer,
InvalidEncodingConfig,
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::OptionsInitFailed => write!(f, "OptionsInitFailed: Initializing webp options failed, internal (memory allocation?) failure"),
Error::DecodeFailed => write!(f, "DecodeFailed: Could not decode input bytes, possibly malformed data"),
Error::DecoderGetInfoFailed => write!(f, "DecoderGetInfoFailed: Decoder could not get metadata of webp stream. Corrupt data?"),
Error::TooLargeCanvas(width, height, max_size) => write!(f, "TooLargeCanvas: Decodable canvas is too large ({} x {} = {} pixels). For now, size is limited to 3840 * 2160 = {} pixels", width, height, width * height, max_size),
Error::EncoderCreateFailed => write!(f, "EncoderCreateFailed: Encoder create failed. Wrong options combination?"),
Error::BufferSizeFailed(expected, received) => write!(f, "BufferSizeFailed: Expected (width * height * 4 = {}) bytes as input buffer, got {} bytes", expected, received),
Error::PictureImportFailed => write!(f, "PictureImportFailed: Raw data could not be converted into webp frame by underlying libwebp library"),
Error::EncoderAddFailed => write!(f, "EncoderAddFailed: Frame could not be added to webp stream by underlying libwebp library"),
Error::WrongColorMode(requested, expected) => write!(f, "WrongColorMode: Requested image in {:?} format but underlying is stored as {:?}", expected, requested),
Error::TimestampMustBeHigherThanPrevious(requested, previous) => write!(f, "TimestampMustBeHigherThanPrevious: Supplied timestamp (got {}) must be higher than {}", requested, previous),
Error::TimestampMustBeEqualOrHigherThanPrevious(requested, previous) => write!(f, "TimestampMustBeEqualOrHigherThanPrevious: Supplied timestamp (got {}) must be higher or equal to {}", requested, previous),
Error::EncoderAssmebleFailed => write!(f, "EncoderAssmebleFailed: Encoder webp assembly failed"),
Error::DimensionsMustbePositive => write!(f, "DimensionsMustbePositive: Supplied dimensions must be positive"),
Error::NoFramesAdded => write!(f, "NoFramesAdded: No frames have been added yet"),
Error::ZeroSizeBuffer => write!(f, "ZeroSizeBuffer: Buffer contains no data"),
Error::InvalidEncodingConfig => write!(f, "InvalidEncodingConfig: encoding configuration validation failed")
}
}
}
impl std::error::Error for Error {}