Gamma.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. constexpr f32x4 gamma_to_linear4(f32x4 x)
  36. {
  37. return (0.8f + 0.2f * x) * x * x;
  38. }
  39. // Transform f32x4 from linear space to gamma2.2 space
  40. // Assumes x is in range [0, 1]
  41. inline f32x4 linear_to_gamma4(f32x4 x)
  42. {
  43. // Source for approximation: https://mimosa-pudica.net/fast-gamma/
  44. constexpr float a = 0.00279491f;
  45. constexpr float b = 1.15907984f;
  46. float c = (b * AK::rsqrt(1.0f + a)) - 1;
  47. return ((b * AK::SIMD::rsqrt(x + a)) - c) * x;
  48. }
  49. // Linearize v1 and v2, lerp them by mix factor, then convert back.
  50. // The output is entirely v1 when mix = 0 and entirely v2 when mix = 1
  51. inline f32x4 gamma_accurate_lerp4(f32x4 v1, f32x4 v2, float mix)
  52. {
  53. return linear_to_gamma4(gamma_to_linear4(v1) * (1 - mix) + gamma_to_linear4(v2) * mix);
  54. }
  55. #endif
  56. // Transform scalar from gamma2.2 space to linear space
  57. // Assumes x is in range [0, 1]
  58. constexpr float gamma_to_linear(float x)
  59. {
  60. return (0.8f + 0.2f * x) * x * x;
  61. }
  62. // Transform scalar from linear space to gamma2.2 space
  63. // Assumes x is in range [0, 1]
  64. inline float linear_to_gamma(float x)
  65. {
  66. // Source for approximation: https://mimosa-pudica.net/fast-gamma/
  67. constexpr float a = 0.00279491;
  68. constexpr float b = 1.15907984;
  69. float c = (b * AK::rsqrt(1 + a)) - 1;
  70. return ((b * AK::rsqrt(x + a)) - c) * x;
  71. }
  72. // Linearize v1 and v2, lerp them by mix factor, then convert back.
  73. // The output is entirely v1 when mix = 0 and entirely v2 when mix = 1
  74. inline float gamma_accurate_lerp(float v1, float v2, float mix)
  75. {
  76. return linear_to_gamma(gamma_to_linear(v1) * (1 - mix) + gamma_to_linear(v2) * mix);
  77. }
  78. // Convert a and b to linear space, blend them by mix factor, then convert back.
  79. // The output is entirely a when mix = 0 and entirely b when mix = 1
  80. inline Color gamma_accurate_blend(Color a, Color b, float mix)
  81. {
  82. #ifdef __SSE__
  83. f32x4 ac = {
  84. (float)a.red(),
  85. (float)a.green(),
  86. (float)a.blue(),
  87. };
  88. f32x4 bc = {
  89. (float)b.red(),
  90. (float)b.green(),
  91. (float)b.blue(),
  92. };
  93. f32x4 out = 255.f * gamma_accurate_lerp4(ac * (1.f / 255.f), bc * (1.f / 255.f), mix);
  94. return Color(out[0], out[1], out[2]);
  95. #else
  96. return {
  97. static_cast<u8>(255.f * gamma_accurate_lerp(a.red() / 255.f, b.red() / 255.f, mix)),
  98. static_cast<u8>(255.f * gamma_accurate_lerp(a.green() / 255.f, b.green() / 255.f, mix)),
  99. static_cast<u8>(255.f * gamma_accurate_lerp(a.blue() / 255.f, b.blue() / 255.f, mix)),
  100. };
  101. #endif
  102. }
  103. }