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/// - `optimize`: Whether to use lossless optimization for JPEG
52/// - `preserve_icc`: Always keep the original ICC data regardless of other options
53#[derive(Copy, Clone)]
54pub struct JpegParameters {
55 pub quality: u32,
56 pub chroma_subsampling: ChromaSubsampling,
57 pub progressive: bool,
58 pub optimize: bool,
59 pub preserve_icc: bool,
60}
61
62/// Struct representing parameters for PNG compression.
63///
64/// Fields:
65/// - `quality`: Quality of the PNG image (0-100)
66/// - `force_zopfli`: Whether to force the use of Zopfli compression (can be very slow)
67/// - `optimization_level`: Optimization level for PNG compression (0-6)
68/// - `optimize`: Whether to use lossless optimization for PNG
69#[derive(Copy, Clone)]
70pub struct PngParameters {
71 pub quality: u32,
72 pub force_zopfli: bool,
73 pub optimization_level: u8,
74 pub optimize: bool,
75}
76
77/// Struct representing parameters for GIF compression.
78///
79/// Fields:
80/// - `quality`: Quality of the GIF image (0-100)
81#[derive(Copy, Clone)]
82pub struct GifParameters {
83 pub quality: u32,
84}
85
86/// Struct representing parameters for WebP compression.
87///
88/// Fields:
89/// - `quality`: Quality of the WebP image (0-100)
90/// - `lossless`: Whether to use lossless compression for WebP
91#[derive(Copy, Clone)]
92pub struct WebPParameters {
93 pub quality: u32,
94 pub lossless: bool,
95}
96
97/// Struct representing parameters for TIFF compression.
98///
99/// Fields:
100/// - `algorithm`: Compression algorithm for TIFF
101/// - `deflate_level`: Deflate level for TIFF compression
102#[derive(Copy, Clone)]
103pub struct TiffParameters {
104 pub algorithm: TiffCompression,
105 pub deflate_level: TiffDeflateLevel,
106}
107
108/// Struct representing overall compression parameters.
109///
110/// Fields:
111/// - `jpeg`: JPEG compression parameters
112/// - `png`: PNG compression parameters
113/// - `gif`: GIF compression parameters
114/// - `webp`: WebP compression parameters
115/// - `tiff`: TIFF compression parameters
116/// - `keep_metadata`: Whether to keep metadata in the compressed image
117/// - `width`: Width of the output image
118/// - `height`: Height of the output image
119#[derive(Copy, Clone)]
120pub struct CSParameters {
121 pub jpeg: JpegParameters,
122 pub png: PngParameters,
123 pub gif: GifParameters,
124 pub webp: WebPParameters,
125 pub tiff: TiffParameters,
126 pub keep_metadata: bool,
127 pub width: u32,
128 pub height: u32,
129}
130impl Default for CSParameters {
131 fn default() -> Self {
132 Self::new()
133 }
134}
135
136impl CSParameters {
137 pub fn new() -> CSParameters {
138 initialize_parameters()
139 }
140}
141
142fn initialize_parameters() -> CSParameters {
143 let jpeg = JpegParameters {
144 quality: 80,
145 chroma_subsampling: ChromaSubsampling::Auto,
146 progressive: true,
147 optimize: false,
148 preserve_icc: true,
149 };
150 let png = PngParameters {
151 quality: 80,
152 force_zopfli: false,
153 optimization_level: 3,
154 optimize: false,
155 };
156 let gif = GifParameters { quality: 80 };
157 let webp = WebPParameters {
158 quality: 80,
159 lossless: false,
160 };
161 let tiff = TiffParameters {
162 algorithm: Deflate,
163 deflate_level: TiffDeflateLevel::Balanced,
164 };
165
166 CSParameters {
167 jpeg,
168 png,
169 gif,
170 webp,
171 tiff,
172 keep_metadata: false,
173 width: 0,
174 height: 0,
175 }
176}