什么是正确的伽玛校正函数?

jan*_*b04 5 opengl rendering colors srgb

目前,我在照明通道后使用以下公式对颜色进行伽玛校正(将它们从 RGB 转换为 sRGB 颜色空间):

output = pow(color, vec3(1.0/2.2));
Run Code Online (Sandbox Code Playgroud)

这个公式是伽玛校正的正确公式吗?我之所以这么问,是因为我遇到过一些人说不是,而且正确的公式更复杂,与 2.4 次方有关,而不是 2.2 次方。我还听说三种颜色 R、G 和 B 应该具有不同的权重(例如 0.2126、0.7152、0.0722)。

GL_FRAMEBUFFER_SRGB我也很好奇OpenGL在启用时使用哪个函数。

编辑:这是盖伊戴维森的演讲“你所知道的关于颜色的一切都是错误的”中涵盖的众多主题之一。这里介绍了伽马校正功能,但整个演讲都与色彩空间有关,包括 sRGB 和伽马校正。

gkv*_*311 11

伽马校正可能有任何值,但考虑到线性 RGB / 非线性 sRGB 转换,2.2 是一个近似值,因此您的公式可能被认为是错误的和正确的: https: //en.wikipedia.org/wiki/SRGB#Theory_of_the_transformation

真正的 sRGB 传递函数基于 2.4 gamma 系数,并且在暗值处具有不连续性,如下所示:

float Convert_sRGB_FromLinear (float theLinearValue) {
  return theLinearValue <= 0.0031308f
       ? theLinearValue * 12.92f
       : powf (theLinearValue, 1.0f/2.4f) * 1.055f - 0.055f;
}
float Convert_sRGB_ToLinear (float thesRGBValue) {
  return thesRGBValue <= 0.04045f
       ? thesRGBValue / 12.92f
       : powf ((thesRGBValue + 0.055f) / 1.055f, 2.4f);
}
Run Code Online (Sandbox Code Playgroud)

事实上,您可能会在某些 GLSL 代码中使用 2.0 系数而不是 2.2 和 2.4 找到更粗略的近似值,这样可以避免使用昂贵的系数pow()x*xsqrt()被使用)。这是为了实现最大性能(在旧图形硬件的情况下)和代码简单性,同时牺牲色彩再现。实际上,这种牺牲并不那么明显,而且大多数游戏都应用了额外的色调映射和用户管理的伽玛校正系数,因此结果与 sRGB 标准并不直接相关。

GL_FRAMEBUFFER_SRGB纹理采样GL_SRGB8预计将使用更正确的公式(在纹理采样的情况下,它更可能是 GPU 上预先计算的查找表,而不是真正的公式,因为只有 256 个值需要转换)。例如,请参阅对GL_ARB_framebuffer_sRGB 扩展的注释:

 Given a linear RGB component, cl, convert it to an sRGB component, cs, in the range [0,1], with this pseudo-code:

     if (isnan(cl)) {
         /* Map IEEE-754 Not-a-number to zero. */
         cs = 0.0;
     } else if (cl > 1.0) {
         cs = 1.0;
     } else if (cl < 0.0) {
         cs = 0.0;
     } else if (cl < 0.0031308) {
         cs = 12.92 * cl;
     } else {
         cs = 1.055 * pow(cl, 0.41666) - 0.055;
     }

  The NaN behavior in the pseudo-code is recommended but not specified in the actual specification language.
  sRGB components are typically stored as unsigned 8-bit fixed-point values.
  If cs is computed with the above pseudo-code, cs can be converted to a [0,255] integer with this formula:

     csi = floor(255.0 * cs + 0.5)
Run Code Online (Sandbox Code Playgroud)

这是另一篇描述 OpenGL 应用程序中 sRGB 用法的文章,您可能会发现它很有用:https ://unlimited3d.wordpress.com/2020/01/08/srgb-color-space-in-opengl/