1 // Copyright 2011 Google Inc. All Rights Reserved.
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the COPYING file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 // -----------------------------------------------------------------------------
10 // Coding tools configuration
12 // Author: Skal (pascal.massimino@gmail.com)
14 #include "../webp/encode.h"
16 //------------------------------------------------------------------------------
18 //------------------------------------------------------------------------------
20 int WebPConfigInitInternal(WebPConfig
* config
,
21 WebPPreset preset
, float quality
, int version
) {
22 if (WEBP_ABI_IS_INCOMPATIBLE(version
, WEBP_ENCODER_ABI_VERSION
)) {
23 return 0; // caller/system version mismatch!
25 if (config
== NULL
) return 0;
27 config
->quality
= quality
;
28 config
->target_size
= 0;
29 config
->target_PSNR
= 0.;
31 config
->sns_strength
= 50;
32 config
->filter_strength
= 60; // mid-filtering
33 config
->filter_sharpness
= 0;
34 config
->filter_type
= 1; // default: strong (so U/V is filtered too)
35 config
->partitions
= 0;
38 config
->show_compressed
= 0;
39 config
->preprocessing
= 0;
40 config
->autofilter
= 0;
41 config
->partition_limit
= 0;
42 config
->alpha_compression
= 1;
43 config
->alpha_filtering
= 1;
44 config
->alpha_quality
= 100;
46 config
->image_hint
= WEBP_HINT_DEFAULT
;
47 config
->emulate_jpeg_size
= 0;
48 config
->thread_level
= 0;
49 config
->low_memory
= 0;
53 case WEBP_PRESET_PICTURE
:
54 config
->sns_strength
= 80;
55 config
->filter_sharpness
= 4;
56 config
->filter_strength
= 35;
57 config
->preprocessing
&= ~2; // no dithering
59 case WEBP_PRESET_PHOTO
:
60 config
->sns_strength
= 80;
61 config
->filter_sharpness
= 3;
62 config
->filter_strength
= 30;
63 config
->preprocessing
|= 2;
65 case WEBP_PRESET_DRAWING
:
66 config
->sns_strength
= 25;
67 config
->filter_sharpness
= 6;
68 config
->filter_strength
= 10;
70 case WEBP_PRESET_ICON
:
71 config
->sns_strength
= 0;
72 config
->filter_strength
= 0; // disable filtering to retain sharpness
73 config
->preprocessing
&= ~2; // no dithering
75 case WEBP_PRESET_TEXT
:
76 config
->sns_strength
= 0;
77 config
->filter_strength
= 0; // disable filtering to retain sharpness
78 config
->preprocessing
&= ~2; // no dithering
81 case WEBP_PRESET_DEFAULT
:
85 return WebPValidateConfig(config
);
88 int WebPValidateConfig(const WebPConfig
* config
) {
89 if (config
== NULL
) return 0;
90 if (config
->quality
< 0 || config
->quality
> 100)
92 if (config
->target_size
< 0)
94 if (config
->target_PSNR
< 0)
96 if (config
->method
< 0 || config
->method
> 6)
98 if (config
->segments
< 1 || config
->segments
> 4)
100 if (config
->sns_strength
< 0 || config
->sns_strength
> 100)
102 if (config
->filter_strength
< 0 || config
->filter_strength
> 100)
104 if (config
->filter_sharpness
< 0 || config
->filter_sharpness
> 7)
106 if (config
->filter_type
< 0 || config
->filter_type
> 1)
108 if (config
->autofilter
< 0 || config
->autofilter
> 1)
110 if (config
->pass
< 1 || config
->pass
> 10)
112 if (config
->show_compressed
< 0 || config
->show_compressed
> 1)
114 #if WEBP_ENCODER_ABI_VERSION > 0x0204
115 if (config
->preprocessing
< 0 || config
->preprocessing
> 7)
117 if (config
->preprocessing
< 0 || config
->preprocessing
> 3)
120 if (config
->partitions
< 0 || config
->partitions
> 3)
122 if (config
->partition_limit
< 0 || config
->partition_limit
> 100)
124 if (config
->alpha_compression
< 0)
126 if (config
->alpha_filtering
< 0)
128 if (config
->alpha_quality
< 0 || config
->alpha_quality
> 100)
130 if (config
->lossless
< 0 || config
->lossless
> 1)
132 if (config
->image_hint
>= WEBP_HINT_LAST
)
134 if (config
->emulate_jpeg_size
< 0 || config
->emulate_jpeg_size
> 1)
136 if (config
->thread_level
< 0 || config
->thread_level
> 1)
138 if (config
->low_memory
< 0 || config
->low_memory
> 1)
143 //------------------------------------------------------------------------------
145 #if WEBP_ENCODER_ABI_VERSION > 0x0202
148 // Mapping between -z level and -m / -q parameter settings.
149 static const struct {
152 } kLosslessPresets
[MAX_LEVEL
+ 1] = {
153 { 0, 0 }, { 1, 20 }, { 2, 25 }, { 3, 30 }, { 3, 50 },
154 { 4, 50 }, { 4, 75 }, { 4, 90 }, { 5, 90 }, { 6, 100 }
157 int WebPConfigLosslessPreset(WebPConfig
* config
, int level
) {
158 if (config
== NULL
|| level
< 0 || level
> MAX_LEVEL
) return 0;
159 config
->lossless
= 1;
160 config
->method
= kLosslessPresets
[level
].method_
;
161 config
->quality
= kLosslessPresets
[level
].quality_
;
166 //------------------------------------------------------------------------------