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