Skip to main content

opencv_rs_core/
image_encoder.rs

1//! Image encoder port.
2
3use crate::{ImageEncodingError, MatView};
4
5/// The output container an [`ImageEncoderPort`] should produce.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum EncodingKind {
8    /// JPEG-encoded bytes.
9    Jpeg,
10    /// WebP-encoded bytes.
11    Webp,
12    /// No encoding; the encoder should return the raw pixel bytes verbatim.
13    None,
14}
15
16impl EncodingKind {
17    /// Human-readable short name for the encoding.
18    pub const fn name(self) -> &'static str {
19        match self {
20            Self::Jpeg => "jpeg",
21            Self::Webp => "webp",
22            Self::None => "none",
23        }
24    }
25}
26
27/// Encodes a [`MatView`] into a byte buffer in one of the [`EncodingKind`]s.
28pub trait ImageEncoderPort: Send + Sync {
29    /// Encode `frame` using `kind` and return the resulting bytes.
30    fn encode(
31        &self,
32        frame: &dyn MatView,
33        kind: EncodingKind,
34    ) -> Result<Vec<u8>, ImageEncodingError>;
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn encoding_kind_names() {
43        assert_eq!(EncodingKind::Jpeg.name(), "jpeg");
44        assert_eq!(EncodingKind::Webp.name(), "webp");
45        assert_eq!(EncodingKind::None.name(), "none");
46    }
47}