[Qt-interest] QImage and ARGB32_Premultiplied

Joshua Grauman jnfo-c at grauman.com
Mon Jan 31 02:03:59 CET 2011


>> Thanks. That's what I figured, but I guess my real question then is how to
>> set an alpha channel for a ARGB32_Premultiplied QImage?
>
> You should check out composition modes (http://doc.qt.nokia.com/4.7/qpainter.html#composition-modes). They allow you to modify a particular color element of the image you're drawing to, and they're generally optimized.
> In your case you set the painter's composition mode to QPainter::CompositionMode_DestinationIn which would modify only the alpha-channel of the target image, and then fill it with a solid-color rect that has your alpha value.
>
>
>  qreal wantedAlpha = 0.5;
>  QPainter painter(image);
>  painter.setCompositionMode(QPainter::CompositionMode_DestionationIn);
>  painter.fillRect(QRect(0, 0, image2.width(), image2.height()), QColor(0, 0, 0, wantedAlpha * 256);
>

Thanks again so much. I tried this as well. The problem with this is that 
it doesn't allow you to *set* the alpha, but only to reduce it... This 
doesn't allow me to fade the alpha to arbitrary values and back. That's 
why I was looking for a method to *set* all the alpha values of a 
ARGB32_Premultiplied QImage... I guess maybe I just have to read and set 
all the pixels in this case. Thanks again.

Josh



More information about the Qt-interest-old mailing list