1// Copyright 2011 Google Inc. All Rights Reserved.
2//
3// This code is licensed under the same terms as WebM:
4//  Software License Agreement:  http://www.webmproject.org/license/software/
5//  Additional IP Rights Grant:  http://www.webmproject.org/license/additional/
6// -----------------------------------------------------------------------------
7//
8// Coding tools configuration
9//
10// Author: Skal (pascal.massimino@gmail.com)
11
12#include "webp/encode.h"
13
14#if defined(__cplusplus) || defined(c_plusplus)
15extern "C" {
16#endif
17
18//------------------------------------------------------------------------------
19// WebPConfig
20//------------------------------------------------------------------------------
21
22int WebPConfigInitInternal(WebPConfig* config,
23                           WebPPreset preset, float quality, int version) {
24  if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_ENCODER_ABI_VERSION)) {
25    return 0;   // caller/system version mismatch!
26  }
27  if (config == NULL) return 0;
28
29  config->quality = quality;
30  config->target_size = 0;
31  config->target_PSNR = 0.;
32  config->method = 4;
33  config->sns_strength = 50;
34  config->filter_strength = 60;   // rather high filtering, helps w/ gradients.
35  config->filter_sharpness = 0;
36  config->filter_type = 1;        // default: strong (so U/V is filtered too)
37  config->partitions = 0;
38  config->segments = 4;
39  config->pass = 1;
40  config->show_compressed = 0;
41  config->preprocessing = 0;
42  config->autofilter = 0;
43  config->partition_limit = 0;
44  config->alpha_compression = 1;
45  config->alpha_filtering = 1;
46  config->alpha_quality = 100;
47  config->lossless = 0;
48  config->image_hint = WEBP_HINT_DEFAULT;
49  config->emulate_jpeg_size = 0;
50  config->thread_level = 0;
51  config->low_memory = 0;
52
53  // TODO(skal): tune.
54  switch (preset) {
55    case WEBP_PRESET_PICTURE:
56      config->sns_strength = 80;
57      config->filter_sharpness = 4;
58      config->filter_strength = 35;
59      break;
60    case WEBP_PRESET_PHOTO:
61      config->sns_strength = 80;
62      config->filter_sharpness = 3;
63      config->filter_strength = 30;
64      break;
65    case WEBP_PRESET_DRAWING:
66      config->sns_strength = 25;
67      config->filter_sharpness = 6;
68      config->filter_strength = 10;
69      break;
70    case WEBP_PRESET_ICON:
71      config->sns_strength = 0;
72      config->filter_strength = 0;   // disable filtering to retain sharpness
73      break;
74    case WEBP_PRESET_TEXT:
75      config->sns_strength = 0;
76      config->filter_strength = 0;   // disable filtering to retain sharpness
77      config->segments = 2;
78      break;
79    case WEBP_PRESET_DEFAULT:
80    default:
81      break;
82  }
83  return WebPValidateConfig(config);
84}
85
86int WebPValidateConfig(const WebPConfig* config) {
87  if (config == NULL) return 0;
88  if (config->quality < 0 || config->quality > 100)
89    return 0;
90  if (config->target_size < 0)
91    return 0;
92  if (config->target_PSNR < 0)
93    return 0;
94  if (config->method < 0 || config->method > 6)
95    return 0;
96  if (config->segments < 1 || config->segments > 4)
97    return 0;
98  if (config->sns_strength < 0 || config->sns_strength > 100)
99    return 0;
100  if (config->filter_strength < 0 || config->filter_strength > 100)
101    return 0;
102  if (config->filter_sharpness < 0 || config->filter_sharpness > 7)
103    return 0;
104  if (config->filter_type < 0 || config->filter_type > 1)
105    return 0;
106  if (config->autofilter < 0 || config->autofilter > 1)
107    return 0;
108  if (config->pass < 1 || config->pass > 10)
109    return 0;
110  if (config->show_compressed < 0 || config->show_compressed > 1)
111    return 0;
112  if (config->preprocessing < 0 || config->preprocessing > 1)
113    return 0;
114  if (config->partitions < 0 || config->partitions > 3)
115    return 0;
116  if (config->partition_limit < 0 || config->partition_limit > 100)
117    return 0;
118  if (config->alpha_compression < 0)
119    return 0;
120  if (config->alpha_filtering < 0)
121    return 0;
122  if (config->alpha_quality < 0 || config->alpha_quality > 100)
123    return 0;
124  if (config->lossless < 0 || config->lossless > 1)
125    return 0;
126  if (config->image_hint >= WEBP_HINT_LAST)
127    return 0;
128  if (config->emulate_jpeg_size < 0 || config->emulate_jpeg_size > 1)
129    return 0;
130  if (config->thread_level < 0 || config->thread_level > 1)
131    return 0;
132  if (config->low_memory < 0 || config->low_memory > 1)
133    return 0;
134  return 1;
135}
136
137//------------------------------------------------------------------------------
138
139#if defined(__cplusplus) || defined(c_plusplus)
140}    // extern "C"
141#endif
142