webp_screenshot_rust/encoder/
mod.rs

1//! WebP encoding module
2
3pub mod webp;
4pub mod simd;
5pub mod gpu;
6
7pub use webp::{WebPEncoder, EncoderOptions};
8pub use simd::{SimdConverter, global_simd_converter};
9
10use crate::{
11    error::EncodingResult,
12    types::{RawImage, WebPConfig},
13};
14
15/// Trait for image encoders
16pub trait ImageEncoder {
17    /// Encode raw image data to a specific format
18    fn encode(&self, image: &RawImage, config: &WebPConfig) -> EncodingResult<Vec<u8>>;
19
20    /// Get encoder name
21    fn name(&self) -> &str;
22
23    /// Check if encoder supports a specific pixel format
24    fn supports_format(&self, format: crate::types::PixelFormat) -> bool;
25}
26
27/// Encoder statistics
28#[derive(Debug, Clone, Default)]
29pub struct EncoderStats {
30    /// Total images encoded
31    pub images_encoded: u64,
32    /// Total bytes processed
33    pub bytes_processed: u64,
34    /// Total bytes output
35    pub bytes_output: u64,
36    /// Average compression ratio
37    pub average_compression_ratio: f64,
38    /// Average encoding time in milliseconds
39    pub average_encoding_time_ms: f64,
40}
41
42impl EncoderStats {
43    /// Update statistics with a new encoding
44    pub fn update(&mut self, input_size: usize, output_size: usize, time_ms: f64) {
45        self.images_encoded += 1;
46        self.bytes_processed += input_size as u64;
47        self.bytes_output += output_size as u64;
48
49        let compression_ratio = output_size as f64 / input_size as f64;
50        self.average_compression_ratio =
51            (self.average_compression_ratio * (self.images_encoded - 1) as f64 + compression_ratio)
52            / self.images_encoded as f64;
53
54        self.average_encoding_time_ms =
55            (self.average_encoding_time_ms * (self.images_encoded - 1) as f64 + time_ms)
56            / self.images_encoded as f64;
57    }
58
59    /// Get space savings percentage
60    pub fn space_savings_percent(&self) -> f64 {
61        if self.bytes_processed == 0 {
62            0.0
63        } else {
64            (1.0 - (self.bytes_output as f64 / self.bytes_processed as f64)) * 100.0
65        }
66    }
67}