Path: blob/master/3rdparty/libwebp/src/enc/config_enc.c
16349 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->show_compressed = 0;42config->preprocessing = 0;43config->autofilter = 0;44config->partition_limit = 0;45config->alpha_compression = 1;46config->alpha_filtering = 1;47config->alpha_quality = 100;48config->lossless = 0;49config->exact = 0;50config->image_hint = WEBP_HINT_DEFAULT;51config->emulate_jpeg_size = 0;52config->thread_level = 0;53config->low_memory = 0;54config->near_lossless = 100;55config->use_delta_palette = 0;56config->use_sharp_yuv = 0;5758// TODO(skal): tune.59switch (preset) {60case WEBP_PRESET_PICTURE:61config->sns_strength = 80;62config->filter_sharpness = 4;63config->filter_strength = 35;64config->preprocessing &= ~2; // no dithering65break;66case WEBP_PRESET_PHOTO:67config->sns_strength = 80;68config->filter_sharpness = 3;69config->filter_strength = 30;70config->preprocessing |= 2;71break;72case WEBP_PRESET_DRAWING:73config->sns_strength = 25;74config->filter_sharpness = 6;75config->filter_strength = 10;76break;77case WEBP_PRESET_ICON:78config->sns_strength = 0;79config->filter_strength = 0; // disable filtering to retain sharpness80config->preprocessing &= ~2; // no dithering81break;82case WEBP_PRESET_TEXT:83config->sns_strength = 0;84config->filter_strength = 0; // disable filtering to retain sharpness85config->preprocessing &= ~2; // no dithering86config->segments = 2;87break;88case WEBP_PRESET_DEFAULT:89default:90break;91}92return WebPValidateConfig(config);93}9495int WebPValidateConfig(const WebPConfig* config) {96if (config == NULL) return 0;97if (config->quality < 0 || config->quality > 100) return 0;98if (config->target_size < 0) return 0;99if (config->target_PSNR < 0) return 0;100if (config->method < 0 || config->method > 6) return 0;101if (config->segments < 1 || config->segments > 4) return 0;102if (config->sns_strength < 0 || config->sns_strength > 100) return 0;103if (config->filter_strength < 0 || config->filter_strength > 100) return 0;104if (config->filter_sharpness < 0 || config->filter_sharpness > 7) return 0;105if (config->filter_type < 0 || config->filter_type > 1) return 0;106if (config->autofilter < 0 || config->autofilter > 1) return 0;107if (config->pass < 1 || config->pass > 10) return 0;108if (config->show_compressed < 0 || config->show_compressed > 1) return 0;109if (config->preprocessing < 0 || config->preprocessing > 7) return 0;110if (config->partitions < 0 || config->partitions > 3) return 0;111if (config->partition_limit < 0 || config->partition_limit > 100) return 0;112if (config->alpha_compression < 0) return 0;113if (config->alpha_filtering < 0) return 0;114if (config->alpha_quality < 0 || config->alpha_quality > 100) return 0;115if (config->lossless < 0 || config->lossless > 1) return 0;116if (config->near_lossless < 0 || config->near_lossless > 100) return 0;117if (config->image_hint >= WEBP_HINT_LAST) return 0;118if (config->emulate_jpeg_size < 0 || config->emulate_jpeg_size > 1) return 0;119if (config->thread_level < 0 || config->thread_level > 1) return 0;120if (config->low_memory < 0 || config->low_memory > 1) return 0;121if (config->exact < 0 || config->exact > 1) return 0;122if (config->use_delta_palette < 0 || config->use_delta_palette > 1) {123return 0;124}125if (config->use_sharp_yuv < 0 || config->use_sharp_yuv > 1) return 0;126127return 1;128}129130//------------------------------------------------------------------------------131132#define MAX_LEVEL 9133134// Mapping between -z level and -m / -q parameter settings.135static const struct {136uint8_t method_;137uint8_t quality_;138} kLosslessPresets[MAX_LEVEL + 1] = {139{ 0, 0 }, { 1, 20 }, { 2, 25 }, { 3, 30 }, { 3, 50 },140{ 4, 50 }, { 4, 75 }, { 4, 90 }, { 5, 90 }, { 6, 100 }141};142143int WebPConfigLosslessPreset(WebPConfig* config, int level) {144if (config == NULL || level < 0 || level > MAX_LEVEL) return 0;145config->lossless = 1;146config->method = kLosslessPresets[level].method_;147config->quality = kLosslessPresets[level].quality_;148return 1;149}150151//------------------------------------------------------------------------------152153154