config_enc.c 5.6 KB

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