From 05cf2f79eafd84a228fb115b42722c62912c4d71 Mon Sep 17 00:00:00 2001 From: xfy Date: Tue, 9 Jun 2026 15:59:02 +0800 Subject: [PATCH] refactor: extract WebP encode helper to reduce duplication --- src/webp.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/webp.rs b/src/webp.rs index a3278c4..b1e1d73 100644 --- a/src/webp.rs +++ b/src/webp.rs @@ -72,26 +72,29 @@ pub fn encode(img: &image::DynamicImage, quality: f32, method: u8) -> Result Result, WebpError> { + EncodeRequest::lossy(config, pixels, layout, width, height) + .encode() + .map_err(|e| WebpError::Encode(e.to_string())) + } + match img { image::DynamicImage::ImageRgba8(rgba) => { - let pixels = rgba.as_raw(); - EncodeRequest::lossy(&config, pixels, PixelLayout::Rgba8, width, height) - .encode() - .map_err(|e| WebpError::Encode(e.to_string())) + do_encode(&config, rgba.as_raw(), PixelLayout::Rgba8, width, height) } image::DynamicImage::ImageRgb8(rgb) => { - let pixels = rgb.as_raw(); - EncodeRequest::lossy(&config, pixels, PixelLayout::Rgb8, width, height) - .encode() - .map_err(|e| WebpError::Encode(e.to_string())) + do_encode(&config, rgb.as_raw(), PixelLayout::Rgb8, width, height) } _ => { // Convert other formats to RGBA8 let rgba = img.to_rgba8(); - let pixels = rgba.as_raw(); - EncodeRequest::lossy(&config, pixels, PixelLayout::Rgba8, width, height) - .encode() - .map_err(|e| WebpError::Encode(e.to_string())) + do_encode(&config, rgba.as_raw(), PixelLayout::Rgba8, width, height) } } }