sharpyuv_gamma.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2022 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. // Gamma correction utilities.
  11. #include "sharpyuv/sharpyuv_gamma.h"
  12. #include <assert.h>
  13. #include <math.h>
  14. #include "src/webp/types.h"
  15. // Gamma correction compensates loss of resolution during chroma subsampling.
  16. // Size of pre-computed table for converting from gamma to linear.
  17. #define GAMMA_TO_LINEAR_TAB_BITS 10
  18. #define GAMMA_TO_LINEAR_TAB_SIZE (1 << GAMMA_TO_LINEAR_TAB_BITS)
  19. static uint32_t kGammaToLinearTabS[GAMMA_TO_LINEAR_TAB_SIZE + 2];
  20. #define LINEAR_TO_GAMMA_TAB_BITS 9
  21. #define LINEAR_TO_GAMMA_TAB_SIZE (1 << LINEAR_TO_GAMMA_TAB_BITS)
  22. static uint32_t kLinearToGammaTabS[LINEAR_TO_GAMMA_TAB_SIZE + 2];
  23. static const double kGammaF = 1. / 0.45;
  24. #define GAMMA_TO_LINEAR_BITS 16
  25. static volatile int kGammaTablesSOk = 0;
  26. void SharpYuvInitGammaTables(void) {
  27. assert(GAMMA_TO_LINEAR_BITS <= 16);
  28. if (!kGammaTablesSOk) {
  29. int v;
  30. const double a = 0.09929682680944;
  31. const double thresh = 0.018053968510807;
  32. const double final_scale = 1 << GAMMA_TO_LINEAR_BITS;
  33. // Precompute gamma to linear table.
  34. {
  35. const double norm = 1. / GAMMA_TO_LINEAR_TAB_SIZE;
  36. const double a_rec = 1. / (1. + a);
  37. for (v = 0; v <= GAMMA_TO_LINEAR_TAB_SIZE; ++v) {
  38. const double g = norm * v;
  39. double value;
  40. if (g <= thresh * 4.5) {
  41. value = g / 4.5;
  42. } else {
  43. value = pow(a_rec * (g + a), kGammaF);
  44. }
  45. kGammaToLinearTabS[v] = (uint32_t)(value * final_scale + .5);
  46. }
  47. // to prevent small rounding errors to cause read-overflow:
  48. kGammaToLinearTabS[GAMMA_TO_LINEAR_TAB_SIZE + 1] =
  49. kGammaToLinearTabS[GAMMA_TO_LINEAR_TAB_SIZE];
  50. }
  51. // Precompute linear to gamma table.
  52. {
  53. const double scale = 1. / LINEAR_TO_GAMMA_TAB_SIZE;
  54. for (v = 0; v <= LINEAR_TO_GAMMA_TAB_SIZE; ++v) {
  55. const double g = scale * v;
  56. double value;
  57. if (g <= thresh) {
  58. value = 4.5 * g;
  59. } else {
  60. value = (1. + a) * pow(g, 1. / kGammaF) - a;
  61. }
  62. kLinearToGammaTabS[v] =
  63. (uint32_t)(final_scale * value + 0.5);
  64. }
  65. // to prevent small rounding errors to cause read-overflow:
  66. kLinearToGammaTabS[LINEAR_TO_GAMMA_TAB_SIZE + 1] =
  67. kLinearToGammaTabS[LINEAR_TO_GAMMA_TAB_SIZE];
  68. }
  69. kGammaTablesSOk = 1;
  70. }
  71. }
  72. static WEBP_INLINE int Shift(int v, int shift) {
  73. return (shift >= 0) ? (v << shift) : (v >> -shift);
  74. }
  75. static WEBP_INLINE uint32_t FixedPointInterpolation(int v, uint32_t* tab,
  76. int tab_pos_shift_right,
  77. int tab_value_shift) {
  78. const uint32_t tab_pos = Shift(v, -tab_pos_shift_right);
  79. // fractional part, in 'tab_pos_shift' fixed-point precision
  80. const uint32_t x = v - (tab_pos << tab_pos_shift_right); // fractional part
  81. // v0 / v1 are in kGammaToLinearBits fixed-point precision (range [0..1])
  82. const uint32_t v0 = Shift(tab[tab_pos + 0], tab_value_shift);
  83. const uint32_t v1 = Shift(tab[tab_pos + 1], tab_value_shift);
  84. // Final interpolation.
  85. const uint32_t v2 = (v1 - v0) * x; // note: v1 >= v0.
  86. const int half =
  87. (tab_pos_shift_right > 0) ? 1 << (tab_pos_shift_right - 1) : 0;
  88. const uint32_t result = v0 + ((v2 + half) >> tab_pos_shift_right);
  89. return result;
  90. }
  91. uint32_t SharpYuvGammaToLinear(uint16_t v, int bit_depth) {
  92. const int shift = GAMMA_TO_LINEAR_TAB_BITS - bit_depth;
  93. if (shift > 0) {
  94. return kGammaToLinearTabS[v << shift];
  95. }
  96. return FixedPointInterpolation(v, kGammaToLinearTabS, -shift, 0);
  97. }
  98. uint16_t SharpYuvLinearToGamma(uint32_t value, int bit_depth) {
  99. return FixedPointInterpolation(
  100. value, kLinearToGammaTabS,
  101. (GAMMA_TO_LINEAR_BITS - LINEAR_TO_GAMMA_TAB_BITS),
  102. bit_depth - GAMMA_TO_LINEAR_BITS);
  103. }