天天看点

使用QIamge对图片进行加亮操作

原理比较简单,就是对每个像素的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;
}
           

来试试效果:

原图:

使用QIamge对图片进行加亮操作

加亮125:

使用QIamge对图片进行加亮操作

减暗60:

使用QIamge对图片进行加亮操作

继续阅读