config.c 5.3 KB

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