use crate::Error;
use image::{DynamicImage, ImageEncoder};
use crate::converter::DEPENDENCIES;
pub fn encoder_info() -> String {
let mut image_version = "";
match DEPENDENCIES.iter().rfind(|&&(name, _)| name == "image") {
Some((_name, version)) => {
image_version = version;
}
None => {
println!("Package '{}' not found", "image");
}
};
format!(
"Using \"webp (from image crate)\" ({})",
image_version
)
}
pub fn encode_webp_image(image: &DynamicImage) -> Result<Vec<u8>, Error> {
let mut output = Vec::new();
if image.color().has_alpha() {
let source_image = image.to_rgba8();
image::codecs::webp::WebPEncoder::new_lossless(&mut output)
.write_image(
source_image.as_ref(),
image.width(),
image.height(),
image::ExtendedColorType::Rgba8,
).map_err(|e| Error::from_string(format!("webp-image encoding failed: {:?}", e)))?;
} else {
let source_image = image.to_rgb8();
image::codecs::webp::WebPEncoder::new_lossless(&mut output)
.write_image(
source_image.as_ref(),
image.width(),
image.height(),
image::ExtendedColorType::Rgb8,
).map_err(|e| Error::from_string(format!("webp-image encoding failed: {:?}", e)))?;
}
Ok(output)
}