图像亮度为here https://web.archive.org/web/20091229041814/http://www.kweii.com/site/color_theory/2007_LV/BrightnessCalculation.pdf- 使用 RGB 值的平均值并移动它们。
对比是here https://web.archive.org/web/20140825114946/http://bobpowell.net/image_contrast.aspx也可以使用其他语言的解决方案。
编辑以防上述链接失效:
下面 Jerry Coffin 给出的答案涵盖了相同的主题,并且包含仍然有效的链接。
但是,要调整亮度,您需要为图像的 R、G、B 字段添加一个常量值。确保使用饱和数学 - 不允许值低于 0 或高于位深度允许的最大值(24 位颜色为 8 位)
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);
作为对比,我已经采取并稍微修改了代码这个网站 http://www.dfstudios.co.uk/articles/programming/image-programming-algorithms/image-processing-algorithms-part-5-contrast-adjustment/:
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)
确保 8 位颜色的值保持在 0 到 255 之间。请注意,许多 CPU 具有在单个周期内完成此操作的内在功能。
size_t truncate(size_t value)
{
if(value < 0) return 0;
if(value > 255) return 255;
return value;
}