View Full Version : QImage setPixel() problem

21st January 2013, 15:20
I see this code in setPixel() from qimage.cpp:

case Format_ARGB32:
case Format_ARGB32_Premultiplied:
((uint *)s)[x] = index_or_rgb;

This means, that the RGB values are not premultiplied with alpha, when the pixel is set. To set the pixel values correctly, do I need to do this?

image.setPixel(x, y, qRgba(qt_div_255(color.alpha() * color.red()),
qt_div_255(color.alpha() * color.green()),
qt_div_255(color.alpha() * color.blue()),

Or when and how do the RGB values get premultiplied? In other words, how to set the pixel so it contains the correct value?

21st January 2013, 15:26
You always need to set pixel data according to the format you specified while creating/loading the image. If you want to set the pixel value in the alpha premultiplied format, you need to multiply each of the RGB components by the alpha value (ranging from 0.0 to 1.0). So the correct calculation is color.red()*color.alpha()/255.0