#[cfg(feature = "zencodec")]
fn main() -> Result<(), Box<dyn std::error::Error>> {
use webpx::zencodec::{WebpDecoderConfig, WebpEncoderConfig};
use zencodec::ImageFormat;
use zencodec::decode::{Decode, DecodeJob, DecoderConfig};
use zencodec::encode::{EncodeJob, Encoder, EncoderConfig};
use zenpixels::{PixelDescriptor, PixelSlice};
let width = 16u32;
let height = 16u32;
let mut rgba = Vec::with_capacity((width * height * 4) as usize);
for y in 0..height {
for x in 0..width {
rgba.extend_from_slice(&[(x * 16) as u8, (y * 16) as u8, 128, 255]);
}
}
let cfg = WebpEncoderConfig::lossy().with_generic_quality(85.0);
println!(
"encoder format: {:?}, generic_quality: {:?}, capabilities.lossy: {}",
<WebpEncoderConfig as EncoderConfig>::format(),
cfg.generic_quality(),
<WebpEncoderConfig as EncoderConfig>::capabilities().lossy(),
);
let job = cfg.job();
let encoder = job.encoder()?;
let pixels = PixelSlice::new(
&rgba,
width,
height,
(width * 4) as usize,
PixelDescriptor::RGBA8_SRGB,
)?;
let webp = encoder.encode(pixels)?.into_vec();
println!("encoded {} bytes of WebP", webp.len());
assert_eq!(
<WebpEncoderConfig as EncoderConfig>::format(),
ImageFormat::WebP
);
let dcfg = WebpDecoderConfig::new();
let djob = dcfg.job();
let info = djob.probe(&webp)?;
println!(
"probe: {}×{}, format={:?}",
info.width, info.height, info.format,
);
let decoder = djob.decoder(
std::borrow::Cow::Borrowed(&webp),
&[PixelDescriptor::RGBA8_SRGB],
)?;
let result = decoder.decode()?;
println!(
"decoded: {}×{}, has_alpha={}",
result.width(),
result.height(),
result.has_alpha(),
);
println!("\nThis program runs identically against zenwebp — change");
println!("only the `use` line at the top of `main()`.");
Ok(())
}
#[cfg(not(feature = "zencodec"))]
fn main() {
eprintln!("Build with `--features zencodec` to run this example.");
}