Image Processing 6. Brightness, saturation, contrast,, gamma correction
2 min read June 15, 2025 #image processing #javascriptImage Processing 1. Introduction
Image Processing 2. Simple transformation
Image Processing 3. Negate filter, channel extraction and inversion
Image Processing 4. Desaturation
Image Processing 5. Color Models
Image Processing 6. Brightness, saturation, contrast,, gamma correction
Let's examine the most fundamental image correction parameters: brightness, saturation (without switching to HSV color model), contrast, and gamma.
Brightness#
We've already covered this, but let's briefly revisit it.
Saturation#
Adjusting saturation doesn't require to switch to the HSV color model, modify the values, and switch back to RGB again. You can achieve it through simple calculation of the difference between color components and the gray value:
for ; i < dst.length; i++
In the example code, I changed value / 255
to value / max
, where max = (value < 0) ? 255 : 128
. This was done solely to increase maximum saturation. Try both options, or reduce 128 and set saturation to the maximum. Unlike switching to HSV, this method provides more precision and smoothness. Photoshop has a Vibrance adjustment that does the same thing. Therefore, I don't recommend using Hue/Saturation in Photoshop solely for changing saturation.
Contrast#
Contrast represents the difference between the maximum and minimum brightness in an image. Look again at the color component calculation formula from the saturation example:
;
r += r - gray * value / 255;
g += g - gray * value / 255;
b += b - gray * value / 255;
What happens if we calculate the gray value for the entire image instead of a single pixel?
;
for ; i < dst.length; i++
gray /= dst.length;
for ; i < dst.length; i++
We get contrast adjustment!
Gamma Correction#
Gamma defines the relationship between a pixel's numerical brightness value and its actual perceived brightness. Our vision perceives brightness non-linearly and is more sensitive to dark tones than bright ones. Conversely, digital camera sensors perceive all brightness linearly.
Applying gamma correction allows us to transition from the digital representation of brightness to human-perceived brightness, and vice versa.
Typically, a simple power function is used for gamma correction:
; // 0.1 ~ 6.0
;
;
for ; i < 256; i++
for ; i < dst.length; i++
Since each pixel component has only 256 possible brightness values, a lookup table can be created for faster processing.
Thanks to gamma correction, previously unnoticeable details can appear in dark photos.