Gamma.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include "Color.h"
  8. #include <math.h>
  9. #include <xmmintrin.h>
  10. #include <AK/SIMD.h>
  11. #define GAMMA 2.2
  12. // Most computer graphics are stored in the sRGB color space, which stores something close to
  13. // the square root of the display intensity of each color channel. This is problematic for most
  14. // operations that we want to perform on colors, since they typically assume that color scales
  15. // linearly (e.g. rgb(127, 0, 0) is half as bright as rgb(255, 0, 0)). This causes incorrect
  16. // results that look more gray than they should, to fix this we have to convert colors to the linear
  17. // color space before performing these operations, then convert back before displaying.
  18. //
  19. // Conversion between linear and sRGB spaces are somewhat expensive to do on the CPU, so we instead
  20. // interpret sRGB colors as gamma2.2 colors, which are close enough in most cases to be indistinguishable.
  21. // Gamma 2.2 colors follow the simple rule of `display_intensity = pow(stored_intensity, 2.2)`.
  22. // This module implements some fast color space transforms between the gamma2.2 and linear color spaces, plus
  23. // some common primitive operations like blending.
  24. //
  25. // For a more in-depth overview of how gamma-adjustment works, check out:
  26. // https://blog.johnnovak.net/2016/09/21/what-every-coder-should-know-about-gamma/
  27. namespace Gfx {
  28. using AK::SIMD::f32x4;
  29. #ifndef NO_FPU
  30. # ifdef __SSE__
  31. // Transform f32x4 from gamma2.2 space to linear space
  32. // Assumes x is in range [0, 1]
  33. // FIXME: Remove this hack once clang-11 is available as the default in Github Actions.
  34. // This is apparently sometime mid-December. https://github.com/actions/virtual-environments/issues/2130
  35. # if !defined(__clang__) || __clang_major__ >= 11
  36. constexpr f32x4 gamma_to_linear4(f32x4 x)
  37. # else
  38. inline f32x4 gamma_to_linear4(f32x4 x)
  39. # endif
  40. {
  41. return (0.8f + 0.2f * x) * x * x;
  42. }
  43. // Transform f32x4 from linear space to gamma2.2 space
  44. // Assumes x is in range [0, 1]
  45. inline f32x4 linear_to_gamma4(f32x4 x)
  46. {
  47. // Source for approximation: https://mimosa-pudica.net/fast-gamma/
  48. constexpr float a = 0.00279491f;
  49. constexpr float b = 1.15907984f;
  50. float c = (b / sqrtf(1.0f + a)) - 1;
  51. return ((b * __builtin_ia32_rsqrtps(x + a)) - c) * x;
  52. }
  53. // Linearize v1 and v2, lerp them by mix factor, then convert back.
  54. // The output is entirely v1 when mix = 0 and entirely v2 when mix = 1
  55. inline f32x4 gamma_accurate_lerp4(f32x4 v1, f32x4 v2, float mix)
  56. {
  57. return linear_to_gamma4(gamma_to_linear4(v1) * (1 - mix) + gamma_to_linear4(v2) * mix);
  58. }
  59. # endif
  60. // Transform scalar from gamma2.2 space to linear space
  61. // Assumes x is in range [0, 1]
  62. constexpr float gamma_to_linear(float x)
  63. {
  64. return (0.8f + 0.2f * x) * x * x;
  65. }
  66. // Transform scalar from linear space to gamma2.2 space
  67. // Assumes x is in range [0, 1]
  68. inline float linear_to_gamma(float x)
  69. {
  70. // Source for approximation: https://mimosa-pudica.net/fast-gamma/
  71. constexpr float a = 0.00279491;
  72. constexpr float b = 1.15907984;
  73. float c = (b / sqrtf(1 + a)) - 1;
  74. return ((b / __builtin_sqrtf(x + a)) - c) * x;
  75. }
  76. // Linearize v1 and v2, lerp them by mix factor, then convert back.
  77. // The output is entirely v1 when mix = 0 and entirely v2 when mix = 1
  78. inline float gamma_accurate_lerp(float v1, float v2, float mix)
  79. {
  80. return linear_to_gamma(gamma_to_linear(v1) * (1 - mix) + gamma_to_linear(v2) * mix);
  81. }
  82. // Convert a and b to linear space, blend them by mix factor, then convert back.
  83. // The output is entirely a when mix = 0 and entirely b when mix = 1
  84. inline Color gamma_accurate_blend(Color a, Color b, float mix)
  85. {
  86. # ifdef __SSE__
  87. f32x4 ac = {
  88. (float)a.red(),
  89. (float)a.green(),
  90. (float)a.blue(),
  91. };
  92. f32x4 bc = {
  93. (float)b.red(),
  94. (float)b.green(),
  95. (float)b.blue(),
  96. };
  97. f32x4 out = 255.f * gamma_accurate_lerp4(ac * (1.f / 255.f), bc * (1.f / 255.f), mix);
  98. return Color(out[0], out[1], out[2]);
  99. # else
  100. return {
  101. static_cast<u8>(255.f * gamma_accurate_lerp(a.red() / 255.f, b.red() / 255.f, mix)),
  102. static_cast<u8>(255.f * gamma_accurate_lerp(a.green() / 255.f, b.green() / 255.f, mix)),
  103. static_cast<u8>(255.f * gamma_accurate_lerp(a.blue() / 255.f, b.blue() / 255.f, mix)),
  104. };
  105. # endif
  106. }
  107. #endif
  108. }