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