Path: blob/master/thirdparty/libwebp/src/enc/config_enc.c
21432 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 <stddef.h>1819#include "src/webp/encode.h"20#include "src/webp/types.h"2122//------------------------------------------------------------------------------23// WebPConfig24//------------------------------------------------------------------------------2526int WebPConfigInitInternal(WebPConfig* config,27WebPPreset preset, float quality, int version) {28if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_ENCODER_ABI_VERSION)) {29return 0; // caller/system version mismatch!30}31if (config == NULL) return 0;3233config->quality = quality;34config->target_size = 0;35config->target_PSNR = 0.;36config->method = 4;37config->sns_strength = 50;38config->filter_strength = 60; // mid-filtering39config->filter_sharpness = 0;40config->filter_type = 1; // default: strong (so U/V is filtered too)41config->partitions = 0;42config->segments = 4;43config->pass = 1;44config->qmin = 0;45config->qmax = 100;46config->show_compressed = 0;47config->preprocessing = 0;48config->autofilter = 0;49config->partition_limit = 0;50config->alpha_compression = 1;51config->alpha_filtering = 1;52config->alpha_quality = 100;53config->lossless = 0;54config->exact = 0;55config->image_hint = WEBP_HINT_DEFAULT;56config->emulate_jpeg_size = 0;57config->thread_level = 0;58config->low_memory = 0;59config->near_lossless = 100;60config->use_sharp_yuv = 0;6162// TODO(skal): tune.63switch (preset) {64case WEBP_PRESET_PICTURE:65config->sns_strength = 80;66config->filter_sharpness = 4;67config->filter_strength = 35;68config->preprocessing &= ~2; // no dithering69break;70case WEBP_PRESET_PHOTO:71config->sns_strength = 80;72config->filter_sharpness = 3;73config->filter_strength = 30;74config->preprocessing |= 2;75break;76case WEBP_PRESET_DRAWING:77config->sns_strength = 25;78config->filter_sharpness = 6;79config->filter_strength = 10;80break;81case WEBP_PRESET_ICON:82config->sns_strength = 0;83config->filter_strength = 0; // disable filtering to retain sharpness84config->preprocessing &= ~2; // no dithering85break;86case WEBP_PRESET_TEXT:87config->sns_strength = 0;88config->filter_strength = 0; // disable filtering to retain sharpness89config->preprocessing &= ~2; // no dithering90config->segments = 2;91break;92case WEBP_PRESET_DEFAULT:93default:94break;95}96return WebPValidateConfig(config);97}9899int WebPValidateConfig(const WebPConfig* config) {100if (config == NULL) return 0;101if (config->quality < 0 || config->quality > 100) return 0;102if (config->target_size < 0) return 0;103if (config->target_PSNR < 0) return 0;104if (config->method < 0 || config->method > 6) return 0;105if (config->segments < 1 || config->segments > 4) return 0;106if (config->sns_strength < 0 || config->sns_strength > 100) return 0;107if (config->filter_strength < 0 || config->filter_strength > 100) return 0;108if (config->filter_sharpness < 0 || config->filter_sharpness > 7) return 0;109if (config->filter_type < 0 || config->filter_type > 1) return 0;110if (config->autofilter < 0 || config->autofilter > 1) return 0;111if (config->pass < 1 || config->pass > 10) return 0;112if (config->qmin < 0 || config->qmax > 100 || config->qmin > config->qmax) {113return 0;114}115if (config->show_compressed < 0 || config->show_compressed > 1) return 0;116if (config->preprocessing < 0 || config->preprocessing > 7) return 0;117if (config->partitions < 0 || config->partitions > 3) return 0;118if (config->partition_limit < 0 || config->partition_limit > 100) return 0;119if (config->alpha_compression < 0) return 0;120if (config->alpha_filtering < 0) return 0;121if (config->alpha_quality < 0 || config->alpha_quality > 100) return 0;122if (config->lossless < 0 || config->lossless > 1) return 0;123if (config->near_lossless < 0 || config->near_lossless > 100) return 0;124if (config->image_hint >= WEBP_HINT_LAST) return 0;125if (config->emulate_jpeg_size < 0 || config->emulate_jpeg_size > 1) return 0;126if (config->thread_level < 0 || config->thread_level > 1) return 0;127if (config->low_memory < 0 || config->low_memory > 1) return 0;128if (config->exact < 0 || config->exact > 1) return 0;129if (config->use_sharp_yuv < 0 || config->use_sharp_yuv > 1) return 0;130131return 1;132}133134//------------------------------------------------------------------------------135136#define MAX_LEVEL 9137138// Mapping between -z level and -m / -q parameter settings.139static const struct {140uint8_t method;141uint8_t quality;142} kLosslessPresets[MAX_LEVEL + 1] = {143{ 0, 0 }, { 1, 20 }, { 2, 25 }, { 3, 30 }, { 3, 50 },144{ 4, 50 }, { 4, 75 }, { 4, 90 }, { 5, 90 }, { 6, 100 }145};146147int WebPConfigLosslessPreset(WebPConfig* config, int level) {148if (config == NULL || level < 0 || level > MAX_LEVEL) return 0;149config->lossless = 1;150config->method = kLosslessPresets[level].method;151config->quality = kLosslessPresets[level].quality;152return 1;153}154155//------------------------------------------------------------------------------156157158