1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
use std::mem;
use crate::{ColorMode, Error};
#[allow(unused_imports)]
use crate::Encoder; use libwebp_sys as webp;
#[derive(Clone)]
pub struct EncoderOptions {
pub minimize_size: bool,
pub kmin: isize,
pub kmax: isize,
pub allow_mixed: bool,
pub verbose: bool,
pub color_mode: ColorMode,
pub encoding_config: Option<EncodingConfig>,
}
impl Default for EncoderOptions {
fn default() -> Self {
Self {
minimize_size: false,
kmin: 0,
kmax: 0,
allow_mixed: false,
verbose: false,
color_mode: ColorMode::Rgba,
encoding_config: None,
}
}
}
#[derive(Debug, Clone)]
pub enum EncodingType {
Lossy(LossyEncodingConfig),
Lossless,
}
impl EncodingType {
pub fn new_lossy() -> Self {
EncodingType::Lossy(LossyEncodingConfig::default())
}
}
#[derive(Debug, Clone)]
pub struct EncodingConfig {
pub encoding_type: EncodingType,
pub quality: f32,
pub method: usize,
}
impl EncodingConfig {
pub fn new_lossy(quality: f32) -> Self {
Self {
encoding_type: EncodingType::new_lossy(),
quality,
..Default::default()
}
}
pub(crate) fn to_config_container(&self) -> Result<ConfigContainer, Error> {
ConfigContainer::new(self)
}
pub(crate) fn apply_to(&self, webp_config: &mut webp::WebPConfig) {
webp_config.lossless = match &self.encoding_type {
EncodingType::Lossy(lossless_config) => {
lossless_config.apply_to(webp_config);
0
}
EncodingType::Lossless => 1,
};
webp_config.quality = self.quality;
}
}
impl Default for EncodingConfig {
fn default() -> Self {
Self {
encoding_type: EncodingType::Lossless,
quality: 1.,
method: 4,
}
}
}
#[derive(Debug, Clone)]
pub struct LossyEncodingConfig {
pub target_size: usize,
pub target_psnr: f32,
pub segments: usize,
pub sns_strength: usize,
pub filter_strength: usize,
pub filter_sharpness: usize,
pub filter_type: usize,
pub autofilter: bool,
pub alpha_compression: bool,
pub alpha_filtering: usize,
pub alpha_quality: usize,
pub pass: usize,
pub show_compressed: bool,
pub preprocessing: bool,
pub partitions: usize,
pub partition_limit: isize,
pub use_sharp_yuv: bool,
}
impl Default for LossyEncodingConfig {
fn default() -> Self {
Self {
target_size: 0,
target_psnr: 0.,
segments: 1,
sns_strength: 50,
filter_strength: 60,
filter_sharpness: 0,
filter_type: 1,
partitions: 0,
pass: 1,
show_compressed: false,
autofilter: false,
alpha_compression: true,
alpha_filtering: 1,
alpha_quality: 100,
preprocessing: false,
partition_limit: 0,
use_sharp_yuv: false,
}
}
}
impl LossyEncodingConfig {
pub fn new_from_default_preset() -> Self {
Self {
..Default::default()
}
}
pub fn new_from_picture_preset() -> Self {
Self {
sns_strength: 80,
filter_sharpness: 4,
filter_strength: 35,
preprocessing: false,
..Default::default()
}
}
pub fn new_from_photo_preset() -> Self {
Self {
sns_strength: 80,
filter_sharpness: 3,
filter_strength: 30,
preprocessing: false,
..Default::default()
}
}
pub fn new_from_drawing_preset() -> Self {
Self {
sns_strength: 25,
filter_sharpness: 6,
filter_strength: 10,
..Default::default()
}
}
pub fn new_from_icon_preset() -> Self {
Self {
sns_strength: 0,
filter_strength: 0,
preprocessing: false,
..Default::default()
}
}
pub fn new_from_text_preset() -> Self {
Self {
sns_strength: 0,
filter_strength: 0,
preprocessing: false,
segments: 2,
..Default::default()
}
}
fn apply_to(&self, webp_config: &mut webp::WebPConfig) {
webp_config.target_size = self.target_size as i32;
webp_config.target_PSNR = self.target_psnr;
webp_config.segments = self.segments as i32;
webp_config.sns_strength = self.sns_strength as i32;
webp_config.filter_strength = self.filter_strength as i32;
webp_config.filter_sharpness = self.filter_sharpness as i32;
webp_config.filter_type = self.filter_type as i32;
webp_config.autofilter = self.autofilter as i32;
webp_config.alpha_compression = self.alpha_compression as i32;
webp_config.alpha_filtering = self.alpha_filtering as i32;
webp_config.alpha_quality = self.alpha_quality as i32;
webp_config.pass = self.pass as i32;
webp_config.show_compressed = self.show_compressed as i32;
webp_config.preprocessing = self.preprocessing as i32;
webp_config.partitions = self.partitions as i32;
webp_config.partition_limit = self.partition_limit as i32;
webp_config.use_sharp_yuv = self.use_sharp_yuv as i32;
}
}
pub(crate) struct ConfigContainer {
config: webp::WebPConfig,
}
impl ConfigContainer {
pub fn new(config: &EncodingConfig) -> Result<Self, Error> {
let mut webp_config = unsafe {
let mut config = mem::zeroed();
webp::WebPConfigInit(&mut config);
config
};
config.apply_to(&mut webp_config);
if unsafe { webp::WebPValidateConfig(&webp_config) } == 0 {
return Err(Error::InvalidEncodingConfig);
}
Ok(Self {
config: webp_config,
})
}
pub fn as_ptr(&self) -> &webp::WebPConfig {
&self.config
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_config_defaults() {
let default_webp_config = unsafe {
let mut config = mem::zeroed();
webp::WebPConfigInit(&mut config);
config
};
let config = ConfigContainer::new(&EncodingConfig::default()).unwrap();
let left = config.as_ptr();
let def = &default_webp_config;
assert_eq!(left.lossless, 1);
assert_eq!(left.quality, 1.0);
assert_eq!(left.method, def.method, "c.method");
assert_eq!(left.image_hint, def.image_hint, "c.image_hint");
assert_eq!(left.target_size, def.target_size, "c.target_size");
assert_eq!(left.target_PSNR, def.target_PSNR, "c.target_PSNR");
assert_eq!(left.segments, def.segments, "c.segments");
assert_eq!(left.sns_strength, def.sns_strength, "c.sns_strength");
assert_eq!(
left.filter_strength, def.filter_strength,
"c.filter_strength"
);
assert_eq!(
left.filter_sharpness, def.filter_sharpness,
"c.filter_sharpness"
);
assert_eq!(left.filter_type, def.filter_type, "c.filter_type");
assert_eq!(left.autofilter, def.autofilter, "c.autofilter");
assert_eq!(
left.alpha_compression, def.alpha_compression,
"c.alpha_compression"
);
assert_eq!(
left.alpha_filtering, def.alpha_filtering,
"c.alpha_filtering"
);
assert_eq!(left.alpha_quality, def.alpha_quality, "c.alpha_quality");
assert_eq!(left.pass, def.pass, "c.pass");
assert_eq!(
left.show_compressed, def.show_compressed,
"c.show_compressed"
);
assert_eq!(left.preprocessing, def.preprocessing, "c.preprocessing");
assert_eq!(left.partitions, def.partitions, "c.partitions");
assert_eq!(
left.partition_limit, def.partition_limit,
"c.partition_limit"
);
assert_eq!(
left.emulate_jpeg_size, def.emulate_jpeg_size,
"c.emulate_jpeg_size"
);
assert_eq!(left.thread_level, def.thread_level, "c.thread_level");
assert_eq!(left.low_memory, def.low_memory, "c.low_memory");
assert_eq!(left.near_lossless, def.near_lossless, "c.near_lossless");
assert_eq!(left.exact, def.exact, "c.exact");
assert_eq!(
left.use_delta_palette, def.use_delta_palette,
"c.use_delta_palette"
);
assert_eq!(left.use_sharp_yuv, def.use_sharp_yuv, "c.use_sharp_yuv");
assert_eq!(left.qmin, def.qmin, "c.qmin");
assert_eq!(left.qmax, def.qmax, "c.qmax");
}
}