1// Copyright 2011 Google Inc. All Rights Reserved.
2//
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// -----------------------------------------------------------------------------
9//
10// Coding tools configuration
11//
12// Author: Skal (pascal.massimino@gmail.com)
13
14#include "webp/encode.h"
15
16//------------------------------------------------------------------------------
17// WebPConfig
18//------------------------------------------------------------------------------
19
20int 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!
24  }
25  if (config == NULL) return 0;
26
27  config->quality = quality;
28  config->target_size = 0;
29  config->target_PSNR = 0.;
30  config->method = 4;
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;
36  config->segments = 4;
37  config->pass = 1;
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;
45  config->lossless = 0;
46  config->image_hint = WEBP_HINT_DEFAULT;
47  config->emulate_jpeg_size = 0;
48  config->thread_level = 0;
49  config->low_memory = 0;
50
51  // TODO(skal): tune.
52  switch (preset) {
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
58      break;
59    case WEBP_PRESET_PHOTO:
60      config->sns_strength = 80;
61      config->filter_sharpness = 3;
62      config->filter_strength = 30;
63      config->preprocessing |= 2;
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      config->preprocessing &= ~2;   // no dithering
74      break;
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
79      config->segments = 2;
80      break;
81    case WEBP_PRESET_DEFAULT:
82    default:
83      break;
84  }
85  return WebPValidateConfig(config);
86}
87
88int WebPValidateConfig(const WebPConfig* config) {
89  if (config == NULL) return 0;
90  if (config->quality < 0 || config->quality > 100)
91    return 0;
92  if (config->target_size < 0)
93    return 0;
94  if (config->target_PSNR < 0)
95    return 0;
96  if (config->method < 0 || config->method > 6)
97    return 0;
98  if (config->segments < 1 || config->segments > 4)
99    return 0;
100  if (config->sns_strength < 0 || config->sns_strength > 100)
101    return 0;
102  if (config->filter_strength < 0 || config->filter_strength > 100)
103    return 0;
104  if (config->filter_sharpness < 0 || config->filter_sharpness > 7)
105    return 0;
106  if (config->filter_type < 0 || config->filter_type > 1)
107    return 0;
108  if (config->autofilter < 0 || config->autofilter > 1)
109    return 0;
110  if (config->pass < 1 || config->pass > 10)
111    return 0;
112  if (config->show_compressed < 0 || config->show_compressed > 1)
113    return 0;
114  if (config->preprocessing < 0 || config->preprocessing > 3)
115    return 0;
116  if (config->partitions < 0 || config->partitions > 3)
117    return 0;
118  if (config->partition_limit < 0 || config->partition_limit > 100)
119    return 0;
120  if (config->alpha_compression < 0)
121    return 0;
122  if (config->alpha_filtering < 0)
123    return 0;
124  if (config->alpha_quality < 0 || config->alpha_quality > 100)
125    return 0;
126  if (config->lossless < 0 || config->lossless > 1)
127    return 0;
128  if (config->image_hint >= WEBP_HINT_LAST)
129    return 0;
130  if (config->emulate_jpeg_size < 0 || config->emulate_jpeg_size > 1)
131    return 0;
132  if (config->thread_level < 0 || config->thread_level > 1)
133    return 0;
134  if (config->low_memory < 0 || config->low_memory > 1)
135    return 0;
136  return 1;
137}
138
139//------------------------------------------------------------------------------
140
141#if WEBP_ENCODER_ABI_VERSION > 0x0202
142#define MAX_LEVEL 9
143
144// Mapping between -z level and -m / -q parameter settings.
145static const struct {
146  uint8_t method_;
147  uint8_t quality_;
148} kLosslessPresets[MAX_LEVEL + 1] = {
149  { 0,  0 }, { 1, 20 }, { 2, 25 }, { 3, 30 }, { 3, 50 },
150  { 4, 50 }, { 4, 75 }, { 4, 90 }, { 5, 90 }, { 6, 100 }
151};
152
153int WebPConfigLosslessPreset(WebPConfig* config, int level) {
154  if (config == NULL || level < 0 || level > MAX_LEVEL) return 0;
155  config->lossless = 1;
156  config->method = kLosslessPresets[level].method_;
157  config->quality = kLosslessPresets[level].quality_;
158  return 1;
159}
160#endif
161
162//------------------------------------------------------------------------------
163