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