Path: blob/master/thirdparty/libwebp/src/enc/config_enc.c
9913 views
// Copyright 2011 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// Coding tools configuration10//11// Author: Skal ([email protected])1213#ifdef HAVE_CONFIG_H14#include "src/webp/config.h"15#endif1617#include "src/webp/encode.h"1819//------------------------------------------------------------------------------20// WebPConfig21//------------------------------------------------------------------------------2223int WebPConfigInitInternal(WebPConfig* config,24WebPPreset preset, float quality, int version) {25if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_ENCODER_ABI_VERSION)) {26return 0; // caller/system version mismatch!27}28if (config == NULL) return 0;2930config->quality = quality;31config->target_size = 0;32config->target_PSNR = 0.;33config->method = 4;34config->sns_strength = 50;35config->filter_strength = 60; // mid-filtering36config->filter_sharpness = 0;37config->filter_type = 1; // default: strong (so U/V is filtered too)38config->partitions = 0;39config->segments = 4;40config->pass = 1;41config->qmin = 0;42config->qmax = 100;43config->show_compressed = 0;44config->preprocessing = 0;45config->autofilter = 0;46config->partition_limit = 0;47config->alpha_compression = 1;48config->alpha_filtering = 1;49config->alpha_quality = 100;50config->lossless = 0;51config->exact = 0;52config->image_hint = WEBP_HINT_DEFAULT;53config->emulate_jpeg_size = 0;54config->thread_level = 0;55config->low_memory = 0;56config->near_lossless = 100;57config->use_sharp_yuv = 0;5859// TODO(skal): tune.60switch (preset) {61case WEBP_PRESET_PICTURE:62config->sns_strength = 80;63config->filter_sharpness = 4;64config->filter_strength = 35;65config->preprocessing &= ~2; // no dithering66break;67case WEBP_PRESET_PHOTO:68config->sns_strength = 80;69config->filter_sharpness = 3;70config->filter_strength = 30;71config->preprocessing |= 2;72break;73case WEBP_PRESET_DRAWING:74config->sns_strength = 25;75config->filter_sharpness = 6;76config->filter_strength = 10;77break;78case WEBP_PRESET_ICON:79config->sns_strength = 0;80config->filter_strength = 0; // disable filtering to retain sharpness81config->preprocessing &= ~2; // no dithering82break;83case WEBP_PRESET_TEXT:84config->sns_strength = 0;85config->filter_strength = 0; // disable filtering to retain sharpness86config->preprocessing &= ~2; // no dithering87config->segments = 2;88break;89case WEBP_PRESET_DEFAULT:90default:91break;92}93return WebPValidateConfig(config);94}9596int WebPValidateConfig(const WebPConfig* config) {97if (config == NULL) return 0;98if (config->quality < 0 || config->quality > 100) return 0;99if (config->target_size < 0) return 0;100if (config->target_PSNR < 0) return 0;101if (config->method < 0 || config->method > 6) return 0;102if (config->segments < 1 || config->segments > 4) return 0;103if (config->sns_strength < 0 || config->sns_strength > 100) return 0;104if (config->filter_strength < 0 || config->filter_strength > 100) return 0;105if (config->filter_sharpness < 0 || config->filter_sharpness > 7) return 0;106if (config->filter_type < 0 || config->filter_type > 1) return 0;107if (config->autofilter < 0 || config->autofilter > 1) return 0;108if (config->pass < 1 || config->pass > 10) return 0;109if (config->qmin < 0 || config->qmax > 100 || config->qmin > config->qmax) {110return 0;111}112if (config->show_compressed < 0 || config->show_compressed > 1) return 0;113if (config->preprocessing < 0 || config->preprocessing > 7) return 0;114if (config->partitions < 0 || config->partitions > 3) return 0;115if (config->partition_limit < 0 || config->partition_limit > 100) return 0;116if (config->alpha_compression < 0) return 0;117if (config->alpha_filtering < 0) return 0;118if (config->alpha_quality < 0 || config->alpha_quality > 100) return 0;119if (config->lossless < 0 || config->lossless > 1) return 0;120if (config->near_lossless < 0 || config->near_lossless > 100) return 0;121if (config->image_hint >= WEBP_HINT_LAST) return 0;122if (config->emulate_jpeg_size < 0 || config->emulate_jpeg_size > 1) return 0;123if (config->thread_level < 0 || config->thread_level > 1) return 0;124if (config->low_memory < 0 || config->low_memory > 1) return 0;125if (config->exact < 0 || config->exact > 1) return 0;126if (config->use_sharp_yuv < 0 || config->use_sharp_yuv > 1) return 0;127128return 1;129}130131//------------------------------------------------------------------------------132133#define MAX_LEVEL 9134135// Mapping between -z level and -m / -q parameter settings.136static const struct {137uint8_t method_;138uint8_t quality_;139} kLosslessPresets[MAX_LEVEL + 1] = {140{ 0, 0 }, { 1, 20 }, { 2, 25 }, { 3, 30 }, { 3, 50 },141{ 4, 50 }, { 4, 75 }, { 4, 90 }, { 5, 90 }, { 6, 100 }142};143144int WebPConfigLosslessPreset(WebPConfig* config, int level) {145if (config == NULL || level < 0 || level > MAX_LEVEL) return 0;146config->lossless = 1;147config->method = kLosslessPresets[level].method_;148config->quality = kLosslessPresets[level].quality_;149return 1;150}151152//------------------------------------------------------------------------------153154155