caesium/
parameters.rs

1use crate::parameters::TiffCompression::Deflate;
2
3/// Enum representing different chroma subsampling options for JPEG compression.
4///
5/// - `CS444`: 4:4:4 chroma subsampling
6/// - `CS422`: 4:2:2 chroma subsampling
7/// - `CS420`: 4:2:0 chroma subsampling
8/// - `CS411`: 4:1:1 chroma subsampling
9/// - `Auto`: Automatic chroma subsampling
10#[derive(Copy, Clone, PartialEq)]
11pub enum ChromaSubsampling {
12    CS444,
13    CS422,
14    CS420,
15    CS411,
16    Auto,
17}
18
19/// Enum representing different compression algorithms for TIFF images.
20///
21/// - `Uncompressed`: No compression
22/// - `Lzw`: LZW compression
23/// - `Deflate`: Deflate compression
24/// - `Packbits`: PackBits compression
25#[derive(Copy, Clone, PartialEq)]
26pub enum TiffCompression {
27    Uncompressed = 0,
28    Lzw = 1,
29    Deflate = 2,
30    Packbits = 3,
31}
32
33/// Enum representing different deflate levels for TIFF compression.
34///
35/// - `Fast`: Fast compression
36/// - `Balanced`: Balanced compression
37/// - `Best`: Best compression
38#[derive(Copy, Clone, PartialEq)]
39pub enum TiffDeflateLevel {
40    Fast = 1,
41    Balanced = 6,
42    Best = 9,
43}
44
45/// Struct representing parameters for JPEG compression.
46///
47/// Fields:
48/// - `quality`: Quality of the JPEG image (0-100)
49/// - `chroma_subsampling`: Chroma subsampling option
50/// - `progressive`: Whether to use progressive JPEG
51#[derive(Copy, Clone)]
52pub struct JpegParameters {
53    pub quality: u32,
54    pub chroma_subsampling: ChromaSubsampling,
55    pub progressive: bool,
56}
57
58/// Struct representing parameters for PNG compression.
59///
60/// Fields:
61/// - `quality`: Quality of the PNG image (0-100)
62/// - `force_zopfli`: Whether to force the use of Zopfli compression (can be very slow)
63/// - `optimization_level`: Optimization level for PNG compression (0-6)
64#[derive(Copy, Clone)]
65pub struct PngParameters {
66    pub quality: u32,
67    pub force_zopfli: bool,
68    pub optimization_level: u8,
69}
70
71/// Struct representing parameters for GIF compression.
72///
73/// Fields:
74/// - `quality`: Quality of the GIF image (0-100)
75#[derive(Copy, Clone)]
76pub struct GifParameters {
77    pub quality: u32,
78}
79
80/// Struct representing parameters for WebP compression.
81///
82/// Fields:
83/// - `quality`: Quality of the WebP image (0-100)
84#[derive(Copy, Clone)]
85pub struct WebPParameters {
86    pub quality: u32,
87}
88
89/// Struct representing parameters for TIFF compression.
90///
91/// Fields:
92/// - `algorithm`: Compression algorithm for TIFF
93/// - `deflate_level`: Deflate level for TIFF compression
94#[derive(Copy, Clone)]
95pub struct TiffParameters {
96    pub algorithm: TiffCompression,
97    pub deflate_level: TiffDeflateLevel,
98}
99
100/// Struct representing overall compression parameters.
101///
102/// Fields:
103/// - `jpeg`: JPEG compression parameters
104/// - `png`: PNG compression parameters
105/// - `gif`: GIF compression parameters
106/// - `webp`: WebP compression parameters
107/// - `tiff`: TIFF compression parameters
108/// - `keep_metadata`: Whether to keep metadata in the compressed image
109/// - `optimize`: Whether to use lossless compression
110/// - `width`: Width of the output image
111/// - `height`: Height of the output image
112#[derive(Copy, Clone)]
113pub struct CSParameters {
114    pub jpeg: JpegParameters,
115    pub png: PngParameters,
116    pub gif: GifParameters,
117    pub webp: WebPParameters,
118    pub tiff: TiffParameters,
119    pub keep_metadata: bool,
120    pub optimize: bool,
121    pub width: u32,
122    pub height: u32,
123}
124impl Default for CSParameters {
125    fn default() -> Self {
126        Self::new()
127    }
128}
129
130impl CSParameters {
131    pub fn new() -> CSParameters {
132        initialize_parameters()
133    }
134}
135
136fn initialize_parameters() -> CSParameters {
137    let jpeg = JpegParameters {
138        quality: 80,
139        chroma_subsampling: ChromaSubsampling::Auto,
140        progressive: true,
141    };
142    let png = PngParameters {
143        quality: 80,
144        force_zopfli: false,
145        optimization_level: 3,
146    };
147    let gif = GifParameters { quality: 80 };
148    let webp = WebPParameters { quality: 80 };
149    let tiff = TiffParameters {
150        algorithm: Deflate,
151        deflate_level: TiffDeflateLevel::Balanced,
152    };
153
154    CSParameters {
155        jpeg,
156        png,
157        gif,
158        webp,
159        tiff,
160        keep_metadata: false,
161        optimize: false,
162        width: 0,
163        height: 0,
164    }
165}