Skip to content

Commit 2473680

Browse files
committed
fix(image): webp encode LumaA8 and Luma8
1 parent ff6e231 commit 2473680

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

packages/binding/src/webp.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use image::ColorType;
1+
use image::{buffer::ConvertBuffer, ColorType, DynamicImage, Rgb, Rgba};
22
use napi::bindgen_prelude::*;
33

44
#[inline]
@@ -45,15 +45,15 @@ pub(crate) unsafe fn lossless_encode_webp_inner(
4545

4646
#[inline]
4747
pub(crate) unsafe fn encode_webp_inner(
48-
input: &[u8],
48+
input: &DynamicImage,
4949
quality_factor: u32,
5050
width: u32,
5151
height: u32,
5252
color_type: &image::ColorType,
5353
) -> Result<(*mut u8, usize)> {
5454
let mut out_buf = std::ptr::null_mut();
55-
let len = match color_type {
56-
ColorType::Rgb8 => {
55+
let len = match input {
56+
DynamicImage::ImageRgb8(input) => {
5757
let stride = width as i32 * 3;
5858
libwebp_sys::WebPEncodeRGB(
5959
input.as_ptr(),
@@ -64,7 +64,7 @@ pub(crate) unsafe fn encode_webp_inner(
6464
&mut out_buf,
6565
)
6666
}
67-
ColorType::Rgba8 => {
67+
DynamicImage::ImageRgba8(input) => {
6868
let stride = width as i32 * 4;
6969
libwebp_sys::WebPEncodeRGBA(
7070
input.as_ptr(),
@@ -75,6 +75,30 @@ pub(crate) unsafe fn encode_webp_inner(
7575
&mut out_buf,
7676
)
7777
}
78+
DynamicImage::ImageLuma8(input) => {
79+
let stride = width as i32 * 3;
80+
let converted: image::ImageBuffer<Rgb<u8>, _> = input.convert();
81+
libwebp_sys::WebPEncodeRGB(
82+
converted.as_ptr(),
83+
width as i32,
84+
height as i32,
85+
stride,
86+
quality_factor as f32,
87+
&mut out_buf,
88+
)
89+
}
90+
DynamicImage::ImageLumaA8(input) => {
91+
let stride = width as i32 * 4;
92+
let converted: image::ImageBuffer<Rgba<u8>, _> = input.convert();
93+
libwebp_sys::WebPEncodeRGBA(
94+
converted.as_ptr(),
95+
width as i32,
96+
height as i32,
97+
stride,
98+
quality_factor as f32,
99+
&mut out_buf,
100+
)
101+
}
78102
_ => {
79103
return Err(Error::new(
80104
Status::InvalidArg,

0 commit comments

Comments
 (0)