Coverage Report

Created: 2025-08-12 07:37

/src/libwebp/src/enc/config_enc.c
Line
Count
Source (jump to first uncovered line)
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
#ifdef HAVE_CONFIG_H
15
#include "src/webp/config.h"
16
#endif
17
18
#include <stddef.h>
19
20
#include "src/webp/encode.h"
21
#include "src/webp/types.h"
22
23
//------------------------------------------------------------------------------
24
// WebPConfig
25
//------------------------------------------------------------------------------
26
27
int WebPConfigInitInternal(WebPConfig* config, WebPPreset preset, float quality,
28
111
                           int version) {
29
111
  if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_ENCODER_ABI_VERSION)) {
30
0
    return 0;  // caller/system version mismatch!
31
0
  }
32
111
  if (config == NULL) return 0;
33
34
111
  config->quality = quality;
35
111
  config->target_size = 0;
36
111
  config->target_PSNR = 0.;
37
111
  config->method = 4;
38
111
  config->sns_strength = 50;
39
111
  config->filter_strength = 60;  // mid-filtering
40
111
  config->filter_sharpness = 0;
41
111
  config->filter_type = 1;  // default: strong (so U/V is filtered too)
42
111
  config->partitions = 0;
43
111
  config->segments = 4;
44
111
  config->pass = 1;
45
111
  config->qmin = 0;
46
111
  config->qmax = 100;
47
111
  config->show_compressed = 0;
48
111
  config->preprocessing = 0;
49
111
  config->autofilter = 0;
50
111
  config->partition_limit = 0;
51
111
  config->alpha_compression = 1;
52
111
  config->alpha_filtering = 1;
53
111
  config->alpha_quality = 100;
54
111
  config->lossless = 0;
55
111
  config->exact = 0;
56
111
  config->image_hint = WEBP_HINT_DEFAULT;
57
111
  config->emulate_jpeg_size = 0;
58
111
  config->thread_level = 0;
59
111
  config->low_memory = 0;
60
111
  config->near_lossless = 100;
61
111
  config->use_sharp_yuv = 0;
62
63
  // TODO(skal): tune.
64
111
  switch (preset) {
65
0
    case WEBP_PRESET_PICTURE:
66
0
      config->sns_strength = 80;
67
0
      config->filter_sharpness = 4;
68
0
      config->filter_strength = 35;
69
0
      config->preprocessing &= ~2;  // no dithering
70
0
      break;
71
0
    case WEBP_PRESET_PHOTO:
72
0
      config->sns_strength = 80;
73
0
      config->filter_sharpness = 3;
74
0
      config->filter_strength = 30;
75
0
      config->preprocessing |= 2;
76
0
      break;
77
0
    case WEBP_PRESET_DRAWING:
78
0
      config->sns_strength = 25;
79
0
      config->filter_sharpness = 6;
80
0
      config->filter_strength = 10;
81
0
      break;
82
0
    case WEBP_PRESET_ICON:
83
0
      config->sns_strength = 0;
84
0
      config->filter_strength = 0;  // disable filtering to retain sharpness
85
0
      config->preprocessing &= ~2;  // no dithering
86
0
      break;
87
0
    case WEBP_PRESET_TEXT:
88
0
      config->sns_strength = 0;
89
0
      config->filter_strength = 0;  // disable filtering to retain sharpness
90
0
      config->preprocessing &= ~2;  // no dithering
91
0
      config->segments = 2;
92
0
      break;
93
111
    case WEBP_PRESET_DEFAULT:
94
111
    default:
95
111
      break;
96
111
  }
97
111
  return WebPValidateConfig(config);
98
111
}
99
100
258
int WebPValidateConfig(const WebPConfig* config) {
101
258
  if (config == NULL) return 0;
102
258
  if (config->quality < 0 || config->quality > 100) return 0;
103
258
  if (config->target_size < 0) return 0;
104
258
  if (config->target_PSNR < 0) return 0;
105
258
  if (config->method < 0 || config->method > 6) return 0;
106
258
  if (config->segments < 1 || config->segments > 4) return 0;
107
258
  if (config->sns_strength < 0 || config->sns_strength > 100) return 0;
108
258
  if (config->filter_strength < 0 || config->filter_strength > 100) return 0;
109
258
  if (config->filter_sharpness < 0 || config->filter_sharpness > 7) return 0;
110
258
  if (config->filter_type < 0 || config->filter_type > 1) return 0;
111
258
  if (config->autofilter < 0 || config->autofilter > 1) return 0;
112
258
  if (config->pass < 1 || config->pass > 10) return 0;
113
258
  if (config->qmin < 0 || config->qmax > 100 || config->qmin > config->qmax) {
114
0
    return 0;
115
0
  }
116
258
  if (config->show_compressed < 0 || config->show_compressed > 1) return 0;
117
258
  if (config->preprocessing < 0 || config->preprocessing > 7) return 0;
118
258
  if (config->partitions < 0 || config->partitions > 3) return 0;
119
258
  if (config->partition_limit < 0 || config->partition_limit > 100) return 0;
120
258
  if (config->alpha_compression < 0) return 0;
121
258
  if (config->alpha_filtering < 0) return 0;
122
258
  if (config->alpha_quality < 0 || config->alpha_quality > 100) return 0;
123
258
  if (config->lossless < 0 || config->lossless > 1) return 0;
124
258
  if (config->near_lossless < 0 || config->near_lossless > 100) return 0;
125
258
  if (config->image_hint >= WEBP_HINT_LAST) return 0;
126
258
  if (config->emulate_jpeg_size < 0 || config->emulate_jpeg_size > 1) return 0;
127
258
  if (config->thread_level < 0 || config->thread_level > 1) return 0;
128
258
  if (config->low_memory < 0 || config->low_memory > 1) return 0;
129
258
  if (config->exact < 0 || config->exact > 1) return 0;
130
258
  if (config->use_sharp_yuv < 0 || config->use_sharp_yuv > 1) return 0;
131
132
258
  return 1;
133
258
}
134
135
//------------------------------------------------------------------------------
136
137
0
#define MAX_LEVEL 9
138
139
// Mapping between -z level and -m / -q parameter settings.
140
static const struct {
141
  uint8_t method;
142
  uint8_t quality;
143
} kLosslessPresets[MAX_LEVEL + 1] = {{0, 0},  {1, 20}, {2, 25}, {3, 30},
144
                                     {3, 50}, {4, 50}, {4, 75}, {4, 90},
145
                                     {5, 90}, {6, 100}};
146
147
0
int WebPConfigLosslessPreset(WebPConfig* config, int level) {
148
0
  if (config == NULL || level < 0 || level > MAX_LEVEL) return 0;
149
0
  config->lossless = 1;
150
0
  config->method = kLosslessPresets[level].method;
151
0
  config->quality = kLosslessPresets[level].quality;
152
0
  return 1;
153
0
}
154
155
//------------------------------------------------------------------------------