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) } } }