Skip to main content

ai_image/codecs/webp/
encoder.rs

1//! Encoding of WebP images.
2
3use alloc::vec::Vec;
4use no_std_io::io::Write;
5
6use crate::error::{EncodingError, UnsupportedError, UnsupportedErrorKind};
7use crate::{DynamicImage, ExtendedColorType, ImageEncoder, ImageError, ImageFormat, ImageResult};
8
9/// WebP Encoder.
10///
11/// ### Limitations
12///
13/// Right now only **lossless** encoding is supported.
14///
15/// If you need **lossy** encoding, you'll have to use `libwebp`.
16/// Example code for encoding a [`DynamicImage`](crate::DynamicImage) with `libwebp`
17/// via the [`webp`](https://docs.rs/webp/latest/webp/) crate can be found
18/// [here](https://github.com/jaredforth/webp/blob/main/examples/convert.rs).
19///
20/// ### Compression ratio
21///
22/// This encoder reaches compression ratios higher than PNG at a fraction of the encoding time.
23/// However, it does not reach the full potential of lossless WebP for reducing file size.
24///
25/// If you need an even higher compression ratio at the cost of much slower encoding,
26/// please encode the image with `libwebp` as outlined above.
27pub struct WebPEncoder<W> {
28    inner: image_webp::WebPEncoder<W>,
29}
30
31impl<W: Write> WebPEncoder<W> {
32    /// Create a new encoder that writes its output to `w`.
33    ///
34    /// Uses "VP8L" lossless encoding.
35    pub fn new_lossless(w: W) -> Self {
36        Self {
37            inner: image_webp::WebPEncoder::new(w),
38        }
39    }
40
41    /// Encode image data with the indicated color type.
42    ///
43    /// The encoder requires image data be Rgb8 or Rgba8.
44    ///
45    /// # Panics
46    ///
47    /// Panics if `width * height * color.bytes_per_pixel() != data.len()`.
48    #[track_caller]
49    pub fn encode(
50        self,
51        buf: &[u8],
52        width: u32,
53        height: u32,
54        color_type: ExtendedColorType,
55    ) -> ImageResult<()> {
56        let expected_buffer_len = color_type.buffer_size(width, height);
57        assert_eq!(
58            expected_buffer_len,
59            buf.len() as u64,
60            "Invalid buffer length: expected {expected_buffer_len} got {} for {width}x{height} image",
61            buf.len(),
62        );
63
64        let color_type = match color_type {
65            ExtendedColorType::L8 => image_webp::ColorType::L8,
66            ExtendedColorType::La8 => image_webp::ColorType::La8,
67            ExtendedColorType::Rgb8 => image_webp::ColorType::Rgb8,
68            ExtendedColorType::Rgba8 => image_webp::ColorType::Rgba8,
69            _ => {
70                return Err(ImageError::Unsupported(
71                    UnsupportedError::from_format_and_kind(
72                        ImageFormat::WebP.into(),
73                        UnsupportedErrorKind::Color(color_type),
74                    ),
75                ))
76            }
77        };
78
79        self.inner
80            .encode(buf, width, height, color_type)
81            .map_err(ImageError::from_webp_encode)
82    }
83}
84
85impl<W: Write> ImageEncoder for WebPEncoder<W> {
86    #[track_caller]
87    fn write_image(
88        self,
89        buf: &[u8],
90        width: u32,
91        height: u32,
92        color_type: ExtendedColorType,
93    ) -> ImageResult<()> {
94        self.encode(buf, width, height, color_type)
95    }
96
97    fn set_icc_profile(&mut self, icc_profile: Vec<u8>) -> Result<(), UnsupportedError> {
98        self.inner.set_icc_profile(icc_profile);
99        Ok(())
100    }
101
102    fn set_exif_metadata(&mut self, exif: Vec<u8>) -> Result<(), UnsupportedError> {
103        self.inner.set_exif_metadata(exif);
104        Ok(())
105    }
106
107    fn make_compatible_img(
108        &self,
109        _: crate::io::encoder::MethodSealedToImage,
110        img: &DynamicImage,
111    ) -> Option<DynamicImage> {
112        crate::io::encoder::dynimage_conversion_8bit(img)
113    }
114}
115
116impl ImageError {
117    fn from_webp_encode(e: image_webp::EncodingError) -> Self {
118        match e {
119            image_webp::EncodingError::IoError(e) => ImageError::IoError(e),
120            _ => ImageError::Encoding(EncodingError::new(ImageFormat::WebP.into(), e)),
121        }
122    }
123}
124
125#[cfg(test)]
126mod tests {
127    use crate::{ImageEncoder, RgbaImage};
128
129    #[test]
130    fn write_webp() {
131        let img = RgbaImage::from_raw(10, 6, (0..240).collect()).unwrap();
132
133        let mut output = Vec::new();
134        super::WebPEncoder::new_lossless(&mut output)
135            .write_image(
136                img.inner_pixels(),
137                img.width(),
138                img.height(),
139                crate::ExtendedColorType::Rgba8,
140            )
141            .unwrap();
142
143        let img2 = crate::load_from_memory_with_format(&output, crate::ImageFormat::WebP)
144            .unwrap()
145            .to_rgba8();
146
147        assert_eq!(img, img2);
148    }
149}