Coverage Report

Created: 2024-06-18 06:05

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