Gamma: Difference between revisions

From vegard.wiki
Jump to navigation Jump to search
Content added Content deleted
(new page)
 
(add link)
 
(5 intermediate revisions by the same user not shown)
Line 3: Line 3:
* sRGB = gamma space
* sRGB = gamma space
* most image files store sRGB
* most image files store sRGB
* sRGB is unfit for blending
* sRGB is unfit for blending, blurring, etc.


The corollary is:
The corollary is:
* Image files loaded as textures should be converted from sRGB to RGB (but some decoders do this for you!)
* Image files loaded as textures should be converted from sRGB to RGB (but some decoders do this for you!)
* Since alpha is linear, [[Premultiplied alpha|alpha premultiplication]] should happen in linear space, i.e. after converting to RGB [http://ssp.impulsetrain.com/gamma-premult.html]
* The shader responsible for putting the final pixels on the screen should have a gamma correction step doing:
* The shader responsible for putting the final pixels on the screen should have a gamma correction step doing:
:<source lang="GLSL">
:<source lang="GLSL">
Line 20: Line 21:
}
}
</source>
</source>

Note, however, that OpenGL may also perform this step for you on any on-screen framebuffers (i.e. the default framebuffer) if you do <code>glEnable(GL_FRAMEBUFFER_SRGB)</code>.


== See also ==
== See also ==
* https://blog.johnnovak.net/2016/09/21/what-every-coder-should-know-about-gamma/
* https://blog.johnnovak.net/2016/09/21/what-every-coder-should-know-about-gamma/
* https://stackoverflow.com/questions/23026151/do-i-need-to-gamma-correct-the-final-color-output-on-a-modern-computer-monitor
* https://www.khronos.org/opengl/wiki/Framebuffer#Colorspace
* https://en.wikipedia.org/wiki/Gamma_correction
* https://en.wikipedia.org/wiki/Gamma_correction
* https://www.cambridgeincolour.com/tutorials/gamma-correction.htm
* https://www.cambridgeincolour.com/tutorials/gamma-correction.htm
* http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
* https://unlimited3d.wordpress.com/2020/01/08/srgb-color-space-in-opengl/
* https://mynameismjp.wordpress.com/2012/10/24/msaa-overview/ ("Working with HDR and Tone Mapping")
* https://iquilezles.org/www/articles/gamma/gamma.htm


[[Category:Graphics programming]]
[[Category:Graphics programming]]

Latest revision as of 10:21, 28 December 2021

Quick rules of thumb, since I can never remember which is which:

  • RGB = linear space
  • sRGB = gamma space
  • most image files store sRGB
  • sRGB is unfit for blending, blurring, etc.

The corollary is:

  • Image files loaded as textures should be converted from sRGB to RGB (but some decoders do this for you!)
  • Since alpha is linear, alpha premultiplication should happen in linear space, i.e. after converting to RGB [1]
  • The shader responsible for putting the final pixels on the screen should have a gamma correction step doing:
const float gamma = 2.2;

out vec4 color;

void main()
{
    // ...

    color = pow(color, 1. / gamma);
}

Note, however, that OpenGL may also perform this step for you on any on-screen framebuffers (i.e. the default framebuffer) if you do glEnable(GL_FRAMEBUFFER_SRGB).

See also