Gamma Correction: Principles, Purpose, and Rendering Pipeline Application
What is Gamma Correction
Gamma correction is a technique that performs nonlinear adjustment of image brightness by applying a power function to pixel values. Its core purpose is to compensate for the nonlinear response of display devices, ensuring images are displayed with correct brightness. Gamma correction is widely used in photography, television, and computer graphics.
Why Gamma Correction Is Needed
Human perception of brightness is nonlinear: more sensitive to dark variations and less to bright ones. Early CRT monitors had an electro-optical transfer function with a similar inverse nonlinearity, resulting in a power-law relationship between input voltage and output luminance (gamma value around 2.2). To correctly reproduce linearly encoded images on the display, a reverse compensation—gamma correction—must be applied before transmission. Modern LCDs, though different in principle, emulate the same gamma response via circuitry to maintain compatibility.
If an image is displayed without gamma correction, it will appear too dark or too bright with distorted colors. Therefore, image files are typically stored with gamma encoding (like the sRGB standard) and decoded by hardware or software during display.
Mathematical Expression of Gamma Correction
Typical gamma correction formulas:
- **Encoding (linear to gamma)**: `V_out = V_in^(1/γ)`, where γ is usually 2.2.
- **Decoding (gamma to linear)**: `V_in = V_out^γ`.
For example, a linear value of 0.5 encodes to about 0.73 (0.5^(1/2.2)), and decoding restores it to 0.5. The sRGB standard uses a more precise piecewise function, but the gamma 2.2 approximation is sufficient for most cases.
Gamma Correction and the Rendering Pipeline
In real-time rendering, lighting calculations must be performed in linear space to ensure physical correctness. The rendering pipeline typically includes:
- **Texture sampling**: Color textures are stored in sRGB format and need decoding to linear space after sampling. Engines handle this automatically (e.g., marking as sRGB in Unity).
- **Lighting calculations**: Done in linear space, producing linear results.
- **Final output**: Encode the linear result back to sRGB for display devices.
- **Unity**: In Player Settings, set Color Space to Linear; the engine automatically handles texture decoding and output encoding. Ensure textures are flagged correctly: color textures as sRGB, data textures (normal, metallic) as Linear.
- **Unreal Engine**: Uses linear workflow by default; set sRGB option on texture import.
- **Notes**:
Without gamma correction, outputting linear values directly would make the image too dark; performing lighting calculations incorrectly in sRGB space would cause blending errors (e.g., darkened colors, abnormal saturation).
Engine Settings and Considerations
- Data textures should not undergo gamma correction, as it distorts numerical values.
- UI elements are usually processed directly in sRGB space, no linear conversion needed.
- Mobile platforms may not support linear rendering; assess performance vs quality.
FAQ
What is the relationship between gamma correction and sRGB?
sRGB is a standard color space that defines a specific gamma encoding curve (approximately gamma 2.2). Gamma correction is the process that converts between sRGB encoding and linear space.
Why should rendering be done in linear space?
Because lighting calculations are linear; computing in linear space ensures physical correctness and avoids color errors. Gamma correction is only applied at input/output stages.
How do I know if a texture needs gamma correction?
Color textures need gamma correction (marked as sRGB); data textures do not (marked as Linear).
What are the consequences of not doing gamma correction?
It leads to images being too dark or too bright, incorrect color blending, and affects visual quality and physical correctness.
How to handle gamma correction on mobile?
Mobile GPUs may not support linear rendering; you can manually process in shaders or accept Gamma space rendering for performance.
FAQ
What is the relationship between gamma correction and sRGB?
sRGB is a standard color space that defines a specific gamma encoding curve; gamma correction is the process that converts between sRGB encoding and linear space.
Why should rendering be done in linear space?
Because lighting calculations are linear; computing in linear space ensures physical correctness and avoids color errors.
How do I know if a texture needs gamma correction?
Color textures need gamma correction (marked as sRGB); data textures do not (marked as Linear).
What are the consequences of not doing gamma correction?
It leads to images being too dark or too bright, incorrect color blending, and affects visual quality and physical correctness.
How to handle gamma correction on mobile?
Mobile GPUs may not support linear rendering; you can manually process in shaders or accept Gamma space rendering for performance.