# Image Processing Algorithms Part 4: Brightness Adjustment

This article was originally published in issue 55 of The Crypt Mag

Adjusting the brightness of an image is one of the easiest image processing operations that can be done. All that is involved is adding the desired change in brightness to each of the red, green and blue colour components.

In pseudo-code it would go something like this:

```   colour = GetPixelColour(x, y)
newRed   = Truncate(Red(colour)   + brightness)
newGreen = Truncate(Green(colour) + brightness)
newBlue  = Truncate(Blue(colour)  + brightness)
PutPixelColour(x, y) = RGB(newRed, newGreen, newBlue)
```

The value of brightness will usually be in the range of -255 to +255 for a 24 bit palette. Negative values will darken the image and, conversely, positive values will brighten the image.

The procedure Truncate() was previously mentioned in part 2 of this series and just ensures that the new values of red, green and blue are within the valid range. Here it is again in pseudo-code:

```   Procedure Truncate(value)
If value < 0 Then value = 0
If value > 255 Then value = 255
Return value
EndProcedure
```

Here we have the the ‘Lena’ and ‘mandrill’ images which have had the brightness adjusted by -128 (darkened) and +128 (brightened):

‘Lena’ image with brightness adjusted by -128 (left) and +128 (right)
(click images to enlarge)

‘Mandrill’ image with brightness adjusted by -128 (left) and +128 (right)

(click images to enlarge)

As you can see adjusting the brightness is really simple.

## 4 thoughts on “Image Processing Algorithms Part 4: Brightness Adjustment”

1. What about the pixel.alpha? Can’t we set the alpha to (255 – 255 * factor) factor being between 0.0 and 1.0? But in that case we will only brighten the image without affecting its contrast… What do you think?

1. The problem with using the alpha channel to brighten an image is that it is very dependent on what is behind the image (since alpha is transparency). You would need to guarantee that whatever is behind your image is white otherwise you could end up with a situation where, for example, you might end up darkening the image if behind the image is black.

You may want to consider switching to a different colourspace such as HSL where you can adjust the lightness (L) of the image without affecting the colour. Have a look here for more information: https://en.wikipedia.org/wiki/HSL_and_HSV

Kind regards,

Francis

2. Lawrence Lee says:

If I am writing a routine to change both brightness and contrast of an image (e.g., void AdjustBC(IMAGE *image, int brightness, int contrast)),
does the order of operation makes a difference?

for each pixel
pixelvalue = pixelvalue + brightness;
pixelvalue = factor*(pixelvalue-128)+128;

1. Hi Lawrence,

The order of the operations will make a difference to the final result. Typically you would adjust the contrast first and then the brightness.

There is already a function that allows you to adjust brightness, contrast and also saturation at the same time, however it is a bit more complex. Have a look at https://docs.rainmeter.net/tips/colormatrix-guide/ which documents it quite well.

Kind regards,

Francis