在计算机图形学领域时常听到gamma correction ,gamma correction 控制了图像整体的亮度,reproduce colors也需要gamma correction的一些理论知识,gamma correction不仅仅是控制了图像的亮度,而且还控制了RGB各个分量的比例,我们知道渲染器是线性的,而显示器并非线性,其实电子打在屏幕上从而产生亮点,电子的运动受电压控制,这两者是指数关系的,所以产生的亮度也跟电压成指数关系,而发送给显示器的voltages范围是0~1:
对于我们输入的图像,如果直接显示,那么就会篇暗,根据已知电压与显示亮度的关系,进行gamma correction ,其实就是对gamma曲线的修正。一般生产厂家不加说明,他们的伽码值大约等于2.5.
代码:
gammaCorrection = 1 / gamma
colour = GetPixelColour(x, y)
newRed = 255 * (Red(colour) / 255) ^ gammaCorrection
newGreen = 255 * (Green(colour) / 255) ^ gammaCorrection
newBlue = 255 * (Blue(colour) / 255) ^ gammaCorrection
PutPixelColour(x, y) = RGB(newRed, newGreen, newBlue)
知道monitor不是一个线性的,那么我们在进行颜色加法时,我们得到的颜色并不是真正的颜色值的相加,比如gamma factor是2.2
red = add (r1, r2);
red= add (0.235,0.156);
对于一个线性设备,red = 0.391,对于未经修正的montior red=0.126;
因为有一个幂函数的运算:C_out = C_in2.2
现在使用gamma correction :C_corrected= C_out1.0/2.2
0.3912.2 = 0.126
0.1261.0/2.2 = 0.39
我们看到使用伽码校正以后我们能得到我们预想的颜色值0.39.
There are two ways to do gamma correction:
- Using the renderer. The renderer (the graphics card or GPU) is a linear device. Modern renderers have the support of gamma correction via sRGB textures and framebuffer formats. See the following OpenGL extensions for more details: GL_ARB_framebuffer_sRGB and GL_EXT_texture_sRGB. With these extensions you can get gamma corrected values for free but gamma correction factor is set to 2.2. You can’t change it.
- Using a software gamma correction. The gamma correction is applied to the final scene buffer thanks to apixel shader and you can set the gamma correction you want.
In OpenGL, using GL_ARB_framebuffer_sRGB is really simple: once your FBO is bound, just enable the sRGB space with
glEnable(GL_FRAMEBUFFER_SRGB);
gamma-correction