A quick google search showed:
adjust the brightness / contrast of a bitmap using C ++
References:
http://www.kweii.com/site/color_theory/2007_LV/BrightnessCalculation.pdf
http://www.bobpowell.net/image_contrast.htm
remember to look for similar questions before posting one :).
EDIT:
Two more links:
Image Processing Algorithms Part 5: Contrast Correction:
http://thecryptmag.com/Online/56/imgproc_5.html
Image Processing Algorithms Part 4: Brightness Control:
http://www.dfstudios.co.uk/articles/image-processing-algorithms-part-4/
EDIT:
You have an error in the second block of code that you posted:
var pixels = context.getImageData(...);
What Johannes Jendersie means is your problem:
Let's say you have a pixel with these values
R = 100; G = 210; B = 20;
And you add 100 to each color, now you have:
R = 200; G = 255;
Now you subtract the same 100:
R = 100; // same G = 155; // different!, this have to be 210!! B = 20; // same
That is why this operation is not reversible. What you can do is always have a copy of the original image, and every time you change the value, you apply a brightness correction.
Thus, the way to cancel the add operation 100 does not subtract 100, but sets the brightness correction value to 0. This is how the image editing program works, you have a slider, and while you are in the slider window, changing it you can always get the original image. if you set it to 0, but once you apply the fix, it cannot be undone when you open the slider window, now β0β is the image that was previously filtered.
Thus, you either save a backup image, or the value of brightnesCorrection somewhere, and every time the value changes, you re-apply the correction on the image or just have to accept the fact that you cannot restore the image to its former glory xD (at least not with such brightness correction, not sure if there is a better way).
Hope this helps.