# Brightness and Contrast

One of the most popular bitmap tone correction tasks is the adjusting of brightness and contrast. The ability to edit brightness and contrast is available in most imaging applications. Professional photo retouchers do not like these algorithms for being too "rough", and prefer using levels and curves correction (described below), but inexperienced users tent to prefer the simplicity of brightness and contrast editing.

Before describing how to use brightness and contrast algorithms in Graphics Mill, let's provide some definitions.

## Definitions

Pixel intensity is the value which characterizes the "lightness" of the pixel. For RGB values the intensity can be interpreted as the mean value of its channels.

Bitmap brightness is the degree of pixel intensity. For brevity let's assume that we are working with a grayscale bitmap. If most pixels of this bitmap have small values (e.g. close to 0), the bitmap is called dark. Vice versa, if most pixels have large values (e.g. close to the maximum value - 255 for 8-bit images, and 65355 for 16-bit images), the bitmap is called bright.

Bitmap contrast is the degree of scattering between pixel values, and can be determined as the difference between the brightest and the darkest pixels of the bitmap. When the image has low contrast it looks faded and the details are hardly perceptible. Alternately, high-contrast images are sharp and details are easy to perceive.

## Brightness

To modify the brightness of the bitmap, the ColorAdjustmentProvider provides the Brightness(Single) method (the appropriate transform class is Brightness). It has a single parameter - a brightness amount. It is a float value in range [-1, 1]. If it is more than 0, the image is brightened (amount = 1 will make the image completely white). If it is less than 0, the image is darkened (amount = -1 will make the image completely black). A zero value will cause no changes.

Here is a code example which demonstrates how to use it:

C#
bitmap.ColorAdjustment.Brightness(0.2f);


Here is the same demonstration using the Brightness transform:

C#
using (var bitmap = new Bitmap(@"Images\in.jpg"))
using (var brightness = new Brightness())
{
brightness.Auto = false;
brightness.Amount = 0.2f;

using (var newbitmap = brightness.Apply(bitmap))
newbitmap.Save(@"Images\Output\out.jpg");
}


Graphics Mill can also correct brightness in automatic mode by simply using the AutoBrightness() method of the ColorAdjustmentProvider class. Alternatively, you can set the Auto property of the Brightness transform as true. The syntax is demonstrated here:

C#
bitmap.ColorAdjustment.AutoBrightness();


Here is the same task using the Brightness transform:

C#
using (var bitmap = new Bitmap(@"Images\in.jpg"))
using (var brightness = new Brightness())
{
brightness.Auto = true;

using (var newbitmap = brightness.Apply(bitmap))
newbitmap.Save(@"Images\Output\out.jpg");
}


The Apply(Bitmap) method produces a transformed copy of a given bitmap, but the source bitmap remains unchanged. You can use the Brightness class to calculate the brightness amount parameter with the CalculateAuto(Bitmap) method without applying the transform itself. This can be useful when you want to suggest some initial parameter to the user.

## Contrast

To modify the contrast you can use the Contrast(Single) method of the ColorAdjustmentProvider (or its analogue - Contrast transform). This method is very similar to brightness: it also has a single parameter - amount, which is also a value in the range [-1, 1]. Positive values mean that the contrast should be increased (value = 1 will set the image contrast to the maximum, i.e. all pixels will have either maximum or minimum intensity, without middle values). Negative values mean that the contrast will be decreased (value = -1 will make the image completely gray, i.e. all pixels will have the same value). A zero value will cause no changes.

Automatic mode works in a similar way, including the automatic calculation of the most appropriate contrast amount.

Here you can see a code example for the contrast:

C#
bitmap.ColorAdjustment.Contrast(0.2f);


Here is the same using the Contrast transform:

C#
using (var bitmap = new Bitmap(@"Images\in.jpg"))
using (var contrast = new Contrast())
{
contrast.Auto = false;
contrast.Amount = 0.2f;

using (var newbitmap = contrast.Apply(bitmap))
newbitmap.Save(@"Images\Output\out.jpg");
}


Automatic mode:

C#
bitmap.ColorAdjustment.AutoContrast();


Here is the same using the Contrast transform:

C#
using (var bitmap = new Bitmap(@"Images\in.jpg"))
using (var contrast = new Contrast())
{
contrast.Auto = true;

using (var newbitmap = contrast.Apply(bitmap))
newbitmap.Save(@"Images\Output\out.jpg");
}


## Combined Brightness and Contrast

Typically both brightness and contrast should be modified simultaneously. Graphics Mill provides the combined method BrightnessContrast(Single, Single) (and the transform class BrightnessContrast) that takes two parameters - brightness and contrast amounts. These are the same as described above.

We recommend that you use the combined method when both brightness and contrast should be modified, rather than the serial calls of the previous ones. It is more convenient in practice and works slightly faster.

The code samples for the combined brightness/contrast method are demonstrated below:

C#
bitmap.ColorAdjustment.BrightnessContrast(0.9f, 0f);


Here is the same using the BrightnessContrast transform:

C#
using (var bitmap = new Bitmap(@"Images\in.jpg"))
using (var brightnessContrast = new BrightnessContrast())
{
brightnessContrast.Brightness = 0.2f;
brightnessContrast.Contrast = 0.2f;

using (var newbitmap = brightnessContrast.Apply(bitmap))
newbitmap.Save(@"Images\Output\out.jpg");
}