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