#![cfg_attr(
not(any(feature = "unsafe-asm", feature = "_dev")),
forbid(unsafe_code)
)]
#![cfg_attr(feature = "_dev", deny(unsafe_code))]
whereat::define_at_crate_info!();
#[cfg(feature = "zencodec")]
mod codec;
mod config;
mod convert;
mod decode_av1;
#[cfg(feature = "unsafe-asm")]
mod decoder;
mod decoder_managed;
pub mod detect;
#[cfg(feature = "encode")]
mod encoder;
mod error;
mod image;
#[cfg(feature = "_dev")]
pub mod simd;
#[cfg(not(feature = "_dev"))]
pub(crate) mod simd;
mod strip_convert;
#[cfg(feature = "_dev")]
pub mod yuv_convert;
#[cfg(not(feature = "_dev"))]
pub(crate) mod yuv_convert;
#[cfg(all(any(target_arch = "x86_64", target_arch = "aarch64"), feature = "_dev"))]
#[allow(unsafe_code)]
pub mod yuv_convert_fast;
#[cfg(feature = "_dev")]
pub mod yuv_convert_libyuv;
#[cfg(not(feature = "_dev"))]
pub(crate) mod yuv_convert_libyuv;
#[cfg(feature = "_dev")]
pub mod yuv_convert_libyuv_autovec;
#[cfg(not(feature = "_dev"))]
pub(crate) mod yuv_convert_libyuv_autovec;
#[cfg(all(any(target_arch = "x86_64", target_arch = "aarch64"), feature = "_dev"))]
pub mod yuv_convert_libyuv_simd;
#[cfg(all(
any(target_arch = "x86_64", target_arch = "aarch64"),
not(feature = "_dev")
))]
pub(crate) mod yuv_convert_libyuv_simd;
#[cfg(feature = "encode")]
use whereat::at;
#[cfg(feature = "zencodec")]
pub use codec::{
AvifAnimationFrameDecoder, AvifDecodeJob, AvifDecoder as AvifZenDecoder, AvifDecoderConfig,
};
#[cfg(all(feature = "zencodec", feature = "encode"))]
pub use codec::{AvifAnimationFrameEncoder, AvifEncodeJob, AvifEncoder, AvifEncoderConfig};
pub use config::DecoderConfig;
pub use decode_av1::decode_av1_obu;
#[cfg(feature = "unsafe-asm")]
pub use decoder::AvifDecoder;
pub use decoder_managed::{AnimationDecoder, ManagedAvifDecoder};
#[cfg(feature = "encode")]
pub use encoder::{
AnimationFrame, AnimationFrame16, AnimationFrameRgba, AnimationFrameRgba16, Av1Backend,
EncodeAlphaMode, EncodeBitDepth, EncodeColorModel, EncodePixelRange, EncodedAnimation,
EncodedImage, EncoderConfig, GainMapConfig, MasteringDisplayConfig, encode_animation_rgb8,
encode_animation_rgb16, encode_animation_rgba8, encode_animation_rgba16, encode_rgb8,
encode_rgb16, encode_rgba8, encode_rgba16,
};
pub use enough::{Stop, StopReason, Unstoppable};
pub use error::{Error, Result};
pub use image::{
AvifDepthMap, AvifGainMap, ChromaSampling, CleanAperture, ColorPrimaries, ColorRange,
ContentLightLevel, DecodedAnimation, DecodedAnimationInfo, DecodedFrame, GainMapChannel,
GainMapMetadata, ImageInfo, ImageMirror, ImageRotation, MasteringDisplayColourVolume,
MatrixCoefficients, PixelAspectRatio, TransferCharacteristics,
};
pub use zenpixels::PixelBuffer;
pub fn decode(data: &[u8]) -> Result<PixelBuffer> {
decode_with(data, &DecoderConfig::default(), &Unstoppable)
}
pub fn decode_with(
data: &[u8],
config: &DecoderConfig,
stop: &(impl Stop + ?Sized),
) -> Result<PixelBuffer> {
#[cfg(feature = "unsafe-asm")]
{
let mut decoder = AvifDecoder::new(data, config)?;
decoder.decode(stop)
}
#[cfg(not(feature = "unsafe-asm"))]
{
let mut decoder = ManagedAvifDecoder::new(data, config)?;
decoder.decode(stop)
}
}
pub fn decode_animation(data: &[u8]) -> Result<DecodedAnimation> {
decode_animation_with(data, &DecoderConfig::default(), &Unstoppable)
}
pub fn decode_animation_with(
data: &[u8],
config: &DecoderConfig,
stop: &(impl Stop + ?Sized),
) -> Result<DecodedAnimation> {
let mut decoder = ManagedAvifDecoder::new(data, config)?;
decoder.decode_animation(stop)
}
#[cfg(feature = "encode")]
pub fn encode(image: &PixelBuffer) -> Result<EncodedImage> {
encode_with(
image,
&EncoderConfig::default(),
almost_enough::StopToken::new(Unstoppable),
)
}
#[cfg(feature = "encode")]
pub fn encode_with(
image: &PixelBuffer,
config: &EncoderConfig,
stop: almost_enough::StopToken,
) -> Result<EncodedImage> {
use zenpixels::PixelDescriptor;
let desc = image.descriptor();
if desc.layout_compatible(PixelDescriptor::RGB8) {
let img = image.try_as_imgref::<rgb::Rgb<u8>>().unwrap();
encode_rgb8(img, config, stop)
} else if desc.layout_compatible(PixelDescriptor::RGBA8) {
let img = image.try_as_imgref::<rgb::Rgba<u8>>().unwrap();
encode_rgba8(img, config, stop)
} else if desc.layout_compatible(PixelDescriptor::RGB16) {
let img = image.try_as_imgref::<rgb::Rgb<u16>>().unwrap();
encode_rgb16(img, config, stop)
} else if desc.layout_compatible(PixelDescriptor::RGBA16) {
let img = image.try_as_imgref::<rgb::Rgba<u16>>().unwrap();
encode_rgba16(img, config, stop)
} else {
Err(at!(Error::Unsupported(
"only RGB/RGBA 8/16-bit encoding is supported",
)))
}
}