7

adjust image brightness/contrast using c++ without using any other 3rd party library or dependancy

Suriyan Suresh
  • 2,964
  • 14
  • 51
  • 80
  • Maybe you could write a software that reminds you to use the buttons on your screen ? Not sure it has to be written in `C++` though. – ereOn Jun 04 '10 at 17:10
  • homework ? add the tag. and show us what have you done till now. – leonbloy Jun 04 '10 at 17:17

2 Answers2

7

Image brightness is here - use the mean of the RGB values and shift them.

Contrast is here with other languages solutions available as well.


Edit in case the above links die:

The answer given by Jerry Coffin below covers the same topic and has links that still live.

But, to adjust brightness, you add a constant value to each for the R,G,B fields of an image. Make sure to use saturated math - don't allow values to go below 0 or above the maximum allowed in your bit-depth (8-bits for 24-bit color)

RGB_struct color = GetPixelColor(x, y);
size_t newRed   = truncate(color.red   + brightAdjust);
size_t newGreen = truncate(color.green + brightAdjust);
size_t newBlue  = truncate(color.blue  + brightAdjust);

For contrast, I have taken and slightly modified code from this website:

float factor = (259.0 * (contrast + 255.0)) / (255.0 * (259.0 - contrast));
RGB_struct color = GetPixelColor(x, y);
size_t newRed   = truncate((size_t)(factor * (color.red   - 128) + 128));
size_t newGreen = truncate((size_t)(factor * (color.green - 128) + 128));
size_t newBlue  = truncate((size_t)(factor * (color.blue  - 128) + 128));

Where truncate(int value) makes sure the value stays between 0 and 255 for 8-bit color. Note that many CPUs have intrinsic functions to do this in a single cycle.

size_t truncate(size_t value)
{
    if(value < 0) return 0;
    if(value > 255) return 255;

    return value;
}
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Michael Dorgan
  • 12,453
  • 3
  • 31
  • 61
  • 3
    contrast code have some dependancies to GDI + dotnet framework – Suriyan Suresh Jun 05 '10 at 01:43
  • > _Image brightness is here - use the mean of the RGB values and shift them_ The very article you linked to says that this is one of the worst possible ways to change brightness. – ScumCoder Sep 06 '21 at 17:19
4

Read in the image with a library just as the Independent JPEG library. When you have raw data, you can convert it from RGB to HSL or (preferably) CIE Lab*. Both contrast and brightness will basically just involve adjustments to the L channel -- to adjust brightness, just adjust all the L values up or down by an appropriate amount. To adjust contrast, you basically adjust the difference between a particular value and the center value. You'll generally want to do this non-linearly, so values near the middle of the range are adjusted quite a bit, but values close to the ends or the range aren't affected nearly as much (and any that are at the very ends, aren't changed at all).

Once you've done that, you can convert back to RGB, and then back to a normal format such as JPEG.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111