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/// - `keep_rotation`: Always preserve the EXIF orientation tag, regardless of `keep_metadata`
118/// - `width`: Width of the output image
119/// - `height`: Height of the output image
120#[derive(Copy, Clone)]
121pub struct CSParameters {
122 pub jpeg: JpegParameters,
123 pub png: PngParameters,
124 pub gif: GifParameters,
125 pub webp: WebPParameters,
126 pub tiff: TiffParameters,
127 pub keep_metadata: bool,
128 pub keep_rotation: bool,
129 pub width: u32,
130 pub height: u32,
131}
132impl Default for CSParameters {
133 fn default() -> Self {
134 Self::new()
135 }
136}
137
138impl CSParameters {
139 pub fn new() -> CSParameters {
140 initialize_parameters()
141 }
142}
143
144fn initialize_parameters() -> CSParameters {
145 let jpeg = JpegParameters {
146 quality: 80,
147 chroma_subsampling: ChromaSubsampling::Auto,
148 progressive: true,
149 optimize: false,
150 preserve_icc: true,
151 };
152 let png = PngParameters {
153 quality: 80,
154 force_zopfli: false,
155 optimization_level: 3,
156 optimize: false,
157 };
158 let gif = GifParameters { quality: 80 };
159 let webp = WebPParameters {
160 quality: 80,
161 lossless: false,
162 };
163 let tiff = TiffParameters {
164 algorithm: Deflate,
165 deflate_level: TiffDeflateLevel::Balanced,
166 };
167
168 CSParameters {
169 jpeg,
170 png,
171 gif,
172 webp,
173 tiff,
174 keep_metadata: false,
175 keep_rotation: false,
176 width: 0,
177 height: 0,
178 }
179}