refactor: extract WebP encode helper to reduce duplication
This commit is contained in:
parent
7d9c4b9e0f
commit
05cf2f79ea
27
src/webp.rs
27
src/webp.rs
@ -72,26 +72,29 @@ pub fn encode(img: &image::DynamicImage, quality: f32, method: u8) -> Result<Vec
|
|||||||
let (width, height) = (img.width(), img.height());
|
let (width, height) = (img.width(), img.height());
|
||||||
let config = LossyConfig::new().with_quality(quality).with_method(method);
|
let config = LossyConfig::new().with_quality(quality).with_method(method);
|
||||||
|
|
||||||
|
fn do_encode(
|
||||||
|
config: &LossyConfig,
|
||||||
|
pixels: &[u8],
|
||||||
|
layout: zenwebp::PixelLayout,
|
||||||
|
width: u32,
|
||||||
|
height: u32,
|
||||||
|
) -> Result<Vec<u8>, WebpError> {
|
||||||
|
EncodeRequest::lossy(config, pixels, layout, width, height)
|
||||||
|
.encode()
|
||||||
|
.map_err(|e| WebpError::Encode(e.to_string()))
|
||||||
|
}
|
||||||
|
|
||||||
match img {
|
match img {
|
||||||
image::DynamicImage::ImageRgba8(rgba) => {
|
image::DynamicImage::ImageRgba8(rgba) => {
|
||||||
let pixels = rgba.as_raw();
|
do_encode(&config, rgba.as_raw(), PixelLayout::Rgba8, width, height)
|
||||||
EncodeRequest::lossy(&config, pixels, PixelLayout::Rgba8, width, height)
|
|
||||||
.encode()
|
|
||||||
.map_err(|e| WebpError::Encode(e.to_string()))
|
|
||||||
}
|
}
|
||||||
image::DynamicImage::ImageRgb8(rgb) => {
|
image::DynamicImage::ImageRgb8(rgb) => {
|
||||||
let pixels = rgb.as_raw();
|
do_encode(&config, rgb.as_raw(), PixelLayout::Rgb8, width, height)
|
||||||
EncodeRequest::lossy(&config, pixels, PixelLayout::Rgb8, width, height)
|
|
||||||
.encode()
|
|
||||||
.map_err(|e| WebpError::Encode(e.to_string()))
|
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
// Convert other formats to RGBA8
|
// Convert other formats to RGBA8
|
||||||
let rgba = img.to_rgba8();
|
let rgba = img.to_rgba8();
|
||||||
let pixels = rgba.as_raw();
|
do_encode(&config, rgba.as_raw(), PixelLayout::Rgba8, width, height)
|
||||||
EncodeRequest::lossy(&config, pixels, PixelLayout::Rgba8, width, height)
|
|
||||||
.encode()
|
|
||||||
.map_err(|e| WebpError::Encode(e.to_string()))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user