diff --git a/src/encoder.rs b/src/encoder.rs index 37e7c63..369ee23 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -332,26 +332,28 @@ impl PictureWrapper { } if unsafe { + let size = color_mode.size() as i32; + match color_mode { ColorMode::Rgba => webp::WebPPictureImportRGBA( &mut self.picture, data.as_ptr(), - self.picture.width * 4, + self.picture.width * size, ), ColorMode::Bgra => webp::WebPPictureImportBGRA( &mut self.picture, data.as_ptr(), - self.picture.width * 4, + self.picture.width * size, ), ColorMode::Rgb => webp::WebPPictureImportRGB( &mut self.picture, data.as_ptr(), - self.picture.width * 3, + self.picture.width * size, ), ColorMode::Bgr => webp::WebPPictureImportBGR( &mut self.picture, data.as_ptr(), - self.picture.width * 3, + self.picture.width * size, ), } } == 0 diff --git a/src/lib.rs b/src/lib.rs index 24fd79c..5dc9356 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,12 +49,11 @@ pub enum ColorMode { } impl ColorMode { + /// Return the pixel bytesize for the color mode pub fn size(&self) -> usize { match self { - Self::Rgb => 3, - Self::Rgba => 4, - Self::Bgra => 4, - Self::Bgr => 3, + Self::Rgb | Self::Bgr => 3, + Self::Rgba | Self::Bgra => 4, } } }