sharpyuv_gamma.c 4.2 KB

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