原理比較簡單,就是對每個像素的r、g、b的值加上亮度值v,然後保留alpha通道。
(注意考慮加上v之後可能小于0切大于255的情況)
這裡我就直入主題了。
//* 增加亮度
QImage AdjustBrightness(QImage image, int brightness)、
{
QImage origin = image;
QColor oldColor;
int delta = brightness;
int r=0,g=0,b=0,a=0;
uchar *line =image.scanLine(0);
uchar *pixel = line;
QImage newImage = QImage(origin.width(), origin.height(), QImage::Format_ARGB32);
for(int y=0; y<newImage.height(); ++y)
{
for(int x=0; x<newImage.width(); ++x)
{
oldColor = QColor(image.pixel(x,y));
r = oldColor.red() + brightness;
g = oldColor.green() + brightness;
b = oldColor.blue() + brightness;
r = r>255?255:r;
g = g>255?255:g;
b = b>255?255:b;
r = r < 0 ? 0 : r;
g = g < 0 ? 0 : g;
b = b < 0 ? 0 : b;
a = qAlpha(image.pixel(x,y));
newImage.setPixel(x,y, qRgba(r,g,b,a));
}
}
return newImage;
}
來試試效果:
原圖:
加亮125:
減暗60: